def main(): parser = argparse.ArgumentParser( prog='', description= 'Консольное приложение, принимающее на вход диапазон ip-адресов ' '(например 192.168.1.0/24)' ' и список портов (например 80, 443, 22, 21, 25).' ' Результатом - список открытых портов с указанием удаленного хоста.', usage='%(prog)s [options]') parser.add_argument('ip_range', type=str, help='диапазон ip-адресов(например 192.168.1.0/24)') parser.add_argument('ports', type=str, help="список портов (например 80, 443, 22, 21, 25).") parser.add_argument('--log_file', type=str, default='hosts.log', help="Файл для сохранения результата." "По умолчанию hosts.log") args = parser.parse_args() args_dict = vars(args) try: ports = list(map(int, args_dict['ports'].split(','))) except ValueError: sys.exit('Ошибка ввода портов, пример ввода - 80, 443') args_dict.update({'ports': ports}) multiprocessing.freeze_support() scanner = PortScanner(**args_dict) scanner.start() scanner.join()
def test_lookup(): assertion = '127.0.0.1' ps = PortScanner('example.com', 1, 1024, True) test_pass = False if ps.lookup() == assertion: test_pass = True assert test_pass
def main(destination, timeout, start, end, t_protocol): scanner = PortScanner(destination, timeout) pool = multiprocessing.Pool(4) scan = pool.imap(scanner.scan_tcp, range(start, end + 1)) if t_protocol == 'tcp' \ else pool.imap(scanner.scan_udp, range(start, end + 1)) for port, protocol in scan: if protocol: print( f'{port} port is open by protocol "{protocol}". Transport protocol: {t_protocol}' )
def test_init_queue_y_make_report(): test_pass = True ps = PortScanner('127.0.0.1', 1, 1024, False) ps.init_queue() res = ps.make_report() if not len(res) == 0: test_pass = False assert test_pass
def test_parse_ipmask(): test_pass = True ps = PortScanner('127.0.0.1', 1, 1024, False) ips = [ '192.168.1.1/24', '192.168.1.129/31', '10.9.122.167/16', '5.0.5.0/8' ] assertions = ['192.168.1.0', '192.168.1.128', '10.9.0.0', '5.0.0.0'] for i, j in zip(ips, assertions): if not ps.parse_ipmask(i)[0] == j: test_pass = False assert test_pass
def __init__(self, executor, vmManager): # games is a dictionary that maps the gameName to Game object. self.games = {} # executor is a concurrent.futures.Executor class that allows us to # run stuff. self.executor = executor # vmManager is the VMManager instance for managing Virtual Machines. self.vmManager = vmManager # scanner is a port scanner. self.scanner = PortScanner() # Create a scoreboard and database instance. self.database = Database() self.scoreboard = ScoreBoard(self.database)
def angieps(host, initial_port, end_port, verbose, moles, dn, sweep): dn2 = False if not sweep and dn == 1: dn2 = True ps = PortScanner(host, initial_port, end_port, dn=dn2) if verbose == 1: ps.set_verbose(True) else: ps.set_verbose(False) ps.set_nmoles(moles) if not sweep: ps.begin_scan() else: print("Sweep Started...") ps.begin_sweep()
import sys from port_scanner import PortScanner from port_scanner.args_validators import TargetArgumentParser def convert_args_to_unicode_if_needed(): """ In Python 2.7, sys.argv are str and not unicode, ipaddress package only works on unicode, hence we need to decode given arguments """ if sys.version_info.major == 2: sys.argv = map(lambda arg: arg.decode(sys.stdout.encoding), sys.argv) if __name__ == "__main__": convert_args_to_unicode_if_needed() parser = TargetArgumentParser() args = parser.parse_args() ps = PortScanner(**vars(args)) ps()
from port_scanner import PortScanner, Target import argparse import sys port_scanner = PortScanner() parser = argparse.ArgumentParser(description='Port scanner') parser.add_argument('ip', type=str) parser.add_argument('--list-scan-techniques', action='store_true') #parser.add_argument('-p', '--port', dest='ports', nargs='+', default=[], help='Port list separated by spaces') parser.add_argument('ports', nargs='+', type=int) args = parser.parse_args() if args.list_scan_techniques: for s in port_scanner.scan_techniques: s = s(None) print(str.format("{}: {}", s.name, s.description)) sys.exit() scan_technique = port_scanner.scan_techniques['TCP Full Connect'] port_scanner.enqueue_scan(Target(args.ip, args.ports), scan_technique) port_scanner.process_scan_queue() print(port_scanner.scan_queue[0].results) print(args)
def test_no_target(): with pytest.raises(ValueError) as e: PortScanner(targets=[]) assert re.match(r"^.+ instance needs at least one target$", str(e.value))
def test_fast_option(): targets_file = "dummy-file.txt" ps1 = PortScanner(targets=[], targets_file=targets_file, fast=False) ps2 = PortScanner(targets=[], targets_file=targets_file, fast=True) assert ps1.command != ps2.command
def test_file_targets(): targets_file = "dummy-file.txt" ps = PortScanner(targets=[], targets_file=targets_file) assert ps.command[-1] == targets_file assert ps.command[-2] == PortScanner._file_option
def test_several_targets(): targets = ["test.hostname-02.com", "192.0.2.1/24"] ps = PortScanner(targets=targets) assert ps.command[-2:] == targets
def scan_ports(self): scanner = PortScanner(self.ip_address) print("Scan ports")