事出有因
最近web系統引來了黑客的攻擊,經常被掃描,各種漏洞嘗試。
分析攻擊日誌,有幾種常見的攻擊手段:
- 上傳webshell
- 遠程執行命令漏洞
- sql注入
- xxs 攻擊
- 試探各種開源框架爆出來的漏洞
分析攻擊信息的特點
說白了就是採用web滲透技術,利用http請求,黑客想盡辦法,在http header ,body,等部分植入非法的命令,非法字符常見的有:exe,cmd,powershell,download,select,union,delete等等。
解決問題思路
- 我們能不能開發個代理服務器,來分析http請求header,body裏面的信息,如果有非法字符,就截斷,拒絕服務。
- 配置允許請求的白名單,拒絕非法Url.
技術選型
常見的代理服務器有nginx,apache,不知道這2個代理服務器能不能靈活的配置,過濾,轉發,沒有深入瞭解。
因此選用nodejs http-proxy。
nodejs優點
- 輕量級
- 快速部署
- 靈活開發
- 高吞吐,異步io
編碼實現邏輯圖
絕對乾貨,分享代碼
代碼依賴
- http-proxy 1.17.0
https://github.com/nodejitsu/... 代碼地址
- "colors": "~0.6.2",
var util = require('util'),
colors = require('colors'),
http = require('http'),
httpProxy = require('./node_modules/http-proxy');
fs = require("fs");
var welcome = [
'# # ##### ##### ##### ##### ##### #### # # # #',
'# # # # # # # # # # # # # # # # ',
'###### # # # # ##### # # # # # # ## # ',
'# # # # ##### ##### ##### # # ## # ',
'# # # # # # # # # # # # # ',
'# # # # # # # # #### # # # '
].join('\n');
Date.prototype.Format = function(fmt) { //author: meizz
var o = {
"M+": this.getMonth() + 1, //月份
"d+": this.getDate(), //日
"h+": this.getHours(), //小時
"m+": this.getMinutes(), //分
"s+": this.getSeconds(), //秒
"S": this.getMilliseconds() //毫秒
};
if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
for (var k in o)
if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
return fmt;
}
// 非法字符
var re = /php|exe|cmd|shell|select|union|delete|update|insert/;
/** 這裏配置轉發
*/
var proxyPassConfig = {
"/hello": "http://www.qingmiaokeji.cn ",
"/": "http://127.0.0.1/"
}
var logRootPath ="g:/httpproxy/";
console.log(welcome.rainbow.bold);
function getCurrentDayFile(){
// console.log(logRootPath+"access_"+(new Date()).Format("yyyy-MM-dd")+".log");
return logRootPath+"access_"+(new Date()).Format("yyyy-MM-dd")+".log";
}
//
// Basic Http Proxy Server
//
var proxy = httpProxy.createProxyServer({});
var server = http.createServer(function (req, res) {
appendLog(req)
var postData = "";
req.addListener('end', function(){
//數據接收完畢
console.log(postData);
if(!isValid(postData)){//post請求非法參數
invalidHandler(res)
}
});
req.addListener('data', function(postDataStream){
postData += postDataStream
});
var result = isValid(req.url)
//驗證http頭部是否非法
for(key in req.headers){
result = result&& isValid(req.headers[key])
}
if (result) {
var patternUrl = urlHandler(req.url);
console.log("patternUrl:" + patternUrl);
if (patternUrl) {
proxy.web(req, res, {target: patternUrl});
} else {
noPattern(res);
}
} else {
invalidHandler(res)
}
});
proxy.on('error', function (err, req, res) {
res.writeHead(500, {
'Content-Type': 'text/plain'
});
res.end('Something went wrong.');
});
/**
* 驗證非法參數
* @param value
* @returns {boolean} 非法返回False
*/
function isValid(value) {
return re.test(value) ? false : true;
}
/**
* 請求轉發
* @param url
* @returns {*}
*/
function urlHandler(url) {
var tempUrl = url.substring(url.lastIndexOf("/"));
return proxyPassConfig[tempUrl];
}
function invalidHandler(res) {
res.writeHead(400, {'Content-Type': 'text/plain'});
res.write('Bad Request ');
res.end();
}
function noPattern(res) {
res.writeHead(404, {'Content-Type': 'text/plain'});
res.write('not found');
res.end();
}
function getClientIp(req){
return req.headers['x-forwarded-for'] ||
req.connection.remoteAddress ||
req.socket.remoteAddress ||
req.connection.socket.remoteAddress;
}
function appendLog(req) {
console.log("request url:" + req.url);
var logData = (new Date()).Format("yyyy-MM-dd hh:mm:ss")+" "+getClientIp(req)+" "+req.method+ " "+req.url+"\n";
fs.exists(logRootPath,function(exists){
if(!exists){
fs.mkdirSync(logRootPath)
}
fs.appendFile(getCurrentDayFile(),logData,'utf8',function(err){
if(err)
{
console.log(err);
}
});
})
}
console.log("listening on port 80".green.bold)
server.listen(80);
思路擴展
- 攔截非法字符後可以發郵件通知管理員
- 可以把日誌發送到日誌系統,進行大數據分析
- 增加頻繁訪問,拒絕Ip功能。 可以利用redis 過期緩存實現。