最近web系統引來了黑客的攻擊,常常被掃描,各類漏洞嘗試。
分析攻擊日誌,有幾種常見的攻擊手段:javascript
說白了就是採用web滲透技術,利用http請求,黑客想盡辦法,在http header ,body,等部分植入非法的命令,非法字符常見的有:exe,cmd,powershell,download,select,union,delete等等。php
網絡拓撲
http proxy 攔截非法請求,拒絕服務。java
常見的代理服務器有nginx,apache,不知道這2個代理服務器能不能靈活的配置,過濾,轉發,沒有深刻了解。
所以選用nodejs http-proxy。node
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);