def getTCPPacket(self): """ 构造TCP数据包 :return: """ try: ip_packet = IP() ip_packet.version = int(self.entries[8].get()) ip_packet.ihl = int(self.entries[9].get()) ip_packet.tos = int(self.entries[10].get()) ip_packet.id = int(self.entries[11].get()) # ip_packet.flags = int(self.entries[12].get()) ip_packet.frag = int(self.entries[13].get()) ip_packet.ttl = int(self.entries[14].get()) # ip_packet.chksum = self.entries[15].get() ip_packet.src = self.entries[16].get() ip_packet.dst = self.entries[17].get() tcp_packet = TCP() tcp_packet.sport = int(self.entries[0].get()) tcp_packet.dport = int(self.entries[1].get()) tcp_packet.seq = int(self.entries[2].get()) tcp_packet.ack = int(self.entries[3].get()) tcp_packet.dataofs = int(self.entries[4].get()) tcp_packet.flags = int(self.entries[5].get()) tcp_packet.window = int(self.entries[6].get()) # tcp_packet.chksum = self.entries[7].get() # scapy自动计算IP、TCP校验和 # 获得数据包的二进制值 pkg_raw = raw(ip_packet / tcp_packet) tcp_packet_raw = pkg_raw[20:] # 构造数据包,自动计算校验和 scapy_chksum_IP = IP(pkg_raw).chksum scapy_chksum_tcp = TCP(tcp_packet_raw).chksum print("scapy自动计算的TCP校验和为:%04x" % scapy_chksum_tcp) # 手动计算TCP校验和 tcp_packet.chksum = 0 packet = ip_packet / tcp_packet tcp_raw = raw(packet)[20:] self_chksum = in4_chksum(socket.IPPROTO_TCP, packet[IP], tcp_raw) print("手动计算的TCP校验和为:%04x" % self_chksum) if self_chksum == scapy_chksum_tcp: print("TCP验证和正确") else: print("TCP验证和不正确") tcp_packet.chksum = scapy_chksum_tcp self.entries[7].delete(0, END) self.entries[7].insert(0, hex(scapy_chksum_tcp)) self.entries[15].delete(0, END) self.entries[15].insert(0, hex(scapy_chksum_IP)) tcp_packet.show() self.resultText.insert('end', tcp_packet.summary() + '\n') self.resultText.insert('end', str(tcp_packet) + '\n') return Ether() / ip_packet / tcp_packet except Exception as e: print(e.with_traceback()) finally: pass
def getUDPPacket(self): """ 构造UDP数据包 :param self: :return: """ try: ip_packet = IP() ip_packet.version = int(self.entries[3].get()) ip_packet.ihl = int(self.entries[4].get()) ip_packet.tos = int(self.entries[5].get()) ip_packet.id = int(self.entries[6].get()) # ip_packet.flags = int(self.entries[7].get()) ip_packet.frag = int(self.entries[8].get()) ip_packet.ttl = int(self.entries[9].get()) # ip_packet.chksum = self.entries[10].get() ip_packet.src = self.entries[11].get() ip_packet.dst = self.entries[12].get() udp_packet = UDP() udp_packet.sport = int(self.entries[0].get()) udp_packet.dport = int(self.entries[1].get()) # udp_packet.chksum = int(self.entries[2].get()) # scapy自动计算IP、UDP校验和 # 获得数据包的二进制值 pkg_raw = raw(ip_packet / udp_packet) udp_packet_raw = pkg_raw[20:] # 构造数据包,自动计算校验和 scapy_chksum_IP = IP(pkg_raw).chksum scapy_chksum_udp = UDP(udp_packet_raw).chksum print("scapy自动计算的UDP校验和为:%04x" % scapy_chksum_udp) # 手动计算UDP校验和 udp_packet.chksum = 0 packet = ip_packet / udp_packet udp_raw = raw(packet)[20:] self_chksum = in4_chksum(socket.IPPROTO_UDP, packet[IP], udp_raw) print("手动计算的UDP校验和为:%04x" % self_chksum) if self_chksum == scapy_chksum_udp: print("UDP验证和正确") else: print("UDP验证和不正确") udp_packet.chksum = scapy_chksum_udp self.entries[2].delete(0, END) self.entries[2].insert(0, hex(scapy_chksum_udp)) self.entries[10].delete(0, END) self.entries[10].insert(0, hex(scapy_chksum_IP)) udp_packet.show() self.resultText.insert('end', udp_packet.summary() + '\n') self.resultText.insert('end', str(udp_packet) + '\n') return Ether() / ip_packet / udp_packet except Exception as e: print(e.with_traceback()) finally: pass
def foo(in_filename, out_filename): # open the input file for reading f = PcapReader(in_filename) # open the output file for writing o = PcapWriter(out_filename) # read the first packet from the input file p = f.read_packet() # while we haven't processed the last packet while p: layer = p.firstlayer() while not isinstance(layer, NoPayload): if (type(layer) is IPv6): new_layer = IP() del new_layer.ihl new_layer.ttl = layer.hlim new_layer.proto = layer.nh new_layer.src = ".".join(map(str, six2four(layer.src))) new_layer.dst = ".".join(map(str, six2four(layer.dst))) new_layer.add_payload(layer.payload) prev_layer = layer.underlayer del layer prev_layer.remove_payload() prev_layer.add_payload(new_layer) if type(prev_layer) is Ether: prev_layer.type = ETH_P_IP layer = new_layer if layer.default_fields.has_key('chksum'): del layer.chksum if layer.default_fields.has_key('len'): del layer.len # advance to the next layer layer = layer.payload # write the packet we just dissected into the output file o.write(p) # read the next packet p = f.read_packet() # close the input file f.close() # close the output file o.close()
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/env python3 from scapy.all import * from scapy.layers.inet import IP, ICMP """ Finding the ttl to 8.8.8.8. """ a = IP(dst="8.8.8.8") a.ttl = 20 b = ICMP() send(a / b)
import socket from scapy.all import * import sys from scapy.layers.inet import IP, ICMP, Ether, conf from scapy.layers.l2 import ARP a = IP(ttl=10) print(a) a.dst = '172.217.172.142' print(a.src, '\n', a.dst) print(a.ttl) a.ttl = 128 print(a.ttl) del a.ttl print(a.ttl)
from scapy.layers.inet import IP, ICMP from scapy.sendrecv import send a=IP() a.dst="8.8.8.8" for ttl in range(1,20): a.ttl=ttl b = ICMP() p = a / b send(p)
for j in range(len(list_of_network_addresses)): if j != 0: packet_ihl = IP(dst=list_of_network_addresses[j]) / TCP(dport=22) packet_tos = IP(dst=list_of_network_addresses[j]) / TCP(dport=22) packet_len = IP(dst=list_of_network_addresses[j]) / TCP(dport=22) packet_id = IP(dst=list_of_network_addresses[j]) / TCP(dport=22) packet_frag = IP(dst=list_of_network_addresses[j]) / TCP(dport=22) packet_ttl = IP(dst=list_of_network_addresses[j]) / TCP(dport=22) packet_proto = IP(dst=list_of_network_addresses[j]) / TCP(dport=22) packet_ihl.ihl = 18 packet_tos.tos = 18 packet_len.len = 18 packet_id.id = 18 packet_frag.frag = 18 packet_ttl.ttl = 18 packet_proto.proto = 18 answer_ihl = sr(packet_ihl, timeout=4) answer_tos = sr(packet_tos, timeout=4) answer_len = sr(packet_len, timeout=4) answer_id = sr(packet_id, timeout=4) answer_frag = sr(packet_frag, timeout=4) answer_ttl = sr(packet_ttl, timeout=4) answer_proto = sr(packet_proto, timeout=4) if len(answer_ihl[0]) != 0: if len( nx.dijkstra_path(graph_ihl, list_of_network_addresses[0], list_of_network_addresses[j])) == 2: graph_ihl.add_edge(str(list_of_network_addresses[0]),
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