forked from adelapie/ghidra-evm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
load_external_contract.py
79 lines (59 loc) · 2.72 KB
/
load_external_contract.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/env python
"""
Ghidra-evm
Copyright (C) 2020 - Antonio de la Piedra
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
"""
load_external_contract.py downloads a contract byte code from
the blockchain and write it into a .evm_h file.
"""
import urllib.request
import urllib.parse
import argparse
import json
def main():
parser = argparse.ArgumentParser()
parser.add_argument("api_key", help="etherscan API key")
parser.add_argument("contract_address", help="contract address")
parser.add_argument("-v", "--verbosity", action="count", default=0)
parser.add_argument("-m", "--main", action="count", default=0, help="Use main net instead of Ropsten (default)")
args = parser.parse_args()
API_KEY = args.api_key
CONTRACT_ADDR = args.contract_address
URL_ROPSTEN= 'https://api-ropsten.etherscan.io/api?module=proxy&action=eth_getCode&address='.strip() + CONTRACT_ADDR + "&tag=latest&apikey=".strip() + API_KEY.strip()
URL_MAIN= 'https://api.etherscan.io/api?module=proxy&action=eth_getCode&address='.strip() + CONTRACT_ADDR + "&tag=latest&apikey=".strip() + API_KEY.strip()
print("[*] Downloading contract byte code...")
try:
if (args.main):
request = urllib.request.urlopen(URL_MAIN)
else:
request = urllib.request.urlopen(URL_ROPSTEN)
data = json.load(request)
if 'result' in data:
if (args.verbosity):
print(data['result'])
print("[*] Writing contract byte code into {}".format(args.contract_address.strip() + ".evm_h"))
contract_file = open(args.contract_address.strip() + ".evm_h", "w")
if (data['result'].startswith("0x")):
len = contract_file.write(data['result'][2:])
if (len == 0):
print("[!] 0 bytes were written")
else:
len = contract_file.write(data['result'])
if (len == 0):
print("[!] 0 bytes were written")
contract_file.close()
except urllib.error.URLError as e:
print(e.reason)
if __name__ == "__main__":
main()