EVMole: function selectors and arguments from bytecode
ETHCC 7 · 11 Jul 2024 · 18:04
Maxim Andreev presents EVMole, his library with Rust, JavaScript and Python implementations, that extracts function selectors and arguments from EVM bytecode so a contract's ABI can be rebuilt without source code. He built a reproducible benchmark using Etherscan-verified contracts as ground truth, covering the 1,000 largest and 50,000 random contracts, and says EVMole is faster and more accurate than existing tools. It runs a custom virtual machine that labels data as it executes and forks at branch comparisons. He says it listed more public functions than Etherscan for some verified contracts, an Etherscan bug since fixed, so even verified code is worth checking. He notes limits such as telling string from bytes.
Speakers
Topics
From ETHCC 7, hosted by Ethereum France.
Transcript
Hello, thank you for coming. Uh today I will talk to you about my library eVmol. Uh who am I? My name is Maxim Andreev, I'm software developer, and this is my pet project. You can find me by handle cdump on GitHub, on Twitter, and on Telegram. So if you have any questions, you can write me after the talk. So today I will talk to you about what is the function selector, uh, why do we need to extract it from a verified contract? Uh I will overview the existing solutions, provide some benchmarks, and then talk why evmoll my new library is better than all existing solutions, and uh talk a little bit about the future plans of the e vmol. So what is e evmall?
Evmole is a uh library, it uh has free implementations in Rust, in JavaScript and in Python, so you can use it from your favorite language, uh which extract function selectors and function arguments, so contract ABI uh from bytecode for so you can use it for unverified contracts when you don't have source code. It's have uh clean code with zero dependencies, it's faster and more accurate than other existing solutions, and it's tested very well on Solidity and Viper contracts. So if you have this simple contract which is token with one public function, and we ask the Solidity compiler to compile it, we can ask for the binary runtime, which is the code which will be deployed to the blockchain.
And also we can ask for the ABI of the contract, which is uh like JSON with all public functions, all events and so on. And if you ever see if your scan uh on the verified contract, then you can notice their read and write contract uh tabs, which is generated from the contract ABI. And if you visit AFR scan for unverified contract, all you can see is just bytecode. So why do we need such tool for unverified contract for extraction of the tools? At first, just for fun, it was interesting for me to learn how it works internally.
It could be used in security audits and finding bugs. Like uh I noticed when I developed this tool that uh there's some contracts, verified contracts on if your scan, but my tool produces uh more public functions than if your scan. So I thought at first that maybe some bugs in my tool, but after using the compiler as like DDo the compiler after debugging it, it's turned out that uh if your scan has a bug, they fixed it in one hour, but it's um it's not okay, like you see the source code, you think what I see is what I get, but then some hidden function, it could be malicious function, so it's better to verify even verified contract with my tool. So uh existing tools.
It's uh what's a what's ABI uh written in JavaScript, actually TypeScript Hemdoll, handle, a vm hound, and git up the compiler. But what's wrong with them? We don't know how accurate are them. There's no compression like uh this one is better, which produce like 10 errors on that data set and so on. So the first thing that I did is uh get write a benchmark. So I get verified contract, uh split into two data sets, uh top one K contract with the largest by the side, so probably it's more most difficult contracts, and uh 50k random contracts. After that, I download ground roof ABI from the if scan because it's verified contracts.
I already have ABI in JSON format, and then I write some scripts which uh produce the output, so the input is the data set and the output is this JSON files. For the selectors, the key is the contract name and the uh value is the array of selectors. And for the arguments, the key is the contract uh address, then selector, and then function arguments. Uh I wrote the same thing for all existing solutions, like for what's IB just as easy like uh iterate all the directory with the contracts and apply what's IB selectors from bytecode. I packed it into the Docker, and after that with this big magic make file uh with some magic, I will able to make reproducible and easy to run benchmarks for all existing tools, like with one command I can like uh on the if scan, like extract arguments on the largest one k data set.
And then I can process the output this uh compare pie script, which just uh like using ground truth apperscan as ground truth and the other provider as other one so uh let's assume that the iferscan got us four selectors and some other tool got us three selectors we can split the errors into uh groups it's false negative and full positive and I count it uh differently in results so and for results uh yes and for arguments uh if ever scan produce free arguments like unsigned dangerous string string and some tool produce unsigned integer bytes bytes then I count this as error and uh the same thing for the fixed size arrays so uh I have another uh mode in compare pie script like fixed size array tuples and string by's can be uh not count as errors because uh the encoding ABI encoding is the same for them but uh in the next table I'll count that for errors so actually the number of errors uh on the next slide is uh lower than you can see and for results as you can see for the function selectors I compare on the free data sets to solidity datasets and one wiper datasets and the less uh the lesser number of the errors is better so if mole have like uh nine errors was uh while what side have uh 11 errors and counter has like 3k errors and the same thing for all other data sets.
And you can notice that the running time of the EV mole is about 10 seconds on random 50k contracts and the other solutions more. Takes more time. And about function arguments, not all these solutions can extract function arguments without external database. So I compare only Hemdol. And you can see on the largest 1K datasets, I have about 42% of errors and EV mall 14% of errors. And on the random 50k, it's 4.5% of errors.
But if I apply uh mentioned previously, like the string is equal to bytes and so on, it's less than one percent of errors on the data set. So how it works internally. As you know, if you deploy contracts on the EVM blockchain, it will be executed on Ethereum virtual machine. If you want to call the contract, want to call balance of function, you need to encode the code data, and then inside the contract, uh the code will split, will take the first four bytes of the call data as function selector, and the other part of the function arguments. And they have uh pretty big comments explaining how they generate this uh internal selector, but in different versions there are some differences.
So but the main idea you just get the first four bytes and then you compare for all known functions and the then call the functions, like make conditional jump. And if you can when you compare selector, you just uh like push for uh constant, call equal of code, and then make additional jump. But it's not so easy because uh different versions of compilers, different uh optimizers run, different compilers like Solidity and Viper handles it differently, so it's not so easy to uh make it in a static way, like finding some patterns. So I decided to write my own implementation of virtual machine.
The main element of all EVMO uh virtual machine is element, it's just uh labeled bytes. So we have some bytes and we can apply some label on it. And the label will uh go through all the operations, like if we have stack, this is just a vector of the elements, we can push, we can pick, and the uh type of the element of the stack is this element of this label. So the implementation of virtual machine is quite easy, like we have code, we have uh pointer stack, stack uh pointer counter, stack memory, and so on. And uh execution of the step of virtual machine is quite easy.
If we have code size opcode, we just push uh one of the hot to the stack. And if we have like add opcode, we uh push the result of the operation to the stack, but also we return the row uh operand of this operation to uh to be able to analyze the result uh later. So I can show you it right now. This is how uh function selectors extraction works. If we have uh shift write operation, and uh after executing that operation, we find out that the value of the stack uh is the first four bytes of the call data, then we label it as a signature. And after that, if we find out operation of equal of XOR or SUP, and uh the first argument is the signature of the second argument of the signature, uh then probably it's the function selector.
So we just add to the selectors uh set. And uh the tricky part, uh, if uh SaliG compilers finds out that there are many functions, they generate a binary tree to make the search of the write function in uh all local and way. So if we uh find Lt or GG operand, we need just to fork our virtual machine to take the both branches of the Lt and GT. And this is how it's look like we have the push, we have call data load, and after that we uh put something on the stack with the label code data.
Uh we execute shift write, after shift write, we find out that the value on the stack is the same as the uh first four bytes of the call data, so we mark it as a signature, and after that we can see that there's gt operation on the signature and some constant, so we fork at this point and execute both parts of the of this if statement. And for the arguments, uh it's pretty the same, but we need to analyze more opcodes like end opcodes.
Uh, if we find out that something uh that the contract executed end opcode on some part of the code data, then probably it's unsigned integer or address or bytes. So it's like just uh find out is it representable in this way or in this way. And this is some example uh that evm can extract such difficult uh function types like we have a tuple of unsigned integers and a dynamic array of that tuple, or even this long, long, long uh argument, and evmol can extract it without source code with zero errors. And uh it's notable, noticeable that uh evmol is the only tool that can work with wipers or from one selector's tables.
It's uh you can read about this in this link. It's uh nothing difficult, but it was easy to implement, so I implemented it. So even more case to success. Uh just label data, just control gas usage. Yes, I didn't mention it. I uh count the gas on the each step, and then I'm out of the gas, so I just stop execution. Uh for on GTLT with uh proper gas limit set, and do not implement 100 opcodes because uh if we uh finding our selectors, we don't need some uh cryptographic function and so on. So usage of EMO is quite simple.
If you like JS, you can just import two public function and pass the bytecode as input and get the uh function argument uh function selector as output, or you can get function arguments, you can need to pass code and the uh function selector, you get the string with the function arguments. The same in Rust, the same in Python, and you can use it from the cast tool from the foundry, which uses even more Rust implementation. You can just uh cast get code from the uh RPC node and cast selectors, you get all the selectors all their function arguments. Uh I also have online demo with uh JavaScript implementation, you can pass contract bytecode, or just pass contract address, and the bytecode will be fetched from the RPC node.
And the plans for the future, I know the true EVM.storage exists, and they have uh open source part uh when they can extract storage layout only from the bytecode, but I think that maybe evmore solution can outperform it in terms of accuracy, but this is just a plans. So I think I have time. Thank you for your attention. Okay, we have uh two or three minutes.
Hi, uh, do you have any breakdown on the percentage of errors based on the Solidity versions? Uh I don't have. From the top of your head, like is it failing more on the older versions, newer versions? Um I don't have such stats, but uh I guess I'm I took uh random 50k contracts, and uh in terms of selectors, uh actually there's errors in the this table. But uh I can describe you that it's not errors.
It's like uh it's impossible to get right selector, like it's a fallback function and so on. So actually it's a zero errors in here should be. Okay, okay. Uh and another question is like on the errors, there are actually errors, any common pattern on them? Uh yeah, especially in the function arguments because uh it's uh impossible to distinguish string and bytes, it's impossible to distinguish address and unsigned integer of the LAN 160, and so on. And uh last question is are you are you doing any work in terms of uh converting the selectors that you identify in the arguments into uh readable format?
Um what do we mean by readable format? It's like converting then into actually what's the function name, maybe by uh comparing against GitHub or uh you can do it externally because my library don't use any external calls, it's like totally offline. I think it should be built on top of my library. Yeah, okay. Thanks. Thank you. Okay, we have time for uh one quick question. Yes. Okay.
Okay, thank you, everybody.
Related talks
- Reconstructing Control Flow Graphs from EVM Bytecode | Maxim Andreev, EVMole | ETHTaipei 2025
- EthereumJS Javascript Ecosystem/Sharding by Holger Drewes, Ethereum Meetup Vienna, October 2018
- J2 PAIN 04 Fredrik Harrysson Running Rust contracts on Kovan with WASM
- EVM.lua: Supercharging Ethereum calls with in-memory execution
- Introduction to Smart Contracts
- #239 Grigore Rosu: The K framework - a framework to formally define all programming languages
- The Road to ETH 2.0: How to Build EWASM DApps with Embark v5 (Devcon5)
- EVM - Some Assembly Required by Alex Bazhenov | Devcon Bogotá
- Reconstructing Control Flow Graphs from EVM Bytecode: Faster, Better, Stronger