-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlooper.py
169 lines (132 loc) · 4.25 KB
/
looper.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
#! /usr/bin/env python
from flask import Flask, render_template, request
import detector
import time
import os
import subprocess
import pickle
import json
app = Flask(__name__)
app.config['MAX_CONTENT_LENGTH'] = 50 * 8 * 1024 * 1024 # 50 MB Limit
@app.route('/')
def cfgexplorer1():
return render_template('traceCFG.html')
@app.route('/cfgexplorer/')
def cfgexplorer2():
return render_template('traceCFG.html')
@app.route('/tracevis/')
def cfgexplorer3():
return render_template('traceCFG.html')
@app.route('/findLoops/', methods=['GET', 'POST'])
def findLoops():
if request.method=='POST':
data = request.data
else:
#Only for testing purpose
# with open('opt.43.dot', 'r') as myfile:
# data = myfile.read()
return
# Write the data to a file
fileName = str(time.time())
with open(fileName, 'wb' ) as tempfile:
tempfile.write(data)
outStr = detector.main([fileName])
#Delete the file
os.remove(fileName)
return outStr
@app.route('/getBackTaint/', methods=['GET', 'POST'])
def getBackTaint():
########### These parameters are now specified by the request object itself
# Read the json file
# with open('analysis.json', 'r') as json_file:
# analysisFile = json.load(json_file)
# bt_analysis = analysisFile["backtaint"]
# script_path = bt_analysis["scriptpath"]
# language = bt_analysis["language"]
# outfilename = bt_analysis["outfilename"]
###########
if request.method=='POST':
received = request.json
# get the taint address
address = received["address"]
data = received["trace"]
script_path = received["scriptpath"]
language = received["language"]
outfilename = received["outfilename"]
else:
#Only for testing purpose
address = "40063f"
with open('listCalc.jascii', 'r') as myfile:
data = myfile.read()
script_path = "backTaint.rb"
language = "ruby"
outfilename = ""
#Write the data to a temp file
fileName = str(time.time())
with open(fileName, 'w') as tempfile:
tempfile.write(data)
# # p = subprocess.Popen(["./backTaint.rb", fileName, address], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
# p = subprocess.Popen(["ruby", "backTaint.rb", fileName, address], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
# output, error = p.communicate()
# #Delete the temp file
# os.remove(fileName)
# if error:
# raise Exception("Error " + str(error))
########### Use the script specified by the request
p = subprocess.Popen([language, script_path, fileName, address], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
output, error = p.communicate()
#Delete the temp file
os.remove(fileName)
if error:
raise Exception("Error " + str(error))
###########
return output
@app.route('/getUERs/', methods=['GET', 'POST'])
def getUERs():
if request.method=='POST':
received = request.json
# get the dotfile
data = received["dotfile"]
analysisType = received["UERtype"]
else:
#Only for testing purpose
analysisType = "allUERs"
with open('uerDetector/listCalc.dot', 'r') as myfile:
data = myfile.read()
#Write the data to a temp file
baseName = str(time.time())
fileName = baseName + ".dot"
with open(fileName, 'w') as tempfile:
tempfile.write(data)
p = subprocess.Popen(["uerDetector/analyze-loops.sh", fileName], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
output, error = p.communicate()
#Delete the temp file
os.remove(fileName)
if error:
raise Exception("Error " + str(error))
outputFile = baseName + ".loopData"
f = open(outputFile, "rb")
loopDataDict = pickle.load(f)
f.close()
os.remove(outputFile)
finalData = {}
# For each new key encountered in the loopDataDict, create a new set
# For existing keys, update the set with the contents of the new list
# After all the keys are iterated over, convert the sets back to lists
for key in loopDataDict:
UERs = loopDataDict[key][analysisType]
for innerkey in UERs:
if (innerkey in finalData):
finalData[innerkey].update(UERs[innerkey])
else:
finalData[innerkey] = set(UERs[innerkey])
for key in finalData:
finalData[key] = list(finalData[key])
# for key in loopDataDict:
# UERs = loopDataDict[key][analysisType]
# for innerkey in UERs:
# if (analysisType == 'allUERs'):
# finalData[innerkey] = list(UERs[innerkey])
# else:
# finalData[innerkey] = UERs[innerkey]
return json.dumps(finalData)