Skip to content

Commit 13b91b0

Browse files
masscan: support --top-ports
1 parent c7510d5 commit 13b91b0

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

bbot/modules/masscan.py

+20-5
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,22 @@ class masscan(BaseModule):
1111
produced_events = ["OPEN_TCP_PORT"]
1212
meta = {"description": "Port scan IP subnets with masscan"}
1313
# 600 packets/s ~= entire private IP space in 8 hours
14-
options = {"ports": "80,443", "rate": 600, "wait": 10, "ping_first": False, "use_cache": False}
14+
options = {
15+
"top_ports": 100,
16+
"ports": "",
17+
"rate": 600,
18+
"wait": 10,
19+
"ping_first": False,
20+
"ping_only": False,
21+
"use_cache": False,
22+
}
1523
options_desc = {
24+
"top_ports": "Top ports to scan (default 100)",
1625
"ports": "Ports to scan",
1726
"rate": "Rate in packets per second",
1827
"wait": "Seconds to wait for replies after scan is complete",
1928
"ping_first": "Only portscan hosts that reply to pings",
29+
"ping_only": "Ping sweep only, no portscan",
2030
"use_cache": "Instead of scanning, use the results from the previous scan",
2131
}
2232
deps_ansible = [
@@ -51,10 +61,12 @@ class masscan(BaseModule):
5161
_qsize = 100
5262

5363
async def setup(self):
64+
self.top_ports = self.config.get("top_ports", 100)
5465
self.ports = self.config.get("ports", "80,443")
5566
self.rate = self.config.get("rate", 600)
5667
self.wait = self.config.get("wait", 10)
5768
self.ping_first = self.config.get("ping_first", False)
69+
self.ping_only = self.config.get("ping_only", False)
5870
self.alive_hosts = dict()
5971
# make a quick dry run to validate ports etc.
6072
self._target_findkey = "9.8.7.6"
@@ -111,7 +123,7 @@ async def handle_event(self, event):
111123
return
112124

113125
# ping scan
114-
if self.ping_first:
126+
if self.ping_first or self.ping_only:
115127
self.verbose("Starting masscan (ping scan)")
116128

117129
await self.masscan(targets, result_callback=self.append_alive_host, exclude=exclude, ping=True)
@@ -121,11 +133,11 @@ async def handle_event(self, event):
121133
return
122134

123135
# TCP SYN scan
124-
if self.ports:
136+
if not self.ping_only:
125137
self.verbose("Starting masscan (TCP SYN scan)")
126138
await self.masscan(targets, result_callback=self.emit_open_tcp_port, exclude=exclude)
127139
else:
128-
self.verbose("No ports specified, skipping TCP SYN scan")
140+
self.verbose("Only ping sweep was requested, skipping TCP SYN scan")
129141
# save memory
130142
self.alive_hosts.clear()
131143

@@ -159,7 +171,10 @@ def _build_masscan_command(self, targets=None, config=None, exclude=None, dry_ru
159171
if ping:
160172
command += ("--ping",)
161173
elif not dry_run:
162-
command += ("-p", self.ports)
174+
if self.ports:
175+
command += ("-p", self.ports)
176+
else:
177+
command += ("--top-ports", str(self.top_ports))
163178
if exclude is not None:
164179
command += ("--exclude", exclude)
165180
if dry_run:

0 commit comments

Comments
 (0)