forked from x-way/iptables-tracer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiptables-tracer.go
280 lines (250 loc) · 6.93 KB
/
iptables-tracer.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
package main
import (
"bufio"
"context"
"flag"
"fmt"
"io"
"log"
"net"
"os"
"os/exec"
"regexp"
"strconv"
"time"
"github.com/florianl/go-nflog/v2"
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
"github.com/mdlayher/netlink"
"github.com/x-way/iptables-tracer/pkg/ctprint"
"github.com/x-way/pktdump"
)
type iptablesRule struct {
Table string
Chain string
Rule string
ChainEntry bool
}
type msg struct {
Time time.Time
Rule iptablesRule
Mark uint32
Iif string
Oif string
Payload []byte
Ct []byte
CtInfo uint32
}
var (
traceDuration = flag.Duration("t", 10*time.Second, "how long to run the iptables-tracer")
packetGap = flag.Duration("g", 10*time.Millisecond, "output empty line when two loglines are separated by at least this duration")
nflogGroup = flag.Int("n", 22, "NFLOG group number to use")
traceFilter = flag.String("f", "-p udp --dport 53", "trace filter (iptables match syntax)")
traceID = flag.Int("i", 0, "trace id (0 = use PID)")
traceRules = flag.Bool("r", false, "trace rules in addition to chains (experimental, currently broken!)")
clearRules = flag.Bool("c", false, "clear all iptables-tracer iptables rules from running config")
fwMark = flag.Int("m", 0, "fwmark to use for packet tracking")
packetLimit = flag.Int("l", 0, "limit of packets per minute to trace (0 = no limit)")
ip6tables = flag.Bool("6", false, "use ip6tables")
debugConntrack = flag.Bool("x", false, "dump all conntrack information")
saveCommand string
restoreCommand string
)
func main() {
flag.Parse()
if *ip6tables {
saveCommand = "ip6tables-save"
restoreCommand = "ip6tables-restore"
} else {
saveCommand = "iptables-save"
restoreCommand = "iptables-restore"
}
var err error
if *traceID == 0 {
*traceID = os.Getpid()
}
if *clearRules {
cleanupIptables(0) // 0 -> clear all IDs
return
}
if (*packetLimit != 0 || *traceRules) && *fwMark == 0 {
log.Fatal("Error: limit or trace rules requires fwmark")
}
lines := iptablesSave()
newIptablesConfig, ruleMap, maxLength := extendIptablesPolicy(lines, *traceID, *traceFilter, *fwMark, *packetLimit, *traceRules, *nflogGroup)
iptablesRestore(newIptablesConfig)
defer cleanupIptables(*traceID)
var nf *nflog.Nflog
config := nflog.Config{
Group: uint16(*nflogGroup),
Copymode: nflog.CopyPacket,
Flags: nflog.FlagConntrack,
ReadTimeout: time.Second,
}
nf, err = nflog.Open(&config)
if err != nil {
log.Fatal(err)
}
defer nf.Close()
ctx, cancel := context.WithTimeout(context.Background(), *traceDuration)
defer cancel()
msgChannel := make(chan msg)
callback := func(m nflog.Attribute) int {
var prefix string
if m.Prefix != nil {
prefix = *m.Prefix
}
prefixRe := regexp.MustCompile(`^iptr:(\d+):(\d+)`)
if res := prefixRe.FindStringSubmatch(prefix); res != nil {
if id, _ := strconv.Atoi(res[1]); id == *traceID {
ruleID, _ := strconv.Atoi(res[2])
if myRule, ok := ruleMap[ruleID]; ok {
var fwMark uint32
var iif string
var oif string
var ctBytes []byte
ctInfo := ^uint32(0)
if m.Mark != nil {
fwMark = *m.Mark
}
if m.InDev != nil {
iif = GetIfaceName(*m.InDev)
}
if m.OutDev != nil {
oif = GetIfaceName(*m.OutDev)
}
if m.Ct != nil {
ctBytes = *m.Ct
}
if m.CtInfo != nil {
ctInfo = *m.CtInfo
}
if m.Payload != nil {
msgChannel <- msg{
Time: time.Now(),
Rule: myRule,
Mark: fwMark,
Iif: iif,
Oif: oif,
Payload: *m.Payload,
Ct: ctBytes,
CtInfo: ctInfo,
}
}
}
}
}
return 0
}
go func() {
var lastTime time.Time
for msg := range msgChannel {
if msg.Time.Sub(lastTime).Nanoseconds() > (*packetGap).Nanoseconds() && !lastTime.IsZero() {
fmt.Println("")
}
lastTime = msg.Time
printRule(maxLength, msg.Time, msg.Rule, msg.Mark, msg.Iif, msg.Oif, msg.Payload, msg.Ct, msg.CtInfo)
if *debugConntrack && len(msg.Ct) > 0 {
ctprint.Print(msg.Ct)
}
}
}()
errorFunc := func(err error) int {
if opError, ok := err.(*netlink.OpError); ok {
if opError.Timeout() || opError.Temporary() {
return 0
}
}
log.Fatalf("Could not receive message: %v\n", err)
return 1
}
err = nf.RegisterWithErrorFunc(ctx, callback, errorFunc)
if err != nil {
log.Fatal(err)
}
// block until context expires
<-ctx.Done()
close(msgChannel)
}
func printRule(maxLength int, ts time.Time, rule iptablesRule, fwMark uint32, iif, oif string, payload, ct []byte, ctInfo uint32) {
packetStr := ""
if *ip6tables {
packetStr = pktdump.Format(gopacket.NewPacket(payload, layers.LayerTypeIPv6, gopacket.Default))
} else {
packetStr = pktdump.Format(gopacket.NewPacket(payload, layers.LayerTypeIPv4, gopacket.Default))
}
ctStr := fmt.Sprintf(" %s 0x%08x", ctprint.InfoString(ctInfo), ctprint.GetCtMark(ct))
if rule.ChainEntry {
fmtStr := fmt.Sprintf("%%s %%-6s %%-%ds 0x%%08x%%s %%s [In:%%s Out:%%s]\n", maxLength)
fmt.Printf(fmtStr, ts.Format("15:04:05.000000"), rule.Table, rule.Chain, fwMark, ctStr, packetStr, iif, oif)
} else {
fmtStr := fmt.Sprintf("%%s %%-6s %%-%ds %%s 0x%%08x%%s %%s [In:%%s Out:%%s]\n", maxLength)
fmt.Printf(fmtStr, ts.Format("15:04:05.000000"), rule.Table, rule.Chain, rule.Rule, fwMark, ctStr, packetStr, iif, oif)
}
}
func writeToCommand(cmd *exec.Cmd, lines []string) error {
cmdWriter, err := cmd.StdinPipe()
if err != nil {
return err
}
if err = cmd.Start(); err != nil {
return err
}
for _, line := range lines {
if _, err := io.WriteString(cmdWriter, line+"\n"); err != nil {
log.Fatal(err)
}
}
cmdWriter.Close()
return cmd.Wait()
}
func readFromCommand(cmd *exec.Cmd) ([]string, error) {
var cmdReader io.ReadCloser
var lines []string
cmdReader, err := cmd.StdoutPipe()
if err != nil {
return lines, err
}
scanner := bufio.NewScanner(cmdReader)
if err = cmd.Start(); err != nil {
return lines, err
}
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
if err = scanner.Err(); err != nil {
return lines, err
}
if err = cmd.Wait(); err != nil {
return lines, err
}
return lines, nil
}
func iptablesSave() []string {
var err error
var lines []string
if lines, err = readFromCommand(exec.Command(saveCommand)); err != nil {
log.Fatal(err)
}
return lines
}
func iptablesRestore(policy []string) {
if err := writeToCommand(exec.Command(restoreCommand, "-t"), policy); err != nil {
log.Fatal(err)
}
if err := writeToCommand(exec.Command(restoreCommand), policy); err != nil {
log.Fatal(err)
}
}
func cleanupIptables(cleanupID int) {
iptablesRestore(clearIptablesPolicy(iptablesSave(), cleanupID))
}
// GetIfaceName takes a network interface index and returns the corresponding name
func GetIfaceName(index uint32) string {
var iface *net.Interface
var err error
if iface, err = net.InterfaceByIndex(int(index)); err != nil {
return ""
}
return iface.Name
}