From 2010bd982ef630ba9f8115aa8104fceec83cc1b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A2=D0=B0=D1=82=D1=8C=D1=8F=D0=BD=D0=B0=20=D0=A4=D0=B0?= =?UTF-8?q?=D1=80=D0=B1=D0=B5=D1=80?= Date: Thu, 22 Oct 2026 00:40:04 +0400 Subject: format --- README.md | 22 ++++++++++ example2.lua | 85 ++++++++++++++++++++++++++++++++++++ index.html | 138 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 245 insertions(+) create mode 100644 example2.lua create mode 100644 index.html diff --git a/README.md b/README.md index 4a78afb..a40ef5a 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,8 @@ UPDATE: добавлена работа со спецификой уязвимо • example.lua — Пример реализации на фазе access_by_lua 7 уровня OSI (фаза блока http). +• example2.lua — Пример реализации с возвратом данных инцендента для заглушки. + • waf/config.lua — Единая точка конфигурации лимитов, белых списков и штрафных матриц. • waf/ban.lua — Двухуровневый кэш блокировок (Локальная L1 таблица воркера ➡️ L2 shared_dict). @@ -91,3 +93,23 @@ http { } ``` + +3. С заглушкой + +``` + error_page 403 = @waf_block_page; + + location @waf_block_page { + root /home/html; + rewrite ^(.*)$ /index.html break; + ssi on; + access_log off; + } + + server { + listen 443 ssl; + server_name test.ru; + access_by_lua_file /home/example2.lua; + } + +``` diff --git a/example2.lua b/example2.lua new file mode 100644 index 0000000..4fc41f2 --- /dev/null +++ b/example2.lua @@ -0,0 +1,85 @@ +local detector = require('waf.detector') +local analyzer = require('waf.analyzer') +local filter = require('waf.filter') +local limits = require('waf.limits') +local config = require('waf.config') +local ban = require('waf.ban') +local log = require('utils.log') + +local duration = config.duration +local valid_ips = config.ips +local ban_add = ban.add +local ban_check = ban.check +local headers +local meta +local client_ip = ngx.var.remote_addr + +local function answer(reason_module, level) + if not reason_module then return ngx.exit(444) end + local ray_id = string.sub(ngx.md5(ngx.time() .. client_ip), 1, 16) + ngx.header['X-WAF-Ray-ID'] = ray_id + ngx.header['X-WAF-Block-Module'] = reason_module or 'system' + ngx.header['X-WAF-Client-IP'] = client_ip + log.warn('🚨 WAF BLOCK: IP ' .. client_ip .. ' | RayID: ' .. ray_id .. ' | Модуль: ' .. (reason_module or 'unknown')) + return ngx.exit(403) +end + +local function valid(ip) + for k=1,#valid_ips do + if valid_ips[k] == ip then return end + end + return false +end + +log.name = 'waf' +log.level = 'trace' + +if valid(client_ip) then return true end + +if ban_check(client_ip) then return ngx.exit(444) end +if ngx.var.is_bot == '1' then return answer('bot_map') end + +if not detector then + log.error('Не найден детектор') +else + headers = ngx.req.get_headers() + meta = { + method = ngx.var.request_method, + uri = ngx.var.uri + } + local res,level = detector.check(headers,meta) + if not res then + ban_add(client_ip, duration/level) + if level==1 then return answer('detector') end + end +end + +if not limits then + log.error('Не найден лимитизатор') +else + local res,level = limits.check(meta,client_ip) + if not res then + ban_add(client_ip, duration/level) + if level==1 then return answer('rate_limiting') end + end +end + +if not analyzer then + log.error('Не найден анализатор') +else + local res,level = analyzer.check() + if not res then + ban_add(client_ip, duration/level) + if level==1 then return answer('uri_analyzer') end + end +end + +if not filter then + log.error('Не найден фильтр') +else + local res = filter.check() + if not res then + ban_add(client_ip, duration) + return answer('body_filter') + end +end diff --git a/index.html b/index.html new file mode 100644 index 0000000..20c30cc --- /dev/null +++ b/index.html @@ -0,0 +1,138 @@ + + + + + + Доступ ограничен | Защита WAF + + + + +
+
+
🛑
+

Error 1020: Access Denied

+
+ +

Ваш запрос был автоматически заблокирован глобальным контуром безопасности Web Application Firewall (WAF). Система зафиксировала аномальное поведение, нарушающее установленные политики сетевой защиты.

+ +
+
+
Your IP:
+ +
+ +
Protected Context
+ +
+
+
WAF Ray ID:
+ +
+ +
N/A
+ +
+
+
Block Reason:
+ +
+ +
security_rule_match
+ +
+
+ + + + +
+ + + + -- cgit v1.2.3