<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/">
    <channel>
        <title>SunWeb3Sec</title>
        <link>https://paragraph.com/@sunweb3sec</link>
        <description>Web3 Cybersecurity Academy</description>
        <lastBuildDate>Sat, 18 Apr 2026 14:03:37 GMT</lastBuildDate>
        <docs>https://validator.w3.org/feed/docs/rss2.html</docs>
        <generator>https://github.com/jpmonette/feed</generator>
        <language>en</language>
        <image>
            <title>SunWeb3Sec</title>
            <url>https://storage.googleapis.com/papyrus_images/e3210b0e828f4ef053d4f5a75b8d735c0f38fab4d554b6c55ebea28300e3846c.jpg</url>
            <link>https://paragraph.com/@sunweb3sec</link>
        </image>
        <copyright>All rights reserved</copyright>
        <item>
            <title><![CDATA[OnChain Transaction Debugging - Lesson 2: Warm up]]></title>
            <link>https://paragraph.com/@sunweb3sec/onchain-transaction-debugging-lesson-2-warm-up</link>
            <guid>3h8Ss2VUtJbhY89mFwq7</guid>
            <pubDate>Wed, 01 Feb 2023 14:03:32 GMT</pubDate>
            <description><![CDATA[Author: Sun Translation: Helen Community Discord This article is published on XREX and WTF Academy On-chain data can include simple one-time transfers, interactions with one DeFi contract or multiple DeFi contracts, flash loan arbitrage, governance proposals, cross-chain transactions, and more. In this section, let’s begin with a simple start. I will introduce on BlockChain Explorer - Etherscan what we are interested in, and then use Phalcon to compare the differences between these transactio...]]></description>
            <content:encoded><![CDATA[<p>Author: <a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://twitter.com/1nf0s3cpt">Sun</a></p><p>Translation: Helen</p><p>Community <a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://discord.gg/Fjyngakf3h">Discord</a></p><p>This article is published on XREX and <a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://github.com/AmazingAng/WTF-Solidity#%E9%93%BE%E4%B8%8A%E5%A8%81%E8%83%81%E5%88%86%E6%9E%90">WTF Academy</a></p><p>On-chain data can include simple one-time transfers, interactions with one DeFi contract or multiple DeFi contracts, flash loan arbitrage, governance proposals, cross-chain transactions, and more. In this section, let’s begin with a simple start. I will introduce on BlockChain Explorer - Etherscan what we are interested in, and then use <a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://phalcon.blocksec.com/">Phalcon</a> to compare the differences between these transaction function calls: Assets transfer, swap on UniSWAP, increase liquidity on Curve 3pool, Compound proposals, Uniswap Flashswap.</p><h2 id="h-start-to-warm-up" class="text-3xl font-header !mt-8 !mb-4 first:!mt-0 first:!mb-0">Start to warm up</h2><ul><li><p>The first step is to install <a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://github.com/foundry-rs/foundry">Foundry</a> in the environment. Please follow the installation <a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://book.getfoundry.sh/getting-started/installation">instructions</a>.</p><ul><li><p>Forge is a Major test tool on the Foundry platform.If it is your first time to use Foundry, you can refer to <a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://book.getfoundry.sh/">Foundry book</a>, <a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://www.youtube.com/watch?v=wJnywGB33O4">Foundry @EthCC</a>, <a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://github.com/AmazingAng/WTF-Solidity/blob/main/Topics/Tools/TOOL07_Foundry/readme.md">WTF Solidity - Foundry</a>.</p></li></ul></li><li><p>Each chain has its own blockchain explorer. In this section, we will use Ethereum&apos;s blockchain network as a case study.</p></li><li><p>Typical information I usually refer to includes:</p><ul><li><p>Transaction Action: Since the transfer of complex ERC-20 tokens can be difficult to discern, Transaction Action can provide the key behavior of the transfer. However, not all transactions include this information.</p></li><li><p>From: msg.sender, the source wallet address that executes this transaction.</p></li><li><p>Interacted With (To): Which contract to interact with</p></li><li><p>ERC-20 Token Transfer: Token Transfer Process</p></li><li><p>Input Data: The raw input data of the transaction. You can see what Function was called and what Value was brought in.</p></li></ul></li><li><p>If you don&apos;t know what tools are commonly used, you can view the transaction analysis tools in <a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://github.com/SunWeb3Sec/DeFiHackLabs/tree/main/academy/onchain_debug/01_tools/en">the first lesson</a>.</p></li></ul><h2 id="h-assets-transfer" class="text-3xl font-header !mt-8 !mb-4 first:!mt-0 first:!mb-0">Assets transfer</h2><figure float="none" data-type="figure" class="img-center" style="max-width: null;"><img src="https://storage.googleapis.com/papyrus_images/d1a861a9ed53ec2da1d6a7165c953dc87cdf613a9f954c02c99eab3730091493.png" alt="" blurdataurl="data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACwAAAAAAQABAAACAkQBADs=" nextheight="600" nextwidth="800" class="image-node embed"><figcaption HTMLAttributes="[object Object]" class="hide-figcaption"></figcaption></figure><ul><li><p>From: This transaction&apos;s source EOA wallet address</p></li><li><p>Interacted With (To): Tether USD (USDT) Contract</p></li><li><p>ERC-20 Tokens Transferred: Transfer 651.13 USDT from user A&apos;s wallet to user B</p></li><li><p>Input Data: Called transfer function</p></li></ul><p>According to <a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://phalcon.blocksec.com/tx/eth/0x836ef3d01a52c4b9304c3d683f6ff2b296c7331b6fee86e3b116732ce1d5d124">Phalcon</a> &quot;Invocation Flow&quot; :</p><ul><li><p>There is only one &apos;&apos;Call USDT.transfer&apos;&apos;. However, you should pay attention to the &quot;Value&quot;.Because the Ethereum Virtual Machine (EVM) does not support floating-point operations, decimals representation is used instead.</p></li><li><p>Each token has its own precision, the number of decimal places used to represent the value of the token. In ERC-20 tokens, the decimals are usually 18 digits, while USDT has 6 digits. If the precision of the token is not handled properly, problems will arise.</p></li><li><p>You can query it on the Etherscan <a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://etherscan.io/token/0xdac17f958d2ee523a2206206994597c13d831ec7">token contract</a>.</p></li></ul><figure float="none" data-type="figure" class="img-center" style="max-width: null;"><img src="https://storage.googleapis.com/papyrus_images/bd10f9f1b0ade7c28d9e1d1f1414cb15e4cfb134e6954c33f7d7c3d5a9a6875a.png" alt="" blurdataurl="data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACwAAAAAAQABAAACAkQBADs=" nextheight="600" nextwidth="800" class="image-node embed"><figcaption HTMLAttributes="[object Object]" class="hide-figcaption"></figcaption></figure><figure float="none" data-type="figure" class="img-center" style="max-width: null;"><img src="https://storage.googleapis.com/papyrus_images/09d1082c28ff4d3006f2529850a6067436d79a6db2a81d4c8b3e0ee5b61932b0.png" alt="" blurdataurl="data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACwAAAAAAQABAAACAkQBADs=" nextheight="600" nextwidth="800" class="image-node embed"><figcaption HTMLAttributes="[object Object]" class="hide-figcaption"></figcaption></figure><h2 id="h-uniswap-swap" class="text-3xl font-header !mt-8 !mb-4 first:!mt-0 first:!mb-0">Uniswap Swap</h2><figure float="none" data-type="figure" class="img-center" style="max-width: null;"><img src="https://storage.googleapis.com/papyrus_images/93d51cd461d148f37ecfd2792a0cec45fb1988a075bc149bf567fca75e7d55a2.png" alt="" blurdataurl="data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACwAAAAAAQABAAACAkQBADs=" nextheight="600" nextwidth="800" class="image-node embed"><figcaption HTMLAttributes="[object Object]" class="hide-figcaption"></figcaption></figure><p>The following can be derived from the <a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://etherscan.io/tx/0x1cd5ceda7e2b2d8c66f8c5657f27ef6f35f9e557c8d1532aa88665a37130da84">Etherscan</a> example above:</p><ul><li><p>Transaction Action: A user performs Swap on Uniswap V2, exchanging 12,716 USDT for 7,118 UNDEAD.</p></li><li><p>From: This transaction&apos;s source wallet address</p></li><li><p>Interacted With (To): A MEV Bot contract called Uniswap contract for Swap.</p></li><li><p>ERC-20 Tokens Transferred: Token exchange process</p></li></ul><p>According to <a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://phalcon.blocksec.com/tx/eth/0x1cd5ceda7e2b2d8c66f8c5657f27ef6f35f9e557c8d1532aa88665a37130da84">Phalcon</a> &quot;Invocation Flow&quot; :</p><ul><li><p>MEV Bot calls the Uniswap V2 USDT/UNDEAD trading pair contract to call the swap function to perform token exchange.</p></li></ul><figure float="none" data-type="figure" class="img-center" style="max-width: null;"><img src="https://storage.googleapis.com/papyrus_images/8586db302a0dcf463d6ea2e9544521be0fd69e4c1c4e28b5ee7a2a9e5332b4e9.png" alt="" blurdataurl="data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACwAAAAAAQABAAACAkQBADs=" nextheight="600" nextwidth="800" class="image-node embed"><figcaption HTMLAttributes="[object Object]" class="hide-figcaption"></figcaption></figure><h3 id="h-foundry" class="text-2xl font-header !mt-6 !mb-4 first:!mt-0 first:!mb-0">Foundry</h3><p>We use Foundry to simulate the operation of using 1BTC to exchange for DAI in Uniswap.</p><ul><li><p><a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://github.com/SunWeb3Sec/DeFiLabs/blob/main/src/test/Uniswapv2.sol">Sample code reference</a>, execute the following command:</p></li></ul><pre data-type="codeBlock" text="forge test --contracts ./src/test/Uniswapv2.sol -vvvv
"><code>forge test <span class="hljs-operator">-</span><span class="hljs-operator">-</span>contracts ./src<span class="hljs-operator">/</span>test<span class="hljs-operator">/</span>Uniswapv2.sol <span class="hljs-operator">-</span>vvvv
</code></pre><ul><li><p>According to the figure - we swap 1 BTC to 16,788 DAI by calling the Uniswap_v2_router.<a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://docs.uniswap.org/contracts/v2/reference/smart-contracts/router-02#swapexacttokensfortokens">swapExactTokensForTokens</a> function.</p></li></ul><figure float="none" data-type="figure" class="img-center" style="max-width: null;"><img src="https://storage.googleapis.com/papyrus_images/7e7dcf7a049fdffd3512e291b66a2aea0f38c56c5746d8a2aca4e9686cf81403.png" alt="" blurdataurl="data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACwAAAAAAQABAAACAkQBADs=" nextheight="600" nextwidth="800" class="image-node embed"><figcaption HTMLAttributes="[object Object]" class="hide-figcaption"></figcaption></figure><h2 id="h-curve-3pool-daiusdcusdt" class="text-3xl font-header !mt-8 !mb-4 first:!mt-0 first:!mb-0">Curve 3pool - DAI/USDC/USDT</h2><figure float="none" data-type="figure" class="img-center" style="max-width: null;"><img src="https://storage.googleapis.com/papyrus_images/fdce3c60a5a7255a5090c8a17f610df55fcec82df2afa7fa86d05b7a2a69308f.png" alt="" blurdataurl="data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACwAAAAAAQABAAACAkQBADs=" nextheight="600" nextwidth="800" class="image-node embed"><figcaption HTMLAttributes="[object Object]" class="hide-figcaption"></figcaption></figure><p>The following can be derived from the <a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://etherscan.io/tx/0x667cb82d993657f2779507a0262c9ed9098f5a387e8ec754b99f6e1d61d92d0b">Etherscan</a> example above:</p><ul><li><p>The purpose of this transaction is to add Liquidity at Curve three pools.</p></li><li><p>From: This transaction&apos;s source wallet address</p></li><li><p>Interacted With (To): <a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="http://Curve.fi">Curve.fi</a>: DAI/USDC/USDT Pool</p></li><li><p>ERC-20 Tokens Transferred: User A transferred 3,524,968.44 USDT to the Curve 3 pools, and then Curve minted 3,447,897.54 3Crv tokens for User A.</p></li></ul><p>According to <a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://phalcon.blocksec.com/tx/eth/0x667cb82d993657f2779507a0262c9ed9098f5a387e8ec754b99f6e1d61d92d0b">Phalcon</a> &quot;Invocation Flow&quot; :</p><ul><li><p>Based on the call sequence, three steps were executed: 1.add_liquidity 2.transferFrom 3.mint.</p></li></ul><figure float="none" data-type="figure" class="img-center" style="max-width: null;"><img src="https://storage.googleapis.com/papyrus_images/d0b9521e637071bbc7da52d0797da81d2964e324f87017fffc4078994ef113a4.png" alt="" blurdataurl="data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACwAAAAAAQABAAACAkQBADs=" nextheight="600" nextwidth="800" class="image-node embed"><figcaption HTMLAttributes="[object Object]" class="hide-figcaption"></figcaption></figure><h2 id="h-compound-propose" class="text-3xl font-header !mt-8 !mb-4 first:!mt-0 first:!mb-0">Compound propose</h2><figure float="none" data-type="figure" class="img-center" style="max-width: null;"><img src="https://storage.googleapis.com/papyrus_images/b3bfe0409db7ef9b7c96b5a90f264e42578df0c920000c78bd11f1ce3d8e6b05.png" alt="" blurdataurl="data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACwAAAAAAQABAAACAkQBADs=" nextheight="600" nextwidth="800" class="image-node embed"><figcaption HTMLAttributes="[object Object]" class="hide-figcaption"></figcaption></figure><p>The following can be derived from the <a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://etherscan.io/tx/0xba69b455c511c500e0be9453cf70319bc61e29eb4235a6e5ca5fe6ddf1934159">Etherscan</a> example above:</p><ul><li><p>The user submitted a proposal on the Compound. The contents of the proposal can be viewed by clicking &quot;Decode Input Data&quot; on Etherscan.</p></li></ul><figure float="none" data-type="figure" class="img-center" style="max-width: null;"><img src="https://storage.googleapis.com/papyrus_images/ccf8dfa0bc32aa9688036a5df489f662cd358b0a1b093ada0fefd4d54b88ac7f.png" alt="" blurdataurl="data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACwAAAAAAQABAAACAkQBADs=" nextheight="600" nextwidth="800" class="image-node embed"><figcaption HTMLAttributes="[object Object]" class="hide-figcaption"></figcaption></figure><p>According to <a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://phalcon.blocksec.com/tx/eth/0xba69b455c511c500e0be9453cf70319bc61e29eb4235a6e5ca5fe6ddf1934159">Phalcon</a> &quot;Invocation Flow&quot; :</p><ul><li><p>Submitting a proposal through the propose function results in proposal number 44.</p></li></ul><figure float="none" data-type="figure" class="img-center" style="max-width: null;"><img src="https://storage.googleapis.com/papyrus_images/51be7a586a94fff07b827161cf8a8103bbe83daad2e1001dd8285a88eec59166.png" alt="" blurdataurl="data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACwAAAAAAQABAAACAkQBADs=" nextheight="600" nextwidth="800" class="image-node embed"><figcaption HTMLAttributes="[object Object]" class="hide-figcaption"></figcaption></figure><h2 id="h-uniswap-flashswap" class="text-3xl font-header !mt-8 !mb-4 first:!mt-0 first:!mb-0">Uniswap Flashswap</h2><p>Here we use Foundry to simulate operations - how to use flash loans on Uniswap. <a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://docs.uniswap.org/contracts/v2/guides/smart-contract-integration/using-flash-swaps">Official Flash swap introduction</a></p><ul><li><p><a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://github.com/SunWeb3Sec/DeFiLabs/blob/main/src/test/Uniswapv2_flashswap.sol">Sample Code</a> Reference, execute the following command:</p></li></ul><pre data-type="codeBlock" text="forge test --contracts ./src/test/Uniswapv2_flashswap.sol -vv
"><code>forge test <span class="hljs-operator">-</span><span class="hljs-operator">-</span>contracts ./src<span class="hljs-operator">/</span>test<span class="hljs-operator">/</span>Uniswapv2_flashswap.sol <span class="hljs-operator">-</span>vv
</code></pre><figure float="none" data-type="figure" class="img-center" style="max-width: null;"><img src="https://storage.googleapis.com/papyrus_images/9557c1f3251a086ccceb0dbb865b03146546bdbe2d6692144fe3d01debd1dd1b.png" alt="" blurdataurl="data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACwAAAAAAQABAAACAkQBADs=" nextheight="600" nextwidth="800" class="image-node embed"><figcaption HTMLAttributes="[object Object]" class="hide-figcaption"></figcaption></figure><ul><li><p>In this example, a flash loan of 100 WETH is borrowed through the Uniswap UNI/WETH exchange. Note that a 0.3% fee must be paid on repayments.</p></li><li><p>According to the figure - call flow, flashswap calls swap, and then repays by calling back uniswapV2Call.</p></li></ul><figure float="none" data-type="figure" class="img-center" style="max-width: null;"><img src="https://storage.googleapis.com/papyrus_images/5b01c6c5eb85a08330e3b17069c9e8b22fbc3b123bbc62a41e004e058f384bd8.png" alt="" blurdataurl="data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACwAAAAAAQABAAACAkQBADs=" nextheight="600" nextwidth="800" class="image-node embed"><figcaption HTMLAttributes="[object Object]" class="hide-figcaption"></figcaption></figure><ul><li><p>Further Introduction to Flashloan and Flashswap:</p><ul><li><p>A. Common points: Both can lend Tokens without collateralizing assets, and they need to be returned in the same block, otherwise the transaction fails.</p></li><li><p>B. The difference: If token0 is borrowed through Flashloan token0/token1, token0 must be returned. Flashswap lends token0, and you can return token0 or token1, which is more flexible.</p></li></ul></li></ul><p>For more DeFi basic operations, please refer to <a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://github.com/SunWeb3Sec/DeFiLabs">DeFiLab</a>.</p><h2 id="h-foundry-cheatcodes" class="text-3xl font-header !mt-8 !mb-4 first:!mt-0 first:!mb-0">Foundry cheatcodes</h2><p>Foundry&apos;s cheatcodes are essential for conducting chain analysis. Here, I will introduce some commonly used functions. More information can be found in the <a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://book.getfoundry.sh/cheatcodes/">Cheatcodes Reference</a>.</p><ul><li><p>createSelectFork: Specifies a network and block height to copy for testing. Must include the RPC for each chain in <a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://github.com/SunWeb3Sec/DeFiHackLabs/blob/main/foundry.toml">foundry.toml</a>.</p></li><li><p>deal: Sets the balance of a test wallet.</p><ul><li><p>Set ETH balance: <code>deal(address(this), 3 ether);</code></p></li><li><p>Set Token balance: <code>deal(address(USDC), address(this), 1 * 1e18);</code></p></li></ul></li><li><p>prank: Specify the wallet address to simulate. It is only effective for the next call and will set the msg.sender to the specified wallet address. Such as simulating a transfer from a whale wallet.</p></li><li><p>startPrank: Specify the wallet address to simulate. It will set the msg.sender to the specified wallet address for all calls until <code>stopPrank()</code> is executed.</p></li><li><p>label: Labels a wallet address for improved readability when using Foundry debug.</p></li><li><p>roll: Adjusts the block height.</p></li><li><p>warp: Adjusts the block timestamp.</p></li></ul><p>Thanks for following along! Time to jump into the next lesson.</p><h2 id="h-resources" class="text-3xl font-header !mt-8 !mb-4 first:!mt-0 first:!mb-0">Resources</h2><p><a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://book.getfoundry.sh/">https://book.getfoundry.sh/</a></p><p><a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://github.com/crisgarner/awesome-foundry">https://github.com/crisgarner/awesome-foundry</a></p><p><a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://blog.infura.io/post/build-a-flash-loan-arbitrage-bot-on-infura-part-i">https://blog.infura.io/post/build-a-flash-loan-arbitrage-bot-on-infura-part-i</a></p>]]></content:encoded>
            <author>sunweb3sec@newsletter.paragraph.com (SunWeb3Sec)</author>
            <enclosure url="https://storage.googleapis.com/papyrus_images/47eaaf323a2ce660eb4d0f01e307465d8859950e3df8487c448c7b7dd5178889.png" length="0" type="image/png"/>
        </item>
        <item>
            <title><![CDATA[OnChain Transaction Debugging - Lesson 2: 熱身篇]]></title>
            <link>https://paragraph.com/@sunweb3sec/onchain-transaction-debugging-lesson-2</link>
            <guid>gEvKtDpcEFm4madu07qp</guid>
            <pubDate>Wed, 01 Feb 2023 14:00:41 GMT</pubDate>
            <description><![CDATA[Author: Sun 社群 Discord 同步發表: XREX | WTF Academy 鏈上交易數據包含從簡單的單筆交易轉帳、1 個 DeFi 合約交互、多個 DeFi 合約交互、閃電貸套利、治理提案、跨鏈交易等等，這一節我們先來熱身一下，先從簡單的開始。我將介紹通常使用區塊鏈瀏覽器 Etherscan 哪些訊息是我們所在意的，再來我們會使用交易分析工具 Phalcon 看一下這些交易從簡單的轉帳、UniSWAP上 Swap、Curve 3pool 增加流動性、Compound 治理提案、閃電貸的調用差異。開始進入熱身篇首先環境上需要先安裝 Foundry，安裝方法請參考 instructions.測試主要會用到 Forge test，如果第一次使用 Foundry，可以參考 Foundry book、Foundry @EthCC、WTF Solidity - Foundry每條鏈上都有專屬的區塊鏈瀏覽器，這節我們都會使用 Ethereum 主網來當案例所以可以透過 Etherscan 來分析.通常我會特別想看的欄位包含:Transaction Action: 因為複雜的...]]></description>
            <content:encoded><![CDATA[<p>Author: <a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://twitter.com/1nf0s3cpt">Sun</a></p><p>社群 <a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://discord.gg/Fjyngakf3h">Discord</a></p><p>同步發表: XREX | <a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://github.com/AmazingAng/WTF-Solidity#%E9%93%BE%E4%B8%8A%E5%A8%81%E8%83%81%E5%88%86%E6%9E%90">WTF Academy</a></p><p>鏈上交易數據包含從簡單的單筆交易轉帳、1 個 DeFi 合約交互、多個 DeFi 合約交互、閃電貸套利、治理提案、跨鏈交易等等，這一節我們先來熱身一下，先從簡單的開始。我將介紹通常使用區塊鏈瀏覽器 Etherscan 哪些訊息是我們所在意的，再來我們會使用交易分析工具 <a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://phalcon.blocksec.com/">Phalcon</a> 看一下這些交易從簡單的轉帳、UniSWAP上 Swap、Curve 3pool 增加流動性、Compound 治理提案、閃電貸的調用差異。</p><h2 id="h-" class="text-3xl font-header !mt-8 !mb-4 first:!mt-0 first:!mb-0">開始進入熱身篇</h2><ul><li><p>首先環境上需要先安裝 <a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://github.com/foundry-rs/foundry">Foundry</a>，安裝方法請參考 <a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://book.getfoundry.sh/getting-started/installation.html">instructions</a>.</p><ul><li><p>測試主要會用到 <a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://book.getfoundry.sh/reference/forge/forge-test">Forge test</a>，如果第一次使用 Foundry，可以參考 <a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://book.getfoundry.sh/">Foundry book</a>、<a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://www.youtube.com/watch?v=wJnywGB33O4">Foundry @EthCC</a>、<a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://github.com/AmazingAng/WTF-Solidity/blob/main/Topics/Tools/TOOL07_Foundry/readme.md">WTF Solidity - Foundry</a></p></li></ul></li><li><p>每條鏈上都有專屬的區塊鏈瀏覽器，這節我們都會使用 Ethereum 主網來當案例所以可以透過 Etherscan 來分析.</p></li><li><p>通常我會特別想看的欄位包含:</p><ul><li><p>Transaction Action: 因為複雜的交易中 ERC-20 Tokens Transferred 會很複雜，可讀性不好，所以可以透過 Transaction Action 看一下關鍵行為但不一定每筆交易都有</p></li><li><p>From: msg.sender 執行這筆交易的來源錢包地址</p></li><li><p>Interacted With (To): 跟哪個合約交互</p></li><li><p>ERC-20 Tokens Transferred: 代幣轉移流程</p></li><li><p>Input Data: 交易的原始 Input 資料，可以看到呼叫什麼 Function 和帶入什麼 Value</p></li></ul></li><li><p>如果還不知道常用工具有哪些可以回顧第一課交易分析<a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://github.com/SunWeb3Sec/DeFiHackLabs/tree/main/academy/onchain_debug/01_tools">工具篇</a></p></li></ul><h2 id="h-" class="text-3xl font-header !mt-8 !mb-4 first:!mt-0 first:!mb-0">鏈上轉帳</h2><figure float="none" data-type="figure" class="img-center" style="max-width: null;"><img src="https://storage.googleapis.com/papyrus_images/d1a861a9ed53ec2da1d6a7165c953dc87cdf613a9f954c02c99eab3730091493.png" alt="" blurdataurl="data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACwAAAAAAQABAAACAkQBADs=" nextheight="600" nextwidth="800" class="image-node embed"><figcaption HTMLAttributes="[object Object]" class="hide-figcaption"></figcaption></figure><p>From: 發送這筆交易的來源錢包地址</p><p>Interacted With (To): Tether USD (USDT) 合約</p><p>ERC-20 Tokens Transferred: 從用戶A 錢包轉 651.13 USDT 到用戶 B</p><p>Input Data: 呼叫了 transfer function</p><p>透過 <a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://phalcon.blocksec.com/tx/eth/0x836ef3d01a52c4b9304c3d683f6ff2b296c7331b6fee86e3b116732ce1d5d124">phalcon</a> 來看: 從調用流程來看就只有一個 <code>Call USDT.transfer</code>，要注意的是 Value. 因為 EVM 不支持浮點數的運算，所以使用精度代表，每個 Token 都要注意它的精度大小，標準 ERC-20 代幣精度為 18，但也有特例，如 USDT 為例，精度是 6 所以 Value 帶入的值為 651130000，如果精度處理不當就容易造成問題。精度的查詢方式可以到 <a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://etherscan.io/token/0xdac17f958d2ee523a2206206994597c13d831ec7">Etherscan</a> 代幣合約上看到。</p><figure float="none" data-type="figure" class="img-center" style="max-width: null;"><img src="https://storage.googleapis.com/papyrus_images/bd10f9f1b0ade7c28d9e1d1f1414cb15e4cfb134e6954c33f7d7c3d5a9a6875a.png" alt="" blurdataurl="data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACwAAAAAAQABAAACAkQBADs=" nextheight="600" nextwidth="800" class="image-node embed"><figcaption HTMLAttributes="[object Object]" class="hide-figcaption"></figcaption></figure><figure float="none" data-type="figure" class="img-center" style="max-width: null;"><img src="https://storage.googleapis.com/papyrus_images/09d1082c28ff4d3006f2529850a6067436d79a6db2a81d4c8b3e0ee5b61932b0.png" alt="" blurdataurl="data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACwAAAAAAQABAAACAkQBADs=" nextheight="600" nextwidth="800" class="image-node embed"><figcaption HTMLAttributes="[object Object]" class="hide-figcaption"></figcaption></figure><h2 id="h-uniswap-swap" class="text-3xl font-header !mt-8 !mb-4 first:!mt-0 first:!mb-0">Uniswap Swap</h2><figure float="none" data-type="figure" class="img-center" style="max-width: null;"><img src="https://storage.googleapis.com/papyrus_images/93d51cd461d148f37ecfd2792a0cec45fb1988a075bc149bf567fca75e7d55a2.png" alt="" blurdataurl="data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACwAAAAAAQABAAACAkQBADs=" nextheight="600" nextwidth="800" class="image-node embed"><figcaption HTMLAttributes="[object Object]" class="hide-figcaption"></figcaption></figure><p>從上圖<a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://etherscan.io/tx/0x1cd5ceda7e2b2d8c66f8c5657f27ef6f35f9e557c8d1532aa88665a37130da84">例子</a> 可以解讀為:</p><p>Transaction Action: 很直覺就可以知道用戶在 Uniswap 上進行 Swap，將 12,716 USDT 換成 7,118 UNDEAD。</p><p>From: 發送這筆交易的來源錢包地址</p><p>Interacted With (To): 這個例子是一個 MEV Bot 合約呼叫 Uniswap 合約進行 Swap</p><p>ERC-20 Tokens Transferred: Token 交換的過程</p><p>透過 <a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://phalcon.blocksec.com/tx/eth/0x1cd5ceda7e2b2d8c66f8c5657f27ef6f35f9e557c8d1532aa88665a37130da84">phalcon</a> 來看: MEV Bot 呼叫 Uniswap V2 USDT/UNDEAD 交易對合約呼叫 <a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://docs.uniswap.org/contracts/v2/reference/smart-contracts/pair#swap-1">swap</a> 函示來進行代幣兌換。</p><figure float="none" data-type="figure" class="img-center" style="max-width: null;"><img src="https://storage.googleapis.com/papyrus_images/8586db302a0dcf463d6ea2e9544521be0fd69e4c1c4e28b5ee7a2a9e5332b4e9.png" alt="" blurdataurl="data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACwAAAAAAQABAAACAkQBADs=" nextheight="600" nextwidth="800" class="image-node embed"><figcaption HTMLAttributes="[object Object]" class="hide-figcaption"></figcaption></figure><p>我們使用 Foundry 來模擬操作使用 1BTC 在 Uniswap 換成 DAI，<a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://github.com/SunWeb3Sec/DeFiLabs/blob/main/src/test/Uniswapv2.sol">範例程式碼</a>參考，執行以下指令</p><pre data-type="codeBlock" text="forge test --contracts ./src/test/Uniswapv2.sol -vvvv
"><code>forge test <span class="hljs-operator">-</span><span class="hljs-operator">-</span>contracts ./src<span class="hljs-operator">/</span>test<span class="hljs-operator">/</span>Uniswapv2.sol <span class="hljs-operator">-</span>vvvv
</code></pre><p>如下圖所示我們透過呼叫 Uniswap_v2_router.<a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://docs.uniswap.org/contracts/v2/reference/smart-contracts/router-02#swapexacttokensfortokens">swapExactTokensForTokens</a> 函式，將 1BTC 換到 16,788 DAI.</p><figure float="none" data-type="figure" class="img-center" style="max-width: null;"><img src="https://storage.googleapis.com/papyrus_images/7e7dcf7a049fdffd3512e291b66a2aea0f38c56c5746d8a2aca4e9686cf81403.png" alt="" blurdataurl="data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACwAAAAAAQABAAACAkQBADs=" nextheight="600" nextwidth="800" class="image-node embed"><figcaption HTMLAttributes="[object Object]" class="hide-figcaption"></figcaption></figure><h2 id="h-curve-3pool-daiusdcusdt" class="text-3xl font-header !mt-8 !mb-4 first:!mt-0 first:!mb-0">Curve 3pool - DAI/USDC/USDT</h2><figure float="none" data-type="figure" class="img-center" style="max-width: null;"><img src="https://storage.googleapis.com/papyrus_images/fdce3c60a5a7255a5090c8a17f610df55fcec82df2afa7fa86d05b7a2a69308f.png" alt="" blurdataurl="data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACwAAAAAAQABAAACAkQBADs=" nextheight="600" nextwidth="800" class="image-node embed"><figcaption HTMLAttributes="[object Object]" class="hide-figcaption"></figcaption></figure><p>從上圖<a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://etherscan.io/tx/0x667cb82d993657f2779507a0262c9ed9098f5a387e8ec754b99f6e1d61d92d0b">例子</a>可以解讀為:</p><p>在 Curve 3pool 增加流動性</p><p>From: 發送這筆交易的來源錢包地址</p><p>Interacted With (To): <a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="http://Curve.fi">Curve.fi</a>: DAI/USDC/USDT Pool</p><p>ERC-20 Tokens Transferred: 用戶 A 轉入 3,524,968.44 USDT 到 Curve 3 pool，然後 Curve 鑄造 3,447,897.54 3Crv 代幣給用戶 A.</p><p>透過 <a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://phalcon.blocksec.com/tx/eth/0x667cb82d993657f2779507a0262c9ed9098f5a387e8ec754b99f6e1d61d92d0b">phalcon</a> 來看: 從調用流程來看執行了三個步驟 1.add_liquidity 2.transferFrom 3.mint</p><figure float="none" data-type="figure" class="img-center" style="max-width: null;"><img src="https://storage.googleapis.com/papyrus_images/d0b9521e637071bbc7da52d0797da81d2964e324f87017fffc4078994ef113a4.png" alt="" blurdataurl="data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACwAAAAAAQABAAACAkQBADs=" nextheight="600" nextwidth="800" class="image-node embed"><figcaption HTMLAttributes="[object Object]" class="hide-figcaption"></figcaption></figure><h2 id="h-compound-propose" class="text-3xl font-header !mt-8 !mb-4 first:!mt-0 first:!mb-0">Compound propose</h2><figure float="none" data-type="figure" class="img-center" style="max-width: null;"><img src="https://storage.googleapis.com/papyrus_images/b3bfe0409db7ef9b7c96b5a90f264e42578df0c920000c78bd11f1ce3d8e6b05.png" alt="" blurdataurl="data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACwAAAAAAQABAAACAkQBADs=" nextheight="600" nextwidth="800" class="image-node embed"><figcaption HTMLAttributes="[object Object]" class="hide-figcaption"></figcaption></figure><p>從上圖<a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://etherscan.io/tx/0xba69b455c511c500e0be9453cf70319bc61e29eb4235a6e5ca5fe6ddf1934159">例子</a>可以解讀為: 用戶在 Compound 治理合約上提交了一個提案，從 Etherscan 上可以點擊 Decode Input Data 就可以看到提案內容。</p><figure float="none" data-type="figure" class="img-center" style="max-width: null;"><img src="https://storage.googleapis.com/papyrus_images/ccf8dfa0bc32aa9688036a5df489f662cd358b0a1b093ada0fefd4d54b88ac7f.png" alt="" blurdataurl="data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACwAAAAAAQABAAACAkQBADs=" nextheight="600" nextwidth="800" class="image-node embed"><figcaption HTMLAttributes="[object Object]" class="hide-figcaption"></figcaption></figure><p>透過 <a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://phalcon.blocksec.com/tx/eth/0xba69b455c511c500e0be9453cf70319bc61e29eb4235a6e5ca5fe6ddf1934159">phalcon</a> 來看: 透過呼叫 propose 函式來提交 proposal 得到編號 44 號提案。</p><figure float="none" data-type="figure" class="img-center" style="max-width: null;"><img src="https://storage.googleapis.com/papyrus_images/51be7a586a94fff07b827161cf8a8103bbe83daad2e1001dd8285a88eec59166.png" alt="" blurdataurl="data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACwAAAAAAQABAAACAkQBADs=" nextheight="600" nextwidth="800" class="image-node embed"><figcaption HTMLAttributes="[object Object]" class="hide-figcaption"></figcaption></figure><h2 id="h-uniswap-flashswap" class="text-3xl font-header !mt-8 !mb-4 first:!mt-0 first:!mb-0">Uniswap Flashswap</h2><p>我們使用 Foundry 來模擬操作看看如何在 Uniswap 上使用閃電貸，<a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://docs.uniswap.org/contracts/v2/guides/smart-contract-integration/using-flash-swaps">官方Flash swap介紹</a></p><p><a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://github.com/SunWeb3Sec/DeFiLabs/blob/main/src/test/Uniswapv2_flashswap.sol">範例程式碼</a>參考，執行以下指令</p><pre data-type="codeBlock" text="forge test --contracts ./src/test/Uniswapv2_flashswap.sol -vv
"><code>forge test <span class="hljs-operator">-</span><span class="hljs-operator">-</span>contracts ./src<span class="hljs-operator">/</span>test<span class="hljs-operator">/</span>Uniswapv2_flashswap.sol <span class="hljs-operator">-</span>vv
</code></pre><figure float="none" data-type="figure" class="img-center" style="max-width: null;"><img src="https://storage.googleapis.com/papyrus_images/9557c1f3251a086ccceb0dbb865b03146546bdbe2d6692144fe3d01debd1dd1b.png" alt="" blurdataurl="data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACwAAAAAAQABAAACAkQBADs=" nextheight="600" nextwidth="800" class="image-node embed"><figcaption HTMLAttributes="[object Object]" class="hide-figcaption"></figcaption></figure><p>以這個例子透過 Uniswap UNI/WETH 交易兌上進行閃電貸借出 100 顆 WETH，再還回去給 Uniswap. 注意還款時要付 0.3% 手續費。</p><p>從下圖調用流程可以看出，呼叫 swap 進行 flashswap 然後透過 callback uniswapV2Call 來還款。</p><figure float="none" data-type="figure" class="img-center" style="max-width: null;"><img src="https://storage.googleapis.com/papyrus_images/5b01c6c5eb85a08330e3b17069c9e8b22fbc3b123bbc62a41e004e058f384bd8.png" alt="" blurdataurl="data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACwAAAAAAQABAAACAkQBADs=" nextheight="600" nextwidth="800" class="image-node embed"><figcaption HTMLAttributes="[object Object]" class="hide-figcaption"></figcaption></figure><p>簡單區分一下 Flashloan 和 Flashswap 的差異，兩種都是無需抵押資產就可以借出 Token，且需要在同一個區塊內還回去不然交易就會失敗，假如透過 token0/token1 進行 Flashloan 借出 token0 就要還 token0回去，Flashswap 借出 token0 可以還 token0 或 token1 回去，比較彈性。</p><p>更多 DeFi 基本操作可以參考 <a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://github.com/SunWeb3Sec/DeFiLabs">DeFiLabs</a></p><h2 id="h-foundry-cheatcodes" class="text-3xl font-header !mt-8 !mb-4 first:!mt-0 first:!mb-0">Foundry cheatcodes</h2><p>Foundry 的 cheatcodes 在我們做鏈上分析必須使用到的，這邊我介紹一下常用到的函式，更多介紹可以參考 <a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://book.getfoundry.sh/cheatcodes/">Cheatcodes Reference</a></p><ul><li><p>createSelectFork: 指定這次測試要複製哪個網路和區塊高度，注意每條鏈的 RPC 要寫在 <a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://github.com/SunWeb3Sec/DeFiHackLabs/blob/main/foundry.toml">foundry.toml</a></p></li><li><p>deal: 設定測試錢包餘額</p><ul><li><p>設定 ETH 餘額 <code>deal(address(this), 3 ether);</code></p></li><li><p>設定 Token 餘額 <code>deal(address(USDC), address(this), 1 * 1e18);</code></p></li></ul></li><li><p>prank: 模擬指定錢包身份，只有在下一個呼叫有效，下一個 msg.sender 是會所指定的錢包，例如使用巨鯨錢包轉帳</p></li><li><p>startPrank: 模擬指定錢包身份，在沒有執行<code>stopPrank()</code>之前，所有 msg.sender 都會是指定的錢包地址</p></li><li><p>label: 將錢包地址標籤化，方便在使用 Foundry debug 時提高可讀性</p></li><li><p>roll: 調整區塊高度</p></li><li><p>warp: 調整 block.timestamp</p></li></ul><p>謝謝收看，我們準備進入下一課</p><h2 id="h-resources" class="text-3xl font-header !mt-8 !mb-4 first:!mt-0 first:!mb-0">Resources</h2><p><a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://book.getfoundry.sh/">https://book.getfoundry.sh/</a></p><p><a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://github.com/crisgarner/awesome-foundry">https://github.com/crisgarner/awesome-foundry</a></p><p><a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://www.youtube.com/watch?v=wJnywGB33O4">Foundry @EthCC</a> | <a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://docs.google.com/presentation/d/1AuQojnFMkozOiR8kDu5LlWT7vv1EfPytmVEeq1XMtM0/edit#slide=id.g13d8bd167cb_0_0">Slides</a></p><p><a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://github.com/AmazingAng/WTF-Solidity/blob/main/Topics/Tools/TOOL07_Foundry/readme.md">https://github.com/AmazingAng/WTF-Solidity/blob/main/Topics/Tools/TOOL07_Foundry/readme.md</a></p><p><a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://blog.infura.io/post/build-a-flash-loan-arbitrage-bot-on-infura-part-i">https://blog.infura.io/post/build-a-flash-loan-arbitrage-bot-on-infura-part-i</a></p>]]></content:encoded>
            <author>sunweb3sec@newsletter.paragraph.com (SunWeb3Sec)</author>
            <enclosure url="https://storage.googleapis.com/papyrus_images/47eaaf323a2ce660eb4d0f01e307465d8859950e3df8487c448c7b7dd5178889.png" length="0" type="image/png"/>
        </item>
        <item>
            <title><![CDATA[OnChain Transaction Debugging - Lesson 1: 工具篇]]></title>
            <link>https://paragraph.com/@sunweb3sec/onchain-transaction-debugging-lesson-1</link>
            <guid>8pNBFRGFsPALHr1Fd4Y8</guid>
            <pubDate>Wed, 01 Feb 2023 13:43:29 GMT</pubDate>
            <description><![CDATA[Author: Sun 社群 Discord 同步發表: XREX | WTF Academy 當初我在學習鏈上交易分析時，很少相關教學文章，只能自己慢慢地收集資料從中挖掘如何分析到測試。 我們將推出一系列 Web3 安全的教學文章, 幫助更多人加入 Web3 安全，共創安全網路。 第一個系列我們將介紹如何進行鏈上分析到撰寫攻擊重現。此技能將能幫助你分析攻擊過程和漏洞原因甚至套利機器人如何套利！工欲善其事，必先利其器在進入分析之前，我先介紹一些常用工具，正確的工具可以幫助你做研究時更有效率。Transaction debugging toolsPhalcon | Tx.viewer | Cruise | Ethtx | Tenderly Transaction Viewer 這類工具是最常用的，可以幫助我們針對想要分析的交易 Transaction，以可視化列出函數呼叫的流程以及每個函式帶入了什麼的參數等。 每個工具大同小異，只差異在鏈的支援度不同和輔助功能，我個人是比較常用 Phalcon 和 Sam 的 Transaction Viewer，如果遇到不支援的鏈則會使用 Ten...]]></description>
            <content:encoded><![CDATA[<p>Author: <a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://twitter.com/1nf0s3cpt">Sun</a></p><p>社群 <a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://discord.gg/Fjyngakf3h">Discord</a></p><p>同步發表: XREX | <a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://github.com/AmazingAng/WTF-Solidity#%E9%93%BE%E4%B8%8A%E5%A8%81%E8%83%81%E5%88%86%E6%9E%90">WTF Academy</a></p><p>當初我在學習鏈上交易分析時，很少相關教學文章，只能自己慢慢地收集資料從中挖掘如何分析到測試。 我們將推出一系列 Web3 安全的教學文章, 幫助更多人加入 Web3 安全，共創安全網路。</p><p>第一個系列我們將介紹如何進行鏈上分析到撰寫攻擊重現。此技能將能幫助你分析攻擊過程和漏洞原因甚至套利機器人如何套利！</p><h2 id="h-" class="text-3xl font-header !mt-8 !mb-4 first:!mt-0 first:!mb-0">工欲善其事，必先利其器</h2><p>在進入分析之前，我先介紹一些常用工具，正確的工具可以幫助你做研究時更有效率。</p><h3 id="h-transaction-debugging-tools" class="text-2xl font-header !mt-6 !mb-4 first:!mt-0 first:!mb-0">Transaction debugging tools</h3><p><a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://phalcon.blocksec.com/">Phalcon</a> | <a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://tx.eth.samczsun.com/">Tx.viewer</a> | <a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://cruise.supremacy.team/">Cruise</a> | <a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://ethtx.info/">Ethtx</a> | <a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://dashboard.tenderly.co/explorer">Tenderly</a></p><p>Transaction Viewer 這類工具是最常用的，可以幫助我們針對想要分析的交易 Transaction，以可視化列出函數呼叫的流程以及每個函式帶入了什麼的參數等。 每個工具大同小異，只差異在鏈的支援度不同和輔助功能，我個人是比較常用 Phalcon 和 Sam 的 Transaction Viewer，如果遇到不支援的鏈則會使用 Tenderly，Tenderly 支援最多鏈，但是可讀性就不是這麼方便，需要 Debug 慢慢分析。不過我最初在研究鏈上分析是先學習 Ethtx 和 Tenderly。</p><h4 id="h-" class="text-xl font-header !mt-6 !mb-3 first:!mt-0 first:!mb-0">鏈支援度比較</h4><p>Phalcon： <code>Ethereum、BSC、Cronos、Avalanche C-Chain、Polygon</code></p><p>Sam&apos;s Transaction viewer： <code>Ethereum、Polygon、BSC、Avalanche C-Chain、Fantom、Arbitrum、Optimism</code></p><p>Cruise： <code>Ethereum、BSC 、Polygon、Arbitrum、Fantom、Optimism、Avalanche、Celo、Gnosis</code></p><p>Ethtx： <code>Ethereum、Goerli testnet</code></p><p>Tendery： <code>Ethereum、Polygon、BSC、Sepolia、Goerli、Gnosis、POA、RSK、Avalanche C-Chain、Arbitrum、Optimism 、Fantom、Moonbeam、Moonriver</code></p><h4 id="h-" class="text-xl font-header !mt-6 !mb-3 first:!mt-0 first:!mb-0">實務操作</h4><p>以 JayPeggers - Insufficient validation + Reentrancy <a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://github.com/SunWeb3Sec/DeFiHackLabs/#20221229---jay---insufficient-validation--reentrancy">事件</a>來當例子 <a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://phalcon.blocksec.com/tx/eth/0xd4fafa1261f6e4f9c8543228a67caf9d02811e4ad3058a2714323964a8db61f6">TXID</a> 使用 Blocksec 開發的 Phalcon 工具來說明，下圖可以看到該交易的基本資訊和餘額變化，從餘額變化可以快速看出攻擊著大概獲利多少，以這個例子攻擊者獲利 15.32 ETH。</p><figure float="none" data-type="figure" class="img-center" style="max-width: null;"><img src="https://storage.googleapis.com/papyrus_images/d581e2d1627d0c72ee3f7b20bc6e7704512eaff69c8956ab2550b4913dee3ca5.png" alt="210571234-402d96aa-fe5e-4bc4-becc-190bd5a78e68-2" blurdataurl="data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACwAAAAAAQABAAACAkQBADs=" nextheight="600" nextwidth="800" class="image-node embed"><figcaption HTMLAttributes="[object Object]" class="">210571234-402d96aa-fe5e-4bc4-becc-190bd5a78e68-2</figcaption></figure><p>Invocation Flow 可視化函式調用流程: 可以讓我們知道這一筆交易調用流程和函式呼叫的層級，有沒有使用閃電貸、涉及了哪些項目、呼叫了哪些函式帶入了什麼參數和原始 data 等等</p><figure float="none" data-type="figure" class="img-center" style="max-width: null;"><img src="https://storage.googleapis.com/papyrus_images/baa0ad0b81bc9e0328aae98f8cd173b0e81cbb5aea420b3efddbd50018e455bf.png" alt="圖片" blurdataurl="data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACwAAAAAAQABAAACAkQBADs=" nextheight="600" nextwidth="800" class="image-node embed"><figcaption HTMLAttributes="[object Object]" class="">圖片</figcaption></figure><p>Phalcon 2.0 新增了資金流向和 Debug + 原始碼分析可以在 Trace 的過程中邊看程式執行的片段、參數、返回值，分析上方便了不少。</p><figure float="none" data-type="figure" class="img-center" style="max-width: null;"><img src="https://storage.googleapis.com/papyrus_images/ac27c3e0c1ff4634e4e27a64ededd2a4e3eaba579fcc4999f7ed0a39c9e592ec.png" alt="image" blurdataurl="data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACwAAAAAAQABAAACAkQBADs=" nextheight="600" nextwidth="800" class="image-node embed"><figcaption HTMLAttributes="[object Object]" class="">image</figcaption></figure><p>換 Sam 的 Transaction Viewer 來看看 <a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://tx.eth.samczsun.com/ethereum/0xd4fafa1261f6e4f9c8543228a67caf9d02811e4ad3058a2714323964a8db61f6">TXID</a> 跟 Phalcon 類似但 Sam 整合了許多小工具在裡面，如下圖的眼睛點下去可以看到 Storage 的變化和每個呼叫所消耗的 Gas。</p><figure float="none" data-type="figure" class="img-center" style="max-width: null;"><img src="https://storage.googleapis.com/papyrus_images/0d099eb3fb47a6d6db56327004c6a03210b8266bc46e60337ec18f13092cd8a0.png" alt="210574290-790f6129-aa82-4152-b3e1-d21820524a0a-2" blurdataurl="data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACwAAAAAAQABAAACAkQBADs=" nextheight="600" nextwidth="800" class="image-node embed"><figcaption HTMLAttributes="[object Object]" class="">210574290-790f6129-aa82-4152-b3e1-d21820524a0a-2</figcaption></figure><p>點擊最左邊的 Call，可以把原始 Input data 嘗試 Decode。</p><figure float="none" data-type="figure" class="img-center" style="max-width: null;"><img src="https://storage.googleapis.com/papyrus_images/1e4a29a4f87231434c28eca23d8d25768aa7d3f7847d7107bfd96cc95354116d.png" alt="圖片" blurdataurl="data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACwAAAAAAQABAAACAkQBADs=" nextheight="600" nextwidth="800" class="image-node embed"><figcaption HTMLAttributes="[object Object]" class="">圖片</figcaption></figure><p>再來換 Tendery 來看看 <a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://dashboard.tenderly.co/tx/mainnet/0xd4fafa1261f6e4f9c8543228a67caf9d02811e4ad3058a2714323964a8db61f6">TXID</a> 在 Tendery 介面上，一樣可以看到基本資訊，但在 Debug 的部分就不是可視化，需要一步一步 Debug 走下去分析，不過好處是可以邊 Debug 邊看程式碼還有 Input data 的轉換過程。</p><figure float="none" data-type="figure" class="img-center" style="max-width: null;"><img src="https://storage.googleapis.com/papyrus_images/17a3f9018c0e22761ae7f8322f6297fd628c34cb2c59cf3cd5d929c2c215d20b.png" alt="圖片" blurdataurl="data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACwAAAAAAQABAAACAkQBADs=" nextheight="600" nextwidth="800" class="image-node embed"><figcaption HTMLAttributes="[object Object]" class="">圖片</figcaption></figure><p>到這邊就可以幫我們釐清大概這筆交易做了哪些事情，在還沒有開始寫 Poc 時，如果想要快速重放攻擊可以嗎? 可以! 可以使用Tendery 或 Phalcon，這兩個工具另外支援了模擬交易重現，在上圖右上角有一個按鈕 Re-Simulate，工具會自動幫你帶上該交易的參數值如下圖 從圖中的欄位可以依照需求任意改變如改block number, From, Value, Input data 等</p><figure float="none" data-type="figure" class="img-center" style="max-width: null;"><img src="https://storage.googleapis.com/papyrus_images/e3e44575d9c7313bc106dd6f776cfbd6cbc4cc2b4aa9d5b57270e4dc0f32485a.png" alt="圖片" blurdataurl="data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACwAAAAAAQABAAACAkQBADs=" nextheight="600" nextwidth="800" class="image-node embed"><figcaption HTMLAttributes="[object Object]" class="">圖片</figcaption></figure><h3 id="h-ethereum-signature-database" class="text-2xl font-header !mt-6 !mb-4 first:!mt-0 first:!mb-0">Ethereum Signature Database</h3><p><a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://www.4byte.directory/">4byte</a> | <a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://sig.eth.samczsun.com/">sig.eth</a> | <a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://www.etherface.io/hash">etherface</a></p><p>在原始 Input data，前面 4bytes 為 Function Signature. 有時遇到 Etherscan 或分析工具無法解出來時，可以透過 Signature Database 來查看看可能是什麼 Function。</p><figure float="none" data-type="figure" class="img-center" style="max-width: null;"><img src="https://storage.googleapis.com/papyrus_images/6a8a173f505add177b3efa0ec4c4be64bfea0268ef23eb903b756853a74f0e98.png" alt="image" blurdataurl="data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACwAAAAAAQABAAACAkQBADs=" nextheight="600" nextwidth="800" class="image-node embed"><figcaption HTMLAttributes="[object Object]" class="">image</figcaption></figure><figure float="none" data-type="figure" class="img-center" style="max-width: null;"><img src="https://storage.googleapis.com/papyrus_images/47e21120b6e58d90f4fd17c27d08c0f9b8917627e10c2fffcafa2a7406965d5e.png" alt="圖片" blurdataurl="data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACwAAAAAAQABAAACAkQBADs=" nextheight="600" nextwidth="800" class="image-node embed"><figcaption HTMLAttributes="[object Object]" class="">圖片</figcaption></figure><h3 id="h-useful-tools" class="text-2xl font-header !mt-6 !mb-4 first:!mt-0 first:!mb-0">Useful tools</h3><p><a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://gnidan.github.io/abi-to-sol/">ABI to interface</a> | <a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://abi.w1nt3r.xyz/">Get ABI for unverified contracts</a> | <a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://apoorvlathey.com/eth-calldata-decoder/">ETH Calldata Decoder</a> | <a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://www.ethcmd.com/">ETHCMD - Guess ABI</a></p><p>ABI to interface: 在開發 Poc 時需要呼叫其他合約時要有 Interface 接口，我們可以透過這個工具幫你快速產生你要的接口。 先去 Etherscan 把 ABI 複製下來，貼過去工具上就可以看到產生出來的 Interface。 <a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://etherscan.io/address/0xb3da8d6da3ede239ccbf576ca0eaa74d86f0e9d3#code">例子</a></p><figure float="none" data-type="figure" class="img-center" style="max-width: null;"><img src="https://storage.googleapis.com/papyrus_images/9ee380f7356ffcda3c055f43fe3dcb1777505186f2c0cc185e9fdddfefc6b6ee.png" alt="圖片" blurdataurl="data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACwAAAAAAQABAAACAkQBADs=" nextheight="600" nextwidth="800" class="image-node embed"><figcaption HTMLAttributes="[object Object]" class="">圖片</figcaption></figure><p>ETH Calldata Decoder: 有時候在沒有 ABI 的情況下想要解看看 Input data 可以試試看 ETH Calldata Decoder，在前面介紹到 Sam 的工具就有支援 Input data decode。</p><figure float="none" data-type="figure" class="img-center" style="max-width: null;"><img src="https://storage.googleapis.com/papyrus_images/6cf2891f079beef0f63fa1416d4938a83a61af00fc6f700acfddd2b7f06c4d00.png" alt="圖片" blurdataurl="data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACwAAAAAAQABAAACAkQBADs=" nextheight="600" nextwidth="800" class="image-node embed"><figcaption HTMLAttributes="[object Object]" class="">圖片</figcaption></figure><p>Get ABI for unverified contracts: 如果遇到未開源的合約，可以透過這個工具嘗試列舉出這個合約中存在的 Function Signature. <a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://abi.w1nt3r.xyz/mainnet/0xaE9C73fd0Fd237c1c6f66FE009d24ce969e98704">例子</a></p><figure float="none" data-type="figure" class="img-center" style="max-width: null;"><img src="https://storage.googleapis.com/papyrus_images/8b733ee208471c410f53a820d0c782552ecaf5f29577a822ed4796cf95f2dbda.png" alt="圖片" blurdataurl="data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACwAAAAAAQABAAACAkQBADs=" nextheight="600" nextwidth="800" class="image-node embed"><figcaption HTMLAttributes="[object Object]" class="">圖片</figcaption></figure><h3 id="h-decompile-tools" class="text-2xl font-header !mt-6 !mb-4 first:!mt-0 first:!mb-0">Decompile tools</h3><p><a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://etherscan.io/address/0xaE9C73fd0Fd237c1c6f66FE009d24ce969e98704#code">Etherscan-decompile bytecode</a> | <a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://library.dedaub.com/decompile">Dedaub</a> | <a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://github.com/Jon-Becker/heimdall-rs">heimdall-rs</a></p><p>Etherscan 內建有一個反編譯功能但可讀性偏差，個人比較常使用 Dedaub，可讀性好一點，也是常常最多人DM 問我都使用哪個工具反編譯。 我們拿一個 MEV Bot 被攻擊來當<a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://twitter.com/1nf0s3cpt/status/1577594615104172033">例子</a> 可以自己試試解看看 <a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://bscscan.com/address/0x64dd59d6c7f09dc05b472ce5cb961b6e10106e1d#code">例子</a></p><figure float="none" data-type="figure" class="img-center" style="max-width: null;"><img src="https://storage.googleapis.com/papyrus_images/6bc8bf71a0742b940fe19ef3a9931f2439e1ea5069153992504d981e35b78602.png" alt="截圖 2023-01-05 上午10 33 15" blurdataurl="data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACwAAAAAAQABAAACAkQBADs=" nextheight="600" nextwidth="800" class="image-node embed"><figcaption HTMLAttributes="[object Object]" class="">截圖 2023-01-05 上午10 33 15</figcaption></figure><figure float="none" data-type="figure" class="img-center" style="max-width: null;"><img src="https://storage.googleapis.com/papyrus_images/85a1ecba0697011d997460c0efa53d55d4b10a9d88fc6df3c5604ab53ab9d963.png" alt="圖片" blurdataurl="data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACwAAAAAAQABAAACAkQBADs=" nextheight="600" nextwidth="800" class="image-node embed"><figcaption HTMLAttributes="[object Object]" class="">圖片</figcaption></figure><h2 id="h-" class="text-3xl font-header !mt-8 !mb-4 first:!mt-0 first:!mb-0">第一課分享就先到這邊，想學更多可以參考以下學習資源。</h2><h2 id="h-" class="text-3xl font-header !mt-8 !mb-4 first:!mt-0 first:!mb-0">學習資源</h2><div data-type="youtube" videoId="HXgu239mPBc">
      <div class="youtube-player" data-id="HXgu239mPBc" style="background-image: url('https://i.ytimg.com/vi/HXgu239mPBc/hqdefault.jpg'); background-size: cover; background-position: center">
        <a href="https://www.youtube.com/watch?v=HXgu239mPBc">
          <img src="{{DOMAIN}}/editor/youtube/play.png" class="play"/>
        </a>
      </div></div><div data-type="youtube" videoId="9fcOffCg2ig">
      <div class="youtube-player" data-id="9fcOffCg2ig" style="background-image: url('https://i.ytimg.com/vi/9fcOffCg2ig/hqdefault.jpg'); background-size: cover; background-position: center">
        <a href="https://www.youtube.com/watch?v=9fcOffCg2ig">
          <img src="{{DOMAIN}}/editor/youtube/play.png" class="play"/>
        </a>
      </div></div><div data-type="youtube" videoId="90GN9Ut8LhU">
      <div class="youtube-player" data-id="90GN9Ut8LhU" style="background-image: url('https://i.ytimg.com/vi/90GN9Ut8LhU/hqdefault.jpg'); background-size: cover; background-position: center">
        <a href="https://www.youtube.com/watch?v=90GN9Ut8LhU">
          <img src="{{DOMAIN}}/editor/youtube/play.png" class="play"/>
        </a>
      </div></div><p><a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://degatchi.com/articles/reading-raw-evm-calldata">https://degatchi.com/articles/reading-raw-evm-calldata</a></p><p><a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://web3sec.xrex.io/">https://web3sec.xrex.io/</a></p>]]></content:encoded>
            <author>sunweb3sec@newsletter.paragraph.com (SunWeb3Sec)</author>
            <enclosure url="https://storage.googleapis.com/papyrus_images/47eaaf323a2ce660eb4d0f01e307465d8859950e3df8487c448c7b7dd5178889.png" length="0" type="image/png"/>
        </item>
        <item>
            <title><![CDATA[OnChain transaction debugging - Lesson 1: Tools]]></title>
            <link>https://paragraph.com/@sunweb3sec/onchain-transaction-debugging-lesson-1-tools</link>
            <guid>FUGLfXM6r7bAcPv28nT8</guid>
            <pubDate>Wed, 01 Feb 2023 13:35:49 GMT</pubDate>
            <description><![CDATA[Author: Sun Community Discord This article is published on XREX and WTF Academy Online resources were scarce when I started learning on-chain transaction analysis. Although slowly, l was able to piece together bits and pieces of information to perform tests and analysis. From my studies, we will launch a series of Web3 security articles to entice more people to join Web3 security and create a secure network together. In the first series, we will introduce how to conduct an on-chain analysis, ...]]></description>
            <content:encoded><![CDATA[<p>Author: <a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://twitter.com/1nf0s3cpt">Sun</a></p><p>Community <a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://discord.gg/Fjyngakf3h">Discord</a></p><p>This article is published on XREX and <a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://github.com/AmazingAng/WTF-Solidity#%E9%93%BE%E4%B8%8A%E5%A8%81%E8%83%81%E5%88%86%E6%9E%90">WTF Academy</a></p><p>Online resources were scarce when I started learning on-chain transaction analysis. Although slowly, l was able to piece together bits and pieces of information to perform tests and analysis.</p><p>From my studies, we will launch a series of Web3 security articles to entice more people to join Web3 security and create a secure network together.</p><p>In the first series, we will introduce how to conduct an on-chain analysis, and then we will reproduce on-chain attack(s). This skill will aid us in understanding the attack process, the root cause of the vulnerability, and even how the arbitrage robot arbitrages!</p><h2 id="h-tools-can-greatly-improve-efficiency" class="text-3xl font-header !mt-8 !mb-4 first:!mt-0 first:!mb-0">Tools can greatly improve efficiency</h2><p>Before getting into the analysis, allow me to introduce some common tools. The right tools can help you do research more efficiently.</p><h3 id="h-transaction-debugging-tools" class="text-2xl font-header !mt-6 !mb-4 first:!mt-0 first:!mb-0">Transaction debugging tools</h3><p><a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://phalcon.blocksec.com/">Phalcon</a> | <a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://tx.eth.samczsun.com/">Tx.viewer</a> | <a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://cruise.supremacy.team/">Cruise</a> | <a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://ethtx.info/">Ethtx</a> | <a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://dashboard.tenderly.co/explorer">Tenderly</a></p><p>Transaction Viewer is the most commonly used tool, it is able to list the stack trace of function calls and the input data in each function during the transaction. Transaction viewer tools are all similar; the major difference is the chain support and auxiliary functions support. I personally use Phalcon and Sam’s Transaction Viewer. If I encounter unsupported chains, I will use Tenderly. Tenderly supports most chains, But the readability is limited, and analysis can be slow using its Debug feature. It is however one of the first tools I learned along with Ethtx.</p><h4 id="h-chain-support-comparison" class="text-xl font-header !mt-6 !mb-3 first:!mt-0 first:!mb-0">Chain support comparison</h4><p>Phalcon： <code>Ethereum、BSC、Cronos、Avalanche C-Chain、Polygon</code></p><p>Sam&apos;s Transaction viewer： <code>Ethereum、Polygon、BSC、Avalanche C-Chain、Fantom、Arbitrum、Optimism</code></p><p>Cruise： <code>Ethereum、BSC 、Polygon、Arbitrum、Fantom、Optimism、Avalanche、Celo、Gnosis</code></p><p>Ethtx： <code>Ethereum、Goerli testnet</code></p><p>Tenderly： <code>Ethereum、Polygon、BSC、Sepolia、Goerli、Gnosis、POA、RSK、Avalanche C-Chain、Arbitrum、Optimism 、Fantom、Moonbeam、Moonriver</code></p><h4 id="h-lab" class="text-xl font-header !mt-6 !mb-3 first:!mt-0 first:!mb-0">Lab</h4><p>We will look at JayPeggers - Insufficient validation + Reentrancy <a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://github.com/SunWeb3Sec/DeFiHackLabs/#20221229---jay---insufficient-validation--reentrancy">Incident</a> as an example transaction <a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://phalcon.blocksec.com/tx/eth/0xd4fafa1261f6e4f9c8543228a67caf9d02811e4ad3058a2714323964a8db61f6">TXID</a> to dissect.</p><p>First I use the Phalcon tool developed by Blocksec to illustrate. The basic information and balance changes of the transaction can be seen in the figure below. From the balance changes, we can quickly see how much profit the attacker has made. In this example, the attacker made a profit of 15.32 ETH.</p><figure float="none" data-type="figure" class="img-center" style="max-width: null;"><img src="https://storage.googleapis.com/papyrus_images/d581e2d1627d0c72ee3f7b20bc6e7704512eaff69c8956ab2550b4913dee3ca5.png" alt="210571234-402d96aa-fe5e-4bc4-becc-190bd5a78e68-2" blurdataurl="data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACwAAAAAAQABAAACAkQBADs=" nextheight="600" nextwidth="800" class="image-node embed"><figcaption HTMLAttributes="[object Object]" class="">210571234-402d96aa-fe5e-4bc4-becc-190bd5a78e68-2</figcaption></figure><p>Invocation Flow Visualization - Is function invocation with trace-level information and event logs. It shows us the call invocation, the function call level of this transaction, whether flash loan is used, which projects are involved, which functions are called, and what parameters and raw data are brought in, etc.</p><figure float="none" data-type="figure" class="img-center" style="max-width: null;"><img src="https://storage.googleapis.com/papyrus_images/baa0ad0b81bc9e0328aae98f8cd173b0e81cbb5aea420b3efddbd50018e455bf.png" alt="圖片" blurdataurl="data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACwAAAAAAQABAAACAkQBADs=" nextheight="600" nextwidth="800" class="image-node embed"><figcaption HTMLAttributes="[object Object]" class="">圖片</figcaption></figure><p>Phalcon 2.0 added funds flow, and Debug + source code analysis directly shows the source code, parameters, and return values along with the trace, which is more convenient for analysis.</p><figure float="none" data-type="figure" class="img-center" style="max-width: null;"><img src="https://storage.googleapis.com/papyrus_images/ac27c3e0c1ff4634e4e27a64ededd2a4e3eaba579fcc4999f7ed0a39c9e592ec.png" alt="image" blurdataurl="data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACwAAAAAAQABAAACAkQBADs=" nextheight="600" nextwidth="800" class="image-node embed"><figcaption HTMLAttributes="[object Object]" class="">image</figcaption></figure><p>Now let&apos;s try Sam&apos;s Transaction Viewer on the same <a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://tx.eth.samczsun.com/ethereum/0xd4fafa1261f6e4f9c8543228a67caf9d02811e4ad3058a2714323964a8db61f6">TXID</a>. Sam integrates many tools in it, as shown in the picture below, you can see the change in Storage and the Gas consumed by each call.</p><figure float="none" data-type="figure" class="img-center" style="max-width: null;"><img src="https://storage.googleapis.com/papyrus_images/0d099eb3fb47a6d6db56327004c6a03210b8266bc46e60337ec18f13092cd8a0.png" alt="210574290-790f6129-aa82-4152-b3e1-d21820524a0a-2" blurdataurl="data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACwAAAAAAQABAAACAkQBADs=" nextheight="600" nextwidth="800" class="image-node embed"><figcaption HTMLAttributes="[object Object]" class="">210574290-790f6129-aa82-4152-b3e1-d21820524a0a-2</figcaption></figure><p>Click Call on the left to decode the raw Input data.</p><figure float="none" data-type="figure" class="img-center" style="max-width: null;"><img src="https://storage.googleapis.com/papyrus_images/1e4a29a4f87231434c28eca23d8d25768aa7d3f7847d7107bfd96cc95354116d.png" alt="圖片" blurdataurl="data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACwAAAAAAQABAAACAkQBADs=" nextheight="600" nextwidth="800" class="image-node embed"><figcaption HTMLAttributes="[object Object]" class="">圖片</figcaption></figure><p>Let&apos;s now switch to Tenderly to analyze the same <a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://dashboard.tenderly.co/tx/mainnet/0xd4fafa1261f6e4f9c8543228a67caf9d02811e4ad3058a2714323964a8db61f6">TXID</a>, you can see the basic information like other tools. But using the Debug feature, it is not visualized and needs to be analyzed step by step. However, the advantage is that you can view the code and the conversion process of Input data while Debugging.</p><figure float="none" data-type="figure" class="img-center" style="max-width: null;"><img src="https://storage.googleapis.com/papyrus_images/17a3f9018c0e22761ae7f8322f6297fd628c34cb2c59cf3cd5d929c2c215d20b.png" alt="圖片" blurdataurl="data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACwAAAAAAQABAAACAkQBADs=" nextheight="600" nextwidth="800" class="image-node embed"><figcaption HTMLAttributes="[object Object]" class="">圖片</figcaption></figure><p>This can help us clarify all the things this transaction did. Before writing the POC, can we run a replay attack? Yes! Both Tenderly or Phalcon support simulated transactions, you can find a button Re-Simulate in the upper right corner in the figure above. The tool will automatically fill the parameter values from the transaction for you as shown in the figure below. Parameters can be changed arbitrarily according to simulation needs, such as changing block number, From, Gas, Input data, etc.</p><figure float="none" data-type="figure" class="img-center" style="max-width: null;"><img src="https://storage.googleapis.com/papyrus_images/e3e44575d9c7313bc106dd6f776cfbd6cbc4cc2b4aa9d5b57270e4dc0f32485a.png" alt="圖片" blurdataurl="data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACwAAAAAAQABAAACAkQBADs=" nextheight="600" nextwidth="800" class="image-node embed"><figcaption HTMLAttributes="[object Object]" class="">圖片</figcaption></figure><h3 id="h-ethereum-signature-database" class="text-2xl font-header !mt-6 !mb-4 first:!mt-0 first:!mb-0">Ethereum Signature Database</h3><p><a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://www.4byte.directory/">4byte</a> | <a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://sig.eth.samczsun.com/">sig.eth</a> | <a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://www.etherface.io/hash">etherface</a></p><p>In the Raw Input data, the first 4 bytes are Function Signatures. Sometimes if Etherscan or analysis tools cannot identify the function, we may check the possible Functions through the Signature Database.</p><p>The following example assumes that we do not know what Function <code>0xac9650d8</code> is</p><figure float="none" data-type="figure" class="img-center" style="max-width: null;"><img src="https://storage.googleapis.com/papyrus_images/6a8a173f505add177b3efa0ec4c4be64bfea0268ef23eb903b756853a74f0e98.png" alt="image" blurdataurl="data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACwAAAAAAQABAAACAkQBADs=" nextheight="600" nextwidth="800" class="image-node embed"><figcaption HTMLAttributes="[object Object]" class="">image</figcaption></figure><p>Through a sig.eth query, we find that the 4 bytes signature is <code>multicall(bytes[])</code></p><figure float="none" data-type="figure" class="img-center" style="max-width: null;"><img src="https://storage.googleapis.com/papyrus_images/47e21120b6e58d90f4fd17c27d08c0f9b8917627e10c2fffcafa2a7406965d5e.png" alt="圖片" blurdataurl="data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACwAAAAAAQABAAACAkQBADs=" nextheight="600" nextwidth="800" class="image-node embed"><figcaption HTMLAttributes="[object Object]" class="">圖片</figcaption></figure><h3 id="h-useful-tools" class="text-2xl font-header !mt-6 !mb-4 first:!mt-0 first:!mb-0">Useful tools</h3><p><a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://gnidan.github.io/abi-to-sol/">ABI to interface</a> | <a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://abi.w1nt3r.xyz/">Get ABI for unverified contracts</a> | <a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://apoorvlathey.com/eth-calldata-decoder/">ETH Calldata Decoder</a> | <a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://www.ethcmd.com/">ETHCMD - Guess ABI</a></p><p>ABI to interface: When developing a POC, it&apos;s necessary to have interfaces to call other contracts. We can use this tool to help you quickly generate the interfaces. Go to Etherscan to copy the ABI, and paste it on the tool to see the generated Interface. <a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://etherscan.io/address/0xb3da8d6da3ede239ccbf576ca0eaa74d86f0e9d3#code">Example</a>.</p><figure float="none" data-type="figure" class="img-center" style="max-width: null;"><img src="https://storage.googleapis.com/papyrus_images/9ee380f7356ffcda3c055f43fe3dcb1777505186f2c0cc185e9fdddfefc6b6ee.png" alt="圖片" blurdataurl="data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACwAAAAAAQABAAACAkQBADs=" nextheight="600" nextwidth="800" class="image-node embed"><figcaption HTMLAttributes="[object Object]" class="">圖片</figcaption></figure><p>ETH Calldata Decoder: If you want to decode Input data without the ABI, this is the tool you need. Sam&apos;s transaction viewer I introduced earlier also supports Input data decoding.</p><figure float="none" data-type="figure" class="img-center" style="max-width: null;"><img src="https://storage.googleapis.com/papyrus_images/6cf2891f079beef0f63fa1416d4938a83a61af00fc6f700acfddd2b7f06c4d00.png" alt="圖片" blurdataurl="data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACwAAAAAAQABAAACAkQBADs=" nextheight="600" nextwidth="800" class="image-node embed"><figcaption HTMLAttributes="[object Object]" class="">圖片</figcaption></figure><p>Obtain ABI for unverified contracts: If you encounter a contract that is not verified, you can use this tool to try to work out the function signatures. <a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://abi.w1nt3r.xyz/mainnet/0xaE9C73fd0Fd237c1c6f66FE009d24ce969e98704">Example</a></p><figure float="none" data-type="figure" class="img-center" style="max-width: null;"><img src="https://storage.googleapis.com/papyrus_images/8b733ee208471c410f53a820d0c782552ecaf5f29577a822ed4796cf95f2dbda.png" alt="圖片" blurdataurl="data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACwAAAAAAQABAAACAkQBADs=" nextheight="600" nextwidth="800" class="image-node embed"><figcaption HTMLAttributes="[object Object]" class="">圖片</figcaption></figure><h3 id="h-decompile-tools" class="text-2xl font-header !mt-6 !mb-4 first:!mt-0 first:!mb-0">Decompile tools</h3><p><a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://etherscan.io/address/0xaE9C73fd0Fd237c1c6f66FE009d24ce969e98704#code">Etherscan-decompile bytecode</a> | <a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://library.dedaub.com/decompile">Dedaub</a> | <a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://github.com/Jon-Becker/heimdall-rs">heimdall-rs</a></p><p>Etherscan has a built-in decompilation feature, but the readability of the result is often poor. Personally, I often use Dedaub, which produces better decompiled code. It is my recommended decompiler. Let&apos;s use a MEV Bot being attacked as an example You can try to decompile it for yourself using this <a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://twitter.com/1nf0s3cpt/status/1577594615104172033">contract</a>.</p><p>First, copy the Bytecodes of the unverified contract and paste it on Dedaub, and click Decompile.</p><figure float="none" data-type="figure" class="img-center" style="max-width: null;"><img src="https://storage.googleapis.com/papyrus_images/6bc8bf71a0742b940fe19ef3a9931f2439e1ea5069153992504d981e35b78602.png" alt="截圖 2023-01-05 上午10 33 15" blurdataurl="data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACwAAAAAAQABAAACAkQBADs=" nextheight="600" nextwidth="800" class="image-node embed"><figcaption HTMLAttributes="[object Object]" class="">截圖 2023-01-05 上午10 33 15</figcaption></figure><figure float="none" data-type="figure" class="img-center" style="max-width: null;"><img src="https://storage.googleapis.com/papyrus_images/85a1ecba0697011d997460c0efa53d55d4b10a9d88fc6df3c5604ab53ab9d963.png" alt="圖片" blurdataurl="data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACwAAAAAAQABAAACAkQBADs=" nextheight="600" nextwidth="800" class="image-node embed"><figcaption HTMLAttributes="[object Object]" class="">圖片</figcaption></figure><p>If you want to learn more, you can refer to the following videos.</p><h2 id="h-resources" class="text-3xl font-header !mt-8 !mb-4 first:!mt-0 first:!mb-0">Resources</h2><div data-type="youtube" videoId="HXgu239mPBc">
      <div class="youtube-player" data-id="HXgu239mPBc" style="background-image: url('https://i.ytimg.com/vi/HXgu239mPBc/hqdefault.jpg'); background-size: cover; background-position: center">
        <a href="https://www.youtube.com/watch?v=HXgu239mPBc">
          <img src="{{DOMAIN}}/editor/youtube/play.png" class="play"/>
        </a>
      </div></div><div data-type="youtube" videoId="9fcOffCg2ig">
      <div class="youtube-player" data-id="9fcOffCg2ig" style="background-image: url('https://i.ytimg.com/vi/9fcOffCg2ig/hqdefault.jpg'); background-size: cover; background-position: center">
        <a href="https://www.youtube.com/watch?v=9fcOffCg2ig">
          <img src="{{DOMAIN}}/editor/youtube/play.png" class="play"/>
        </a>
      </div></div><div data-type="youtube" videoId="90GN9Ut8LhU">
      <div class="youtube-player" data-id="90GN9Ut8LhU" style="background-image: url('https://i.ytimg.com/vi/90GN9Ut8LhU/hqdefault.jpg'); background-size: cover; background-position: center">
        <a href="https://www.youtube.com/watch?v=90GN9Ut8LhU">
          <img src="{{DOMAIN}}/editor/youtube/play.png" class="play"/>
        </a>
      </div></div><p><a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://degatchi.com/articles/reading-raw-evm-calldata">https://degatchi.com/articles/reading-raw-evm-calldata</a></p><p><a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://web3sec.xrex.io/">https://web3sec.xrex.io/</a></p>]]></content:encoded>
            <author>sunweb3sec@newsletter.paragraph.com (SunWeb3Sec)</author>
            <enclosure url="https://storage.googleapis.com/papyrus_images/47eaaf323a2ce660eb4d0f01e307465d8859950e3df8487c448c7b7dd5178889.png" length="0" type="image/png"/>
        </item>
    </channel>
</rss>