chaitin-waf

Description

After enabling the chaitin-waf plugin, the traffic will be forwarded to the Chaitin WAF service for the detection and prevention of various web application attacks, ensuring the security of the application and user data.

Response Headers

Depending on the plugin configuration, it is optional to add additional response headers.

The response headers are listed below:

  • X-APISIX-CHAITIN-WAF: Whether APISIX forwards the request to the WAF server.
    • yes: forwarded
    • no: no forwarded
    • unhealthy: matches the match variables, but no WAF server is available.
    • err: an error occurred during the execution of the plugin. Also with X-APISIX-CHAITIN-WAF-ERROR request header
    • waf-err: Error while interacting with the WAF server. Also with X-APISIX-CHAITIN-WAF-ERROR request header
    • timeout: Timeout for request to the WAF server
  • X-APISIX-CHAITIN-WAF-ERROR: Debug header. WAF error message
  • X-APISIX-CHAITIN-WAF-TIME: The time in milliseconds that APISIX spent interacting with WAF.
  • X-APISIX-CHAITIN-WAF-STATUS: The status code returned to APISIX by the WAF server.
  • X-APISIX-CHAITIN-WAF-ACTION: Processing result returned to APISIX by the WAF server.
    • pass: request valid and passed
    • reject: request rejected by WAF service
  • X-APISIX-CHAITIN-WAF-SERVER: Debug header. Picked WAF server.

Plugin Metadata

NameTypeRequiredDefault valueDescription
nodesarray(object)trueA list of addresses for the Chaitin SafeLine WAF service.
nodes[0].hoststringtrueThe address of Chaitin SafeLine WAF service. Supports IPV4, IPV6, Unix Socket, etc.
nodes[0].portstringfalse80The port of Chaitin SafeLine WAF service.
configobjectfalseConfiguration of the Chaitin SafeLine WAF service. The parameters configured here will be used when route is not configured.
config.connect_timeoutintegerfalse1000connect timeout, in milliseconds
config.send_timeoutintegerfalse1000send timeout, in milliseconds
config.read_timeoutintegerfalse1000read timeout, in milliseconds
config.req_body_sizeintegerfalse1024request body size, in KB
config.keepalive_sizeintegerfalse256maximum concurrent idle connections to the SafeLine WAF detection service
config.keepalive_timeoutintegerfalse60000idle connection timeout, in milliseconds

An example configuration is as follows.

chaitin-waf - 图1note

You can fetch the admin_key from config.yaml and save to an environment variable with the following command:

  1. admin_key=$(yq '.deployment.admin.admin_key[0].key' conf/config.yaml | sed 's/"//g')
  1. curl http://127.0.0.1:9180/apisix/admin/plugin_metadata/chaitin-waf -H "X-API-KEY: $admin_key" -X PUT -d '
  2. {
  3. "nodes":[
  4. {
  5. "host": "unix:/path/to/safeline/resources/detector/snserver.sock",
  6. "port": 8000
  7. }
  8. ]
  9. }'

Attributes

NameTypeRequiredDefault valueDescription
matcharray[object]falseThe list of matching rules, default is empty
match.varsarray[array]falseList of variables to match for filtering requests for conditional traffic split. It is in the format {variable operator value}. For example, {“arg_name”, “==”, “json”}. The variables here are consistent with NGINX internal variables. For details on supported operators, lua-resty-expr.
append_waf_resp_headerboolfalsetrueWhether to add response headers
append_waf_debug_headerboolfalsefalseWhether or not to add debugging headers, effective when add_header is true.
configobjectfalseConfiguration of the Chaitin SafeLine WAF service. When the route is not configured, the parameters configured in the metadata are used.
config.connect_timeoutintegerfalseconnect timeout, in milliseconds
config.send_timeoutintegerfalsesend timeout, in milliseconds
config.read_timeoutintegerfalseread timeout, in milliseconds
config.req_body_sizeintegerfalserequest body size, in KB
config.keepalive_sizeintegerfalsemaximum concurrent idle connections to the SafeLine WAF detection service
config.keepalive_timeoutintegerfalseidle connection timeout, in milliseconds

A sample configuration is shown below, using httpbun.org as the example backend, which can be replaced as needed:

  1. curl http://127.0.0.1:9180/apisix/admin/routes/1 -H "X-API-KEY: $admin_key" -X PUT -d '
  2. {
  3. "uri": "/*",
  4. "plugins": {
  5. "chaitin-waf": {
  6. "match": [
  7. {
  8. "vars": [
  9. ["http_waf","==","true"]
  10. ]
  11. }
  12. ]
  13. }
  14. },
  15. "upstream": {
  16. "type": "roundrobin",
  17. "nodes": {
  18. "httpbun.org:80": 1
  19. }
  20. }
  21. }'

Test Plugin

Test the above example configuration.

