22

Web防护自给自足:给Express写一个WAF中间件!

 4 years ago
source link: https://www.freebuf.com/articles/web/223735.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

NodeJS做为后端神器是很流行的。

Node的后端程序,绝大多数都用大名鼎鼎的Express做Web服务。

本文将展示如何为Express写一个WAF中间件,为Web服务做安防,防止常见的如SQL注入、XSS等黑客攻击。

EjYveu3.jpg!web

首先奉上完整演示代码:

var express = require('express');
var app = express();
//当访问根目录时触发
app.get('/', function (req, res) {
   res.send('Hello ShareWAF.com');
})
//WAF中间件
app.use(function(req, res, next) {
    var path = req.url;
    if(waf_detect(path) == false){
        next();
    }
});
//使用正则表达式,检测字符串是否含有攻击特征,检测到攻击特征返回true,没检测到返回false
function waf_detect(str_to_detect){
		//Rule from ShareWAF(sharewaf.com)
    var regexp_rule =[
        /select.+(from|limit)/i,
        /(?:(union(.*?)select))/i,
        /sleep\((\s*)(\d*)(\s*)\)/i,
        /group\s+by.+\(/i,
        /(?:from\W+information_schema\W)/i,
        /(?:(?:current_)user|database|schema|connection_id)\s*\(/i,
        /\s*or\s+.*=.*/i,
        /order\s+by\s+.*--$/i,
        /benchmark\((.*)\,(.*)\)/i,
        /base64_decode\(/i,
        /(?:(?:current_)user|database|version|schema|connection_id)\s*\(/i,
        /(?:etc\/\W*passwd)/i,
        /into(\s+)+(?:dump|out)file\s*/i,
        /xwork.MethodAccessor/i,
        /(?: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)\(/i,
        /\<(iframe|script|body|img|layer|div|meta|style|base|object|input)/i,
        /(onmouseover|onmousemove|onerror|onload)\=/i,
        /javascript:/i,
        /\.\.\/\.\.\//i,
        /\|\|.*(?:ls|pwd|whoami|ll|ifconfog|ipconfig|&&|chmod|cd|mkdir|rmdir|cp|mv)/i,
        /(?:ls|pwd|whoami|ll|ifconfog|ipconfig|&&|chmod|cd|mkdir|rmdir|cp|mv).*\|\|/i,
        /(gopher|doc|php|glob|file|phar|zlib|ftp|ldap|dict|ogg|data)\:\//i
    ];
    for(i=0; i< regexp_rule.length; i++){
        if(regexp_rule[i].test(str_to_detect) == true){
			console.log("attack detected, rule number:", "("+i+")", regexp_rule[i]);
			return true;
        }
    }
    return false;
}
var server = app.listen(8000, function () {
   var host = server.address().address
   var port = server.address().port
})

本示例是一个由Express实现的的,带有WAF功能的Web应用。

代码重点演示WAF中间件,实现WAF防护逻辑的核心部分代码:

//WAF中间件
app.use(function(req, res, next) {
    var path = req.url;
    if(waf_detect(path) == false){
        next();
    }
});

这个简洁的中间件会对客户发起的请求进行过滤,判断请求路径中是否有恶意指令。如果有,则不让中件间进行next(),即禁止请求继续,如此则请求被中断。

恶意指令检测使用的是正则表达式,这是WAF常用的检测方式,这套规则来自ShareWAF。

//Rule from ShareWAF(sharewaf.com)
var regexp_rule =[
        /select.+(from|limit)/i,
        /(?:(union(.*?)select))/i,
        /sleep\((\s*)(\d*)(\s*)\)/i,
        /group\s+by.+\(/i,
        /(?:from\W+information_schema\W)/i,
        /(?:(?:current_)user|database|schema|connection_id)\s*\(/i,
        /\s*or\s+.*=.*/i,
        /order\s+by\s+.*--$/i,
        /benchmark\((.*)\,(.*)\)/i,
        /base64_decode\(/i,
        /(?:(?:current_)user|database|version|schema|connection_id)\s*\(/i,
        /(?:etc\/\W*passwd)/i,
        /into(\s+)+(?:dump|out)file\s*/i,
        /xwork.MethodAccessor/i,
        /(?: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)\(/i,
        /\<(iframe|script|body|img|layer|div|meta|style|base|object|input)/i,
        /(onmouseover|onmousemove|onerror|onload)\=/i,
        /javascript:/i,
        /\.\.\/\.\.\//i,
        /\|\|.*(?:ls|pwd|whoami|ll|ifconfog|ipconfig|&&|chmod|cd|mkdir|rmdir|cp|mv)/i,
        /(?:ls|pwd|whoami|ll|ifconfog|ipconfig|&&|chmod|cd|mkdir|rmdir|cp|mv).*\|\|/i,
        /(gopher|doc|php|glob|file|phar|zlib|ftp|ldap|dict|ogg|data)\:\//i
    ];

正则表达式的规则,逻辑很清晰,比如/select.+(from|limit)/i含意是不区分大小写,检测是含包含selet接任意字符然后出现from或limit关键字,这是典型的SQL注入检测规则。

这份规则只是WAF正则检测的一部分,而且如果对正则表达式较熟悉,可以自己编写规则,扩展检测能力。

用Node启动上面的程序,并模拟访问发起攻击:

http://127.0.0.1:8000/index.html?select * from admin

即在url中传入select*from admin这句最常见的SQL注入攻击指令:

32E3maF.jpg!web

可以看到,网站无法打开。且在后台输出了拦截信息,并提示出触发了哪条WAF防护规则。

本文仅做演示,只检测了url路径。加以括展的,对cookie、user-agent、post等常见攻击字段进行检测,即可成为一套基于规则匹配的、正二八经的WAF中间件!

*本文原创作者:w2sfoot,本文属于FreeBuf原创奖励计划,未经许可禁止转载


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK