Skip to content

Commit be6e68b

Browse files
committed
add dhcp listener tutorial
1 parent 5101b00 commit be6e68b

File tree

3 files changed

+14
-11
lines changed

3 files changed

+14
-11
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ This is a repository of all the tutorials of [The Python Code](https://www.thepy
77
- [Getting Started With Scapy: Python Network Manipulation Tool](https://www.thepythoncode.com/article/getting-started-with-scapy)
88
- [Building an ARP Spoofer](https://www.thepythoncode.com/article/building-arp-spoofer-using-scapy). ([code](scapy/arp-spoofer))
99
- [Detecting ARP Spoof attacks](https://www.thepythoncode.com/article/detecting-arp-spoof-attacks-using-scapy). ([code](scapy/arp-spoof-detector))
10-
- DHCP Listener script. ([code](scapy/dhcp_listener))
10+
- [How to Make a DHCP Listener using Scapy in Python](https://www.thepythoncode.com/article/dhcp-listener-using-scapy-in-python). ([code](scapy/dhcp_listener))
1111
- [Fake Access Point Generator](https://www.thepythoncode.com/article/create-fake-access-points-scapy). ([code](scapy/fake-access-point))
1212
- [Forcing a device to disconnect using scapy in Python](https://www.thepythoncode.com/article/force-a-device-to-disconnect-scapy). ([code](scapy/network-kicker))
1313
- [Simple Network Scanner](https://www.thepythoncode.com/article/building-network-scanner-using-scapy). ([code](scapy/network-scanner))

scapy/dhcp_listener/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
# Listening for new Connected Devices in the Network using DHCP
1+
# [How to Make a DHCP Listener using Scapy in Python](https://www.thepythoncode.com/article/dhcp-listener-using-scapy-in-python)
22
to run this:
33
- `pip3 install -r requirements.txt`
44
-
55
```
6-
python3 dhcp_listener.py
6+
$ python3 dhcp_listener.py
77
```

scapy/dhcp_listener/dhcp_listener.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
from scapy.all import *
22
import time
33

4-
hosts = []
5-
Ether = 1
6-
74

85
def listen_dhcp():
96
# Make sure it is DHCP with the filter options
10-
k = sniff(prn=print_packet, filter='udp and (port 67 or port 68)')
7+
sniff(prn=print_packet, filter='udp and (port 67 or port 68)')
8+
119

1210
def print_packet(packet):
11+
# initialize these variables to None at first
1312
target_mac, requested_ip, hostname, vendor_id = [None] * 4
13+
# get the MAC address of the requester
1414
if packet.haslayer(Ether):
1515
target_mac = packet.getlayer(Ether).src
1616
# get the DHCP options
@@ -21,15 +21,18 @@ def print_packet(packet):
2121
except ValueError:
2222
continue
2323
if label == 'requested_addr':
24+
# get the requested IP
2425
requested_ip = value
2526
elif label == 'hostname':
27+
# get the hostname of the device
2628
hostname = value.decode()
2729
elif label == 'vendor_class_id':
30+
# get the vendor ID
2831
vendor_id = value.decode()
29-
if target_mac and vendor_id and hostname and requested_ip and target_mac not in hosts:
30-
hosts.append(target_mac)
31-
time_now = time.strftime("[%Y-%m-%d - %H:%M:%S] ")
32-
print("{}: {} - {} / {} requested {}".format(time_now, target_mac, hostname, vendor_id, requested_ip))
32+
if target_mac and vendor_id and hostname and requested_ip:
33+
# if all variables are not None, print the device details
34+
time_now = time.strftime("[%Y-%m-%d - %H:%M:%S]")
35+
print(f"{time_now} : {target_mac} - {hostname} / {vendor_id} requested {requested_ip}")
3336

3437

3538
if __name__ == "__main__":

0 commit comments

Comments
 (0)