Author: SunSec
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, 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!
Before getting into the analysis, allow me to introduce some common tools. The right tools can help you do research more efficiently.
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'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.
Phalcon: Ethereum、BSC、Cronos、Avalanche C-Chain、Polygon
Ethtx: Ethereum、Goerli testnet
Tendery: Ethereum、Polygon、BSC、Sepolia、Goerli、Gnosis、POA、RSK、Avalanche C-Chain、Arbitrum、Optimism 、Fantom、Moonbeam、Moonriver
We will look at JayPeggers - Insufficient validation + Reentrancy Incident as an example transaction TXID to dissect.
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.
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.
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.
Now let's try Sam's Transaction Viewer on the same TXID. 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.
Click Call on the left to decode the raw Input data.
Let's now switch to Tendery to analyze the same TXID, 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.
This can help us clarify all the things this transaction did. Before writing the POC, can we run a replay attack? Yes! Both Tendery 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.
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.
The following example assumes that we do not know what Function 0xac9650d8
is
Through a sig.eth query, we find that the 4 bytes signature is multicall(bytes[])
ABI to interface | Get ABI for unverified contracts | ETH Calldata Decoder | ETHCMD - Guess ABI
ABI to interface: When developing POC, you need to call other contracts but you need an interface. 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. Example.
ETH Calldata Decoder: If you want to decode Input data without the ABI, this is the tool you need. Sam's transaction viewer I introduced earlier also supports Input data decoding.
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. Example
Etherscan-decompile bytecode | Dedaub | heimdall-rs
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's use a MEV Bot being attacked as an example You can try to decompile it for yourself using this contract.
First, copy the Bytecodes of the unverified contract and paste it on Dedaub, and click Decompile.
If you want to learn more, you can refer to the following videos.
samczsun's eth txn explorer and vscode extension
Vulnerabilities in DeFi by Daniel V.F.
Tenderly.co - Debug Transaction