-
Notifications
You must be signed in to change notification settings - Fork 113
/
Copy pathapi.js
760 lines (693 loc) · 24.2 KB
/
api.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
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
const bcrypt = require('bcrypt');
const openpgp = require('openpgp');
const { Storage } = require('@google-cloud/storage');
const express = require('express');
const cors = require('cors');
const path = require('path');
const uuid = require('uuid');
const asyncfs = require('fs').promises;
const fs = require('fs');
const sessions = require('@truffledustin/node-client-sessions');
const favicon = require('serve-favicon');
const database = require('./database.js');
const Users = database.Users;
const Secrets = database.Secrets;
const safeCompare = require('safe-compare');
const { Op } = require("sequelize");
const PayloadFireResults = database.PayloadFireResults;
const CollectedPages = database.CollectedPages;
const InjectionRequests = database.InjectionRequests;
const constants = require('./constants.js');
const validate = require('express-jsonschema').validate;
const get_hashed_password = require('./utils.js').get_hashed_password;
const get_secure_random_string = require('./utils.js').get_secure_random_string;
const {google} = require('googleapis');
const {OAuth2Client} = require('google-auth-library');
const Sentry = require('@sentry/node');
const SCREENSHOTS_DIR = path.resolve(process.env.SCREENSHOTS_DIR);
const SCREENSHOT_FILENAME_REGEX = new RegExp(/^[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}\.png$/i);
const SCREENSHOT_FILENAME_REGEX_ENC = new RegExp(/^[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}\.b64png.enc$/i);
var sessions_middleware = false;
var sessions_settings_object = {
cookieName: 'session',
duration: 7 * 24 * 60 * 60 * 1000, // Default session time is a week
activeDuration: 1000 * 60 * 5, // Extend for five minutes if actively used
cookie: {
httpOnly: true,
secureProxy: process.env.NODE_ENV == 'production'
}
}
function makeRandomPath(length) {
var result = '';
var characters = 'abcdefghijklmnopqrstuvwxyz0123456789';
var charactersLength = characters.length;
for ( var i = 0; i < length; i++ ) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
}
return result;
}
function session_wrapper_function(req, res, next) {
return sessions_middleware(req, res, next);
}
async function check_file_exists(file_path) {
return asyncfs.access(file_path, fs.constants.F_OK).then(() => {
return true;
}).catch(() => {
return false;
});
}
async function set_up_api_server(app) {
// Check for existing session secret value
const session_secret_setting = process.env.SESSION_SECRET_KEY;
if (!session_secret_setting) {
console.error(`No session secret is set, can't start API server (this really shouldn't happen...)!`);
throw new Error('NO_SESSION_SECRET_SET');
return
}
const updated_session_settings = {
...sessions_settings_object,
...{
secret: session_secret_setting
}
};
sessions_middleware = sessions(updated_session_settings);
// Session management
app.use(session_wrapper_function);
/* lol make this be a thing later TODO
// Limit how big uploads are
app.use(fileUpload({
limits: {
fileSize: 2000000 //2mb
},
abortOnLimit: true
}));
*/
// If that's not present, the request should be rejected.
app.use(async function(req, res, next) {
// Must be an API route else CSRF protection doesn't matter
if(!req.path.startsWith(constants.API_BASE_PATH)) {
next();
return
}
// Check to see if the required CSRF header is set
// If it's not set, reject the request.
const csrf_header_value = req.header(constants.csrf_header_name);
if(!csrf_header_value) {
res.status(401).json({
"success": false,
"error": "No CSRF header specified, request rejected.",
"code": "CSRF_VIOLATION"
}).end();
return
}
// Otherwise we're fine to continue
next();
});
// Restrict all API routes unless the user is authenticated.
app.use(async function(req, res, next) {
const AUTHENTICATION_REQUIRED_ROUTES = [
constants.API_BASE_PATH + 'payloadfires',
constants.API_BASE_PATH + 'collected_pages',
constants.API_BASE_PATH + 'settings',
constants.API_BASE_PATH + 'xss-uri',
constants.API_BASE_PATH + 'user-path',
];
// Check if the path being accessed required authentication
var requires_authentication = false;
AUTHENTICATION_REQUIRED_ROUTES.map(authenticated_route => {
if(req.path.toLowerCase().startsWith(authenticated_route)) {
requires_authentication = true;
}
});
// If the route is not one of the authentication required routes
// then we can allow it through.
if(!requires_authentication) {
next();
return;
}
// If the user is authenticated, let them pass
if(req.session.authenticated === true) {
// const user = await Users.findOne({ where: { 'id': req.session.user_id } });
// if (user == null) {
// req.session.destroy();
// res.redirect(302, '/').json({
// "success": false,
// "error": "You must be authenticated to use this endpoint.",
// "code": "NOT_AUTHENTICATED"
// }).end();
// return
// }
next();
return;
}
// Otherwise, fall to blocking them by default.
res.status(401).json({
"success": false,
"error": "You must be authenticated to use this endpoint.",
"code": "NOT_AUTHENTICATED"
}).end();
return
});
app.get('/login', (req, res) => {
const client = new OAuth2Client(process.env.CLIENT_ID, process.env.CLIENT_SECRET, process.env.NODE_ENV == 'production' ? `https://${process.env.HOSTNAME}/oauth-login` : `http://${process.env.HOSTNAME}/oauth-login`);
const authUrl = client.generateAuthUrl({
redirect_uri: process.env.NODE_ENV == 'production' ? `https://${process.env.HOSTNAME}/oauth-login` : `http://${process.env.HOSTNAME}/oauth-login`,
access_type: 'offline',
scope: ['email', 'profile'],
prompt: 'select_account'
});
res.redirect(authUrl);
});
app.get('/oauth-login', async (req, res) => {
const client = new OAuth2Client(process.env.CLIENT_ID, process.env.CLIENT_SECRET, process.env.NODE_ENV == 'production' ? `https://${process.env.HOSTNAME}/oauth-login` : `http://${process.env.HOSTNAME}/oauth-login`);
try{
const code = req.query.code;
const {tokens} = await client.getToken(code);
client.setCredentials(tokens);
const oauth2 = google.oauth2({version: 'v2', auth: client});
const googleUserProfile = await oauth2.userinfo.v2.me.get();
const email = googleUserProfile.data.email
const [user, created] = await Users.findOrCreate({ where: { 'email': email } });
if(created){
user.path = makeRandomPath(10);
user.injectionCorrelationAPIKey = makeRandomPath(20);
user.save();
console.log(`Created new user ID: ${user.id}`)
}
req.session.email = user.email;
req.session.user_id = user.id;
req.session.authenticated = true;
res.redirect("/app/");
} catch (error) {
console.error(`Error Occured: ${error}`);
Sentry.captureException(error);
res.status(500).send("Error Occured. We're seeing a lot of traffic now. Please try again soon.");
}
});
app.get('/screenshots/:screenshotFilename', async (req, res) => {
const screenshot_filename = req.params.screenshotFilename;
// Come correct or don't come at all.
if(!SCREENSHOT_FILENAME_REGEX.test(screenshot_filename) && !SCREENSHOT_FILENAME_REGEX_ENC.test(screenshot_filename)) {
return res.sendStatus(404);
}
const gz_image_path = `${screenshot_filename}.gz`;
if (process.env.USE_CLOUD_STORAGE == "true"){
const storage = new Storage();
const bucket = storage.bucket(process.env.BUCKET_NAME);
const file = bucket.file(gz_image_path);
try {
// Download the gzipped image
const [image] = await file.download();
// Send the gzipped image in the response
res.set('Content-Encoding', 'gzip');
if(gz_image_path.endsWith(".b64png.enc.gz")){
res.set('Content-Type', 'text/plain');
res.setHeader('Content-disposition', 'attachment; filename=screenshot.b64png.enc');
res.send(image);
}else{
res.set('Content-Type', 'image/png');
res.send(image);
}
} catch (error) {
console.error(error.stack);
console.log("An error occurred looking up object")
Sentry.captureException(error);
res.status(404).send(`Error retrieving image from GCS`);
}
}else{
const image_exists = await check_file_exists(gz_image_path);
if(!image_exists) {
return res.sendStatus(404);
}
// Return the gzipped image file with the appropriate
// Content-Encoding header, should be widely supported.
res.sendFile(gz_image_path, {
// Why leak anything you don't have to?
lastModified: false,
acceptRanges: false,
cacheControl: true,
headers: {
"Content-Type": "image/png",
"Content-Encoding": "gzip"
}
})
}
});
// Serve the front-end
app.use('/app/', express.static(
'./front-end/dist/',
{
setHeaders: function (res, path, stat) {
res.set("Content-Security-Policy", "default-src 'none'; script-src 'self'; style-src 'self' 'unsafe-inline'; img-src 'self'; font-src 'self'; connect-src 'self'; prefetch-src 'self'; manifest-src 'self'");
},
},
));
app.use(favicon('./front-end/dist/favicon.ico'));
/*
Endpoint which returns if the user is logged in or not.
*/
app.get(constants.API_BASE_PATH + 'auth-check', async (req, res) => {
res.status(200).json({
"success": true,
"result": {
"is_authenticated": (req.session.authenticated == true)
}
}).end();
});
/*
Just returns the user's XSS URI if logged in.
*/
app.get(constants.API_BASE_PATH + 'xss-uri', async (req, res) => {
const user = await Users.findOne({ where: { 'id': req.session.user_id } });
const uri = process.env.XSS_HOSTNAME + "/" + user.path;
res.status(200).json({
"success": true,
"result": {
"uri": uri
}
}).end();
});
/*
Get the user's path.
*/
app.get(constants.API_BASE_PATH + 'user-path', async (req, res) => {
const user = await Users.findOne({ where: { 'id': req.session.user_id } });
res.status(200).json({
"success": true,
"result": {
"path": user.path
}
}).end();
});
/*
Update the user's path.
*/
app.put(constants.API_BASE_PATH + 'user-path', async (req, res) => {
let collisionUser;
let desiredPath;
if(typeof req.body.user_path == 'string'){
desiredPath = req.body.user_path;
collisionUser = await Users.findOne({ where: { 'path': desiredPath } });
}else{
return res.status(200).json({
"success": false,
"error": "invalid path"
}).end();
}
if( collisionUser ){
return res.status(200).json({
"success": false,
"error": "Path taken by another user"
}).end();
}
const user = await Users.findOne({ where: { 'id': req.session.user_id } });
user.path = desiredPath;
user.save();
res.status(200).json({
"success": true,
"result": {
"path": user.path
}
}).end();
});
/*
Attempt to log into the administrator account
*/
const LoginSchema = {
type: 'object',
properties: {
password: {
type: 'string',
minLength: 1,
maxLength: 72,
required: true,
},
}
}
/*
Deletes a given XSS payload(s)
*/
const DeletePayloadFiresSchema = {
type: 'object',
properties: {
ids: {
type: 'array',
required: true,
items: {
type: 'string'
}
}
}
}
app.delete(constants.API_BASE_PATH + 'payloadfires', validate({ body: DeletePayloadFiresSchema }), async (req, res) => {
console.debug("Deleting payload fires: " + req.body.ids)
const ids_to_delete = req.body.ids;
// Pull the corresponding screenshot_ids from the DB so
// we can delete all the payload fire images as well as
// the payload records themselves.
const screenshot_id_records = await PayloadFireResults.findAll({
where: {
id: {
[Op.in]: ids_to_delete
},
user_id: req.session.user_id
},
attributes: ['id', 'screenshot_id', 'encrypted']
});
if ( process.env.USE_CLOUD_STORAGE == "true"){
const storage = new Storage();
await Promise.all(screenshot_id_records.map(payload => {
let filename = ""
if(payload.encrypted){
filename = `${payload.screenshot_id}.b64png.enc.gz`
}else{
filename = `${payload.screenshot_id}.png.gz`;
}
return storage.bucket(process.env.BUCKET_NAME).file(filename).delete();
}));
}else{
await Promise.all(screenshot_id_records.map(payload => {
let filename = "${SCREENSHOTS_DIR}/"
if(payload.encrypted){
filename = `${payload.screenshot_id}.b64png.enc.gz`
}else{
fileName = `${payload.screenshot_id}.png.gz`;
}
return asyncfs.unlink(filename);
}));
}
const payload_fires = await PayloadFireResults.destroy({
where: {
id: {
[Op.in]: ids_to_delete
}
}
});
res.status(200).json({
'success': true,
'result': {}
}).end();
});
/*
Returns the list of XSS payload fire results.
*/
const ListPayloadFiresSchema = {
type: 'object',
properties: {
page: {
type: "Integer",
required: false,
minimum: 1,
default: 1,
},
limit: {
type: "Integer",
required: false,
minimum: 1,
default: 10,
},
}
}
app.get(constants.API_BASE_PATH + 'payloadfires', validate({ query: ListPayloadFiresSchema }), async (req, res) => {
const page = (parseInt(req.query.page) - 1);
const limit = parseInt(req.query.limit);
const offset = (page * limit);
const payload_fires = await PayloadFireResults.findAndCountAll({
where: {
user_id: req.session.user_id
},
limit: limit,
offset: (page * limit),
order: [['createdAt', 'DESC']],
});
let return_payloads = [];
for(let payload of payload_fires.rows){
let secrets = await Secrets.findAndCountAll({
where: {
payload_id: payload.id
}
});
let payload_secrets = [];
for(let secret of secrets.rows){
payload_secrets.push(secret);
}
const new_payload = {
"url": payload.url,
"ip_address": payload.ip_address,
"referer": payload.referer,
"user_agent": payload.user_agent,
"cookies": payload.cookies,
"title": payload.title,
"origin": payload.origin,
"screenshot_id": payload.screenshot_id,
"was_iframe": payload.was_iframe,
"browser_timestamp": payload.browser_timestamp,
"CORS": payload.CORS,
"gitExposed": payload.gitExposed,
"createdAt": payload.createdAt,
"id": payload.id,
"encrypted": payload.encrypted,
"encrypted_data": payload.encrypted_data,
"public_key": payload.public_key,
"updatedAt": payload.updatedAt,
"secrets": payload_secrets
}
return_payloads.push(new_payload);
}
res.status(200).json({
'success': true,
'result': {
'payload_fires': return_payloads,
'total': payload_fires.count
}
}).end();
});
/*
Returns the list of collected pages
*/
const ListCollectedPagesSchema = {
type: 'object',
properties: {
page: {
type: 'string',
required: false,
default: '0',
pattern: '[0-9]+',
},
limit: {
type: 'string',
required: false,
default: '10',
pattern: '[0-9]+',
},
}
}
app.get(constants.API_BASE_PATH + 'collected_pages', validate({ query: ListCollectedPagesSchema }), async (req, res) => {
const page = (parseInt(req.query.page) - 1);
const limit = parseInt(req.query.limit);
const offset = (page * limit);
const collected_pages = await CollectedPages.findAndCountAll({
where: {
user_id: req.session.user_id
},
limit: limit,
offset: (page * limit),
order: [['createdAt', 'DESC']],
});
res.status(200).json({
'success': true,
'result': {
'collected_pages': collected_pages.rows,
'total': collected_pages.count
}
}).end();
});
/*
Deletes a given collected page(s)
*/
const DeleteCollectedPagesSchema = {
type: 'object',
properties: {
ids: {
type: 'array',
required: true,
items: {
type: 'string'
}
}
}
}
app.delete(constants.API_BASE_PATH + 'collected_pages', validate({ body: DeleteCollectedPagesSchema }), async (req, res) => {
const ids_to_delete = req.body.ids;
const payload_fires = await CollectedPages.destroy({
where: {
id: {
[Op.in]: ids_to_delete
}
}
});
res.status(200).json({
'success': true,
'result': {}
}).end();
});
/*
Correlated injections API endpoint.
Authentication is custom for this endpoint
(Uses the correlation API key)
*/
const RecordCorrelatedRequestSchema = {
type: 'object',
properties: {
request: {
type: 'string',
required: true,
},
owner_correlation_key: {
type: 'string',
required: true,
},
injection_key: {
type: 'string',
required: true,
},
}
}
app.post(constants.API_BASE_PATH + 'record_injection', validate({ body: RecordCorrelatedRequestSchema }), async (req, res) => {
const user = await Users.findOne({
where: {
injectionCorrelationAPIKey: req.body.owner_correlation_key
}
});
if (! user) {
res.status(200).json({
"success": false,
"error": "Invalid authentication provided. Please provide a proper correlation API key.",
"code": "INVALID_CREDENTIALS"
}).end();
return
}
try {
// Create injection correlation record
await InjectionRequests.create({
id: uuid.v4(),
request: req.body.request,
injection_key: req.body.injection_key,
});
} catch (e) {
if(e.name === 'SequelizeUniqueConstraintError') {
res.status(200).json({
"success": false,
"error": "That injection key has already been used previously.",
"code": "EXISTING_INJECTION_KEY"
}).end();
return
}
Sentry.captureException(e);
res.status(200).json({
"success": false,
"error": "An unexpected error occurred.",
"code": e.name.toString(),
}).end();
return
}
res.status(200).json({
"success": true,
"message": "Injection request successfully recorded!"
}).end();
});
/*
Returns current settings values for the UI
*/
app.get(constants.API_BASE_PATH + 'settings', async (req, res) => {
let returnObj = {}
const user = await Users.findOne({ where: { 'id': req.session.user_id } });
if(! user){
return res.send("Invalid");
}
returnObj.correlation_api_key = user.injectionCorrelationAPIKey;
returnObj.chainload_uri = user.additionalJS;
returnObj.pgp_key = user.pgp_key;
returnObj.send_alert_emails = user.sendEmailAlerts;
res.status(200).json({
'success': true,
'result': returnObj
}).end();
});
/*
Updates a specific config for the service
*/
const UpdateConfigSchema = {
type: 'object',
properties: {
password: {
type: 'string',
required: false,
},
correlation_api_key: {
type: 'boolean',
required: false,
},
chainload_uri: {
type: 'string',
required: false,
},
send_alert_emails: {
type: 'boolean',
required: false,
},
revoke_all_sessions: {
type: 'boolean',
required: false,
},
pages_to_collect: {
type: 'array',
required: false,
items: {
type: 'string'
}
}
}
}
app.put(constants.API_BASE_PATH + 'settings', validate({ body: UpdateConfigSchema }), async (req, res) => {
const user = await Users.findOne({ where: { 'id': req.session.user_id } });
if(! user){
return res.send("Invalid");
}
if(req.body.correlation_api_key === true) {
user.injectionCorrelationAPIKey = req.body.correlation_api_key;
}
// Intentionally no URL validation incase people want to do
// data: for inline extra JS.
if(req.body.chainload_uri) {
user.additionalJS = req.body.chainload_uri;
}else if (req.body.chainload_uri === ""){
user.additionalJS = null;
}
if(req.body.pgp_key){
try{
const publicKey = await openpgp.readKey({ armoredKey: req.body.pgp_key });
}catch(e){
console.log(e.stack);
return res.status(400).json({
'status': false,
'error': 'invalid PGP key'
}).end();
}
user.pgp_key = req.body.pgp_key;
}else if(req.body.pgp_key === ""){
user.pgp_key = null;
}
if(req.body.send_alert_emails !== undefined) {
user.sendEmailAlerts = req.body.send_alert_emails;
}
await user.save();
res.status(200).json({
'success': true,
'result': {}
}).end();
});
}
module.exports = {
set_up_api_server
};