2010-03-24 3 views
0

SharpPcap 라이브러리를 사용하여 패킷을 캡처하려고합니다. 패킷 세부 정보를 반환 할 수 있지만 패킷 내용이 무엇인지 알아보기 위해 문제가 있습니다.SharpPcap - 패킷 캡처 문제가 발생했습니다.

.Data를 사용하여 패킷을 반환하고 메시지를 사용할 때 반환합니다 (System.Byte []). 나는 비트를 사용할 필요가 있으므로

데이터 바이트 배열입니다 내가 답을 발견

string packetData; 
     private void packetCapturingThreadMethod() 
      { 

      Packet packet = null; 
      int countOfPacketCaptures = 0; 

      while ((packet = device.GetNextPacket()) != null) 
       { 

       packet = device.GetNextPacket(); 
       if (packet is TCPPacket) 
        { 
        TCPPacket tcp = (TCPPacket)packet; 
        myPacket tempPacket = new myPacket(); 

        tempPacket.packetType = "TCP"; 
        tempPacket.sourceAddress = Convert.ToString(tcp.SourceAddress); 
        tempPacket.destinationAddress = Convert.ToString(tcp.DestinationAddress); 
        tempPacket.sourcePort = Convert.ToString(tcp.SourcePort); 
        tempPacket.destinationPort = Convert.ToString(tcp.DestinationPort); 
        tempPacket.packetMessage = Convert.ToString(tcp.Data); 
        packetsList.Add(tempPacket); 

        packetData = 
         "Type= TCP" + 
         " Source Address = "+ Convert.ToString(tcp.SourceAddress)+ 
         " Destination Address =" +Convert.ToString(tcp.DestinationAddress)+ 
         " SourcePort =" + Convert.ToString(tcp.SourcePort)+ 
         " SourcePort =" +Convert.ToString(tcp.DestinationPort)+ 
         " Messeage =" + Convert.ToString(tcp.Data); 
        txtpackets.Invoke(new UpdatetxtpacketsCallback(this.Updatetxtpackets), 
      new object[] { packetData }); 


        string[] row = { packetsList[countOfPacketCaptures].packetType, packetsList[countOfPacketCaptures].sourceAddress, packetsList[countOfPacketCaptures].destinationAddress, packetsList[countOfPacketCaptures].sourcePort, packetsList[countOfPacketCaptures].destinationPort, packetsList[countOfPacketCaptures].packetMessage }; 
        try { //dgwPacketInfo.Rows.Add(row); countOfPacketCaptures++; 
        //lblCapturesLabels.Text = Convert.ToString(countOfPacketCaptures); 
        } 
        catch (Exception e) { } 

        } 
       else if (packet is UDPPacket) 
        { 

        UDPPacket udp = (UDPPacket)packet; 


        myPacket tempPacket = new myPacket(); 

        tempPacket.packetType = "UDP"; 
        tempPacket.sourceAddress = Convert.ToString(udp.SourceAddress); 
        tempPacket.destinationAddress = Convert.ToString(udp.DestinationAddress); 
        tempPacket.sourcePort = Convert.ToString(udp.SourcePort); 
        tempPacket.destinationPort = Convert.ToString(udp.DestinationPort); 
        tempPacket.packetMessage = udp.Data.ToArray() + "\n"; 
        packetsList.Add(tempPacket); 

        packetData = 
         "Type= UDP" + 
         " Source Address = "+ Convert.ToString(udp.SourceAddress)+ 
         " Destination Address =" +Convert.ToString(udp.DestinationAddress)+ 
         " SourcePort =" + Convert.ToString(udp.SourcePort)+ 
         " SourcePort =" +Convert.ToString(udp.DestinationPort)+ 
         " Messeage =" + udp.Data.ToArray() + "\n"; 
        string[] row = { packetsList[countOfPacketCaptures].packetType, packetsList[countOfPacketCaptures].sourceAddress, packetsList[countOfPacketCaptures].destinationAddress, packetsList[countOfPacketCaptures].sourcePort, packetsList[countOfPacketCaptures].destinationPort, packetsList[countOfPacketCaptures].packetMessage }; 
        try { 
         //dgwPacketInfo.Rows.Add(row); 
        //countOfPacketCaptures++; 
        //lblCapturesLabels.Text = Convert.ToString(countOfPacketCaptures); 
         txtpackets.Invoke(new UpdatetxtpacketsCallback(this.Updatetxtpackets), 
       new object[] { packetData }); 

        } 
        catch (Exception e) { } 


        } 


       } 
      } 

답변

0

파서는

내가 Packet.Net 코드를 보았다 (SharpPcap에 대한 해석 인) 및 모든 필드는 일반적으로에 저장됩니다 ... 그 복잡하지 사용 된 형식.

IP 주소는 System.Net.IPAddress 형식으로 저장되므로 .ToString을 호출하여 점선이 제대로 포함 된 텍스트 문자열을 얻을 수 있습니다.

포트 번호는 다른 정수와 동일하게 인쇄 할 수있는 ushort로 저장됩니다.

이진 형식으로 해석되어야하는 유일한 부분은 다음 계층에서 어떤 프로토콜이 사용되는지에 따라 변경되기 때문에 데이터 필드입니다. SharpPcap/Packet.Net은 이미 대부분의 작업을 수행하며 필드는 프로토콜 사양에있는 양식과 가장 편리하거나 동일한 양식으로 저장됩니다. Intellisense를 사용하여 필드의 유형을 확인하고 익숙하지 않은 경우 (예 : System.Net.IPAddress 또는 System.NetworkInformation.PhysicalAddress (MAC 주소의 경우)) google에 그냥 입력하십시오.

관련 문제