forked from sigttou/analyzecrypt.py
-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_functions.py
102 lines (86 loc) · 2.45 KB
/
get_functions.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
#!/usr/bin/env python2
import sys
from pwn import log
from pycparser import parse_file
import pycparser
import json
import os.path
FAKE_LIBC = "c_code/utils/fake_libc_include"
class information:
def __init__(self):
self.isptr = False
self.name = ""
self.type = ""
self.isfunct = False
self.params = []
def get_type(x):
f = information()
while(not isinstance(x, pycparser.c_ast.IdentifierType)):
if(isinstance(x, pycparser.c_ast.PtrDecl)):
f.isptr = True
x = x.type
f.type = " ".join(x.names)
return f
def print_function(f):
tolog = []
for p in f.params:
con = " "
if(p.isptr):
con = "* "
if(p.name):
tolog.append(p.type + con + p.name)
params = ", ".join(tolog)
con = " "
if(f.isptr):
con = "* "
log.info(f.type + con + f.name + "(" + params + ")")
def export_function(f):
fdesc = {}
fdesc["name"] = f.name
fdesc["parameters"] = []
fdesc["monitor"] = True
for p in f.params:
entry = {}
entry["name"] = p.name
if(p.isptr):
entry["type"] = "addr"
if("char" in p.type):
entry["type"] = "string"
else:
entry["type"] = "num"
entry["monitor"] = False
if(p.name):
fdesc["parameters"].append(entry)
filename = "functions/" + f.name + ".json"
if(os.path.isfile(filename)):
log.warn("File " + filename + " already exists!")
else:
with open(filename, 'w') as outfile:
json.dump(fdesc, outfile,
sort_keys=True, indent=4, separators=(',', ': '))
def get_functions(tree):
functions = []
for e in tree:
if isinstance(e.type, pycparser.c_ast.FuncDecl):
f = get_type(e)
f.isfunct = True
f.name = e.name
for ee in e.type.args.params:
p = get_type(ee)
p.name = ee.name
f.params.append(p)
functions.append(f)
return functions
def main(filename):
ast = parse_file(filename, use_cpp=True, cpp_args="-I" + FAKE_LIBC)
functions = get_functions(ast.ext)
for f in functions:
# print_function(f)
export_function(f)
if __name__ == '__main__':
if len(sys.argv) != 2:
try:
log.error("Usage: %s <path_to_file>" % __file__)
except:
sys.exit(-1)
main(sys.argv[1])