This repository has been archived by the owner on Jun 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 89
/
index.js
63 lines (55 loc) · 1.66 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
'use strict';
const async = require('async');
const util = require('../../util');
const Shannon = require('./shannon');
const shannon = new Shannon();
module.exports = function Entropy(options) {
options = util.defaultValue(options, {});
options = util.permittedArgs(options, []);
const self = {};
self.key = 'entropy';
self.name = 'Entropy';
self.description = 'Scans files for strings with high entropy';
self.enabled = false;
let fileManager;
self.handles = function(manager) {
util.enforceType(manager, Object);
fileManager = manager;
return true;
};
self.run = function(results, done) {
const checkEntropy = (file, contents, nextFile) => {
/* jshint maxcomplexity: 5 */
const re = /\w{10,}/g;
let m;
do {
m = re.exec(contents);
if (m) {
const word = m[0];
const entropy = shannon.entropy(word);
if(entropy > 4.5) {
const line = contents.split(m[0])[0].split('\n').length;
const item = {
code: '1',
offender: file,
description: 'High entropy string detected in file',
mitigation: 'Check line number: ' + line,
data: {}
};
results.low(item);
}
}
} while (m);
async.setImmediate(nextFile);
};
(function executeChecksAgainstFiles() {
async.eachSeries(fileManager.languageFiles, (file, nextFile) => {
fileManager.readFile(file, (err, contents) => {
if(err) { return nextFile(); }
checkEntropy(file, contents, nextFile);
});
}, done);
})();
};
return Object.freeze(self);
};