2015-10-08 18:39:52 +08:00
|
|
|
import socket
|
|
|
|
import struct
|
|
|
|
import logging
|
|
|
|
from threading import Thread
|
|
|
|
from network.info import local_ips, get_free_tcp_port
|
|
|
|
from network.firewall import app as firewall
|
2016-08-13 23:38:31 +08:00
|
|
|
from transport.base import get_last_serve_time
|
2015-10-08 18:39:52 +08:00
|
|
|
from difflib import get_close_matches
|
|
|
|
from network.tools import check_port_tcp
|
2015-10-14 22:22:05 +08:00
|
|
|
from model import VictimHost
|
2015-10-08 18:39:52 +08:00
|
|
|
import time
|
|
|
|
|
|
|
|
__author__ = 'hoffer'
|
|
|
|
|
|
|
|
LOG = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
MCAST_GROUP = '224.1.1.1'
|
|
|
|
MCAST_PORT = 5007
|
|
|
|
BUFFER_READ = 1024
|
|
|
|
DEFAULT_TIMEOUT = 10
|
2016-01-14 22:14:07 +08:00
|
|
|
QUIT_TIMEOUT = 60 * 10 # 10 minutes
|
2015-11-30 16:56:20 +08:00
|
|
|
|
2015-10-08 18:39:52 +08:00
|
|
|
|
2016-05-27 00:52:15 +08:00
|
|
|
def _set_multicast_socket(timeout=DEFAULT_TIMEOUT, adapter=''):
|
2015-10-08 18:39:52 +08:00
|
|
|
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
|
|
|
|
sock.settimeout(timeout)
|
|
|
|
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
2016-05-27 00:52:15 +08:00
|
|
|
sock.bind((adapter, MCAST_PORT))
|
2015-11-30 16:56:20 +08:00
|
|
|
sock.setsockopt(socket.IPPROTO_IP,
|
|
|
|
socket.IP_ADD_MEMBERSHIP,
|
|
|
|
struct.pack("4sl", socket.inet_aton(MCAST_GROUP), socket.INADDR_ANY))
|
2015-10-08 18:39:52 +08:00
|
|
|
return sock
|
|
|
|
|
|
|
|
|
2016-08-13 23:38:31 +08:00
|
|
|
def _check_tunnel(address, port, existing_sock=None):
|
|
|
|
if not existing_sock:
|
|
|
|
sock = _set_multicast_socket()
|
|
|
|
else:
|
|
|
|
sock = existing_sock
|
|
|
|
|
|
|
|
LOG.debug("Checking tunnel %s:%s", address, port)
|
|
|
|
is_open, _ = check_port_tcp(address, int(port))
|
|
|
|
if not is_open:
|
|
|
|
LOG.debug("Could not connect to %s:%s", address, port)
|
|
|
|
if not existing_sock:
|
|
|
|
sock.close()
|
|
|
|
return False
|
|
|
|
|
|
|
|
try:
|
|
|
|
sock.sendto("+", (address, MCAST_PORT))
|
|
|
|
except Exception, exc:
|
|
|
|
LOG.debug("Caught exception in tunnel registration: %s", exc)
|
|
|
|
|
|
|
|
if not existing_sock:
|
|
|
|
sock.close()
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
2015-10-14 22:22:05 +08:00
|
|
|
def find_tunnel(default=None, attempts=3, timeout=DEFAULT_TIMEOUT):
|
2015-10-08 18:39:52 +08:00
|
|
|
l_ips = local_ips()
|
|
|
|
|
2016-08-13 23:38:31 +08:00
|
|
|
if default:
|
|
|
|
if default.find(':') != -1:
|
|
|
|
address, port = default.split(':', 1)
|
|
|
|
if _check_tunnel(address, port):
|
|
|
|
return address, port
|
|
|
|
|
2016-05-27 00:52:15 +08:00
|
|
|
for adapter in l_ips:
|
|
|
|
for attempt in range(0, attempts):
|
|
|
|
try:
|
|
|
|
LOG.info("Trying to find using adapter %s", adapter)
|
|
|
|
sock = _set_multicast_socket(timeout, adapter)
|
|
|
|
sock.sendto("?", (MCAST_GROUP, MCAST_PORT))
|
|
|
|
tunnels = []
|
|
|
|
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
answer, address = sock.recvfrom(BUFFER_READ)
|
|
|
|
if answer not in ['?', '+', '-']:
|
|
|
|
tunnels.append(answer)
|
|
|
|
except socket.timeout:
|
|
|
|
break
|
2015-10-12 19:56:44 +08:00
|
|
|
|
2016-05-27 00:52:15 +08:00
|
|
|
for tunnel in tunnels:
|
|
|
|
if tunnel.find(':') != -1:
|
|
|
|
address, port = tunnel.split(':', 1)
|
|
|
|
if address in l_ips:
|
|
|
|
continue
|
2015-10-14 22:22:05 +08:00
|
|
|
|
2016-08-13 23:38:31 +08:00
|
|
|
if _check_tunnel(address, port, sock):
|
|
|
|
sock.close()
|
|
|
|
return address, port
|
2015-10-12 19:56:44 +08:00
|
|
|
|
2016-05-27 00:52:15 +08:00
|
|
|
except Exception, exc:
|
|
|
|
LOG.debug("Caught exception in tunnel lookup: %s", exc)
|
|
|
|
continue
|
2015-10-08 18:39:52 +08:00
|
|
|
|
|
|
|
return None
|
|
|
|
|
2015-11-30 16:56:20 +08:00
|
|
|
|
2015-10-08 18:39:52 +08:00
|
|
|
def quit_tunnel(address, timeout=DEFAULT_TIMEOUT):
|
|
|
|
try:
|
|
|
|
sock = _set_multicast_socket(timeout)
|
|
|
|
sock.sendto("-", (address, MCAST_PORT))
|
|
|
|
sock.close()
|
|
|
|
LOG.debug("Success quitting tunnel")
|
|
|
|
except Exception, exc:
|
|
|
|
LOG.debug("Exception quitting tunnel: %s", exc)
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
|
|
class MonkeyTunnel(Thread):
|
|
|
|
def __init__(self, proxy_class, target_addr=None, target_port=None, timeout=DEFAULT_TIMEOUT):
|
|
|
|
self._target_addr = target_addr
|
|
|
|
self._target_port = target_port
|
|
|
|
self._proxy_class = proxy_class
|
|
|
|
self._broad_sock = None
|
|
|
|
self._timeout = timeout
|
|
|
|
self._stopped = False
|
|
|
|
self._clients = []
|
2015-10-14 22:22:05 +08:00
|
|
|
self.local_port = None
|
2015-10-08 18:39:52 +08:00
|
|
|
super(MonkeyTunnel, self).__init__()
|
2015-10-12 19:56:44 +08:00
|
|
|
self.daemon = True
|
2015-11-30 16:56:20 +08:00
|
|
|
self.l_ips = None
|
2015-10-08 18:39:52 +08:00
|
|
|
|
|
|
|
def run(self):
|
|
|
|
self._broad_sock = _set_multicast_socket(self._timeout)
|
2015-11-26 22:30:49 +08:00
|
|
|
self.l_ips = local_ips()
|
2015-10-14 22:22:05 +08:00
|
|
|
self.local_port = get_free_tcp_port()
|
2015-10-08 18:39:52 +08:00
|
|
|
|
2015-10-14 22:22:05 +08:00
|
|
|
if not self.local_port:
|
2015-10-08 18:39:52 +08:00
|
|
|
return
|
|
|
|
|
2015-10-14 22:22:05 +08:00
|
|
|
if not firewall.listen_allowed(localport=self.local_port):
|
2015-10-08 18:39:52 +08:00
|
|
|
LOG.info("Machine firewalled, listen not allowed, not running tunnel.")
|
|
|
|
return
|
|
|
|
|
2015-10-14 22:22:05 +08:00
|
|
|
proxy = self._proxy_class(local_port=self.local_port, dest_host=self._target_addr, dest_port=self._target_port)
|
2015-12-02 17:18:27 +08:00
|
|
|
LOG.info("Running tunnel using proxy class: %s, listening on port %s, routing to: %s:%s",
|
|
|
|
proxy.__class__.__name__,
|
|
|
|
self.local_port,
|
|
|
|
self._target_addr,
|
|
|
|
self._target_port)
|
2015-10-08 18:39:52 +08:00
|
|
|
proxy.start()
|
|
|
|
|
|
|
|
while not self._stopped:
|
|
|
|
try:
|
|
|
|
search, address = self._broad_sock.recvfrom(BUFFER_READ)
|
|
|
|
if '?' == search:
|
2015-11-26 22:30:49 +08:00
|
|
|
ip_match = get_close_matches(address[0], self.l_ips) or self.l_ips
|
2015-10-08 18:39:52 +08:00
|
|
|
if ip_match:
|
2015-10-14 22:22:05 +08:00
|
|
|
answer = '%s:%d' % (ip_match[0], self.local_port)
|
2015-10-08 18:39:52 +08:00
|
|
|
LOG.debug("Got tunnel request from %s, answering with %s", address[0], answer)
|
2015-10-14 22:22:05 +08:00
|
|
|
self._broad_sock.sendto(answer, (address[0], MCAST_PORT))
|
2015-10-08 18:39:52 +08:00
|
|
|
elif '+' == search:
|
|
|
|
if not address[0] in self._clients:
|
2016-08-13 23:38:31 +08:00
|
|
|
LOG.debug("Tunnel control: Added %s to watchlist", address[0])
|
2015-10-08 18:39:52 +08:00
|
|
|
self._clients.append(address[0])
|
|
|
|
elif '-' == search:
|
2016-08-13 23:38:31 +08:00
|
|
|
LOG.debug("Tunnel control: Removed %s from watchlist", address[0])
|
2015-10-08 18:39:52 +08:00
|
|
|
self._clients = [client for client in self._clients if client != address[0]]
|
|
|
|
|
|
|
|
except socket.timeout:
|
|
|
|
continue
|
|
|
|
|
2016-08-13 23:38:31 +08:00
|
|
|
LOG.info("Stopping tunnel, waiting for clients: %s" % repr(self._clients))
|
|
|
|
|
|
|
|
# wait till all of the tunnel clients has been disconnected, or no one used the tunnel in QUIT_TIMEOUT seconds
|
|
|
|
while self._clients and (time.time() - get_last_serve_time() < QUIT_TIMEOUT):
|
2015-10-08 18:39:52 +08:00
|
|
|
try:
|
|
|
|
search, address = self._broad_sock.recvfrom(BUFFER_READ)
|
|
|
|
if '-' == search:
|
2016-08-13 23:38:31 +08:00
|
|
|
LOG.debug("Tunnel control: Removed %s from watchlist", address[0])
|
2015-10-08 18:39:52 +08:00
|
|
|
self._clients = [client for client in self._clients if client != address[0]]
|
|
|
|
except socket.timeout:
|
|
|
|
continue
|
2016-08-13 23:38:31 +08:00
|
|
|
|
2015-10-08 18:39:52 +08:00
|
|
|
LOG.info("Closing tunnel")
|
|
|
|
self._broad_sock.close()
|
|
|
|
proxy.stop()
|
|
|
|
proxy.join()
|
|
|
|
|
2015-10-14 22:22:05 +08:00
|
|
|
def set_tunnel_for_host(self, host):
|
|
|
|
assert isinstance(host, VictimHost)
|
2016-07-21 16:45:29 +08:00
|
|
|
|
|
|
|
if not self.local_port:
|
|
|
|
return
|
|
|
|
|
2015-11-26 22:30:49 +08:00
|
|
|
ip_match = get_close_matches(host.ip_addr, local_ips()) or self.l_ips
|
2015-10-14 22:22:05 +08:00
|
|
|
host.default_tunnel = '%s:%d' % (ip_match[0], self.local_port)
|
|
|
|
|
2015-10-08 18:39:52 +08:00
|
|
|
def stop(self):
|
2016-08-13 23:38:31 +08:00
|
|
|
self._stopped = True
|