当前位置: 首页 > 后端技术 > Node.js

nodejshttp-proxy开发反向代理服务器,防火墙,过滤常见web渗透

时间:2023-04-03 23:36:37 Node.js

事件的发生是因为最近web系统受到黑客攻击,经常被扫描尝试各种漏洞。分析攻击日志,常见的攻击方式有几种:上传webshel??l远程命令执行漏洞sql注入xxs攻击测试各种开源框架分析漏洞特征分析攻击信息尽量在httpheader、body植入非法命令等。常见的非法字符包括:exe、cmd、powershell、download、select、union、delete等。解决思路能否开发一个代理服务器来分析http请求的header和body中的信息,以及如果有非法字符,将被截断并拒绝服务。配置允许请求的白名单,拒绝非法URL。网络拓扑http代理拦截非法请求并拒绝服务。技术选型常用的代理服务器有nginx和apache。不知道这两个代理服务器能不能灵活配置,过滤,转发。我没有深入的了解。所以选择nodejshttp-proxy。nodejs优点轻量级,快速部署,灵活开发,高吞吐,异步io编码实现逻辑图,绝对干货,代码分享代码依赖http-proxy1.17.0https://github.com/nodejitsu/...代码地址“颜色”:“?0.6.2”,varutil=require('util'),colors=require('colors'),http=require('http'),httpProxy=require('./node_modules/http-代理人');fs=require("fs");varwelcome=['###################################','################','########################','#######################','##############','################'].join('\n');Date.prototype.Format=function(fmt){//作者:meizzvaro={"M+":this.getMonth()+1,//月"d+":this.getDate(),//日"h+":this.getHours(),//小时"m+":this.getMinutes(),//分钟"s+":this.getSeconds(),//秒"S":this.getMilliseconds()//每秒};如果(/(y+)/.test(fmt))fmt=fmt.replace(RegExp.$1,(this.getFullYear()+"").substr(4-RegExp.$1.length));for(varkino)if(newRegExp("("+k+")").test(fmt))fmt=fmt.replace(RegExp.$1,(RegExp.$1.length==1)?(o[k]):(("00"+o[k]).substr((""+o[k]).length)));returnfmt;}//非法字符varre=/php|exe|cmd|shell|select|union|delete|update|insert/;/**这里配置转发*/varproxyPassConfig={"/hello":"http://www.qingmiaokeji.cn","/":"http://127.0.0.1/"}varlogRootPath="g:/httpproxy/";console.log(welcome.rainbow.bold);functiongetCurrentDayFile(){//console.log(logRootPath+"access_"+(newDate()).Format("yyyy-MM-dd")+".log");returnlogRootPath+"access_"+(newDate()).Format("yyyy-MM-dd")+".log";}////BasicHttpProxyServer//varproxy=httpProxy.createProxyServer({});varserver=http.createServer(function(req,res){appendLog(req)varpostData="";req.addListener('end',function(){//接收数据console.log(postData);if(!isValid(postData)){//post请求无效参数invalidHandler(res)}});req.addListener('data',function(postDataStream){postData+=postDataStream});varresult=isValid(req.url)//验证httpheader是否非法for(keyinreq.headers){result=result&&isValid(req.headers[key])}if(result){varpatternUrl=urlHandler(请求.url);console.log("patternUrl:"+patternUrl);如果(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('出了点问题。');});/***验证无效参数*@paramvalue*@returns{boolean}非法返回False*/functionisValid(value){re转动重新测试(值)?false:true;}/***请请求转发*@paramurl*@returns{*}*/functionurlHandler(url){vartempUrl=url.substring(url.lastIndexOf("/"));returnproxyPassConfig[tempUrl];}functioninvalidHandler(res){res.writeHead(400,{'Content-Type':'text/plain'});res.write('错误请求');res.end();}functionnoPattern(res){res.writeHead(404,{'Content-Type':'text/plain'});res.write('未找到');res.end();}functiongetClientIp(req){返回req.headers['x-forwarded-for']||req.connection.remoteAddress||req.socket.remoteAddress||req.connection.socket.remoteAddress;}functionappendLog(req){console.log("requesturl:"+req.url);varlogData=(newDate()).Format("yyyy-MM-ddhh: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("监听80端口".green.bold)server.listen(80);思路拓展拦截非法字符后,可以发邮件通知管理员,可以将日志发送到日志系统进行大数据分析,增加访问频率。拒绝IP的功能可以通过redis过期缓存来实现。