<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Valentin Wüstholz on Medium]]></title>
        <description><![CDATA[Stories by Valentin Wüstholz on Medium]]></description>
        <link>https://medium.com/@wuestholz?source=rss-4e81a7415404------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*6_8-lVYBdtqcQTpxkKa9gw.png</url>
            <title>Stories by Valentin Wüstholz on Medium</title>
            <link>https://medium.com/@wuestholz?source=rss-4e81a7415404------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 03 Dec 2026 00:56:26 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@wuestholz/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Checking Custom Correctness Properties of Smart Contracts Using MythX]]></title>
            <link>https://medium.com/consensys-diligence/checking-custom-correctness-properties-of-smart-contracts-using-mythx-25cbac5d7852?source=rss-4e81a7415404------2</link>
            <guid isPermaLink="false">https://medium.com/p/25cbac5d7852</guid>
            <category><![CDATA[correctness]]></category>
            <category><![CDATA[program-analysis]]></category>
            <category><![CDATA[ethereum]]></category>
            <category><![CDATA[testing]]></category>
            <category><![CDATA[security]]></category>
            <dc:creator><![CDATA[Valentin Wüstholz]]></dc:creator>
            <pubDate>Tue, 08 Oct 2019 08:27:23 GMT</pubDate>
            <atom:updated>2019-10-08T08:27:23.230Z</atom:updated>
            <content:encoded><![CDATA[<p>Previous attacks on smart contracts, such as the DAO attack and the Parity attacks, have demonstrated that it is incredibly challenging to write correct and secure contracts. Given how much money and reputation is on the line, developers need to use the complete arsenal of techniques at their disposal to eradicate bugs in contracts <em>before deployment</em>. Luckily, automated analysis tools are constantly improving and new techniques are being developed to detect bugs more reliably and, in some cases, prove their absence. This also holds for the tools that power our <a href="https://mythx.io/">MythX smart contract analysis</a> service, such as <a href="https://github.com/ConsenSys/mythril">Mythril</a> and <a href="https://arxiv.org/pdf/1905.06944.pdf">Harvey</a>. In this post, we will demonstrate how to use such tools more effectively to catch intricate bugs in contracts.</p><p>Imagine you were developing the following smart contract:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bfebaa046391ea6b2e02486a2b40db62/href">https://medium.com/media/bfebaa046391ea6b2e02486a2b40db62/href</a></iframe><p>The contract implements a simple token for storing and transferring assets between users. It is written in the Solidity programming language and conforms to the widely-used ERC20 API.</p><p>During development, you obviously made sure to address all warnings or issues that were reported by the compiler. On top of that, you also ran our MythX analysis service and addressed all the issues it detected.</p><p>Since there are no more issues, the contract should be ready for deployment, right? Well, not quite… How can you be sure it actually does <em>what it is supposed to do</em>? There are a few common techniques to answer that question: for instance, you might want to write tests that exercise different usage scenarios or even hire auditors (such as our friends at <a href="https://diligence.consensys.net/">ConsenSys Diligence</a>) to make sure you did not miss something.</p><p>Most automated tools try to detect generic issues, such as reentancy or arithmetic overflows. However, to check custom properties about the intended behavior of a specific contract (often referred to as “functional correctness properties”), tools need some help. For instance, it would be great if the developer or an auditor could provide some description of what is considered “intended behavior” and the tool would report any unintended behavior as issues. If you are using MythX you can do this out-of-the-box, provided you have some properties in mind that you want to check.</p><p>For example, the transfer function should never change the sum of the two balances for msg.sender and _to. We can easily add a runtime check for such a property by adding the transferEnsures modifier to the transfer function:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6019d6d05cc5d7993224dfde5768afd0/href">https://medium.com/media/6019d6d05cc5d7993224dfde5768afd0/href</a></iframe><p>We can see that the modifier stores the sum before entering the body of the transfer function in a local variable. After executing the body (_ placeholder) it checks the above property by emitting a special AssertionFailed(string) event in case it is violated. MythX will report paths to the AssertionFailed(string) event as a vulnerability. While MythX will also report violations of built-in assert-statements, the above approach allows you to specify a nice message that helps with understanding the tool’s warnings and, if necessary, mapping them to locations in the source code.</p><p>After making these minor changes to the contract, we can perform another MythX scan:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BWlIsNLbCv_n7Qv9ShKVIQ.png" /><figcaption>Output of running MythX on the contract (with custom property check)</figcaption></figure><p>It turns out that under certain conditions the property is violated! If we inspect the transaction data (see the <a href="https://medium.com/consensys-diligence/sabre-mythx-development-update-transaction-traces-deep-analysis-and-report-dashboard-7b81c53c8ad1">transaction sequence</a> at the bottom of the above screenshot) we can see that the property only fails if one transfers tokens to oneself (msg.sender == _to). Such a case is easily missed when manually writing test cases. However, automated analysis tools are able to explore it and create a runnable test case for you to inspect.</p><p>In this post we have demonstrated how we can get more out of our automated analysis tools by adding custom check for functional correctness properties via the AssertionFailed(string) event. Such checks are able to reveal more contract-specific issues than the built-in checks for arithmetic overflows or reentrancy.</p><p>To catch even more bugs make sure to give our <a href="https://mythx.io/plans"><em>MythX full mode analysis</em></a> a try! It runs a much deeper analysis by exploring more program paths and longer transaction sequences for up to 30 minutes (as opposed to 2 minutes for our quick mode).</p><p><em>Thanks to Bernhard Mueller and Nathan Peercy for feedback on drafts of this article.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=25cbac5d7852" width="1" height="1" alt=""><hr><p><a href="https://medium.com/consensys-diligence/checking-custom-correctness-properties-of-smart-contracts-using-mythx-25cbac5d7852">Checking Custom Correctness Properties of Smart Contracts Using MythX</a> was originally published in <a href="https://medium.com/consensys-diligence">Consensys Diligence</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Detecting Reentrancy Issues in Smart Contracts Using Fuzzing]]></title>
            <link>https://medium.com/consensys-diligence/detecting-reentrancy-issues-in-smart-contracts-using-fuzzing-e81474ba3a2e?source=rss-4e81a7415404------2</link>
            <guid isPermaLink="false">https://medium.com/p/e81474ba3a2e</guid>
            <category><![CDATA[program-analysis]]></category>
            <category><![CDATA[security]]></category>
            <category><![CDATA[testing]]></category>
            <category><![CDATA[ethereum]]></category>
            <category><![CDATA[fuzzing]]></category>
            <dc:creator><![CDATA[Valentin Wüstholz]]></dc:creator>
            <pubDate>Mon, 06 May 2019 19:48:02 GMT</pubDate>
            <atom:updated>2019-05-07T05:37:43.554Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qX_yMVPI5KqcoxPw_-srbw.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/photos/OfYuB4Aby1M?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Asael Peña</a> on <a href="https://unsplash.com/search/photos/asael-pena?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>In previous posts, we <a href="https://medium.com/consensys-diligence/finding-vulnerabilities-in-smart-contracts-175c56affe2">introduced Harvey</a>, a fuzzer for Ethereum smart contracts, and presented two techniques to boost its effectiveness: <a href="https://medium.com/consensys-diligence/fuzzing-smart-contracts-using-input-prediction-29b30ba8055c">input prediction</a> and <a href="https://medium.com/consensys-diligence/fuzzing-smart-contracts-using-multiple-transactions-51471e4b3c69">multi-transaction fuzzing</a>.</p><p><em>Harvey is being developed by </em><a href="https://consensys.net/diligence"><em>ConsenSys Diligence</em></a><em> in collaboration with </em><a href="https://mariachris.github.io/"><em>Maria Christakis</em></a><em> from MPI-SWS. It is one of the tools that powers the </em><a href="https://mythx.io/"><em>MythX platform</em></a><em>. Sign up for </em><a href="https://medium.com/consensys-diligence/mythx-is-real-f3fafd86685e"><em>our beta</em></a><em> to give it a try!</em></p><p>Starting with the <a href="https://www.coindesk.com/understanding-dao-hack-journalists">DAO attack</a>, reentrancy issues have been exploited several times over the last few years to hack smart contracts. In previous posts, we saw examples of how we can use Harvey to detect assertion violations in smart contracts.</p><p>In this post, we will explain how Harvey is able to detect other issues and in particular reentrancy issues.</p><h3>Motivating Example</h3><p>Let’s look at the following smart contract (written in the Solidity programming language) to illustrate two reentrancy issues.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d8171f6d67f07243eb4477deb3a06a4d/href">https://medium.com/media/d8171f6d67f07243eb4477deb3a06a4d/href</a></iframe><p>The contract implements functionality for depositing (function Deposit) and withdrawing (functions WithdrawBuggy and WithdrawVeryBuggy) crypto assets (ether in our case). Many types of smart contracts, such as wallets and tokens, also implement such functionality. The contract manages balances of users by storing them in a map balance.</p><p>When withdrawing the assets in function WithdrawVeryBuggy, the contract calls the sender of the transaction (i.e., the “owner” of the assets) on Line 13 in order to transfer the assets. Such external calls are very common in many contracts, but may lead to subtle issues if a call re-enters the calling contract. In our example, assuming the callee (i.e., owner being called) is another contract, it could have been programmed by an attacker to call WithdrawVeryBuggy again. This is possible since the owner’s balance is only updated <em>after</em> the call and, eventually, could allow the attacker to drain the contract’s assets. A similar scenario was exploited in the attack on the DAO contract.</p><h3>Preventing state updates after external calls</h3><p>To detect such issues, Harvey warns the user about contracts that update the persistent state after external calls. It does so by monitoring the execution of every test input that is generated by the fuzzer. Such a runtime monitor records detected issues on-the-fly and one can easily create new detection components using a custom tracer that is hooked into the <a href="https://github.com/ethereum/go-ethereum/blob/9b3601cfce4d61cd303f5e243813fa89426259d4/core/vm/logger.go#L100">tracing component</a> of the underlying Ethereum virtual machine (EVM) implementation (go-ethereum in our case).</p><p>Harvey emits a warning (including a <em>runnable test input</em>) if it is able to generate a successful transaction that updates the persistent state (storage or balances) after an external call. Note that, in general, it is very difficult to automatically generate a concrete exploit for vulnerable contracts since one would need to synthesize the code of the callee contract (for example, to call back into a specific function with specific arguments under specific circumstances).</p><p>To prevent this issue, the developer could move the balance update before the external call.</p><h3>Preventing state reads after external calls</h3><p>Function WithdrawBuggy demonstrates this, but contains another subtle flaw. After the external call, the code emits an event if the balance was decreased. At least that seems to be the intention.</p><p>However, in our example, this event could also be triggered if the balance was increased since the callee could trigger a call-back by invoking Deposit. The reason for this is that there is a read of the persistent state after the external call. A developer might wrongly assume that the state was not changed during the external call.</p><p>Even though this issue may seem less critical, it constitutes a flaw in the contract’s business logic that should be fixed or at the very least reviewed. Imagine a front-end component reacting to such wrongly emitted events (for example, by suggesting donations to the user whose balance supposedly decreased).</p><p>Harvey will warn developers about this potential issue if it is able to generate a transaction that performs a read operation on the persistent state of a contract (storage and balance) after an external call. Like for state writes, Harvey is able to detect this issue by directly monitoring the execution of the EVM.</p><p>One way to prevent this issue is by reading the state right before the external call. This tends to work well in cases where developers implicitly assume that external calls do not modify the contract’s state. However, in general, a fix should be in line with the developer’s intention. For instance, in our example, the developer might decide that call-backs should be prevented in the first place or that events should only be emitted within the outermost call (and not within call-backs).</p><h3>Detecting other issues</h3><p>Harvey also uses runtime monitoring to detect other types of issues in smart contracts (currently <a href="https://smartcontractsecurity.github.io/SWC-registry/docs/SWC-101">SWC-101</a>, <a href="https://smartcontractsecurity.github.io/SWC-registry/docs/SWC-104">SWC-104</a>, <a href="https://smartcontractsecurity.github.io/SWC-registry/docs/SWC-107">SWC-107</a>, <a href="https://smartcontractsecurity.github.io/SWC-registry/docs/SWC-110">SWC-110</a>, <a href="https://smartcontractsecurity.github.io/SWC-registry/docs/SWC-123">SWC-123</a>, <a href="https://smartcontractsecurity.github.io/SWC-registry/docs/SWC-124">SWC-124</a>, and <a href="https://smartcontractsecurity.github.io/SWC-registry/docs/SWC-127">SWC-127</a>), and can be easily extended by writing custom detection monitors. A later post might describe how to easily write such a runtime monitor in Go.</p><p>In this post, we have illustrated how reentrancy issues can make smart contracts vulnerable to attacks and how Harvey is able to detect them using fuzzing and runtime monitoring of concrete executions of a contract.</p><p>In the next post of this series, we will look at how to use Harvey to analyze systems with several contracts. Stay tuned!</p><p>In the meanwhile, sign up for <a href="https://medium.com/consensys-diligence/mythx-is-real-f3fafd86685e">our beta</a> if you want to try Harvey out yourself!</p><p><em>Thanks to </em><a href="https://mariachris.github.io/"><em>Maria Christakis</em></a><em>, </em><a href="https://twitter.com/joranhonig"><em>Joran Honig</em></a><em>, John Mardlin (aka </em><a href="https://twitter.com/maurelian_"><em>Maurelian</em></a><em>), Mike Pumphrey, and </em><a href="https://twitter.com/g3rh4rdw4gn3r"><em>Gerhard Wagner</em></a><em> for feedback on drafts of this article.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e81474ba3a2e" width="1" height="1" alt=""><hr><p><a href="https://medium.com/consensys-diligence/detecting-reentrancy-issues-in-smart-contracts-using-fuzzing-e81474ba3a2e">Detecting Reentrancy Issues in Smart Contracts Using Fuzzing</a> was originally published in <a href="https://medium.com/consensys-diligence">Consensys Diligence</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Fuzzing Smart Contracts Using Multiple Transactions]]></title>
            <link>https://medium.com/consensys-diligence/fuzzing-smart-contracts-using-multiple-transactions-51471e4b3c69?source=rss-4e81a7415404------2</link>
            <guid isPermaLink="false">https://medium.com/p/51471e4b3c69</guid>
            <category><![CDATA[fuzzing]]></category>
            <category><![CDATA[program-analysis]]></category>
            <category><![CDATA[testing]]></category>
            <category><![CDATA[security]]></category>
            <category><![CDATA[ethereum]]></category>
            <dc:creator><![CDATA[Valentin Wüstholz]]></dc:creator>
            <pubDate>Thu, 31 Jan 2019 09:54:05 GMT</pubDate>
            <atom:updated>2019-05-12T09:56:30.936Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1LAICM7Tx_tIFnA5wcy1lg.jpeg" /></figure><p>In previous posts, we <a href="https://medium.com/consensys-diligence/finding-vulnerabilities-in-smart-contracts-175c56affe2">introduced Harvey</a>, a fuzzer for Ethereum smart contracts, and presented a <a href="https://medium.com/consensys-diligence/fuzzing-smart-contracts-using-input-prediction-29b30ba8055c">novel input prediction technique</a> to improve its effectiveness.</p><p><em>Harvey is being developed by </em><a href="https://consensys.net/diligence"><em>ConsenSys Diligence</em></a><em> in collaboration with </em><a href="https://mariachris.github.io/"><em>Maria Christakis</em></a><em> from MPI-SWS. It is one of the tools that powers the </em><a href="https://mythx.io/"><em>MythX analysis platform</em></a><em>.</em></p><p>Most real-world contracts transition through many different states (e.g., one for each user bidding during an auction or betting in a game) during their lifetime and it is crucial not to focus a security analysis exclusively on the states reachable after one or very few transactions. In other words, an analysis should not ignore <em>deep vulnerabilities</em> which only manifest themselves after executing a number of transactions first.</p><p>In this article, we will look at how to detect such deep vulnerabilities in Ethereum smart contracts. More specifically, we will look at how fuzzers can generate sequences of transactions that ultimately exploit a vulnerability.</p><h3>Motivating Example</h3><p>Let’s look at the following smart contract (written in the Solidity programming language) to illustrate the challenges faced by fuzzers that want to make the assertion in function Bar fail.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/943eaee91df2dcf630de0aa9dbe921c1/href">https://medium.com/media/943eaee91df2dcf630de0aa9dbe921c1/href</a></iframe><p>The constructor initializes the (persistent) storage variable x to zero (on Line 6). Let’s assume that this is our initial state. In its initial state, it is not possible to trigger the assertion violation (on Line 12) by sending a single transaction to the contract. In fact, it requires at least three transactions invoking functions SetY(42), CopyY(), and finally Bar(). Alternatively, the same could be achieved by invoking function IncX 42 times and subsequently calling Bar. Finding the right sequence of transactions with just the right arguments can be challenging.</p><p>A simple fuzzer might instead perform additional fuzzing operations that modify the state directly; for instance, by setting variable x to 42 even though there is no setter function. However, this can lead to reporting spurious errors if there is no valid sequence of transactions that are able to change the state in such a way. For instance, there could be input validation for function SetY that makes sure that y is never set to 42 and function IncX could be changed to only increment up to 16.</p><p>In Harvey we want to avoid any such spurious errors.</p><h3>Fuzzing Sequences of Transactions Exhaustively</h3><p>To avoid spurious errors, a fuzzer might instead try to explore all possible sequences of transactions. A grey-box fuzzer could easily achieve this by including all transactions when computing the path identifier (see the <a href="https://medium.com/consensys-diligence/finding-vulnerabilities-in-smart-contracts-175c56affe2">previous post</a> for more details); essentially making the path span all transactions. However, the number of possible sequences grows exponentially in the number of transactions, let alone the number of paths. As a consequence, the test suite would likely grow very quickly and the fuzzer would find it challenging to decide which “promising” test inputs (i.e., sequences of transactions) to focus on for increasing coverage most effectively.</p><p>In our example, the only branch that cannot be covered with a single transaction is the one leading to the assertion. Therefore, a fuzzer might want to focus on sequences that end with an invocation of Bar. Similarly, there is little to be gained from exploring sequences that repeatedly call SetY.</p><h3>Demand-driven Sequence Fuzzing</h3><p>Harvey uses a number of techniques to avoid these issues. First, Harvey only adds test inputs to the test suite if they increase path coverage for the <em>final step</em> (i.e., transaction), which in our case is Bar. The intuition behind this design is to consider the steps before the final step primarily as a way to incrementally change the state of the contract such that we can explore a given path in the final step.</p><p>Second, before considering additional steps, Harvey will check if there is actually something to be gained in terms of path coverage. More specifically, Harvey is designed to periodically fuzz a selected input by allowing direct mutations of the state before the last step (similar to the simple approach described above of directly fuzzing x). Unless this more aggressive fuzzing uncovers new paths, we will not even consider inserting additional steps before the last step. In other words, Harvey only considers longer sequences <em>on demand</em>. Note that new inputs found during this more aggressive fuzzing are never added to the test suite to avoid spurious errors.</p><p>In our example, Harvey will only consider longer sequences of steps for finding the input that invokes Bar and executes the assertion. Once it finds and adds the sequence that triggers the assertion violation (after roughly 18 seconds), it will stop considering longer sequences completely (since the aggressive fuzzing can no longer increase coverage). Note that this also demonstrates how our <a href="https://medium.com/consensys-diligence/fuzzing-smart-contracts-using-input-prediction-29b30ba8055c">technique for input prediction</a> is easily applied across several transactions; it is responsible for predicting the right argument for SetY that will trigger the assertion in Bar.</p><h3>Mutation Operations</h3><p>To actually come up with new sequences, Harvey uses several types of mutation operations that are applied to the selected input. For each of them it first picks an existing step S. It then applies one of the following operations: (1) fuzzing step S (i.e., changing the inputs of the transaction), (2) inserting a new step before step S, and (3) replacing the steps before S with a sequence of new steps.</p><p>For the last two types of operations, Harvey uses both a pool of individual steps and a pool of sequences that are populated as new tests are added to the test suite. For our example, the step pool will, for instance, contain a step that invokes SetY whereas the sequence pool will eventually contain the sequence invoking SetY followed by CopyY. Picking from the pools is more efficient than building steps from scratch every time they might be needed; they can subsequently be “tweaked” by applying operations of the first type.</p><p>In this post, we have illustrated the challenges in finding deep vulnerabilities and we described a few techniques to address those challenges when fuzzing smart contracts. In practice, Harvey uses additional techniques to quickly find such vulnerabilities and we might cover some of these in later posts. Finding deep vulnerabilities is generally one of the most challenging aspects of analyzing smart contracts.</p><p>In the <a href="https://medium.com/consensys-diligence/detecting-reentrancy-issues-in-smart-contracts-using-fuzzing-e81474ba3a2e">next post</a> of this series, we will look at how we can use Harvey to automatically detect <a href="https://smartcontractsecurity.github.io/SWC-registry/docs/SWC-107">reentrancy</a> issues — a well-known source of smart contract vulnerabilities. Stay tuned!</p><p>Thanks to <a href="https://mariachris.github.io/">Maria Christakis</a>, <a href="https://twitter.com/joranhonig">Joran Honig</a>, and <a href="https://twitter.com/muellerberndt">Bernhard Mueller</a> for feedback on drafts of this article.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=51471e4b3c69" width="1" height="1" alt=""><hr><p><a href="https://medium.com/consensys-diligence/fuzzing-smart-contracts-using-multiple-transactions-51471e4b3c69">Fuzzing Smart Contracts Using Multiple Transactions</a> was originally published in <a href="https://medium.com/consensys-diligence">Consensys Diligence</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Fuzzing Smart Contracts Using Input Prediction]]></title>
            <link>https://medium.com/consensys-diligence/fuzzing-smart-contracts-using-input-prediction-29b30ba8055c?source=rss-4e81a7415404------2</link>
            <guid isPermaLink="false">https://medium.com/p/29b30ba8055c</guid>
            <category><![CDATA[testing]]></category>
            <category><![CDATA[ethereum]]></category>
            <category><![CDATA[fuzzing]]></category>
            <category><![CDATA[security]]></category>
            <category><![CDATA[program-analysis]]></category>
            <dc:creator><![CDATA[Valentin Wüstholz]]></dc:creator>
            <pubDate>Tue, 08 Jan 2019 10:02:14 GMT</pubDate>
            <atom:updated>2019-01-31T10:02:32.547Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kI8C_1KA1zDnNeBndoa1hw.jpeg" /></figure><p>In the <a href="https://medium.com/consensys-diligence/finding-vulnerabilities-in-smart-contracts-175c56affe2">previous post</a>, we discussed several approaches for automatically finding vulnerabilities in smart contracts and we introduced Harvey: a fuzzer for Ethereum smart contracts being developed by <a href="https://consensys.net/diligence/">ConsenSys Diligence: Smart contract auditing</a>, in collaboration with <a href="https://mariachris.github.io/">Maria Christakis</a> from MPI-SWS and that will be one of the tools powering our <a href="https://mythx.io/">MythX analysis platform</a>.</p><p>Grey-box fuzzers are able to achieve high code coverage even for complex code without using more expensive techniques, such as automated SMT solvers, to reason about the program. However, for many programs it can still pay off to perform some lightweight reasoning about the code to increase code coverage more quickly. In this post, we will present a novel input prediction technique for grey-box fuzzers.</p><h3>Motivating Example</h3><p>Let’s go back to the smart contract from the previous post (written in the Solidity programming language) to illustrate a limitation in state-of-the-art grey-box fuzzers.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1cbc0cf7e814600e8a8bd1102041192c/href">https://medium.com/media/1cbc0cf7e814600e8a8bd1102041192c/href</a></iframe><p>Most grey-box fuzzers will take a long time to detect the assertion violation since inputs are fuzzed randomly; the chance of picking a to be 42 is tiny. For instance, <a href="http://lcamtuf.coredump.cx/afl">AFL</a>, a state-of-the-art grey-box fuzzer, was not able to generate a test that violates the assertion within 12 hours. While there are a number of heuristics that fuzzers can employ (e.g., picking new inputs from the constants in the program), Harvey also integrates a more systematic approach that we will present below.</p><h3>Predicting New Inputs</h3><p>Let’s assume that Harvey generated an input I with a==0, b==3, and c==-3; it explores the path that returns 3 (on Line 12). We would like to predict a new input F, that will explore the then-branch of the last conditional along the execution of input I (on Line 8).</p><p>As discussed in the previous post, one could use white-box fuzzers that symbolically execute the desired path and use SMT/SAT solvers to create new test cases. However, this can be expensive and one might prefer a more lightweight approach that builds on the grey-box fuzzing algorithm introduced in the previous post.</p><p>Let’s assume Harvey generates a new input J by changing parameter a of input I to 7. Even though this new input still executes the same path, we can extract information that will allow us to predict an input that explores the desired branch.</p><p>All we need to do is measure for both inputs how far we are from “flipping” that branch (i.e., by negating the branch condition). For input I this distance is 42 since we would flip the branch if we were to increase a by 42. Similarly, for input J the distance is 35. We record these distances while running inputs which does not incur much overhead since grey-box fuzzers already perform some lightweight instrumentation to record a path identifier during execution. The distance for a given branch is computed automatically during the execution of the EVM bytecode based on the type of comparison operator that is used (e.g., == and &lt;=), and the concrete arguments to the operator (e.g., 7 and 42). In other words, we use a simple mathematical formula that determines the distance for each operator and its arguments. For instance, the distance formula for flipping to the then-branch is absoluteValue(42-a), whereas it would be 42 — a+1 if the condition of the desired branch was 42 &lt; a.</p><p>By simply comparing the distance of the given branch for both inputs, we already learn something useful: the change in distance confirms that we are on the right track by fuzzing a. Essentially, we learn that a taints the condition of the desired branch <em>without even running any expensive taint analysis</em>.</p><p>Now, to actually predict the new input, we can think of the distance as an unknown (but computable) mathematical function f(x) where x is an input parameter that was changed. In our example, parameter a was changed and we happen to know two concrete values of our distance function f: f(0)==42 and f(7)==35. The key insight is that we are now able to phrase the problem of finding the desired input as the problem of finding a value x’ such that f(x&#39;)==0.</p><p>This problem has been studied extensively and there are many different methods, such as <a href="https://en.wikipedia.org/wiki/Newton%27s_method">Newton’s method</a>. In Harvey, we use an approach that is known as the <a href="https://en.wikipedia.org/wiki/Secant_method">Secant method</a>. It works by essentially drawing a line through the two known points of the function, p1 at (0,42) and p2 at (7,35), and determining the x-coordinate of point p’ where the line intersects with the x-axis, i.e., (42,0). Harvey then uses this information to derive a new input with a==42 that will be tried next.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cxr2FEkCGV9NxfxCHmmPMg.png" /><figcaption>Known points p1 and p2 let us derive point p’ whose x-coordinate corresponds to the new predicted input.</figcaption></figure><p>Et voilà! By running the input we confirm that it indeed explores the desired branch. If we were less lucky, we could apply the same approach iteratively, for instance, if the branch condition was a*a*a==27. However, in practice, we found that this is almost never necessary.</p><p>This simple but elegant technique typically allows Harvey to <em>find vulnerabilities orders-of-magnitude faster</em>. For our example, it allows Harvey to achieve full coverage <em>within less than a second</em> whereas it takes Harvey roughly 14 seconds without input prediction. See our <a href="https://arxiv.org/pdf/1807.07875.pdf">paper</a> for more details (including an extensive evaluation on real-world smart contracts).</p><p>In the <a href="https://link.medium.com/pFqM9dQhVT">next post</a> of this series, we will look at how Harvey detects vulnerabilities that can only be triggered if an attacker issues multiple transactions. Stay tuned!</p><p>Thanks to <a href="https://mariachris.github.io/">Maria Christakis</a>, <a href="https://twitter.com/joranhonig">Joran Honig</a>, John Mardlin (aka <a href="https://twitter.com/maurelian_">Maurelian</a>), and <a href="https://twitter.com/muellerberndt">Bernhard Mueller</a> for feedback on drafts of this article.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=29b30ba8055c" width="1" height="1" alt=""><hr><p><a href="https://medium.com/consensys-diligence/fuzzing-smart-contracts-using-input-prediction-29b30ba8055c">Fuzzing Smart Contracts Using Input Prediction</a> was originally published in <a href="https://medium.com/consensys-diligence">Consensys Diligence</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Finding Vulnerabilities in Smart Contracts]]></title>
            <link>https://medium.com/consensys-diligence/finding-vulnerabilities-in-smart-contracts-175c56affe2?source=rss-4e81a7415404------2</link>
            <guid isPermaLink="false">https://medium.com/p/175c56affe2</guid>
            <category><![CDATA[security]]></category>
            <category><![CDATA[program-analysis]]></category>
            <category><![CDATA[fuzzing]]></category>
            <category><![CDATA[testing]]></category>
            <category><![CDATA[ethereum]]></category>
            <dc:creator><![CDATA[Valentin Wüstholz]]></dc:creator>
            <pubDate>Wed, 12 Dec 2018 15:42:33 GMT</pubDate>
            <atom:updated>2019-01-22T13:03:22.678Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*klCaEkAR2OMnjKed3sEwhw.jpeg" /></figure><p>Smart contracts are increasingly complex programs that often hold and manage large amounts of assets. Getting their business logic right is challenging and developers should use tools to analyze their smart contracts before deploying them.</p><p>Starting with the mother of all smart contract hacks — the infamous DAO attack — we have seen a number of high-profile hacks over the last years that resulted in tens of millions of dollars in damages. The majority of these hacks were pulled off by locating holes in smart contracts that left them vulnerable to exploitation.</p><p>This mini-series will cover various techniques for efficiently finding vulnerabilities in smart contracts. It also introduces <em>Harvey</em>: a fuzzer for Ethereum smart contracts being developed by <a href="https://consensys.net/diligence/">ConsenSys Diligence: Smart contract auditing</a>, in collaboration with <a href="https://mariachris.github.io/">Maria Christakis</a> from MPI-SWS and that will be one of the tools powering our <a href="https://mythx.io/">MythX analysis platform</a>.</p><h3>What’s a Fuzzer?</h3><p>Conceptually, fuzzers are easy to understand: they take a set of program inputs (also known as seed inputs) and generate new ones automatically. However, not all fuzzers are created equal and, thus, have different strengths and weaknesses (just one reason why the MythX platform relies on several different security analysis techniques). Typically, we distinguish fuzzers based on how much information they collect about a program.</p><p>On one end of the spectrum, we find <em>black-box fuzzers</em> that do not collect any information about the program and typically perform random mutations to an input before running the program with it. They are easy to implement and can try millions of different inputs within minutes. However, they struggle with achieving high code coverage for complex code.</p><p>On the other end of the spectrum, we find <em>white-box fuzzers</em> that symbolically execute each instruction run by a given input and use automated solvers (e.g., SMT/SAT solvers) to find new inputs to cover a new execution path. Typically, these tools are very effective at covering new paths. However, for most non-trivial programs it is infeasible to explore them all. This is known as the path explosion problem and most fuzzers use heuristic search strategies to cover “interesting paths”.</p><p>Over the last few years, a third type of fuzzer has emerged: so-called <em>grey-box fuzzers</em>. They use a light-weight program instrumentation to record the execution path for each tested input and compute its path identifier. This short identifier allows the fuzzer to determine inputs that cover new paths and, for instance, avoid fuzzing the same path over and over. Many state-of-the-art tools use sophisticated evolutionary algorithms to select which inputs to fuzz. Unlike black-box fuzzers they are able to achieve high code coverage even for complex code without using more expensive techniques, such as automated solvers, to reason about the program.</p><h3>Introducing Harvey</h3><p>Harvey is a new fuzzer for Ethereum smart contracts being developed by the ConsenSys Diligence team in collaboration with Maria Christakis from MPI-SWS.</p><p>Harvey started out as a grey-box fuzzer, but over time we added new techniques to turn it into a lighter shade of grey in order to more efficiently explore complex programs. We will cover some of these techniques in subsequent posts.</p><p>At their core, all grey-box fuzzers rely on the following basic algorithm to build up a test suite TS for a given program P starting from seed inputs S:</p><pre>TS := run_seed_inputs(P, S)<br>while (not interrupted) {<br>  i := select_input(TS)<br>  e := assign_energy(i)<br>  while (0 &lt; e) {<br>    f := fuzz_input(i)<br>    pid := run_input(P, f)<br>    if pid not in TS {<br>      TS[pid] := f<br>    }<br>    e := e - 1<br>  }<br>}</pre><p>As mentioned earlier, the fuzzer assigns each input a path identifier (PID) based on the execution path it explored. The test suite stores the corresponding input for each PID and is initialized by running all the seed inputs S.</p><p>The fuzzer can be interrupted at any point (e.g., after exceeding a user-provided time limit). Until then, it will keep fuzzing existing inputs to expand the test suite. To do so, it selects an input i (e.g., randomly or in a round-robin fashion) from the test suite and assigns it energy e. The energy specifies how many times the given input should be fuzzed. The fuzzing operation itself produces a new input f from the existing one. After running the new input, the fuzzer will know its PID and can determine if it found a new path. If so, the input is added to the test suite.</p><p>This basic algorithm can be improved easily by developing new techniques for selecting inputs to fuzz and for assigning energy to them. Harvey implements several such improvements.</p><h3><strong>Harvey in Practice</strong></h3><p>To illustrate how this works in practice let’s look at the following simple smart contract written in the Solidity programming language:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1cbc0cf7e814600e8a8bd1102041192c/href">https://medium.com/media/1cbc0cf7e814600e8a8bd1102041192c/href</a></iframe><p>Suppose we want to check if the assertion in function Bar is reachable. Instead of manually reasoning about the code, we can use Harvey to help us with this task.</p><p>Since Harvey works directly with bytecode for the Ethereum virtual machine (EVM), we need to compile the contract first. The Solidity compiler will provide us with the following snippet of bytecode that will create a contract Foo on the blockchain:</p><pre>608060405234801561001057600080fd5b5060e88061001f6000396000f300608060405260043610603e5763ffffffff7c01000000000000000000000000000000000000000000000000000000006000350416632121699a81146043575b600080fd5b348015604e57600080fd5b50605e6004356024356044356070565b60408051918252519081900360200190f35b60008282016001811215609f576003841215608d576001915060b4565b84602a1415609757fe5b6003915060b4565b602a83121560af576004915060b4565b600591505b5093925050505600a165627a7a7230582056907da61e430dcc2b1631bc3800ce597d7eb43bac887ae350e94527c7511ee70029</pre><p>This is all that Harvey needs to get started. In our context, an input consists of a sequence of transactions that each invoke functions of a smart contract. Let’s assume that our seed transaction invokes Bar with all arguments being zero.</p><p>It will explore the path that returns 4 (on Line 15) and the sequence of transactions (of length one in this case) will be added to the test suite.</p><p>For lack of other options, the fuzzer will now fuzz this transaction sequence until it finds one that explores a new path. For instance, it might eventually change input c to 100, which will explore the path that returns 5 (on Line 17).</p><p>After roughly 14 seconds (on a regular desktop machine) and trying around 30,000 transaction sequences, it will eventually generate a test that violates the assertion and achieves full path coverage. In contrast, for a black-box fuzzer this could take forever since the input space is huge! The probability of setting a to 42 is tiny (<em>1/(2²⁵⁶)</em> to be precise).</p><p>Harvey is able to achieve high coverage quickly by implementing several techniques and optimizations that go beyond traditional greybox-fuzzing. In fact, even <a href="http://lcamtuf.coredump.cx/afl">AFL</a>, a state-of-the-art grey-box fuzzer, was not able to generate such a test within 12 hours.</p><p>In subsequent posts, we will cover some more advanced techniques (e.g., <a href="https://medium.com/consensys-diligence/fuzzing-smart-contracts-using-input-prediction-29b30ba8055c">input prediction</a>) that make it possible to achieve the same <em>within less than a second</em>. Stay tuned for more!</p><p>Thanks to Rocky Bernstein, Maria Christakis, Joran Honig, Everett Muzzy, and Gerhard Wagner for feedback on drafts of this article.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=175c56affe2" width="1" height="1" alt=""><hr><p><a href="https://medium.com/consensys-diligence/finding-vulnerabilities-in-smart-contracts-175c56affe2">Finding Vulnerabilities in Smart Contracts</a> was originally published in <a href="https://medium.com/consensys-diligence">Consensys Diligence</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>