Skip to content

Instantly share code, notes, and snippets.

@partytime
Last active January 27, 2021 07:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save partytime/f7e539c2ce62f1cdfec8667078813600 to your computer and use it in GitHub Desktop.
Save partytime/f7e539c2ce62f1cdfec8667078813600 to your computer and use it in GitHub Desktop.
rogue dhcp detector
#!/usr/bin/python
import sys
from os import geteuid
from scapy.all import *
#check for root
if geteuid() != 0:
print "This must be run as root"
exit(1)
# CONFIGURATION #
"""
Scapy normally makes sure that replies come from the same IP address the stimulus was sent to. But our DHCP packet is sent to the broadcast address (255.255.255.255) and any answer packet will have the IP address of the replying DHCP server as its source IP address (e.g. 192.168.1.1). Because these IP addresses don't match, we have to disable Scapy's check with conf.checkIPaddr = False before sending the stimulus.
"""
conf.checkIPaddr = False
fam,hw = get_if_raw_hwaddr(conf.iface)
# CRAFTING THE DHCP PACKET #
dhcp_discover_packets = Ether(dst="ff:ff:ff:ff:ff:ff")/IP(src="0.0.0.0",dst="255.255.255.255")/UDP(sport=68,dport=67)/BOOTP(chaddr=hw)/DHCP(options=[("message-type","discover"),"end"])
# SENDING THE PACKET #
ans, unans = srp(dhcp_discover_packets, multi=True, timeout=3)
# GET THE RESULTS #
replyingServers=[]
for result in ans:
print "\nGot a DHCP offer from", result[1][IP].src, "with MAC address", result[1][Ether].src
replyingServers.append(result[1][IP].src)
if len(replyingServers) > 1:
print ("!!!!!!!!!!!!\nWARNING: MORE THAN 1 DHCP SERVER REPLIED TO OUR PACKETS\n!!!!!!!!!!!!")
elif len(replyingServers) < 1:
print ("Didnt get a reply from anyone....is there a DHCP server on this subnet?")
else:
print ("\nGot one DHCP reply, looks good!")
@partytime
Copy link
Author

requires scapy

@solsticedhiver
Copy link

You have to choose between python2 and 3. So for print it is either the function print() or the statement print without (). Do not mix the two.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment