Created
February 26, 2024 13:17
-
-
Save dreizehnutters/26bdcaf9d0f774ad01f0b1989d18dfd3 to your computer and use it in GitHub Desktop.
Extract host that run specifc services from nmap scan results (XML)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
from os import listdir, path | |
from argparse import ArgumentParser | |
import xml.etree.ElementTree as ET | |
__version__ = 1.0 | |
def scan(in_file, search_pattern): | |
systems = {} | |
try: | |
root = ET.parse(in_file).getroot() | |
for cur_host in root.findall('host'): | |
ipv4_addr = [h.attrib['addr'] for h in cur_host.findall('address') if h.attrib['addrtype'] == 'ipv4'][0] | |
systems[ipv4_addr] = set() | |
for cur_xml_port in cur_host.findall('ports/port'): | |
port_id = cur_xml_port.attrib['portid'] | |
systems[ipv4_addr].update(port_id for tag in cur_xml_port if search_pattern in str(tag.attrib)) | |
except Exception as err: | |
print(err) | |
raise Exception(f"[!] Can not parse '{in_file}'") | |
return systems | |
def write_file(systems_dict, output): | |
try: | |
with open(output, 'w+', encoding='utf-8') as fd: | |
for key in systems_dict.keys(): | |
for val in systems_dict[key]: | |
loot = f"{key}:{val}" | |
print(loot) | |
fd.write(f"{loot}\n") | |
except Exception as err: | |
print(err) | |
raise Exception(f"[!] Can not write '{output}'") | |
def arguments_parser(): | |
parser = ArgumentParser( | |
description="Creates an input file for service-scans (ssl, ssh) based on nmap script scan results.", | |
epilog="Example usage: ./%(prog)s -i nmap-dir ssl") | |
parser.add_argument('-i', '--input', help="nmap input directory or file (containing script scan .xml)", type=str, required=True) | |
parser.add_argument('-o', '--output', help="output file to write found sockets", type=str, default=None) | |
parser.add_argument('service', help="service type to scan for (ssl, ssh, ftp, ...)", type=str) | |
return parser.parse_args() | |
if __name__ == "__main__": | |
banner = f""" | |
______ | |
(_____ \ | |
____ ____ _____ ____ ____) ) ___ _ _ ____ | |
| _ \| \(____ | _ \ / ____/ /___) | | / ___) | |
| | | | | | / ___ | |_| | (_____|___ |\ V ( (___ | |
|_| |_|_|_|_\_____| __/|_______|___/ \_/ \____) | |
|_| v{__version__}\n""" | |
print(banner) | |
args = arguments_parser() | |
print(f"Input: {args.input}") | |
args_output = f"{args.service}-servcies.txt" if args.output == None else args.output | |
print(f"Output: {args_output}\n") | |
print(f"[*] scanning for service: '{args.service}'") | |
results = {} | |
try: | |
if not path.isdir(args.input): | |
if args.input.endswith('.xml'): | |
results = scan(args.input, args.service) | |
else: | |
print("Input is not a xml file.") | |
else: | |
for file in [path.join(args.input, f) for f in listdir(args.input) if f.endswith('.xml')]: | |
result_dict = scan(file, args.service) | |
for ip_key in result_dict: | |
try: | |
results[ip_key].update(result_dict[ip_key]) | |
except KeyError: | |
results[ip_key] = result_dict[ip_key] | |
write_file(results, args_output) | |
except Exception as err: | |
print(err) |
Hi Bro This is Useful
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In summary, the script automates the process of extracting relevant information from nmap scan results for a specific service type and saves it to a file for further analysis or usage.