-
Notifications
You must be signed in to change notification settings - Fork 1
/
extractor.py
106 lines (94 loc) · 2.97 KB
/
extractor.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
import os
import re
# obtain the specific line of the block
def firstPhase(n):
inputFileDir = "./binary_cfg_code/delegatecall/"
dirs = os.listdir(inputFileDir)
# dirs.sort(key=lambda x: int(x[:-4]))
print(dirs)
for file in dirs:
inputFilePath = inputFileDir + file
f = open(inputFilePath, "r")
lines = f.readlines()
nodes = "./binary_graph_data/node/" + str(file)[0:-4] + ".txt"
edges = "./binary_graph_data/edge/" + str(file)[0:-4] + ".txt"
f_node = open(nodes, 'a')
f_edge = open(edges, "a")
flag = 0
for line in lines:
if "block" in line and flag == 0:
f_node.write(line.strip() + "\n")
if "block" in line and flag != 0:
f_edge.write(line.strip() + "\n")
if "}" in line:
flag += 1
continue
# remove the symbols of '\l'
def secondPhase(n):
inputFileDir = "./binary_graph_data/node/"
dirs = os.listdir(inputFileDir)
dirs.sort(key=lambda x: int(x[:-4]))
for file in dirs:
inputFilePath = inputFileDir + file
f = open(inputFilePath, "r+")
n = n + 1
nodes = "./binary_graph_data/new_node/" + str(file)[0:-4] + ".txt"
f_node = open(nodes, "w")
lines = f.readlines()
for line in lines:
tt = re.sub(r'\\l', r' ', line)
f_node.write(tt)
# reserve '0x' and remove 'block'
def thirdPhase(n):
inputFileDir = "./binary_graph_data/new_node/"
dirs = os.listdir(inputFileDir)
dirs.sort(key=lambda x: int(x[:-4]))
for file in dirs:
inputFilePath = inputFileDir + file
f = open(inputFilePath, "r+")
n = n + 1
nodes = "./binary_graph_data/new_node1/" + str(file)[0:-4] + ".txt"
f_node = open(nodes, "w")
s = ''
lines = f.readlines()
i = 0
for line in lines:
list = line.split(" ")
for i in list:
if i[0:2] == "0x":
s += i
if i[0:1].isupper() != 0:
s += i
s += ' '
s += '\n'
s = s.replace(" ", " ")
print(s)
f_node.write(s)
# extract the pure bytecode
def extractPureBytecode(n):
inputFileDir = "./bytecode/delegatecall/"
dirs = os.listdir(inputFileDir)
print(dirs)
for file in dirs:
inputFilePath = inputFileDir + file
f = open(inputFilePath, "r+")
n = n + 1
nodes = "./binary_cfg_code/delegatecall/" + str(file)[0:-4] + ".txt"
f_node = open(nodes, "w")
s = ''
lines = f.readlines()
i = 0
for line in lines:
list = line.split(" ")
for i in list:
if i[0:4] == "6080":
s += i
s = s.replace(" ", " ")
print(s)
f_node.write(s)
if __name__ == "__main__":
n = 0
firstPhase(n)
# secondPhase(n)
# thirdPhase(n)
# extractPureBytecode(n)