This repository was archived by the owner on Jan 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathsearchString.js
More file actions
365 lines (315 loc) · 9.59 KB
/
searchString.js
File metadata and controls
365 lines (315 loc) · 9.59 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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
'use strict';
const d3 = require('d3-queue');
const fuzzy = require('fast-fuzzy');
const entropy = require('./lib/entropy');
const entropyThreshold = require('./lib/entropyThreshold');
///////////////////////////////////////////////////////////////////////////////
// Preprocessor features
function processSingleReplace(string, pattern, replacement, callback) {
try {
string.s = string.s.replace(new RegExp(pattern, 'g'), replacement);
}
catch (err) {
return callback(err);
}
return callback(null);
}
function processSingleExclude(string, rule, callback) {
if (rule.hasOwnProperty('disallowedString') && string.s.indexOf(rule.disallowedString) !== -1) {
return callback(); // don't run exclusion if it contains string
}
if (rule.hasOwnProperty('minLength') && string.s.length < rule.minLength) {
string.s = '';
return callback();
}
if (rule.hasOwnProperty('maxLength') && string.s.length > rule.maxLength) {
string.s = '';
return callback();
}
return callback();
}
function processSingleBulkIgnore(string, rule, callback) {
let exclusions = require(rule.path);
if (exclusions === null || exclusions.length === 0) {
return callback();
}
for (let exclusion of exclusions) {
if (string.s.indexOf(exclusion) !== -1) {
string.s = string.s.replace(new RegExp(('\\b' + exclusion + '\\b'), 'g'), '\n');
}
}
return callback();
}
///////////////////////////////////////////////////////////////////////////////
// Preprocessor logic
function triagePreprocess(string, rule, next) {
if (rule.hasOwnProperty ('disabled') && rule.disabled === true) {
return next();
}
if (rule.type === 'remove') {
processSingleReplace(string, rule.pattern, '\n', function(err) {
if (err) {
return next(err);
}
return next();
});
}
if (rule.type === 'replace') {
processSingleReplace(string, rule.pattern, rule.replace, function(err) {
if (err) {
return next(err);
}
return next();
});
}
if (rule.type === 'exclude') {
processSingleExclude(string, rule, function(err) {
if (err) {
return next(err);
}
return next();
});
}
if (rule.type === 'bulkIgnore') {
processSingleBulkIgnore(string, rule, function(err) {
if (err) {
return next(err);
}
return next();
});
}
}
function preProcess(rules, string, callback) {
let q = d3.queue(1);
if (rules.hasOwnProperty('preprocess')) {
if(Array.isArray(rules.preprocess)) {
rules.preprocess.forEach(function(rule) {
q.defer(triagePreprocess, string, rule);
});
} else {
q.defer(triagePreprocess, string, rules.preprocess);
}
}
q.awaitAll(function(err) {
if (err) {
return callback(err);
}
return callback();
});
}
///////////////////////////////////////////////////////////////////////////////
// Main processor features
function processSingleRegex(rules, string, matchingRules, rule, next) {
let matched = null;
try {
matched = string.match(new RegExp(rules.regex[rule].pattern, 'g'));
}
catch (err) {
return next(err);
}
if (matched !== null && matched.length > 0 && rules.regex[rule].hasOwnProperty('minEntropy')) { // There's an entropy threshold
matched = matched.filter((match) => {
return entropy(match) >= rules.regex[rule].minEntropy;
});
}
if (matched !== null && matched.length > 0) {
matchingRules.push(rule);
}
return next();
}
function processSingleFuzzy(rules, string, matchingRules, rule, next) {
let stringToFuzz = string;
let choices = rules.fuzzy[rule].phrases;
if (!(rules.fuzzy[rule].hasOwnProperty('caseSensitive') && rules.fuzzy[rule].caseSensitive)) { // default to case insensitive
stringToFuzz = stringToFuzz.toLowerCase();
choices = choices.map(s => s.toLowerCase());
}
for (let choice of choices) {
if (fuzzy.fuzzy(choice, stringToFuzz) > rules.fuzzy[rule].threshold) {
matchingRules.push(rule);
return next();
}
}
return next();
}
function processSingleEntropy(rules, string, matchingRules, rule, next) {
let matches = false;
let regex = new RegExp('\\b[0-9a-zA-Z]{' + rules.entropy[rule].minLength + ',' + rules.entropy[rule].maxLength + '}\\b', 'g');
let candidates = string.match(regex);
if (candidates === null) {
return next();
}
for (let candidate of candidates) {
if (!matches) {
let type = '';
if (/^[A-Z]+$/.test(candidate) || /^[a-z]+$/.test(candidate)) {
type = 'alpha';
} else if (/^[a-zA-Z]+$/.test(candidate)) {
type = 'alphaCase';
} else if (/^[0-9A-F]+$/.test(candidate) || /^[0-9a-f]+$/.test(candidate)) {
type = 'hex';
} else if (/^[0-9A-Z]+$/.test(candidate) || /^[0-9a-z]+$/.test(candidate)) {
type = 'alphaNum';
} else {
type = 'alphaNumCase';
}
try {
let threshold = entropyThreshold(type, candidate.length, rules.entropy[rule].percentile);
matches = entropy(candidate) >= threshold;
} catch (err) {
return next(err);
}
if (matches) {
matchingRules.push(rule);
return next();
}
}
}
return next();
}
///////////////////////////////////////////////////////////////////////////////
// Main processor logic
function mainProcess(rules, string, matchingRules, callback) {
let q = d3.queue();
if (rules.hasOwnProperty('regex')) {
for (let rule in rules.regex) {
if (rules.regex[rule].hasOwnProperty ('disabled') && rules.regex[rule].disabled) {
continue;
}
q.defer(processSingleRegex, rules, string, matchingRules, rule);
}
}
if (rules.hasOwnProperty('fuzzy')) {
for (let rule in rules.fuzzy) {
if (rules.fuzzy[rule].hasOwnProperty ('disabled') && rules.fuzzy[rule].disabled) {
continue;
}
q.defer(processSingleFuzzy, rules, string, matchingRules, rule);
}
}
if (rules.hasOwnProperty('entropy')) {
for (let rule in rules.entropy) {
if (rules.entropy[rule].hasOwnProperty ('disabled') && rules.entropy[rule].disabled) {
continue;
}
q.defer(processSingleEntropy, rules, string, matchingRules, rule);
}
}
q.awaitAll(function(err) {
if (err) {
return callback(err);
}
return callback(); // For the moment keep chugging along even if there are errors
});
}
///////////////////////////////////////////////////////////////////////////////
// Postprocessor features
function processSingleIgnoreFinding(matchingRules, rule, callback) {
if (matchingRules.r.indexOf(rule.finding) !== -1) {
matchingRules.r = [];
}
return callback();
}
///////////////////////////////////////////////////////////////////////////////
// Postprocessor logic
function triagePostprocess(string, matchingRules, rule, next) {
if (rule.hasOwnProperty ('disabled') && rule.disabled === true) {
return next();
}
if (rule.type === 'ignoreFinding') {
processSingleIgnoreFinding(matchingRules, rule, function(err) {
if (err) {
return next(err);
}
return next();
});
}
}
function postProcess(rules, string, matchingRules, callback) {
//return callback(null, matchingRules.r);
let q = d3.queue(1);
if (rules.hasOwnProperty('postprocess')) {
if(Array.isArray(rules.postprocess)) {
rules.postprocess.forEach(function(rule) {
q.defer(triagePostprocess, string, matchingRules, rule);
});
} else {
q.defer(triagePostprocess, string, matchingRules, rules.postprocess);
}
}
q.awaitAll(function(err) {
if (err) {
return callback(err);
}
return callback(null, matchingRules.r);
});
}
///////////////////////////////////////////////////////////////////////////////
// Magic happens here
function searchString(inputString, options) {
options = options || {};
return new Promise((resolve, reject) => {
let rules;
if (options.hasOwnProperty('rules')) {
rules = options.rules;
} else {
console.err('Error: calling searchString.js without rules no longer works!');
return reject({
numCode: 251,
code: 'DEPRECATED',
msg: 'Erorr: searchString without rules no longer works!'
});
}
let string = {s: inputString};
let matchingRules = [];
if (inputString === null || typeof inputString === 'undefined') {
return reject({
numCode: 254,
code: 'MISSING_ARGS',
msg: 'Error: searchString requires an argument.'
});
}
preProcess(rules, string, function(err) {
if (err) {
return reject({
numCode: 64,
code: 'PREPROC_FAIL',
msg: 'Error: searchString preprocessing failed',
detailedError: err
});
}
mainProcess(rules, string.s, matchingRules, function(err) {
if (err) {
return reject({
numCode: 65,
code: 'MAINPROC_FAIL',
msg: 'Error: searchString main processing failed',
detailedError: err
});
}
postProcess(rules, string.s, {r: matchingRules}, function(err, postprocessedRules) {
if (err) {
return reject({
numCode: 66,
code: 'POSTPROC_FAIL',
msg: 'Error: searchString postprocessing failed',
detailedError: err
});
}
return resolve(postprocessedRules);
});
});
});
return resolve([]);
});
}
module.exports = searchString;
if (require.main === module) {
searchString(process.argv[2]).then((matchingRules) => {
console.log(matchingRules);
return 0;
}).catch((err) => {
console.log(err);
return 1;
});
}