Skip to content

Commit ca8368a

Browse files
add an option to allow adding a comment to iptables rules (#386)
1 parent 9e6a4f3 commit ca8368a

File tree

5 files changed

+17
-4
lines changed

5 files changed

+17
-4
lines changed

config/crowdsec-firewall-bouncer.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ iptables_chains:
3131
- INPUT
3232
# - FORWARD
3333
# - DOCKER-USER
34+
iptables_add_rule_comments: true
3435

3536
## nftables
3637
nftables:

pkg/cfg/config.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ type BouncerConfig struct {
5252

5353
// specific to iptables, following https://github.com/crowdsecurity/cs-firewall-bouncer/issues/19
5454
IptablesChains []string `yaml:"iptables_chains"`
55+
IptablesAddRuleComments bool `yaml:"iptables_add_rule_comments"`
56+
5557
SupportedDecisionsTypes []string `yaml:"supported_decisions_types"`
5658
// specific to nftables, following https://github.com/crowdsecurity/cs-firewall-bouncer/issues/74
5759
Nftables struct {
@@ -79,7 +81,9 @@ func MergedConfig(configPath string) ([]byte, error) {
7981
}
8082

8183
func NewConfig(reader io.Reader) (*BouncerConfig, error) {
82-
config := &BouncerConfig{}
84+
config := &BouncerConfig{
85+
IptablesAddRuleComments: true,
86+
}
8387

8488
fcontent, err := io.ReadAll(reader)
8589
if err != nil {

pkg/iptables/iptables.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ func NewIPTables(config *cfg.BouncerConfig) (types.Backend, error) {
6565
target: target,
6666
loggingEnabled: config.DenyLog,
6767
loggingPrefix: config.DenyLogPrefix,
68+
addRuleComments: config.IptablesAddRuleComments,
6869
}
6970
ipv6Ctx := &ipTablesContext{
7071
version: "v6",
@@ -77,6 +78,7 @@ func NewIPTables(config *cfg.BouncerConfig) (types.Backend, error) {
7778
target: target,
7879
loggingEnabled: config.DenyLog,
7980
loggingPrefix: config.DenyLogPrefix,
81+
addRuleComments: config.IptablesAddRuleComments,
8082
}
8183

8284
ipv4Ctx.iptablesSaveBin, err = exec.LookPath("iptables-save")

pkg/iptables/iptables_context.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ type ipTablesContext struct {
5151

5252
loggingEnabled bool
5353
loggingPrefix string
54+
55+
addRuleComments bool
5456
}
5557

5658
func (ctx *ipTablesContext) setupChain() {
@@ -174,7 +176,7 @@ func (ctx *ipTablesContext) deleteChain() {
174176
}
175177
}
176178

177-
func (ctx *ipTablesContext) createRule(setName string) {
179+
func (ctx *ipTablesContext) createRule(setName string, origin string) {
178180
target := ctx.target
179181

180182
if ctx.loggingEnabled {
@@ -183,6 +185,10 @@ func (ctx *ipTablesContext) createRule(setName string) {
183185

184186
cmd := []string{"-I", chainName, "-m", "set", "--match-set", setName, "src", "-j", target}
185187

188+
if ctx.addRuleComments {
189+
cmd = append(cmd, "-m", "comment", "--comment", "CrowdSec: "+origin)
190+
}
191+
186192
c := exec.Command(ctx.iptablesBin, cmd...)
187193

188194
log.Infof("Creating rule : %s %s", ctx.iptablesBin, strings.Join(cmd, " "))
@@ -308,7 +314,7 @@ func (ctx *ipTablesContext) commit() error {
308314

309315
if !ctx.ipsetContentOnly {
310316
// Create the rule to use the set
311-
ctx.createRule(set.Name())
317+
ctx.createRule(set.Name(), origin)
312318
}
313319
}
314320
}

pkg/iptables/metrics.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import (
2929
// In case of a rule, the counters represent the number of packets and bytes that have been matched by the rule (ie, the packets that have been dropped).
3030

3131
var chainRegexp = regexp.MustCompile(`^\[(\d+):(\d+)\]`)
32-
var ruleRegexp = regexp.MustCompile(`^\[(\d+):(\d+)\] -A [0-9A-Za-z_-]+ -m set --match-set (.*) src -j \w+`)
32+
var ruleRegexp = regexp.MustCompile(`^\[(\d+):(\d+)\] -A [0-9A-Za-z_-]+ -m set --match-set (.*) src .*-j \w+`)
3333

3434
// In ipset mode, we have to track the numbers of processed bytes/packets at the chain level
3535
// This is not really accurate, as a rule *before* the crowdsec rule could impact the numbers, but we don't have any other way.

0 commit comments

Comments
 (0)