From 6ff0952d7501d456c277a51f55eb718bb26b8854 Mon Sep 17 00:00:00 2001 From: VakarisZ Date: Fri, 12 Apr 2019 16:15:03 +0300 Subject: [PATCH] Refactored get_interface_to_target(dst) to get IP by trying to connect instead of IP comparison by string. --- monkey/infection_monkey/exploit/tools.py | 26 ++++++++++++++---------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/monkey/infection_monkey/exploit/tools.py b/monkey/infection_monkey/exploit/tools.py index a7a137557..0b496f8be 100644 --- a/monkey/infection_monkey/exploit/tools.py +++ b/monkey/infection_monkey/exploit/tools.py @@ -7,7 +7,6 @@ import socket import struct import sys import urllib -from difflib import get_close_matches from impacket.dcerpc.v5 import transport, srvs from impacket.dcerpc.v5.dcom import wmi @@ -19,7 +18,6 @@ from impacket.smbconnection import SMBConnection, SMB_DIALECT import infection_monkey.config import infection_monkey.monkeyfs as monkeyfs -from infection_monkey.network import local_ips from infection_monkey.network.firewall import app as firewall from infection_monkey.network.info import get_free_tcp_port, get_routes from infection_monkey.transport import HTTPServer, LockedHTTPServer @@ -418,9 +416,15 @@ class HTTPTools(object): def get_interface_to_target(dst): if sys.platform == "win32": - ips = local_ips() - matches = get_close_matches(dst, ips) - return matches[0] if (len(matches) > 0) else ips[0] + s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + try: + s.connect((dst, 1)) + ip_to_dst = s.getsockname()[0] + except KeyError: + ip_to_dst = '127.0.0.1' + finally: + s.close() + return ip_to_dst else: # based on scapy implementation @@ -430,17 +434,17 @@ def get_interface_to_target(dst): routes = get_routes() dst = atol(dst) - pathes = [] + paths = [] for d, m, gw, i, a in routes: aa = atol(a) if aa == dst: - pathes.append((0xffffffff, ("lo", a, "0.0.0.0"))) + paths.append((0xffffffff, ("lo", a, "0.0.0.0"))) if (dst & m) == (d & m): - pathes.append((m, (i, a, gw))) - if not pathes: + paths.append((m, (i, a, gw))) + if not paths: return None - pathes.sort() - ret = pathes[-1][1] + paths.sort() + ret = paths[-1][1] return ret[1]