我正在做一个网络嗅探器。我打开了一个在我的主网络接口(192.168.2.11)上侦听的原始套接字。代码并不是必需的,但如果您需要它,我可以提供。然后我从一个接收到的数据包(它应该包含一个IP头,然后是一个UDP头,最后跟着一个数据包)中创建一个MemoryStream
。我转换这个MemoryStream
为BinaryReader
,并使用以下过程解析IP报头解析IP标头中的UDP标头VB.NET
Version and header length BinaryReader.ReadByte() 'As two nibbles make a byte
Type of service BinaryReader.ReadByte()
Total length BinaryReader.ReadInt16()
Identification BinaryReader.ReadInt16()
Flags and offset BinaryReader.ReadInt16() 'As 3 bits + 13 bits is 16
Time to live BinaryReader.ReadByte()
Protocol BinaryReader.ReadByte()
Checksum BinaryReader.ReadInt16()
Source address BinaryReader.ReadInt32()
Destination address BinaryReader.ReadInt32()
(更多处理完成这一数据,但没有改变的BinaryReader
制造)
我再查询Version
字段来查看它是否是17(UDP),如果是我继续下一步,否则我放弃这个过程。
现在我检查Internet标题长度(IHL)是否是5(5 * 32或160位或20字节)。我这样做是为了确保我的程序不会在不必要的IP选项上跳脱。如果不是5,我会放弃这个过程。
IP头解析完美...现在使用完全相同的BinaryReader
,我试图解析封装的UDP头。我使用该代码,这是
Out("Source port?: " & IPAddress.NetworkToHostOrder(bin.ReadInt16()))
Out("Destination port?: " & IPAddress.NetworkToHostOrder(bin.ReadInt16()))
Out("Length?: " & IPAddress.NetworkToHostOrder(bin.ReadInt16()))
Out("Checksum?: " & IPAddress.NetworkToHostOrder(bin.ReadInt16()))
但我总是得到一些荒谬的......这是我的控制台应用程序的输出
Source port?: -364
Destination port?: 53
Length?: 35
Checksum?: -7570
目标看起来不错,53 DNS,然后长度长相不错,但来源?港口如何消极?校验和可能被允许为负数,我不确定。我的代码有什么问题?如果你需要IP报头的输出,它是在这里...
Version: 4 (0100)
Internet header length: 5
Words (32 bits): 5
Octets (8 bits): 20
Bits (1 bit): 160
Type of service: 0x00000000
Precedence: 000 (Routine)
Delay: 0 (Normal Delay)
Throughput: 0 (Normal Throughput)
Reliability: 0 (Normal Reliability)
Total length: 55
Identification: 2866
Flags: 0x00
Reserved: False
Don't Fragment: False
More Fragments: False
Fragment offset: 0
Time to live: 128
Protocol: 17 (UDP)
Header checksum: -21977
Source address: 184723648 (192.168.2.11)
Destination address: 16951488 (192.168.2.1)
Payload length (bits): 288
[UDP端口号从0到65535](https://www.iana.org/assignments/service-names-port-numbers/service-names-port-numbers.xhtml),所以你会想要一个UInt16那里,不是Int16。 –
“_I然后查询'Version'字段以查看它是否为17(UDP),如果是我继续下一步,否则我放弃该过程。”这没有任何意义。 'Version'字段是IP版本,它应该是'4'或'6'。它是'Protocol'字段,应该是'17'。 –
@RonMaupin我不好,在代码中,我实际上是在查询协议,但是我不小心键入了版本。 – Adam