google / google/gopacket

Sending packets isn't supported on the "any" device

Open
#844 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
Go
Stars
6.8k
Forks
1.2k
PR merge metrics
No merged PRs in 30d

Description

I'm running with some following codes, it will listen on "any" device and capture each TCP layer packet, and try to send RST packet to abort the TCP connection (yes, works like tcpkill!)

But it didn't work, and after captured the packet, it prints out one line error log and exit:

```
Sending packets isn't supported on the "any" device
```

The reproduce steps:

```
# listen on 5555
nc -l -p 5555

# open a new terminal, and try to connect to 5555 via port 6666
nc 172.17.0.2 5555 -p 7777

# and then, run the code (let's call it faketcpkill):
./faketcpkill

#and it will print out logs below:
172.17.0.2:5555(personal-agent) -> 172.17.0.2:7777(cbt)
2020/12/13 02:43:05 send 172.17.0.2:7777(cbt) > 172.17.0.2:5555(personal-agent) [RST] seq 2343069003
2020/12/13 02:43:05 Sending packets isn't supported on the "any" device
```

faketcpkill is simple, and its code shown below:

```
package main

import (
"fmt"
"log"
"net"

"github.com/google/gopacket"
"github.com/google/gopacket/layers"
"github.com/google/gopacket/pcap"
)

func main() {
if handle, err := pcap.OpenLive("any", 1600, true, pcap.BlockForever); err != nil {
panic(err)
} else if err := handle.SetBPFFilter("tcp"); err != nil { // optional
panic(err)
} else {
packetSource := gopacket.NewPacketSource(handle, handle.LinkType())
for packet := range packetSource.Packets() {
err := handlePacket(handle, packet) // Do something with a packet here.
if err != nil {
log.Fatal(err)
}
}
}
}

func sendRST(device gopacket.SerializableLayer, srcIP, dstIP net.IP, srcPort, dstPort layers.TCPPort, seq uint32, handle *pcap.Handle) error {
log.Printf("send %v:%v > %v:%v [RST] seq %v", srcIP.String(), srcPort.String(), dstIP.String(), dstPort.String(), seq)

iPv4 := layers.IPv4{
SrcIP: srcIP,
DstIP: dstIP,
Version: 4,
TTL: 64,
Protocol: layers.IPProtocolTCP,
}

tcp := layers.TCP{
SrcPort: srcPort,
DstPort: dstPort,
Seq: seq,
RST: true,
}

if err := tcp.SetNetworkLayerForChecksum(&iPv4); err != nil {
return err
}

buffer := gopacket.NewSerializeBuffer()
options := gopacket.SerializeOptions{
FixLengths: true,
ComputeChecksums: true,
}
if err := gopacket.SerializeLayers(buffer, options, device, &iPv4, &tcp); err != nil {
return err
}

err := handle.WritePacketData(buffer.Bytes())
if err != nil {
return err
}

return nil
}

func handlePacket(handle *pcap.Handle, packet gopacket.Packet) error {
ethLayer := packet.Layer(layers.LayerTypeEthernet)
eth, _ := ethLayer.(*layers.Ethernet)
if eth == nil {
// try to broadcast.
eth = &layers.Ethernet{
EthernetType: layers.EthernetTypeIPv4,
SrcMAC: net.HardwareAddr{0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
DstMAC: net.HardwareAddr{0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
}
}
if ipLayer := packet.Layer(layers.LayerTypeIPv4); ipLayer != nil {
ip, _ := ipLayer.(*layers.IPv4)

if tcpLayer := packet.Layer(layers.LayerTypeTCP); tcpLayer != nil {
// Get actual TCP data from this layer
tcp, _ := tcpLayer.(*layers.TCP)
fmt.Printf("%s:%s -> %s:%s\n", ip.SrcIP, tcp.SrcPort, ip.DstIP, tcp.DstPort)

// Send RST
seq := tcp.Ack + uint32(1)*uint32(tcp.Window)
err := sendRST(eth, ip.DstIP, ip.SrcIP, tcp.DstPort, tcp.SrcPort, seq, handle)
if err != nil {
return err
}
}
}
return nil
}
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.