分享一個php的防火牆,攔截SQL注入和xss

這個是一個基於php的防火牆程序,攔截sql注入和xss攻擊,無需服務器支持php

爲何須要這樣一個防火牆呢?由於不少如今不少服務器並無開啓網站防火牆,開發者的水平有高有低,mysql

參考nginx上最多star的防火牆程序編寫https://github.com/loveshell/ngx_lua_waf.gitlinux

image

安裝

composer require xielei/waf

使用說明

$waf = new \Xielei\Waf\Waf();
$waf->run();
一般請在用戶端頁面引入該防火牆,後端操做沒必要。

自定義攔截規則

$rules = [
    '\.\./', //禁用包含 ../ 的參數
    '\<\?', //禁止php腳本出現
    '\s*or\s+.*=.*', //匹配' or 1=1 ,防止sql注入
    'select([\s\S]*?)(from|limit)', //防止sql注入
    '(?:(union([\s\S]*?)select))', //防止sql注入
    'having|updatexml|extractvalue', //防止sql注入
    'sleep\((\s*)(\d*)(\s*)\)', //防止sql盲注
    'benchmark\((.*)\,(.*)\)', //防止sql盲注
    'base64_decode\(', //防止sql變種注入
    '(?:from\W+information_schema\W)', //防止sql注入
    '(?:(?:current_)user|database|schema|connection_id)\s*\(', //防止sql注入
    '(?:etc\/\W*passwd)', //防止窺探linux用戶信息
    'into(\s+)+(?:dump|out)file\s*', //禁用mysql導出函數
    'group\s+by.+\(', //防止sql注入
    '(?:define|eval|file_get_contents|include|require|require_once|shell_exec|phpinfo|system|passthru|preg_\w+|execute|echo|print|print_r|var_dump|(fp)open|alert|showmodaldialog)\(', //禁用webshell相關某些函數
    '(gopher|doc|php|glob|file|phar|zlib|ftp|ldap|dict|ogg|data)\:\/', //防止一些協議攻擊
    '\$_(GET|post|cookie|files|session|env|phplib|GLOBALS|SERVER)\[', //禁用一些內置變量,建議自行修改
    '\<(iframe|script|body|img|layer|div|meta|style|base|object|input)', //防止xss標籤植入
    '(onmouseover|onerror|onload|onclick)\=', //防止xss事件植入
    '\|\|.*(?:ls|pwd|whoami|ll|ifconfog|ipconfig|&&|chmod|cd|mkdir|rmdir|cp|mv)', //防止執行shell
    '\s*and\s+.*=.*' //匹配 and 1=1
];
$waf = new \Xielei\Waf($rules);
$waf->run();

自定義攔截頁面

$waf = new \Xielei\Waf\Waf();
if(!$waf->check()){
    echo '非法請求';
    die;
}

開源地址

https://github.com/xielei/wafnginx

相關文章
相關標籤/搜索