-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
296 lines (254 loc) · 9.01 KB
/
main.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
import argparse
from scapy.all import *
import nmap
def arp_scan(ip):
request = Ether(dst="ff:ff:ff:ff:ff:ff") / ARP(pdst=ip)
ans, unans = srp(request, timeout=2, retry=1)
result = []
for element in ans:
device_dict = {"ip": element[1].psrc, "mac": element[1].hwsrc}
result.append(device_dict)
return result
def icmp_echo_ping(ip):
print("ICMP Echo Ping Scan:")
scanner = nmap.PortScanner()
scanner.scan(hosts=ip, arguments='-PE')
hosts = scanner.all_hosts()
if hosts:
return hosts
else:
return None
def icmp_echo_ping_sweep(ip):
print("ICMP Echo Ping Sweep Scan:")
scanner = nmap.PortScanner()
scanner.scan(hosts=ip, arguments='-PS')
hosts = scanner.all_hosts()
if hosts:
return hosts
else:
return None
def icmp_timestamp_ping(ip):
print("ICMP Timestamp Ping Scan:")
scanner = nmap.PortScanner()
scanner.scan(hosts=ip, arguments='-PP')
hosts = scanner.all_hosts()
if hosts:
return hosts
else:
return None
def icmp_address_mask_ping(ip):
print("ICMP Address Mask Ping Scan:")
scanner = nmap.PortScanner()
scanner.scan(hosts=ip, arguments='-PM')
hosts = scanner.all_hosts()
if hosts:
return hosts
else:
return None
def udp_port_scan(ip,ports):
print("UDP Port Scan:")
for port in ports:
resp = sr1(IP(dst=ip)/UDP(sport=RandShort(),dport=port), timeout=2, verbose=False)
if resp != None:
if resp.haslayer(UDP):
print('Port {} is Open.'.format(port))
elif int(resp[ICMP].type)==3 and int(resp[ICMP].code)==3:
print('Port {} is Closed'.format(port))
else:
print('Port {} is Filtered'.format(port))
def tcp_scan(ip, ports):
print("SYN SCAN")
try:
syn = IP(dst=ip) / TCP(dport=ports, flags="S")
except socket.gaierror:
raise ValueError('Hostname {} could not be resolved.'.format(ip))
ans, unans = sr(syn, timeout=2, retry=1)
result = []
for sent, received in ans:
if received[TCP].flags == "SA":
result.append(received[TCP].sport)
return result
def tcp_scan_xmas(ip, ports):
print('Xmas Scan')
try:
syn = IP(dst=ip) / TCP(sport=RandShort(),dport=ports, flags="FPU")
except socket.gaierror:
raise ValueError('Hostname {} could not be resolved.'.format(ip))
ans= sr1(syn, timeout=2, retry=1)
if ans !=None:
if ans.haslayer(TCP):
if ans[TCP].flags==4:
print("Closed")
else:
print("Filtered")
else:
print("Open|Filtered",'Port {}.'.format(ports))
def tcp_scan_fin(ip, ports):
print('FIN Scan')
try:
syn = IP(dst=ip) / TCP(sport=RandShort(),dport=ports, flags="F")
except socket.gaierror:
raise ValueError('Hostname {} could not be resolved.'.format(ip))
ans= sr1(syn, timeout=2, retry=1)
if ans !=None:
if ans.haslayer(TCP):
if ans[TCP].flags==4:
print("Closed")
elif int(ans[ICMP].type)==3 and int(scan[ICMP].code) in [1,2,3,9,10,13]:
print("Filtered")
else:
print("Open|Filtered",'Port {}.'.format(ports))
def tcp_scan_null(ip, ports):
print('NULL Scan')
try:
syn = IP(dst=ip) / TCP(sport=RandShort(),dport=ports, flags="")
except socket.gaierror:
raise ValueError('Hostname {} could not be resolved.'.format(ip))
ans= sr1(syn, timeout=2, retry=1)
if ans !=None:
if ans.haslayer(TCP):
if ans[TCP].flags==20:
print("Closed")
elif int(ans[ICMP].type)==3 and int(scan[ICMP].code) in [1,2,3,9,10,13]:
print("Filtered")
else:
print("Open|Filtered",'Port {} is open.'.format(ports))
def tcp_scan_ack(ip, ports):
print('ACK Scan')
try:
syn = IP(dst=ip) / TCP(sport=RandShort(),dport=ports, flags="A")
except socket.gaierror:
raise ValueError('Hostname {} could not be resolved.'.format(ip))
ans= sr1(syn, timeout=2, retry=1)
if ans !=None:
if ans.haslayer(TCP):
if ans[TCP].flags==20:
print("Closed")
elif int(ans[ICMP].type)==3 and int(scan[ICMP].code) in [1,2,3,9,10,13]:
print("Filtered")
else:
print("Open|Filtered",'Port {} is open.'.format(ports))
def IP_SCAN(ip):
print("IP Protocol Ping Scan:")
protocols = { 0: "IP" ,1: "ICMP", 6: "TCP"}
for proto, name in protocols.items():
resp = sr1(IP(dst=ip)/IP(proto=proto), timeout=2, verbose=False)
if resp:
print("Protocol", name, "is supported")
else:
print("Protocol", name, "is not supported")
def main():
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers(
dest="command", help="Command to perform.", required=True
)
arp_subparser = subparsers.add_parser(
'ARP', help='Perform a network scan using ARP requests.'
)
arp_subparser.add_argument(
'IP', help='An IP address (e.g. 192.168.1.1) or address range (e.g. 192.168.1.1/24) to scan.'
)
tcp_subparser = subparsers.add_parser(
'TCP', help='Perform a TCP scan using SYN packets.'
)
tcp_subparser.add_argument('IP', help='An IP address or hostname to ip.')
tcp_subparser.add_argument(
'ports', nargs='+', type=int,
help='Ports to scan, delimited by spaces. When --range is specified, scan a range of ports. Otherwise, scan individual ports.'
)
tcp_subparser.add_argument(
'--range', action='store_true',
help='Specify a range of ports. When this option is specified, <ports> should be given as <low_port> <high_port>.'
)
udp_subparser = subparsers.add_parser(
'UDP', help='Perform a UDP scan.'
)
udp_subparser.add_argument(
'IP', help='An IP address or hostname to ip.'
)
udp_subparser.add_argument(
'ports', nargs='+', type=int,
help='Ports to scan, delimited by spaces. When --range is specified, scan a range of ports. Otherwise, scan individual ports.'
)
udp_subparser.add_argument(
'--range', action='store_true',
help='Specify a range of ports. When this option is specified, <ports> should be given as <low_port> <high_port>.'
)
icmp_subparser = subparsers.add_parser(
'ICMP', help='Perform a ICMP scan.'
)
icmp_subparser.add_argument(
'IP', help='An IP address or hostname to ip.'
)
ip_subparser = subparsers.add_parser(
'IP', help='Perform a IP scan.'
)
ip_subparser.add_argument(
'IP', help='An IP address or hostname to ip.'
)
args = parser.parse_args()
if args.command == 'ARP':
result = arp_scan(args.IP)
print("IP Address\t\tMAC Address")
print("-----------------------------------------")
for device in result:
print(device["ip"] + "\t\t" + device["mac"])
elif args.command == 'TCP':
if args.range:
ports = tuple(args.ports)
else:
ports = args.ports
try:
result = tcp_scan(args.IP, ports)
for port in result:
print('Port {} is open.'.format(port))
tcp_scan_xmas(args.IP, ports)
tcp_scan_fin(args.IP, ports)
tcp_scan_null(args.IP, ports)
tcp_scan_ack(args.IP, ports)
except ValueError as error:
print(error)
exit(1)
elif args.command=='UDP':
if args.range:
ports=tuple(args.ports)
else:
ports=args.ports
try:
udp_port_scan(args.IP,ports)
except ValueError as error:
print(error)
exit(1)
elif args.command=='ICMP':
try:
result=icmp_echo_ping(args.IP)
if result:
for r in result:
print(r)
else:
print("No hosts found.")
result=icmp_echo_ping_sweep(args.IP)
if result:
for r in result:
print(r)
else:
print("No hosts found.")
result=icmp_timestamp_ping(args.IP)
if result:
for r in result:
print(r)
else:
print("No hosts found.")
result=icmp_address_mask_ping(args.IP)
if result:
for r in result:
print(r)
else:
print("No hosts found.")
except ValueError as error:
print(error)
exit(1)
elif args.command=='IP':
IP_SCAN(args.IP)
if __name__ == '__main__':
main()