-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
288 lines (228 loc) · 10.5 KB
/
main.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
import re
import json
import os
from bs4 import BeautifulSoup
from urllib.parse import urljoin, urlparse
import requests
# Function to create the directory that will contain the exported files
def create_export_directories(subdomain):
"""
Create directories for exporting files.
Parameters:
- subdomain (str): The subdomain for the dApp.
Returns:
- str: Path to the created subdomain directory.
"""
exports_directory = "exports"
subdomain_directory = os.path.join(exports_directory, subdomain)
os.makedirs(subdomain_directory, exist_ok=True)
return subdomain_directory
# Function to export the ABI JSON to a file
def export_abi_json(abi_json, name_field, subdomain_directory):
"""
Export the ABI JSON to a file.
Parameters:
- abi_json (dict): The ABI JSON data.
- name_field (str): The name for the ABI.
- subdomain_directory (str): Directory to save the ABI file.
Returns:
- str: Path to the saved ABI JSON file.
"""
# Creating a filename based on the "name" field, ensuring uniqueness
filename_base = name_field.replace(" ", "_")
filename = filename_base + ".json"
file_path = os.path.join(subdomain_directory, filename)
unique_path = file_path
os.path.join(subdomain_directory, f"{filename_base}.json")
# Writing the ABI JSON to the file
with open(unique_path, 'w') as file:
json.dump(abi_json, file, indent=4)
return unique_path
def repair_json(s):
"""
Repairs a JSON-like string by adding necessary formatting.
Parameters:
- s (str): The JSON-like string to be repaired.
Returns:
- str: Repaired JSON-like string.
"""
# Adding quotes around keys
s = re.sub(r'(\b[a-zA-Z_]\w*\b)(?=\s*[:])', r'"\1"', s)
# Replacing single quotes with double quotes
s = s.replace("'", '"')
# Escaping any unescaped backslashes
s = s.replace("\\", "\\\\")
# Replacing !0 with true and !1 with false
s = s.replace('!0', 'true')
s = s.replace('!1', 'false')
return json.loads(s)
def find_literal_jsons(js_code):
# Pattern to find JSON.parse calls with a basic JSON structure
# This pattern assumes the JSON string is well-formed and doesn't contain nested objects
# Adjust the pattern if your JSON strings can contain nested quotes or other complexities
pattern = r'JSON.parse\(\'({.*?})\'\)'
# Find all JSON strings within JSON.parse calls
json_strings = re.findall(pattern, js_code)
# Filter and extract JSONs that have the specified fields in the required order
matching_jsons = []
for json_str in json_strings:
try:
# Convert JSON string into a Python dictionary
parsed_json = json.loads(json_str)
# Check for the presence and order of 'version', 'name', 'bytecode'
keys = list(parsed_json.keys())
if ('version' in keys and 'name' in keys and 'bytecode' in keys and
keys.index('version') < keys.index('name') < keys.index('bytecode')):
matching_jsons.append(parsed_json)
except json.JSONDecodeError:
# Handle cases where the JSON string is not well-formed
print("Found a malformed JSON string.")
return matching_jsons
def find_literal_json_deployments(js_code):
# Pattern to find JSON.parse calls with a basic JSON structure
# This pattern assumes the JSON string is well-formed and doesn't contain nested objects
# Adjust the pattern if your JSON strings can contain nested quotes or other complexities
pattern = r'JSON.parse\(\'({.*?})\'\)'
# Find all JSON strings within JSON.parse calls
json_strings = re.findall(pattern, js_code)
# Filter and extract JSONs that have the specified fields in the required order
matching_jsons = []
for json_str in json_strings:
try:
# Convert JSON string into a Python dictionary
parsed_json = json.loads(json_str)
# Check for the presence and order of 'version', 'name', 'bytecode'
keys = list(parsed_json.keys())
if ('deployerAddress' in keys and 'contracts' in keys and 'scripts' in keys and 'migrations' in keys):
matching_jsons.append(parsed_json)
except json.JSONDecodeError:
# Handle cases where the JSON string is not well-formed
print("Found a malformed JSON string.")
return matching_jsons
def find_if_abi(js_code):
main_abi_regex = re.compile(r'\{version:\s*(\w+),\s*name:\s*(\w+),\s*bytecode:\s*(\w+),\s*codeHash:\s*(\w+),\s*fieldsSig:\s*(\w+),\s*eventsSig:\s*(\w+),\s*functions:\s*(\w+),\s*constants:\s*(\w+),\s*enums:\s*(\w+)\s*')
match = main_abi_regex.search(js_code)
if not match:
return None
version_var, name_var, bytecode_var, codeHash_var, fieldsSig_var, eventsSig_var, functions_var, constants_var, enums_var = match.groups()
return version_var, name_var, bytecode_var, codeHash_var, fieldsSig_var, eventsSig_var, functions_var, constants_var, enums_var
def find_if_deployments(js_code):
main_abi_regex = re.compile(r'\{deployerAddress:\s*(\w+),\s*contracts:\s*(\w+),\s*scripts:\s*(\w+),\s*migrations:\s*(\w+)\s*')
match = main_abi_regex.search(js_code)
if not match:
return None
version_var, name_var, bytecode_var, codeHash_var, fieldsSig_var, eventsSig_var, functions_var, constants_var, enums_var = match.groups()
return version_var, name_var, bytecode_var, codeHash_var, fieldsSig_var, eventsSig_var, functions_var, constants_var, enums_var
def extract_var_value(var_name, js_code):
regex = re.compile(var_name + r'\s*=\s*([^;]+),', re.DOTALL)
var_match = regex.search(js_code)
output = None
if var_match:
output = var_match.group(1).strip().split('=')[0]
output = output[0:output.rfind(',')]
return output
def break_js_code_to_variables(js_code, groups):
pieces = js_code.replace(' = ', '=').split('=')
output = {}
index = 0
while index < len(pieces):
s = pieces[index + 1]
if ',' in s:
if s.split(',')[1] in groups:
s = s.split(',')[0]
output[pieces[index]] = s
index += 2
return output
# Function to get JS URLs from the given URL
def get_js_urls(url):
"""
Retrieves JavaScript URLs from the given web page URL.
Parameters:
- url (str): URL of the web page to retrieve JS URLs from.
Returns:
- List[str]: List of JavaScript URLs found in the web page.
"""
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
js_urls = [urljoin(url, script['src']) for script in soup.find_all('script') if
'src' in script.attrs and script['src'].endswith('.js')]
return js_urls
def find_abis(js_code, url):
parsed_url = urlparse(url)
domain = parsed_url.netloc
subdomain_directory = create_export_directories(domain)
consts = js_code.split("const ")
files = find_literal_jsons(js_code)
deploys = find_literal_json_deployments(js_code)
deployCount = 0
for file in files:
name_field = file["name"]
abi_json_path = export_abi_json(file, name_field, subdomain_directory)
print(f"Saved file to: {abi_json_path}")
for file in deploys:
abi_json_path = export_abi_json(file, f"deployment.{deployCount}", subdomain_directory)
deployCount += 1
print(f"Saved file to: {abi_json_path}")
for const in consts:
groups = find_if_abi(const)
if groups is not None:
version_var, name_var, bytecode_var, codeHash_var, fieldsSig_var, eventsSig_var, functions_var, constants_var, enums_var = groups
abi_components = {
"version": json.loads(extract_var_value(version_var, const)),
"name": json.loads(extract_var_value(name_var, const)),
"bytecode": json.loads(extract_var_value(bytecode_var, const)),
"codeHash": json.loads(extract_var_value(codeHash_var, const)),
"fieldsSig": repair_json(extract_var_value(fieldsSig_var, const)),
"eventsSig": repair_json(extract_var_value(eventsSig_var, const)),
"functions": repair_json(extract_var_value(functions_var, const)),
"constants": repair_json(extract_var_value(constants_var, const)),
"enums": repair_json(extract_var_value(enums_var, const))
}
name_field = abi_components["name"]
abi_json_path = export_abi_json(abi_components, name_field, subdomain_directory)
print(f"Saved file to: {abi_json_path}")
files.append(abi_components)
return files
# Function to process JS URL and extract ABI JSONs
def process_js_url(js_url):
"""
Processes a JavaScript URL to extract ABI JSONs.
Parameters:
- js_url (str): JavaScript URL to process.
Returns:
- List[str]: List of extracted ABI JSONs from the JavaScript.
"""
# Send a request to get JS code
response = requests.get(js_url)
js_code = response.text
find_abis(js_code, js_url)
# Main function to accept URL and process it
def main():
"""
Main function to accept a URL from the user, process it, and extract ABI JSONs.
The function prompts the user for a dApp URL, extracts JavaScript URLs, processes each JS URL,
and extracts ABI JSONs from them.
Returns:
- None
"""
url = input("Please enter the URL of the dApp to process: ")
js_urls = get_js_urls(url)
for js_url in js_urls:
print(f"Processing JavaScript URL: {js_url}")
process_js_url(js_url)
if __name__ == '__main__':
asciiart = """
____ _____ ______ _ _
/\ | _ \_ _| | ____| | | | |
/ \ | |_) || | | |__ __ _| |_ _ __ __ _ ___| |_ ___ _ __
/ /\ \ | _ < | | | __| \ \/ / __| '__/ _` |/ __| __/ _ \| '__|
/ ____ \| |_) || |_ | |____ > <| |_| | | (_| | (__| || (_) | |
/_/ \_\____/_____| |______/_/\_\\__|_| \__,_|\___|\__\___/|_|
___ ___ _ _ _ ___ _ __
| _ )_ _ / __| |___ _| | | __| |/ _|
| _ \ || | \__ \ / / || | | | _|| | _|
|___/\_, | |___/_\_\\_,_|_|_|___|_|_|
|__/
"""
print(asciiart)
main()