forked from securitum/book-extra-content
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
628 lines (550 loc) · 17.3 KB
/
main.ts
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
import * as express from 'express';
import * as nunjucks from 'nunjucks';
import * as bodyParser from 'body-parser';
import * as expressSession from 'express-session';
import { exec, ExecOptions } from 'child_process';
import * as expressWs from 'express-ws';
import * as mysql from 'mysql';
import * as libxmljs from 'libxmljs';
import { createHmac } from 'crypto';
interface Example {
title: string;
description: string;
href: string;
};
const examples: Array<Example> = [
{
title: 'Logowanie #1',
description: 'Atak bruteforce na logowanie: ustalenie nazwy użytkownika i hasła',
href: '/1'
},
{
title: 'Logowanie #2',
description: 'Atak bruteforce na logowanie. Czy tutaj da się ustalić poprawną nazwę użytkownika?',
href: '/2'
},
{
title: 'ping',
description: 'Aplikacja udostępnia prostą funkcjonalność: ping. Czy coś może pójść nie tak?',
href: '/ping'
},
{
title: 'Faktury',
description: 'Aplikacja pozwala wyłącznie na dostęp do swojej faktury...',
href: '/invoice?key=901ee71e933b4ae83353586c18a3d623&invoiceId=123'
},
{
title: 'Kontrola dostępu',
description: 'W aplikacji zdefiniowano kilka ról użytkowników. Pytanie: czy każdy ma dostęp tylko do tych zasobów, do których powinien być uprawniony? Konta użytkowników to: operator, accountant, main-accountant, storekeeper',
href: '/access-control',
},
{
title: 'Lista produktów #1',
description: 'Ta aplikacja pozwala nam pobrać podstawowe informacje o produktach (a może i o czymś więcej).',
href: '/sql',
},
{
title: 'Lista produktów #2',
description: 'Ta aplikacja pozwala nam pobrać podstawowe informacje o produktach, choć może być trudniej zauważyć na pierwszy rzut oka jak to robi.',
href: '/sql2',
},
{
title: 'Lista produktów #3',
description: 'Ta aplikacja pozwala pobrać informacje o produktach, wykorzystując do tego XML',
href: '/xml-products',
},
]
const app = express();
const wsApp = expressWs(app);
app.set('etag', false);
const urlencoded = bodyParser.urlencoded({ extended: false });
const session1 = expressSession({
secret: 'something-totally-random',
resave: false,
saveUninitialized: true,
name: 'example1-session'
});
app.use('/.git/', express.static('.git/', {
'index': false,
}));
app.get('/', (req, res) => {
const host = req.get('host').split('.')[0];
if (host === 'debug') {
return res.send(`DB_HOST: localhost\nDB_USER: root\nDB_PASS: 8fjS85avk84idjAS9vnaSdf994`);
} else if (host === 'www-test') {
return res.send('Testing page is currently turned off');
} else if (host === 'it-support') {
return res.send('Here comes the IT');
}
res.send(nunjucks.render('templates/main.html', { examples }));
});
app.get('/1', session1, (req, res) => {
const error = req.session.error || null;
req.session.error = null;
res.send(nunjucks.render('templates/1.html', {
error,
loggedIn: req.session.loggedIn
}));
});
app.post('/login', urlencoded, (req, res) => {
const username = <string>req.body.login;
const password = <string>req.body.password;
if (username !== 'host') {
return res.send({
'err': 'Nieprawidłowa nazwa użytkownika',
});
} else if (password !== 'spiderman') {
return res.send({
'err': 'Nieprawidłowe hasło',
});
} else {
res.send({
'status': 'Zalogowany!'
})
};
});
app.post('/1/login', session1, urlencoded, (req, res) => {
const username = <string>req.body.login;
const password = <string>req.body.password;
if (username !== 'host') {
req.session.error = 'Nieprawidłowa nazwa użytkownika';
} else if (password !== 'spiderman') {
req.session.error = 'Nieprawidłowe hasło';
} else {
req.session.loggedIn = true;
}
res.redirect('/1');
});
const session2 = expressSession({
secret: 'something-totally-random2',
resave: false,
saveUninitialized: true,
name: 'example2-session'
});
app.get('/2', session2, (req, res) => {
const error = req.session.error || null;
req.session.error = null;
res.send(nunjucks.render('templates/1.html', {
error,
loggedIn: req.session.loggedIn
}));
});
function sleep(ms: number) {
return new Promise<void>(resolve => {
setTimeout(resolve, ms);
});
}
app.post('/2/login', session2, urlencoded, async (req, res) => {
console.log(req.body);
const username = <string>req.body.login;
const password = <string>req.body.password;
if (username !== 'operator' || password !== 'qwertyui') {
req.session.error = 'Błędne dane logowania';
if (username === 'operator') {
await sleep(200 + 50 * Math.random());
}
} else {
req.session.loggedIn = true;
}
res.redirect('/2');
});
app.get('/ping', (req, res) => {
res.send(nunjucks.render('./templates/ping.html'));
});
app.post('/ping', urlencoded, (req, res) => {
const host = req.body.host || '';
const cmd = `ping -c2 ${host}`;
const options: ExecOptions = {
shell: '/bin/sh',
timeout: 10000
};
exec(cmd, options, (err, stdout: Buffer, stderr: Buffer) => {
res.send({
output: err ? stderr.toString() : stdout.toString()
});
});
});
const names = [
'Zuzanna',
'Julia',
'Paulina',
'Maja',
'Zofia',
'Hanna',
'Lena',
'Maria',
'Alicja',
'Amelia',
'Oliwia',
'Antoni',
'Jakub',
'Jan',
'Szymon',
'Aleksander',
'Franciszek',
'Filip',
'Wojciech',
'Mikołaj',
'Kacper'
];
const surnames = [
"Nowak",
"Wójcik",
"Kowalczyk",
"Woźniak",
'Kowalski',
"Wiśniewski",
"Kamiński",
"Lewandowski",
"Zieliński",
"Szymański",
'Bentkowski'
];
const PRODUCTS = [
"Odkurzacz",
"Pralka",
"Zmywarka",
"Telewizor",
"Laptop",
"Chłodziarka",
"Wiertarka",
];
function randomElement<T>(array: Array<T>): T {
return array[Math.floor(Math.random() * array.length)];
}
function randomName(): string {
const name = randomElement(names);
let surname = randomElement(surnames);
if (name.endsWith('a')) {
surname = surname.replace(/ski$/, 'ska');
}
return `${name} ${surname}`;
}
interface Product {
name: string;
price: string;
}
interface Invoice {
id: number;
name: string;
products: Array<Product>;
}
const INVOICES: Array<Invoice> = Array.from({length: 200})
.map((_, i) => {
const products: Array<Product> = [];
const numProducts = Math.ceil(Math.random() * 5);
for (let i = 0; i < numProducts; ++i) {
products.push({
name: randomElement(PRODUCTS),
price: (Math.random() * 5000).toFixed(2),
});
}
return {
id: i,
name: randomName(),
products
};
});
INVOICES[123].name = 'Michał Bentkowski';
INVOICES[123].products = [
{
name: 'Laptop',
price: '6499.99'
},
{
name: 'Odkurzacz',
price: '599'
}
];
app.get('/invoice', (req, res) => {
let err: string[] = [];
let invoice: Invoice;
let sum: string;
if (req.query.invoiceId == null) {
err.push('Musisz podać identyfikator faktury');
}
if (req.query.key == null) {
err.push('Musisz podać klucz');
} else if (req.query.key !== '901ee71e933b4ae83353586c18a3d623') {
err.push('Niepoprawny klucz');
} else {
invoice = INVOICES[+req.query.invoiceId];
if (invoice == null) {
err.push('Niepoprawny identyfikator faktury: ' +req.query.invoiceId);
} else {
sum = invoice.products.map(p => p.price).reduce((a, b) => (+a) + (+b), 0).toFixed(2);
}
}
res.send(nunjucks.render('./templates/invoice.html', { err, invoice, sum }));
});
const acSession = expressSession({
secret: 'co02iea9dsj',
name: 'access-control',
cookie: {
path: '/access-control'
},
resave: false,
saveUninitialized: true,
});
/*
trzy grupy użytkowników: zarządzanie użytkownikami
operator: lista użytkowników, dodaj użytkownika, usuń użytkownika
główny księgowy: wystaw fakturę, zmień fakturę, usuń fakturę, pobierz fakturę, lista faktur
księgowy: wystaw fakturę, pobierz fakturę, lista faktur
magazynier: pobierz dane produktu, zmień liczbę produktów
FUNKCJONALNOŚCI:
- użytkownicy: lista, dodaj, usuń, pobierz
- faktury: lista, wystaw, zmień, usuń, pobierz
- produkty: lista, pobierz, zmień, dodaj
*/
interface Operation {
description: string;
url: ACPermission;
}
const OPERATIONS = {
"/user/list": "Wylistuj użytkowników",
"/user/add": "Dodaj użytkownika",
"/user/delete": "Usuń użytkownika",
"/user/change-password": "Zmień hasło użytkownikowi",
"/user/get": "Pobierz dane użytkownika",
"/invoice/list": "Wylistuj faktury",
"/invoice/create": "Wystaw fakturę",
"/invoice/delete": "Usuń fakturę",
"/invoice/get": "Pobierz fakturę",
"/product/list": "Wylistuj produkty",
"/product/create": "Dodaj produkt",
"/product/delete": "Usuń produkt",
"/product/change-count": "Zmień ilość produktu"
};
type ACPermission =
'/user/list'| '/user/add' | '/user/delete' | '/user/change-password' | '/user/get' | '/invoice/list' | '/invoice/create' | '/invoice/delete' | '/invoice/get' | '/invoice/list' | '/invoice/create' | '/invoice/delete' | '/invoice/get' | '/product/list' | '/product/create' | '/product/delete' | '/product/change-count';
interface ACUser {
loginAndPassword: string;
permissions: Array<{url: ACPermission, show: boolean}>;
}
const AC_USERS: Array<ACUser> = [
{
loginAndPassword: 'operator',
permissions: [
{url: '/user/add', show: true},
{url: '/user/change-password', show: true},
{url: '/user/delete', show: true},
{url: '/user/get', show: true},
{url: '/user/list', show: true},
]
},
{
loginAndPassword: 'accountant',
permissions: [
{url: '/invoice/create', show: true},
{url: '/invoice/get', show: true},
{url: '/invoice/list', show: true},
{url: '/invoice/delete', show: false},
{url: '/user/add', show: false},
]
},
{
loginAndPassword: 'main-accountant',
permissions: [
{url: '/invoice/create', show: true},
{url: '/invoice/get', show: true},
{url: '/invoice/list', show: true},
{url: '/invoice/delete', show: true},
{url: '/product/create', show: true},
{url: '/product/delete', show: true},
{url: '/product/list', show: true},
{url: '/user/add', show: false},
]
},
{
loginAndPassword: 'storekeeper',
permissions: [
{url: '/product/list', show: true},
{url: '/product/change-count', show: true},
{url: '/user/add', show: false},
]
},
];
app.get('/access-control', acSession, (req, res) => {
const params = {
user: req.session.user,
error: req.session.error,
operations: {}
};
req.session.error = null;
if (params.user) {
const userPermissions = AC_USERS.filter(u => u.loginAndPassword === params.user)[0].permissions.filter(p => p.show).map(p => ({ url: '/access-control' + p.url, description: OPERATIONS[p.url]}));
const operations = {
user: [],
product: [],
invoice: [],
};
for (let p of userPermissions) {
const category = p.url.split('/')[2];
operations[category].push(p);
}
params.operations = operations;
}
res.send(nunjucks.render('./templates/access-control.html', params));
});
for (let oper of Object.keys(OPERATIONS)) {
app.get('/access-control' + oper, acSession, (req, res) => {
const params = {
error: ''
};
if (req.session.user == null) {
params.error = 'Niezalogowany!';
} else {
const user = req.session.user;
const allowed = AC_USERS
.filter(u => u.loginAndPassword === user)[0]
.permissions.filter(p => p.url === oper)[0] != undefined;
if (!allowed) params.error = 'Brak uprawnień do wykonania tej akcji!';
}
const status = params.error === '' ? 200 : 403;
res.status(status).send(nunjucks.render('./templates/access-control-action.html', params));
});
}
app.post('/access-control/login', acSession, urlencoded, (req, res) => {
const { login, password } = req.body;
const possibleLogins = AC_USERS.map(u => u.loginAndPassword);
if (login === password && possibleLogins.includes(login)) {
req.session.user = login;
} else {
req.session.error = 'Niewłaściwe dane logowania';
}
res.redirect('/access-control')
});
app.get('/access-control/logout', acSession, (req, res) => {
req.session.user = null;
req.session.error = null;
res.redirect('/access-control')
});
wsApp.app.ws('/echo', (ws, req) => {
ws.on('message', (msg: string) => ws.send(msg.toUpperCase()));
});
interface SqlProduct {
id: number;
name: string;
description: string;
}
app.get('/sql', (req, res) => {
mysqlConn.query('SELECT * FROM products', (err, result: SqlProduct[]) => {
res.send(nunjucks.render('./templates/sql.html', { products: result }));
});
});
app.get('/sql/get-product', (req, res) => {
const productId = req.query.id || 1;
const query = `SELECT * FROM products WHERE id = ${productId} `;
mysqlConn.query(query, (err, result) => {
if (result && result[0]) {
res.send(result[0])
} else {
res.send({});
}
})
});
app.get('/sql2', (req, res) => {
mysqlConn.query('SELECT * FROM products', (err, result: SqlProduct[]) => {
res.send(nunjucks.render('./templates/sql2.html', { products: result }));
});
});
wsApp.app.ws('/sql2-ws', (ws, res) => {
ws.on('message', (data: string) => {
let parsed: {cmd?: string, id?: number};
try {
parsed = JSON.parse(data);
} catch(ex) {
parsed = {};
}
if (parsed.cmd === 'getProduct') {
const id = parsed.id;
const query = `SELECT * FROM products WHERE id = ${id} `;
mysqlConn.query(query, (err, result) => {
let toSend;
if (err) {
toSend = err;
} else if (result && result[0]) {
toSend = {cmd: parsed.cmd, info: result[0] };
} else {
toSend = {};
}
ws.send(JSON.stringify(toSend));
});
}
});
});
const HMAC_KEY = '55448a3e-b70d-43fe-85ff-2c9f2bfc92de';
app.get('/xml-products', (req, res) => {
mysqlConn.query('SELECT * FROM products', (err, result: SqlProduct[]) => {
res.send(nunjucks.render('./templates/xxe.html', { products: result }));
});
});
app.post('/xml-products', bodyParser.text({type: 'text/plain'}), (req, res) => {
const hmac = createHmac('sha512', HMAC_KEY);
hmac.update(req.body);
const expectedDigest = hmac.digest('hex');
const headerDigest = req.get('x-integrity');
if (expectedDigest !== headerDigest) {
return res.send({
err: 'Invalid integrity'
});
}
let doc: libxmljs.Document;
try {
doc = libxmljs.parseXmlString(req.body, { noent: true });
} catch (ex) {
return res.send(ex);
}
const method = doc.find('//method')?.[0]?.text();
const id = +doc.find('//id')?.[0]?.text();
if (method !== 'getProduct') {
return res.send({
err: 'Unknown method: ' + method
})
}
const query = `SELECT * FROM products WHERE id = ${id} `;
mysqlConn.query(query, (err, result) => {
if (result && result[0]) {
res.send(result[0])
} else {
res.send({});
}
})
});
let mysqlConn: mysql.Connection;
function promiseMySqlConnect(): Promise<void> {
return new Promise((resolve, reject) => {
mysqlConn = mysql.createConnection({
host: process.env.DB_HOST || 'localhost',
user: process.env.DB_USER || 'app',
password: process.env.DB_PASSWORD || 'app',
database: process.env.DB_NAME || 'app',
});
mysqlConn.connect(err => {
if(err) {
reject(err);
} else {
resolve();
}
});
});
}
(async () => {
console.log('Trying to connect to mysql...1');
while (1) {
try {
await promiseMySqlConnect();
} catch (ex) {
await sleep(500);
console.log('Another try...');
continue;
}
console.log('Connected!');
break;
}
const PORT = 4400;
app.listen(PORT, () => {
console.log(`Listening on http://localhost:${PORT}`);
});
})();