Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion masscan/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import subprocess
import sys
import json
import random
import string

from multiprocessing import Process

Expand Down Expand Up @@ -214,6 +216,28 @@ def scan_result(self):
"""
return json.dumps(self._scan_result)

def copy_hosts_to_file(self, h_args):
"""
Copy hosts to file, due to argv size limitations

:returns: temp_file as str
"""
if sys.platform.startswith('win32'):
tmp_location = '%temp%\\'
else:
tmp_location = '/tmp/'

RANDOM_STRING_LENGHT = 32
temp_file_name = "".join(random.choice(string.ascii_letters) for i in range(RANDOM_STRING_LENGHT))
temp_file = tmp_location + temp_file_name

# Put all the hosts in the file
with open(temp_file, 'w') as f:
f.writelines(s + '\n' for s in h_args)

return temp_file


def scan(self, hosts='127.0.0.1', ports=PORTS, arguments='', sudo=False):
"""
Scan given hosts.
Expand Down Expand Up @@ -251,7 +275,7 @@ def scan(self, hosts='127.0.0.1', ports=PORTS, arguments='', sudo=False):
f_args = shlex.split(arguments)

# Launch scan
args = [self._masscan_path, '-oJ', '-'] + h_args + ['-p', ports] * (ports is not None) + f_args
args = [self._masscan_path, '-oJ', '-'] + ['-iL', self.copy_hosts_to_file(h_args)] + ['-p', ports] * (ports is not None) + f_args

self._args = ' '.join(args)

Expand Down