-
Notifications
You must be signed in to change notification settings - Fork 415
/
waf_test.go
238 lines (201 loc) · 7.05 KB
/
waf_test.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
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016 Datadog, Inc.
//go:build appsec
// +build appsec
package appsec_test
import (
"io"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
pAppsec "gopkg.in/DataDog/dd-trace-go.v1/appsec"
httptrace "gopkg.in/DataDog/dd-trace-go.v1/contrib/net/http"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/mocktracer"
"gopkg.in/DataDog/dd-trace-go.v1/internal/appsec"
"github.com/stretchr/testify/require"
)
// TestWAF is a simple validation test of the WAF protecting a net/http server. It only mockups the agent and tests that
// the WAF is properly detecting an LFI attempt and that the corresponding security event is being sent to the agent.
// Additionally, verifies that rule matching through SDK body instrumentation works as expected
func TestWAF(t *testing.T) {
appsec.Start()
defer appsec.Stop()
if !appsec.Enabled() {
t.Skip("appsec disabled")
}
// Start and trace an HTTP server
mux := httptrace.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello World!\n"))
})
mux.HandleFunc("/body", func(w http.ResponseWriter, r *http.Request) {
pAppsec.MonitorParsedHTTPBody(r.Context(), "$globals")
w.Write([]byte("Hello Body!\n"))
})
srv := httptest.NewServer(mux)
defer srv.Close()
t.Run("lfi", func(t *testing.T) {
mt := mocktracer.Start()
defer mt.Stop()
// Send an LFI attack
req, err := http.NewRequest("POST", srv.URL+"/../../../secret.txt", nil)
if err != nil {
panic(err)
}
res, err := srv.Client().Do(req)
require.NoError(t, err)
// Check that the handler was properly called
b, err := io.ReadAll(res.Body)
require.NoError(t, err)
require.Equal(t, "Hello World!\n", string(b))
finished := mt.FinishedSpans()
require.Len(t, finished, 2)
// Two requests were performed by the client request (due to the 301 redirection) and the two should have the LFI
// attack attempt event (appsec rule id crs-930-110).
event := finished[0].Tag("_dd.appsec.json")
require.NotNil(t, event)
require.Contains(t, event, "crs-930-110")
event = finished[1].Tag("_dd.appsec.json")
require.NotNil(t, event)
require.Contains(t, event, "crs-930-110")
})
// Test a PHP injection attack via request parsed body
t.Run("SDK-body", func(t *testing.T) {
mt := mocktracer.Start()
defer mt.Stop()
req, err := http.NewRequest("POST", srv.URL+"/body", nil)
if err != nil {
panic(err)
}
res, err := srv.Client().Do(req)
require.NoError(t, err)
// Check that the handler was properly called
b, err := io.ReadAll(res.Body)
require.NoError(t, err)
require.Equal(t, "Hello Body!\n", string(b))
finished := mt.FinishedSpans()
require.Len(t, finished, 1)
event := finished[0].Tag("_dd.appsec.json")
require.NotNil(t, event)
require.True(t, strings.Contains(event.(string), "crs-933-130"))
})
t.Run("obfuscation", func(t *testing.T) {
mt := mocktracer.Start()
defer mt.Stop()
// Send a malicious request with sensitive data that should be both
// obfuscated by the obfuscator key and value regexps.
form := url.Values{}
// Form value detected by a XSS attack that should be obfuscated by the
// obfuscator value regex.
const sensitivePayloadValue = `BEARER lwqjedqwdoqwidmoqwndun32i`
form.Add("payload", `
{
"activeTab":"39612314-1890-45f7-8075-c793325c1d70",
"allOpenTabs":["132ef2e5-afaa-4e20-bc64-db9b13230a","39612314-1890-45f7-8075-c793325c1d70"],
"lastPage":{
"accessToken":"`+sensitivePayloadValue+`",
"account":{
"name":"F123123 .htaccess",
"contactCustomFields":{
"ffa77959-1ff3-464b-a3af-e5410e436f1f":{
"questionServiceEntityType":"CustomField",
"question":{
"code":"Manager Name",
"questionTypeInfo":{
"questionType":"OpenEndedText",
"answerFormatType":"General"
,"scores":[]
},
"additionalInfo":{
"codeSnippetValue":"<!-- Google Tag Manager (noscript) -->\r\n<embed src=\"https://www.googletagmanager.com/ns.html?id=GTM-PCVXQNM\"\r\nheight=\"0\" width=\"0\" style=\"display:none"
}
}
}
}
}
}
`)
// Form value detected by a SQLi rule that should be obfuscated by the
// obfuscator value regex.
sensitiveParamKeyValue := "I-m-sensitive-please-dont-expose-me"
form.Add("password", sensitiveParamKeyValue+"1234%20union%20select%20*%20from%20credit_cards"+sensitiveParamKeyValue)
req, err := http.NewRequest("POST", srv.URL, nil)
req.URL.RawQuery = form.Encode()
if err != nil {
panic(err)
}
res, err := srv.Client().Do(req)
require.NoError(t, err)
// Check that the handler was properly called
b, err := io.ReadAll(res.Body)
require.NoError(t, err)
require.Equal(t, "Hello World!\n", string(b))
finished := mt.FinishedSpans()
require.Len(t, finished, 1)
// Check that the tags don't hold any sensitive information
event := finished[0].Tag("_dd.appsec.json")
require.NotNil(t, event)
require.Contains(t, event, "crs-942-270") // SQLi
require.Contains(t, event, "crs-941-230") // XSS
require.NotContains(t, event, sensitiveParamKeyValue)
require.NotContains(t, event, sensitivePayloadValue)
})
}
// Test that http blocking works by using custom rules/rules data
func TestBlocking(t *testing.T) {
t.Setenv("DD_APPSEC_RULES", "testdata/blocking.json")
appsec.Start()
defer appsec.Stop()
if !appsec.Enabled() {
t.Skip("AppSec needs to be enabled for this test")
}
// Start and trace an HTTP server
mux := httptrace.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello World!\n"))
})
srv := httptest.NewServer(mux)
defer srv.Close()
t.Run("block", func(t *testing.T) {
mt := mocktracer.Start()
defer mt.Stop()
req, err := http.NewRequest("POST", srv.URL, nil)
if err != nil {
panic(err)
}
// Hardcoded IP header holding an IP that is blocked
req.Header.Set("x-forwarded-for", "1.2.3.4")
res, err := srv.Client().Do(req)
require.NoError(t, err)
// Check that the request was blocked
b, err := io.ReadAll(res.Body)
require.NoError(t, err)
require.NotEqual(t, "Hello World!\n", string(b))
require.Equal(t, 403, res.StatusCode)
})
t.Run("no-block", func(t *testing.T) {
mt := mocktracer.Start()
defer mt.Stop()
req1, err := http.NewRequest("POST", srv.URL, nil)
if err != nil {
panic(err)
}
req2, err := http.NewRequest("POST", srv.URL, nil)
if err != nil {
panic(err)
}
req2.Header.Set("x-forwarded-for", "1.2.3.5")
for _, r := range []*http.Request{req1, req2} {
res, err := srv.Client().Do(r)
require.NoError(t, err)
// Check that the request was not blocked
b, err := io.ReadAll(res.Body)
require.NoError(t, err)
require.Equal(t, "Hello World!\n", string(b))
}
})
}