2018-02-07 01:56:25 +08:00
|
|
|
from itertools import izip_longest
|
2016-09-08 00:01:19 +08:00
|
|
|
from random import shuffle
|
2018-02-07 01:55:01 +08:00
|
|
|
|
|
|
|
from network import HostScanner, HostFinger
|
2018-08-08 22:57:34 +08:00
|
|
|
from network.tools import check_tcp_ports, tcp_port_to_service
|
2015-08-30 15:27:35 +08:00
|
|
|
|
|
|
|
__author__ = 'itamar'
|
|
|
|
|
2015-09-29 22:55:54 +08:00
|
|
|
BANNER_READ = 1024
|
|
|
|
|
2015-11-30 16:56:20 +08:00
|
|
|
|
2015-09-29 22:55:54 +08:00
|
|
|
class TcpScanner(HostScanner, HostFinger):
|
|
|
|
def __init__(self):
|
2015-08-30 15:27:35 +08:00
|
|
|
self._config = __import__('config').WormConfiguration
|
|
|
|
|
|
|
|
def is_host_alive(self, host):
|
2015-09-29 22:55:54 +08:00
|
|
|
return self.get_host_fingerprint(host, True)
|
|
|
|
|
|
|
|
def get_host_fingerprint(self, host, only_one_port=False):
|
2018-02-07 01:56:25 +08:00
|
|
|
"""
|
|
|
|
Scans a target host to see if it's alive using the tcp_target_ports specified in the configuration.
|
|
|
|
:param host: VictimHost structure
|
|
|
|
:param only_one_port: Currently unused.
|
|
|
|
:return: T/F if there is at least one open port. In addition, the host object is updated to mark those services as alive.
|
|
|
|
"""
|
2015-08-30 15:27:35 +08:00
|
|
|
|
2016-09-08 00:01:19 +08:00
|
|
|
# maybe hide under really bad detection systems
|
|
|
|
target_ports = self._config.tcp_target_ports[:]
|
|
|
|
shuffle(target_ports)
|
2015-09-29 22:55:54 +08:00
|
|
|
|
2018-02-07 02:13:27 +08:00
|
|
|
ports, banners = check_tcp_ports(host.ip_addr, target_ports, self._config.tcp_scan_timeout / 1000.0,
|
|
|
|
self._config.tcp_scan_get_banner)
|
2018-02-07 01:56:25 +08:00
|
|
|
for target_port, banner in izip_longest(ports, banners, fillvalue=None):
|
2018-08-08 22:57:34 +08:00
|
|
|
service = tcp_port_to_service(target_port)
|
2018-02-07 01:56:25 +08:00
|
|
|
host.services[service] = {}
|
|
|
|
if banner:
|
|
|
|
host.services[service]['banner'] = banner
|
|
|
|
if only_one_port:
|
|
|
|
break
|
|
|
|
|
|
|
|
return len(ports) != 0
|