def getICMPPacket(self): """ 构造ICMP报文 :return: """ try: icmp_packet = IP() / ICMP() icmp_packet.version = int(self.entries[2].get()) icmp_packet.id = int(self.entries[3].get()) icmp_packet.flags = int(self.entries[4].get()) icmp_packet.frag = int(self.entries[5].get()) icmp_packet.ttl = int(self.entries[6].get()) # ip_packet.chksum = str(self.entries[7].get()) icmp_packet.src = str(self.entries[8].get()) icmp_packet.dst = str(self.entries[9].get()) icmp_packet.type = int(self.entries[0].get()) # icmp_packet.chksum = str(self.entries[1].get()) # 获得数据包的二进制值 pkg_raw = raw(icmp_packet) # 构造数据包,自动计算校验和 icmp_packet = IP(pkg_raw) # 去除数据包的IP首部,并构建ICMP对象,这样可以获得ICMP的校验和 pkg_icmp = pkg_raw[20:] pkg_icmp = ICMP(pkg_icmp) print("scapy自动计算的ICMP的校验和为:%04x" % pkg_icmp.chksum) self.entries[1].delete(0, END) self.entries[1].insert(0, hex(pkg_icmp.chksum)) self.entries[7].delete(0, END) self.entries[7].insert(0, hex(icmp_packet.chksum)) icmp_packet.show() self.resultText.insert('end', icmp_packet.summary() + '\n') self.resultText.insert('end', str(icmp_packet) + '\n') return Ether() / icmp_packet except Exception as e: print(e.with_traceback()) finally: pass
#!/usr/bin/python3 from scapy.all import * # Construct IP header from scapy.layers.inet import IP #src="l49.127.74.171", ip = IP(src="49.127.74.171", dst="seanyiyi.com") ip.id = 1000 # Identification ip.frag = 0 # Offset of this IP fragment ip.flags = 1 # Flags # Construct UDP header udp = UDP(sport=7070, dport=9090, chksum=0) udp.len = 200 # This should be the combined length of all fragments # Construct payload payload = "A" * 72 # Put 80 bytes in the first fragment (72 byte from payload + 8byte udp header) # Construct the entire packet and send it out pkt = ip / udp / payload # For other fragments, we should use ip/payload send(pkt, verbose=0) pkt.show() #----------------------second fragment--------------------- print( "----------------------------------second fragment---------------------------------" ) ip.proto = 17 payload2 = "B" * 80 # 80 / 8 ip.frag = 10 pkt2 = ip / payload2 send(pkt2, verbose=0) pkt2.show()
def getIPPacket(self): """ 构造IP数据包 :return: """ # chksum = self.entries[9].get() try: eth = Ether() eth.src = self.entries[0].get() eth.dst = self.entries[1].get() eth.type = int(self.entries[2].get()) ip_packet = IP() ip_packet.versionion = int(self.entries[3].get()) ip_packet.ihl = int(self.entries[4].get()) ip_packet.tos = int(self.entries[5].get()) ip_packet.len = int(self.entries[6].get()) ip_packet.id = int(self.entries[7].get()) ip_packet.flags = int(self.entries[8].get()) ip_packet.frag = int(self.entries[9].get()) ip_packet.ttl = int(self.entries[10].get()) ip_packet.proto = int(self.entries[11].get()) payload = self.entries[16].get() ip_packet.src = self.entries[13].get() ip_packet.dst = self.entries[14].get() # 不含payload计算首部校验和 if payload == '': print("无payload的IP报文") ip_packet.show() checksum_scapy = IP(raw(ip_packet)).chksum # 自主计算验证IP首部检验和并进行填充 print("scapy自动计算的IP首部检验和是:%04x (%s)" % (checksum_scapy, str(checksum_scapy))) # 1.将IP首部和自动设置为0 ip_packet.chksum = 0 # 2.生成ip首部的数据字符串 x = raw(ip_packet) ipString = "".join("%02x" % orb(x) for x in x) # 3.将ip首部的数据字符串转换成字节数组 ipbytes = bytearray.fromhex(ipString) # 4.调用校验和计算函数计算校验和 checksum_self = self.IP_headchecksum(ipbytes) # 5.进行校验和验证 print("验证计算IP首部的检验和是:%04x (%s)" % (checksum_self, str(checksum_self))) # 含payload计算首部校验和 else: print("含有payload的IP报文") ip_packet = ip_packet / payload ip_packet.show() ip_packet.len = 20 + len(payload) checksum_scapy = IP(raw(ip_packet)).chksum print("scapy自动计算的IP首部检验和是:%04x (%s)" % (checksum_scapy, str(checksum_scapy))) ip_packet.chksum = 0 ip_packet.ihl = 5 print('\n 报文长度是:%s' % str(ip_packet.len)) x = raw(ip_packet) ipString = "".join("%02x" % orb(x) for x in x) ipbytes = bytearray.fromhex(ipString) checksum_self = self.IP_headchecksum(ipbytes[0:ip_packet.ihl * 4]) print("验证计算IP首部的检验和是:%04x (%s)" % (checksum_self, str(checksum_self))) if checksum_self == checksum_scapy: print("检验和正确") else: print("检验和不正确") ip_packet.chksum = checksum_self self.entries[12].delete(0, END) self.entries[12].insert(0, hex(ip_packet.chksum)) ip_packet.show() self.resultText.insert('end', ip_packet.summary() + '\n') self.resultText.insert('end', str(ip_packet) + '\n') return eth / ip_packet except Exception as e: print(e.with_traceback()) finally: pass