-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathwafcad.go
310 lines (253 loc) · 8.47 KB
/
wafcad.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
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
package wafris_caddy
import (
"context"
"fmt"
"io"
"math/big"
"net"
"net/http"
"strconv"
"strings"
"time"
"github.com/caddyserver/caddy/v2"
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
"github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile"
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
"github.com/redis/go-redis/v9"
"go.uber.org/zap"
)
func init() {
caddy.RegisterModule(WafrisCaddy{})
httpcaddyfile.RegisterHandlerDirective("wafris", parseCaddyfileHandlerDirective)
}
// Wafris, a free, open source WAF (web application firewall)
type WafrisCaddy struct {
WafrisURL string `json:"wafris_url,omitempty"`
WafrisTimeout float64 `json:"wafris_timeout,omitempty"` //in seconds
logger *zap.Logger
redisClient *redis.Client
coreScript *redis.Script
}
// CaddyModule returns the Caddy module information.
func (WafrisCaddy) CaddyModule() caddy.ModuleInfo {
return caddy.ModuleInfo{
ID: "http.handlers.wafris",
New: func() caddy.Module { return new(WafrisCaddy) },
}
}
func redisClient(ctx caddy.Context, url string) (*redis.Client, error) {
// sugar := ctx.Logger().Sugar()
// caution, this may expose redis password in logs!
// sugar.Infoln(3637760338, "wafris-caddy creating redis client with url:", url)
opts, err := redis.ParseURL(url)
if err != nil {
return nil, fmt.Errorf("redis.ParseURL err: %v", err)
}
// caution, this may expose redis password in logs!
// sugar.Warnln(3637760339, "wafris-caddy creating redis client with opts", opts)
rdb := redis.NewClient(opts)
return rdb, nil
}
// Provision sets up the module.
func (wc *WafrisCaddy) Provision(ctx caddy.Context) error {
wc.logger = ctx.Logger() // g.logger is a *zap.Logger
sugar := wc.logger.Sugar()
sugar.Debugln(2858015990, "[Wafris] Provision()")
rclient, err := redisClient(ctx, wc.WafrisURL)
if err != nil {
return fmt.Errorf("3025479311 [Wafris] failure to create redis client: %v", err)
}
str_cmd := rclient.ScriptLoad(context.Background(), wafris_core_lua)
sha_or_err := str_cmd.String()
if strings.Contains(sha_or_err, ": ERR ") {
return fmt.Errorf("9688903171 [Wafris] SCRIPT LOAD returned error: %v", sha_or_err)
}
// Assuming the script load is successful, now create the hash.
_, err = rclient.HSet(context.Background(), "waf-settings", "version", "v0.0.1", "client", "wafris-caddy").Result()
if err != nil {
return fmt.Errorf("error setting waf-settings: %v", err)
}
wc.coreScript = redis.NewScript(wafris_core_lua)
wc.redisClient = rclient
LoadUserDefinedProxies(sugar)
// sugar.Debugln(2858015995, "[Wafris] coreScript", wc.coreScript)
return nil
}
// Validate implements caddy.Validator.
func (wc *WafrisCaddy) Validate() error {
sugar := wc.logger.Sugar()
sugar.Debugln(2147895300, "[Wafris] Validate()")
if wc.WafrisURL == "" {
return fmt.Errorf("423327974 [Wafris] WafrisURL cannot be empty")
}
return nil
}
// ServeHTTP implements caddyhttp.MiddlewareHandler.
func (wc WafrisCaddy) ServeHTTP(rw http.ResponseWriter, req *http.Request, next caddyhttp.Handler) error {
sugar := wc.logger.Sugar()
rdb := wc.redisClient
ip := getRealIp(req, sugar)
// sugar.Debugf("2659600773 req getRealIp ip: %v", ip)
// sugar.Debugf("2659600774 req.Get x-forwarded-for: %v", req.Header.Get("x-forwarded-for"))
// sugar.Debugf("2659600775 req.Values x-forwarded-for: %v", req.Header.Values("x-forwarded-for"))
parsed_ip := net.ParseIP(ip)
args := []interface{}{
//ip
ip,
// ip integer string
Ip2IntString(parsed_ip),
// time
time.Now().UnixMilli(),
// request user agent
req.UserAgent(),
// request path
req.URL.RawPath,
// request query string
req.URL.RawQuery,
// request host
req.Host,
// request method
req.Method,
}
ctx := context.Background()
if wc.WafrisTimeout > 0 {
timeout_duration := time.Duration(wc.WafrisTimeout * float64(time.Second))
var cancel func()
ctx, cancel = context.WithTimeout(ctx, timeout_duration)
defer cancel()
}
ch := make(chan *redis.Cmd)
go func() {
select {
// Run does EVALSHA or falls back to EVAL, which loads the script so subsequent Runs do EVALSHA
case ch <- wc.coreScript.Run(ctx, rdb, []string{}, args...):
default:
return
}
}()
var redis_cmd *redis.Cmd
select {
case <-ctx.Done():
sugar.Infoln("2724205532 [Wafris] Wafris timed out during processing. Request passed without rules check.")
return next.ServeHTTP(rw, req)
case result := <-ch:
redis_cmd = result
}
n, err := redis_cmd.Result()
if err == nil {
result_string, isString := n.(string)
if isString {
// only for debugging
// req.Header.Set("X-WafrisResult", result_string)
if result_string == "Blocked" {
sugar.Infoln("2548097413 [Wafris] Blocked:", ip, req.Method, req.Host, req.URL.String())
return writeBlockedResponse(rw)
}
} else {
// result_string is something else
sugar.Warnf("2548097416 [Wafris] Redis returned type that wasn't a string: %T, value: %v. Request passed without rules check.", n, n)
}
} else {
sugar.Warnln(2548097418, "[Wafris] Redis connection error:", err, "Request passed without rules check.")
}
// debug
// req.Header.Set("X-Wafris-Result", fmt.Sprintf("%v", n))
// Pass by default if something is wrong with Redis
return next.ServeHTTP(rw, req)
}
// UnmarshalCaddyfile implements caddyfile.Unmarshaler.
func (wc *WafrisCaddy) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
if d.CountRemainingArgs() == 2 {
// if d.CountRemainingArgs() == 2 that means we have:
// wafris SOME_REDIS_URL
// where the first arg is wafris and the second arg is SOME_REDIS_URL
// calling d.Next() pops wafris of the dispenser fifo and the next time we call d.Args() it should give us SOME_REDIS_URL
for d.Next() {
if !d.Args(&wc.WafrisURL) {
return fmt.Errorf("12046737182 [Wafris] %v", d.ArgErr())
}
}
} else {
// if d.CountRemainingArgs() == 1 that means we expect:
// wafris {
// url SOME_REDIS_URL
// timeout TIMEOUT_IN_SECONDS
// }
// the {} and everything in it counts as one block and we have to nest down into it.
// pop wafris off the fifo
for d.Next() {
// nest into the block of subdirectives
for nesting := d.Nesting(); d.NextBlock(nesting); {
// log.Println(2937386010, "val", d.Val())
// d.Val() should either be `url` or `timeout` only.
switch d.Val() {
case "url":
if !d.Args(&wc.WafrisURL) {
return fmt.Errorf("1204673717 [Wafris] %v", d.ArgErr())
}
case "timeout":
var timeout_string string
if !d.Args(&timeout_string) {
return fmt.Errorf("1204673718 [Wafris] %v", d.ArgErr())
}
// log.Printf("4143840214 %T %v", timeout_string, timeout_string)
timeout_in_seconds, err := strconv.ParseFloat(timeout_string, 64)
if err != nil {
return fmt.Errorf("1204673719 [Wafris] cannot parse timeout in seconds: %v", timeout_string)
}
wc.WafrisTimeout = timeout_in_seconds
default:
return fmt.Errorf("1204673730 [Wafris] unexpected subdirective %v", d.ArgErr())
}
}
}
}
// log.Println(8747176953, "wc.WafrisURL", wc.WafrisURL)
// log.Printf("8747176955 wc.WafrisTimeout %g", wc.WafrisTimeout)
return nil
}
// parseCaddyfileHandlerDirective parses the `wafris` Caddyfile directive
func parseCaddyfileHandlerDirective(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error) {
var wc WafrisCaddy
err := wc.UnmarshalCaddyfile(h.Dispenser)
return wc, err
}
// best effort based on x-forwarded-for and RemoteAddr
func getRealIp(req *http.Request, sugar *zap.SugaredLogger) string {
// var err error
xff_values := req.Header.Values("x-forwarded-for")
if len(xff_values) != 0 {
// reverse the slice
for i, j := 0, len(xff_values)-1; i < j; i, j = i+1, j-1 {
xff_values[i], xff_values[j] = xff_values[j], xff_values[i]
}
// sugar.Debugf("req.xff_values: %v", xff_values)
for _, ip := range xff_values {
// sugar.Debugf("3450939168 req.IsTrustedProxy: %v, %v", ip, IsTrustedProxy(ip))
if !IsTrustedProxy(ip) {
// ues this one
return ip
}
}
}
ip, _, err := net.SplitHostPort(req.RemoteAddr)
if err != nil {
sugar.Errorf("req.RemoteAddr: %q is not IP:port", req.RemoteAddr)
ip = req.RemoteAddr
}
return ip
}
// https://andrew.red/posts/golang-ipv4-ipv6-to-decimal-integer
func Ip2IntString(ip net.IP) string {
if ip == nil {
return "0"
}
big_int := big.NewInt(0)
big_int.SetBytes(ip)
return big_int.String()
}
func writeBlockedResponse(w http.ResponseWriter) error {
w.WriteHeader(http.StatusForbidden)
io.WriteString(w, "Blocked")
return nil
}