-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfirewall.go
More file actions
240 lines (204 loc) · 7.35 KB
/
firewall.go
File metadata and controls
240 lines (204 loc) · 7.35 KB
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
package main
import (
"fmt"
"os"
"os/exec"
"strings"
)
const (
netClsClassID = "0x00100001"
classIDPath = "/sys/fs/cgroup/net_cls/jail/net_cls.classid"
)
// detectFirewallTool detects which firewall tool is available and used on the system
func detectFirewallTool() (string, error) {
// Check nftables first (more modern)
if isNftablesAvailable() {
fmt.Println("Detected nftables as primary firewall tool")
return "nftables", nil
}
// Check iptables
if isIptablesAvailable() {
fmt.Println("Detected iptables as primary firewall tool")
return "iptables", nil
}
return "", fmt.Errorf("neither nftables nor iptables found on system")
}
// isNftablesAvailable checks if nftables is available and usable
func isNftablesAvailable() bool {
// Check if nft command exists
if !commandExists("nft") {
return false
}
// Check if we can list tables (tests permissions and availability)
cmd := exec.Command("nft", "list", "tables")
if err := cmd.Run(); err != nil {
return false
}
return true
}
// isIptablesAvailable checks if iptables is available and usable
func isIptablesAvailable() bool {
// Check if iptables command exists
if !commandExists("iptables") {
return false
}
// Check if we can list rules (tests permissions and availability)
cmd := exec.Command("iptables", "-L", "-n")
if err := cmd.Run(); err != nil {
return false
}
return true
}
// commandExists checks if a command exists in PATH
func commandExists(cmd string) bool {
_, err := exec.LookPath(cmd)
return err == nil
}
// setupNetworkJail configures firewall rules to block traffic from the jail cgroup
func setupNetworkJail(state *JailerState) error {
if state.FirewallTool == "nftables" {
return setupNftablesJail(state)
} else if state.FirewallTool == "iptables" {
return setupIptablesJail(state)
}
return fmt.Errorf("unsupported firewall tool: %s", state.FirewallTool)
}
// setupNftablesJail configures nftables rules for the jail
func setupNftablesJail(state *JailerState) error {
// Create a dedicated table for the jail
commands := [][]string{
// Create the jail table
{"nft", "add", "table", "inet", "jail"},
// Create a chain to filter outgoing traffic
{"nft", "add", "chain", "inet", "jail", "output", "{", "type", "filter", "hook", "output", "priority", "100", ";", "}"},
// Create a chain to filter incoming traffic
{"nft", "add", "chain", "inet", "jail", "input", "{", "type", "filter", "hook", "input", "priority", "100", ";", "}"},
}
// Add rules to block traffic from the jail cgroup
if state.CgroupVersion == 2 {
// For cgroups v2, use socket cgroupv2
commands = append(commands, []string{
"nft", "add", "rule", "inet", "jail", "output",
"socket", "cgroupv2", "level", "1", "\"jail\"", "drop",
})
commands = append(commands, []string{
"nft", "add", "rule", "inet", "jail", "input",
"socket", "cgroupv2", "level", "1", "\"jail\"", "drop",
})
} else {
// For cgroups v1, use net_cls classid
// First define a classid for the jail cgroup
if err := writeFile(classIDPath, netClsClassID+"\n"); err != nil {
return fmt.Errorf("failed to set net_cls classid: %v", err)
}
commands = append(commands, []string{
"nft", "add", "rule", "inet", "jail", "output",
"meta", "cgroup", netClsClassID, "drop",
})
commands = append(commands, []string{
"nft", "add", "rule", "inet", "jail", "input",
"meta", "cgroup", netClsClassID, "drop",
})
}
// Execute all commands
for _, cmdArgs := range commands {
cmd := exec.Command(cmdArgs[0], cmdArgs[1:]...)
if output, err := cmd.CombinedOutput(); err != nil {
return fmt.Errorf("failed to execute nftables command %v: %v\nOutput: %s",
cmdArgs, err, string(output))
}
}
fmt.Println("Nftables jail rules configured successfully")
return nil
}
// setupIptablesJail configures iptables rules for the jail
func setupIptablesJail(state *JailerState) error {
var commands [][]string
if state.CgroupVersion == 2 {
// For cgroups v2, use cgroup match
commands = [][]string{
// Block outgoing traffic from jail cgroup
{"iptables", "-A", "OUTPUT", "-m", "cgroup", "--path", "jail", "-j", "DROP"},
// Block incoming traffic to jail cgroup
{"iptables", "-A", "INPUT", "-m", "cgroup", "--path", "jail", "-j", "DROP"},
}
} else {
// For cgroups v1, use net_cls classid
// First define a classid for the jail cgroup
if err := writeFile(classIDPath, netClsClassID+"\n"); err != nil {
return fmt.Errorf("failed to set net_cls classid: %v", err)
}
commands = [][]string{
// Block outgoing traffic with classid
{"iptables", "-A", "OUTPUT", "-m", "cgroup", "--cgroup", netClsClassID, "-j", "DROP"},
// Block incoming traffic with classid
{"iptables", "-A", "INPUT", "-m", "cgroup", "--cgroup", netClsClassID, "-j", "DROP"},
}
}
// Add logging to capture details about the iptables rules and any errors
fmt.Println("Setting up iptables rules for the jail...")
// Execute all commands
for _, cmdArgs := range commands {
fmt.Printf("Executing iptables command: %v\n", cmdArgs)
cmd := exec.Command(cmdArgs[0], cmdArgs[1:]...)
if output, err := cmd.CombinedOutput(); err != nil {
fmt.Printf("Error executing iptables command %v: %v\nOutput: %s\n", cmdArgs, err, string(output))
return fmt.Errorf("failed to execute iptables command %v: %v\nOutput: %s", cmdArgs, err, string(output))
}
}
fmt.Println("Iptables jail rules configured successfully")
return nil
}
// cleanupNetworkJail removes firewall rules from the jail
func cleanupNetworkJail(state *JailerState) error {
if state.FirewallTool == "nftables" {
return cleanupNftablesJail()
} else if state.FirewallTool == "iptables" {
return cleanupIptablesJail(state)
}
return fmt.Errorf("unsupported firewall tool: %s", state.FirewallTool)
}
// cleanupNftablesJail removes nftables rules from the jail
func cleanupNftablesJail() error {
// Remove the entire jail table
cmd := exec.Command("nft", "delete", "table", "inet", "jail")
if output, err := cmd.CombinedOutput(); err != nil {
// Don't fail if the table doesn't exist
if !strings.Contains(string(output), "No such file or directory") {
return fmt.Errorf("failed to cleanup nftables jail: %v\nOutput: %s", err, string(output))
}
}
fmt.Println("Nftables jail rules cleaned up")
return nil
}
// cleanupIptablesJail removes iptables rules from the jail
func cleanupIptablesJail(state *JailerState) error {
var commands [][]string
if state.CgroupVersion == 2 {
commands = [][]string{
{"iptables", "-D", "OUTPUT", "-m", "cgroup", "--path", "jail", "-j", "DROP"},
{"iptables", "-D", "INPUT", "-m", "cgroup", "--path", "jail", "-j", "DROP"},
}
} else {
commands = [][]string{
{"iptables", "-D", "OUTPUT", "-m", "cgroup", "--cgroup", netClsClassID, "-j", "DROP"},
{"iptables", "-D", "INPUT", "-m", "cgroup", "--cgroup", netClsClassID, "-j", "DROP"},
}
}
// Execute removal commands
for _, cmdArgs := range commands {
cmd := exec.Command(cmdArgs[0], cmdArgs[1:]...)
if output, err := cmd.CombinedOutput(); err != nil {
// Don't fail if the rule doesn't exist
if !strings.Contains(string(output), "No chain/target/match by that name") {
fmt.Printf("Warning: failed to remove iptables rule %v: %v\n", cmdArgs, err)
}
}
}
fmt.Println("Iptables jail rules cleaned up")
return nil
}
// writeFile writes content to a file (helper function)
func writeFile(path, content string) error {
return os.WriteFile(path, []byte(content), 0644)
}