NetExec/nxc/parsers/ip.py

29 lines
973 B
Python
Raw Normal View History

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
2021-09-21 09:29:07 +00:00
from ipaddress import ip_address, ip_network, summarize_address_range, ip_interface
2016-05-16 23:48:31 +00:00
2023-05-02 15:17:59 +00:00
2016-05-16 23:48:31 +00:00
def parse_targets(target):
try:
2023-05-02 15:17:59 +00:00
if "-" in target:
start_ip, end_ip = target.split("-")
2016-05-16 23:48:31 +00:00
try:
end_ip = ip_address(end_ip)
except ValueError:
first_three_octets = start_ip.split(".")[:-1]
first_three_octets.append(end_ip)
2023-05-02 15:17:59 +00:00
end_ip = ip_address(".".join(first_three_octets))
2016-05-16 23:48:31 +00:00
for ip_range in summarize_address_range(ip_address(start_ip), end_ip):
for ip in ip_range:
yield str(ip)
else:
2023-05-08 18:39:36 +00:00
if ip_interface(target).ip.version == 6 and ip_address(target).is_link_local:
yield str(target)
else:
for ip in ip_network(target, strict=False):
2021-09-21 09:29:07 +00:00
yield str(ip)
except ValueError as e:
yield str(target)