Files
MalinandClaude Opus 5 c67e0edebc feat: X-Accel-Redirect como opcion (xaccel_403) en vez de parche a mano; v1.0.6
Hasta ahora esto era un parche a mano en class-itk-bot-blocker.php, aplicado en
31 sitios (venus 11, funky 19, neptune 1) y nunca subido al repo. El coste
medido el 2026-11-13: en bugsy, donde nginx corre en el HOST y no dentro de la
jaula, la localizacion /__iwp_403 no existe y TODO bloqueo devolvio 500 durante
dos semanas sin que nadie lo notara, porque un bot no se queja. Y cada
actualizacion obligaba a mantener dos variantes del fichero.

Ahora `itk_security['xaccel_403']`: vacio (por defecto) = el comportamiento de
siempre desde PHP; una ruta absoluta = se delega en nginx. Se aplica a las
CUATRO rutas de bloqueo (WAF, protection, bot-blocker y el 429 de ritmo), no
solo a una, que era la otra incoherencia.

El valor acaba en una cabecera HTTP, asi que se valida en el lector y no solo en
el formulario: exige ruta absoluta y rechaza \r y \n. Probado con un arnes
aislado: 10 casos, incluidas dos inyecciones de cabecera y una opcion corrupta.
Campo anadido al panel con la advertencia de que la localizacion tiene que
existir en nginx o cada bloqueo da 500.

php -l limpio en los cinco ficheros.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-11-13 20:44:20 +01:00

320 lines
13 KiB
PHP

