Python构造IP报文
1 import socket 2 import sys 3 import time 4 import struct 5 6 HOST, PORT = "10.60.66.66", 10086 7 8 def make_forward_iphdr(source_ip = '1.0.0.1', dest_ip = '2.0.0.2', proto = socket.IPPROTO_UDP) : 9 # ip header fields 10 ip_ihl = 5 11 ip_ver = 4 12 ip_tos = 0 13 ip_tot_len = 0 # kernel will fill the correct total length 14 ip_id = 54321 #Id of this packet 15 ip_frag_off = 0 16 ip_ttl = 255 17 ip_proto = proto 18 ip_check = 0 # kernel will fill the correct checksum 19 ip_saddr = socket.inet_aton ( source_ip ) #Spoof the source ip address if you want to 20 ip_daddr = socket.inet_aton ( dest_ip ) 21 22 ip_ihl_ver = (ip_ver << 4) + ip_ihl 23 24 # the ! in the pack format string means network order 25 ip_header = struct.pack('!BBHHHBBH4s4s', ip_ihl_ver, ip_tos, ip_tot_len, ip_id, ip_frag_off, ip_ttl, ip_proto, ip_check, ip_saddr, ip_daddr) 26 return ip_header 27 28 def make_forward_udphdr(src_port = 1024, dst_port = 10086) : 29 udp_header = struct.pack('!HHHH', src_port, dst_port, 0, 0) 30 return udp_header 31 32 # checksum functions needed for calculation checksum 33 def checksum(msg): 34 s = 0 35 36 # loop taking 2 characters at a time 37 for i in range(0, len(msg), 2): 38 w = ord(msg[i]) + (ord(msg[i+1]) << 8 ) 39 s = s + w 40 41 s = (s>>16) + (s & 0xffff); 42 s = s + (s >> 16); 43 44 #complement and mask to 4 byte short 45 s = ~s & 0xffff 46 47 return s 48 49 def make_tcp_data(ip_header, src_port = 1024, dst_port = 10086, source_ip='1.0.0.1', dest_ip='2.0.0.2', user_data = 'test') : 50 tcp_source = src_port # source port 51 tcp_dest = dst_port # destination port 52 tcp_seq = 454 53 tcp_ack_seq = 0 54 tcp_doff = 5 #4 bit field, size of tcp header, 5 * 4 = 20 bytes 55 #tcp flags 56 tcp_fin = 0 57 tcp_syn = 1 58 tcp_rst = 0 59 tcp_psh = 0 60 tcp_ack = 0 61 tcp_urg = 0 62 tcp_window = socket.htons (5840) # maximum allowed window size 63 tcp_check = 0 64 tcp_urg_ptr = 0 65 66 tcp_offset_res = (tcp_doff << 4) + 0 67 tcp_flags = tcp_fin + (tcp_syn << 1) + (tcp_rst << 2) + (tcp_psh <<3) + (tcp_ack << 4) + (tcp_urg << 5) 68 69 # the ! in the pack format string means network order 70 tcp_header = struct.pack('!HHLLBBHHH' , tcp_source, tcp_dest, tcp_seq, tcp_ack_seq, tcp_offset_res, tcp_flags, tcp_window, tcp_check, tcp_urg_ptr) 71 72 source_address = socket.inet_aton(source_ip) 73 dest_address = socket.inet_aton(dest_ip) 74 placeholder = 0 75 protocol = socket.IPPROTO_TCP 76 tcp_length = len(tcp_header) + len(user_data) 77 78 psh = struct.pack('!4s4sBBH' , source_address , dest_address , placeholder , protocol , tcp_length); 79 psh = psh + tcp_header + user_data; 80 81 tcp_check = checksum(psh) 82 #print tcp_checksum 83 84 # make the tcp header again and fill the correct checksum - remember checksum is NOT in network byte order 85 tcp_header = struct.pack('!HHLLBBH' , tcp_source, tcp_dest, tcp_seq, tcp_ack_seq, tcp_offset_res, tcp_flags, tcp_window) + struct.pack('H' , tcp_check) + struct.pack('!H' ,tcp_urg_ptr) 86 87 # final full packet - syn packets dont have any data 88 packet = ip_header + tcp_header + user_data 89 return packet