-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathnftrace.go
210 lines (187 loc) · 5.08 KB
/
nftrace.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
package nftrace
import (
"bufio"
"crypto/sha256"
"fmt"
"log"
"os"
"strconv"
"strings"
"sync"
"github.com/coreos/go-iptables/iptables"
"github.com/eiginn/nftrace/pktdump"
"github.com/florianl/go-nflog/v2"
"github.com/gopacket/gopacket"
"github.com/gopacket/gopacket/layers"
sysctl "github.com/lorenzosaino/go-sysctl"
)
var (
ruleset = map[string]map[string][]string{}
packets = packetagg{ids: make(map[string]gopacket.Packet)}
ipv6 = false
)
const (
lockPath = "/var/run/xtables.lock"
nlgroup uint16 = 0 // 0 MUST be used for TRACE
)
type packetagg = struct {
sync.RWMutex
ids map[string]gopacket.Packet
}
func getTableNames(proto iptables.Protocol) []string {
var (
path string
tables []string
)
if proto == iptables.ProtocolIPv6 {
path = "/proc/net/ip6_tables_names"
} else {
path = "/proc/net/ip_tables_names"
}
file, err := os.Open(path)
if err != nil {
log.Fatal(err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
tables = append(tables, scanner.Text())
}
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
return tables
}
func SetIPv6(b bool) {
if b == true {
ipv6 = true
}
}
func GetRuleSet(proto iptables.Protocol) {
ipt, err := iptables.NewWithProtocol(proto)
if err != nil {
log.Fatal(err)
}
for _, table := range getTableNames(proto) {
ruleset[table] = map[string][]string{}
chains, _ := ipt.ListChains(table)
for _, chain := range chains {
ruleset[table][chain] = []string{}
rules, _ := ipt.List(table, chain)
for _, rule := range rules {
ruleset[table][chain] = append(ruleset[table][chain], rule)
}
}
}
}
func InsertTraceRule(proto iptables.Protocol, chain string, rule *string, limit *string) {
var matchers []string
ipt, err := iptables.NewWithProtocol(proto)
if err != nil {
log.Fatal(err)
}
if *limit != "" {
matchers = append(strings.Split(*rule, " "), []string{"-m", "limit", "--limit", *limit, "-j", "TRACE"}...)
} else {
matchers = append(strings.Split(*rule, " "), []string{"-j", "TRACE"}...)
}
log.Printf("Adding rule: -t raw -I %s %s", chain, strings.Join(matchers, " "))
err = ipt.Insert("raw", chain, 1, matchers...)
if err != nil {
log.Fatal(err)
}
}
func CleanTraceRule(proto iptables.Protocol, chain string, rule *string, limit *string) {
var matchers []string
ipt, err := iptables.NewWithProtocol(proto)
if err != nil {
log.Fatal(err)
}
if *limit != "" {
matchers = append(strings.Split(*rule, " "), []string{"-m", "limit", "--limit", *limit, "-j", "TRACE"}...)
} else {
matchers = append(strings.Split(*rule, " "), []string{"-j", "TRACE"}...)
}
err = ipt.Delete("raw", chain, matchers...)
if err != nil {
log.Printf("WARNING %s\n", err)
} else {
log.Printf("Removing rule: -t raw -A %s %s", chain, strings.Join(matchers, " "))
}
}
func lookupRule(packet gopacket.Packet, prefix string) string {
fields := strings.Split(strings.TrimPrefix(string(prefix), "TRACE: "), ":")
rulenum, err := strconv.Atoi(strings.TrimSpace(fields[3]))
if err != nil {
log.Fatal(err)
}
id := packetID(packet.Data())
switch kind := fields[2]; kind {
case "policy":
return fmt.Sprintf("%s %s %#v", id[:12], prefix, ruleset[fields[0]][fields[1]][0])
case "rule":
return fmt.Sprintf("%s %s %#v", id[:12], prefix, ruleset[fields[0]][fields[1]][rulenum])
}
return fmt.Sprintf("%s %s", id[:12], prefix)
}
func addPacketAgg(packet gopacket.Packet) {
id := packetID(packet.Data())
packets.RLock()
if _, ok := packets.ids[id]; ok == false {
packets.RUnlock()
packets.Lock()
if _, ok := packets.ids[id]; ok == false {
packets.ids[id] = packet
}
packets.Unlock()
} else {
packets.RUnlock()
}
}
func packetID(rawpacket []byte) string {
return fmt.Sprintf("%x", sha256.Sum256(rawpacket))
}
func CheckSysctl(af string) {
val, err := sysctl.Get(fmt.Sprintf("net.netfilter.nf_log.%s", af))
if err != nil {
log.Fatal(err)
}
if val != "nfnetlink_log" {
log.Fatalf("nfnetlink_log not loaded for address family, 'modprobe nfnetlink_log' kernel module and "+
"enable it with: 'sysctl -w net.netfilter.nf_log.%s=nfnetlink_log'", af)
}
}
func PrintPackets(verbose bool) {
packets.RLock()
if verbose == true {
for id, p := range packets.ids {
fmt.Printf("%s %s\n", id[:12], p)
}
} else {
for id, p := range packets.ids {
fmt.Printf("%s %s\n", id[:12], pktdump.Format(p))
}
}
packets.RUnlock()
fmt.Println("")
}
func PrintTrace(attrs nflog.Attribute) int {
// would be nice if we could exclude messages from the wrong address family but
// attrs does not have it only parent netlink message does
//fmt.Printf("%#v\n", attrs)
// We could get non netfilter trace messages on group 0
// like nf_conntrack_log_invalid
if !strings.Contains(*attrs.Prefix, "TRACE:") {
return 0
}
if ipv6 == true {
packet := gopacket.NewPacket(*attrs.Payload, layers.LayerTypeIPv6, gopacket.Default)
fmt.Printf("%s\n", lookupRule(packet, *attrs.Prefix))
addPacketAgg(packet)
} else {
packet := gopacket.NewPacket(*attrs.Payload, layers.LayerTypeIPv4, gopacket.Default)
fmt.Printf("%s\n", lookupRule(packet, *attrs.Prefix))
addPacketAgg(packet)
}
return 0
}