2016-07-20 19 views
-2

を解凍するにはあまりにも多くの値Iは、端子を介してそれを実行したときしかし、私は次のエラーを取得しています:Pythonの:私はPythonでプログラムに取り組んでいます

Traceback (most recent call last): 
    File "packetSniffer.py", line 120, in <module> 
    main() 
    File "packetSniffer.py", line 27, in main 
    (version, headerLength, timeToLive, protocol, source, target, data) = ip(data) 
ValueError: too many values to unpack (expected 7) 

私はすべてに適切な方法をしていますが(I私は自分のコードに間違いがないと思っているからです)。新鮮な目で私の問題を見つけられることを願っています。事前に

import struct 
import textwrap 
import socket 

TAB_1 = '\t - ' 
TAB_2 = '\t\t - ' 
TAB_3 = '\t\t\t - ' 
TAB_4 = '\t\t\t\t - ' 

DATA_TAB_1 = '\t ' 
DATA_TAB_2 = '\t\t ' 
DATA_TAB_3 = '\t\t\t ' 
DATA_TAB_4 = '\t\t\t\t ' 


def main(): 
    connection = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, socket.ntohs(3)) 

    while True: 
     rawData, address = connection.recvfrom(65535) 
     reciever_mac, sender_mac, ethernetProtocol, data = ethernet_frame(rawData) 
     print('\nEthernet Frame: ') 
     print(TAB_1 + 'Destination: {}, Source: {}, Protocol: {}'.format(reciever_mac, sender_mac, ethernetProtocol)) 

     #Make sure you are using Ethernet protocol 8 
     if ethernetProtocol == 8: 
      (version, headerLength, timeToLive, protocol, source, target, data) = ip(data) 
      print(TAB_1 + 'IP Packet:') 
      print(TAB_2 + 'Version: {}, Header Length: {}, TTL: {}'.format(version, headerLength, timeToLive)) 
      print(TAB_2 + 'Protocol: {}, Source: {}, Target: {}'.format(protocol, source, target)) 

      if protocol == 1: 
       icmpType, code, checkSum, data = icmpPackets(data) 
       print(TAB_1 + 'ICMP Packet:') 
       print(TAB_2 + 'Type: {}, Code: {}, Checksum: {},'.format(icmpType, code, checkSum)) 
       print(TAB_2 + 'Data:') 
       print(formatMultiLine(DATA_TAB_3, data)) 

      elif protocol == 6: 
       (sourcePort, destinationPort, sequence, acknowledgement, flagURG, flagACK, flagPSH, flagRST, flagSYN, flagFIN, data) = tcpSegment(data) 
       print(TAB_2 + 'Source Port: {}, Destination Port: {}'.format(sourcePort, destinationPort)) 
       print(TAB_2 + 'Sequence: {}, Acknowledgment: {}'.format(sequence, acknowledgement)) 
       print(TAB_2 + 'Flags:') 
       print(TAB_3 + 'URG: {}, ACK: {}, PSH: {}'.format(flagURG, flagACK, flagPSH)) 
       print(TAB_3 + 'RST: {}, SYN: {}, FIN:{}'.format(flagRST, flagSYN, flagSYN)) 
       print(formatMultiLine(DATA_TAB_3, data)) 

      elif protocol == 17: 
       (sourcePort, destinationPort, length, data) = udpSegment(data) 
       print(TAB_1 + 'UDP Segment:') 
       print(TAB_2 + 'Source Port: {}, Destination Port: {}, Length: {}'.format(sourcePort, destinationPort, length)) 

      else: 
       print(TAB_1 + 'Other IPv4 Data:') 
       print(formatMultiLine(DATA_TAB_2, data)) 

     else: 
      print('Ethernet Data:') 
      print(formatMultiLine(DATA_TAB_1, data)) 


# Unpack ethernet frame 
def ethernet_frame(data): 
    reciever_mac, sender_mac, protocol = struct.unpack('! 6s 6s H', data[:14]) 
    return getMacAddress(reciever_mac), getMacAddress(sender_mac), socket.htons(protocol), data[14:] 

# Convert the Mac address from the jumbled up form from above into human readable format 
def getMacAddress(bytesAddress): 
    bytesString = map('{:02x}'.format, bytesAddress) 
    macAddress = ':'.join(bytesString).upper() 
    return macAddress 

#Unpack IP header data 
def ip_packet(data): 
    versionHeaderLength = data[0] 
    version = versionHeaderLength >> 4 
    headerLength = (versionHeaderLength & 15) * 4 

    timeToLive, protocol, source, target = struct.unpack('! 8x B B 2x 4s 4s', data[:20]) 
    return version, headerLength, timeToLive, protocol, ip(source), ip(target), data[headerLength:] 

