$npx -y skills add transilienceai/communitytools --skill blockchain-securitySmart contract security testing and blockchain CTF exploitation. Covers Solidity vulnerability analysis, EVM storage manipulation, delegatecall attacks, CREATE/CREATE2 address prediction, and common DeFi exploit patterns. Use when analyzing Solidity contracts, solving blockchain
| 1 | # Blockchain Security |
| 2 | |
| 3 | ## Quick Start |
| 4 | 1. Download and decompile contracts (source or bytecode) |
| 5 | 2. Map storage layout and identify privileged operations |
| 6 | 3. Check for delegatecall, CREATE address prediction, reentrancy, access control |
| 7 | 4. Deploy exploit contracts via web3.py or cast/forge |
| 8 | 5. Verify win condition (isSolved/flag endpoint) |
| 9 | |
| 10 | ## Blockchain CTF Challenge Pattern |
| 11 | ```bash |
| 12 | # Get connection info |
| 13 | curl http://$HOST:$PORT/connection_info # -> PrivateKey, Address, TargetAddress, setupAddress |
| 14 | # RPC endpoint |
| 15 | RPC_URL="http://$HOST:$PORT/rpc" |
| 16 | # Win condition: Setup.isSolved() must return true |
| 17 | ``` |
| 18 | |
| 19 | ## Key Attack Vectors |
| 20 | |
| 21 | ### 1. Delegatecall Storage Manipulation |
| 22 | When contract A does `delegatecall` to contract B, B's code runs with A's storage. |
| 23 | - Deploy exploit contract that mirrors A's storage layout |
| 24 | - Exploit contract writes to A's storage slots via delegatecall |
| 25 | - **Critical**: Storage layout must match exactly (same slot ordering) |
| 26 | - See [reference/delegatecall-attacks.md](reference/delegatecall-attacks.md) |
| 27 | |
| 28 | ### 2. CREATE Address Prediction (Nonce Manipulation) |
| 29 | Contract addresses from CREATE are deterministic: `keccak256(rlp([sender, nonce]))[12:]` |
| 30 | - Brute-force nonce to find which nonce produces target address |
| 31 | - Send dummy transactions (self-transfers) to increment nonce |
| 32 | - Deploy exploit contract at the exact nonce that hits target address |
| 33 | - See [reference/create-address-prediction.md](reference/create-address-prediction.md) |
| 34 | |
| 35 | ### 3. Storage Layout & Slot Computation |
| 36 | - Mappings: `keccak256(h(key) || uint256(slot_number))` |
| 37 | - Value types: `h(k) = abi.encode(k)` (left-padded to 32 bytes) |
| 38 | - String/bytes: `h(k) = keccak256(k)` |
| 39 | - Read private variables via `eth_getStorageAt` |
| 40 | - See [reference/storage-layout.md](reference/storage-layout.md) |
| 41 | |
| 42 | ### 4. Empty Array / Zero-Length Input Bypass |
| 43 | When a function loops over a user-supplied array to validate items (signatures, approvals, votes), passing an **empty array** skips the loop entirely. If there's no minimum-length check, validation is bypassed. |
| 44 | - Check: `for (uint i = 0; i < arr.length; i++)` with no `require(arr.length >= N)` |
| 45 | - Exploit: Call the function with `[]` to skip all validation |
| 46 | |
| 47 | ### 5. ECDSA Signature Malleability |
| 48 | Raw `ecrecover` accepts both `(v, r, s)` and `(v', r, N-s)` (where N = secp256k1 order, v flipped 27↔28). If a contract deduplicates signatures by hash of raw bytes, the malleable form has a different hash but recovers to the same signer. |
| 49 | - Check: `ecrecover` used without `s <= N/2` enforcement (OpenZeppelin's ECDSA.sol enforces this) |
| 50 | - Exploit: Take a known valid signature, compute `new_s = N - s`, flip `v`, submit as "new" signature |
| 51 | |
| 52 | ### 6. Common Vulnerability Classes |
| 53 | | Vulnerability | Check | |
| 54 | |---|---| |
| 55 | | Reentrancy | External calls before state updates | |
| 56 | | Access control | Missing onlyOwner / msg.sender checks | |
| 57 | | Integer overflow | Solidity < 0.8.0 without SafeMath | |
| 58 | | Delegatecall injection | User-controlled delegatecall target | |
| 59 | | tx.origin auth | `tx.origin` instead of `msg.sender` | |
| 60 | | Selfdestruct | Force-send ETH, reset contract nonce | |
| 61 | | Weak randomness | blockhash/timestamp as entropy source | |
| 62 | | Empty array bypass | Loop validation with no min-length check | |
| 63 | | Signature malleability | Raw ecrecover without s-normalization | |
| 64 | |
| 65 | ## Tools |
| 66 | ```python |
| 67 | # web3.py essentials |
| 68 | from web3 import Web3 |
| 69 | w3 = Web3(Web3.HTTPProvider(RPC_URL)) |
| 70 | acct = w3.eth.account.from_key(PRIVATE_KEY) |
| 71 | |
| 72 | # Read private storage |
| 73 | w3.eth.get_storage_at(contract_addr, slot) |
| 74 | |
| 75 | # Deploy contract |
| 76 | from solcx import compile_source, install_solc |
| 77 | install_solc("0.8.13") |
| 78 | compiled = compile_source(source, output_values=["abi", "bin"], solc_version="0.8.13") |
| 79 | |
| 80 | # Send raw bytecode deployment |
| 81 | tx = {'data': bytecode, 'gas': 3000000, 'gasPrice': w3.eth.gas_price, 'nonce': nonce, 'chainId': chain_id} |
| 82 | signed = acct.sign_transaction(tx) |
| 83 | w3.eth.send_raw_transaction(signed.raw_transaction) |
| 84 | ``` |
| 85 | |
| 86 | ## Reference |
| 87 | - [Delegatecall Attacks](reference/delegatecall-attacks.md) |
| 88 | - [CREATE Address Prediction](reference/create-address-prediction.md) |
| 89 | - [Storage Layout](reference/storage-layout.md) |
| 90 | |
| 91 | ## Critical Rules |
| 92 | - Always read storage before attacking (private vars are readable on-chain) |
| 93 | - Mirror exact storage layout when exploiting delegatecall |
| 94 | - For CREATE nonce brute-force, check nonces 0-100000+ systematically |
| 95 | - CTF instances are often ephemeral -- script the full attack for speed |