From d6bc09e2e9bb8cf4c6801e07d6a9bf561476eb2e Mon Sep 17 00:00:00 2001 From: Nikhil Parasaram Date: Tue, 19 Jun 2018 20:46:09 +0530 Subject: [PATCH] add typecheck for the contracts --- mythril/laser/ethereum/natives.py | 18 ++++++++++++++---- mythril/laser/ethereum/svm.py | 16 +++++++++++----- 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/mythril/laser/ethereum/natives.py b/mythril/laser/ethereum/natives.py index 98607165..e32ab81a 100644 --- a/mythril/laser/ethereum/natives.py +++ b/mythril/laser/ethereum/natives.py @@ -1,10 +1,11 @@ # -*- coding: utf8 -*- + import copy import hashlib + import coincurve from py_ecc.secp256k1 import N as secp256k1n - from mythril.laser.ethereum.helper import ALL_BYTES, bytearray_to_int, concrete_int_to_bytes, sha3, zpad @@ -40,7 +41,10 @@ def extract32(data, i): def ecrecover(data): - data = bytearray(data) + try: + data = bytearray(data) + except TypeError: + return "ecrecover_"+str(data) message = b''.join(map(lambda x: ALL_BYTES[x], data[0:32])) v = extract32(data, 32) r = extract32(data, 64) @@ -56,12 +60,18 @@ def ecrecover(data): def sha256(data): - data = bytes(data) + try: + data = bytes(data) + except TypeError: + return "sha256_"+str(data) return hashlib.sha256(data).digest() def ripemd160(data): - data = bytes(data) + try: + data = bytes(data) + except TypeError: + return "ripemd160_"+str(data) return 12*[0]+[i for i in hashlib.new('ripemd160', data).digest()] diff --git a/mythril/laser/ethereum/svm.py b/mythril/laser/ethereum/svm.py index d2f003ee..1c24eeaf 100644 --- a/mythril/laser/ethereum/svm.py +++ b/mythril/laser/ethereum/svm.py @@ -994,12 +994,14 @@ class LaserEVM: state.stack.append(ret) continue - if int(callee_address, 16) < 5 and int(callee_address, 16) > 0: + if 0 < int(callee_address, 16) < 5: logging.info("Native contract called: " + callee_address) calldata, calldata_type = self._get_calldata(meminstart, meminsz, state, pad=False) if calldata == [] and calldata_type == CalldataType.SYMBOLIC: logging.debug("CALL with symbolic data not supported") + ret = BitVec("retval_" + str(instr['address']), 256) + state.stack.append(ret) continue data = natives.native_contracts(int(callee_address, 16 ), calldata) @@ -1008,14 +1010,18 @@ class LaserEVM: mem_out_sz = memoutsz.as_long() except AttributeError: logging.debug("CALL with symbolic start or offset not supported") + ret = BitVec("retval_" + str(instr['address']), 256) + state.stack.append(ret) continue state.mem_extend(mem_out_start, mem_out_sz) - for i in range(min(len(data), mem_out_sz)): # If more data is used then it's chopped off - state.memory[mem_out_start+i] = data[i] + try: + for i in range(min(len(data), mem_out_sz)): # If more data is used then it's chopped off + state.memory[mem_out_start+i] = data[i] + except: + state.memory[mem_out_start] = BitVec(data, 256) + state.stack.append(1) - ret = BitVec("retval_" + str(instr['address']), 256) - state.stack.append(ret) continue try: