Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix windows packet capture #943

Merged
merged 1 commit into from
Jun 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,16 @@ release-mac:
rm -rf /tmp/gor-build

release-windows:
echo $(pwd)
docker run -it --rm \
-v `pwd`:/go/src/github.com/buger/goreplay \
-w /go/src/github.com/buger/goreplay \
-e CGO_ENABLED=1 \
docker.elastic.co/beats-dev/golang-crossbuild:1.16.4-main \
--build-cmd "VERSION=make build" \
--build-cmd "make VERSION=$(VERSION) build" \
-p "windows/amd64"

mv ./gor ./gor-$(VERSION)$(PREFIX).exe
mv ./gor ./gor.exe
zip gor-$(VERSION)$(PREFIX)_windows.zip ./gor.exe
rm -rf ./gor.exe

build:
go build -o $(BIN_NAME) $(LDFLAGS)
Expand Down
93 changes: 49 additions & 44 deletions capture/capture.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ type Listener struct {
Transport string // transport layer default to tcp
Activate func() error // function is used to activate the engine. it must be called before reading packets
Handles map[string]gopacket.ZeroCopyPacketDataSource
Interfaces []net.Interface
Interfaces []pcap.Interface
loopIndex int
Reading chan bool // this channel is closed when the listener has started reading packets
PcapOptions
Expand Down Expand Up @@ -103,6 +103,10 @@ func NewListener(host string, port uint16, transport string, engine EngineType,
l = &Listener{}

l.host = host
if l.host == "localhost" {
l.host = "127.0.0.1"
}

l.port = port
l.Transport = "tcp"
if transport != "" {
Expand All @@ -125,6 +129,7 @@ func NewListener(host string, port uint16, transport string, engine EngineType,
l.Activate = l.activatePcapFile
return
}

err = l.setInterfaces()
if err != nil {
return nil, err
Expand Down Expand Up @@ -168,7 +173,7 @@ func (l *Listener) ListenBackground(ctx context.Context, handler PacketHandler)

// Filter returns automatic filter applied by goreplay
// to a pcap handle of a specific interface
func (l *Listener) Filter(ifi net.Interface) (filter string) {
func (l *Listener) Filter(ifi pcap.Interface) (filter string) {
// https://www.tcpdump.org/manpages/pcap-filter.7.html

hosts := []string{l.host}
Expand Down Expand Up @@ -218,7 +223,7 @@ func (l *Listener) Filter(ifi net.Interface) (filter string) {

// PcapHandle returns new pcap Handle from dev on success.
// this function should be called after setting all necessary options for this listener
func (l *Listener) PcapHandle(ifi net.Interface) (handle *pcap.Handle, err error) {
func (l *Listener) PcapHandle(ifi pcap.Interface) (handle *pcap.Handle, err error) {
var inactive *pcap.InactiveHandle
inactive, err = pcap.NewInactiveHandle(ifi.Name)
if err != nil {
Expand All @@ -243,12 +248,22 @@ func (l *Listener) PcapHandle(ifi net.Interface) (handle *pcap.Handle, err error
return nil, fmt.Errorf("monitor mode error: %q, interface: %q", err, ifi.Name)
}
}

var snap int
if l.Snaplen {

if !l.Snaplen {
infs, _ := net.Interfaces()
for _, i := range infs {
if i.Name == ifi.Name {
snap = i.MTU + 200
}
}
}

if snap == 0 {
snap = 64<<10 + 200
} else if ifi.MTU > 0 {
snap = ifi.MTU + 200
}

err = inactive.SetSnapLen(snap)
if err != nil {
return nil, fmt.Errorf("snapshot length error: %q, interface: %q", err, ifi.Name)
Expand Down Expand Up @@ -281,7 +296,7 @@ func (l *Listener) PcapHandle(ifi net.Interface) (handle *pcap.Handle, err error
}

// SocketHandle returns new unix ethernet handle associated with this listener settings
func (l *Listener) SocketHandle(ifi net.Interface) (handle Socket, err error) {
func (l *Listener) SocketHandle(ifi pcap.Interface) (handle Socket, err error) {
handle, err = NewSocket(ifi)
if err != nil {
return nil, fmt.Errorf("sock raw error: %q, interface: %q", err, ifi.Name)
Expand Down Expand Up @@ -418,7 +433,7 @@ func (l *Listener) activatePcapFile() (err error) {

tmp := l.host
l.host = ""
l.BPFFilter = l.Filter(net.Interface{})
l.BPFFilter = l.Filter(pcap.Interface{})
l.host = tmp

if e = handle.SetBPFFilter(l.BPFFilter); e != nil {
Expand All @@ -430,60 +445,50 @@ func (l *Listener) activatePcapFile() (err error) {
}

func (l *Listener) setInterfaces() (err error) {
var ifis []net.Interface
ifis, err = net.Interfaces()
var pifis []pcap.Interface
pifis, err = pcap.FindAllDevs()
ifis, _ := net.Interfaces()
if err != nil {
return
}
for i := range ifis {
if ifis[i].Flags&net.FlagLoopback != 0 {
l.loopIndex = ifis[i].Index
for _, pi := range pifis {
var ni net.Interface
for _, i := range ifis {
if i.Name == pi.Name {
ni = i
break
}
}
if ifis[i].Flags&net.FlagUp == 0 {

if net.Flags(pi.Flags)&net.FlagLoopback != 0 {
l.loopIndex = ni.Index
}
if net.Flags(pi.Flags)&net.FlagUp == 0 {
continue
}
if isDevice(l.host, ifis[i]) {
l.Interfaces = []net.Interface{ifis[i]}
if isDevice(l.host, pi) {
l.Interfaces = []pcap.Interface{pi}
return
}
addrs, e := ifis[i].Addrs()
if e != nil {
// don't give up on a failure from a single interface
continue
}
for _, addr := range addrs {
if cutMask(addr) == l.host {
l.Interfaces = []net.Interface{ifis[i]}
for _, addr := range pi.Addresses {
if addr.IP.String() == l.host {
l.Interfaces = []pcap.Interface{pi}
return
}
}
}
l.Interfaces = ifis
l.Interfaces = pifis
return
}

func cutMask(addr net.Addr) string {
mask := addr.String()
for i, v := range mask {
if v == '/' {
return mask[:i]
}
}
return mask
}

func isDevice(addr string, ifi net.Interface) bool {
return addr == ifi.Name || addr == fmt.Sprintf("%d", ifi.Index) || (addr != "" && addr == ifi.HardwareAddr.String())
func isDevice(addr string, ifi pcap.Interface) bool {
return addr == ifi.Name
}

func interfaceAddresses(ifi net.Interface) []string {
func interfaceAddresses(ifi pcap.Interface) []string {
var hosts []string
if addrs, err := ifi.Addrs(); err == nil {
for _, addr := range addrs {
if ip := addr.(*net.IPNet).IP.To16(); ip != nil {
hosts = append(hosts, ip.String())
}
}
for _, addr := range ifi.Addresses {
hosts = append(hosts, addr.IP.String())
}
return hosts
}
Expand Down
18 changes: 17 additions & 1 deletion capture/sock_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,23 @@ type SockRaw struct {
}

// NewSocket returns new M'maped sock_raw on packet version 2.
func NewSocket(ifi net.Interface) (*SockRaw, error) {
func NewSocket(pifi pcap.Interface) (*SockRaw, error) {
var ifi net.Interface

infs, _ := net.Interfaces()
found := false
for _, i := range infs {
if i.Name == pifi.Name {
ifi = i
found = true
break
}
}

if !found {
return nil, fmt.Errorf("Can't find matching interface")
}

// sock create
fd, err := unix.Socket(unix.AF_PACKET, unix.SOCK_RAW, int(ETHALL))
if err != nil {
Expand Down
5 changes: 3 additions & 2 deletions capture/sock_others.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ package capture

import (
"errors"
"net"

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

// NewSocket returns new M'maped sock_raw on packet version 2.
func NewSocket(_ net.Interface) (Socket, error) {
func NewSocket(_ pcap.Interface) (Socket, error) {
return nil, errors.New("afpacket socket is only available on linux")
}
1 change: 1 addition & 0 deletions proto/proto.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package proto
import (
"bufio"
"bytes"
_ "fmt"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a blank import should be only in a main or test package, or have a comment justifying it

"net/http"
"net/textproto"
"strings"
Expand Down
7 changes: 2 additions & 5 deletions tcp/tcp_message.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package tcp
import (
"encoding/binary"
"encoding/hex"
_ "fmt"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a blank import should be only in a main or test package, or have a comment justifying it

"sort"
"time"

Expand Down Expand Up @@ -112,11 +113,7 @@ func (m *Message) MissingChunk() bool {
}

func (m *Message) PacketData() [][]byte {
var totalLen int
for _, p := range m.packets {
totalLen += len(p.Payload)
}
tmp := make([][]byte, totalLen)
tmp := make([][]byte, len(m.packets))

for i, p := range m.packets {
tmp[i] = p.Payload
Expand Down