22
33import ipaddress
44import logging
5+ import netifaces
56logging .getLogger ("scapy.runtime" ).setLevel (logging .ERROR ) # suppress warnings
67
78from scapy .all import *
@@ -37,7 +38,10 @@ def __init__(self, iface, cidrlen, s_time, gateway, disable_ipv6, ipv6_preflen):
3738 self .subnet_ipv4_sr = f"{ '.' .join (self .subnet_ipv4 )} .0/{ self .cidrlen_ipv4 } "
3839
3940 self .gateway_ipv4 = gateway or self .get_gateway_ipv4 (self .network_interface )
40- self .gateway_mac = getmacbyip (self .gateway_ipv4 )
41+ if not self .gateway_ipv4 :
42+ raise Exception (f"{ RED } [!]{ WHITE } Unable to automatically set IPv4 gateway address, try setting manually"
43+ f" by passing (-g, --set-gateway)..." )
44+ self .gateway_mac = self .get_gateway_mac ()
4145 if not self .gateway_mac :
4246 raise Exception (f"{ RED } [-]{ WHITE } Unable to get gateway mac -> { self .gateway_ipv4 } " )
4347 self .gateway_ipv6 = mac2ipv6_ll (self .gateway_mac , IPV6_LL_PREF )
@@ -58,6 +62,23 @@ def __init__(self, iface, cidrlen, s_time, gateway, disable_ipv6, ipv6_preflen):
5862 printf (f"{ RED } [-]{ WHITE } IPv6 RA spoof is disabled, skipping ping6..." )
5963 self .abort = False
6064
65+ def get_gateway_mac (self ):
66+ gateway_hwaddr = getmacbyip (self .gateway_ipv4 ) # fetch MAC using ARP req
67+ if not gateway_hwaddr :
68+ try :
69+ result = subprocess .run (['ip' , 'neighbor' , 'show' , 'default' ], capture_output = True , text = True )
70+ output = result .stdout .strip ()
71+
72+ for line in output .split ('\n ' ):
73+ columns = line .split ()
74+ if len (columns ) >= 4 :
75+ if columns [3 ] == 'lladdr' and columns [4 ] != '<incomplete>' and columns [2 ] == iface :
76+ gateway_hwaddr = columns [4 ]
77+ break
78+ except Exception as exc :
79+ pass
80+ return gateway_hwaddr
81+
6182 def user_abort (self ):
6283 printf (DELIM )
6384 printf (f"{ RED } [-]{ WHITE } User requested to stop..." )
@@ -155,12 +176,18 @@ def start_attack(self):
155176
156177 @staticmethod
157178 def get_gateway_ipv4 (iface ):
179+ try :
180+ gateways = netifaces .gateways ()
181+ ipv4_gateways = gateways [netifaces .AF_INET ] # ipv4 gateways
182+ for ipv4_data in ipv4_gateways :
183+ if ipv4_data [1 ] == iface :
184+ return ipv4_data [0 ]
185+ except Exception :
186+ pass # try scapy instead
158187 try :
159188 return [r [2 ] for r in conf .route .routes if r [3 ] == iface and r [2 ] != '0.0.0.0' ][0 ]
160189 except Exception :
161- printf (f"{ RED } [!]{ WHITE } Unable to automatically set IPv4 gateway address, try setting manually"
162- f" by passing (-g, --set-gateway)..." )
163- exit ()
190+ pass
164191
165192
166193if __name__ == "__main__" :
0 commit comments