-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathscapy_scan.py
executable file
·179 lines (144 loc) · 7.44 KB
/
scapy_scan.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import config_params
from common_utils import menu_utils
from common_utils import var_utils
from scapy.all import * # external package
import logging
logging.getLogger("scapy.runtime").setLevel(logging.ERROR)
""" This module uses Scapy in order to scan vulnerable devices """
def _tcp_scan(ip, port):
""" This method performs TCP scans """
menu_utils.mixed_info("\nTCP scan: Analysing port: ", str(port))
response = sr1(IP(dst=ip) / TCP(dport=int(port), flags="S"), timeout=config_params.SCAPY_TIMEOUT) # Sending flag SYN
print("Response type: " + str(type(response)))
if str(type(response)) == "<class 'NoneType'>":
menu_utils.warning("[-] Port %s closed" % port)
return "CLOSED"
elif response.haslayer(TCP):
if response.getlayer(TCP).flags == 0x12: # Receiving flags ACK + SYN (00010010b)
menu_utils.super_highlighted_info("[+] Port %s open" % port)
return "OPEN"
elif response.haslayer(ICMP):
if response.getlayer(TCP).flags == 0x14:
menu_utils.warning("[-] Port %s closed" % port) # Receiving flags ACK + RST (00010100b)
return "CLOSED"
else:
return "UNKNOWN"
def _xmas_scan(ip, port):
""" This method performs XMAS scans """
menu_utils.mixed_info("\nXMAS scan: Analysing port: ", str(port))
response = sr1(IP(dst=ip) / TCP(dport=int(port), flags="FPU"), timeout=config_params.SCAPY_TIMEOUT) # Sending flags
print("Response type: " + str(type(response))) # FIN, PUSH, URGENT
if response is None: # Receiving no answer
menu_utils.highlighted_info("[?] Port %s open or filtered" % port)
return "OPEN|FILTERED"
elif response.haslayer(TCP):
if response.getlayer(TCP).flags == 0x14:
menu_utils.warning("[-] Port %s closed" % port) # Receiving flags ACK + RST (00010100b)
return "CLOSED"
elif response.haslayer(ICMP): # Receiving ICMP error
if (response.getlayer(ICMP).type == 3) & (int(response.getlayer(ICMP).code) in [1, 2, 3, 9, 10, 13]):
menu_utils.warning("[x] Port %s filtered" % port)
return "FILTERED"
else:
return "UNKNOWN"
def _null_scan(ip, port):
""" This method performs NULL scans """
menu_utils.mixed_info("\nNULL scan: Analysing port: ", str(port))
response = sr1(IP(dst=ip) / TCP(dport=int(port), flags=""), timeout=config_params.SCAPY_TIMEOUT) # Sending no flags
print("Response type: " + str(type(response)))
if str(type(response)) == "<class 'NoneType'>": # Receiving no answer
menu_utils.highlighted_info("[?] Port %s open or filtered" % port)
return "OPEN|FILTERED"
elif response.haslayer(TCP):
if response.getlayer(TCP).flags == 0x14: # Receiving flags ACK + RST (00010100b)
menu_utils.warning("[-] Port %s closed" % port)
return "CLOSED"
elif response.haslayer(ICMP): # Receiving ICMP error
if (response.getlayer(ICMP).type == 3) & (int(response.getlayer(ICMP).code) in [1, 2, 3, 9, 10, 13]):
menu_utils.warning("[x] Port %s filtered" % port)
return "FILTERED"
else:
return "UNKNOWN"
def _ack_scan(ip, port):
""" This method performs ACK scans """
menu_utils.mixed_info("\nACK scan: Analysing port: ", str(port))
response = sr1(IP(dst=ip) / TCP(dport=int(port), flags="A"), timeout=config_params.SCAPY_TIMEOUT) # Sending flag ACK
print("Response type: " + str(type(response)))
if str(type(response)) == "<class 'NoneType'>":
menu_utils.warning("[-] Firewall found")
return "FILTERED"
elif response.haslayer(TCP):
if response.getlayer(TCP).flags == 0x4: # Receiving flag RST (00000100b)
menu_utils.highlighted_info("[+] Firewall not found")
return "UNFILTERED"
elif response.haslayer(ICMP): # Receiving ICMP error
if (response.getlayer(ICMP).type == 3) & (int(response.getlayer(ICMP).code) in [1, 2, 3, 9, 10, 13]):
menu_utils.warning("[-] Firewall found")
return "FILTERED"
else:
return "UNKNOWN"
def _udp_scan(ip, port):
""" This method performs UDP scans """
menu_utils.mixed_info("\nUDP scan: Analysing port: ", str(port))
response = sr1(IP(dst=ip) / UDP(dport=int(port)), timeout=config_params.SCAPY_TIMEOUT)
print("Response type: " + str(type(response)))
if str(type(response)) == "<class 'NoneType'>":
menu_utils.highlighted_info("[?] Port %s open or filtered" % port)
return "OPEN|FILTERED"
elif response.haslayer(UDP):
menu_utils.super_highlighted_info("[+] Port % open" % port)
return "OPEN"
elif response.haslayer(ICMP):
if (int(response.getlayer(ICMP).type) == 3) & (int(response.getlayer(ICMP).code == 3)):
menu_utils.warning("[-] Port %s closed" % port)
return "CLOSED"
elif (int(response.getlayer(ICMP).type) == 3) & (int(response.getlayer(ICMP).code) in [1, 2, 9, 10, 13]):
menu_utils.warning("[x] Port %s filtered" % port)
return "FILTERED"
else:
return "UNKNOWN"
def scan(scan_type, ip, ports):
""" This method performs different type of SCAPY scans """
open_tcp_ports = []
unfiltered_tcp_ports = []
open_udp_ports = []
open_or_filtered_udp_ports = []
menu_utils.header('SCAPY SCAN, IP: %s ' % ip)
for port in ports:
res_tcp_tcp = ""
res_tcp_xmas = ""
res_tcp_null = ""
res_tcp_ack = ""
res_udp = ""
if scan_type == 'TCP':
res_tcp_tcp = _tcp_scan(ip, port)
elif scan_type == 'XMAS':
res_tcp_xmas = _xmas_scan(ip, port)
elif scan_type == 'NULL':
res_tcp_null = _null_scan(ip, port)
elif scan_type == 'ACK':
res_tcp_ack = _ack_scan(ip, port)
elif scan_type == 'UDP':
res_udp = _udp_scan(ip, port)
else:
res_tcp_tcp = _tcp_scan(ip, port)
res_tcp_xmas = _xmas_scan(ip, port)
res_tcp_null = _null_scan(ip, port)
res_tcp_ack = _ack_scan(ip, port)
res_udp = _udp_scan(ip, port)
if (res_tcp_tcp == 'OPEN') | (res_tcp_xmas == 'OPEN') | (res_tcp_null == 'OPEN') | (res_tcp_ack == 'OPEN'):
open_tcp_ports.append(port)
if (res_tcp_tcp == 'UNFILTERED') | (res_tcp_xmas == 'UNFILTERED') | (res_tcp_null == 'UNFILTERED') | \
(res_tcp_ack == 'UNFILTERED'):
unfiltered_tcp_ports.append(port)
if res_udp == 'OPEN':
open_udp_ports.append(port)
if res_udp == 'OPEN|FILTERED':
open_or_filtered_udp_ports.append(port)
menu_utils.header('SCAPY SCAN, IP: %s ' % ip)
menu_utils.mixed_info("Open TCP ports", var_utils.list_2_string(open_tcp_ports))
menu_utils.mixed_info("Unfiltered TCP ports", var_utils.list_2_string(var_utils.numbers_2_ranges(unfiltered_tcp_ports)))
menu_utils.mixed_info("Open UDP ports", var_utils.list_2_string(open_udp_ports))
menu_utils.mixed_info("Open or filtered UDP ports", var_utils.list_2_string(open_or_filtered_udp_ports))