forked from PunitTailor55/WebMap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
views.py
718 lines (604 loc) · 33.8 KB
/
views.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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
from django.shortcuts import render
from django.http import HttpResponse
import xmltodict, json, html, os, hashlib, re, urllib.parse, base64
from collections import OrderedDict
from nmapreport.functions import *
def setscanfile(request, scanfile):
xmlfiles = os.listdir('/opt/xml')
for i in xmlfiles:
if i == scanfile:
request.session['scanfile'] = i
break
if scanfile == 'unset':
if 'scanfile' in request.session:
del(request.session['scanfile'])
return render(request, 'nmapreport/index.html', { 'out': '', 'table': '', 'scaninfo': '<script> location.href="/"; </script>', 'scandetails': '', 'trhost': '' })
def port(request, port):
return render(request, 'nmapreport/index.html', { 'out': '', 'table': '', 'scaninfo': '', 'scandetails': '', 'trhost': '' })
def details(request, address):
r = {}
oo = xmltodict.parse(open('/opt/xml/'+request.session['scanfile'], 'r').read())
r['out2'] = json.dumps(oo['nmaprun'], indent=4)
o = json.loads(r['out2'])
r['trhost'] = ''
v,e,z,h = '','','',''
pc,po,pf=0,0,0
scanmd5 = hashlib.md5(str(request.session['scanfile']).encode('utf-8')).hexdigest()
addressmd5 = hashlib.md5(str(address).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 = {}
cvefiles = os.listdir('/opt/notes')
for cf in cvefiles:
m = re.match('^('+scanmd5+')_([a-z0-9]{32,32})\.([0-9]+)\.cve$', cf)
if m is not None:
if m.group(1) not in cvehost:
cvehost[m.group(1)] = {}
if m.group(2) not in cvehost[m.group(1)]:
cvehost[m.group(1)][m.group(2)] = {}
cvehost[m.group(1)][m.group(2)][m.group(3)] = open('/opt/notes/'+cf, 'r').read()
r['trhead'] = '<tr><th>Port</th><th style="width:300px;">Product / Version</th><th>Extra Info</th><th> </th></tr>'
pel=0
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:
#r['out'] = json.dumps(i, indent=4)
h = '<span style="color:#999;font-size:12px;"><i>No Hostname</i></span>'
if 'hostnames' in i:
if type(i['hostnames']) is dict and 'hostname' in i['hostnames']:
if '@name' in i['hostnames']['hostname']:
h = '<span style="color:#999;font-size:12px;">'+i['hostnames']['hostname']['@name']+'</span>'
labelout = '<span id="hostlabel"></span>'
if scanmd5 in labelhost:
if addressmd5 in labelhost[scanmd5]:
labelcolor = labelToColor(labelhost[scanmd5][addressmd5])
labelmargin = labelToMargin(labelhost[scanmd5][addressmd5])
labelout = '<span id="hostlabel" style="margin-left:60px;margin-top:-24px;" class="rightlabel '+labelcolor+'">'+html.escape(labelhost[scanmd5][addressmd5])+'</span>'
r['scaninfo'] = '<div class="row">'+\
' <div class="col s3"><span class="card-title">Host Details:</span><h6>'+html.escape(address)+'</h6>'+h+labelout+'</div>'+\
' <div class="col s3" id="detailspo"></div>'+\
' <div class="col s3" id="detailspc"></div>'+\
' <div class="col s3" id="detailspf"></div>'+\
'</div>'
rmdupl = {}
for pobj in i['ports']['port']:
if type(pobj) is dict:
p = pobj
else:
p = i['ports']['port']
if p['@portid'] in rmdupl:
continue
rmdupl[p['@portid']] = 1
if p['state']['@state'] == 'closed':
pc = (pc + 1)
elif p['state']['@state'] == 'open':
po = (po + 1)
elif p['state']['@state'] == 'filtered':
pf = (pf + 1)
pel = (pel + 1)
oshtml = ''
if '@ostype' in p['service']:
oshtml = '<div style="font-family:monospace;padding:6px;margin:6px;border-left:solid #666 1px;"><sup style="border-bottom:solid #ccc 1px;">Operating System</sup><br>'+html.escape(p['service']['@ostype'])+'</div>'
so = ''
if 'script' in p:
if '@id' in p['script']:
if p['script']['@id'] != 'fingerprint-strings':
so += '<div style="word-wrap: break-word;word-break: break-all;padding:6px;margin-left:6px;border-left:solid #666 1px;max-width:300px;font-size:12px;color:#ccc;font-family:monospace;"><sup style="color:#999;border-bottom:solid #999 1px;">script output</sup><br><b>'+html.escape(p['script']['@id'])+'</b> '+html.escape(p['script']['@output'])+'</div>'
else:
for sosc in p['script']:
if '@id' in sosc:
if sosc['@id'] != 'fingerprint-strings':
so += '<div style="word-wrap: break-word;word-break: break-all;padding:6px;margin:6px;border-left:solid #666 1px;max-width:300px;font-size:12px;color:#ccc;font-family:monospace;"><sup style="color:#999;border-bottom:solid #999 1px;">script output</sup><br><b>'+html.escape(sosc['@id'])+'</b> '+html.escape(sosc['@output'])+'</div>'
v,z,e = '','','<i class="grey-text">N/A</i>'
if p['state']['@state'] == 'open':
if '@version' in p['service']:
v = p['service']['@version']
else:
v = '<i class="grey-text">No Version</i>'
if '@product' in p['service']:
z = p['service']['@product']
else:
z = '<i class="grey-text">No Product</i>'
if '@extrainfo' in p['service']:
e = p['service']['@extrainfo']
cpe = ''
if 'cpe' in p['service']:
if type(p['service']['cpe']) is list:
for cpei in p['service']['cpe']:
cpe += '<span class="grey-text" style="font-family:monospace;font-size:12px;">'+html.escape(cpei)+'</span><br>'
else:
cpe = '<span class="grey-text" style="font-family:monospace;font-size:12px;">'+html.escape(p['service']['cpe'])+'</span><br>'
r['trhost'] += '<tr><td style="vertical-align:top;">'+\
'<span style="color:#999;font-size:12px;">'+p['service']['@name']+'</span><br>'+\
'<span class="new badge blue" data-badge-caption="">'+p['@protocol']+' / '+p['@portid']+'</span>'+\
'</td>'+\
'<td>'+z+' / '+v+'<br><span style="font-size:12px;color:#999;">State: '+p['state']['@state']+'<br>Reason: '+p['state']['@reason']+'</span></td>'+\
'<td style="vertical-align:top">'+e+'<br>'+cpe+'</td>'+\
'<td><ul id="dropdown'+str(pel)+'" class="dropdown-content" style="min-width:300px;">'+\
' <li><a href="#!" class="btncpy" data-clipboard-text="curl -v -A \'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.1\' -k \'http://'+html.escape(address)+':'+html.escape(p['@portid'])+'\'">Copy as curl command</a></li>'+\
' <li><a href="#!" class="btncpy" data-clipboard-text="nikto -host \'http://'+html.escape(address)+':'+html.escape(p['@portid'])+'\'">Copy as nikto command</a></li>'+\
' <li><a href="#!" class="btncpy" data-clipboard-text="telnet '+html.escape(address)+' '+html.escape(p['@portid'])+'">Copy as telnet command</a></li>'+\
'</ul><a class="dropdown-trigger btn blue right" href="#!" data-target="dropdown'+str(pel)+'"><i class="material-icons">arrow_drop_down</i></a> '+\
'<button onclick="javascript:apiPortDetails(\''+html.escape(address)+'\',\''+html.escape(p['@portid'])+'\');" class="btn blue right"><i class="material-icons">receipt</i></button></td>'+\
'</tr>'
elif p['state']['@state'] == 'filtered':
r['trhost'] += '<tr><td><span class="new badge grey" data-badge-caption="">'+p['@protocol']+' / '+p['@portid']+'</span><br>'+\
'<span style="color:#999;font-size:12px;">'+p['service']['@name']+'</span></td>'+\
'<td colspan="2" style="color:#999;font-size:12px;">State: filtered<br>Reason: '+p['state']['@reason']+'</td>'+\
'<td><button onclick="javascript:apiPortDetails(\''+html.escape(address)+'\',\''+html.escape(p['@portid'])+'\');" class="btn blue right"><i class="material-icons">receipt</i></button></td></tr>'
else:
r['trhost'] += '<tr><td><span class="new badge grey" data-badge-caption="">'+p['@protocol']+' / '+p['@portid']+'</span><br>'+\
'<span style="color:#999;font-size:12px;">'+p['service']['@name']+'</span></td>'+\
'<td colspan="2" style="color:#999;font-size:12px;">State: '+p['state']['@state']+'<br>Reason: '+p['state']['@reason']+'</td>'+\
'<td><button onclick="javascript:apiPortDetails(\''+html.escape(address)+'\',\''+html.escape(p['@portid'])+'\');" class="btn blue right"><i class="material-icons">receipt</i></button></td></tr>'
# this fix single host report
if type(ik) is not dict:
break;
r['table'] = ''
notesout,notesb64,removenotes = '','',''
if scanmd5 in noteshost:
if addressmd5 in noteshost[scanmd5]:
notesb64 = noteshost[scanmd5][addressmd5]
r['table'] += '<div class="card" style="background-color:#3e3e3e;">'+\
' <div class="card-content"><h5>Notes</h5>'+\
' '+base64.b64decode(urllib.parse.unquote(notesb64)).decode('ascii')+\
' </div>'+\
'</div>'
#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 = ''
if scanmd5 in cvehost:
if addressmd5 in cvehost[scanmd5]:
for cveport in cvehost[scanmd5][addressmd5]:
cvejson = json.loads(cvehost[scanmd5][addressmd5][cveport])
# r['out'] = json.dumps(cvejson, indent=4)
for cveobj in cvejson:
cverefout = ''
for cveref in cveobj['references']:
cverefout += '<a href="'+cveref+'">'+cveref+'</a><br>'
cveexdbout = ''
if 'exploit-db' in cveobj:
cveexdbout = '<br><div class="small" style="line-height:20px;"><b>Exploit DB:</b><br>'
for cveexdb in cveobj['exploit-db']:
if 'title' in cveexdb:
cveexdbout += '<a href="'+cveexdb['source']+'">'+html.escape(cveexdb['title'])+'</a><br>'
cveexdbout += '</div>'
cveout += '<div style="line-height:28px;padding:10px;border:solid #333 1px;border-radius:4px;margin-top:10px;">'+\
' <span class="label red">'+html.escape(cveobj['id'])+'</span> '+html.escape(cveobj['summary'])+'<br><br>'+\
' <div class="small" style="line-height:20px;"><b>References:</b><br>'+cverefout+'</div>'+\
cveexdbout+\
'</div>'
r['table'] += '<div class="card" style="background-color:#3e3e3e;">'+\
' <div class="card-content"><span class="card-title">CVE LIST:</span>'+\
cveout+\
' </div>'+\
'</div>'
r['pretable'] = '<script> '+\
'$(document).ready(function() { '+\
' $("#scantitle").html("'+html.escape(request.session['scanfile'])+'");'+\
' var clipboard = new ClipboardJS(".btncpy"); '+\
' clipboard.on("success", function(e) { '+\
' M.toast({html: "Copied to clipboard"}); '+\
' }); '+\
' $(".dropdown-trigger").dropdown(); '+\
' $("#detailspo").html(\'<center><h4><i class="fas fa-door-open green-text"></i> '+str(po)+'</h4><span class="small grey-text">OPEN PORTS</span></center>\');'+\
' $("#detailspc").html(\'<center><h4><i class="fas fa-door-closed red-text"></i> '+str(pc)+'</h4><span class="small grey-text">CLOSED PORTS</span></center>\');'+\
' $("#detailspf").html(\'<center><h4><i class="fas fa-filter grey-text"></i> '+str(pf)+'</h4><span class="small grey-text">FILTERED PORTS</span></center>\');'+\
'}); '+\
'</script>'
return render(request, 'nmapreport/index.html', r)
def index(request, filterservice="", filterportid=""):
r = {}
if 'scanfile' in request.session:
oo = xmltodict.parse(open('/opt/xml/'+request.session['scanfile'], 'r').read())
r['out2'] = json.dumps(oo['nmaprun'], indent=4)
o = json.loads(r['out2'])
else:
# no file selected
xmlfiles = os.listdir('/opt/xml')
r['table'] = '<div class="card" style="background-color:#3e3e3e;"><div class="card-content">'+\
' Put your Nmap XML files in <span class="tmlabel grey-text" style="background-color:transparent;">/opt/xml/</span> directory, example:<br><br>'+\
' <div class="tmlabel black grey-text" style="padding:10px;font-size:14px;">nmap -A -T4 -oX myscan.xml 192.168.1.0/24<br>'+\
' mv myscan.xml <docker webmap xml dir><br><br>'+\
' # or you can copy myscan.xml to the webmap container:<br>'+\
' docker cp myscan.xml webmap:/opt/xml/</div>'+\
'</div></div>'
r['table'] += '<div class="row hide-on-med-and-down" style="margin-top:60px;padding:10px;border-top:solid #444 1px;">'+\
' <div class="col s4" style="text-align:center;">'+\
' <img src="/static/logo.png" style="width:320px;" /><br>'+\
' <span style="color:#999;">Made with <i class="fas fa-heart red-text"></i> by Andrea <b><a href="https://twitter.com/Menin_TheMiddle">theMiddle</a></b> Menin</span><br><br>'+\
' </div>'+\
' <div class="col s4" style="color:#999;text-align:center;">'+\
' <img src="/static/rev3rse_logo.png" style="width:180px;" /><br>'+\
' <span style="color:#999;">A Rev3rse Security Project</span>'+\
' <!-- <a class="github-button" href="https://github.com/theMiddleBlue" data-size="large" data-show-count="true" aria-label="Follow theMiddle on GitHub">Follow theMiddle</a><br>'+\
' <a class="github-button" href="https://github.com/Rev3rseSecurity/WebMap/subscription" data-icon="octicon-eye" data-size="large" data-show-count="true" aria-label="Watch Rev3rseSecurity/WebMap on GitHub">Watch</a><br>'+\
' <a class="github-button" href="https://github.com/Rev3rseSecurity/WebMap/" data-icon="octicon-star" data-size="large" data-show-count="true" aria-label="Star Rev3rseSecurity/WebMap on GitHub">Star</a> -->'+\
' </div>'+\
' <div class="col s4" style="color:#999;text-align:left;">'+\
' <div style="color:#999;margin-bottom:10px;">Support us & Follow us</div>'+\
' <a href="https://github.com/Rev3rseSecurity/WebMap/" class="white-text"><i class="fab fa-github fa-1x white-text"></i> WebMap on GitHub</a><br>'+\
' <a href="https://twitter.com/rev3rsesecurity" class="white-text"><i class="fab fa-twitter fa-1x blue-text" style=""></i> <b>Rev3rse Security</b> on Twitter</a><br>'+\
' <a href="https://www.youtube.com/rev3rsesecurity" class="white-text"><i class="fab fa-youtube fa-1x red-text" style=""></i> <b>Rev3rse Security</b> on YouTube</a><br>'+\
' <!-- <a href="https://twitter.com/Menin_TheMiddle?ref_src=twsrc%5Etfw" class="twitter-follow-button" data-size="large" data-show-count="true">Follow @Menin_TheMiddle</a><script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script><br>'+\
' <script src="https://apis.google.com/js/platform.js"></script><div class="g-ytsubscribe" data-channelid="UCzvJStjySZVvOBsPl-Vgj0g" data-layout="default" data-theme="dark" data-count="default"></div> -->'+\
' </div>'+\
'</div>'
r['trhost'] = ''
r['trhead'] = '<tr><th>Filename</th><th>Scan Start Time</th><th>Hosts</th><th> </th></tr>'
xmlfilescount = 0
for i in xmlfiles:
if re.search('\.xml$', i) is None:
continue
xmlfilescount = (xmlfilescount + 1)
oo = xmltodict.parse(open('/opt/xml/'+i, 'r').read())
r['out2'] = json.dumps(oo['nmaprun'], indent=4)
o = json.loads(r['out2'])
if 'host' in o:
if type(o['host']) is not dict:
hostnum = str(len(o['host']))
else:
hostnum = '1'
else:
hostnum = '0'
if hostnum != '0':
viewhref = '/setscanfile/'+html.escape(i)
else:
viewhref = '/" disabled="disabled'
r['trhost'] += '<tr>'+\
' <td style="font-family:monospace">'+html.escape(i)+'</td>'+\
' <td>'+html.escape(o['@startstr'])+'</td>'+\
' <td>'+hostnum+'</td>'+\
' <td><a href="'+viewhref+'" class="btn blue right">view</a></td>'+\
'</tr>'
r['scaninfo'] = '<span class="card-title">Select a Nmap XML file</span><p>Nmap XML files: '+ str(xmlfilescount) +'</p>'
return render(request, 'nmapreport/index.html', r)
scanmd5 = hashlib.md5(str(request.session['scanfile']).encode('utf-8')).hexdigest()
r['topcontainer'] = '<div class="fixed-action-btn">'+\
' <a class="btn-floating btn-large red">'+\
' <i class="large material-icons">mode_edit</i>'+\
' </a>'+\
' <ul>'+\
' <li><a class="btn-floating red tooltipped" data-position="left" data-tooltip="PDF Report" onclick="javascript:genPDF(\''+scanmd5+'\');"><i class="material-icons">insert_chart</i></a></li>'+\
' <li><a class="btn-floating blue darken-1 tooltipped" data-position="left" data-tooltip="Hide/Show hosts with no open ports" onclick="javascript:$(\'.zeroportopen\').fadeToggle();"><i class="material-icons">view_day</i></a></li>'+\
' <li><a class="btn-floating orange tooltipped" data-position="left" data-tooltip="Check for CVE and Exploits on all hosts" onclick="javascript:checkCVE();"><i class="fas fa-bolt"></i></a></li>'+\
' </ul>'+\
'</div>'
# 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 = {}
cvefiles = os.listdir('/opt/notes')
for cf in cvefiles:
m = re.match('^('+scanmd5+')_([a-z0-9]{32,32})\.([0-9]+)\.cve$', cf)
if m is not None:
if m.group(1) not in cvehost:
cvehost[m.group(1)] = {}
if m.group(2) not in cvehost[m.group(1)]:
cvehost[m.group(1)][m.group(2)] = {}
cvehost[m.group(1)][m.group(2)][m.group(3)] = open('/opt/notes/'+cf, 'r').read()
tableout = ''
hostsup = 0
hostindex = 1
ports = { 'open': 0, 'closed': 0, 'filtered': 0 }
allostypelist, sscount, picount, cpe = {}, {}, {}, {}
r['trhost'] = ''
r['trhead'] = '<tr><th width="260">Host</th><th>Port State</th><th width="160" style="text-align:center;">Tot Ports</th><th width="200">Services</th><th width="200">Ports</th><th> </th></tr>'
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 += '<br><span style="color:#999;font-size:10px;">'+str(i['hostnames']['hostname']['@name'])+'</span>'
if i['status']['@state'] == 'up':
hostsup = (hostsup + 1)
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']
addressmd5 = hashlib.md5(str(address).encode('utf-8')).hexdigest()
striggered = False
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']
if filterservice != "" and p['service']['@name'] == filterservice:
striggered = True
if filterportid != "" and p['@portid'] == filterportid:
striggered = True
ss[p['service']['@name']] = p['service']['@name']
pp[p['@portid']] = p['@portid']
# cpehtml = ''
cpe[address] = {}
if 'cpe' in p['service']:
if type(p['service']['cpe']) is list:
for cpei in p['service']['cpe']:
if p['@portid'] not in cpe[address]:
cpe[address][p['@portid']] = {}
cpe[address][p['@portid']][cpei] = cpei
else:
if p['@portid'] not in cpe[address]:
cpe[address][p['@portid']] = {}
cpe[address][p['@portid']][p['service']['cpe']] = p['service']['cpe']
if '@ostype' in p['service']:
if p['service']['@ostype'] in allostypelist:
allostypelist[p['service']['@ostype']] = (allostypelist[p['service']['@ostype']] +1)
else:
allostypelist[p['service']['@ostype']] = 1;
ost[p['service']['@ostype']] = p['service']['@ostype']
if p['service']['@name'] in sscount:
sscount[p['service']['@name']] = (sscount[p['service']['@name']] + 1)
else:
sscount[p['service']['@name']] = 1
if p['@portid'] in picount:
picount[p['@portid']] = (picount[p['@portid']] + 1)
else:
picount[p['@portid']] = 1
if p['state']['@state'] == 'closed':
ports['closed'] = (ports['closed'] + 1)
pc = (pc + 1)
elif p['state']['@state'] == 'open':
ports['open'] = (ports['open'] + 1)
po = (po + 1)
elif p['state']['@state'] == 'filtered':
ports['filtered'] = (ports['filtered'] + 1)
pf = (pf + 1)
services = ''
for s in ss:
if filterservice != ss[s]:
services += '<a href="/report/service/'+ss[s]+'/">'+ss[s]+'</a>, '
else:
services += '<span class="tmlabel" style="background-color:#ffcc00;color:#333;">'+ss[s]+'</span>, '
ostype = ''
for oty in ost:
ostype += '<i class="'+fromOSTypeToFontAwesome(html.escape(ost[oty]))+'"></i> <span class="grey-text small">'+ost[oty].lower()+'</span> '
tdports = ''
for kp in pp:
if filterportid != pp[kp]:
tdports += '<a href="/report/portid/'+pp[kp]+'/">'+pp[kp]+'</a>, '
else:
tdports += '<span class="tmlabel" style="background-color:#ffcc00;color:#333;">'+pp[kp]+'</span>, '
poclass = ''
if po == 0:
poclass = 'zeroportopen'
labelout = '<span id="hostlabel'+str(hostindex)+'"></span>'
if scanmd5 in labelhost:
if addressmd5 in labelhost[scanmd5]:
labelcolor = labelToColor(labelhost[scanmd5][addressmd5])
labelmargin = labelToMargin(labelhost[scanmd5][addressmd5])
labelout = '<span id="hostlabel'+str(hostindex)+'" style="margin-left:'+labelmargin+'" class="rightlabel '+labelcolor+'">'+html.escape(labelhost[scanmd5][addressmd5])+'</span>'
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]:
for cveport in cvehost[scanmd5][addressmd5]:
cvejson = json.loads(cvehost[scanmd5][addressmd5][cveport])
for cveobj in cvejson:
cvecount = (cvecount + 1)
cveout = '<br><span class="small grey-text"><i class="fas fa-exclamation-triangle orange-text"></i> '+str(cvecount)+' CVE found</span>'
# removenotes = '<li><a href="#!" onclick="javascript:removeNotes(\''+addressmd5+'\', \''+str(hostindex)+'\');">Remove notes</a></li>'
if (filterservice != "" and striggered is True) or (filterportid != "" and striggered is True) or (filterservice == "" and filterportid == ""):
portstateout = '<td style="font-size:10px;color:#999;width:300px;"><div style="overflow:none;background-color:#666;" class="tooltipped" data-position="top" data-tooltip="'+str(po)+' open, '+str(pc)+' closed, '+str(pf)+' filtered">'+\
' <div class="perco" data-po="'+str(po)+'" style="padding-left:16px;padding-right:20px;">'+str(po)+'</div>'+\
' </div></td>'
if (filterservice != "" and striggered is True):
portstateout = '<td style="font-size:10px;color:#999;width:300px;"><div style="overflow:none;background-color:#666;" class="tooltipped" data-position="top" data-tooltip="'+str(po)+' open, '+str(pc)+' closed, '+str(pf)+' filtered">'+\
' <div class="perco" data-po="'+str(po)+'" data-pt="'+str((po + pf + pc))+'" style="padding-left:16px;padding-right:20px;">'+str(po)+'</div>'+\
' </div></td>'
r['trhost'] += '<tr class="'+poclass+'">'+\
' <td><span class="leftlabel" style="background-color:#4a4a4a;color:#999;">'+str(hostindex)+'</span>'+\
' '+ostype+'<br>'+\
' <b><a href="/report/'+str(address)+'">'+str(address)+'</a></b>'+hostname+''+\
notesout+\
cveout+\
' </td>'+\
portstateout+\
' <td style="font-family:monospace;text-align:center;">'+str((po + pf + pc))+'</td>'+\
' <td style="font-size:12px;">'+str(services[0:-2])+'</td>'+\
' <td style="font-size:12px;">'+str(tdports[0:-2])+'</td>'+\
' <td>'+\
' <ul id="hostdd'+str(hostindex)+'" class="dropdown-content" style="min-width:200px;">'+\
' <li><a href="#!" onclick="javascript:setLabel(\'host\', \'Vulnerable\', \''+hashlib.md5(str(address).encode('utf-8')).hexdigest()+'\', '+str(hostindex)+');"><span class="tmlabel red">Vulnerable</span></a></li>'+\
' <li><a href="#!" onclick="javascript:setLabel(\'host\', \'Critical\', \''+hashlib.md5(str(address).encode('utf-8')).hexdigest()+'\', '+str(hostindex)+');"><span class="tmlabel black">Critical</span></a></li>'+\
' <li><a href="#!" onclick="javascript:setLabel(\'host\', \'Warning\', \''+hashlib.md5(str(address).encode('utf-8')).hexdigest()+'\', '+str(hostindex)+');"><span class="tmlabel orange"><span class="tmlabel orange">Warning</span></a></li>'+\
' <li><a href="#!" onclick="javascript:setLabel(\'host\', \'Checked\', \''+hashlib.md5(str(address).encode('utf-8')).hexdigest()+'\', '+str(hostindex)+');"><span class="tmlabel blue"><span class="tmlabel blue">Checked</span></a></li>'+\
' <li><a href="#!" onclick="javascript:removeLabel(\'host\', \''+addressmd5+'\', '+str(hostindex)+');">Remove label</a></li>'+\
' <li class="divider"></li>'+\
' <li><a href="#!" onclick="javascript:openNotes(\''+hashlib.md5(str(address).encode('utf-8')).hexdigest()+'\', \''+notesb64+'\');">Insert notes</a></li>'+\
' '+removenotes+\
' </ul>'+\
labelout+\
' <button class="btn darken-2 grey right dropdown-trigger" data-target="hostdd'+str(hostindex)+'"><i class="material-icons">arrow_drop_down</i></button>'+\
' </td>'+\
'</tr>'
hostindex = (hostindex + 1)
# this fix single host report
if type(ik) is not dict:
break;
totports = (ports['open']+ports['closed']+ports['filtered'])
if filterservice == "" and filterportid == "":
scaninfobox2 = '<canvas id="chart1"></canvas>'
scaninfobox3 = '<canvas id="chart3" height="150"></canvas>'
else:
scaninfobox2 = ''+\
' Filter port / service: <b>'+html.escape(filterportid+filterservice)+'</b> <a href="/"><i class="fas fa-trash-alt"></i></a><br>'+\
' Total Ports: '+str(totports)+'<br>'+\
' Open Ports: '+str(ports['open'])+'<br>'+\
' Closed Ports: '+str(ports['closed'])+'<br>'+\
' Filtered Ports: '+str(ports['filtered'])+'<br>'
scaninfobox3 = '<div id="detailstopports"></div>'
r['scaninfo'] = ''+\
'<div class="row">'+\
' <div class="col s4">Scan Information</div>'+\
' <div class="col s4">Ports Status</div>'+\
' <div class="col s4">Top Ports / Services</div>'+\
'</div>'+\
'<div class="row">'+\
' <div class="col s4">'+\
' <b class="orange-text">Start:</b> '+o['@startstr']+'<br>'+\
' <b class="orange-text">Scan Type:</b> '+o['scaninfo']['@type']+'<br>'+\
' <b class="orange-text">Scan Protocol:</b> '+o['scaninfo']['@protocol']+'<br>'+\
' <b class="orange-text">Nmap Command:</b> <a class="activator" href="#!">view details</a>'+\
' </div>'+\
' <div class="col s4" style="border-left:solid #999 1px;">'+\
scaninfobox2+\
' </div>'+\
' <div class="col s4" style="border-left:solid #999 1px;">'+\
scaninfobox3+\
' </div>'+\
'</div>'
r['scandetails'] = '<div class="code" style="word-wrap: break-word;overflow-wrap: break-word;">'+\
' <p>'+o['@args']+'</p>'+\
' <p>'+\
' version: '+o['@version']+'<br>'+\
' xmloutputversion: '+o['@xmloutputversion']+'<br>'+\
' </p>'+\
'</div>'
allss = ''
allsslabels = ''
allssdata = ''
allssc = 0
for i in sorted(sscount, key=sscount.__getitem__, reverse=True):
if allssc <= 30:
if filterservice != i:
allss += '<a href="/report/service/'+html.escape(i)+'/">'+html.escape(i)+'('+str(sscount[i])+')</a>, '
else:
allss += '<span class="tmlabel" style="background-color:#ffcc00;color:#333;">'+html.escape(i)+'</span>, '
allsslabels += '"'+html.escape(i)+'", '
allssdata += ''+str(sscount[i])+','
allssc = (allssc + 1)
allpilabels = ''
allpidata = ''
allpilinks = ''
allpic = 1
for i in sorted(picount, key=picount.__getitem__, reverse=True):
if allpic <= 5:
allpilinks += '<a href="/report/portid/'+str(i)+'/">'+str(i)+'</a>, '
allpilabels += '"'+html.escape(i)+'", '
allpidata += ''+str(picount[i])+','
allpic = (allpic + 1)
elif allpic > 5 and allpic <= 10:
allpilinks += '<a href="/report/portid/'+str(i)+'/">'+str(i)+'</a>, '
allpic = (allpic + 1)
allostypelinks = ''
for i in sorted(allostypelist, key=allostypelist.__getitem__, reverse=True):
allostypelinks += '<a href="">'+str(i)+'</a>, '
r['pretable'] = ''
if filterservice == "" and filterportid == "":
r['pretable'] += '<div class="row">'+\
' <div class="col s3" style="padding:1px;"><div class="card" style="text-align:center;padding:6px;background-color:#3e3e3e;"><h4><i class="fab fa-creative-commons-sampling"></i> <span class="blue-text">'+str(hostsup)+'</span></h4><span class="small grey-text">HOSTS UP</span></div></div>'+\
' <div class="col s3" style="padding:1px;"><div class="card" style="text-align:center;padding:6px;background-color:#3e3e3e;"><h4><i class="fas fa-door-open"></i> <span class="green-text">'+str(ports['open'])+'</span></h4><span class="small grey-text">OPEN PORTS</span></div></div>'+\
' <div class="col s3" style="padding:1px;"><div class="card" style="text-align:center;padding:6px;background-color:#3e3e3e;"><h4><i class="fas fa-door-closed"></i> <span class="red-text">'+str(ports['closed'])+'</span></h4><span class="small grey-text">CLOSED PORTS</span></div></div>'+\
' <div class="col s3" style="padding:1px;"><div class="card" style="text-align:center;padding:6px;background-color:#3e3e3e;"><h4><i class="fas fa-filter"></i> <span class="orange-text">'+str(ports['filtered'])+'</span></h4><span class="small grey-text">FILTERED PORTS</span></div></div>'+\
'</div>'+\
'<div class="card" style="background-color:#3e3e3e;">'+\
' <div class="card-content">'+\
' <div class="row">'+\
' <div class="col s4"><b>Services:</b></div><div class="col s8"><b>Services:</b></div>'+\
' <div class="col s4" style="border-right:solid #999 1px;margin-top:10px;min-height:410px;">'+\
' <span style="font-family:monospace;font-size:12px;">'+allss[0:-2]+'</span><br><br>'+\
' <b>Top 10 Ports:</b><br><span style="font-family:monospace;font-size:12px;">'+allpilinks[0:-2]+'</span><br><br>'+\
' <b>OS Type List:</b><br><span style="font-family:monospace;font-size:12px;">'+allostypelinks[0:-2]+'</span>'+\
' </div>'+\
' <div class="col s8" style="margin-top:10px;"><canvas id="chart2" height="200"></canvas></div>'+\
' </div>'+\
' </div>'+\
'</div>'
r['pretable'] += '<script>'+\
' $(document).ready(function() {'+\
' var ctx = document.getElementById("chart1").getContext("2d");'+\
' var myChart = new Chart(ctx, {'+\
' type: "doughnut", data: {labels:["Open", "Filtered", "Closed"], datasets: [{ data: ['+str(ports['open'])+','+str(ports['filtered'])+','+str(ports['closed'])+'], backgroundColor:["rgba(0,200,0,0.8)","rgba(255,200,0,0.8)","rgba(255,0,0,0.8)"], borderColor:"#3e3e3e" }]}, options: {legend: { position: "right", labels: { fontColor: "#cccccc" } }}'+\
' });'+\
' var ctx = document.getElementById("chart3").getContext("2d");'+\
' var myChart = new Chart(ctx, {'+\
' type: "doughnut", data: {labels:['+allpilabels[0:-2]+'], datasets: [{ data: ['+allpidata[0:-1]+'], borderColor: "#3e3e3e", backgroundColor:["#e6194b", "#3cb44b", "#ffe119", "#4363d8", "#f58231", "#911eb4", "#46f0f0", "#f032e6", "#bcf60c", "#fabebe", "#008080", "#e6beff", "#9a6324", "#fffac8", "#800000", "#aaffc3", "#808000", "#ffd8b1", "#000075", "#808080", "#ffffff", "#000000"] }]}, options: {legend: { position: "right", labels: { fontColor: "#cccccc" }}}'+\
' });'+\
' var ctx = document.getElementById("chart2").getContext("2d");'+\
' var myChart = new Chart(ctx, {'+\
' type: "horizontalBar", data: { labels:['+allsslabels[0:-2]+'], datasets: [{ data: ['+allssdata[0:-1]+'], backgroundColor: "rgba(0,140,220,0.8)" }]}, options: {legend: { display: false }, scales: { xAxes: [{ ticks: { beginAtZero: true, fontColor: "#cccccc" } }], yAxes: [{ ticks: { fontColor: "#cccccc" } }] } }'+\
' });'+\
' });'+\
'</script>'
r['pretable'] += '<script>'+\
' $(document).ready(function() {'+\
' $("#scantitle").html("'+html.escape(request.session['scanfile'])+'");'+\
' $(".dropdown-trigger").dropdown();'+\
' $(".tooltipped").tooltip();'+\
' $(".perco").each(function() { '+\
' var pwidth = ( (($(this).attr("data-po") * 100) / '+str(totports)+') ); '+\
' /* console.log(pwidth); */ '+\
' $(this).css("width", pwidth+"%" ); '+\
' if($(this).attr("data-po") < 1) { $(this).html(" "); $(this).css("background-color","#666") } '+\
' });'+\
' $("#detailstopports").html(\'<span class="small">'+str(allss[0:-2])+'</span>\');'+\
' });'+\
'</script>'
#r['pretable'] += '<button class="btn blue" onclick="javascript:$(\'.zeroportopen\').fadeToggle();">Hide/Show hosts with no open ports</button>'+\
#' <button class="btn red" onclick="javascript:genPDF(\''+scanmd5+'\');">PDF</button>'+\
#'<br><br>'
r['pretable'] += ' <input type="hidden" id="cpestring" value="'+urllib.parse.quote_plus(base64.b64encode(json.dumps(cpe).encode()))+'" /> '
return render(request, 'nmapreport/index.html', r)