forked from p34709852/monkey
Merge pull request #297 from VakarisZ/get_interface_to_target_fix
Refactored get_interface_to_target fix
This commit is contained in:
commit
4cc19bb42b
|
@ -7,7 +7,6 @@ import socket
|
||||||
import struct
|
import struct
|
||||||
import sys
|
import sys
|
||||||
import urllib
|
import urllib
|
||||||
from difflib import get_close_matches
|
|
||||||
|
|
||||||
from impacket.dcerpc.v5 import transport, srvs
|
from impacket.dcerpc.v5 import transport, srvs
|
||||||
from impacket.dcerpc.v5.dcom import wmi
|
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.config
|
||||||
import infection_monkey.monkeyfs as monkeyfs
|
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.firewall import app as firewall
|
||||||
from infection_monkey.network.info import get_free_tcp_port, get_routes
|
from infection_monkey.network.info import get_free_tcp_port, get_routes
|
||||||
from infection_monkey.transport import HTTPServer, LockedHTTPServer
|
from infection_monkey.transport import HTTPServer, LockedHTTPServer
|
||||||
|
@ -418,9 +416,15 @@ class HTTPTools(object):
|
||||||
|
|
||||||
def get_interface_to_target(dst):
|
def get_interface_to_target(dst):
|
||||||
if sys.platform == "win32":
|
if sys.platform == "win32":
|
||||||
ips = local_ips()
|
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||||
matches = get_close_matches(dst, ips)
|
try:
|
||||||
return matches[0] if (len(matches) > 0) else ips[0]
|
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:
|
else:
|
||||||
# based on scapy implementation
|
# based on scapy implementation
|
||||||
|
|
||||||
|
@ -430,17 +434,17 @@ def get_interface_to_target(dst):
|
||||||
|
|
||||||
routes = get_routes()
|
routes = get_routes()
|
||||||
dst = atol(dst)
|
dst = atol(dst)
|
||||||
pathes = []
|
paths = []
|
||||||
for d, m, gw, i, a in routes:
|
for d, m, gw, i, a in routes:
|
||||||
aa = atol(a)
|
aa = atol(a)
|
||||||
if aa == dst:
|
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):
|
if (dst & m) == (d & m):
|
||||||
pathes.append((m, (i, a, gw)))
|
paths.append((m, (i, a, gw)))
|
||||||
if not pathes:
|
if not paths:
|
||||||
return None
|
return None
|
||||||
pathes.sort()
|
paths.sort()
|
||||||
ret = pathes[-1][1]
|
ret = paths[-1][1]
|
||||||
return ret[1]
|
return ret[1]
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue