我一直在尝试编写一个脚本来嗅探 HTTP header 。到目前为止,我已经将套接字绑定(bind)到端口 80 并且似乎收到了数据包,但我无法将它们转换为字符串形式。所有输出都是“E”连续。我之前将字节更改为十六进制,似乎有一些数据传入,但当前代码无法将字节更改为字符串。有没有其他方法可以解码将给出正确字符串的字节?
byte[] input = BitConverter.GetBytes(1);
byte[] buffer = new byte[4096];
Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.IP);
s.Bind(new IPEndPoint(IPAddress.Parse(strIP), 80));
s.IOControl(IOControlCode.ReceiveAll, input, null);
int bytes;
bytes = s.Receive(buffer);
while (bytes > 0)
{
log(System.Text.Encoding.ASCII.GetString(buffer, 0, bytes));
bytes = s.Receive(buffer);
}
最佳答案
当您使用原始套接字嗅探数据时,您正在接收 Internet 协议(protocol) (IP) 数据包。每个 IP 数据包都以 IP header 开头。此 header 的长度通常为 20 个字节,但也可以更长。在 IP header 之后是传输层的 header ,例如 Transmission Control Protocol (TCP) header 或 User Datagram Protocol (UDP) header 。在此 header 之后是您要查找的数据,即HTTP。因此,在解析数据时,您需要先跳过 IP header 和传输层 header 。
关于C# 获取数据包数据,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/1850948/