def test_results_dir_absolute(tmp_path): targetfile = tmp_path / "test_targetlist" targetfile.write_text("stuff.com") tl = TargetList(target_file=str(targetfile), results_dir=str((tmp_path / "recon-results").resolve())) out = tl.output() assert out.path == str( (tmp_path / "recon-results" / "target-results" / "domains").resolve())
def test_results_dir_empty(tmp_path): targetfile = tmp_path / "test_targetlist" targetfile.write_text("stuff.com") tl = TargetList(target_file=str(targetfile), results_dir="") out = tl.output() # different asserts used here because an empty string to results_dir causes Path() to use "." i.e. cwd # when running tests, this conflicts with tmp_path, but is only a problem during testing assert str(Path(out.path).parent.stem) == "target-results" assert Path(out.path).stem == "domains"
def test_creates_ips(tmp_path): targetfile = tmp_path / "test_targetlist" targetfile.write_text("127.0.0.1") tl = TargetList(target_file=str(targetfile), results_dir=str(tmp_path / "recon-results")) out = tl.output() assert out.path == str((tmp_path / "recon-results" / "target-results" / "ip_addresses").resolve())
def requires(self): """ AmassScan depends on TargetList to run. TargetList expects target_file as a parameter. Returns: luigi.ExternalTask - TargetList """ args = { "target_file": self.target_file, "results_dir": self.results_dir } return TargetList(**args)
def run(self): """ Defines the options/arguments sent to masscan after processing. Returns: list: list of options/arguments, beginning with the name of the executable to run """ if self.ports and self.top_ports: # can't have both logging.error( "Only --ports or --top-ports is permitted, not both.") exit(1) if not self.ports and not self.top_ports: # need at least one logging.error("Must specify either --top-ports or --ports.") exit(2) if self.top_ports < 0: # sanity check logging.error("--top-ports must be greater than 0") exit(3) if self.top_ports: # if --top-ports used, format the top_*_ports lists as strings and then into a proper masscan --ports option top_tcp_ports_str = ",".join( str(x) for x in top_tcp_ports[:self.top_ports]) top_udp_ports_str = ",".join( str(x) for x in top_udp_ports[:self.top_ports]) self.ports = f"{top_tcp_ports_str},U:{top_udp_ports_str}" self.top_ports = 0 target_list = yield TargetList(target_file=self.target_file, results_dir=self.results_dir) Path(self.output().path).parent.mkdir(parents=True, exist_ok=True) Path(self.output().path).parent.mkdir(parents=True, exist_ok=True) if target_list.path.endswith("domains"): yield ParseAmassOutput(target_file=self.target_file, exempt_list=self.exempt_list, results_dir=self.results_dir) command = [ "masscan", "-v", "--open", "--banners", "--rate", self.rate, "-e", self.interface, "-oJ", self.output().path, "--ports", self.ports, "-iL", ] if target_list.path.endswith("domains"): command.append( target_list.path.replace("domains", "ipv4_addresses")) else: command.append(target_list.path.replace("domains", "ip_addresses")) subprocess.run(command)
def test_filenotfound(tmp_path): tl = TargetList(target_file="doesnt_exist", results_dir="") out = tl.output() assert out is None