-
Notifications
You must be signed in to change notification settings - Fork 11
/
api.py
357 lines (274 loc) · 10.8 KB
/
api.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
from django.shortcuts import render
from django.http import HttpResponse
import xmltodict, json, html, os, hashlib, re, requests, base64, urllib.parse
from collections import OrderedDict
from nmapreport.functions import *
def rmNotes(request, hashstr):
if 'auth' not in request.session:
return False
scanfilemd5 = hashlib.md5(str(request.session['scanfile']).encode('utf-8')).hexdigest()
if re.match('^[a-f0-9]{32,32}$', hashstr) is not None:
os.remove('/opt/notes/'+scanfilemd5+'_'+hashstr+'.notes')
res = {'ok':'notes removed'}
else:
res = {'error':'invalid format'}
return HttpResponse(json.dumps(res), content_type="application/json")
def saveNotes(request):
if 'auth' not in request.session:
return False
if request.method == "POST":
scanfilemd5 = hashlib.md5(str(request.session['scanfile']).encode('utf-8')).hexdigest()
if re.match('^[a-f0-9]{32,32}$', request.POST['hashstr']) is not None:
f = open('/opt/notes/'+scanfilemd5+'_'+request.POST['hashstr']+'.notes', 'w')
f.write(request.POST['notes'])
f.close()
res = {'ok':'notes saved'}
else:
res = {'error': request.method }
return HttpResponse(json.dumps(res), content_type="application/json")
def rmlabel(request, objtype, hashstr):
if 'auth' not in request.session:
return False
types = {
'host':True,
'port':True
}
scanfilemd5 = hashlib.md5(str(request.session['scanfile']).encode('utf-8')).hexdigest()
if re.match('^[a-f0-9]{32,32}$', hashstr) is not None:
os.remove('/opt/notes/'+scanfilemd5+'_'+hashstr+'.'+objtype+'.label')
res = {'ok':'label removed'}
return HttpResponse(json.dumps(res), content_type="application/json")
def label(request, objtype, label, hashstr):
labels = {
'Vulnerable':True,
'Critical':True,
'Warning':True,
'Checked':True
}
types = {
'host':True,
'port':True
}
scanfilemd5 = hashlib.md5(str(request.session['scanfile']).encode('utf-8')).hexdigest()
if label in labels and objtype in types:
if re.match('^[a-f0-9]{32,32}$', hashstr) is not None:
f = open('/opt/notes/'+scanfilemd5+'_'+hashstr+'.'+objtype+'.label', 'w')
f.write(label)
f.close()
res = {'ok':'label set', 'label':str(label)}
return HttpResponse(json.dumps(res), content_type="application/json")
def port_details(request, address, portid):
r = {}
if 'auth' not in request.session:
return False
oo = xmltodict.parse(open('/opt/xml/'+request.session['scanfile'], 'r').read())
r['out'] = json.dumps(oo['nmaprun'], indent=4)
o = json.loads(r['out'])
for ik in o['host']:
# this fix single host report
if type(ik) is dict:
i = ik
else:
i = o['host']
if '@addr' in i['address']:
saddress = i['address']['@addr']
elif type(i['address']) is list:
for ai in i['address']:
if ai['@addrtype'] == 'ipv4':
saddress = ai['@addr']
if str(saddress) == address:
for pobj in i['ports']['port']:
if type(pobj) is dict:
p = pobj
else:
p = i['ports']['port']
if p['@portid'] == portid:
return HttpResponse(json.dumps(p, indent=4), content_type="application/json")
def genPDF(request):
if 'auth' not in request.session:
return False
if 'scanfile' in request.session:
pdffile = hashlib.md5(str(request.session['scanfile']).encode('utf-8')).hexdigest()
if os.path.exists('/opt/nmapdashboard/nmapreport/static/'+pdffile+'.pdf'):
os.remove('/opt/nmapdashboard/nmapreport/static/'+pdffile+'.pdf')
os.popen('/opt/wkhtmltox/bin/wkhtmltopdf --cookie sessionid '+request.session._session_key+' --enable-javascript --javascript-delay 6000 http://127.0.0.1:8000/view/pdf/ /opt/nmapdashboard/nmapreport/static/'+pdffile+'.pdf')
res = {'ok':'PDF created', 'file':'/static/'+pdffile+'.pdf'}
return HttpResponse(json.dumps(res), content_type="application/json")
def getCVE(request):
res = {}
if 'auth' not in request.session:
return False
if request.method == "POST":
scanfilemd5 = hashlib.md5(str(request.session['scanfile']).encode('utf-8')).hexdigest()
cveproc = os.popen('python3 /opt/nmapdashboard/nmapreport/nmap/cve.py '+request.session['scanfile'])
res['cveout'] = cveproc.read()
cveproc.close()
return HttpResponse(json.dumps(res), content_type="application/json")
#hostmd5 = hashlib.md5(str(request.POST['host']).encode('utf-8')).hexdigest()
#portmd5 = hashlib.md5(str(request.POST['port']).encode('utf-8')).hexdigest()
# request.POST['host']
cpe = json.loads(base64.b64decode(urllib.parse.unquote(request.POST['cpe'])).decode('ascii'))
for cpestr in cpe:
r = requests.get('http://cve.circl.lu/api/cvefor/'+cpestr)
cvejson = r.json()
for host in cpe[cpestr]:
hostmd5 = hashlib.md5(str(host).encode('utf-8')).hexdigest()
if type(cvejson) is list and len(cvejson) > 0:
res[host] = cvejson[0]
f = open('/opt/notes/'+scanfilemd5+'_'+hostmd5+'.cve', 'w')
f.write(json.dumps(cvejson))
f.close()
return HttpResponse(json.dumps(res), content_type="application/json")
r = requests.get('http://cve.circl.lu/api/cvefor/'+request.POST['cpe'])
if request.POST['host'] not in res:
res[request.POST['host']] = {}
cvejson = r.json()
if type(cvejson) is list and len(cvejson) > 0:
res[request.POST['host']][request.POST['port']] = cvejson[0]
f = open('/opt/notes/'+scanfilemd5+'_'+hostmd5+'.cve', 'w')
f.write(json.dumps(cvejson))
f.close()
return HttpResponse(json.dumps(res), content_type="application/json")
def apiv1_hostdetails(request, scanfile, faddress=""):
if token_check(request.GET['token']) is not True:
return HttpResponse(json.dumps({'error':'invalid token'}, indent=4), content_type="application/json")
oo = xmltodict.parse(open('/opt/xml/'+scanfile, 'r').read())
out2 = json.dumps(oo['nmaprun'], indent=4)
o = json.loads(out2)
r = {'file':scanfile, 'hosts': {}}
scanmd5 = hashlib.md5(str(scanfile).encode('utf-8')).hexdigest()
# collect all labels in labelhost dict
labelhost = {}
labelfiles = os.listdir('/opt/notes')
for lf in labelfiles:
m = re.match('^('+scanmd5+')_([a-z0-9]{32,32})\.host\.label$', lf)
if m is not None:
if m.group(1) not in labelhost:
labelhost[m.group(1)] = {}
labelhost[m.group(1)][m.group(2)] = open('/opt/notes/'+lf, 'r').read()
# collect all notes in noteshost dict
noteshost = {}
notesfiles = os.listdir('/opt/notes')
for nf in notesfiles:
m = re.match('^('+scanmd5+')_([a-z0-9]{32,32})\.notes$', nf)
if m is not None:
if m.group(1) not in noteshost:
noteshost[m.group(1)] = {}
noteshost[m.group(1)][m.group(2)] = open('/opt/notes/'+nf, 'r').read()
# collect all cve in cvehost dict
cvehost = get_cve(scanmd5)
for ik in o['host']:
# this fix single host report
if type(ik) is dict:
i = ik
else:
i = o['host']
hostname = {}
if 'hostnames' in i and type(i['hostnames']) is dict:
# hostname = json.dumps(i['hostnames'])
if 'hostname' in i['hostnames']:
# hostname += '<br>'
if type(i['hostnames']['hostname']) is list:
for hi in i['hostnames']['hostname']:
hostname[hi['@type']] = hi['@name']
else:
hostname[i['hostnames']['hostname']['@type']] = i['hostnames']['hostname']['@name'];
if i['status']['@state'] == 'up':
po,pc,pf = 0,0,0
ss,pp,ost = {},{},{}
lastportid = 0
if '@addr' in i['address']:
address = i['address']['@addr']
elif type(i['address']) is list:
for ai in i['address']:
if ai['@addrtype'] == 'ipv4':
address = ai['@addr']
if faddress != "" and faddress != address:
continue
addressmd5 = hashlib.md5(str(address).encode('utf-8')).hexdigest()
#cpe[address] = {}
labelout = ''
if scanmd5 in labelhost:
if addressmd5 in labelhost[scanmd5]:
labelout = labelhost[scanmd5][addressmd5]
notesout,notesb64,removenotes = '','',''
if scanmd5 in noteshost:
if addressmd5 in noteshost[scanmd5]:
notesb64 = noteshost[scanmd5][addressmd5]
# notesout = '<br><a id="noteshost'+str(hostindex)+'" href="#!" onclick="javascript:openNotes(\''+hashlib.md5(str(address).encode('utf-8')).hexdigest()+'\', \''+notesb64+'\');" class="small"><i class="fas fa-comment"></i> contains notes</a>'
# removenotes = '<li><a href="#!" onclick="javascript:removeNotes(\''+addressmd5+'\', \''+str(hostindex)+'\');">Remove notes</a></li>'
cveout = ''
#cvecount = 0
if scanmd5 in cvehost:
if addressmd5 in cvehost[scanmd5]:
cveout = json.loads(cvehost[scanmd5][addressmd5])
# for cveobj in cvejson:
# cvecount = (cvecount + 1)
if faddress == "":
r['hosts'][address] = {'hostname':hostname, 'label':labelout, 'notes':notesb64}
else:
r['hosts'][address] = {'ports':[], 'hostname':hostname, 'label':labelout, 'notes':notesb64, 'CVE':cveout}
if 'ports' in i and 'port' in i['ports']:
for pobj in i['ports']['port']:
if type(pobj) is dict:
p = pobj
else:
p = i['ports']['port']
if lastportid == p['@portid']:
continue
else:
lastportid = p['@portid']
v,z,e='','',''
pp[p['@portid']] = p['@portid']
servicename = ''
if 'service' in p:
ss[p['service']['@name']] = p['service']['@name']
if '@version' in p['service']:
v = p['service']['@version']
if '@product' in p['service']:
z = p['service']['@product']
if '@extrainfo' in p['service']:
e = p['service']['@extrainfo']
servicename = p['service']['@name']
if faddress != "":
r['hosts'][address]['ports'].append({
'port': p['@portid'],
'name': servicename,
'state': p['state']['@state'],
'protocol': p['@protocol'],
'reason': p['state']['@reason'],
'product': z,
'version': v,
'extrainfo': e
})
return HttpResponse(json.dumps(r, indent=4), content_type="application/json")
def apiv1_scan(request):
r = {}
if token_check(request.GET['token']) is not True:
return HttpResponse(json.dumps({'error':'invalid token'}, indent=4), content_type="application/json")
gitcmd = os.popen('cd /opt/nmapdashboard/nmapreport && git rev-parse --abbrev-ref HEAD')
r['webmap_version'] = gitcmd.read().strip()
xmlfiles = os.listdir('/opt/xml')
r['scans'] = {}
xmlfilescount = 0
for i in xmlfiles:
if re.search('\.xml$', i) is None:
continue
xmlfilescount = (xmlfilescount + 1)
try:
oo = xmltodict.parse(open('/opt/xml/'+i, 'r').read())
except:
r['scans'][i] = {'filename':html.escape(i), 'startstr': '', 'nhost':0, 'port_stats':{'open':0,'closed':0,'filtered':0}}
continue
rout = json.dumps(oo['nmaprun'], indent=4)
o = json.loads(rout)
if 'host' in o:
if type(o['host']) is not dict:
hostnum = str(len(o['host']))
else:
hostnum = '1'
else:
hostnum = '0'
portstats = nmap_ports_stats(i)
r['scans'][i] = {'filename':html.escape(i), 'startstr': html.escape(o['@startstr']), 'nhost':hostnum, 'port_stats':{'open':portstats['po'],'closed':portstats['pc'],'filtered':portstats['pf']}}
return HttpResponse(json.dumps(r, indent=4), content_type="application/json")