Security analysis tool for EVM bytecode. Supports smart contracts built for Ethereum, Hedera, Quorum, Vechain, Roostock, Tron and other EVM-compatible blockchains.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
mythril/contractstorage.py

82 lines
2.1 KiB

from rpc.client import EthJsonRpc
from ethcontract import ETHContract
from tinydb import TinyDB, Query
import codecs
import hashlib
class ContractStorage:
def __init__(self):
self.db = TinyDB('./contracts.json')
def initialize(self, rpchost, rpcport):
eth = EthJsonRpc(rpchost, rpcport)
blockNum = eth.eth_blockNumber()
while(blockNum > 0):
if not blockNum % 1000:
print("Processing block: " + str(blockNum))
block = eth.eth_getBlockByNumber(blockNum)
for tx in block['transactions']:
if not tx['to']:
receipt = eth.eth_getTransactionReceipt(tx['hash'])
contract_address = receipt['contractAddress']
contract_code = eth.eth_getCode(contract_address)
m = hashlib.md5()
m.update(contract_code.encode('UTF-8'))
contract_hash = codecs.encode(m.digest(), 'hex_codec')
contract_id = contract_hash.decode("utf-8")
contract_balance = eth.eth_getBalance(contract_address)
Contract = Query()
new_instance = {'address': contract_address, 'balance': contract_balance}
s = self.db.search(Contract.id == contract_id)
if not len(s):
self.db.insert({'id': contract_id, 'code': contract_code, 'instances': [new_instance]})
else:
instances = s[0]['instances']
instances.append(new_instance)
self.db.update({'instances': instances}, Contract.id == contract_id)
blockNum -= 1
def search(self, expression):
all_contracts = self.db.all()
for c in all_contracts:
for instance in c['instances']:
if ('balance' in instance):
contract = ETHContract(c['code'], instance['balance'])
if (contract.matches_expression(expression)):
print("Found contract:" + instance['address'])