Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from flask import Flask, render_template
- from flask_socketio import SocketIO, emit
- from scapy.all import Ether, IP, UDP, BOOTP, DHCP, RandMAC, conf, AsyncSniffer, srp1
- import psutil
- import threading
- import time
- app = Flask(__name__)
- socketio = SocketIO(app)
- def mac_to_bytes(mac_addr: str) -> bytes:
- return int(mac_addr.replace(":", "").replace("-", ""), 16).to_bytes(6, "big")
- def sniff_dhcp_responses(interface_name, duration):
- conf.checkIPaddr = False
- sniff_filter = "udp and (port 67 or port 68) and not ether src host " + ":".join(psutil.net_if_addrs()[interface_name][0].address.split(":"))
- sniffer = AsyncSniffer(iface=interface_name, filter=sniff_filter, prn=handle_dhcp_response)
- sniffer.start()
- time.sleep(duration)
- sniffer.stop()
- def handle_dhcp_response(packet):
- if DHCP in packet:
- dhcp_options = packet[DHCP].options
- desired_options = ['server_id', 'subnet_mask', 'router', 'name_server', 'domain']
- extracted_options = {}
- for option, *values in dhcp_options:
- value = values[0] if values else None
- if isinstance(value, bytes):
- value = value.decode('utf-8')
- if option in desired_options:
- if option not in extracted_options:
- extracted_options[option] = value
- offered_ip = packet[BOOTP].yiaddr
- result = {
- 'interface': packet.sniffed_on,
- 'options': extracted_options,
- 'offered_ip': offered_ip
- }
- socketio.emit('dhcp_response', result)
- @socketio.on('send_dhcp_request')
- def handle_send_dhcp_request(data):
- interface_name = data['interface']
- duration = 6 # Adjust the duration as needed
- interface_id = [iface for iface, addrs in psutil.net_if_addrs().items() if iface == interface_name]
- if not interface_id:
- result = {'interface': interface_name, 'result': 'Invalid interface name.'}
- emit('dhcp_response', result)
- return
- mac_address = psutil.net_if_addrs()[interface_name][0].address
- DHCP_DISCOVER = (
- Ether(dst='ff:ff:ff:ff:ff:ff', src=mac_address.replace("-", ":"), type=0x0800) \
- / IP(src='0.0.0.0', dst='255.255.255.255') \
- / UDP(dport=67, sport=68) \
- / BOOTP(op=1, chaddr=mac_address.replace("-", ":")) \
- / DHCP(options=[('message-type', 'discover'), ('end')])
- )
- threading.Thread(target=sniff_dhcp_responses, args=(interface_name, duration)).start()
- srp1(DHCP_DISCOVER, iface=interface_name, verbose=1, timeout=6)
- @app.route('/')
- def index():
- exclude_words = ['Bluetooth', 'VMware', 'VPN', 'Loopback', 'Local Area'] # Add the words you want to exclude
- interfaces = [iface for iface, addrs in psutil.net_if_addrs().items() if not iface.startswith('lo') and all(word.lower() not in iface.lower() for word in exclude_words)]
- return render_template('index.html', interfaces=interfaces)
- if __name__ == '__main__':
- socketio.run(app, debug=True)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement