-
Notifications
You must be signed in to change notification settings - Fork 1
/
slides.tex
1616 lines (1428 loc) · 47.7 KB
/
slides.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
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
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
%% -*- latex -*-
\newtoks\nicsroot\directlua{ tex.settoks("nicsroot", os.getenv("NICS_ROOT") or error("NICS_ROOT environment variable has to be set, use the Makefile")) }
\input{\the\nicsroot /src/nics-cached.tex}
\endofdump
\input{\the\nicsroot /src/nics-noncached.tex}
\hypersetup{
pdfauthor={Mihaly Barasz (Nilcons)},
pdftitle={Secure C++ Programming},
}
\nicsgrid=0
\begin{document}
%% \nicstitleslide{images/chain-laptop}{Secure C++ Programming}{Ide szeretnék valami jobbat (könyv és telefon nélkül) vagy csak szebbet}
%% \nicstitleslide{images/asm-puzzle}{Assembly Basics}{Hello World in x86/x86-64 Asm}
%% \nicstitleslide{images/workshop}{Assembly Hello World}{Workshop | Ide lehetne olyan, ahol puzzle-t összeraknak}
%% \nicstitleslide{images/overflow-waterfall}{Stack Buffer Overflows}{Introduction | Valami jobban ``túlcsordulós'' | flood, spill, dam}
%% \nicstitleslide{images/stack-smashing}{Stack Smashing}{Workshop}
%% \nicstitleslide{images/hardhats}{\mono{W^X}}{Executable-space protection}
%% \nicstitleslide{images/stack-coins}{Stack Protector}{Detect stack smashing}
%% \nicstitleslide{images/dice}{ASLR}{Address Randomization}
%% \nicstitleslide{images/ro-maci}{RELRO}{Relocation Read Only | Valami, ami jobban kifér?}
%% \nicstitleslide{images/heap-overflow}{Heap Overflows}{}
%% \nicstitleslide{images/format-string}{Format String Vulnerabilities}{}
%% \nicstitleslide{images/question}{Integer Vulnerabilities}{Valami (egész) számok?}
%% \nicstitleslide{images/race}{Concurrency}{}
%% \nicstitleslide{images/best-practices}{Best Practices}{}
%% \nicstitleslide{images/question}{Modern C++}{Valami modern épület?}
%% \nicstitleslide{images/thankyou}{The End}{Thanks, and good luck with all your coding!}
\nicstitleslide[Scanrail1/shutterstock]{images/titlepage}{Secure C++ Programming}{Dublin, May 13--14, 2019}
\section{Welcome}
\begin{slide}{Introductions}{}
\begin{nicscolumn}
\nicsheader{Each person please say:}
\nicsitem{Their name}
\nicsitem{What is their background}
\nicsitem{How much C/C++ experience they have}
\nicsitem{The main thing they want to learn}
\nicsmedskip
\nicspar{Me: Mihály Bárász <mihaly.barasz@nilcons.com>}
\end{nicscolumn}
\end{slide}
\begin{slide}{Schedule}{}
\begin{nicscolumn*}{1cm}{1.5cm}{8cm}
\nicsheader{Today}
\nicsitem{Assembly}
\nicsitem{Basic vulnerabilities}
\nicsitem{Some philosophy}
\nicsitem{Most important generic protections}
\nicsitem{Down-to-the-metal exercises}
\nicsmedskip
\nicsheader{Tomorrow}
\nicsitem{More vulnerabilities}
\nicsitem{Best practices}
\nicsitem{More philosophy}
\nicsitem{More exercises}
\end{nicscolumn*}
\begin{nicscolumn*}{9.5cm}{2cm}{5.5cm}
\begin{nicsextern}{}
\nicsincludepic{images/schedule}
\end{nicsextern}
\end{nicscolumn*}
\end{slide}
\section{Assembly}
\nicstitleslide{images/asm-puzzle}{Assembly Basics}{Hello World in x86/x86-64 Asm}
\begin{slide}{Resources}{Learning & Reference}
\begin{nicscolumn}
\nicsheader{Online references}
\nicsitem{\url{https://www.felixcloutier.com/x86/}}
%\nicsitem{\url{https://www.aldeid.com/wiki/X86-assembly/Instructions}\\ (less comprehensive, more readable)}
\nicsitem{\url{https://godbolt.org/}}
\nicsitem{\url{https://defuse.ca/online-x86-assembler.htm}\\(online assembler/disassembler)}
\nicsitem{\url{http://www.sandpile.org/}}
\nicsheader{Learning, Books}
\nicsitem{\url{https://en.wikibooks.org/wiki/X86_Assembly}}
\nicsitem{Programming from the Ground Up, Jonathan Bartlett\\
old, 32 bit, but very good \url{https://savannah.nongnu.org/projects/pgubook/}}
\end{nicscolumn}
\end{slide}
\begin{slide}{Syntax}{AT&T vs Intel syntax}
\begin{nicscolumn*}{0.7cm}{2cm}{7.2cm}
\nicsitem{Common on Linux, default syntax for most Linux tools:
\mono{gcc}, \mono{as}, \mono{gdb}\dots\\ ~ \\}
\nicsitem{\mono{mov \%rax, \%rbx}\\
Order: source, destination\\
Registers prefixed with \%}
\nicsitem{\mono{mov $0x41, \%rax}\\
Constants (immediates) prefixed with \$;
C syntax for hexadecimals
}
\end{nicscolumn*}
\begin{nicscolumn*}{8.3cm}{2cm}{7cm}
\nicsitem{Universal on Windows, used in many popular Linux tools
(\mono{\{n,y,f\}asm}), can be switched to in most tools \\ ~}
\nicsitem{\mono{mov rbx, rax}\\Order: destination, source\\}
\nicsitem{\mono{mov rax, 41h}\\
Special syntax for hexadecimals (often \mono{0x} can be used too)
}
\end{nicscolumn*}
\end{slide}
\begin{slide}{Syntax\dots}{AT&T vs Intel}
\begin{nicscolumn*}{0.7cm}{1.5cm}{7cm}
\nicsitem{\mono{mov $symbol, \%rax}\\
Loads the address of \mono{symbol}}
\nicsitem{\mono{mov symbol, \%rax}\\
Loads the content of \mono{symbol}\\}
\nicspar{Memory references:}
\nicsitem{\mono{mov 0x18(\%rdi,\%rsi,4), \%rax}\\
{\tiny displacement(base register, offset register, scalar)}}
\nicsitem{\mono{movq $5, (\%rax)}\\
\mono{movb $5, (\%rax)}\\
\mono{b}, \mono{w}, \mono{l}, \mono{q} suffix to the opcode}
\end{nicscolumn*}
\begin{nicscolumn*}{8.3cm}{1.5cm}{7.5cm}
\nicsitem{\mono{mov rax, symbol}\\ ~}
\nicsitem{\mono{mov rax, [symbol]}\\ ~ \\}
\nicspar{}
\nicsitem{\mono{mov rax, [rdi + 18h + 4*rsi]}\\
{\tiny [base register + displacement + scalar*offset register]}}
\nicsitem{\mono{mov qword [rax], 5}\\
\mono{mov byte [rax], 5} \\
\mono{byte}, \mono{word}, \mono{dword}, \mono{qword} prefix \\ for the operand}
\end{nicscolumn*}
\end{slide}
\begin{slide}{Syntax\dots}{AT&T vs Intel}
\begin{nicscolumn*}{0.7cm}{1.5cm}{7cm}
\nicspar{Mnemonic differences}
\nicsitem{\mono{cwtd}, \mono{cdtl}, \mono{cqto}, \dots\\}
\nicsitem{Comments prefixed with \mono{\#}}
\nicsitem{Directives prefixed with \mono{.}}
\end{nicscolumn*}
\begin{nicscolumn*}{8.3cm}{1.5cm}{7.5cm}
\nicspar{}
\nicsitem{\mono{cwd}, \mono{cdq}, \mono{cqo}, \dots\\}
\nicsitem{Comments prefixed with \mono{;}}
\nicsitem{Directives have no prefix}
\end{nicscolumn*}
\end{slide}
\begin{slide}{Syntax example}{AT&T vs Intel}
\begin{nicscolumn*}{4cm}{1.5cm}{8cm}
\nicsexterncode{c}
\begin{nicsextern}[width=5cm]{}
int example(long long a, int *b) {
return a / b[5];
}
\end{nicsextern}
\end{nicscolumn*}
\begin{nicscolumn*}{0.7cm}{3cm}{7cm}
\nicsexterncode{asm}
\begin{nicsextern}[height=3.7cm]{}
.section .text
.global example
example:
# First argument to %rax and sign extend to %rdx
movq %rdi, %rax
cqto
# b[5] sign extended to %rcx
movslq 20(%rsi), %rcx
idivq %rcx
# Result is in %rax where we need it
ret
\end{nicsextern}
\end{nicscolumn*}
\begin{nicscolumn*}{8.3cm}{3cm}{7.5cm}
\nicsexterncode{asm}
\begin{nicsextern}[height=3.7cm]{}
section .text
global example
example:
; First argument to rax and sign extend to rdx
mov rax, rdi
cqo
; b[5] sign extended to rcx
movsxd rcx, dword [rsi+20]
idiv rcx
; Result is in rax where we need it
ret
\end{nicsextern}
\end{nicscolumn*}
\end{slide}
\nicstitleslide{images/workshop}{Assembly Hello World}{Workshop}
\begin{slide}{Assembly Workshop}{Hello World!}
\begin{nicscolumn}
\nicsheader{Goal: get comfortable with the environment and assembly on Linux}
\nicspar{Let's write following simple programs:}
\nicsitem{Exit with some fix exit code (no crashing!)}
\nicsitem{Exit with the exit code equal to number of command line arguments \\
Hint: debug and inspect the top of the stack}
\nicsitem{Classic ``Hello World''}
\nicspar{Additionally:}
\nicsitem{Try both syntaxes}
\nicsitem{Try 32 and 64 bit executables}
\nicsitem{Learn how to get just the actual code part}
\end{nicscolumn}
\end{slide}
\begin{slide}{Assembly Workshop\dots}{}
\begin{nicscolumn*}{0.7cm}{1.5cm}{7cm}
% TODO(klao): a slide with the regular ABI
% References
\nicsheader{Linux syscall ABI}
\nicspar{64 bit}
\nicsitem{Syscall number in \mono{rax}}
\nicsitem{Arguments: \mono{rdi}, \mono{rsi}, \mono{rdx}, \mono{r10}, \mono{r8}, \mono{r9}}
\nicsitem{Instruction: \mono{syscall}}
\nicsitem{Result in \mono{rax}}
\nicsitem{Note: clobbers \mono{rcx} and \mono{r11}}
\end{nicscolumn*}
\begin{nicscolumn*}{8.3cm}{1.5cm}{7.5cm}
\nicsheader{}
\nicspar{32 bit}
\nicsitem{Syscall number in \mono{eax}}
\nicsitem{Arguments: \mono{ebx}, \mono{ecx}, \mono{edx}, \mono{esi}, \mono{edi}, \mono{ebp}}
\nicsitem{Instruction: \mono{int 0x80}}
\nicsitem{Result in \mono{eax}}
\nicsitem{Preserves registers}
\end{nicscolumn*}
\end{slide}
\begin{slide}{Assembly Workshop\dots}{}
\begin{nicscolumn*}{1cm}{1.7cm}{14cm}
\nicsheader{Assembling and linking}
\end{nicscolumn*}
\begin{nicscolumn*}{1.5cm}{2.5cm}{5cm}
\SMALL\nicsverbnoautocenter1
\begin{nicsterm}
as -o program.o program.s
ld -o program program.o
\end{nicsterm}
\end{nicscolumn*}
\begin{nicscolumn*}{7cm}{2.5cm}{5cm}
\SMALL\nicsverbnoautocenter1
\begin{nicsterm}
as --32 -o program.o program.s
ld -m elf_i386 -o program program.o
\end{nicsterm}
\end{nicscolumn*}
\begin{nicscolumn*}{1.5cm}{3.8cm}{5cm}
\SMALL\nicsverbnoautocenter1
\begin{nicsterm}
yasm -f elf64 program.s
ld -o program program.o
\end{nicsterm}
\end{nicscolumn*}
\begin{nicscolumn*}{7cm}{3.8cm}{5cm}
\SMALL\nicsverbnoautocenter1
\begin{nicsterm}
yasm -f elf32 program.s
ld -m elf_i386 -o program program.o
\end{nicsterm}
\end{nicscolumn*}
\begin{nicscolumn*}{1cm}{5.2cm}{14cm}
\nicsheader{Extracting the code}
\end{nicscolumn*}
\begin{nicscolumn*}{1.5cm}{6cm}{10cm}
\SMALL\nicsverbnoautocenter1
\begin{nicsterm}
objcopy -O binary -j .text program program.bin
\end{nicsterm}
\end{nicscolumn*}
\begin{nicscolumn*}{1.5cm}{6.7cm}{10cm}
\SMALL\nicsverbnoautocenter1
\begin{nicsterm}
yasm -f bin program.s -o program.bin
# BITS 32 or BITS 64 directive is required!
\end{nicsterm}
\end{nicscolumn*}
\begin{nicscolumn*}{10cm}{5.2cm}{14cm}
\nicsheader{Disassemble}
\end{nicscolumn*}
\begin{nicscolumn*}{10.5cm}{6cm}{7cm}
\SMALL\nicsverbnoautocenter1
\begin{nicsterm}
objdump -d
disassemble # in gdb
\end{nicsterm}
\end{nicscolumn*}
\end{slide}
% TODO(klao): tool slide:
% objdump -d
% gdb disassemble
% readelf ...
% TODO(klao): -g
% Into Makefiles and note on the slides
\section{Stack Smashing}
\nicstitleslide{images/overflow-waterfall}{Stack Buffer Overflows}{Introduction}
\begin{slide}{Stack Buffer Overflows}{Brief history}
\begin{nicscolumn}
\nicsitem{\slant{1972} -- US Air Force study}
% TODO(klao): learn
\nicsitem{\nicsleadword{\slant{1988} --} Morris worm \\ CERT formed}
\nicsitem{\slant{1995} -- ``How to Write Buffer Overflows''}
\nicsitem{\slant{1996} -- ``Stack Smashing''}
\nicsitem{\slant{2003} -- Blaster worm}
\kern1cm
\nicspar{``Memory Corruption Attacks The (almost) Complete History'' \\
\tiny\url{https://media.blackhat.com/bh-us-10/whitepapers/Meer/BlackHat-USA-2010-Meer-History-of-Memory-Corruption-Attacks-wp.pdf}}
\end{nicscolumn}
\end{slide}
\begin{slide}{Stack Buffer Overflows}{Brief history}
\begin{nicscolumn}
\begin{nicsextern}[width=8cm]{}
\nicsincludepic{images/mem-corruption-chart}
\end{nicsextern}
\end{nicscolumn}
\end{slide}
% TODO(klao): next two slides fix %rbp pointer
\begin{slide}{Stack Frame}{Stack Frame in GCC}
\begin{nicscolumn*}{3cm}{2cm}{14cm}
\nicspar{\nicsincludepic[width=10cm]{images/stack-frame-k}}
\end{nicscolumn*}
\end{slide}
\begin{slide}{Stack Frame}{Buffer Overflow}
\begin{nicscolumn*}{3cm}{2cm}{14cm}
\nicspar{\nicsincludepic[width=10cm]{images/stack-frame-bo.pdf}}
\end{nicscolumn*}
\end{slide}
\begin{slide}{Stack Frame}{Function Prologue and Epilogue}
\begin{nicscolumn}
\nicsexterncode{asm}
\begin{nicsextern}[height=6cm]{}
push %rbp Save frame pointer
movq %rsp, %rbp Initialize frame pointer
pushq %rbx Save non-volatile registers
pushq %r12 clobbered by function
pushq %r13
subq $48, %rsp Allocate stack frame
... Function body
addq $48, %rsp Deallocate stack frame
popq %r13 Restore registers
popq %r12
popq %rbx
leave Restore %rbp
and deallocate stack
ret
\end{nicsextern}
\end{nicscolumn}
\end{slide}
\begin{slide}{Stack Frame}{Function Prologue and Epilogue (Actually)}
\begin{nicscolumn}
\nicsexterncode{asm}
\begin{nicsextern}[height=6cm]{}
movq %rbx,-24(%rsp) Save registers
movq %r12,-16(%rsp)
movq %r13,-8(%rsp)
subq $72, %rsp Allocate stack frame
... Function body
movq 48(%rsp),%rbx Restore registers
movq 56(%rsp),%r12
movq 64(%rsp),%r13
addq $72, %rsp Deallocate stack frame
ret
\end{nicsextern}
\end{nicscolumn}
\end{slide}
\begin{slide}{Stack Frame}{Example}
\begin{nicscolumn}
\nicsexterncode{c}
\begin{nicsextern}[height=5cm]{}
#include <stdio.h>
#include <string.h>
void greet(char *name) {
char buf[20] = "Hello: ";
strcat(buf, name);
puts(buf);
}
\end{nicsextern}
\end{nicscolumn}
\end{slide}
\begin{slide}{Stack Frame}{Example}
\begin{nicscolumn*}{1cm}{1.2cm}{14cm}
\nicsexterncode{asm}
\begin{nicsextern}[height=5.75cm]{}
greet:
pushl %ebp
movl %esp, %ebp
subl $40, %esp
movl $0x6c6c6548, -28(%ebp)
movl $0x203a6f, -24(%ebp)
movl $0, -20(%ebp)
movl $0, -16(%ebp)
movl $0, -12(%ebp)
subl $8, %esp
pushl 8(%ebp)
leal -28(%ebp), %eax
pushl %eax
call strcat
addl $16, %esp
subl $12, %esp
leal -28(%ebp), %eax
pushl %eax
call puts
addl $16, %esp
nop
leave
ret
\end{nicsextern}
\kern3mm
\begin{nicsterm}
gcc -m32 -O0 -S -fno-stack-protector -fno-pie
\end{nicsterm}
\end{nicscolumn*}
\end{slide}
\begin{slide}{Stack Frame}{Example}
\begin{nicscolumn*}{1cm}{1.5cm}{14cm}
\nicsexterncode{asm}
\begin{nicsextern}[height=5cm]{}
greet:
pushl %ebx
subl $48, %esp
movl $0x6c6c6548, 20(%esp)
movl $0x203a6f, 24(%esp)
movl $0, 28(%esp)
movl $0, 32(%esp)
movl $0, 36(%esp)
pushl 56(%esp)
leal 31(%esp), %eax
leal 24(%esp), %ebx
pushl %eax
call strcpy
movl %ebx, (%esp)
call puts
addl $56, %esp
popl %ebx
ret
\end{nicsextern}
\kern5mm
\begin{nicsterm}
gcc -m32 -O2 -S -fno-stack-protector -fno-pie
\end{nicsterm}
\end{nicscolumn*}
\end{slide}
\begin{slide}{Stack Frame}{Example}
\begin{nicscolumn*}{1cm}{1.5cm}{14cm}
\nicsexterncode{asm}
\begin{nicsextern}[height=5cm]{}
greet:
pushl %ebp
xorl %eax, %eax
movl $3, %ecx
movl %esp, %ebp
pushl %edi
leal -20(%ebp), %edi
subl $44, %esp
movl $0x6c6c6548, -28(%ebp)
rep stosl
leal -28(%ebp), %edi
pushl 8(%ebp)
pushl %edi
movl $0x203a6f, -24(%ebp)
call strcat
movl %edi, (%esp)
call puts
addl $16, %esp
movl -4(%ebp), %edi
leave
ret
\end{nicsextern}
\kern5mm
\begin{nicsterm}
gcc -m32 -Os -S -fno-stack-protector -fno-pie
\end{nicsterm}
\end{nicscolumn*}
\end{slide}
\nicstitleslide{images/stack-smashing}{Stack Smashing}{Workshop}
\begin{slide}{Stack Smashing Workshop}{}
\begin{nicscolumn}
\nicsheader{Goal: get first-hand experience}
\nicsitem{All protections turned off}
\nicsitem{Explore GCC stack organization on x86-64}
\nicsitem{Very simple arc injection}
\nicsitem{Basic shell code}
\end{nicscolumn}
\end{slide}
\begin{slide}{Buffer Overflows}{Mitigation}
\begin{nicscolumn}
\nicsheader{What can you do as a programmer?}
\nicsitem{Use \mono{std::string}}
\nicsitem{Use modern, high abstraction C++ (e.g. \mono{std::array})}
\nicsitem{Use safe variants \mono{strncpy}, \mono{strlcat}, \dots}
\nicsitem{Aggressively document and check bounds and invariants}
\nicsitem{Undefined behavior --- \textbf{No!}}
\end{nicscolumn}
\end{slide}
\begin{slide}{Buffer Overflows}{Mitigation}
\begin{nicscolumn}
\nicsheader{Undefined Behavior}
\nicspar{\just Examples of undefined behavior are memory accesses
outside of array bounds, signed integer overflow, null pointer
dereference, modification of the same scalar more than once in
an expression without sequence points, access to an object
through a pointer of a different type, etc. Compilers are not
required to diagnose undefined behavior (although many simple
situations are diagnosed), and \bold{the compiled program is not
required to do anything meaningful}.}
\nicssmallskip
\nicspar{\SMALL\url{https://en.cppreference.com/w/cpp/language/ub}}
\end{nicscolumn}
\end{slide}
\begin{slide}{Buffer Overflows}{Mitigation}
\begin{nicscolumn}
\nicsheader{SEI CERT standard}
\nicspar{\SMALL\url{https://wiki.sei.cmu.edu/confluence/display/seccode/SEI+CERT+Coding+Standards}}
\nicsmedskip
\nicsitem{Familiarize yourself to know the extent}
\nicsitem{When in doubt read the rule(s) and weigh the options}
\nicsbigskip
\nicsheader{C++ Core Guidlines}
\nicspar{\SMALL\url{http://isocpp.github.io/CppCoreGuidelines/}}
\nicsbigskip
\nicspar{And more\dots}
\end{nicscolumn}
\end{slide}
\section{Security Mindset}
\nicstitleslide{images/eng_hack}{Security Mindset}{Engineering vs Hacking}
\begin{slide}{Security Mindset}{}
\begin{nicscolumn}
\nicsheader{Philosophising}
\nicsitem{Our brains are faulty}
\nicsitem{Our models are faulty}
\nicsitem{Our training is bad}
\end{nicscolumn}
\end{slide}
\begin{slide}{Security Mindset}{Brains}
\begin{nicscolumn*}{4cm}{1.5cm}{8cm}
\begin{nicsextern}{}
\nicsincludepic{images/overconfidence}
\end{nicsextern}
\end{nicscolumn*}
\end{slide}
\begin{slide}{Security Mindset}{Models}
\begin{nicscolumn}
\nicsheader{``Secure Perimeter''}
\nicsitem{is almost always wrong}
\end{nicscolumn}
\end{slide}
\begin{slide}{Security Mindset}{Training}
\begin{nicscolumn}
\nicsheader{Code reviews / Testing}
\nicsitem{Don't do ``security'' unless you specifically train for that}
\end{nicscolumn}
\end{slide}
\begin{slide}{Security Mindset}{Brains}
\begin{nicscolumn}
\nicsheader{You don't see your own blindspots}
\end{nicscolumn}
\end{slide}
\begin{slide}{Security Mindset}{Models}
\begin{nicscolumn}
\nicsheader{``You are not a target''}
\nicsitem{is almost always wrong}
\end{nicscolumn}
\end{slide}
\begin{slide}{Security Mindset}{Training}
\begin{nicscolumn}
\nicsheader{Best practices}
\nicsitem{Don't work without the security mindset}
\kern3cm
\nicspar{Bruce Schneier: \href{https://www.schneier.com/blog/archives/2008/03/the_security_mi_1.html}{The Security Mindset}}
\end{nicscolumn}
\end{slide}
\begin{slide}{Security Mindset}{Example}
\begin{nicscolumn}
\nicsexterncode{c}
\begin{nicsextern}[height=6cm]{}
#define LOG_INPUT_SIZE 80
int outputFilenameToLog(char *filename) {
int success;
// buffer with size appropriate for logging
char buf[LOG_INPUT_SIZE];
// copy filename to buffer
strcpy(buf, filename);
// save to log
success = saveToLog(buf);
return success;
}
\end{nicsextern}
\end{nicscolumn}
\end{slide}
\begin{slide}{Security Mindset}{Example}
\begin{nicscolumn}
\nicsexterncode{c}
\begin{nicsextern}[height=6cm]{}
#define LOG_INPUT_SIZE 80
int outputFilenameToLog(char *filename, int length) {
int success;
// buffer with size appropriate for logging
char buf[LOG_INPUT_SIZE];
// copy filename to buffer
strncpy(buf, filename, length);
// save to log
success = saveToLog(buf);
return success;
}
\end{nicsextern}
\end{nicscolumn}
\end{slide}
\begin{slide}{Security Mindset}{Example}
\begin{nicscolumn}
\nicsexterncode{c}
\begin{nicsextern}[height=4cm]{}
int outputFilenameToLog(char *filename, int length) {
int success;
std::string buf = filename;
// save to log
success = saveToLog(buf.c_str());
return success;
}
\end{nicsextern}
\end{nicscolumn}
\end{slide}
\begin{slide}{Security Mindset}{Example}
\begin{nicscolumn}
\nicsexterncode{c}
\begin{nicsextern}[height=6cm]{}
#define LOG_INPUT_SIZE 80
int outputFilenameToLog(char *filename, int length) {
int success;
char buf[LOG_INPUT_SIZE];
if (length > LOG_INPUT_SIZE - 1)
return -1;
strncpy(buf, filename, length);
buf[length] = 0;
success = saveToLog(buf);
return success;
}
\end{nicsextern}
\end{nicscolumn}
\end{slide}
\begin{slide}{Security Mindset}{Example}
\begin{nicscolumn}
\nicsexterncode{c}
\begin{nicsextern}[height=6cm]{}
#define LOG_INPUT_SIZE 80
int outputFilenameToLog(char *filename, int length) {
int success;
char buf[LOG_INPUT_SIZE];
if (length > LOG_INPUT_SIZE - 1)
return -1;
strncpy(buf, filename, LOG_INPUT_SIZE - 1);
buf[LOG_INPUT_SIZE - 1] = 0;
success = saveToLog(buf);
return success;
}
\end{nicsextern}
\end{nicscolumn}
\end{slide}
\begin{slide}{Security Mindset}{Brains}
\begin{nicscolumn}
\nicsheader{Engineer vs Hacker}
\nicsitem{Coffee machine story}
\end{nicscolumn}
\end{slide}
\section{W^X}
\nicstitleslide{images/hardhats}{\mono{W^X}}{Executable-space protection}
\begin{slide}{W^X}{History}
\begin{nicscolumn}
\nicsitem{First in OpenBSD in 2003}
\nicsitem{Windows XP, 2004}
\nicsitem{Linux}
\begin{nicsindent}
\nicsitem{PaX: 2000}
\nicsitem{Exec Shield: 2003}
\end{nicsindent}
\kern4mm
\nicsitem{NX bit in AMD's 64 bit CPUs}
\kern4mm
\nicsitem{Enabled by default in all modern 64bit distros}
\nicsitem{In 32bit mode enabled with PAE}
\end{nicscolumn}
\end{slide}
\begin{slide}{W^X}{Explore}
\begin{nicscolumn}
\nicsitem{Check binaries and .so's with \mono{readelf -l}}
\nicsitem{Check running processes: \mono{/proc/<pid>/maps}}
\kern3mm
\nicsitem{Our ``target'' from Workshop 2, what if we remove the \mono{-z execstack}}
\begin{nicsindent}
\nicsitem{Would arc injection work?}
\nicsitem{Would shell code work?}
\nicsitem{How does it fail?}
\end{nicsindent}
\end{nicscolumn}
\end{slide}
\begin{slide}{W^X}{Safe?}
\begin{nicscolumn}
\nicsheader{How much protection does it provide?}
\nicssmallskip
\nicspar{\just Say we find a stack buffer overflow. We can't run a
shell code unless we un-\mono{mprotect} it, but we can't just
call \mono{mprotect} because we can't run arbitrary code. So,
we can't do too much right?}
\end{nicscolumn}
\end{slide}
\begin{slide}{Return Oriented Programming}{}
\begin{nicscolumn*}{1cm}{2cm}{6cm}
\tiny\nicsverbnoautocenter1
\begin{nicscode}
| |
|--------|
| 0x0765 | §\sans{← Original ret address}
|--------|
| |
\end{nicscode}
\end{nicscolumn*}
\end{slide}
\begin{slide}{Return Oriented Programming}{}
\begin{nicscolumn*}{1cm}{2cm}{6cm}
\tiny\nicsverbnoautocenter1
\begin{nicscode}
| ... |
|--------|
| 0xgag1 | §\sans{← Original ret address}
| 0x0 |
|--------|
| 0xg002 |
| 0x3c |
|--------|
| 0xgeg3 |
| ... |
\end{nicscode}
\end{nicscolumn*}
\begin{nicscolumn*}{8cm}{2cm}{6cm}
\tiny\nicsverbnoautocenter1
\begin{nicscode}
0xgag1: pop %rdi
ret
\end{nicscode}
\nicssmallskip
\begin{nicscode}
0xg002: pop %rax
ret
\end{nicscode}
\nicssmallskip
\begin{nicscode}
0xgeg3: syscall
\end{nicscode}
\end{nicscolumn*}
\end{slide}
\begin{slide}{Return Oriented Programming}{Discussion}
\begin{nicscolumn}
\nicsitem{All executable memory regions can be used}
\begin{nicsindent}
\nicsitem{Main program's \mono{.text}}
\nicsitem{Shared libraries}
\end{nicsindent}
\nicsitem{Not security specific libraries rarely checked for presence of rop gadgets}
\end{nicscolumn}
\end{slide}
\begin{slide}{Return Oriented Programming}{Demo}
\begin{nicscolumn}
\nicspar{Try the \mono{ropper} tool}
\end{nicscolumn}
\end{slide}
\section{Stack Protector}
\nicstitleslide{images/stack-coins}{Stack Protector}{Detect stack smashing}
\begin{slide}{Stack Protector}{History}
\begin{nicscolumn}
\nicsitem{StackGuard: 1997}
\nicsitem{GCC}
\begin{nicsindent}
\nicsitem{2001 -- 2005 ProPolice (IBM)}
\nicsitem{2005: reimplemented \mono{-fstack-protector} and \mono{-fstack-protector-all}}
\nicsitem{2012: \mono{-fstack-protector-strong}}
\nicsitem{On by default in some distros (Arch, Fedora -- strong), but not in others (Debian)}
\end{nicsindent}
\end{nicscolumn}
\end{slide}
\begin{slide}{Stack Protector}{Explore}
\begin{nicscolumn}
\nicsitem{Check Workshop 2's ``target'' without \mono{-fno-stack-protector}}
\nicsitem{Check with \mono{-fstack-protector} and \mono{-fstack-protector-all}}
\nicsitem{Godbolt demo}
\end{nicscolumn}
\end{slide}
\begin{slide}{Stack Protector}{Discussion}
\begin{nicscolumn}
\nicsitem{Only protects the return address}
\nicsitem{Local function pointers}
\begin{nicsindent}
\nicsitem{Tools to rearrange local variables}
\end{nicsindent}
\nicsitem{Initialized once for the lifetime of a thread}
\end{nicscolumn}
\end{slide}
\section{ASLR}
\nicstitleslide{images/dice}{ASLR}{Address Randomization}
\begin{slide}{Address Space Layout Randomization}{History}
\begin{nicscolumn}
\nicsitem{Linux}
\begin{nicsindent}
\nicsitem{2001: PaX project}
\nicsitem{2002: full implementation}
\nicsitem{2005: enabled by default (2.6.12)}
\nicsitem{????: PIE by default}
\end{nicsindent}
\nicsitem{OpenBSD: 2003 first ASLR by default, 2008 first to PIE by default}
\nicsitem{Windows: 2007, Vista (not for old binaries)}
\nicsitem{Mac OS X: 2007 system libs, 2011 for all apps}
\nicsitem{iOS and Android: 2011}
\end{nicscolumn}
\end{slide}
\begin{slide}{ASLR}{What and how}
\begin{nicscolumn}
\nicsitem{What is randomized?}
\nicsitem{What isn't randomized?}
\nicsitem{Randomized by how much?}
\end{nicscolumn}
\end{slide}
\begin{slide}{ASLR}{Explore}
\begin{nicscolumn}
\nicsheader{Goal: learn details of ASLR in Linux}
\nicsitem{Build (or choose) a PIE executable}
\nicsitem{Observe its memory map over multiple runs}
\nicsitem{How many independently randomized segments are there?}
\nicsitem{What are they?}
\nicsitem{Write your own C or C++ program and check your findings ``from within''}
\nicsitem{Repeat for 64 and 32 bits}
\nicssmallskip
\nicsheader{Additionally}
\nicsitem{What about non-PIE executables?}
\end{nicscolumn}
\end{slide}
\begin{slide}{ASLR}{How to defeat}
\begin{nicscolumn}
\nicsitem{Reduce entropy}
\begin{nicsindent}
\nicsitem{Heap spraying and NOP slides}
\end{nicsindent}
\nicsitem{Information leak}
\begin{nicsindent}
\nicsitem{Through other vulnerability}
\nicsitem{Side channel}
\begin{nicsindent}
\nicsitem{\href{http://www.cs.ucr.edu/~nael/pubs/micro16.pdf}{Jump Over ASLR}}
\nicsitem{\href{https://www.vusec.net/projects/anc/}{ASLR ⊕ Cache}}
\end{nicsindent}
\end{nicsindent}
\nicsitem{Brute force}
\begin{nicsindent}
\nicsitem{Mitigation: throttle crashes}
\end{nicsindent}
\end{nicscolumn}
\end{slide}
\begin{slide}{ASLR}{Linux details}
\begin{nicscolumn}
\nicsheader{Performance}
\nicsitem{On x86-32 there is quite a price (\href{http://ewontfix.com/18/}{read})}
\nicsitem{x86-64: much better, hardware support for PC-relative addressing, but not free}
\nicsmedskip
\nicsheader{Statically linked binaries}
\nicsitem{Limited ASLR}
\nicsitem{Build a dynamic binary which has everything statically linked}
\end{nicscolumn}
\end{slide}
\begin{slide}{ASLR}{Enable/disable}
\begin{nicscolumn}
\Small
\begin{nicsterm}
echo 0 > /proc/sys/kernel/randomize_va_space
sysctl -w kernel.randomize_va_space=0
\end{nicsterm}
\nicsitem{0: no randomization}
\nicsitem{1: conservative randomization (shared libs, stack, mmap, VDSO)}
\nicsitem{2: full randomization (brk() aka. heap too)}
\nicsmedskip
\nicspar{\mono{personality} syscall to change execution domain for
a single process (and its children)}
\begin{nicsterm}
setarch `uname -m` -R /bin/bash
\end{nicsterm}
\nicsmedskip
\nicspar{GDB turns it off by default for easier debugging! Turn
back on with \\ \mono{set disable-randomization off} (or simply
\mono{aslr on} with GEF and co.)}
\end{nicscolumn}
\end{slide}
%% End of day 1
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Trade-offs}
\nicstitleslide{images/chess}{Security is a Trade-off}{}
\begin{slide}{All or nothing?}{}
\begin{nicscolumn}
\nicspar{Security is not all or nothing}
\nicspar{Have to prioritize it together with}
\begin{nicsindent}
\nicsitem{costs}
\nicsitem{speed of development}
\nicsitem{maintainability}
\nicsitem{performance}
\nicsitem{code quality}
\nicsitem{\dots}