2018-01-19 16:05:48 +08:00
|
|
|
import time
|
2016-09-08 00:01:19 +08:00
|
|
|
from random import shuffle
|
2017-11-12 02:32:12 +08:00
|
|
|
from network import HostScanner, HostFinger
|
2018-01-19 16:05:48 +08:00
|
|
|
from model.host import VictimHost
|
|
|
|
from network.tools import check_port_tcp
|
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-01-19 16:05:48 +08:00
|
|
|
assert isinstance(host, VictimHost)
|
2015-08-30 15:27:35 +08:00
|
|
|
|
2018-01-19 16:05:48 +08:00
|
|
|
count = 0
|
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-01-19 16:05:48 +08:00
|
|
|
for target_port in target_ports:
|
|
|
|
|
|
|
|
is_open, banner = check_port_tcp(host.ip_addr,
|
|
|
|
target_port,
|
|
|
|
self._config.tcp_scan_timeout / 1000.0,
|
|
|
|
self._config.tcp_scan_get_banner)
|
|
|
|
|
|
|
|
if is_open:
|
|
|
|
count += 1
|
|
|
|
service = 'tcp-' + str(target_port)
|
|
|
|
host.services[service] = {}
|
|
|
|
if banner:
|
|
|
|
host.services[service]['banner'] = banner
|
|
|
|
if only_one_port:
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
time.sleep(self._config.tcp_scan_interval / 1000.0)
|
|
|
|
|
|
|
|
return count != 0
|