发送ARP响应

def send_arp_reply(ifname, src_ip, src_mac, dst_ip, dst_mac): s = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, socket.htons(ETH_P_ALL)) s.bind((ifname, ETH_P_ALL)) arp_packet = struct.pack('!6s6s2s2s1s1s2s6s4s6s4s', bytes(dst_mac.replace(':', ''), 'utf-8'), bytes(src_mac.replace(':', ''), 'utf-8'), bytes.fromhex('08 06'), bytes.fromhex('00 02'), bytes.fromhex('08'), bytes.fromhex('00'), bytes.fromhex('06 04'), bytes.fromhex('00 02'), socket.inet_aton(src_ip), bytes.fromhex(dst_mac.replace(':', '')), socket.inet_aton(dst_ip)) s.send(arp_packet)

发送ICMP请求

def send_icmp_request(ifname, src_ip, src_mac, dst_ip): s = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, socket.htons(ETH_P_ALL)) s.bind((ifname, ETH_P_ALL)) dst_mac = ip_mac_dict[dst_ip] # 获取目标主机的MAC地址 icmp_packet = struct.pack('!6s6s2s1s1s2s4s4s', bytes(dst_mac.replace(':', ''), 'utf-8'), bytes(src_mac.replace(':', ''), 'utf-8'), bytes.fromhex('08 00'), bytes.fromhex('00'), bytes.fromhex('00'), bytes.fromhex('00 00'), socket.inet_aton(src_ip), socket.inet_aton(dst_ip)) s.send(icmp_packet)

发送ICMP响应

def send_icmp_reply(ifname, src_ip, src_mac, dst_ip): s = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, socket.htons(ETH_P_ALL)) s.bind((ifname, ETH_P_ALL)) dst_mac = ip_mac_dict[dst_ip] # 获取目标主机的MAC地址 icmp_packet = struct.pack('!6s6s2s1s1s2s4s4s', bytes(dst_mac.replace(':', ''), 'utf-8'), bytes(src_mac.replace(':', ''), 'utf-8'), bytes.fromhex('08 00'), bytes.fromhex('00'), bytes.fromhex('00'), bytes.fromhex('00 00'), socket.inet_aton(src_ip), socket.inet_aton(dst_ip)) s.send(icmp_packet)

发送TCP SYN

def send_tcp_syn(ifname, src_ip, src_mac, dst_ip, dst_port): s = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, socket.htons(ETH_P_ALL)) s.bind((ifname, ETH_P_ALL)) dst_mac = ip_mac_dict[dst_ip] # 获取目标主机的MAC地址 tcp_packet = struct.pack('!6s6s2s2s2s1s1s2s4s4s2s2s', bytes.fromhex(dst_mac.replace(':', '')), bytes.fromhex(src_mac.replace(':', '')), bytes.fromhex('08 00'), bytes.fromhex('45'), bytes.fromhex('00 28'), bytes.fromhex('00 00'), bytes.fromhex('40 00'), bytes.fromhex('40 06'), bytes.fromhex('00 00 00 00'), socket.inet_aton(src_ip), socket.inet_aton(dst_ip), bytes.fromhex('00 00'), bytes.fromhex('00 00'), bytes.fromhex('00 00'), bytes.fromhex('00 00'), bytes.fromhex('50 02'), bytes.fromhex('00 00'), bytes.fromhex('00 00')) tcp_packet += struct.pack('!H', dst_port) tcp_packet += struct.pack('!H', 0x1234) tcp_packet += struct.pack('!I', 0) tcp_packet += struct.pack('!I', 0) tcp_packet += bytes.fromhex('50 02 20 00') s.send(tcp_packet)

解析ARP包

def parse_arp_packet(packet): src_mac = ':'.join(['%02x' % b for b in packet[6:12]]) src_ip = socket.inet_ntoa(packet[28:32]) dst_ip = socket.inet_ntoa(packet[38:42]) arp_op = struct.unpack('!H', packet[20:22])[0] if arp_op == ARP_REQUEST: send_arp_reply(ifname, dst_ip, get_mac_address(ifname), src_ip, src_mac) elif arp_op == ARP_REPLY: ip_mac_dict[src_ip] = src_mac

解析ICMP包

def parse_icmp_packet(packet): src_mac = ':'.join(['%02x' % b for b in packet[6:12]]) src_ip = socket.inet_ntoa(packet[26:30]) dst_ip = socket.inet_ntoa(packet[30:34]) icmp_type = struct.unpack('!B', packet[34:35])[0] icmp_code = struct.unpack('!B', packet[35:36])[0] if icmp_type == ICMP_ECHO_REQUEST: send_icmp_reply(ifname, dst_ip, get_mac_address(ifname), src_ip)

解析TCP包

def parse_tcp_packet(packet): src_mac = ':'.join(['%02x' % b for b in packet[6:12]]) src_ip = socket.inet_ntoa(packet[26:30]) dst_ip = socket.inet_ntoa(packet[30:34]) tcp_flags = struct.unpack('!B', packet[47:48])[0] if tcp_flags == TCP_SYN: attack_source_dict[src_ip] = src_mac

监听网络数据包

def sniff_packets(ifname): s = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, socket.htons(ETH_P_ALL)) s.bind((ifname, ETH_P_ALL)) while True

# 发送ARP响应def send_arp_replyifname src_ip src_mac dst_ip dst_mac s = socketsocketsocketAF_PACKET socketSOCK_RAW sockethtonsETH_P_ALL sbindifname ETH_P_ALL arp_packet = structpack!6s6s2s2s1s1s2

原文地址: http://www.cveoy.top/t/topic/fnZA 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录