mirror of
https://github.com/ChronosX88/PyNesca.git
synced 2024-11-22 05:02:18 +00:00
Update Parser.py
Added function docstrings, comments, PEP8 compliance
This commit is contained in:
parent
825b89e62a
commit
7c8356ca4d
72
Parser.py
72
Parser.py
@ -2,53 +2,75 @@ import ipaddress
|
|||||||
|
|
||||||
|
|
||||||
def getPortsFromString(ports):
|
def getPortsFromString(ports):
|
||||||
# Converts ports from form 20-40,100-900,40000-70000
|
"""
|
||||||
# It will automatically prune off non-existent ports (<1 >65535)
|
Parses ports from string, returns them as integers in the list.
|
||||||
if ports is None:
|
Handles non-existent ports and non-port values.
|
||||||
return [21, 22, 23, 25, 80, 443, 110, 111, 135, 139, 445, 8080, 8443, 53, 143, 989, 990, 3306, 1080, 5554, 6667, 2222, 4444, 666, 6666, 1337, 2020, 31337] # Change to default ports from constant
|
"""
|
||||||
else:
|
if ports:
|
||||||
if "-" not in ports:
|
# Using set to avoid repetitions
|
||||||
tports = ports.split(",")
|
parsed = set()
|
||||||
print(tports)
|
ports = ports.split(",")
|
||||||
else:
|
for port in ports:
|
||||||
ports = ports.split(",")
|
try:
|
||||||
tports = []
|
# Input is in range form ("100-200"):
|
||||||
for port in ports:
|
if '-' in port:
|
||||||
if "-" not in port:
|
start, end = map(int, port.split('-'))
|
||||||
tports.append(int(port))
|
parsed.update(
|
||||||
|
[p for p in range(start, end + 1) if 65355 >= p > 0])
|
||||||
|
# Input is a single port ("80"):
|
||||||
else:
|
else:
|
||||||
# I made this one line because I wanted to
|
parsed.add(int(port))
|
||||||
tports.extend(
|
except ValueError as e:
|
||||||
list(range(int(port.split("-")[0]), int(port.split("-")[1]) + 1)))
|
# If we get any not integer just ignore it
|
||||||
ports = [int(n) for n in tports if int(n) > 0 and int(n) < 65536]
|
pass
|
||||||
return ports
|
return sorted(list(parsed))
|
||||||
|
else:
|
||||||
|
# Change to default ports from constant
|
||||||
|
return [21, 22, 23, 25, 80, 443, 110, 111, 135, 139, 445, 8080, 8443, 53, 143, 989, 990, 3306, 1080, 5554, 6667, 2222, 4444, 666, 6666, 1337, 2020, 31337]
|
||||||
|
|
||||||
|
|
||||||
def getCIDRFromRanges(ips):
|
def getCIDRFromRanges(ips):
|
||||||
|
"""
|
||||||
|
Parses ip input string, returns the generator over them.
|
||||||
|
|
||||||
|
Supports next inputs:
|
||||||
|
1) 1.2.3.4
|
||||||
|
2) 192.168.0.0/24
|
||||||
|
3) 1.2.3.4 - 5.6.7.8
|
||||||
|
Any non-ip value will be ignored.
|
||||||
|
"""
|
||||||
|
|
||||||
|
# A set to contain non repeating ip objects from ipaddress
|
||||||
ip_objects = set()
|
ip_objects = set()
|
||||||
inputs = [ip.strip() for ip in ips.split(',')]
|
inputs = [ip.strip() for ip in ips.split(',')]
|
||||||
|
|
||||||
for input_ in inputs:
|
for input_ in inputs:
|
||||||
try:
|
try:
|
||||||
|
# Input is in range form ("1.2.3.4 - 5.6.7.8"):
|
||||||
if '-' in input_:
|
if '-' in input_:
|
||||||
input_ips = input_.split('-')
|
input_ips = input_.split('-')
|
||||||
ranges = {ipaddr for ipaddr in ipaddress.summarize_address_range(
|
ranges = {ipaddr for ipaddr in ipaddress.summarize_address_range(
|
||||||
ipaddress.IPv4Address(input_ips[0]),
|
ipaddress.IPv4Address(input_ips[0]),
|
||||||
ipaddress.IPv4Address(input_ips[1]))
|
ipaddress.IPv4Address(input_ips[1]))
|
||||||
}
|
}
|
||||||
ip_objects.update(ranges)
|
ip_objects.update(ranges)
|
||||||
|
# Input is in CIDR form ("192.168.0.0/24"):
|
||||||
elif '/' in input_:
|
elif '/' in input_:
|
||||||
network = ipaddress.ip_network(input_)
|
network = ipaddress.ip_network(input_)
|
||||||
ip_objects.add(network)
|
ip_objects.add(network)
|
||||||
|
# Input is a single ip ("1.1.1.1"):
|
||||||
else:
|
else:
|
||||||
ip = ipaddress.ip_address(input_)
|
ip = ipaddress.ip_address(input_)
|
||||||
ip_objects.add(ip)
|
ip_objects.add(ip)
|
||||||
except ValueError as e:
|
except ValueError as e:
|
||||||
print(e)
|
# If we get any non-ip value just ignore it
|
||||||
|
pass
|
||||||
|
|
||||||
for ip_obj in ip_objects:
|
for ip_obj in ip_objects:
|
||||||
if not isinstance(ip_obj, ipaddress.IPv4Address):
|
# The object is just one ip, simply yield it:
|
||||||
for ip in ip_obj.hosts():
|
if isinstance(ip_obj, ipaddress.IPv4Address):
|
||||||
yield ip
|
|
||||||
else:
|
|
||||||
yield ip_obj
|
yield ip_obj
|
||||||
|
# The object is a network, yield every host in it:
|
||||||
|
else:
|
||||||
|
for host in ip_obj.hosts():
|
||||||
|
yield host
|
||||||
|
Loading…
Reference in New Issue
Block a user