-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
147 lines (138 loc) · 4.3 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
var aws = require('aws-sdk');
var readline = require('readline');
var https = require('https');
var fs = require('fs');
var path = require('path');
var async = require('async');
var waf = new aws.WAF();
var botsConf = JSON.parse(fs.readFileSync(path.join('.', 'conf', 'bots.json')));
var wafConf = {
"byteSetPrefix": "badbot-data",
"byteSetIncrementBy" : 1,
"filtersLimit": 10
};
var updateParams = function (botname) {
return {
'Action': 'INSERT',
'ByteMatchTuple': {
'FieldToMatch': {
'Type': 'HEADER',
'Data': 'User-Agent'
},
'PositionalConstraint': 'CONTAINS',
'TargetString': botname,
'TextTransformation': 'LOWERCASE'
}
};
};
//get badbots from mod_security
var getBadBotData = function(callback) {
botsConf.url.forEach(function(url, index, array) {
console.log(url);
async.parallel([
function(cback) {
// get bot names
var bots = [];
https.get(url, function (response) {
// create a reader object to read the list one line at a time
var reader = readline.createInterface({ terminal: false, input: response });
reader.on('line', function (line) {
if (line && !line.trim().startsWith('#')) {
var bot = line;
// add bot to array if it not a duplicate
if (bots.indexOf(bot) === -1) {
bots.push(bot);
}
}
});
reader.on('close', function () {
console.log(bots.length + ' bots read from the bad bots data at ' + url);
cback(null, bots);
});
}).on('error', function (err) {
console.error('Error downloading bad bots data at ' + url, err);
cback(err);
});
},
function(cback) {
// get bytematchsets
var params = {Limit: 1};
var bytematchsets = [];
(function createByteMatchSetArray() {
waf.listByteMatchSets(params, function(err, bmsets) {
if (err) {
cback(err, null);
} else {
var nm = bmsets.NextMarker;
bmsets.ByteMatchSets.forEach(function(bmset) {
if(bmset.Name.toLowerCase().startsWith(wafConf.byteSetPrefix)) {
bytematchsets.push(bmset);
}
});
}
if (nm != null) {
params = {Limit: 1, NextMarker: nm};
createByteMatchSetArray();
} else {
// console.log("bytematchsets = " + bytematchsets.length);
cback(null, bytematchsets);
}
});
})();
}
], function(err, result) {
if (err) {
callback(err, null);
} else {
var bots = result[0];
var byteMatchSets = result[1];
var updates = [];
// compile a list ot bytematchsets; compare list of bots against them; add missing bots to bytematchsets
async.map(byteMatchSets, function (bmset, cb) {
waf.getByteMatchSet({ByteMatchSetId: bmset.ByteMatchSetId}, function(e, b) {
if (e) {
cb(e, null);
} else {
cb(null, b.ByteMatchTuples);
}
});
}, function(e, tuples) {
if (e) {
console.log(e, e.stack);
} else {
// check if bots are already exist in bytesets.
// if not, add them to existing bytesets that have room. Else, create new bytematchsets.
for (var i =0; i < bots.length; i++) {
var bot = bots[i];
var found = false;
tuples.forEach(function(tuple) {
if (tuple.TargetString.toLowerCase() == bot.toLowerCase()) {
found = true;
bots.splice(i, 1);
}
});
if (!found) {
var tuple = updateParams(bot);
updates.push(tuple);
}
}
callback(null, updates);
}
});
}
});
});
};
var updateByteMatchSets = function (event, context) {
getBadBotData(function(err, byteMatchSets) {
console.log(byteMatchSets);
});
};
function callback(err, data) {
if (err) {
console.log(err, err.stack);
} else {
console.log(data);
}
}
updateByteMatchSets(null, null);