Skip to content

Commit 0387d82

Browse files
authored
Merge pull request #5 from flashnuke/test/gateway_mac
feat / additional ways to get gateway ip/mac
2 parents e2a2c28 + 8964bf0 commit 0387d82

2 files changed

Lines changed: 32 additions & 4 deletions

File tree

deadnet.py

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import ipaddress
44
import logging
5+
import netifaces
56
logging.getLogger("scapy.runtime").setLevel(logging.ERROR) # suppress warnings
67

78
from 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

166193
if __name__ == "__main__":

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
scapy
2+
netifaces

0 commit comments

Comments
 (0)