-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathasthook-manager
More file actions
executable file
·250 lines (196 loc) · 7.35 KB
/
asthook-manager
File metadata and controls
executable file
·250 lines (196 loc) · 7.35 KB
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
#!/usr/bin/env python3
import argparse
import sys
import os
import shutil
import psutil
from asthook.conf import DIR, PACKAGE_PATH, VERSION
import subprocess
import json
from tabulate import tabulate
from asthook.log import error
from difflib import context_diff, unified_diff
from pathlib import Path
import re
DEV=False
pid_file="/tmp/server_asthook.pid"
def list_project(args):
projects = sorted(os.listdir(DIR))
table = []
for project in projects:
if args.search and not re.search(args.search, project):
continue
infos = {'decompiler' : "", "prev_decompiler": []}
info_path = f"{DIR}/{project}/info.json"
if os.path.exists(info_path):
with open(info_path, "r") as info_file:
infos.update(json.loads(info_file.read()))
table.append([project, infos['decompiler'],
','.join(infos['prev_decompiler'])])
print(tabulate(table, headers=["project", "decompiler", "prev decompiler"],
tablefmt="fancy_grid"))
def diff_project(project1, project2 = None, decompiler = None,
decompiler2 = None, globing = "**/*"):
infos1 = {'decompiler' : "", "prev_decompiler": []}
infos2 = infos1.copy()
info_path = f"{DIR}/{project1}/info.json"
if os.path.exists(info_path):
with open(info_path, "r") as info_file:
infos1.update(json.loads(info_file.read()))
if not project2:
project2 = project1
infos2 = infos1.copy()
else:
info_path = f"{DIR}/{project2}/info.json"
if os.path.exists(info_path):
with open(info_path, "r") as info_file:
infos2.update(json.loads(info_file.read()))
if not decompiler or infos1['decompiler'] == decompiler:
project1_path = Path(f"{DIR}/{project1}/decompiled_app/")
decompiler = infos1['decompiler']
else:
if not decompiler in infos1['prev_decompiler']:
error(f"{project1} have not been decompile with {decompiler}")
sys.exit(1)
project1_path = Path(f"{DIR}/{project1}/prev_decompilation/{decompiler}")
if not decompiler2:
decompiler2 = decompiler
if infos2['decompiler'] == decompiler2:
project2_path = Path(f"{DIR}/{project2}/decompiled_app/")
decompiler2 = infos2['decompiler']
else:
if not decompiler2 in infos2['prev_decompiler']:
error(f"{project2} have not been decompile with {decompiler2}")
sys.exit(1)
project2_path = Path(f"{DIR}/{project2}/prev_decompilation/{decompiler2}")
if project1_path == project2_path:
error("the project and the decompiler you ask is the same")
sys.exit(5)
whitelist = ['.txt', '.xml', '.java']
result_project1 = []
for i in Path(project1_path).rglob(globing):
if i.suffixes and i.suffixes[0] in whitelist:
result_project1.append(i)
result_project2 = []
for i in Path(project2_path).rglob(globing):
if i.suffixes and i.suffixes[0] in whitelist:
result_project2.append(i)
r_paths2 = []
for path2 in result_project2:
r_paths2.append(str(path2)[len(str(Path(project2_path))):])
for path1 in result_project1:
r_path1 = str(path1)[len(str(Path(project1_path))):]
if not r_path1 in r_paths2:
print(r_path1)
pass
else:
try:
text1 = open(f"{project1_path}/{r_path1}").readlines()
text2 = open(f"{project2_path}/{r_path1}").readlines()
for line in unified_diff(text1, text2,
fromfile=f"{project1} - {decompiler} : {r_path1}",
tofile=f"{project2} - {decompiler2} : {r_path1}"):
print(line, end='')
except UnicodeDecodeError as e:
error(e)
r_paths2.remove(r_path1)
print('\n'.join(r_paths2))
def remove_project(project):
if not os.path.exists(f"{DIR}/{project}"):
print(f"project {project} doesn't exist")
sys.exit(1)
shutil.rmtree(f"{DIR}/{project}")
def stop_server():
if not os.path.exists(pid_file):
sys.exit(1)
with open(pid_file, "r") as f:
try:
p = psutil.Process(int(f.read()))
p.terminate()
except:
pass
os.remove(pid_file)
def start_server(hostname="localhost", port=6000):
if os.path.exists(pid_file):
stop_server()
if DEV:
pid = subprocess.Popen(["nohup", "python3", f"{PACKAGE_PATH}/../asthook_server_ast.py",
f"{hostname}", f"{port}"],
preexec_fn=os.setpgrp).pid
else:
pid = subprocess.Popen(["nohup", "python3", f"{PACKAGE_PATH}/asthook_server_ast.py",
f"{hostname}", f"{port}"],
preexec_fn=os.setpgrp).pid
with open(pid_file, "w") as f:
f.write(str(pid))
def main():
main_parser = argparse.ArgumentParser(description="Asthook manager")
main_parser.add_argument(
'-v', '--version',
action="store_true",
help="show version")
service_parsers = main_parser.add_subparsers(title="service",
dest="service_command")
list_parser = service_parsers.add_parser("list", help="list project",)
list_parser.add_argument("--search",
type=str,
help="project to search with a regexp")
remove_parser = service_parsers.add_parser("remove", help="remove project",)
remove_parser.add_argument(
'project',
help='project name to remove')
start_server_parser = service_parsers.add_parser("start_server",
help="start server")
start_server_parser.add_argument("--hostname",
type=str,
help="hostname",
default="localhost")
start_server_parser.add_argument("--port",
type=str,
help="port",
default=6000)
stop_server_parser = service_parsers.add_parser("stop_server",
help="stop server")
diff_parser = service_parsers.add_parser("diff",
help="difference between 2 compilers or/and 2 projects")
diff_parser.add_argument("--project1",
type=str,
help="project1")
diff_parser.add_argument("--project2",
type=str,
help="project2")
diff_parser.add_argument("--decompiler1",
type=str,
help="decompiler1")
diff_parser.add_argument("--decompiler2",
type=str,
help="decompiler2")
diff_parser.add_argument("--globing",
type=str,
help="globing to include only certains files",
default="**/*")
args = main_parser.parse_args()
if args.version:
print(f"Version : {VERSION}")
sys.exit(0)
arg = args.service_command
if arg == "list":
list_project(args)
elif arg == "remove":
remove_project(args.project)
elif arg == "start_server":
start_server(args.hostname, args.port)
elif arg == "stop_server":
stop_server()
elif arg == "diff":
if not args.project1:
error("project1 argument is mandatory")
sys.exit(3)
diff_project(args.project1, args.project2, args.decompiler1,
args.decompiler2, args.globing)
else:
sys.exit(2)
if __name__ == '__main__':
main()
#parser_ = parsing()
#args = parser_.parse_args()