gpt4 book ai didi

go - 将自定义层添加到捕获的数据包失败

转载 作者:数据小太阳 更新时间:2023-10-29 03:22:22 25 4
gpt4 key购买 nike

我正在尝试在 TCP 之上实现我自己的解码层,到目前为止,它仅在我创建没有任何 Eth/IP/TCP header 的数据包并将其层手动设置为我的自定义层时才有效。自定义协议(protocol)的数据在普通的 TCP 负载中。

如何仅将 TCP 层的有效载荷解码为另一层?

package main

import (
"fmt"
"github.com/google/gopacket"
"github.com/google/gopacket/pcap"
)

var (
pcapFile string = "capt.pcap"
handle *pcap.Handle
err error
)

type CustomLayer struct {
SomeByte byte
AnotherByte byte
restOfData []byte
}

var CustomLayerType = gopacket.RegisterLayerType(
2001,
gopacket.LayerTypeMetadata{
"CustomLayerType",
gopacket.DecodeFunc(decodeCustomLayer),
},
)

func (l CustomLayer) LayerType() gopacket.LayerType {
return CustomLayerType
}

func (l CustomLayer) LayerContents() []byte {
return []byte{l.SomeByte, l.AnotherByte}
}

func (l CustomLayer) LayerPayload() []byte {
return l.restOfData
}

func decodeCustomLayer(data []byte, p gopacket.PacketBuilder) error {
p.AddLayer(&CustomLayer{data[0], data[1], data[2:]})

// nil means this is the last layer. No more decoding
return nil
}

func main() {
handle, err = pcap.OpenOffline(pcapFile)
if err != nil {
log.Fatal(err)
}
defer handle.Close()

packetSource := gopacket.NewPacketSource(handle, handle.LinkType())
for packet := range packetSource.Packets() {
tcpLayer := packet.Layer(layers.LayerTypeTCP)
if tcpLayer != nil {
fmt.Println("TCP layer detected.")
tcp, _ := tcpLayer.(*layers.TCP)
fmt.Println("Sequence number: ", tcp.Seq)
customLayer := packet.Layer(CustomLayerType)
if customLayer != nil { // always nil
customLayerContent, _ := customLayer.(*CustomLayer)
// Now we can access the elements of the custom struct
fmt.Println("Payload: ", customLayerContent.LayerPayload())
fmt.Println("SomeByte element:", customLayerContent.SomeByte)
fmt.Println("AnotherByte element:", customLayerContent.AnotherByte)
}
}
fmt.Println()
}
}

大部分代码来自this great post by devdungeon .

最佳答案

因为没有人回应,我现在要自己回答。

基本上我们有 3 个选项来处理这个问题:

  1. 创建一个扩展的 TCP 层来处理我们的额外字节并通过将 layers.LinkTypeMetadata[layers.LinkTypeTCP] 设置为我们的扩展版本来覆盖默认层。 Have a look at this example .

  2. 使用 gopacket.NewPacketfirstLayerDecoder 设置为 CustomLayerType 从 TCP 负载创建一个新数据包并正常解码。

  3. 因为您通常不需要实际的层,而是填充的 CustomLayer 结构,只需编写一个 DecodeBytesToCustomStruct 函数,您就可以在其中传递 TCP 负载。通过这种方式,我们甚至可以从一个数据包有效负载中返回多个结构,否则这是不可能的。

省略上面的所有 CustomLayer 代码。

type CustomStruct struct {
SomeByte byte
AnotherByte byte
restOfData []byte
}

func (customStruct *CustomStruct) DecodeStructFromBytes(data []byte) error {
customStruct.SomeByte = data[0]
customStruct.AnotherByte = data[1]
customStruct.restOfData = data[2:]
return nil
}

在你的 main.go 中

for packet := range packetSource.Packets() {
tcpLayer := packet.Layer(layers.LayerTypeTCP)
if tcpLayer != nil {
tcp, _ := tcpLayer.(*layers.TCP)
if tcp.Payload != nil && len(tcpLayer.Payload) > 0 {
customStruct := CustomStruct{}
customStruct.DecodeStructFromBytes(tcp.Payload)
fmt.Println("SomeByte element:", customStruct.SomeByte)
}
}
}

tcp.Payloadpacket.ApplicationLayer().Payload() 相同

关于go - 将自定义层添加到捕获的数据包失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51593997/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com