forked from crowdsecurity/cs-firewall-bouncer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpf.go
211 lines (168 loc) · 4.96 KB
/
pf.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
// +build openbsd freebsd
package main
import (
"fmt"
"os"
"os/exec"
"strconv"
"strings"
"time"
"github.com/crowdsecurity/crowdsec/pkg/models"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
)
type pfContext struct {
proto string
table string
version string
}
type pf struct {
inet *pfContext
inet6 *pfContext
}
const (
backendName = "pf"
pfinetTable = "crowdsec-blacklists"
pfinet6Table = "crowdsec6-blacklists"
pfctlCmd = "/sbin/pfctl"
pfDevice = "/dev/pf"
addBanFormat = "%s: add ban on %s for %s sec (%s)"
delBanFormat = "%s: del ban on %s for %s sec (%s)"
)
func newPF(config *bouncerConfig) (interface{}, error) {
ret := &pf{}
inetCtx := &pfContext{
table: pfinetTable,
proto: "inet",
version: "ipv4",
}
inet6Ctx := &pfContext{
table: pfinet6Table,
proto: "inet6",
version: "ipv6",
}
ret.inet = inetCtx
if config.DisableIPV6 {
return ret, nil
}
ret.inet6 = inet6Ctx
return ret, nil
}
func (ctx *pfContext) checkTable() error {
log.Infof("Checking pf table: %s", ctx.table)
cmd := exec.Command(pfctlCmd, "-s", "Tables")
out, err := cmd.CombinedOutput()
if err != nil {
return errors.Wrapf(err, "pfctl error : %v - %s", err, string(out))
} else if !strings.Contains(string(out), ctx.table) {
return errors.Errorf("table %s doesn't exist", ctx.table)
}
return nil
}
func (ctx *pfContext) shutDown() error {
cmd := exec.Command(pfctlCmd, "-t", ctx.table, "-T", "flush")
log.Infof("pf table clean-up : %s", cmd.String())
if out, err := cmd.CombinedOutput(); err != nil {
log.Errorf("Error while flushing table (%s): %v --> %s", cmd.String(), err, string(out))
}
return nil
}
func (ctx *pfContext) Add(decision *models.Decision) error {
banDuration, err := time.ParseDuration(*decision.Duration)
if err != nil {
return err
}
log.Debugf(addBanFormat, backendName, *decision.Value, strconv.Itoa(int(banDuration.Seconds())), *decision.Scenario)
cmd := exec.Command(pfctlCmd, "-t", ctx.table, "-T", "add", *decision.Value)
log.Debugf("pfctl add : %s", cmd.String())
if out, err := cmd.CombinedOutput(); err != nil {
log.Infof("Error while adding to table (%s): %v --> %s", cmd.String(), err, string(out))
}
return nil
}
func (ctx *pfContext) Delete(decision *models.Decision) error {
banDuration, err := time.ParseDuration(*decision.Duration)
if err != nil {
return err
}
log.Debugf(delBanFormat, backendName, *decision.Value, strconv.Itoa(int(banDuration.Seconds())), *decision.Scenario)
cmd := exec.Command(pfctlCmd, "-t", ctx.table, "-T", "delete", *decision.Value)
log.Debugf("pfctl del : %s", cmd.String())
if out, err := cmd.CombinedOutput(); err != nil {
log.Infof("Error while deleting from table (%s): %v --> %s", cmd.String(), err, string(out))
}
return nil
}
func initPF(ctx *pfContext) error {
if err := ctx.shutDown(); err != nil {
return fmt.Errorf("pf table flush failed: %s", err.Error())
}
if err := ctx.checkTable(); err != nil {
return fmt.Errorf("pf init failed: %s", err.Error())
}
log.Infof("%s initiated for %s", backendName, ctx.version)
return nil
}
func (pf *pf) Init() error {
if _, err := os.Stat(pfDevice); err != nil {
return fmt.Errorf("%s device not found: %s", pfDevice, err.Error())
}
if _, err := exec.LookPath(pfctlCmd); err != nil {
return fmt.Errorf("%s command not found: %s", pfctlCmd, err.Error())
}
if err := initPF(pf.inet); err != nil {
return err
}
if pf.inet6 != nil {
if err := initPF(pf.inet6); err != nil {
return err
}
}
return nil
}
func (pf *pf) Add(decision *models.Decision) error {
if strings.Contains(*decision.Value, ":") && pf.inet6 != nil { // inet6
if pf.inet6 != nil {
if err := pf.inet6.Add(decision); err != nil {
return fmt.Errorf("failed to add ban ip '%s' to inet6 table", *decision.Value)
}
} else {
log.Debugf("not adding '%s' because ipv6 is disabled", *decision.Value)
return nil
}
} else { // inet
if err := pf.inet.Add(decision); err != nil {
return fmt.Errorf("failed adding ban ip '%s' to inet table", *decision.Value)
}
}
return nil
}
func (pf *pf) Delete(decision *models.Decision) error {
if strings.Contains(*decision.Value, ":") { // ipv6
if pf.inet6 != nil {
if err := pf.inet6.Delete(decision); err != nil {
return fmt.Errorf("failed to remove ban ip '%s' from inet6 table", *decision.Value)
}
} else {
log.Debugf("not removing '%s' because ipv6 is disabled", *decision.Value)
return nil
}
} else { // ipv4
if err := pf.inet.Delete(decision); err != nil {
return fmt.Errorf("failed to remove ban ip '%s' from inet6 table", *decision.Value)
}
}
return nil
}
func (pf *pf) ShutDown() error {
log.Infof("flushing 'crowdsec' table(s)")
if err := pf.inet.shutDown(); err != nil {
return fmt.Errorf("unable to flush %s table (%s): ", pf.inet.version, pf.inet.table)
}
if pf.inet6 != nil {
if err := pf.inet6.shutDown(); err != nil {
return fmt.Errorf("unable to flush %s table (%s): ", pf.inet6.version, pf.inet6.table)
}
}
return nil
}