-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilter_test.go
44 lines (41 loc) · 1.2 KB
/
filter_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package gohijack
import (
"fmt"
"github.com/davecgh/go-spew/spew"
"github.com/google/gopacket/layers"
"github.com/google/gopacket/pcap"
"github.com/ssst0n3/awesome_libs/log"
"github.com/stretchr/testify/assert"
"golang.org/x/net/bpf"
"testing"
)
func TestCompileBPFFilter(t *testing.T) {
snaplen := 65535
filter := "tcp and host 169.254.169.254 and port 80"
pcapBPF, err := pcap.CompileBPFFilter(layers.LinkTypeEthernet, snaplen, filter)
assert.NoError(t, err)
spew.Dump(pcapBPF)
}
func TestDisassemble(t *testing.T) {
snaplen := 65535
//filter := "tcp and host 169.254.169.254 and port 80"
//filter := "tcp and host 169.254.169.254 and port 80"
//filter := "tcp and host 10.1.0.105 and port 80"
//filter := "tcp and src host 10.1.0.105 and src port 80"
filter := "tcp and src host 169.254.169.254 and src port 80"
pcapBPF, err := pcap.CompileBPFFilter(layers.LinkTypeEthernet, snaplen, filter)
assert.NoError(t, err)
var rawIns []bpf.RawInstruction
for _, p := range pcapBPF {
rawIns = append(rawIns, bpf.RawInstruction{
Op: p.Code,
Jt: p.Jt,
Jf: p.Jf,
K: p.K,
})
}
ins, allDecoded := bpf.Disassemble(rawIns)
spew.Dump(ins)
fmt.Printf("%#v\n", ins)
log.Logger.Info(allDecoded)
}