-
Notifications
You must be signed in to change notification settings - Fork 4
/
server.py
393 lines (326 loc) · 14 KB
/
server.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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
from flask import (
Flask,
jsonify,
request,
Response
)
import database as db
from flasgger import Swagger
from flasgger.utils import swag_from
import json
import logging as log
import configparser
import datetime
import time
config = configparser.ConfigParser()
config.read('config.ini')
template = {
"swagger": "2.0",
"info": {
"title": "APK scanner API",
"description": "An API that scans APKs looking for security vulnerabilities",
"contact": {
"responsibleOrganization": "ISCTE - Instituto Universitário de Lisboa",
"responsibleDeveloper": "Carlos Serrão",
"email": "carlos.serrao@iscte-iul.pt",
"url": "istar.iscte-iul.pt",
},
"termsOfService": "http://me.com/terms",
"version": "0.0.1"
},
"host": "127.0.0.1:5000", # overrides localhost:500
"basePath": "/", # base bash for blueprint registration
"schemes": [
"http",
"https"
],
"operationId": "getmyData"
}
# Create the application instance
app = Flask(__name__, template_folder="templates")
Swagger(app, template=template)
log.basicConfig(filename=config['GENERAL']['logDir'] + "appsentinel.log", filemode='a', format='%(asctime)s,%(msecs)d | %(name)s | %(levelname)s | %(funcName)s:%(lineno)d | %(message)s', datefmt='%H:%M:%S', level=log.DEBUG)
# Create a URL route in our application for "/"
# This is purely to see if the server is running, there is currently no website planned
@app.route('/')
def home():
return jsonify({'status': True, 'message': 'API is responding... keep trying!'}), 200
@app.route('/apkscan', methods=['POST'])
@swag_from('./docs/apkscan.yml')
def apkscan():
log.debug("APKSCAN REQUEST RECEIVED")
data = request.values
# check if an MD5 has been passed
if "md5" not in data:
return jsonify({'status': False, 'message': 'Hey...! Where is my MD5???'}), 200, {'Access-Control-Allow-Origin':'*'}
else:
md5Apk = data["md5"]
log.debug("APK MD5 = " + md5Apk)
# 1. add the apk to scan to the database
if db.apk_id_exists(md5Apk, 'apkresults'):
return jsonify({'status': False,
'message': 'That APK was previously processed and exists on the results database. Please check the results using the appropriate API call!'}), 200, {'Access-Control-Allow-Origin':'*'}
else:
if db.apk_id_exists(md5Apk, 'apk2scan'):
return jsonify({'status': False, 'message': 'That APK is already in the pipeline to be processed... wait for the results!'}), 200, {'Access-Control-Allow-Origin':'*'}
else:
db.insert_new_apk2scan(md5Apk)
return jsonify({'status': True, 'message': 'APK was passed to the scanning engine... please hold on!'}), 200, {'Access-Control-Allow-Origin':'*'}
@app.route('/apkfeedback/<id>', methods=['GET'])
@swag_from('./docs/apkfeedback.yml')
def apkfeedback(id):
log.debug("APKSCAN REQUEST RECEIVED")
log.debug("APK MD5 = " + id)
if not id:
return jsonify({'status': False, 'message': 'No MD5 APK id was passed'}), 500, {'Access-Control-Allow-Origin':'*'}
else:
results_data = db.get_apk_status(id)
if results_data:
# we need to check the status...
if results_data[0]['status'] != -1:
print(results_data[0]['results_location'])
file = open(results_data[0]['results_location'])
json_data = json.load(file)
data = {'status': 'OK',
'M1': json_data['M1'],
'M2': json_data['M2'],
'M3': json_data['M3'],
'M4': json_data['M4'],
'M5': json_data['M5'],
'M6': json_data['M6'],
'M7': json_data['M7'],
'M8': json_data['M8'],
'M9': json_data['M9'],
'M10': json_data['M10']}
return jsonify(data), 200, {'Access-Control-Allow-Origin':'*'}
else:
return jsonify({'status': False, 'message': results_data[0]['details']}), 500, {'Access-Control-Allow-Origin':'*'}
else:
return jsonify({'status': False, 'message': 'APK work was not finished... please come back l8r!'}), 500, {'Access-Control-Allow-Origin':'*'}
@app.route('/vulnerabilities/<id>', methods=['GET'])
@swag_from('./docs/vulnerabilities.yml')
def apkvullevel(id):
log.debug("REQUEST TO GET VULNERABILITIES LEVEL")
log.debug("APK MD5 = " + id)
if not id:
return jsonify({'status': False, 'message': 'No MD5 APK id was passed'}), 500, {'Access-Control-Allow-Origin':'*'}
else:
results_data = db.get_apk_vuln_level(id)
if results_data:
# we need to check the status...
if results_data[0]['status'] != -1:
print(results_data[0]['results_location'])
file = open(results_data[0]['results_location'])
json_data = json.load(file)
data = {'status': 'OK', 'vulnerabilities': json_data['vulnerabilities']}
return jsonify(data), 200, {'Access-Control-Allow-Origin':'*'}
else:
return jsonify({'status': False, 'message': results_data[0]['details']}), 500, {'Access-Control-Allow-Origin':'*'}
else:
return jsonify({'status': False, 'message': 'APK work was not finished... please come back l8r!'}), 500, {'Access-Control-Allow-Origin':'*'}
@app.route('/vulnerabilities', methods=['GET'])
@swag_from('./docs/apkmonthlevels.yml')
def apkmonthlevels():
log.debug("REQUEST TO GET VULNERABILITIES LEVEL BY MONTH")
info=0
notice=0
warning=0
critical=0
data={}
data_month={}
month_list=[]
id = request.args.get('sort')
if int(id) not in range(3, 13):
return jsonify({'status': False, 'message': 'sort value must be between 3 and 12'}), 500, {'Access-Control-Allow-Origin':'*'}
else:
now = datetime.datetime.now()
for number in range(0,int(id)):
if number >= now.month:
month = datetime.date(now.year,(12-number+now.month),now.day).strftime('%B')
else:
month = datetime.date(now.year,abs(now.month-number),now.day).strftime('%B')
month_list.append(month)
results_data = db.get_apk_month_level(id)
if results_data:
for x in results_data:
month = time.strftime('%B', time.struct_time((0,x['created_at'].month,0,)+(0,)*6))
file = open(x['results_location'])
json_data = json.load(file)
try:
month_list.remove(month)
except:
pass
for p in json_data['vulnerabilities']:
if 'Info' in p['severity']:
info += 1
if 'Notice' in p['severity']:
notice += 1
if 'Warning' in p['severity']:
warning += 1
if 'Critical' in p['severity']:
critical += 1
data_month[month] = []
data_month[month] = ({
'Info': info,
'Notice': notice,
'Warning': warning,
'Critical': critical
})
data_month['info'] = []
data_month['info'].append({
'month': month,
'values': [
{'Info': info},
{'Notice': notice},
{'Warning': warning},
{'Critical': critical}]
})
data = {'status': 'OK', 'info': data_month['info']}
#json_data = json.dumps(data)
for empty_values in month_list:
data_month['info'].append({
'month': empty_values,
'values': [
{'Info': 0},
{'Notice': 0},
{'Warning': 0},
{'Critical': 0}]
})
return jsonify(data), 200, {'Access-Control-Allow-Origin':'*'}
else:
return jsonify({'status': False, 'message': 'Error'}), 500, {'Access-Control-Allow-Origin':'*'}
@app.route('/vulnerabilities/levels/<id>', methods=['GET'])
@swag_from('./docs/apklevels.yml')
def apklevels(id):
log.debug("APK MD5 = " + id)
if not id:
return jsonify({'status': False, 'message': 'No MD5 APK id was passed'}), 500, {'Access-Control-Allow-Origin':'*'}
else:
results_data = db.get_apk_levels(id)
if results_data:
# we need to check the status...
if results_data[0]['status'] != -1:
print(results_data[0]['results_location'])
file = open(results_data[0]['results_location'])
json_data = json.load(file)
return jsonify(json_data), 200, {'Access-Control-Allow-Origin':'*'}
else:
return jsonify({'status': False, 'message': results_data[0]['details']}), 500, {'Access-Control-Allow-Origin':'*'}
else:
return jsonify({'status': False, 'message': 'APK work was not finished... please come back l8r!'}), 500, {'Access-Control-Allow-Origin':'*'}
@app.route('/vulnerabilities/levels', methods=['GET'])
@swag_from('./docs/allapksvulnlevels.yml')
def allapksvulnlevels():
log.debug("REQUEST TO GET APKS LIST INFORMATION")
info=0
notice=0
warning=0
critical=0
data={}
data_list={}
data_list['list'] = []
results_data = db.get_all_apk_levels()
if results_data:
for x in results_data:
file = open(x['results_location'])
json_data = json.load(file)
for p in json_data['vulnerabilities']:
if 'Info' in p['severity']:
info += 1
if 'Notice' in p['severity']:
notice += 1
if 'Warning' in p['severity']:
warning += 1
if 'Critical' in p['severity']:
critical += 1
# to do download and rating
data_list['list'] = [{
'level': 'Info',
'value': info},
{'level': 'Notice',
'value': notice},
{'level': 'Warinig',
'value': warning},
{'level': 'Critical',
'value': critical}]
data = {'status': 'OK', 'list': data_list['list']}
#json_data = json.dumps(data)
return jsonify(data), 200, {'Access-Control-Allow-Origin':'*'}
else:
return jsonify({'status': False, 'message': 'Error'}), 500, {'Access-Control-Allow-Origin':'*'}
@app.route('/vulnerabilities/apks/list/', methods=['GET'])
@swag_from('./docs/apkslist.yml')
def apkslist():
log.debug("REQUEST TO GET APKS LIST INFORMATION")
info = 0
notice = 0
warning = 0
critical = 0
data_list = {}
data_list['apkslistinfo'] = []
data = {}
results_data = db.get_all_apk_levels()
if results_data:
for x in results_data:
file = open(x['results_location'])
print("File to open: " + x['results_location'])
json_data = json.load(file)
print("JSON data: " + str(json_data))
for p in json_data['vulnerabilities']:
if 'Notice' in p['severity']:
notice += 1
if 'Warning' in p['severity']:
warning += 1
if 'Critical' in p['severity']:
critical += 1
data_list['apkslistinfo'].append({
'apk_md5': x['md5'],
'vulnerability_levels': [{'Info': info},
{'Notice': notice},
{'Warning': warning},
{'Critical': critical}],
'download': '23456',
'rating': '5'
})
data = {'status': 'OK', 'list': data_list['apkslistinfo']}
return jsonify(data), 200, {'Access-Control-Allow-Origin':'*'}
else:
return jsonify({'status': False, 'message': 'Error'}), 500, {'Access-Control-Allow-Origin':'*'}
@app.route('/feedback/rules/', methods=['GET'])
@swag_from('./docs/getrules.yml')
def getrules():
log.debug("REQUEST TO GET RULES INFORMATION")
results_data = db.get_rules()
data = {}
data['rules'] = []
if results_data:
for row in results_data:
data['rules'] = ({
'status': 'OK',
'vulnerability_levels':[{
'info': row['info'],
'notice': row['notice'],
'warning': row['warning'],
'critical': row['critical']
}],
'vulnerability_name': row['vulnerability_name'],
'videos': row['videos'],
'links': row['link'],
'severity_levels': row['severity_levels'],
'email_template': row['email_template']
})
return jsonify(data['rules']), 200, {'Access-Control-Allow-Origin':'*'}
else:
return jsonify({'status': False, 'message': 'Error'}), 500, {'Access-Control-Allow-Origin':'*'}
@app.route('/feedback/rules/', methods=['PUT'])
@swag_from('./docs/updaterules.yml')
def updaterules():
log.debug("UPDATE RULES INFORMATION")
data = request.values
if db.insert_rules(data['info'], data['notice'], data['warning'], data['critical'], data['vulnerability_name'], data['videos'], data['link'], data['severity_levels'], data['email_template']):
return jsonify({'status': True, 'message': 'This was called and returns something!'}), 200, {'Access-Control-Allow-Origin': '*'}
else:
return jsonify({'status': True, 'message': 'Some error occured while updating the feedback rules!!!'}), 200, {'Access-Control-Allow-Origin': '*'}
# If we're running in stand alone mode, run the application
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000, debug=True)