Use bytearrays to handle bytecode

pull/2/head
Bernhard Mueller 7 years ago
parent b8002f2c0a
commit 2a6d83b93b
  1. 16
      ether/asm.py
  2. 1
      lol
  3. 5
      mythril.py
  4. 20
      util.py

@ -6,14 +6,6 @@ import re
regex_PUSH = re.compile('^PUSH(\d*)$')
def safe_decode(hex_encoded_string):
if (hex_encoded_string.startswith("0x")):
return codecs.decode(hex_encoded_string[2:], 'hex_codec')
else:
return codecs.decode(hex_encoded_string, 'hex_codec')
def disassembly_to_easm(disassembly):
easm = ""
@ -21,7 +13,7 @@ def disassembly_to_easm(disassembly):
easm += instruction['opcode']
if 'argument' in instruction:
easm += " 0x" + instruction['argument']
easm += " 0x" + codecs.decode(instruction['argument'], 'ascii')
easm += "\n"
@ -117,7 +109,7 @@ def disassemble(bytecode):
instruction = {}
try:
opcode = opcodes.opcodes[ord(bytecode[i])]
opcode = opcodes.opcodes[bytecode[i]]
except KeyError:
# invalid opcode
disassembly.append({'opcode': "INVALID"})
@ -151,10 +143,10 @@ def assemble(disassembly):
except RuntimeError:
opcode = 0xbb
bytecode += chr(opcode)
bytecode += opcode.to_bytes(1, byteorder='big')
if 'argument' in instruction:
bytecode += codecs.decode(instruction['argument'], 'hex_codec')
bytecode += bytes(instruction['argument'], 'ascii')
return bytecode

1
lol

@ -0,0 +1 @@
`60`40PP

@ -39,7 +39,6 @@ if (args.disassemble):
encoded_bytecode = util.bytecode_from_blockchain(args.transaction_hash, args.rpchost, args.rpcport)
print(encoded_bytecode)
except Exception as e:
exitWithError("Exception loading bytecode via RPC" + str(e.message))
@ -64,9 +63,9 @@ elif (args.assemble):
assembly = asm.assemble(disassembly)
if (args.outfile):
util.string_to_file(args.outfile, assembly)
util.raw_bytes_to_file(args.outfile, assembly)
else:
print("0x" + codecs.encode(assembly, "hex_codec"))
print("0x" + str(codecs.encode(assembly, "hex_codec"), 'ascii'))
else:

@ -1,11 +1,12 @@
from ether.jsonrpc import EthJsonRpcWithDebug
import codecs
def safe_decode(hex_encoded_string):
if (hex_encoded_string.startswith("0x")):
return hex_encoded_string[2:].decode("hex")
return codecs.decode(hex_encoded_string[2:], 'hex_codec')
else:
return hex_encoded_string.decode("hex")
return codecs.decode(hex_encoded_string, 'hex_codec')
def bytecode_from_blockchain(creation_tx_hash, rpc_host='127.0.0.1', rpc_port=8545):
@ -24,12 +25,17 @@ def bytecode_from_blockchain(creation_tx_hash, rpc_host='127.0.0.1', rpc_port=85
raise RuntimeError("Transaction trace didn't return any bytecode")
def raw_bytes_to_file(filename, bytestring):
with open(filename, 'wb') as f:
f.write(bytestring)
def string_to_file(filename, string):
outfile = open(filename, "w")
outfile.write(string)
outfile.close()
with open(filename, 'w') as f:
f.write(string)
def file_to_string(filename):
infile = open(filename, "r")
return infile.read()
with open(filename, 'r') as f:
data = f.read()
return data

Loading…
Cancel
Save