-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathsimulator.js
175 lines (157 loc) · 5.25 KB
/
simulator.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
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
const { config } = require('dotenv');
const fs = require('fs');
const path = require('path');
const log = require('./log');
const randomizer = require('./randomizer');
const honeytraps = {
robots: require('./honeytraps/robots-txt'),
exposedFiles: require('./honeytraps/exposed-files'),
cookies: require('./honeytraps/cookie'),
};
class Simulator {
constructor(app, http, templates, dynamicTemplates) {
this.app = app;
this.http = http;
this.templates = templates;
this.dynamicTemplates = dynamicTemplates;
}
apply() {
//built-in modules
this.builtinModules(this.app.config);
for (let index = 0; index < this.dynamicTemplates.length; index++) {
this.mockDynamicRequest(this.dynamicTemplates[index]);
}
for (let index = 0; index < this.templates.length; index++) {
//check if its dynamic js file
this.mockTemplate(this.templates[index]);
}
}
builtinModules(config) {
if (config.disableBuiltIn && config.disableBuiltIn.includes('traps')) {
return;
}
//load internal honeytraps
honeytraps.robots(this.http);
honeytraps.exposedFiles(this.http);
honeytraps.cookies(this.http);
}
mockDynamicRequest(template) {
this.app.logger.info(
'Simulator -> Mocking Dynamic request ' + template
);
require(template)(this.http);
}
mockTemplate(template) {
for (const request of template.requests) {
this.mockRequest(request, template);
}
}
mockRequest(request, template) {
let expect = request.expect;
this.app.logger.info('Simulator -> Mocking request ' + template.id);
//parse reply
if (request.reply.body.view) {
let viewContents = fs.readFileSync(
this.app.resourcesDir + '/' + request.reply.body.view,
{ encoding: 'utf8' }
);
request.reply.body.contents = viewContents;
}
switch (expect.method) {
case 'ALL':
this.http.all(
expect.path,
this.handler(request, template, expect)
);
break;
case 'GET':
this.http.get(
expect.path,
this.handler(request, template, expect)
);
break;
case 'HEAD':
this.http.head(
expect.path,
this.handler(request, template, expect)
);
break;
case 'POST':
this.http.post(
expect.path,
this.handler(request, template, expect)
);
break;
case 'PUT':
this.http.put(
expect.path,
this.handler(request, template, expect)
);
break;
case 'DELETE':
this.http.delete(
expect.path,
this.handler(request, template, expect)
);
break;
case 'patch':
this.http.delete(
expect.path,
this.handler(request, template, expect)
);
break;
}
}
handler(request, template) {
let reply = request.reply;
return (req, res) => {
//if this a trap mark the full session as malicious
if (request.isTrap) {
//TODO: mention trap name
this.app.logger.info(
'Simulator -> Trap hit, marking the session as malicious '
);
req.session.isMalicious = true;
}
//report the malicious activities
if (req.session.isMalicious) {
this.http.logger(
template.id,
template.info.title,
template.info
);
}
res.set(reply.headers);
if (reply.body.static) {
this.app.logger.info(
'Simulator -> Loading static file FROM ' +
this.app.resourcesDir +
'/' +
request.reply.body.static
);
let staticC = fs.readFileSync(
this.app.resourcesDir + '/' + request.reply.body.static
);
res.status(reply.status).send(staticC);
return;
}
let templateVars = Object.assign(
{
params: req.params,
body: req.body,
query: req.query,
},
reply.body.vars || {}
);
let enableCache = reply.body.cache === false ? false : true; //default is true
let renderedContents = randomizer.render(
req,
reply.body.contents,
templateVars,
enableCache
);
res.status(parseInt(reply.status)).send(renderedContents);
};
}
}
module.exports = Simulator;