-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
184 lines (158 loc) · 4.53 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
183
184
package main
import (
"bytes"
"context"
_ "embed"
"encoding/binary"
"errors"
"fmt"
"log"
"os"
"os/signal"
"syscall"
"github.com/cilium/ebpf"
"github.com/cilium/ebpf/link"
"github.com/cilium/ebpf/perf"
"github.com/spf13/cobra"
"golang.org/x/sys/unix"
)
//go:generate go run github.com/cilium/ebpf/cmd/bpf2go -no-global-types -cc clang skbtracer ./ebpf/skbtracer.c -- -D__TARGET_ARCH_x86 -I./ebpf/headers -Wall
var usage = `examples:
skbtracer-iptables # trace all packets
skbtracer-iptables --proto=icmp -H 1.2.3.4 --icmpid 22 # trace icmp packet with addr=1.2.3.4 and icmpid=22
skbtracer-iptables --proto=tcp -H 1.2.3.4 -P 22 # trace tcp packet with addr=1.2.3.4:22
skbtracer-iptables --proto=udp -H 1.2.3.4 -P 22 # trace udp packet wich addr=1.2.3.4:22
skbtracer-iptables -t -T -p 1 -P 80 -H 127.0.0.1 --proto=tcp --icmpid=100 -N 10000
`
var rootCmd = cobra.Command{
Use: "skbtracer-iptables",
Short: "Trace any packet through iptables",
Long: usage,
Run: func(cmd *cobra.Command, args []string) {
if err := cfg.parse(); err != nil {
fmt.Println(err)
return
}
runGops()
runEbpf()
},
}
func main() {
cobra.CheckErr(rootCmd.Execute())
}
// runEbpf attaches the kprobes and prints the kprobes' info.
func runEbpf() {
if err := unix.Setrlimit(unix.RLIMIT_NOFILE, &unix.Rlimit{
Cur: 4096,
Max: 4096,
}); err != nil {
log.Fatalf("failed to set temporary rlimit: %s", err)
}
if err := unix.Setrlimit(unix.RLIMIT_MEMLOCK, &unix.Rlimit{
Cur: unix.RLIM_INFINITY,
Max: unix.RLIM_INFINITY,
}); err != nil {
log.Fatalf("Failed to set temporary rlimit: %s", err)
}
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer stop()
bpfSpec, err := loadSkbtracer()
if err != nil {
log.Printf("Failed to load bpf spec: %v", err)
return
}
if err := bpfSpec.RewriteConstants(map[string]interface{}{
"CFG": getBpfConfig(),
}); err != nil {
log.Printf("Failed to rewrite const for config: %v", err)
return
}
var bpfObj skbtracerObjects
if err := bpfSpec.LoadAndAssign(&bpfObj, &ebpf.CollectionOptions{
Programs: ebpf.ProgramOptions{
LogSize: ebpf.DefaultVerifierLogSize * 10,
},
}); err != nil {
var ve *ebpf.VerifierError
if errors.As(err, &ve) {
log.Printf("Failed to load bpf obj: %v\n%-50v", err, ve)
} else {
log.Printf("Failed to load bpf obj: %v", err)
}
return
}
isHighVersion, err := isKernelVersionGte_5_16()
if err != nil {
log.Printf("Failed to check kernel version: %v", err)
return
}
kIptDoTable := bpfObj.K_iptDoTable
if !isHighVersion {
kIptDoTable = bpfObj.IptDoTableOld
}
if kp, err := link.Kprobe("ipt_do_table", kIptDoTable, nil); err != nil {
log.Printf("Failed to kprobe(ipt_do_table): %v", err)
return
} else {
defer kp.Close()
log.Printf("Attached kprobe(ipt_do_table)")
}
if krp, err := link.Kretprobe("ipt_do_table", bpfObj.KrIptDoTable, nil); err != nil {
log.Printf("Failed to kretprobe(ipt_do_table): %v", err)
return
} else {
defer krp.Close()
log.Printf("Attached kretprobe(ipt_do_table)")
}
if kp, err := link.Kprobe("nf_hook_slow", bpfObj.K_nfHookSlow, nil); err != nil {
log.Printf("Failed to kprobe(nf_hook_slow): %v", err)
return
} else {
defer kp.Close()
log.Printf("Attached kprobe(nf_hook_slow)")
}
if krp, err := link.Kretprobe("nf_hook_slow", bpfObj.KrNfHookSlow, nil); err != nil {
log.Printf("Failed to kretprobe(nf_hook_slow): %v", err)
return
} else {
defer krp.Close()
log.Printf("Attached kretprobe(nf_hook_slow)")
}
rd, err := perf.NewReader(bpfObj.SkbtracerEvent, cfg.PerCPUBuffer)
if err != nil {
log.Printf("Failed to create perf event reader: %v", err)
return
}
go func() {
<-ctx.Done()
_ = rd.Close()
log.Println("Received signal, exiting program...")
}()
fmt.Printf("%-10s %-20s %-12s %-8s %-6s %-18s %-18s %-6s %-54s %s\n",
"TIME", "SKB", "NETWORK_NS", "PID", "CPU", "INTERFACE", "DEST_MAC", "IP_LEN",
"PKT_INFO", "IPTABLES_INFO")
var event perfEvent
for {
record, err := rd.Read()
if err != nil {
if errors.Is(err, perf.ErrClosed) {
return
}
log.Printf("Reading from perf event reader: %v", err)
}
if record.LostSamples != 0 {
log.Printf("Perf event ring buffer full, dropped %d samples", record.LostSamples)
continue
}
if err := binary.Read(bytes.NewBuffer(record.RawSample), binary.LittleEndian, &event); err != nil {
log.Printf("Failed to parse perf event: %v", err)
continue
}
fmt.Println(event.output())
select {
case <-ctx.Done():
return
default:
}
}
}