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

时间:2018-07-30 12:28:46

标签: go gopacket

我正在尝试在TCP之上实现自己的解码层,到目前为止,它仅在创建不带任何Eth / IP / TCP头的数据包并将其层手动设置为我的自定义层时起作用。自定义协议的数据位于普通的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

1 个答案:

答案 0 :(得分:0)

由于没有人回答,我现在要亲自回答。

基本上,我们有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()