#Returns properly formatted IP address 
def ip(address): 
    return '.'.join(map(str, address)) 

#Unpack ICMP packets 
def icmpPackets(data): 
    icmpType, code, checkSum = struct.unpack('! B B H', data[:4]) 
    return icmpType, code, checkSum, data[4:] 

#Unpack TCP segments: 
def tcpSegment(data): 
    (sourcePort, destinationPort, sequence, acknowledgement, offsetReservedFlags) = struct.unpack('! H H L L H', data[:14]) 
    offset = (offsetReservedFlags >> 12) * 4 

    flagURG = (offsetReservedFlags & 32) >> 5 
    flagACK = (offsetReservedFlags & 16) >> 4 
    flagPSH = (offsetReservedFlags & 8) >> 3 
    flagRST = (offsetReservedFlags & 4) >> 2 
    flagSYN = (offsetReservedFlags & 2) >> 1 
    flagFIN = offsetReservedFlags & 1 

    return sourcePort, destinationPort, sequence, acknowledgement, flagURG, flagACK, flagPSH, flagRST, flagSYN, flagFIN, data[offset:] 

#Unpack UDP segments: 
def udpSegment(data): 
    sourcePort, destinationPort, size = struct.unpack('! H H 2x H', data[:8]) 
    return sourcePort, destinationPort, size, data[8:] 

#Breaks down and formats large, multi-lined data 
def formatMultiLine(prefix, string, size = 80): 
    size -= len(prefix) 
    if isinstance(string, bytes): 
     string = ''.join(r'\x{:02X}'.format(byte) for byte in string) 
     if size % 2: 
      size -= 1 

    return '\n'.join([prefix + line for line in textwrap.wrap(string, size)]) 

main() 

ありがとう:

は、ここに私の全体のプログラムです!

+0

So..whatは 'IP(データ)'のですか? –

+0

それは私が持っている別の機能です...プログラムコード全体を投稿したいですか? –

+0

私たちのクリスタルボールを使って 'ip()'が何を返すのか把握すべきでしょうか? –

答えて

0

右誤った行の前に:

(version, headerLength, timeToLive, protocol, source, target, data) = ip(data) 

次を配置する必要があります

print(data) 
print(ip(data)) 

というの出力は、あなた正確かを示す必要があります問題は - これは101のものをデバッグしている、それはほとんどあなたのコードをデバッグしようとすると、情報量が少なくなります。

この場合、ip(data)は、、7より多くのアイテムを返すことがほぼ保証されています。

+0

それはより多くの情報を表示しますが、すべての情報が読めませんhaha –

+0

最終的にそれを想像してください...それはtypo haha​​でした –

0

これはip(data)が7以上のelemsを持つseqを返すようです。これを試してみてください :

(version, headerLength, timeToLive, protocol, source, target, data) = ip(data)[:7] 
+0

それは私にエラーを与えませんが、私はどちらかを探しているデータを与えません –

+1

私はあなたのためにあなたがあなたのproblem.Youを見つけるためにあなたを助けることがあなたが必要とするものを見つける必要があるあなたは何を必要としているのですか? –

0

ここでの問題ip関数は文字列を返しますが、単純に解凍するだけでは抽出できないものを抽出したい場合は、ip関数を再定義してどのように使用するかを変更します。

ところで、より大きなシーケンスの場合には、*を使用して残りのすべてのものを1つだけ入れて、あなたのアンパックリストの変数のうちの1つだけが、誰もが

>>> test="255.255.255.255" 
>>> a,b,c,d,e,f,g,*h=test 
>>> a 
'2' 
>>> b 
'5' 
>>> c 
'5' 
>>> d 
'.' 
>>> h 
['.', '2', '5', '5', '.', '2', '5', '5'] 
>>> 
>>> *a,b,c,d,e,f,g,h=test 
>>> a 
['2', '5', '5', '.', '2', '5', '5', '.'] 
>>> b 
'2' 
>>> c 
'5' 
>>> a,b,*c,d,e,f,g,h=test 
>>> a 
'2' 
>>> b 
'5' 
>>> c 
['5', '.', '2', '5', '5', '.', '2', '5'] 
>>> d 
'5' 
>>>  

とあなたが見ることができるようにアンパックあることを行っているデータODの部分は、文字列を開梱するだけであなたの性格を与えるだろう...

関連する問題