我想使用 gopacket 制作自定义 TCP 数据包,然后使用原始套接字发送它们。
这是一个简短易读的示例 go 程序,演示了我想做的事情:
package main
import (
"code.google.com/p/gopacket"
"code.google.com/p/gopacket/examples/util"
"code.google.com/p/gopacket/layers"
"log"
"net"
)
func main() {
defer util.Run()()
// XXX create tcp/ip packet
srcIP := net.ParseIP("127.0.0.1")
dstIP := net.ParseIP("192.168.0.1")
//srcIPaddr := net.IPAddr{
// IP: srcIP,
//}
dstIPaddr := net.IPAddr{
IP: dstIP,
}
ipLayer := layers.IPv4{
SrcIP: srcIP,
DstIP: dstIP,
Protocol: layers.IPProtocolTCP,
}
tcpLayer := layers.TCP{
SrcPort: layers.TCPPort(666),
DstPort: layers.TCPPort(22),
SYN: true,
}
tcpLayer.SetNetworkLayerForChecksum(&ipLayer)
buf := gopacket.NewSerializeBuffer()
opts := gopacket.SerializeOptions{
FixLengths: true,
ComputeChecksums: true,
}
err := gopacket.SerializeLayers(buf, opts, &ipLayer, &tcpLayer)
if err != nil {
panic(err)
}
// XXX end of packet creation
// XXX send packet
ipConn, err := net.ListenPacket("ip4:tcp", "0.0.0.0")
if err != nil {
panic(err)
}
_, err = ipConn.WriteTo(buf.Bytes(), &dstIPaddr)
if err != nil {
panic(err)
}
log.Print("packet sent!\n")
}
但是运行这个程序不起作用...... SerializeLayer 失败。这是恐慌:
恐慌:src IP 127.0.0.1 无效
goroutine 16 [运行]: runtime.panic(0x5bb020, 0xc2090723e0) /home/human/golang-empire/go/src/pkg/runtime/panic.c:279 +0xf5 main.main() /home/human/golang-帝国/gopkg/src/github.com/david415/HoneyBadger/packetSendTest.go:41 +0x464
goroutine 19 [finalizer wait]: runtime.park(0x413cc0, 0x7bc6c0, 0x7bb189) /home/human/golang-empire/go/src/pkg/runtime/proc.c:1369 +0x89 runtime.parkunlock(0x7bc6c0, 0x7bb189) / home/human/golang-empire/go/src/pkg/runtime/proc.c:1385 +0x3b runfinq() /home/human/golang-empire/go/src/pkg/runtime/mgc0.c:2644 +0xcf runtime.goexit() /home/human/golang-empire/go/src/pkg/runtime/proc.c:1445