-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathNetwork_Scanner.py
More file actions
159 lines (121 loc) · 3.71 KB
/
Network_Scanner.py
File metadata and controls
159 lines (121 loc) · 3.71 KB
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
from flask import Flask, render_template, request, redirect, url_for, flash
import os
from datetime import date, datetime
import ipaddress
import sys
import validators
import socket
import urllib.parse
import nmap
try:
import re2 as re
except ImportError:
import re
app = Flask(__name__)
@app.route("/",methods=['POST','GET'])
def home():
error = ''
return render_template("main/index.html",err = error)
@app.route("/scan_home",methods=['POST','GET'])
def scan_home():
cat=''
global ip_url
global port_select
global p_range
p_range = ''
ip_url = ''
error = ''
port_range_pattern = re.compile("([0-9]+)-([0-9]+)")
if (request.method == 'POST'):
cat = request.form.get('cat')
ip_url = request.form.get('ip_url')
p_range = request.form.get('port_range')
port_select = request.form.get('port_select')
if cat == 'ip':
try:
ip = ipaddress.ip_address(ip_url)
if(port_range_pattern.search(p_range.replace(" ",""))):
return redirect(url_for('page2'))
else:
error = 'Invalid Port Range'
except ValueError:
error = 'IP Address Invalid'
except:
error = 'error'
elif cat == 'url':
if validators.url(ip_url) == True:
try:
o = urllib.parse.urlsplit(ip_url)
if (o.scheme=='https'):
new_url=ip_url.replace('https://','')
elif (o.scheme=='http'):
new_url=ip_url.replace('http://','')
ip_url = socket.gethostbyname(new_url)
if(port_range_pattern.search(p_range.replace(" ",""))):
return redirect(url_for('page2'))
else:
error = 'Invalid Port Range'
except (UnboundLocalError, socket.gaierror):
sys.exit()
else:
error = 'Invalid URL'
else:
error = 'Invalid Input'
return render_template("scan/index.html",err = error)
@app.route("/page2", methods=['GET', 'POST'])
def page2():
global ip_url
global port_select
global p_range
open_port=[]
state=[]
service=[]
product=[]
error = ''
try:
if(port_select=='open_pt'):
nmScan = nmap.PortScanner()
nmScan.scan(ip_url, p_range)
for host in nmScan.all_hosts():
hos=nmScan[host].hostname()
working=nmScan[host].state()
for protocol in nmScan[host].all_protocols():
Protocol = protocol
ports = nmScan[host][protocol].keys()
sorted(ports)
for port in ports:
if(nmScan[host][protocol][port]['state']=='open'):
open_ports=port
open_port.append(open_ports)
states=nmScan[host][protocol][port]['state']
state.append(states)
services=nmScan[host][protocol][port]['name']
service.append(services)
products=nmScan[host][protocol][port]['product']
product.append(products)
elif(port_select=='filt_pt'):
nmScan = nmap.PortScanner()
nmScan.scan(ip_url, p_range)
for host in nmScan.all_hosts():
hos=nmScan[host].hostname()
working=nmScan[host].state()
for protocol in nmScan[host].all_protocols():
Protocol = protocol
ports = nmScan[host][protocol].keys()
sorted(ports)
for port in ports:
if(nmScan[host][protocol][port]['state']=='filtered'):
open_ports=port
open_port.append(open_ports)
states=nmScan[host][protocol][port]['state']
state.append(states)
services=nmScan[host][protocol][port]['name']
service.append(services)
products=nmScan[host][protocol][port]['product']
product.append(products)
return render_template("scan/page_2.html",i_u = ip_url,op_pt=open_port,sta=state,serv=service,prot=Protocol,host_nm=hos,work=working,prod=product)
except Exception as e:
error = 'IP Address OR URL Not Scannable Try Another IP OR Website'
return render_template("scan/index.html",err = error)
return redirect(url_for('scan_home'))
app.run(debug=True)