/
waf_test.go
104 lines (85 loc) · 3 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
// 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/ioutil"
"net/http"
"net/http/httptest"
"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 := ioutil.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 := ioutil.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"))
})
}