@@ -19,6 +19,7 @@ import (
19
19
)
20
20
21
21
const chainName = "CROWDSEC_CHAIN"
22
+ const loggingChainName = "CROWDSEC_LOG"
22
23
23
24
type ipTablesContext struct {
24
25
version string
@@ -42,6 +43,9 @@ type ipTablesContext struct {
42
43
//Store the origin of the decisions, and use the index in the slice as the name
43
44
//This is not stable (ie, between two runs, the index of a set can change), but it's (probably) not an issue
44
45
originSetMapping []string
46
+
47
+ loggingEnabled bool
48
+ loggingPrefix string
45
49
}
46
50
47
51
func (ctx * ipTablesContext ) setupChain () {
@@ -69,6 +73,43 @@ func (ctx *ipTablesContext) setupChain() {
69
73
continue
70
74
}
71
75
}
76
+
77
+ if ctx .loggingEnabled {
78
+ // Create the logging chain
79
+ cmd = []string {"-N" , loggingChainName , "-t" , "filter" }
80
+
81
+ c = exec .Command (ctx .iptablesBin , cmd ... )
82
+
83
+ log .Infof ("Creating logging chain : %s %s" , ctx .iptablesBin , strings .Join (cmd , " " ))
84
+
85
+ if out , err := c .CombinedOutput (); err != nil {
86
+ log .Errorf ("error while creating logging chain : %v --> %s" , err , string (out ))
87
+ return
88
+ }
89
+
90
+ // Insert the logging rule
91
+ cmd = []string {"-I" , loggingChainName , "-j" , "LOG" , "--log-prefix" , ctx .loggingPrefix }
92
+
93
+ c = exec .Command (ctx .iptablesBin , cmd ... )
94
+
95
+ log .Infof ("Adding logging rule : %s %s" , ctx .iptablesBin , strings .Join (cmd , " " ))
96
+
97
+ if out , err := c .CombinedOutput (); err != nil {
98
+ log .Errorf ("error while adding logging rule : %v --> %s" , err , string (out ))
99
+ }
100
+
101
+ // Add the desired target to the logging chain
102
+
103
+ cmd = []string {"-A" , loggingChainName , "-j" , ctx .target }
104
+
105
+ c = exec .Command (ctx .iptablesBin , cmd ... )
106
+
107
+ log .Infof ("Adding target rule to logging chain : %s %s" , ctx .iptablesBin , strings .Join (cmd , " " ))
108
+
109
+ if out , err := c .CombinedOutput (); err != nil {
110
+ log .Errorf ("error while setting logging chain policy : %v --> %s" , err , string (out ))
111
+ }
112
+ }
72
113
}
73
114
74
115
func (ctx * ipTablesContext ) deleteChain () {
@@ -105,10 +146,38 @@ func (ctx *ipTablesContext) deleteChain() {
105
146
if out , err := c .CombinedOutput (); err != nil {
106
147
log .Errorf ("error while deleting chain : %v --> %s" , err , string (out ))
107
148
}
149
+
150
+ if ctx .loggingEnabled {
151
+ cmd = []string {"-F" , loggingChainName }
152
+
153
+ c = exec .Command (ctx .iptablesBin , cmd ... )
154
+
155
+ log .Infof ("Flushing logging chain : %s %s" , ctx .iptablesBin , strings .Join (cmd , " " ))
156
+
157
+ if out , err := c .CombinedOutput (); err != nil {
158
+ log .Errorf ("error while flushing logging chain : %v --> %s" , err , string (out ))
159
+ }
160
+
161
+ cmd = []string {"-X" , loggingChainName }
162
+
163
+ c = exec .Command (ctx .iptablesBin , cmd ... )
164
+
165
+ log .Infof ("Deleting logging chain : %s %s" , ctx .iptablesBin , strings .Join (cmd , " " ))
166
+
167
+ if out , err := c .CombinedOutput (); err != nil {
168
+ log .Errorf ("error while deleting logging chain : %v --> %s" , err , string (out ))
169
+ }
170
+ }
108
171
}
109
172
110
173
func (ctx * ipTablesContext ) createRule (setName string ) {
111
- cmd := []string {"-I" , chainName , "-m" , "set" , "--match-set" , setName , "src" , "-j" , ctx .target }
174
+ target := ctx .target
175
+
176
+ if ctx .loggingEnabled {
177
+ target = loggingChainName
178
+ }
179
+
180
+ cmd := []string {"-I" , chainName , "-m" , "set" , "--match-set" , setName , "src" , "-j" , target }
112
181
113
182
c := exec .Command (ctx .iptablesBin , cmd ... )
114
183
0 commit comments