Advertisement
Mihao

dhcp.py

May 21st, 2022
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.95 KB | None | 0 0
  1. from scapy.all import *
  2. import time
  3.  
  4.  
  5. def listen_dhcp():
  6.  
  7. sniff(prn=print_packet, filter='udp and (port 67 or port 68)')
  8.  
  9. def print_packet(packet):
  10. target_mac, requested_ip, hostname, vendor_id = [None] * 4
  11. if packet.haslayer(Ether):
  12. target_mac = packet.getlayer(Ether).src
  13. dhcp_options = packet[DHCP].options
  14. for item in dhcp_options:
  15. try:
  16. label, value = item
  17. except ValueError:
  18. continue
  19. if label == 'requested_addr':
  20. requested_ip = value
  21. elif label == 'hostname':
  22. hostname = value.decode()
  23. elif label == 'vendor_class_id':
  24. vendor_id = value.decode()
  25. if target_mac and vendor_id and hostname and requested_ip:
  26. time_now = time.strftime("[%Y-%m-%d - %H:%M:%S]")
  27. print(f"{time_now} : {target_mac} - {hostname} / {vendor_id} requested {requested_ip}")
  28.  
  29. if __name__ == "__main__":
  30. listen_dhcp()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement