-
Notifications
You must be signed in to change notification settings - Fork 0
/
port.py
40 lines (35 loc) · 1.17 KB
/
port.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
class Port:
def __init__(self, text):
self.text = text
self.any = False
self.portList = set()
if text == "any":
self.any = True
else:
self.parse_ports(text)
def __repr__(self):
if self.any:
return "Any Port"
else:
return "Port: " + self.text
def parse_ports(self, text):
"""
Parse the input text to create a list of port numbers or ranges.
"""
if ':' in text:
start, end = map(int, text.split(':'))
self.portList.update(range(start, end + 1))
elif ',' in text:
self.portList.update(map(int, text.split(',')))
else:
self.portList.add(int(text))
def match(self, port):
"""
Check if the provided port matches the criteria set by the Port object.
"""
return self.any or port in self.portList
def is_nmap_port_scan(self, packet):
"""
Check if the packet indicates an Nmap port scan.
"""
return packet.haslayer(TCP) and (packet[TCP].flags == 2 or packet[TCP].flags == 18)