Advertisement
nahakubuilder

scanner.py

Jan 15th, 2024
607
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.04 KB | None | 0 0
  1. from flask import Flask, render_template
  2. from flask_socketio import SocketIO, emit
  3. from scapy.all import Ether, IP, UDP, BOOTP, DHCP, RandMAC, conf, AsyncSniffer, srp1
  4. import psutil
  5. import threading
  6. import time
  7.  
  8.  
  9. app = Flask(__name__)
  10. socketio = SocketIO(app)
  11.  
  12. def mac_to_bytes(mac_addr: str) -> bytes:
  13.     return int(mac_addr.replace(":", "").replace("-", ""), 16).to_bytes(6, "big")
  14.  
  15. def sniff_dhcp_responses(interface_name, duration):
  16.     conf.checkIPaddr = False
  17.     sniff_filter = "udp and (port 67 or port 68) and not ether src host " + ":".join(psutil.net_if_addrs()[interface_name][0].address.split(":"))
  18.  
  19.     sniffer = AsyncSniffer(iface=interface_name, filter=sniff_filter, prn=handle_dhcp_response)
  20.     sniffer.start()
  21.    
  22.     time.sleep(duration)
  23.    
  24.     sniffer.stop()
  25.  
  26. def handle_dhcp_response(packet):
  27.     if DHCP in packet:
  28.         dhcp_options = packet[DHCP].options
  29.         desired_options = ['server_id', 'subnet_mask', 'router', 'name_server', 'domain']
  30.         extracted_options = {}
  31.  
  32.         for option, *values in dhcp_options:
  33.             value = values[0] if values else None
  34.  
  35.             if isinstance(value, bytes):
  36.                 value = value.decode('utf-8')
  37.  
  38.             if option in desired_options:
  39.                 if option not in extracted_options:
  40.                     extracted_options[option] = value
  41.  
  42.         offered_ip = packet[BOOTP].yiaddr
  43.  
  44.         result = {
  45.             'interface': packet.sniffed_on,
  46.             'options': extracted_options,
  47.             'offered_ip': offered_ip
  48.         }
  49.  
  50.         socketio.emit('dhcp_response', result)
  51.  
  52. @socketio.on('send_dhcp_request')
  53. def handle_send_dhcp_request(data):
  54.     interface_name = data['interface']
  55.     duration = 6  # Adjust the duration as needed
  56.  
  57.     interface_id = [iface for iface, addrs in psutil.net_if_addrs().items() if iface == interface_name]
  58.    
  59.     if not interface_id:
  60.         result = {'interface': interface_name, 'result': 'Invalid interface name.'}
  61.         emit('dhcp_response', result)
  62.         return
  63.  
  64.     mac_address = psutil.net_if_addrs()[interface_name][0].address
  65.  
  66.     DHCP_DISCOVER = (
  67.         Ether(dst='ff:ff:ff:ff:ff:ff', src=mac_address.replace("-", ":"), type=0x0800) \
  68.         / IP(src='0.0.0.0', dst='255.255.255.255') \
  69.         / UDP(dport=67, sport=68) \
  70.         / BOOTP(op=1, chaddr=mac_address.replace("-", ":")) \
  71.         / DHCP(options=[('message-type', 'discover'), ('end')])
  72.     )
  73.  
  74.     threading.Thread(target=sniff_dhcp_responses, args=(interface_name, duration)).start()
  75.    
  76.     srp1(DHCP_DISCOVER, iface=interface_name, verbose=1, timeout=6)
  77.    
  78. @app.route('/')
  79. def index():
  80.     exclude_words = ['Bluetooth', 'VMware', 'VPN', 'Loopback', 'Local Area']  # Add the words you want to exclude
  81.     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)]
  82.     return render_template('index.html', interfaces=interfaces)
  83.  
  84. if __name__ == '__main__':
  85.     socketio.run(app, debug=True)
  86.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement