-
Notifications
You must be signed in to change notification settings - Fork 0
/
nmap_on_crack.py
85 lines (53 loc) · 1.93 KB
/
nmap_on_crack.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import sys
import xml.etree.ElementTree as ET
from collections import defaultdict
import os
from datetime import datetime
service_mappings = {
'ms-wbt-server' : 'rdp',
'domain' : 'dns'
}
def initnmap(IP):
os.system (f"nmap -p- {IP} -oX result_{IP}_{datetime.date(datetime.now())}.xml")
return (f"result_{IP}_{datetime.date(datetime.now())}.xml")
def treeparse(file):
tree = ET.parse(file)
root = tree.getroot()
hosts = defaultdict(list)
for child in root:
if child.tag != 'host': continue
e_address = child.find('address')
e_ports = child.find('ports')
ip_addr = e_address.get('addr')
for e_port in e_ports:
if e_port.tag != 'port': continue
port = int(e_port.get('portid'))
# state is always present for a port
e_state = e_port.find('state')
state = e_state.get('state')
e_service = e_port.find('service')
service_name = 'n/a'
# service is not always present
if e_service is not None:
service_name = e_service.get('name')
if service_name in service_mappings:
service_name = service_mappings[service_name]
if state == 'open':
hosts[ip_addr].append((port, service_name))
return (hosts)
def scripts(resolved_hosts):
for ip, ports in resolved_hosts.items():
print(f'[*] ip : {ip}')
portlist = []
for (port, service_name) in ports:
if service_name == 'n/a': continue
portlist.append((port,service_name))
for i in portlist:
print(f'\t{i}')
input("Press Enter to continue...")
for j in portlist:
os.system(f'nmap -Pn -sS -p{j[0]} --script "{j[1]}*" -v {ip}')
IP = sys.argv[1]
path = initnmap(IP)
clean = treeparse(path)
script_results = scripts(clean)