Excellent thread about metamorphic contracts by @0xOwenThurm !
Here is my contribution on how they can still be created for visual learners like me:
I just received my first compensation for an audit on @code4rena! 🥹😍
5
51
2,902
getting to the finish line on damnvulnerabledefi.xyz (12/15), feeling astonished by how much I've learned so far and would definitely recommend doing them for every solidity devs and junior auditors.
Props to this amigo @tinchoabbate 🐐
2
4
501
Uniswap cheatsheet - basic concepts
Reference: github.com/runtimeverificati…
A bad actor can send a transaction from your wallet if you sign a message like this.
Let's reproduce this attack:
This thread explains a vulnerability on signature verification.
(v, r, s) and (v, r, -s mod(n)) are valid signatures for the same message hash 🚩
n comes from secp256k1 specs github.com/ethereum/go-ether…
Openzeppelin address this on ECDSA library by accepting only the lower s value.
This 15 byte sized contract calculates the sum of two numbers.
I created it using only EVM opcodes, no solidity and no compilation involved.
Goerli: goerli.etherscan.io/address/…
Come see how I did it
Understanding the contract creation bytecode 📋
To deploy a contract you have to send a transaction to the null address (not to be confused with 0x000... address) with the contract creation bytecode as the transaction data = <init code><contract bytecode><constructor arguments>
Here on my example:
on the not indexed event, LOG1 is being called on top of the following stack:
0x00...80 (offset)
0x00...20 (size)
0x49...91 (topic)
it is getting the address value from memory so LOG1 is actually costing 1006 because of the memory expansion
Replying to @0xmonsoon
At the beginning this thread might have read like one where I introduce you to some EVM quirk and use it explain the scenario.
But no. I don't know why this is happening. Have spent a couple of hours looking but not been able to figure it out.
Please explain it to me anon 😭😭
That's a very clever way to implement a reentrancy guard 🤔
Reading a variable from storage twice costs different amount of gas each time because the slot will be warm after the first read.
This is the coolest thing I've seen today. Reentrancy guard that reverts if the slot is warm.
Author: @rage_pit
@transmissions11 Can we use this in the golf-course!?
1
173
I just joined @code4rena as a warden 🫡
I am still early on my studies but let's start getting used to the contests 🔥
1
6
392
An outstanding article about the EVM stack, worth a read
It’s out! 🪂 🎉
Part 4 of the All About Solidity Data Locations subseries
Some cool learnings:
- how block scopes work {} (the mystery unraveled)
- what is the « call stack » vs the « stack »
- fixing « stack too deep » error
#Solidity #SmartContracts
betterprogramming.pub/solidi…
1
2
215
@JeanCavallera I would love to see one example of the stack underflow in practice. I tried some inline assembly but couldn't reproduce this
1
1
119
Shouldn't it throw the compiler error "Stack too deep" ? 🤔
How was this code compiled with solidity 0.8.17 and the function was able to access items deeper than index 16 in the stack?
1
3
312
That's because you can change the way the EVM compiles your contract. Instead of going straight from solidity to EVM opcodes you can use the "intermediate representation in Yul" by adding
"viaIR": true
to your compiler_config.json
docs.soliditylang.org/en/v0.…
2
3
249
Arithmetic overflow/underflow, the unchecked block and how you can save gas on math operations.
In solidity version < 0.8 there is no arithmetic check and if an overflow happens the variable simply cycle back to zero and in an underflow it goes to the maximum possible value.
1
7
305
Have you ever seen a stack overflow on EVM?
I guess you wouldn't find it everyday in the wild. But let's push through the limits and see how it shows up, shall we?🤠
Let's start by creating a function with 1022 local variables.
Have you ever ran into this compile error?
That happens when your function declares too much local variables, right?
Not really... Your function can have hundreds of local variables but you can only access the last 16 of them because we only have the opcodes DUP1 to DUP16
Two functions doing the same thing, but the latter is 100+ gas more expensive🤔
The EVM will take a shortcut on the first function and use the opcode SELFBALANCE (5 gas) to evaluate address(this).balance
61 0100 60 40 52
(61) PUSH2 0x0100
(60) PUSH1 0x40
(52) MSTORE
It tells us that the memory between 0x80 and 0x00ff are in use (4x 32bytes) and they store the 4 immutable variables: _factoryV2, _positionManager, factoryV3, _WETH
Replying to @0xmonsoon
13/n
This is usually the case but there do exist contracts which don't start with 0x6080604052.
Like the Uniswap V3 Router 2: etherscan.io/address/0x68b34…
Can you figure out why anon?
2
174





































