Skip to content

Instantly share code, notes, and snippets.

@chtzvt
Created April 21, 2015 22:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chtzvt/0b1d6f008d08401b4f66 to your computer and use it in GitHub Desktop.
Save chtzvt/0b1d6f008d08401b4f66 to your computer and use it in GitHub Desktop.
//Nasty bot catcher adapted from https://github.com/ctrezevant/node-form-inspector
//Loads our dependencies.
var http = require('http');
var url = require('url') ;
var qs = require('querystring');
var fs = require('fs');
//On what port should the server run?
var PORT = 8082;
//Date & time function so that responses are timestamped.
function getDateTime() {
var date = new Date();
var hour = date.getHours();
hour = (hour < 10 ? "0" : "") + hour;
var min = date.getMinutes();
min = (min < 10 ? "0" : "") + min;
var sec = date.getSeconds();
sec = (sec < 10 ? "0" : "") + sec;
var year = date.getFullYear();
var month = date.getMonth() + 1;
month = (month < 10 ? "0" : "") + month;
var day = date.getDate();
day = (day < 10 ? "0" : "") + day;
return year + ":" + month + ":" + day + ":" + hour + ":" + min + ":" + sec;
}
//This function is used to determine whether the request object is empty.
function isEmptyRequest(query) {
for(var prop in query) {
if(query.hasOwnProperty(prop))
return false;
}
return true;
}
http.createServer(function (req, res) {
//This if/else block deals with the different methods necessary to deal with different kinds of requests.
if(req.method === "GET") {
var params = url.parse(req.url,true).query;
params.fi_timeStamp = getDateTime();
params.fi_requestIP = req.connection.remoteAddress;
params.fi_method = "GET";
params.req_headers = JSON.parse(JSON.stringify(req.headers, null, 4));
res.writeHead(200);
fs.appendFile("/var/www/etc/log/honeypot.txt", JSON.stringify(params, null, 4), function(err) { console.log(err); });
res.end();
} else if (req.method === "POST") {
var body = '';
req.on('data', function (data) {
body += data;
//Stop requests that are too large.
//This kills the skiddie :)
if (body.length > 1e6) {
req.connection.destroy();
}
});
req.on('end', function () {
var params = qs.parse(body);
params.fi_timeStamp = getDateTime();
params.fi_requestIP = req.connection.remoteAddress;
params.fi_method = "POST";
params.req_headers = JSON.parse(JSON.stringify(req.headers, null, 4));
console.log(params.fi_timeStamp + " Got these parameters: " + JSON.stringify(params, null, 4));
res.writeHead(200);
fs.appendFile("/var/www/etc/log/honeypot.txt", JSON.stringify(params, null, 4), function(err) { console.log(err); });
res.end();
});
}}).listen(PORT);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment