mirror of
https://github.com/ChronosX88/PyNesca.git
synced 2024-11-21 20:52: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):
|
||||
# Converts ports from form 20-40,100-900,40000-70000
|
||||
# It will automatically prune off non-existent ports (<1 >65535)
|
||||
if ports is None:
|
||||
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 "-" not in ports:
|
||||
tports = ports.split(",")
|
||||
print(tports)
|
||||
else:
|
||||
ports = ports.split(",")
|
||||
tports = []
|
||||
for port in ports:
|
||||
if "-" not in port:
|
||||
tports.append(int(port))
|
||||
"""
|
||||
Parses ports from string, returns them as integers in the list.
|
||||
Handles non-existent ports and non-port values.
|
||||
"""
|
||||
if ports:
|
||||
# Using set to avoid repetitions
|
||||
parsed = set()
|
||||
ports = ports.split(",")
|
||||
for port in ports:
|
||||
try:
|
||||
# Input is in range form ("100-200"):
|
||||
if '-' in port:
|
||||
start, end = map(int, port.split('-'))
|
||||
parsed.update(
|
||||
[p for p in range(start, end + 1) if 65355 >= p > 0])
|
||||
# Input is a single port ("80"):
|
||||
else:
|
||||
# I made this one line because I wanted to
|
||||
tports.extend(
|
||||
list(range(int(port.split("-")[0]), int(port.split("-")[1]) + 1)))
|
||||
ports = [int(n) for n in tports if int(n) > 0 and int(n) < 65536]
|
||||
return ports
|
||||
parsed.add(int(port))
|
||||
except ValueError as e:
|
||||
# If we get any not integer just ignore it
|
||||
pass
|
||||
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):
|
||||
"""
|
||||
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()
|
||||
inputs = [ip.strip() for ip in ips.split(',')]
|
||||
|
||||
for input_ in inputs:
|
||||
try:
|
||||
# Input is in range form ("1.2.3.4 - 5.6.7.8"):
|
||||
if '-' in input_:
|
||||
input_ips = input_.split('-')
|
||||
ranges = {ipaddr for ipaddr in ipaddress.summarize_address_range(
|
||||
ipaddress.IPv4Address(input_ips[0]),
|
||||
ipaddress.IPv4Address(input_ips[1]))
|
||||
}
|
||||
}
|
||||
ip_objects.update(ranges)
|
||||
# Input is in CIDR form ("192.168.0.0/24"):
|
||||
elif '/' in input_:
|
||||
network = ipaddress.ip_network(input_)
|
||||
ip_objects.add(network)
|
||||
# Input is a single ip ("1.1.1.1"):
|
||||
else:
|
||||
ip = ipaddress.ip_address(input_)
|
||||
ip_objects.add(ip)
|
||||
except ValueError as e:
|
||||
print(e)
|
||||
# If we get any non-ip value just ignore it
|
||||
pass
|
||||
|
||||
for ip_obj in ip_objects:
|
||||
if not isinstance(ip_obj, ipaddress.IPv4Address):
|
||||
for ip in ip_obj.hosts():
|
||||
yield ip
|
||||
else:
|
||||
# The object is just one ip, simply yield it:
|
||||
if isinstance(ip_obj, ipaddress.IPv4Address):
|
||||
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