因为我有的只是pyload里面的字符串数据。没有协议头的部分,所以只能自己创建协议头的数据。
主要使用的就是go的gopacket和gopcap包。下面直接上代码
package main
import (
"bytes"
"encoding/base64"
"fmt"
"log"
"math/rand"
"time"
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
"github.com/google/gopacket/pcapgo"
)
// writeToPCAP_HTTP 函数用于将HTTP数据包写入到 PCAP 文件中
func writeToPCAP_HTTP(w *pcapgo.Writer, requestData []byte, responseData []byte) error {
// 每个请求随机选择本地端口号
srcPort := layers.TCPPort(rand.Intn(16383) + 49152) // 49152 - 65535 是动态/私有端口范围
dstPort := layers.TCPPort(80)
// 创建以太网帧
ethernetLayer := &layers.Ethernet{
SrcMAC: []byte{
0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
DstMAC: []byte{
0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
EthernetType: layers.EthernetTypeIPv4,
}
// 创建IP数据包(请求)
requestIP := &layers.IPv4{
Version: 4,
IHL: 5,
TTL: 64,
Protocol: layers.IPProtocolTCP,
SrcIP: []byte{
127, 0, 0, 1},
DstIP: []byte{
127, 0, 0, 1},
}
// 计算IP数据包长度(请求)
//requestIP.Length = uint16(len