Skip to content

Commit

Permalink
You can see a live output from masscan now
Browse files Browse the repository at this point in the history
  • Loading branch information
nullt3r committed Apr 10, 2022
1 parent 05ef13f commit 424458a
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 19 deletions.
2 changes: 1 addition & 1 deletion jfscan/__version__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
VERSION = (1, 1, 9)
VERSION = (1, 2, 0)

__version__ = '.'.join(map(str, VERSION))
9 changes: 7 additions & 2 deletions jfscan/core/modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,11 @@ def scan_masscan(self, resources, ports, max_rate=30000, top_ports = None, inter
logger = self.logger
utils = self.utils

if logging.INFO >= logging.root.level:
stream_output = True
else:
stream_output = False

logger.info("port scanning started")

ips = resources.get_ips()
Expand All @@ -158,7 +163,6 @@ def scan_masscan(self, resources, ports, max_rate=30000, top_ports = None, inter
"no resources were given, nothing to scan"
)
raise SystemExit


masscan_input = f"/tmp/_jfscan_{utils.random_string()}"
masscan_output = f"/tmp/_jfscan_{utils.random_string()}"
Expand All @@ -173,7 +177,8 @@ def scan_masscan(self, resources, ports, max_rate=30000, top_ports = None, inter
f.write(f"{cidr}\n")

result = utils.handle_command(
f"masscan {'--interface ' + interface if interface is not None else ''} {'--router-ip ' + router_ip if router_ip is not None else ''} --open {'--ports ' + ports if top_ports is None else '--top-ports ' + str(top_ports)} --max-rate {max_rate} -iL {masscan_input} -oJ {masscan_output}"
f"masscan {'--interface ' + interface if interface is not None else ''} {'--router-ip ' + router_ip if router_ip is not None else ''} --open {'--ports ' + ports if top_ports is None else '--top-ports ' + str(top_ports)} --max-rate {max_rate} -iL {masscan_input} -oJ {masscan_output}",
stream_output
)

if "FAIL: could not determine default interface" in result.stderr.decode('utf-8'):
Expand Down
58 changes: 42 additions & 16 deletions jfscan/core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import socket
import random
import string
import selectors


class Utils:
Expand Down Expand Up @@ -45,27 +46,52 @@ def check_dependency(self, bin, version_flag = None, version_string = None):
raise SystemExit


def handle_command(self, cmd):
def handle_command(self, cmd, stream_output = False):
logger = self.logger

result = None

logger.debug("running command %s", cmd)

result = subprocess.run(
cmd,
capture_output=True,
shell=True,
check=False,
)

if result.returncode != 0:
logger.error(
"there was an exception while running command:\n %s",
cmd
if stream_output == False:
process = subprocess.run(
cmd,
capture_output=True,
shell=True,
check=False,
)

return result
if process.returncode != 0:
logger.error(
"there was an exception while running command:\n %s",
cmd
)

return process

_stdout = b''
_stderr = b''

process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

sel = selectors.DefaultSelector()
sel.register(process.stdout, selectors.EVENT_READ)
sel.register(process.stderr, selectors.EVENT_READ)

while True:
for key, _ in sel.select():
data = key.fileobj.read1()
if not data:
returncode = process.poll()
if returncode != 0:
logger.error(
"there was an exception while running command:\n %s",
cmd
)
return subprocess.CompletedProcess(process.args, process.returncode, _stdout, _stderr)
if key.fileobj is process.stdout:
print(data.decode(), end="")
_stdout += data
else:
print(data.decode(), end="", file=sys.stderr)
_stderr += data

def resolve_host(self, host):
logger = self.logger
Expand Down

0 comments on commit 424458a

Please sign in to comment.