Skip to content

Commit 3af7564

Browse files
authored
Merge pull request #46 from MyKings/fix_masscan_result
fix: Format the result returned by `masscan -oJ`
2 parents 6daac79 + 60f86ee commit 3af7564

3 files changed

Lines changed: 32 additions & 104 deletions

File tree

README.rst

Lines changed: 17 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -56,98 +56,26 @@ Result
5656
.. code-block:: json
5757
5858
{
59-
"masscan": {
60-
"scanstats": {
61-
"uphosts": "6",
62-
"timestr": "2017-03-23 18:09:28",
63-
"downhosts": "0",
64-
"totalhosts": "6",
65-
"elapsed": "2"
59+
"command_line": "masscan -oJ - 192.168.1.1 -p 80,1900",
60+
"scan": {
61+
"192.168.1.1": [
62+
{
63+
"status": "open",
64+
"reason": "syn-ack",
65+
"ttl": 64,
66+
"port": 1900,
67+
"proto": "tcp"
6668
},
67-
"command_line": "masscan -oX - 172.0.8.78/24 -p 22,80,8080"
68-
},
69-
"scan": {
70-
"172.0.8.222": {
71-
"tcp": {
72-
"22": {
73-
"state": "open",
74-
"reason": "syn-ack",
75-
"reason_ttl": "64"
76-
},
77-
"80": {
78-
"state": "open",
79-
"reason": "syn-ack",
80-
"reason_ttl": "64"
81-
}
82-
}
83-
},
84-
"172.0.8.205": {
85-
"tcp": {
86-
"80": {
87-
"state": "open",
88-
"reason": "syn-ack",
89-
"reason_ttl": "128"
90-
}
91-
}
92-
},
93-
"172.0.8.207": {
94-
"tcp": {
95-
"8080": {
96-
"state": "open",
97-
"reason": "syn-ack",
98-
"reason_ttl": "128"
99-
}
100-
}
101-
},
102-
"172.0.8.206": {
103-
"tcp": {
104-
"8080": {
105-
"state": "open",
106-
"reason": "syn-ack",
107-
"reason_ttl": "128"
108-
}
109-
}
110-
},
111-
"172.0.8.203": {
112-
"tcp": {
113-
"22": {
114-
"state": "open",
115-
"reason": "syn-ack",
116-
"reason_ttl": "64"
117-
},
118-
"80": {
119-
"state": "open",
120-
"reason": "syn-ack",
121-
"reason_ttl": "64"
122-
},
123-
"8080": {
124-
"state": "open",
125-
"reason": "syn-ack",
126-
"reason_ttl": "64"
127-
}
128-
}
129-
},
130-
"172.0.8.202": {
131-
"tcp": {
132-
"22": {
133-
"state": "open",
134-
"reason": "syn-ack",
135-
"reason_ttl": "64"
136-
},
137-
"80": {
138-
"state": "open",
139-
"reason": "syn-ack",
140-
"reason_ttl": "64"
141-
},
142-
"8080": {
143-
"state": "open",
144-
"reason": "syn-ack",
145-
"reason_ttl": "64"
146-
}
147-
}
69+
{
70+
"status": "open",
71+
"reason": "syn-ack",
72+
"ttl": 64,
73+
"port": 80,
74+
"proto": "tcp"
14875
}
149-
}
76+
]
15077
}
78+
}
15179
15280
15381
Contributors

example.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@
1414
sys.exit(1)
1515

1616
print("masscan version: {}".format(mas.masscan_version))
17-
mas.scan('192.168.1.1', ports='80,1900')
17+
mas.scan('192.168.1.1', ports='T:80,1900')
1818
print("masscan command line: {}".format(mas.command_line))
1919
print('maascan has_host: {}'.format(mas.has_host("192.168.1.1")))
20-
20+
print(mas.scan_result)
2121
for host in mas.all_hosts:
2222
print("Host: %s %s" % (host, mas[host]))
2323

masscan/__init__.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ def __init__(self, masscan_search_path=(
8888
8989
"""
9090
self._masscan_path = '' # masscan path
91-
self._scan_result = {"command_line": {}, "scan": []}
91+
self._scan_result = {"command_line": {}, "scan": {}}
9292
self._masscan_version_number = 0 # masscan version number
9393
self._masscan_subversion_number = 0 # masscan subversion number
9494
self._masscan_revised_number = 0 # masscan revised number
@@ -163,9 +163,8 @@ def __getitem__(self, host):
163163
else:
164164
assert type(host) is str, 'Wrong type for [host], should be a string [was {0}]'.format(type(host))
165165

166-
for item in self._scan_result['scan']:
167-
if item["ip"] == host:
168-
return item
166+
if host in self._scan_result['scan']:
167+
return self._scan_result['scan'][host]
169168
return None
170169

171170
@property
@@ -193,9 +192,7 @@ def all_hosts(self):
193192
"""Return a sorted list of all hosts."""
194193
host_list = []
195194
if self._scan_result['scan']:
196-
for item in self._scan_result['scan']:
197-
if "ip" in item and item["ip"] not in host_list:
198-
host_list.append(item["ip"])
195+
host_list = self._scan_result['scan'].keys()
199196
return host_list
200197

201198
@property
@@ -215,7 +212,7 @@ def scan_result(self):
215212
216213
may raise AssertionError exception if called before scanning
217214
"""
218-
return self._scan_result
215+
return json.dumps(self._scan_result)
219216

220217
def scan(self, hosts='127.0.0.1', ports=PORTS, arguments='', sudo=False):
221218
"""
@@ -306,18 +303,21 @@ def scan(self, hosts='127.0.0.1', ports=PORTS, arguments='', sudo=False):
306303
# raise PortScannerError(masscan_err)
307304
masscan_err_keep_trace.append(masscan_err)
308305
try:
309-
self._scan_result["scan"] = json.loads(self._masscan_last_output)
306+
scan_result = json.loads(self._masscan_last_output)
307+
for item in scan_result:
308+
if item["ip"] not in self._scan_result["scan"]:
309+
self._scan_result["scan"][item["ip"]] = []
310+
self._scan_result["scan"][item["ip"]].extend(item["ports"])
311+
310312
except ValueError as ex:
311313
pass
312314

313315
return self._scan_result
314316

315317
def has_host(self, host):
316318
"""If host has result it returns True, False otherwise."""
317-
if self._scan_result['scan']:
318-
for item in self._scan_result['scan']:
319-
if "ip" in item and item["ip"] == host:
320-
return True
319+
if self._scan_result['scan'] and host in self._scan_result['scan']:
320+
return True
321321
return False
322322

323323

0 commit comments

Comments
 (0)