<?php
if (!defined('ABSPATH')) exit;
/**
* ITK WAF — Web Application Firewall
*
* Loads rules from config/waf-rules.conf and inspects incoming requests
* (GET params, REQUEST_URI, optionally POST, cookies, and User-Agent) for
* known attack signatures. On a match it logs, notifies the attacks API,
* and either blocks or logs-only depending on the configured action.
*
* Options key: itk_waf (array)
* enabled bool Master switch.
* action string 'block' (default) or 'log_only'.
* scan_post bool Also inspect $_POST values.
* scan_cookies bool Also inspect $_COOKIE values.
* scan_ua bool Also inspect HTTP_USER_AGENT.
* log_attacks bool Persist matches to the DB via ITK_Database::log_attack().
* response_code string HTTP status code to send when blocking (e.g. '403', '301_custom').
* redirect_url string Destination URL when response_code === '301_custom'.
* custom_message string Body text sent with non-redirect block responses.
* block_{cat} bool Per-category enable flag (e.g. block_sqli, block_xss, …).
*/
class ITK_WAF {
/** Transient key for the cached parsed ruleset. */
const TRANSIENT_KEY = 'itk_waf_rules';
/** Transient lifetime in seconds. */
const CACHE_TTL = 300;
/** Absolute path to the rules file. */
private string $rules_file;
/* ── Bootstrap ────────────────────────────────────────────── */
public function __construct() {
$options = get_option('itk_waf', []);
if (empty($options['enabled'])) return;
$this->rules_file = ITK_PATH . 'config/waf-rules.conf';
add_action('init', [$this, 'inspect'], 0);
}
/* ── Main inspection hook ─────────────────────────────────── */
/**
* Runs at priority 0 on 'init'. Inspects the current request against
* all enabled WAF rules and calls handle_match() on the first hit.
*/
/**
* Flatten a (possibly nested) request value into scalar leaves so that
* nested params (e.g. foo[bar]=payload) are inspected too. Casting an array
* straight to string yields "Array" (a PHP warning) and silently skips the
* payload -- both log noise and a WAF bypass.
*/
private function collect_inputs($value, string $source, string $key, array &$inputs): void {
if (is_array($value)) {
foreach ($value as $k => $v) {
$this->collect_inputs($v, $source, $key . '[' . $k . ']', $inputs);
}
return;
}
$inputs[$source][$key] = (string)$value;
}
public function inspect(): void {
// Never block logged-in administrators or wp-admin non-AJAX requests.
if (is_admin() && !wp_doing_ajax()) return;
if (function_exists('current_user_can') && current_user_can('manage_options')) return;
// Skip all WAF checks for whitelisted IPs/UAs.
if (ITK_Whitelist::allowed()) return;
$options = get_option('itk_waf', []);
$rules = $this->load_rules();
if (empty($rules)) return;
// ── Build input map ────────────────────────────────────
$inputs = [];
// Always scan GET params (nested arrays flattened).
foreach ($_GET as $key => $value) {
$this->collect_inputs($value, 'GET', (string)$key, $inputs);
}
// Always scan REQUEST_URI.
$inputs['URI']['REQUEST_URI'] = (string)($_SERVER['REQUEST_URI'] ?? '');
// Optionally scan POST.
if (!empty($options['scan_post'])) {
foreach ($_POST as $key => $value) {
$this->collect_inputs($value, 'POST', (string)$key, $inputs);
}
}
// Optionally scan cookies.
if (!empty($options['scan_cookies'])) {
foreach ($_COOKIE as $key => $value) {
$this->collect_inputs($value, 'COOKIE', (string)$key, $inputs);
}
}
// Optionally scan User-Agent.
if (!empty($options['scan_ua'])) {
$ua = (string)($_SERVER['HTTP_USER_AGENT'] ?? '');
if ($ua !== '') {
$inputs['UA']['HTTP_USER_AGENT'] = $ua;
}
}
// ── Match loop ─────────────────────────────────────────
foreach ($inputs as $source => $params) {
foreach ($params as $key => $raw_value) {
// Decode URL-encoding so encoded payloads are matched.
$value = rawurldecode(urldecode($raw_value));
foreach ($rules as $rule) {
// Skip categories disabled in options.
$opt_key = 'block_' . $rule['category'];
if (empty($options[$opt_key])) continue;
if (@preg_match($rule['pattern'], $value)) {
$this->handle_match($rule, $source, (string)$key, $raw_value);
return; // Stop on first match; handle_match() may exit().
}
}
}
}
}
/* ── Rule loading ─────────────────────────────────────────── */
/**
* Reads and parses config/waf-rules.conf.
* Results are cached in a transient for CACHE_TTL seconds.
*
* @return array<int, array{category: string, pattern: string, desc: string}>
*/
public function load_rules(): array {
$cached = get_transient(self::TRANSIENT_KEY);
if (is_array($cached)) return $cached;
$rules = [];
if (!file_exists($this->rules_file)) {
set_transient(self::TRANSIENT_KEY, $rules, self::CACHE_TTL);
return $rules;
}
$lines = file($this->rules_file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
foreach ($lines as $line) {
$line = trim($line);
// Skip comment and header lines.
if ($line === '' || $line[0] === '#') continue;
$parts = explode('|', $line, 3);
if (count($parts) < 3) continue;
[$category, $pattern, $desc] = $parts;
$category = trim($category);
$pattern = trim($pattern);
$desc = trim($desc);
if ($category === '' || $pattern === '') continue;
// Wrap bare patterns (no delimiter) with # delimiters so they are
// valid PCRE. Patterns that already start with a delimiter are
// used as-is.
$delimiters = ['/', '#', '~', '!', '@', '%'];
if (!in_array($pattern[0], $delimiters, true)) {
$pattern = '#' . $pattern . '#';
}
// Validate the regex before adding it to the ruleset.
if (@preg_match($pattern, '') === false) continue;
$rules[] = [
'category' => $category,
'pattern' => $pattern,
'desc' => $desc,
];
}
set_transient(self::TRANSIENT_KEY, $rules, self::CACHE_TTL);
return $rules;
}
/* ── Match handler ────────────────────────────────────────── */
/**
* Handles a rule match: logs the event, queues it for the attacks API,
* then either returns (log-only mode) or terminates the request.
*
* @param array $rule Parsed rule: category, pattern, desc.
* @param string $source Input source label: GET, POST, COOKIE, URI, UA.
* @param string $key Parameter name that triggered the rule.
* @param string $value Raw parameter value.
*/
public function handle_match(array $rule, string $source, string $key, string $value): void {
$options = get_option('itk_waf', []);
$event = [
'ip' => $this->get_ip(),
'attack_type' => $rule['category'],
'rule_desc' => $rule['desc'],
'source' => $source,
'param' => $key,
'payload' => substr($value, 0, 500),
'uri' => (string)($_SERVER['REQUEST_URI'] ?? ''),
'method' => (string)($_SERVER['REQUEST_METHOD'] ?? ''),
'ua' => (string)($_SERVER['HTTP_USER_AGENT'] ?? ''),
];
// Persist to local DB if logging is enabled.
if (!empty($options['log_attacks']) && class_exists('ITK_Database') &&
method_exists('ITK_Database', 'log_attack')) {
ITK_Database::log_attack($event);
}
// Queue for the attacks API.
if (class_exists('ITK_Attacks_API') && method_exists('ITK_Attacks_API', 'queue')) {
ITK_Attacks_API::queue($event);
}
// Log-only mode: record the event but do not block the request.
$action = $options['action'] ?? 'block';
if ($action === 'log_only') return;
// ── Block the request ──────────────────────────────────
$response_code = $options['response_code'] ?? '403';
$redirect_url = $options['redirect_url'] ?? '';
$custom_message = $options['custom_message'] ?? 'Access denied.';
// A 301 on a non-idempotent request (POST/PUT/PATCH/DELETE) is
// silently fatal: a server-to-server client does NOT re-send its body
// to the redirect target, so the request is lost with no visible
// error. Only redirect safe methods (GET/HEAD); block the rest with a
// real status code. (Payment-gateway source networks are whitelisted
// in ITK_Whitelist, so a well-behaved gateway never reaches this path.)
$method = strtoupper((string)($_SERVER['REQUEST_METHOD'] ?? 'GET'));
$redirectable = ($method === 'GET' || $method === 'HEAD');
// A block response must NEVER be cacheable. It is a decision about ONE
// request from ONE client, and a shared cache in front of us will store
// it under the plain URL and then serve it to everybody.
//
// Measured 2026-11-13: ads.galexbeauty.es ran for a few minutes with
// `response_code` at its shipped default '301_custom' towards
// 'https://example.com/blocked'. Souin, the Caddy cache on the edge
// POPs, stored that 301 for /wp-login.php and kept serving it long
// after the origin was fixed -- and only to some visitors, so it
// imitated an IP allowlist perfectly and cost real time to diagnose.
// WordPress's own login page carries `no-store` and was never the
// problem; this short-circuit, which cuts in before WordPress sets any
// headers, was.
//
// nocache_headers() is core's own helper and emits exactly what is
// wanted: `no-cache, must-revalidate, max-age=0, no-store, private`
// plus a past Expires. It lives in wp-includes/functions.php, loaded
// well before any plugin, so it is always available on this path.
nocache_headers();
// Si el sitio delega los bloqueos en nginx (opcion `xaccel_403`), se
// entrega ahi y se sale. Vacia por defecto: sin configurar, el
// comportamiento es exactamente el de siempre.
if (itk_block_via_nginx($response_code === '301_custom' ? 403 : ((int) $response_code ?: 403))) {
exit;
}
if ($response_code === '301_custom' && !empty($redirect_url) && $redirectable) {
header('Location: ' . esc_url_raw($redirect_url), true, 301);
} else {
status_header($response_code === '301_custom' ? 403 : ((int)$response_code ?: 403));
echo esc_html($custom_message);
}
exit;
}
/* ── IP resolution ────────────────────────────────────────── */
/**
* Returns the best available client IP address, checking proxy headers
* in order of trustworthiness. Mirrors the pattern used by other ITK
* classes.
*/
public function get_ip(): string {
$keys = [
'HTTP_CLIENT_IP',
'HTTP_X_FORWARDED_FOR',
'HTTP_X_FORWARDED',
'HTTP_X_CLUSTER_CLIENT_IP',
'HTTP_FORWARDED_FOR',
'HTTP_FORWARDED',
'REMOTE_ADDR',
];
foreach ($keys as $key) {
if (empty($_SERVER[$key])) continue;
// X-Forwarded-For may contain a comma-separated list; take first.
$ip = trim(explode(',', $_SERVER[$key])[0]);
if (filter_var($ip, FILTER_VALIDATE_IP)) return $ip;
}
return 'UNKNOWN';
}
/* ── Cache management ─────────────────────────────────────── */
/**
* Deletes the cached ruleset transient so the next request re-parses
* the rules file. Call this after saving updated rules.
*/
public function invalidate_cache(): void {
delete_transient(self::TRANSIENT_KEY);
}
}