def setUp(self): # TCP - sport: 60655, dport: 80, sec: 0, HLen: 40, Flags: 0x02, win_size: 5840 # cksum: 0x64cb, Options: 0x20 self.frame = b'\xec\xef\x00\x50\xa8\xbd\xea\x4c\x00\x00\x00\x00\xa0\x02\x16\xd0' \ b'\x64\xcb\x00\x00\x02\x04\x05\xb4\x04\x02\x08\x0a\x00\xdc\xd6\x12' \ b'\x00\x00\x00\x00\x01\x03\x03\x06' self.tcp = TCP(self.frame)
def run(self): try: frame = '\x12\x34\x00\x50\x00\x00\x00\x01\x00\x00\x00\x00' \ '\x60\x00\x00\x00\x8d\x5c\x00\x00\x02\x00\x00\x00' tcp = TCP(frame) except ImpactPacketException as e: if str(e) != "'TCP Option length is too low'": raise e except: pass
def reply(ip, tcp): buf = tcp.get_packet()[:20] buf = [n for n in buf] buf[12] = 80 buf = bytes(buf) tcp = TCP(buf) ip.contains(tcp) tcp.swapSourceAndDestination() return ip, tcp
def run(self): try: frame = '\x12\x34\x00\x50\x00\x00\x00\x01\x00\x00\x00\x00' \ '\x60\x00\x00\x00\x8d\x5c\x00\x00\x02\x00\x00\x00' tcp = TCP(frame) #except Exception,e: # print "aaaaaaaaaaaaaaa" # print e #except Exception,e: except ImpactPacketException, e: if str(e) != "'TCP Option length is too low'": raise e
buf = [n for n in buf] buf[12] = 80 buf = bytes(buf) tcp = TCP(buf) ip.contains(tcp) tcp.swapSourceAndDestination() return ip, tcp while True: data, addr = server.recvfrom(4096) #print(addr, data) ip = IP(data) ip_len = ip.get_size() tcp = TCP(data[ip_len:]) if tcp.get_th_dport() == 1234: print('state', state) #buf = tcp.get_packet() #print([hex(n)[2:] for n in buf]) print(tcp, tcp.get_th_seq(), tcp.get_th_ack()) if state == 0: ip, tcp = reply(ip, tcp) tcp.set_th_ack(tcp.get_th_seq() + 1) tcp.set_th_seq(0) #print('###', tcp.get_th_seq()) tcp.set_ACK() tcp.calculate_checksum() buf = ip.get_packet() print(tcp, tcp.get_th_seq(), tcp.get_th_ack()) server.sendto(buf, ('127.0.0.1', 0))