Skip to content

Instantly share code, notes, and snippets.

@jaglinux
Created December 26, 2022 09:27
Show Gist options
  • Save jaglinux/06e636ee40e48bf74bb3c2d1f1f98df0 to your computer and use it in GitHub Desktop.
Save jaglinux/06e636ee40e48bf74bb3c2d1f1f98df0 to your computer and use it in GitHub Desktop.
Inspired from https://gist.github.com/ItsCuzzo/dbce3c4b2f60f8cf9d3d8ac17b248fee , EVM disassembler to decode selector from bytecode
opcodes = {
0x00: 'STOP',
0x01: 'ADD',
0x02: 'MUL',
0x03: 'SUB',
0x04: 'DIV',
0x05: 'SDIV',
0x06: 'MOD',
0x07: 'SMOD',
0x08: 'ADDMOD',
0x09: 'MULMOD',
0x0a: 'EXP',
0x0b: 'SIGNEXTEND',
0x10: 'LT',
0x11: 'GT',
0x12: 'SLT',
0x13: 'SGT',
0x14: 'EQ',
0x15: 'ISZERO',
0x16: 'AND',
0x17: 'OR',
0x18: 'XOR',
0x19: 'NOT',
0x1a: 'BYTE',
0x1b: 'SHL',
0x1c: 'SHR',
0x1d: 'SAR',
0x20: 'SHA3',
0x30: 'ADDRESS',
0x31: 'BALANCE',
0x32: 'ORIGIN',
0x33: 'CALLER',
0x34: 'CALLVALUE',
0x35: 'CALLDATALOAD',
0x36: 'CALLDATASIZE',
0x37: 'CALLDATACOPY',
0x38: 'CODESIZE',
0x39: 'CODECOPY',
0x3a: 'GASPRICE',
0x3b: 'EXTCODESIZE',
0x3c: 'EXTCODECOPY',
0x3d: 'RETURNDATASIZE',
0x3e: 'RETURNDATACOPY',
0x3f: 'EXTCODEHASH',
0x46: 'CHAINID',
0x47: 'SELFBALANCE',
0x48: 'BASEFEE',
0x40: 'BLOCKHASH',
0x41: 'COINBASE',
0x42: 'TIMESTAMP',
0x43: 'NUMBER',
0x44: 'DIFFICULTY',
0x45: 'GASLIMIT',
0x50: 'POP',
0x51: 'MLOAD',
0x52: 'MSTORE',
0x53: 'MSTORE8',
0x54: 'SLOAD',
0x55: 'SSTORE',
0x56: 'JUMP',
0x57: 'JUMPI',
0x58: 'PC',
0x59: 'MSIZE',
0x5a: 'GAS',
0x5b: 'JUMPDEST',
0x60: 'PUSH1',
0x61: 'PUSH2',
0x62: 'PUSH3',
0x63: 'PUSH4',
0x64: 'PUSH5',
0x65: 'PUSH6',
0x66: 'PUSH7',
0x67: 'PUSH8',
0x68: 'PUSH9',
0x69: 'PUSH10',
0x6a: 'PUSH11',
0x6b: 'PUSH12',
0x6c: 'PUSH13',
0x6d: 'PUSH14',
0x6e: 'PUSH15',
0x6f: 'PUSH16',
0x70: 'PUSH17',
0x71: 'PUSH18',
0x72: 'PUSH19',
0x73: 'PUSH20',
0x74: 'PUSH21',
0x75: 'PUSH22',
0x76: 'PUSH23',
0x77: 'PUSH24',
0x78: 'PUSH25',
0x79: 'PUSH26',
0x7a: 'PUSH27',
0x7b: 'PUSH28',
0x7c: 'PUSH29',
0x7d: 'PUSH30',
0x7e: 'PUSH31',
0x7f: 'PUSH32',
0x80: 'DUP1',
0x81: 'DUP2',
0x82: 'DUP3',
0x83: 'DUP4',
0x84: 'DUP5',
0x85: 'DUP6',
0x86: 'DUP7',
0x87: 'DUP8',
0x88: 'DUP9',
0x89: 'DUP10',
0x8a: 'DUP11',
0x8b: 'DUP12',
0x8c: 'DUP13',
0x8d: 'DUP14',
0x8e: 'DUP15',
0x8f: 'DUP16',
0x90: 'SWAP1',
0x91: 'SWAP2',
0x92: 'SWAP3',
0x93: 'SWAP4',
0x94: 'SWAP5',
0x95: 'SWAP6',
0x96: 'SWAP7',
0x97: 'SWAP8',
0x98: 'SWAP9',
0x99: 'SWAP10',
0x9a: 'SWAP11',
0x9b: 'SWAP12',
0x9c: 'SWAP13',
0x9d: 'SWAP14',
0x9e: 'SWAP15',
0x9f: 'SWAP16',
0xa0: 'LOG0',
0xa1: 'LOG1',
0xa2: 'LOG2',
0xa3: 'LOG3',
0xa4: 'LOG4',
0xf0: 'CREATE',
0xf1: 'CALL',
0xf2: 'CALLCODE',
0xf3: 'RETURN',
0xf4: 'DELEGATECALL',
0xf5: 'CREATE2',
0xfa: 'STATICCALL',
0xfd: 'REVERT',
0xff: 'SELFDESTRUCT',
}
class Disassembler:
def selectors(self, code):
selectors = []
idx = 0
while idx < len(code):
instruction = code[idx:idx+2]
instruction = int(instruction, 16)
opcode = opcodes[instruction] if instruction in opcodes else 'INVALID'
print(idx, hex(instruction), opcode)
if 'PUSH' not in opcode:
idx += 2
continue
n = int(opcode[4:])
if n == 4:
s = code[idx+2:idx+10]
selectors.append(s)
idx += (n * 2) + 2
return set(selectors)
a = "608060405234801561001057600080fd5b50600436106100935760003560e01c80639d19c0d1116100665780639d19c0d1146100fe578063a209081e14610115578063c2fe202214610135578063cfcddeb014610148578063d40e8e8d1461016a57600080fd5b80630f6d2115146100985780631b446cb0146100ad5780633c1940d0146100d85780638f20e9f8146100eb575b600080fd5b6100ab6100a6366004610839565b61017d565b005b6100c06100bb36600461097e565b61024c565b6040516100cf93929190610a2e565b60405180910390f35b6100ab6100e636600461097e565b610300565b6100ab6100f93660046108d4565b6103b5565b61010760025481565b6040519081526020016100cf565b61012861012336600461097e565b6103d7565b6040516100cf91906109e1565b610128610143366004610839565b610502565b61015b610156366004610839565b6105e9565b6040516100cf93929190610a5c565b6100ab610178366004610867565b610693565b6001600160a01b038116600090815260208181526040808320338452600481019092529091205460ff16156101ed5760405162461bcd60e51b8152602060048201526011602482015270416c726561647920466f6c6c6f77696e6760781b60448201526064015b60405180910390fd5b3360008181526004830160209081526040808320805460ff1916600190811790915560028601805485526005870190935290832080546001600160a01b0319169094179093558054909190610243908490610a81565b90915550505050565b60016020526000908152604090208054819061026790610a99565b80601f016020809104026020016040519081016040528092919081815260200182805461029390610a99565b80156102e05780601f106102b5576101008083540402835291602001916102e0565b820191906000526020600020905b8154815290600101906020018083116102c357829003601f168201915b50505050600183015460029093015491926001600160a01b031691905083565b6000818152600160209081526040808320338452600381019092529091205460ff161561035f5760405162461bcd60e51b815260206004820152600d60248201526c105b1c9958591e48131a5ad959609a1b60448201526064016101e4565b3360008181526003830160209081526040808320805460ff1916600190811790915560028601805485526004870190935290832080546001600160a01b0319169094179093558054909190610243908490610a81565b3360009081526020818152604090912082516103d39284019061072c565b5050565b6060600254821061041d5760405162461bcd60e51b815260206004820152601060248201526f1a5b9d985b1a5908151dd9595d081a5960821b60448201526064016101e4565b6000828152600160205260408120600281015490918167ffffffffffffffff81111561045957634e487b7160e01b600052604160045260246000fd5b604051908082528060200260200182016040528015610482578160200160208202803683370190505b50905060005b828110156104f957600081815260048501602052604090205482516001600160a01b03909116908390839081106104cf57634e487b7160e01b600052603260045260246000fd5b6001600160a01b0390921660209283029190910190910152806104f181610ad4565b915050610488565b50949350505050565b6001600160a01b038116600090815260208190526040812060028101546060928167ffffffffffffffff81111561054957634e487b7160e01b600052604160045260246000fd5b604051908082528060200260200182016040528015610572578160200160208202803683370190505b50905060005b828110156104f957600081815260058501602052604090205482516001600160a01b03909116908390839081106105bf57634e487b7160e01b600052603260045260246000fd5b6001600160a01b0390921660209283029190910190910152806105e181610ad4565b915050610578565b60006020819052908152604090208054819061060490610a99565b80601f016020809104026020016040519081016040528092919081815260200182805461063090610a99565b801561067d5780601f106106525761010080835404028352916020019161067d565b820191906000526020600020905b81548152906001019060200180831161066057829003601f168201915b5050505050908060010154908060020154905083565b60025460009081526001602052604090206106af8184846107b0565b50600180820180546001600160a01b031916339081179091556000908152602081905260408120808301805491939290916106eb908490610a81565b90915550506002805460038301805460018181018355600092835260208320909101929092558254919291610721908490610a81565b909155505050505050565b82805461073890610a99565b90600052602060002090601f01602090048101928261075a57600085556107a0565b82601f1061077357805160ff19168380011785556107a0565b828001600101855582156107a0579182015b828111156107a0578251825591602001919060010190610785565b506107ac929150610824565b5090565b8280546107bc90610a99565b90600052602060002090601f0160209004810192826107de57600085556107a0565b82601f106107f75782800160ff198235161785556107a0565b828001600101855582156107a0579182015b828111156107a0578235825591602001919060010190610809565b5b808211156107ac5760008155600101610825565b60006020828403121561084a578081fd5b81356001600160a01b0381168114610860578182fd5b9392505050565b60008060208385031215610879578081fd5b823567ffffffffffffffff80821115610890578283fd5b818501915085601f8301126108a3578283fd5b8135818111156108b1578384fd5b8660208285010111156108c2578384fd5b60209290920196919550909350505050565b6000602082840312156108e5578081fd5b813567ffffffffffffffff808211156108fc578283fd5b818401915084601f83011261090f578283fd5b81358181111561092157610921610b05565b604051601f8201601f19908116603f0116810190838211818310171561094957610949610b05565b81604052828152876020848701011115610961578586fd5b826020860160208301379182016020019490945295945050505050565b60006020828403121561098f578081fd5b5035919050565b60008151808452815b818110156109bb5760208185018101518683018201520161099f565b818111156109cc5782602083870101525b50601f01601f19169290920160200192915050565b6020808252825182820181905260009190848201906040850190845b81811015610a225783516001600160a01b0316835292840192918401916001016109fd565b50909695505050505050565b606081526000610a416060830186610996565b6001600160a01b039490941660208301525060400152919050565b606081526000610a6f6060830186610996565b60208301949094525060400152919050565b60008219821115610a9457610a94610aef565b500190565b600181811c90821680610aad57607f821691505b60208210811415610ace57634e487b7160e01b600052602260045260246000fd5b50919050565b6000600019821415610ae857610ae8610aef565b5060010190565b634e487b7160e01b600052601160045260246000fd5b634e487b7160e01b600052604160045260246000fdfea26469706673582212202568cef2d8ff4741d0f20898b0657542113198ba5d00571cbb8e5c76d696fa3a64736f6c63430008040033"
#a="608060405234801561001057600080fd5b5061026c806100206000396000f3fe608060405234801561001057600080fd5b506004361061004c5760003560e01c8063119fbbd4146100515780633c6bb4361461005b5780638abf6077146100795780638da5cb5b14610097575b600080fd5b6100596100b5565b005b6100636100d1565b604051610070919061015a565b60405180910390f35b6100816100d7565b60405161008e919061013f565b60405180910390f35b61009f6100fd565b6040516100ac919061013f565b60405180910390f35b600a600260008282546100c89190610175565b92505081905550565b60025481565b600160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b60008054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b61012a816101cb565b82525050565b610139816101fd565b82525050565b60006020820190506101546000830184610121565b92915050565b600060208201905061016f6000830184610130565b92915050565b6000610180826101fd565b915061018b836101fd565b9250827fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff038211156101c0576101bf610207565b5b828201905092915050565b60006101d6826101dd565b9050919050565b600073ffffffffffffffffffffffffffffffffffffffff82169050919050565b6000819050919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601160045260246000fdfea264697066735822122005c7ec0903632e0b3d2db34e6a9345b89e06f7df6e17f76975d17554d5a9f01c64736f6c63430008070033"
dis = Disassembler()
print(dis.selectors(a))
# set(['4e487b71', '8f20e9f8', 'a209081e', '9d19c0d1', '3c1940d0', 'cfcddeb0', '0f6d2115', 'c2fe2022', 'd40e8e8d', '1b446cb0'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment