__( 'SQL injection', 'ai-guardwall' ), 'xss' => __( 'Cross-site scripting (XSS)', 'ai-guardwall' ), 'lfi' => __( 'File inclusion / path traversal', 'ai-guardwall' ), 'rce' => __( 'Code and command execution', 'ai-guardwall' ), 'upload' => __( 'Malicious file upload', 'ai-guardwall' ), 'scanner_ua' => __( 'Known attack tools (user agent)', 'ai-guardwall' ), 'bad_paths' => __( 'Sensitive and exploit paths', 'ai-guardwall' ), 'php_in_uploads' => __( 'PHP requested inside uploads', 'ai-guardwall' ), 'xmlrpc_multicall' => __( 'XML-RPC brute-force amplification', 'ai-guardwall' ), ); } private static function patterns() { return array( 'sqli' => '/(?:\bunion\b[\s\/\*]+(?:all[\s\/\*]+)?\bselect\b)|(?:\bselect\b[\s\S]{1,120}\bfrom\b[\s\S]{1,60}\b(?:information_schema|mysql\.user|wp_users|wp_options)\b)|(?:\b(?:sleep|benchmark|pg_sleep)\s*\(\s*\d)|(?:\binto\s+(?:out|dump)file\b)|(?:\bload_file\s*\()|(?:[\'"`]\s*(?:or|and)\s*[\'"`]?\d+[\'"`]?\s*=\s*[\'"`]?\d+)|(?:\bor\s+1\s*=\s*1\b)|(?:\/\*![0-9]*\s*(?:union|select))|(?:@@(?:version|datadir|basedir))|(?:\bconcat\s*\(\s*0x)|(?:\bxp_cmdshell\b)|(?:\bupdatexml\s*\()|(?:\bextractvalue\s*\()|(?:\bgroup_concat\s*\([^)]*(?:user_login|user_pass))/i', 'xss' => '/]*\bon\w+\s*=|document\.cookie|String\.fromCharCode\s*\(|]*\bonerror|expression\s*\(|vbscript\s*:|data:text\/html|]*http-equiv/i', 'lfi' => '/(?:(?:\.\.\/|\.\.\\\\){2,})|(?:\/etc\/(?:passwd|shadow|group|hosts)\b)|(?:\/proc\/self\/environ)|(?:php:\/\/(?:input|filter|data|memory))|(?:data:\/\/)|(?:expect:\/\/)|(?:zip:\/\/)|(?:phar:\/\/)|(?:glob:\/\/)|(?:\bwp-config\.php\b)|(?:%2e%2e%2f)|(?:\.\.%2f)|(?:%2e%2e\/)|(?:\/windows\/win\.ini)/i', 'rce' => '/(?:\b(?:eval|assert|system|passthru|shell_exec|exec|popen|proc_open|pcntl_exec|base64_decode|gzinflate|str_rot13|create_function|call_user_func)\s*\()|(?:<\?php)|(?:\bphpinfo\s*\()|(?:allow_url_include|auto_prepend_file|auto_append_file)|(?:\$_(?:GET|POST|REQUEST|COOKIE|SERVER|FILES)\s*\[)|(?:\b(?:curl|wget)\s+(?:-[a-zA-Z]+\s+)*https?:\/\/[^\s]+\s*(?:\||;|>))|(?:\/bin\/(?:ba|z|da)?sh\b)|(?:\bcmd\.exe\b)|(?:;\s*(?:ls|cat|id|whoami|uname|wget|curl|chmod|rm)\s+-?[a-zA-Z\/])|(?:\$\{IFS\})|(?:\bbase64\s+-d\b)/i', 'scanner_ua'=> '/\b(?:sqlmap|nikto|nmap|masscan|wpscan|acunetix|nessus|netsparker|zgrab|dirbuster|gobuster|ffuf|nuclei|w3af|arachni|havij|jbrofuzz|libwww-perl|zmeu|morfeus|fimap|commix|xsser|wfuzz|joomscan|droopescan|cmsmap|whatweb|bbscan|backdoorbot|projectdiscovery|burpcollaborator|python-urllib\/\d|Jorgee|MJ12bot\/v1\.0\.8|PetalBot\/0)\b/i', 'bad_paths' => '/(?:\/\.(?:env|git|svn|hg|DS_Store|htpasswd|bash_history|ssh)(?:\/|$|\?))|(?:\/vendor\/phpunit\/.*eval-stdin\.php)|(?:\/wp-config\.php(?:\.(?:bak|old|orig|save|txt|swp|dist))?(?:$|\?))|(?:\/wp-admin\/(?:install|setup-config)\.php)|(?:\/(?:phpmyadmin|pma|adminer|myadmin|mysql|dbadmin)\/)|(?:\.(?:sql|sql\.gz|bak|orig|swp)(?:$|\?))|(?:\/wp-content\/debug\.log(?:$|\?))|(?:\/(?:cgi-bin|fckeditor|ckfinder)\/.*\.(?:php|cgi))|(?:\/wp-includes\/[^\/]+\.php\?)|(?:\/(?:shell|c99|r57|wso|alfa|xleet|bypass|backdoor|webshell|mini|cmd|uploader|up|upl|x|xx|xxx|sx|lol|about|radio|moon|chosen|content|wp-content\/plugins\/[^\/]+\/)?(?:shell|c99|r57|wso|alfa|xleet|bypass|backdoor|webshell)\d*\.php(?:$|\?))/i', ); } /** * Inspect a request. * * @since 1.0.0 * @param array $req ['uri','get','post','cookies','ua','files','body'] * @param array $enabled rule id => bool * @return array[] matches: array( rule, where, sample ) */ public static function inspect( $req, $enabled ) { $matches = array(); $patterns = self::patterns(); $uri = isset( $req['uri'] ) ? (string) $req['uri'] : ''; if ( ! empty( $enabled['php_in_uploads'] ) && preg_match( '#/wp-content/uploads/.*\.(?:php\d?|phtml|phar|pht|phps)(?:$|\?|/|%00)#i', $uri ) ) { $matches[] = array( 'rule' => 'php_in_uploads', 'where' => 'uri', 'sample' => self::sample( $uri ) ); } if ( ! empty( $enabled['bad_paths'] ) && preg_match( $patterns['bad_paths'], $uri ) ) { $matches[] = array( 'rule' => 'bad_paths', 'where' => 'uri', 'sample' => self::sample( $uri ) ); } if ( ! empty( $enabled['scanner_ua'] ) && isset( $req['ua'] ) && preg_match( $patterns['scanner_ua'], (string) $req['ua'] ) ) { $matches[] = array( 'rule' => 'scanner_ua', 'where' => 'user-agent', 'sample' => self::sample( (string) $req['ua'] ) ); } if ( ! empty( $enabled['upload'] ) && ! empty( $req['files'] ) ) { foreach ( (array) $req['files'] as $name ) { if ( preg_match( '/\.(?:php\d?|phtml|phar|pht|phps|shtml|cgi|pl|py|sh|htaccess|ini|exe|dll)(?:\.|$)/i', (string) $name ) ) { $matches[] = array( 'rule' => 'upload', 'where' => 'files', 'sample' => self::sample( (string) $name ) ); break; } } } if ( ! empty( $enabled['xmlrpc_multicall'] ) && preg_match( '#/xmlrpc\.php#', $uri ) && isset( $req['body'] ) && false !== stripos( (string) $req['body'], 'system.multicall' ) ) { $matches[] = array( 'rule' => 'xmlrpc_multicall', 'where' => 'body', 'sample' => 'system.multicall' ); } // Parameter inspection. $values = array(); self::flatten( isset( $req['get'] ) ? $req['get'] : array(), 'get', $values ); self::flatten( isset( $req['post'] ) ? $req['post'] : array(), 'post', $values ); self::flatten( isset( $req['cookies'] ) ? $req['cookies'] : array(), 'cookie', $values ); $query = (string) wp_parse_url( $uri, PHP_URL_QUERY ); if ( '' !== $query ) { $values[] = array( 'where' => 'query', 'value' => $query ); } $values[] = array( 'where' => 'path', 'value' => (string) wp_parse_url( $uri, PHP_URL_PATH ) ); foreach ( array( 'sqli', 'xss', 'lfi', 'rce' ) as $rule ) { if ( empty( $enabled[ $rule ] ) ) { continue; } foreach ( $values as $entry ) { if ( 'cookie' === $entry['where'] && 'xss' === $rule ) { continue; // Cookies carry HTML from some plugins (cart contents); XSS there cannot execute server-side. } foreach ( self::variants( $entry['value'] ) as $variant ) { if ( preg_match( $patterns[ $rule ], $variant ) ) { $matches[] = array( 'rule' => $rule, 'where' => $entry['where'], 'sample' => self::sample( $variant ) ); continue 3; } } } } return $matches; } private static function flatten( $data, $where, array &$out, $depth = 0 ) { if ( $depth > 6 || count( $out ) > 600 ) { return; } foreach ( (array) $data as $key => $value ) { if ( is_array( $value ) ) { self::flatten( $value, $where, $out, $depth + 1 ); } elseif ( is_scalar( $value ) ) { $out[] = array( 'where' => $where, 'value' => substr( (string) $key, 0, 200 ) . '=' . substr( (string) $value, 0, 30000 ) ); } } } private static function variants( $value ) { $value = str_replace( "\0", '', (string) $value ); $list = array( $value ); $d1 = rawurldecode( $value ); if ( $d1 !== $value ) { $list[] = $d1; } $d2 = rawurldecode( $d1 ); if ( $d2 !== $d1 ) { $list[] = $d2; } $h = html_entity_decode( $d2, ENT_QUOTES | ENT_HTML5, 'UTF-8' ); if ( $h !== $d2 ) { $list[] = $h; } return $list; } private static function sample( $text ) { return Softvolt_Ai_Bodyguard_Helpers::trim_text( preg_replace( '/\s+/', ' ', (string) $text ), 200 ); } }