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
#traceroute traceroute(["www.google.com"], maxttl=20) #traceroute con puerto 23 traceroute (["10.1.99.2"],dport=23,maxttl=20) #paquete por capas IP() IP()/TCP() Ether()/IP()/TCP() IP()/TCP()/"GET / HTTP/1.1\r\n\r\n" Ether()/IP(dst="api.tidex.com")/TCP(dport=443)/"GET /api/3/ticker/eth_btc HTTP/1.1\r\nHost: api.tidex.com\r\nConnection: close\r\n\r\n" Ether()/IP()/IP()/UDP() #armado de paquetes por capas a=Ether(); a.show() b=IP(); b.show() c=TCP(); c.show() d=sr(a/b/c,timeout=2) #enviar paquete por capas ans,unans=sr(IP(dst="api.tidex.com")/TCP(sport=RandShort(),dport=443)/"GET /api/3/ticker/eth_btc HTTP/1.1\r\nHost: api.tidex.com\r\nConnection: close\r\n\r\n") #############HTTP ##super importante #https://stackoverflow.com/questions/9058052/unwanted-rst-tcp-packet-with-scapy #iptables -A OUTPUT -p tcp --tcp-flags RST RST -s 192.168.43.55 -j DROP load_layer("http") req = HTTP()/HTTPRequest( Accept_Encoding=b'gzip, deflate', Cache_Control=b'no-cache', Connection=b'keep-alive', Host=b'www.secdev.org',