-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.js
More file actions
236 lines (164 loc) · 4.78 KB
/
index.js
File metadata and controls
236 lines (164 loc) · 4.78 KB
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/**
* Required modules
**/
const fs = require('fs');
const _ = require('underscore');
const async = require('async');
const redis = require('redis');
const crypto = require('crypto');
const S = require('string');
const url = require('url');
/**
* Object to expose
**/
var Phishtank = {};
/**
* Load in the nice and big blacklist,
* with a timeout for safety
**/
Phishtank.loadBlacklist = function(options, fn) {
// make sure to be only called once
var callback = _.once(fn);
var timer = null;
// read in the file buffer
fs.readFile(options.file || './blacklist.json', function(err, data){
// check for a error and return
if(err) return callback(err);
// right so parse the response
var body = JSON.parse(data.toString());
// stop the timer
if(timer) clearTimeout(timer);
// return the body
callback(null, body);
});
// after 5 minutes, just timeout
timer = setTimeout(function() {
// done with error
callback(new Error('Gave up after 5 minutes ...'));
}, 1000 * 60 * 5);
};
/**
* Starts the download process
**/
Phishtank.boot = function(options, fn) {
// clean callback
var callback = _.once(fn);
// debugging
if(options.debug === true)
console.log('loading blacklist...');
// first load the blacklist
Phishtank.loadBlacklist(options, function(err, list) {
// check for a error
if(err) {
// output the error
if(options.debug === true) {
// output a friendly message
console.error('Problem loading the blacklist');
// output the stacktrace
console.dir(err);
}
// exit
return callback(err);
}
// keep track how many we actually saved
var count = 0;
// debug
if(options.debug === true)
console.log('Found ' + list.length + ' records in blacklist');
// debug
if(options.debug === true)
console.log('connecting to redis server: ' + options.host);
/**
* Creating the actual client for redis
**/
var client = redis.createClient(options.port || 6379, options.host || '127.0.0.1');
// exit if we can't connect
var exitTimer = setTimeout(function() {
// debugging
if(options.debug === true)
console.log('process took longer than 20 minutes, stopping now ...')
// stop
callback(new Error('process took longer than 20 minutes, stopping now ...'))
}, 1000 * 60 * 20);
/**
* Handle any error from connection
**/
client.on('error', function(err) {
// check for debugging
if(options.debug === true) {
// output the error
console.error('Problem connecting to redis');
// otutput stack
console.dir(err);
}
// done
fn(err);
});
// output the error
if(options.debug === true)
console.log('connection to ' + options.host + ' success !');
// list of chunks
var chunks = [];
var len = 1000;
// devide the list up in chunks of len
for(var i = 0; i < list.length; i++) {
// if mod of len
if(i % len == 0) chunks.push(i);
}
// run each chunk
async.eachLimit(chunks, 1, function(chunk, cbb) {
// debug
if(options.debug === true)
console.log('processing records from index ' + chunk + ' till ' + (chunk + len));
// add each of these results to the cache
async.eachLimit(list.slice(chunk, chunk + len), 1, function(entry, cb) {
// check if verified
if(entry.verified != 'yes')
return cb(null);
// must have a url
if(!entry.url)
return cb(null);
// increment
count++;
// remove the query and hash params
var uri = url.parse( entry.url.toLowerCase() );
// remove the hash
uri.hash = '';
uri.search = '';
// create the sha1 hash
var shasum = crypto.createHash('sha1');
shasum.update(url.format(uri));
var hash = shasum.digest('hex');
// create the key
var key = [
'passmarked',
'phishtank',
hash
].join(':');
// save in redis
client.set(key, JSON.stringify([ entry ]), function (err) {
// set to expire in 2 days if not updated before then
client.expire(key, 60 * 24 * 2, function(err) {
// done
cb(null);
});
});
}, function() {
// done
cbb(null);
});
}, function() {
// debug
if(options.debug === true)
console.log('Cached ' + count + ' out of the ' + list.length + ' known phishing attacks')
// close redis connection
client.quit();
// done
callback(null);
});
});
};
/**
* Expose as the module
**/
module.exports = exports = Phishtank;