Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitbe6e68b

Browse files
committed
add dhcp listener tutorial
1 parent5101b00 commitbe6e68b

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 Listenerscript. ([code](scapy/dhcp_listener))
10+
-[How to Make aDHCP Listenerusing 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
fromscapy.allimport*
22
importtime
33

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

85
deflisten_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
defprint_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
ifpacket.haslayer(Ether):
1515
target_mac=packet.getlayer(Ether).src
1616
# get the DHCP options
@@ -21,15 +21,18 @@ def print_packet(packet):
2121
exceptValueError:
2222
continue
2323
iflabel=='requested_addr':
24+
# get the requested IP
2425
requested_ip=value
2526
eliflabel=='hostname':
27+
# get the hostname of the device
2628
hostname=value.decode()
2729
eliflabel=='vendor_class_id':
30+
# get the vendor ID
2831
vendor_id=value.decode()
29-
iftarget_macandvendor_idandhostnameandrequested_ipandtarget_macnotinhosts:
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+
iftarget_macandvendor_idandhostnameandrequested_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)

[8]ページ先頭

©2009-2025 Movatter.jp