-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathnmap.py
More file actions
193 lines (176 loc) · 6.81 KB
/
nmap.py
File metadata and controls
193 lines (176 loc) · 6.81 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import json
import re
def ParseNmap():
filename = "nmap-service-probes"
with open(filename, encoding="utf-8") as f:
lines = f.readlines()
probes = []
probe = {}
for line in lines:
line = line.strip()
if line == "":
continue
if line.startswith("#"):
continue
if line.startswith("Exclude "):
continue
if line.startswith("Probe "):
if probe:
probes.append(probe)
probe = {
"protocol": "",
"probename": "",
"probestring": "",
"ports": [],
"sslports": [],
"totalwaitms": "",
"tcpwrappedms": "",
"rarity": "",
"fallback": "",
"matches": [],
"softmatches": []
}
# get probe
protocol = line[6:9]
if protocol not in ["TCP", "UDP"]:
raise Exception(protocol + " 不支持")
probename_start = 10
probename_end = line.index(" ", probename_start)
if probename_end - probename_start <= 0:
raise Exception("probename解析失败")
probename = line[probename_start:probename_end]
probestring_start = line.index("q|", probename_end) + 1
probestring = line[probestring_start:].strip("|")
probe["protocol"] = protocol
probe["probename"] = probename
probe["probestring"] = probestring
elif line.startswith("match "):
# Syntax: match <service> <pattern> [<versioninfo>]
# match iperf3 m|^\t$|
# softmatch quic m|^\r\x89\xc1\x9c\x1c\*\xff\xfc\xf1((?:Q[0-8]\d\d)+)$| i/QUIC versions$SUBST(1,"Q",", Q")/
matchtext = line[len("match "):]
index = matchtext.index(" m")
m = matchtext[index + 2] # 获取m后边的字符
name = matchtext[:index]
matchtext = matchtext[len(name):].strip()
regx_start = 2
regx_end = matchtext.index(m, regx_start)
regx = matchtext[regx_start:regx_end]
regx_flag = ""
if regx_end + 1 < len(matchtext):
regx_flag = matchtext[regx_end + 1].strip()
dd = {
"pattern": regx,
"name": name,
"pattern_flag": regx_flag,
'versioninfo': {'cpename': "",
'devicetype': "",
'hostname': "",
'info': "",
'operatingsystem': "",
'vendorproductname': "",
'version': ""
}
}
matchtext = matchtext[regx_end:]
regx_p = "(\w|cpe:)/(.*?)/"
ll = re.findall(regx_p, matchtext)
for w, content in ll:
if w == "p":
dd["versioninfo"]["vendorproductname"] = content
elif w == "v":
dd["versioninfo"]["version"] = content
elif w == "i":
dd["versioninfo"]["info"] = content
elif w == "h":
dd["versioninfo"]["hostname"] = content
elif w == "o":
dd["versioninfo"]["operatingsystem"] = content
elif w == "d":
dd["versioninfo"]["devicetype"] = content
elif w == "cpe:":
dd["versioninfo"]["cpename"] = content
probe["matches"].append(dd)
elif line.startswith("softmatch "):
matchtext = line[len("softmatch "):]
index = matchtext.index(" m")
m = matchtext[index + 2] # 获取m后边的字符
name = matchtext[:index]
matchtext = matchtext[len(name):].strip()
regx_start = 2
regx_end = matchtext.index(m, regx_start)
regx = matchtext[regx_start:regx_end]
regx_flag = ""
if regx_end + 1 < len(matchtext):
regx_flag = matchtext[regx_end + 1].strip()
dd = {
"pattern": regx,
"name": name,
"pattern_flag": regx_flag,
'versioninfo': {'cpename': "",
'devicetype': "",
'hostname': "",
'info': "",
'operatingsystem': "",
'vendorproductname': "",
'version': ""
}
}
matchtext = matchtext[regx_end:]
regx_p = "(\w|cpe:)/(.*?)/"
ll = re.findall(regx_p, matchtext)
for w, content in ll:
if w == "p":
dd["versioninfo"]["vendorproductname"] = content
elif w == "v":
dd["versioninfo"]["version"] = content
elif w == "i":
dd["versioninfo"]["info"] = content
elif w == "h":
dd["versioninfo"]["hostname"] = content
elif w == "o":
dd["versioninfo"]["operatingsystem"] = content
elif w == "d":
dd["versioninfo"]["devicetype"] = content
elif w == "cpe:":
dd["versioninfo"]["cpename"] = content
probe["softmatches"].append(dd)
elif line.startswith("ports "):
ports = line[len("ports "):].split(",")
probe["ports"] = ports
elif line.startswith("sslports "):
sslports = line[len("sslports "):].split(",")
probe["sslports"] = sslports
elif line.startswith("totalwaitms "):
totalwaitms = line[len("totalwaitms "):]
probe["totalwaitms"] = totalwaitms
elif line.startswith("tcpwrappedms "):
tcpwrappedms = line[len("tcpwrappedms "):]
probe["totalwaitms"] = tcpwrappedms
elif line.startswith("rarity "):
rarity = line[len("rarity "):]
probe["rarity"] = rarity
elif line.startswith("fallback "):
fallback = line[len("fallback "):]
probe["fallback"] = fallback
else:
print("[x] ", line)
# print(line)
if probe:
probes.append(probe)
return probes
if __name__ == '__main__':
mm = ParseNmap()
with open("nmap.json", "w") as f:
json.dump(mm, f, indent=4)
# print(len(mm))
# tongji = {
# "tcp": 0,
# "udp": 0
# }
# for i in mm:
# if i["protocol"] == "TCP":
# tongji["tcp"] += 1
# else:
# tongji["udp"] += 1
# print(tongji)