使用gopacket从原始字节创建数据包,并将其写入pcap文件。

huangapple go评论79阅读模式
英文:

Create a packet from raw bytes and write to pcap file using gopacket

问题

我想使用gopacket从原始字节创建一个数据包,并将数据包写入pcap文件。

我已经按照以下方式从原始字节创建了一个UDP数据包。

package main

import (
    "fmt"
    "net"
    "log"
    "os"
    "time"
    "github.com/google/gopacket"
    "github.com/google/gopacket/layers"
    "github.com/google/gopacket/pcap"
    "github.com/google/gopacket/pcapgo"
)

var (
    device       string = "eth0"
    snapshot_len int32  = 65535
    promiscuous  bool   = false
    err          error
    timeout      time.Duration = 30 * time.Second
    handle       *pcap.Handle
    buffer       gopacket.SerializeBuffer
    options      gopacket.SerializeOptions
)

func main() {
    ethernetLayer := &layers.Ethernet{
        SrcMAC: net.HardwareAddr{0xFF, 0xAA, 0xFA, 0xAA, 0xFF, 0xAA},
        DstMAC: net.HardwareAddr{0xBD, 0xBD, 0xBD, 0xBD, 0xBD, 0xBD},
    }
    ipLayer := &layers.IPv4{
        SrcIP: net.IP{127,0,0,1},
        DstIP: net.IP{8,8,8,8},
    }
    udpLayer := &layers.UDP{
        SrcPort: 51190,
        DstPort: 4789,
    }
    rawBytes := []byte{10, 20, 30}
    buffer = gopacket.NewSerializeBuffer()
    gopacket.SerializeLayers(buffer, options,
        ethernetLayer,
        ipLayer,
        udpLayer,
        gopacket.Payload(rawBytes),
    )
    outgoingPacket := buffer.Bytes()
    fmt.Println(outgoingPacket)
    ethpacket := gopacket.NewPacket(outgoingPacket,
        layers.LayerTypeEthernet,
        gopacket.Default )

我被困在如何将这些数据包写入pcap文件的问题上。这是代码的一部分。

    f, err := os.Create("temp.pcap")
    if err != nil {
        log.Fatal(err)
        os.Exit(1)
    }
    defer f.Close()

    //handle, err = pcap.OpenLive(device, snapshot_len, promiscuous, timeout)
    //if err != nil {
    //        log.Fatal(err)
    //}
    //defer handle.Close()

    //handle.WritePacketData(outgoingPacket)
    w := pcapgo.NewWriter(f)
    w.WriteFileHeader(uint32(snapshot_len), layers.LinkTypeEthernet)
    w.WritePacket(ethpacket.Metadata().CaptureInfo, ethpacket.Data())

这是我遇到的错误。请帮助我如何继续进行。

[root@centos-sqa-blr01 gopacket]# go run create_pcap.go
#command-line-arguments
./create_pcap.go:75:26: packet.MetaData undefined (type gopacket.Packet has no field or method MetaData, but does have Metadata)

英文:

I want to create a packet from raw bytes using gopacket and write the packets to a pcap file.

I created a UDP packet from raw bytes as below.

package main
import (
"fmt"
"net"
"log"
"os"
"time"
//"encoding/hex"
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
"github.com/google/gopacket/pcap"
"github.com/google/gopacket/pcapgo"
)
var (
device       string = "eth0"
snapshot_len int32  = 65535
promiscuous  bool   = false
err          error
timeout      time.Duration = 30 * time.Second
handle       *pcap.Handle
buffer       gopacket.SerializeBuffer
options      gopacket.SerializeOptions
)
func main() {
ethernetLayer := &layers.Ethernet{
SrcMAC: net.HardwareAddr{0xFF, 0xAA, 0xFA, 0xAA, 0xFF, 0xAA},
DstMAC: net.HardwareAddr{0xBD, 0xBD, 0xBD, 0xBD, 0xBD, 0xBD},
}
ipLayer := &layers.IPv4{
SrcIP: net.IP{127,0,0,1},
DstIP: net.IP{8,8,8,8},
}
udpLayer := &layers.UDP{
SrcPort: 51190,
DstPort: 4789,
}
rawBytes := []byte{10, 20, 30}
buffer = gopacket.NewSerializeBuffer()
gopacket.SerializeLayers(buffer, options,
ethernetLayer,
ipLayer,
udpLayer,
gopacket.Payload(rawBytes),
)
outgoingPacket := buffer.Bytes()
fmt.Println(outgoingPacket)
ethpacket := gopacket.NewPacket(outgoingPacket,
layers.LayerTypeEthernet,
gopacket.Default )

I am struck how to write these packets to a pcap file. This is that part of the code.

        f, err := os.Create("temp.pcap")
if err != nil {
log.Fatal(err)
os.Exit(1)
}
defer f.Close()
//handle, err = pcap.OpenLive(device, snapshot_len, promiscuous, timeout)
//if err != nil {
//        log.Fatal(err)
//}
//defer handle.Close()
//handle.WritePacketData(outgoingPacket)
w := pcapgo.NewWriter(f)
w.WriteFileHeader(uint32(snapshot_len), layers.LinkTypeEthernet)
w.WritePacket(ethpacket.Metadata().CaptureInfo, ethpacket.Data())

This is the error I am getting. Please help how to proceed further

[root@centos-sqa-blr01 gopacket]# go run create_pcap.go
#command-line-arguments
./create_pcap.go:75:26: packet.MetaData undefined (type gopacket.Packet has no field or method MetaData, but does have Metadata)

答案1

得分: 1

请注意错误信息:“MetaData”不是“Metadata”。

猜测你需要修改:

-            w.WritePacket(packet.MetaData().CaptureInfo, ethpacket.Data())
+            w.WritePacket(packet.Metadata().CaptureInfo, ethpacket.Data())

更新:

如果你只想创建和写入一个数据包,请注释掉以下代码。这对我来说是有效的。

	// handle, err = pcap.OpenLive(device, snapshot_len, promiscuous, timeout)
	// if err != nil {
	// 	log.Fatal(err)
	// }
	// defer handle.Close()
英文:

Notice the error info: "MetaData" is not "Metadata".

Guess you need to modify:

-            w.WritePacket(packet.MetaData().CaptureInfo, ethpacket.Data())
+            w.WritePacket(packet.Metadata().CaptureInfo, ethpacket.Data())

UPDATE:

If u just want to create and write a packet, comment the following codes. It works for me.

	// handle, err = pcap.OpenLive(device, snapshot_len, promiscuous, timeout)
	// if err != nil {
	// 	log.Fatal(err)
	// }
	// defer handle.Close()

huangapple
  • 本文由 发表于 2021年8月20日 14:42:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/68857945.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定