From 6305442f2646226d0ba9a9e29e6a95ec8fd37cbb Mon Sep 17 00:00:00 2001 From: sysadmin Date: Sat, 29 Jul 2023 23:58:48 +0200 Subject: [PATCH] Initial commit --- .gitignore | 1 + index.js | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ mod.info | 4 ++++ 3 files changed, 63 insertions(+) create mode 100644 .gitignore create mode 100755 index.js create mode 100755 mod.info diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..47d6e3b --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +commit.sh diff --git a/index.js b/index.js new file mode 100755 index 0000000..9c52a23 --- /dev/null +++ b/index.js @@ -0,0 +1,58 @@ +var EasyWaf = require('easy-waf'); +if(EasyWaf.default) { + EasyWaf = EasyWaf.default; +} +var fs = require("fs"); +var os = require("os"); +var url = require("url"); +var easywafconfig = {}; +var logm = {}; +if (fs.existsSync("easywaf-config.json")) easywafconfig = JSON.parse(fs.readFileSync("easywaf-config.json").toString()) +easywafconfig.preBlockHook = function(req, moduleInfo, ip) { + try { + logm[ip]("Request blocked by EasyWAF. Module: " + moduleInfo.name); + } catch (ex) { + + } + return true; +} +easywafconfig.disableLogging = true; +const easyWaf = EasyWaf(easywafconfig); + +function Mod() {} +Mod.prototype.callback = function callback(req, res, serverconsole, responseEnd, href, ext, uobject, search, defaultpage, users, page404, head, foot, fd, elseCallback, configJSON, callServerError) { + return function() { + logm[req.socket.remoteAddress] = serverconsole.errmessage; + //REQ.BODY + function readableHandler() { + try { + req.body = req._readableState.buffer._getString(); + if (req.headers["content-type"] == "application/x-www-form-urlencoded") req.body = url.parse("?" + req.body.strip(), true).query; + if (req.headers["content-type"] == "application/json") req.body = JSON.parse(req.body.strip()); + } catch (ex) { + } + + //EASYWAF + easyWaf(req, res, function() { + if (href == "/easywaf-config.json" || (os.platform() == "win32" && href.toLowerCase() == "/easywaf-config.json")) { + if (callServerError) { + callServerError(403, "easy-waf-integration/1.0.0"); + } else { + res.writeHead(403, "Forbidden", { + "Server": "SVR.JS" + }); + res.end("403 Forbidden!"); + } + } else { + elseCallback(); + } + }); + } + if(req._readableState.length > 0 || req._readableState.ended) { + readableHandler(); + } else { + req.once("readable", readableHandler); + } + } +} +module.exports = Mod; diff --git a/mod.info b/mod.info new file mode 100755 index 0000000..4d4c017 --- /dev/null +++ b/mod.info @@ -0,0 +1,4 @@ +{ + "name": "Integration with EasyWAF", + "version": "1.1.0" +}