2012-03-03 238 views
0

我編寫了一個類似於嗅探器的應用程序,它可以在端口上偵聽並向用戶顯示在您的計算機和Internet之間傳輸的信息,如源和目標ip。寫端口監聽器

我該如何改變它來檢索url地址而不是ip地址有可能嗎? 我使用套接字工作。

private byte[] byUDPData = new byte[4096]; //Data carried by the UDP packet 

    public UDPHeader(byte [] byBuffer, int nReceived) 
    { 
     MemoryStream memoryStream = new MemoryStream(byBuffer, 0, nReceived); 
     BinaryReader binaryReader = new BinaryReader(memoryStream); 

     //The first sixteen bits contain the source port 
     usSourcePort = (ushort)IPAddress.NetworkToHostOrder(binaryReader.ReadInt16()); 

     //The next sixteen bits contain the destination port 
     usDestinationPort = (ushort)IPAddress.NetworkToHostOrder(binaryReader.ReadInt16()); 

     //The next sixteen bits contain the length of the UDP packet 
     usLength = (ushort)IPAddress.NetworkToHostOrder(binaryReader.ReadInt16()); 

     //The next sixteen bits contain the checksum 
     sChecksum = IPAddress.NetworkToHostOrder(binaryReader.ReadInt16());    

     //Copy the data carried by the UDP packet into the data buffer 
     Array.Copy(byBuffer, 
        8,    
        byUDPData, 
        0, 
        nReceived - 8); 
    } 
+1

您必須重新組裝應用程序級別的數據並從HTTP請求中提取URL。這是一項非常大的工作(因爲您必須複製幾個協議層的功能),並且您應該認真考慮調整(或使用)現有工具。你的問題是:「我有一把錘子和一些2x4s,我想建一座吊橋。」 – 2012-03-03 11:34:36

+0

重要的事實是,http不會運行udp ... – 2012-03-03 13:37:13

回答

1

通常不需要那樣做。看看Wire Shark。如果你無法準確找到你需要的東西,你可以編寫自己的協議分析器和其他加載項。

另外,看看WinPCap,它可以讓你的應用程序訪問Wire Shark的相同嗅探能力。

0

當你說

URL地址,而不是IP地址

你的意思是DNS名稱?在這種情況下,您需要使用dns類和Dns.GetHostEntry方法從IP地址查找名稱。

如果您正在尋找URL信息,那麼就像@David Schwartz所說的那樣,您將不得不解碼您捕獲的所有數據,這些數據可能有也可能沒有網址。