If the match condition is not met, the request can be reached normally:

  1. curl -H "Host: httpbun.org" http://127.0.0.1:9080/get -i
  2. HTTP/1.1 200 OK
  3. Content-Type: application/json
  4. Content-Length: 408
  5. Connection: keep-alive
  6. X-APISIX-CHAITIN-WAF: no
  7. Date: Wed, 19 Jul 2023 09:30:42 GMT
  8. X-Powered-By: httpbun/3c0dc05883dd9212ac38b04705037d50b02f2596
  9. Server: APISIX/3.3.0
  10. {
  11. "args": {},
  12. "headers": {
  13. "Accept": "*/*",
  14. "Connection": "close",
  15. "Host": "httpbun.org",
  16. "User-Agent": "curl/8.1.2",
  17. "X-Forwarded-For": "127.0.0.1",
  18. "X-Forwarded-Host": "httpbun.org",
  19. "X-Forwarded-Port": "9080",
  20. "X-Forwarded-Proto": "http",
  21. "X-Real-Ip": "127.0.0.1"
  22. },
  23. "method": "GET",
  24. "origin": "127.0.0.1, 122.231.76.178",
  25. "url": "http://httpbun.org/get"
  26. }

Potential injection requests are also forwarded as is and encounter a 404 error:

  1. curl -H "Host: httpbun.org" http://127.0.0.1:9080/getid=1%20AND%201=1 -i
  2. HTTP/1.1 404 Not Found
  3. Content-Type: text/plain; charset=utf-8
  4. Content-Length: 19
  5. Connection: keep-alive
  6. X-APISIX-CHAITIN-WAF: no
  7. Date: Wed, 19 Jul 2023 09:30:28 GMT
  8. X-Content-Type-Options: nosniff
  9. X-Powered-By: httpbun/3c0dc05883dd9212ac38b04705037d50b02f2596
  10. Server: APISIX/3.3.0
  11. 404 page not found

Normal requests are still reachable when the match condition is met:

  1. curl -H "Host: httpbun.org" -H "waf: true" http://127.0.0.1:9080/get -i
  2. HTTP/1.1 200 OK
  3. Content-Type: application/json
  4. Content-Length: 427
  5. Connection: keep-alive
  6. X-APISIX-CHAITIN-WAF-TIME: 2
  7. X-APISIX-CHAITIN-WAF-STATUS: 200
  8. X-APISIX-CHAITIN-WAF: yes
  9. X-APISIX-CHAITIN-WAF-ACTION: pass
  10. Date: Wed, 19 Jul 2023 09:29:58 GMT
  11. X-Powered-By: httpbun/3c0dc05883dd9212ac38b04705037d50b02f2596
  12. Server: APISIX/3.3.0
  13. {
  14. "args": {},
  15. "headers": {
  16. "Accept": "*/*",
  17. "Connection": "close",
  18. "Host": "httpbun.org",
  19. "User-Agent": "curl/8.1.2",
  20. "Waf": "true",
  21. "X-Forwarded-For": "127.0.0.1",
  22. "X-Forwarded-Host": "httpbun.org",
  23. "X-Forwarded-Port": "9080",
  24. "X-Forwarded-Proto": "http",
  25. "X-Real-Ip": "127.0.0.1"
  26. },
  27. "method": "GET",
  28. "origin": "127.0.0.1, 122.231.76.178",
  29. "url": "http://httpbun.org/get"
  30. }

Potential attack requests will be intercepted and returned a 403 error:

  1. curl -H "Host: httpbun.org" -H "waf: true" http://127.0.0.1:9080/getid=1%20AND%201=1 -i
  2. HTTP/1.1 403 Forbidden
  3. Date: Wed, 19 Jul 2023 09:29:06 GMT
  4. Content-Type: text/plain; charset=utf-8
  5. Transfer-Encoding: chunked
  6. Connection: keep-alive
  7. X-APISIX-CHAITIN-WAF: yes
  8. X-APISIX-CHAITIN-WAF-TIME: 2
  9. X-APISIX-CHAITIN-WAF-ACTION: reject
  10. X-APISIX-CHAITIN-WAF-STATUS: 403
  11. Server: APISIX/3.3.0
  12. Set-Cookie: sl-session=UdywdGL+uGS7q8xMfnJlbQ==; Domain=; Path=/; Max-Age=86400
  13. {"code": 403, "success":false, "message": "blocked by Chaitin SafeLine Web Application Firewall", "event_id": "51a268653f2c4189bfa3ec66afbcb26d"}

Delete Plugin

To remove the chaitin-waf plugin, you can delete the corresponding JSON configuration from the Plugin configuration. APISIX will automatically reload and you do not have to restart for this to take effect:

  1. $ curl http://127.0.0.1:9180/apisix/admin/routes/1 -H "X-API-KEY: $admin_key" -X PUT -d '
  2. {
  3. "uri": "/*",
  4. "upstream": {
  5. "type": "roundrobin",
  6. "nodes": {
  7. "httpbun.org:80": 1
  8. }
  9. }
  10. }'