generated from Soil-Security/bpf-go-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
182 lines (150 loc) · 3.17 KB
/
main.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package main
import (
"context"
"encoding/binary"
"flag"
"fmt"
"io"
"os"
"os/signal"
"path"
"path/filepath"
"syscall"
"time"
"unsafe"
"github.com/cilium/ebpf"
)
const SO_ATTACH_BPF = 50
const (
ICMP = 0x01
TCP = 0x06
UDP = 0x11
)
func main() {
if err := run(setupHandler()); err != nil {
fmt.Fprintf(os.Stderr, "error: %s", err.Error())
os.Exit(1)
}
os.Exit(0)
}
func run(ctx context.Context) error {
index := flag.Int("index", 0, "specify ethernet index")
flag.Parse()
var bpfObjects bpfObjects
executable, err := os.Executable()
if err != nil {
return err
}
bpfObjectFile := path.Join(filepath.Dir(executable), "socket.bpf.o")
spec, err := ebpf.LoadCollectionSpec(bpfObjectFile)
if err != nil {
return err
}
err = spec.LoadAndAssign(&bpfObjects, &ebpf.CollectionOptions{})
if err != nil {
return err
}
defer bpfObjects.Close()
sock, err := openRawSock(*index)
if err != nil {
return err
}
defer syscall.Close(sock)
err = syscall.SetsockoptInt(sock, syscall.SOL_SOCKET, SO_ATTACH_BPF, bpfObjects.SocketProg.FD())
if err != nil {
return err
}
fmt.Printf("Filtering as uid=%d(%s) on eth index: %d\n", os.Getuid(), os.Getenv("USER"), *index)
go func() {
for {
time.Sleep(time.Second)
var icmp uint32
var tcp uint32
var udp uint32
err := bpfObjects.CountMap.Lookup(uint32(ICMP), &icmp)
if err != nil {
panic(err)
}
err = bpfObjects.CountMap.Lookup(uint32(TCP), &tcp)
if err != nil {
panic(err)
}
err = bpfObjects.CountMap.Lookup(uint32(UDP), &udp)
if err != nil {
panic(err)
}
fmt.Printf("\r\033[m\tICMP: %d TCP: %d UDP: %d", icmp, tcp, udp)
}
}()
<-ctx.Done()
return nil
}
var onlyOneSignalHandler = make(chan struct{})
func setupHandler() context.Context {
close(onlyOneSignalHandler)
ctx, cancel := context.WithCancel(context.Background())
c := make(chan os.Signal, 2)
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
go func() {
<-c
cancel()
<-c
os.Exit(1)
}()
return ctx
}
type bpfObjects struct {
bpfPrograms
bpfMaps
}
func (o *bpfObjects) Close() error {
return bpfClose(
&o.bpfPrograms,
&o.bpfMaps,
)
}
type bpfPrograms struct {
SocketProg *ebpf.Program `ebpf:"socket_prog"`
}
func (p *bpfPrograms) Close() error {
return bpfClose(
p.SocketProg,
)
}
type bpfMaps struct {
CountMap *ebpf.Map `ebpf:"countmap"`
}
func (m *bpfMaps) Close() error {
return bpfClose(
m.CountMap,
)
}
func bpfClose(closers ...io.Closer) error {
for _, closer := range closers {
if err := closer.Close(); err != nil {
return err
}
}
return nil
}
func openRawSock(index int) (int, error) {
sock, err := syscall.Socket(syscall.AF_PACKET, syscall.SOCK_RAW|syscall.SOCK_NONBLOCK|syscall.SOCK_CLOEXEC, int(htons(syscall.ETH_P_ALL)))
if err != nil {
return 0, err
}
sll := syscall.SockaddrLinklayer{
Ifindex: index,
Protocol: htons(syscall.ETH_P_ALL),
}
err = syscall.Bind(sock, &sll)
if err != nil {
return 0, err
}
return sock, nil
}
// htons converts the unsigned short integer from host byte order to network byte order.
func htons(i uint16) uint16 {
b := make([]byte, 2)
binary.BigEndian.PutUint16(b, i)
return *(*uint16)(unsafe.Pointer(&b[0]))
}