首页
python
Win桌面应用自动化pywinauto
pywinauto遍历展示程序的所有菜单项
Python3使用串口
Python字符串处理
Python3实现配置文件差异对比(键值对key=value的形式)
正则
正则匹配两个字符之间的字符串
正则提取markdown中图片链接
正则表达式基本知识
字符串-正则实例
Python生成8位随机字符串的一些方法
python3批量ping检测
python3检测一批主机ip的tcp端口
Python3--DNS
3D人体骨架检测(mediapipe)
Python-网络
检测到win电脑断网就发出告警声音
Python编写的DNS服务可靠性测试程序
Python编写的TCP/UDP端口探测程序
Python抓包-Pyshark
并发扫描TCP、UDP端口
Python-DHCP
端口的问题以及绑定端口(Python)
VSCode编辑器
python远程开机工具
Pyhton3--Win注册表
开源IT运维项目
python知识点
Python开源运维项目集合
python3备份交换机配置
Python3使用snmp获取H3C交换机arp表
Python检测tcp端口状态并发送到企微群
Python3使用smtp发邮件
Python3使用smtp发邮件(带附件)
Python3拉取微软AD域所有用户信息
Python3使用微软AD域账号认证
自动更新公网域名的解析(阿里云)
每日定时推送Excel中排班表信息到企微群
Django
Django模板标签
Django在发送POST请求时返回403错误
Django-mysql数据库条件查询
uwsgi 配置 python virtualenv 虚拟环境目录 ( ini 配置)
常用收藏
python3数据类型转换
python处理文本
txt每行一个字符串_用英文逗号连接
txt_匹配并拆分出需要字符串
python获取日期时间
python执行linux命令的三种方式
python使用ssh连接到linux服务器执行命令
本文档由 内网文摘 发布,转载请注明出处
-
+
首页
Python-DHCP
脚本无法使用,还需调试 ``` import socket import struct import logging from datetime import datetime, timedelta # 设置日志 logging.basicConfig(level=logging.INFO) # DHCP服务器的IP地址和端口(DHCP服务器通常使用67端口) dhcp_server_ip = '0.0.0.0' # 绑定到所有接口 # dhcp_server_ip = '192.168.1.1' dhcp_server_port = 67 # 子网掩码 subnet_mask = '255.255.255.0' # 要分配的IP地址范围 dhcp_pool_start = '192.168.1.100' dhcp_pool_end = '192.168.1.200' # 租期(示例中使用1小时) lease_duration = timedelta(hours=1) # IP地址分配状态 ip_allocations = {} # 将IP地址转换为整数 def ip_to_int(ip): return struct.unpack("!I", socket.inet_aton(ip))[0] # 检查IP地址是否在分配池内 def is_valid_dhcp_request(requested_ip): return ip_to_int(dhcp_pool_start) <= ip_to_int(requested_ip) <= ip_to_int(dhcp_pool_end) # 解析DHCP消息 def parse_dhcp_message(data): options = {} if len(data) < 240: return options options['message_type'] = data[240] option_index = 241 while option_index < len(data): option = data[option_index] if option == 255: break length = data[option_index + 1] option_index += 2 if option == 53: # DHCP message type options['message_type'] = data[option_index] elif option == 51: # IP address lease time options['lease_time'] = struct.unpack('!I', data[option_index:option_index+4])[0] elif option == 50: # Requested IP address options['requested_ip'] = socket.inet_ntoa(data[option_index:option_index+4]) elif option == 54: # Server identifier options['server_id'] = socket.inet_ntoa(data[option_index:option_index+4]) option_index += length return options # 构建DHCP OFFER响应包 def build_dhcp_offer(requested_ip): offer_packet = b'' offer_packet += b'\xff' + b'\xd2' + b'\x01' + b'\x01' offer_packet += b'\x00' * 4 + b'\x00' * 6 + b'\x00' * 10 offer_packet += b'\x00' * 4 offer_packet += socket.inet_aton(dhcp_server_ip) offer_packet += b'\x00' * 10 offer_packet += b'\x00' * 2 offer_packet += b'\x63' + b'\x82' + b'\x53' + b'\x63' offer_packet += b'\x35' + b'\x01' + b'\x02' offer_packet += b'\x36' + b'\x04' + socket.inet_aton(requested_ip) offer_packet += b'\x03' + b'\x04' + socket.inet_aton(subnet_mask) offer_packet += b'\x33' + b'\x04' + socket.inet_aton(dhcp_server_ip) offer_packet += b'\x35' + b'\x05' + b'\xff' + b'\xff' + b'\xff' + b'\xff' offer_packet += b'\x05' + b'\x04' + socket.inet_aton('0.0.0.0') offer_packet += b'\x07' + b'\x04' + socket.inet_aton('0.0.0.0') offer_packet += b'\xff' return offer_packet # 发送DHCP OFFER响应 def send_dhcp_offer(client_socket, client_ip, client_port, requested_ip): offer_packet = build_dhcp_offer(requested_ip) client_socket.sendto(offer_packet, (client_ip, client_port)) logging.info(f"Sent DHCP OFFER to {client_ip}:{client_port}") # 构建DHCP ACK响应包 def build_dhcp_ack(requested_ip): ack_packet = b'' ack_packet += b'\xff' + b'\xd2' + b'\x01' + b'\x01' ack_packet += b'\x00' * 4 + b'\x00' * 6 + b'\x00' * 10 ack_packet += b'\x00' * 4 ack_packet += socket.inet_aton(requested_ip) ack_packet += b'\x00' * 10 ack_packet += b'\x00' * 2 ack_packet += b'\x63' + b'\x82' + b'\x53' + b'\x63' ack_packet += b'\x35' + b'\x01' + b'\x05' ack_packet += b'\x36' + b'\x04' + socket.inet_aton(requested_ip) ack_packet += b'\x03' + b'\x04' + socket.inet_aton(subnet_mask) ack_packet += b'\x33' + b'\x04' + socket.inet_aton(dhcp_server_ip) ack_packet += b'\x35' + b'\x05' + b'\xff' + b'\xff' + b'\xff' + b'\xff' ack_packet += b'\x05' + b'\x04' + socket.inet_aton('0.0.0.0') ack_packet += b'\x07' + b'\x04' + socket.inet_aton('0.0.0.0') ack_packet += b'\xff' return ack_packet # 发送DHCP ACK响应 def send_dhcp_ack(client_socket, client_ip, client_port, requested_ip): ack_packet = build_dhcp_ack(requested_ip) client_socket.sendto(ack_packet, (client_ip, client_port)) logging.info(f"Sent DHCP ACK to {client_ip}:{client_port}") # DHCP服务器主函数 def dhcp_server(): server_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1) server_socket.bind((dhcp_server_ip, dhcp_server_port)) while True: logging.info("Waiting for DHCP DISCOVER...") data, client_address = server_socket.recvfrom(2048) logging.info(f"Received DHCP DISCOVER from {client_address[0]}:{client_address[1]}") logging.info(f"DHCP data {data}") options = parse_dhcp_message(data) if 'message_type' in options and options['message_type'] == 1: # DISCOVER requested_ip = options.get('requested_ip', dhcp_pool_start) if is_valid_dhcp_request(requested_ip) and requested_ip not in ip_allocations: send_dhcp_offer(server_socket, *client_address, requested_ip) ip_allocations[requested_ip] = client_address logging.info("Waiting for DHCP REQUEST...") data, client_address = server_socket.recvfrom(2048) logging.info(f"Received DHCP REQUEST from {client_address[0]}:{client_address[1]}") options = parse_dhcp_message(data) if 'message_type' in options and options['message_type'] == 3: # REQUEST requested_ip = options.get('requested_ip', dhcp_pool_start) if is_valid_dhcp_request(requested_ip) and requested_ip in ip_allocations: send_dhcp_ack(server_socket, *client_address, requested_ip) # 运行DHCP服务器 if __name__ == "__main__": dhcp_server() ``` 用于解析DHCP数据包并打印出详细信息 ``` # 一个完整的Python脚本,用于解析DHCP数据包并打印出详细信息。这个脚本将导入必要的模块,解析DHCP数据包,并打印出每个字段的详细信息。 import socket import struct def parse_dhcp_packet(data): if len(data) < 240: return "Invalid DHCP packet" # Parse the DHCP header operation = "BOOTREQUEST" if data[0] == 1 else "BOOTREPLY" hardware_type = data[1] hardware_address_length = data[2] hops = data[3] transaction_id = struct.unpack('!I', data[4:8])[0] seconds = struct.unpack('!H', data[8:10])[0] flags = struct.unpack('!I', data[10:14])[0] client_ip = socket.inet_ntoa(struct.pack('!I', (data[16] << 24) + (data[17] << 16) + (data[18] << 8) + data[19])) your_ip = socket.inet_ntoa(struct.pack('!I', (data[20] << 24) + (data[21] << 16) + (data[22] << 8) + data[23])) server_ip = socket.inet_ntoa(struct.pack('!I', (data[24] << 24) + (data[25] << 16) + (data[26] << 8) + data[27])) gateway_ip = socket.inet_ntoa(struct.pack('!I', (data[28] << 24) + (data[29] << 16) + (data[30] << 8) + data[31])) client_mac_address = ":".join(f"{b:02x}" for b in data[28:34]) # Parse the DHCP options options = {} index = 240 while index < len(data): option = data[index] if option == 255: # End option break length = data[index + 1] option_index = index + 2 if option == 53: # DHCP Message Type options['DHCP Message Type'] = data[option_index] elif option == 50: # Requested IP Address requested_ip = socket.inet_ntoa(struct.pack('!I', (data[option_index] << 24) + (data[option_index + 1] << 16) + (data[option_index + 2] << 8) + data[option_index + 3])) options['Requested IP Address'] = requested_ip elif option == 51: # IP Address Lease Time lease_time = struct.unpack('!I', data[option_index:option_index + 4])[0] options['Lease Time'] = lease_time elif option == 54: # Server Identifier server_id = socket.inet_ntoa(struct.pack('!I', (data[option_index] << 24) + (data[option_index + 1] << 16) + (data[option_index + 2] << 8) + data[option_index + 3])) options['Server Identifier'] = server_id elif option == 58: # Renewal Time renewal_time = struct.unpack('!I', data[option_index:option_index + 4])[0] options['Renewal Time'] = renewal_time elif option == 61: # Client Identifier client_id = data[option_index:option_index + length] options['Client Identifier'] = client_id.hex() index += length + 2 return { 'Operation': operation, 'Hardware Type': hardware_type, 'Hardware Address Length': hardware_address_length, 'Hops': hops, 'Transaction ID': transaction_id, 'Seconds': seconds, 'Flags': flags, 'Client IP': client_ip, 'Your IP': your_ip, 'Server IP': server_ip, 'Gateway IP': gateway_ip, 'Client MAC Address': client_mac_address, 'Options': options } def print_byte_details(data): print("Offset (h): 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F Index (d): Data (h) ASCII") print("-" * 120) index = 0 for offset in range(0, len(data), 16): line = f"{offset:08x} " hex_line = "" ascii_line = "" for i in range(16): if index < len(data): byte = data[index] hex_line += f"{byte:02x} " ascii_line += (chr(byte) if 32 <= byte < 127 else '.') else: hex_line += " " ascii_line += " " index += 1 hex_line = hex_line.strip() ascii_line = ascii_line.strip() print(f"{line}{hex_line:<47} {index-offset:02x} {ascii_line}") print("-" * 120) def main(): server_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1) # server_socket.bind(('0.0.0.0', 67)) server_socket.bind(('192.168.1.1', 67)) while True: logging.info("Waiting for DHCP DISCOVER...") data, client_address = server_socket.recvfrom(2048) logging.info(f"Received DHCP DISCOVER from {client_address[0]}:{client_address[1]}") parsed_data = parse_dhcp_packet(data) print_byte_details(data) print("Parsed DHCP Data:") for key, value in parsed_data.items(): print(f"{key}: {value}") if __name__ == "__main__": import logging logging.basicConfig(level=logging.INFO) main() ```
local
2024年10月24日 21:17
分享文档
收藏文档
上一篇
下一篇
微信扫一扫
复制链接
手机扫一扫进行分享
复制链接
关于 LocalNetwork
LocalNetwork
是由mrdoc开源
LocalNetwork.cn
修改的在线文档系统,作为个人和小型团队的云笔记、文档和知识库管理工具。
如果此文档给你或你的团队带来了帮助,欢迎支持作者持续投入精力更新和维护!内网文摘 & LocalNetwork
>>>主页
logo
logo
下载Markdown文件
分享
链接
类型
密码
更新密码