miaopanel
1#!/usr/bin/env bash
2# Standalone WAF smoke test executed on the target host.
3set -uo pipefail
4ENTRY="${1:?usage: smoke-waf.sh <entry>}"
5BASE="http://127.0.0.1:8888/$ENTRY"
6PASS=$(awk -F': ' '/^password:/{print $2}' /www/server/panel/data/initial_password.txt)
7ORIGIN="http://127.0.0.1:8888"
8
9curl -s -c /tmp/ck.txt -H 'Content-Type: application/json' -H "Origin: $ORIGIN" \
10 -d "{\"username\":\"admin\",\"password\":\"$PASS\"}" "$BASE/api/v1/auth/login" >/tmp/login.out
11CSRF=$(python3 -c "import json;print(json.load(open('/tmp/login.out'))['data']['csrf'])")
12H=(-b /tmp/ck.txt -H "X-CSRF-Token: $CSRF" -H 'Content-Type: application/json' -H "Origin: $ORIGIN")
13
14echo "== 1. 产生流量:正常 10 / sqlmap 6 / 敏感路径 4 =="
15for i in $(seq 1 10); do curl -s -o /dev/null -H 'Host: demo.test' http://127.0.0.1/; done
16for i in $(seq 1 6); do curl -s -o /dev/null -A 'sqlmap/1.5' -H 'Host: demo.test' http://127.0.0.1/; done
17for i in $(seq 1 4); do curl -s -o /dev/null -H 'Host: demo.test' http://127.0.0.1/.git/config; done
18sleep 14
19
20echo "== 2. 概览 =="
21curl -s -b /tmp/ck.txt "$BASE/api/v1/waf/overview" | python3 -c "
22import json,sys
23d=json.load(sys.stdin)['data']
24print('requests=%d blocked=%d qps=%.2f traffic=%.0fB/s rt=%.2fms protected=%d/%d' % (
25 d['requests'], d['blocked'], d['qps'], d['traffic'], d['rt'], d['protected_sites'], d['sites']))"
26
27echo "== 3. 曲线(5m / 1h) =="
28curl -s -b /tmp/ck.txt "$BASE/api/v1/waf/series?window=5m" | python3 -c "
29import json,sys
30s=json.load(sys.stdin)['data']['series']
31print('points', {k:len(v) for k,v in s.items()}, 'last_qps', s['requests'][-1]['value'])"
32curl -s -b /tmp/ck.txt "$BASE/api/v1/waf/series?window=1h" | python3 -c "
33import json,sys
34s=json.load(sys.stdin)['data']['series']
35print('1h points', {k:len(v) for k,v in s.items()})"
36
37echo "== 4. 拦截事件 =="
38curl -s -b /tmp/ck.txt "$BASE/api/v1/waf/events?limit=6" | python3 -c "
39import json,sys
40ev=json.load(sys.stdin)['data']
41print('events', len(ev))
42for e in ev[:6]:
43 print(' ', e['ip'], e['status'], e['rule'], e['uri'][:40])"
44
45echo "== 5. 自定义规则拦截 =="
46curl -s "${H[@]}" -d '{"name":"block waf test","match_on":"uri","op":"contains","pattern":"/waf-rule-test","action":"block","enabled":true}' \
47 "$BASE/api/v1/waf/rules" > /tmp/rule.json
48RID=$(python3 -c "import json;print(json.load(open('/tmp/rule.json'))['data']['id'])")
49echo "rule id=$RID"
50sleep 1
51printf 'GET /waf-rule-test -> %s (expect 444)\n' "$(curl -s -o /dev/null -w '%{http_code}' -H 'Host: demo.test' http://127.0.0.1/waf-rule-test)"
52
53echo "== 6. 拦截码改为 403 =="
54curl -s -b /tmp/ck.txt "$BASE/api/v1/waf/settings" > /tmp/settings.json
55python3 - <<'PY'
56import json
57d = json.load(open('/tmp/settings.json'))['data']
58d['block_status'] = 403
59d['rate'] = 30
60json.dump(d, open('/tmp/settings-put.json', 'w'))
61PY
62curl -s "${H[@]}" -X PUT -d @/tmp/settings-put.json "$BASE/api/v1/waf/settings" | head -c 100; echo
63sleep 1
64printf 'nikto UA -> %s (expect 403)\n' "$(curl -s -o /dev/null -w '%{http_code}' -A 'nikto/2.5' -H 'Host: demo.test' http://127.0.0.1/)"
65
66echo "== 7. 站点级开关 =="
67SID=$(curl -s -b /tmp/ck.txt "$BASE/api/v1/waf/sites" | python3 -c "
68import json,sys
69print([s['id'] for s in json.load(sys.stdin)['data'] if s['name']=='demo'][0])")
70curl -s "${H[@]}" -d '{"enabled":false}' "$BASE/api/v1/waf/sites/$SID" >/dev/null
71sleep 1
72printf 'WAF off, sqlmap -> %s (expect 200)\n' "$(curl -s -o /dev/null -w '%{http_code}' -A 'sqlmap/1.5' -H 'Host: demo.test' http://127.0.0.1/)"
73curl -s "${H[@]}" -d '{"enabled":true}' "$BASE/api/v1/waf/sites/$SID" >/dev/null
74sleep 1
75printf 'WAF on, sqlmap -> %s (expect 444)\n' "$(curl -s -o /dev/null -w '%{http_code}' -A 'sqlmap/1.5' -H 'Host: demo.test' http://127.0.0.1/)"
76
77echo "== 8. 恢复默认并清理 =="
78python3 - <<'PY'
79import json
80p = '/tmp/settings-put.json'
81d = json.load(open(p)); d['block_status'] = 444
82json.dump(d, open(p, 'w'))
83PY
84curl -s "${H[@]}" -X PUT -d @/tmp/settings-put.json "$BASE/api/v1/waf/settings" >/dev/null
85curl -s "${H[@]}" -X DELETE "$BASE/api/v1/waf/rules/$RID" >/dev/null
86nginx -t 2>&1 | tail -1