Actualiser DHCP2.py
This commit is contained in:
@@ -1,121 +1,52 @@
|
|||||||
#!/usr/bin/env python3
|
from scapy.all import Ether, IP, UDP, BOOTP, DHCP, sendp, sniff, RandMAC
|
||||||
"""scapy-dhcp-listener.py
|
import random
|
||||||
Listen for DHCP packets using scapy to learn when LAN
|
|
||||||
hosts request IP addresses from DHCP Servers.
|
|
||||||
Copyright (C) 2018 Jonathan Cutrer
|
|
||||||
License Dual MIT, 0BSD
|
|
||||||
"""
|
|
||||||
|
|
||||||
from __future__ import print_function
|
# Configuration des paramètres DHCP
|
||||||
from scapy.all import *
|
DHCP_SERVER_IP = "192.168.1.10" # L'adresse IP du serveur DHCP
|
||||||
import time
|
DHCP_POOL_START = "192.168.1.100" # Début de la plage d'adresses
|
||||||
|
DHCP_POOL_END = "192.168.1.200" # Fin de la plage d'adresses
|
||||||
|
SUBNET_MASK = "255.255.255.0"
|
||||||
|
ROUTER_IP = "192.168.1.2" # Adresse du routeur
|
||||||
|
DNS_IP = "192.168.1.2" # Adresse du serveur DNS
|
||||||
|
LEASE_TIME = 86400 # Durée de location en secondes
|
||||||
|
|
||||||
__version__ = "0.0.3"
|
allocated_ips = set() # Pour suivre les adresses IP attribuées
|
||||||
|
|
||||||
# Fixup function to extract dhcp_options by key
|
def generate_ip():
|
||||||
def get_option(dhcp_options, key):
|
# Générer une adresse IP dans la plage DHCP
|
||||||
|
while True:
|
||||||
|
ip = f"192.168.1.{random.randint(100, 200)}"
|
||||||
|
if ip not in allocated_ips:
|
||||||
|
allocated_ips.add(ip)
|
||||||
|
return ip
|
||||||
|
|
||||||
must_decode = ['hostname', 'domain', 'vendor_class_id']
|
def generate_dhcp_offer(pkt):
|
||||||
try:
|
if pkt.haslayer(DHCP) and pkt[DHCP].options[0][1] == 1: # Discover
|
||||||
for i in dhcp_options:
|
client_mac = pkt[Ether].src
|
||||||
if i[0] == key:
|
offered_ip = generate_ip()
|
||||||
# If DHCP Server Returned multiple name servers
|
|
||||||
# return all as comma seperated string.
|
# Construction de la réponse DHCP Offer
|
||||||
if key == 'name_server' and len(i) > 2:
|
dhcp_offer = (
|
||||||
return ",".join(i[1:])
|
Ether(dst=client_mac, src=RandMAC()) /
|
||||||
# domain and hostname are binary strings,
|
IP(src=DHCP_SERVER_IP, dst='255.255.255.255') /
|
||||||
# decode to unicode string before returning
|
UDP(sport=67, dport=68) /
|
||||||
elif key in must_decode:
|
BOOTP(op=2, yiaddr=offered_ip, siaddr=DHCP_SERVER_IP, chaddr=client_mac) /
|
||||||
return i[1].decode()
|
DHCP(options=[
|
||||||
else:
|
("message-type", "offer"),
|
||||||
return i[1]
|
("server_id", DHCP_SERVER_IP),
|
||||||
except:
|
("lease_time", LEASE_TIME),
|
||||||
pass
|
("subnet_mask", SUBNET_MASK),
|
||||||
|
("router", ROUTER_IP),
|
||||||
|
("dns", DNS_IP),
|
||||||
|
("end")
|
||||||
|
])
|
||||||
|
)
|
||||||
|
|
||||||
|
sendp(dhcp_offer, iface='eth0', verbose=False) # Remplacez 'eth0' par votre interface
|
||||||
|
|
||||||
def handle_dhcp_packet(packet):
|
def start_dhcp_server():
|
||||||
|
print("Démarrage du serveur DHCP...")
|
||||||
# Match DHCP discover
|
sniff(filter="udp and (port 67 or port 68)", prn=generate_dhcp_offer, store=0)
|
||||||
if DHCP in packet and packet[DHCP].options[0][1] == 1:
|
|
||||||
print('---')
|
|
||||||
print('New DHCP Discover')
|
|
||||||
#print(packet.summary())
|
|
||||||
#print(ls(packet))
|
|
||||||
hostname = get_option(packet[DHCP].options, 'hostname')
|
|
||||||
print(f"Host {hostname} ({packet[Ether].src}) asked for an IP")
|
|
||||||
|
|
||||||
|
|
||||||
# Match DHCP offer
|
|
||||||
elif DHCP in packet and packet[DHCP].options[0][1] == 2:
|
|
||||||
print('---')
|
|
||||||
print('New DHCP Offer')
|
|
||||||
#print(packet.summary())
|
|
||||||
#print(ls(packet))
|
|
||||||
|
|
||||||
subnet_mask = get_option(packet[DHCP].options, 'subnet_mask')
|
|
||||||
lease_time = get_option(packet[DHCP].options, 'lease_time')
|
|
||||||
router = get_option(packet[DHCP].options, 'router')
|
|
||||||
name_server = get_option(packet[DHCP].options, 'name_server')
|
|
||||||
domain = get_option(packet[DHCP].options, 'domain')
|
|
||||||
|
|
||||||
print(f"DHCP Server {packet[IP].src} ({packet[Ether].src}) "
|
|
||||||
f"offered {packet[BOOTP].yiaddr}")
|
|
||||||
|
|
||||||
print(f"DHCP Options: subnet_mask: {subnet_mask}, lease_time: "
|
|
||||||
f"{lease_time}, router: {router}, name_server: {name_server}, "
|
|
||||||
f"domain: {domain}")
|
|
||||||
|
|
||||||
|
|
||||||
# Match DHCP request
|
|
||||||
elif DHCP in packet and packet[DHCP].options[0][1] == 3:
|
|
||||||
print('---')
|
|
||||||
print('New DHCP Request')
|
|
||||||
#print(packet.summary())
|
|
||||||
#print(ls(packet))
|
|
||||||
|
|
||||||
requested_addr = get_option(packet[DHCP].options, 'requested_addr')
|
|
||||||
hostname = get_option(packet[DHCP].options, 'hostname')
|
|
||||||
print(f"Host {hostname} ({packet[Ether].src}) requested {requested_addr}")
|
|
||||||
|
|
||||||
|
|
||||||
# Match DHCP ack
|
|
||||||
elif DHCP in packet and packet[DHCP].options[0][1] == 5:
|
|
||||||
print('---')
|
|
||||||
print('New DHCP Ack')
|
|
||||||
#print(packet.summary())
|
|
||||||
#print(ls(packet))
|
|
||||||
|
|
||||||
subnet_mask = get_option(packet[DHCP].options, 'subnet_mask')
|
|
||||||
lease_time = get_option(packet[DHCP].options, 'lease_time')
|
|
||||||
router = get_option(packet[DHCP].options, 'router')
|
|
||||||
name_server = get_option(packet[DHCP].options, 'name_server')
|
|
||||||
|
|
||||||
print(f"DHCP Server {packet[IP].src} ({packet[Ether].src}) "
|
|
||||||
f"acked {packet[BOOTP].yiaddr}")
|
|
||||||
|
|
||||||
print(f"DHCP Options: subnet_mask: {subnet_mask}, lease_time: "
|
|
||||||
f"{lease_time}, router: {router}, name_server: {name_server}")
|
|
||||||
|
|
||||||
# Match DHCP inform
|
|
||||||
elif DHCP in packet and packet[DHCP].options[0][1] == 8:
|
|
||||||
print('---')
|
|
||||||
print('New DHCP Inform')
|
|
||||||
#print(packet.summary())
|
|
||||||
#print(ls(packet))
|
|
||||||
|
|
||||||
hostname = get_option(packet[DHCP].options, 'hostname')
|
|
||||||
vendor_class_id = get_option(packet[DHCP].options, 'vendor_class_id')
|
|
||||||
|
|
||||||
print(f"DHCP Inform from {packet[IP].src} ({packet[Ether].src}) "
|
|
||||||
f"hostname: {hostname}, vendor_class_id: {vendor_class_id}")
|
|
||||||
|
|
||||||
else:
|
|
||||||
print('---')
|
|
||||||
print('Some Other DHCP Packet')
|
|
||||||
print(packet.summary())
|
|
||||||
print(ls(packet))
|
|
||||||
|
|
||||||
return
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
sniff(filter="udp and (port 67 or 68)", prn=handle_dhcp_packet)
|
start_dhcp_server()
|
||||||
|
|||||||
Reference in New Issue
Block a user