2017-02-26 93 views
-1

我的代碼有問題。 我寫一個簡單的TCP數據包嗅探器,我需要顯示的TCP數據包的標誌與此代碼:如何在python中使用tcp標誌

import socket 
import struct 


server = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_TCP) 
     while True: 
       packet = server.recvfrom(65565) 
       packet = packet[0] 
       print "Packing data: " 
       print "\n", packet 

       ip_header = packet[0:20] 
       ip_unpack = struct.unpack('!BBHHHBBH4s4s' , ip_header) 
       print "\n Unpack ip header" 
       print ip_unpack 

       version_ihl = ip_unpack[0] 
       version = version_ihl >> 4 
       ihl = version_ihl & 0xF 
       print ihl 
       ip_ihl = ihl * 4 
       print ip_ihl 

       def getflags(packet): 
         Flag_URG = {1: "URG-Urgent flag)"} 
         Flag_ACK = {1: "ACK-Acknowledgment flag"} 
         Flag_PSH = {1: "PSH-Push flag"} 
         Flag_RST = {1: "RST-Reset flag"} 
         Flag_SYN = {1: "SYN-Synchronize flag"} 
         Flag_FIN = {1: "FIN-End of data flag"} 

         URG = packet & 0x020 
         URG >>= 5 
         ACK = packet & 0x010 
         ACK >>= 4 
         PSH = packet & 0x008 
         PSH >>= 3 
         RST = packet & 0x004 
         RST >>= 2 
         SYN = packet & 0x002 
         SYN >>= 1 
         FIN = packet & 0x001 
         FIN >>= 0 


         new_line = "\n" 

         Flags = Flag_URG[URG] + new_line + Flag_ACK[ACK] + new_line + Flag_PSH[PSH] + new_line + Flag_RST[RST] + new_line + Flag_SYN[SYN] + new_line + Flag_FIN[FIN] 
         return Flags 


      #tcp 
      tcp_header = packet[ip_ihl:ip_ihl+20] 
      tcp_unpack = struct.unpack("!HHLLBBHHH", tcp_header) 
      print "\nUnpack TCP header: " 
      print "\n", tcp_unpack 

      tcp_length = tcp_unpack[4] >> 4 

      print "source port: ", tcp_unpack[0] 
      print "destination port: ", tcp_unpack[1] 
      print "sequence number: ", tcp_unpack[2] 
      print "ack number: ", tcp_unpack[3] 
      print "dataoffset: ", tcp_unpack[4] 
      print "tcp length: ", tcp_length 
      print "flag: ", getflags(tcp_unpack[3]) 

      header_size = ip_ihl + tcp_length * 4 
      data_size = len(packet) - header_size 
      data = packet[header_size:] 
      print "data:" 
      print "\n", data 

,但我有此錯誤:

Traceback (most recent call last): 
File "tcp.py", line 67, in <module> 
print "flag: ", getflags(tcp_unpack[3]) 
File "tcp.py", line 49, in getflags 
Flags = Flag_URG[URG] + new_line + Flag_ACK[ACK] + new_line + Flag_PSH[PSH] + new_line + Flag_RST[RST] + new_line + Flag_SYN[SYN] + new_line + Flag_FIN[FIN] 
KeyError: 0L 

如何解決這一問題?以及如何在tcp包中使用flags和ecn?

回答

0

你得到了KeyError,因爲所有的字典對象只有1作爲鍵。 (Flag_URG,Flag_ACK ...)如果packet中有任何標誌丟失,則0將用作密鑰,以便發生錯誤。

添加0避免錯誤:

    Flag_URG = {0:"",1: "URG-Urgent flag"} 
        Flag_ACK = {0:"",1: "ACK-Acknowledgment flag"} 
        Flag_PSH = {0:"",1: "PSH-Push flag"} 
        Flag_RST = {0:"",1: "RST-Reset flag"} 
        Flag_SYN = {0:"",1: "SYN-Synchronize flag"} 
        Flag_FIN = {0:"",1: "FIN-End of data flag"} 

看來tcp_unpack[3]是ACK號,所以print "flag: ", getflags(tcp_unpack[3])看起來很奇怪。

嘗試將其更改爲print "flag: ", getflags(tcp_unpack[5])

相關問題