-
Notifications
You must be signed in to change notification settings - Fork 5
/
x86-crash-course.tex
1409 lines (1263 loc) · 44.8 KB
/
x86-crash-course.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
\documentclass[]{beamer}
\usetheme[block=fill]{metropolis}
%\usepackage{beamerthemesplit}
\usepackage{tabularx}
\usepackage{epstopdf}
\usepackage{amsmath}
\usepackage{tipa}
\usepackage{mathptmx}
\usepackage[11pt]{moresize}
\usepackage{tikz}
\usepackage{drawstack}
\usepackage{booktabs}
\usepackage{multirow}
\usetikzlibrary{shapes}
\tikzset{%
stack/.style={%
rectangle split,%
draw,%
text width=10em,%
minimum width=10.1em,%
anchor=west}
}
% solarized color scheme
\definecolor{base03}{HTML}{002B36}
\definecolor{base02}{HTML}{073642}
\definecolor{base01}{HTML}{586E75}
\definecolor{base00}{HTML}{657B83}
\definecolor{base0}{HTML}{839496}
\definecolor{base1}{HTML}{93A1A1}
\definecolor{base2}{HTML}{EEE8D5}
\definecolor{base3}{HTML}{FDF6E3}
\definecolor{yellow}{HTML}{B58900}
\definecolor{orange}{HTML}{CB4B16}
\definecolor{red}{HTML}{DC322F}
\definecolor{magenta}{HTML}{D33682}
\definecolor{violet}{HTML}{6C71C4}
\definecolor{blue}{HTML}{268BD2}
\definecolor{cyan}{HTML}{2AA198}
\definecolor{green}{HTML}{859900}
\usepackage{listings}
\lstset{%
showspaces=false,
showtabs=false,
breaklines=true,
belowskip=10pt,
aboveskip=10pt,
abovecaptionskip=0pt,
sensitive=true,
backgroundcolor=\color{base3},
keywordstyle=\color{cyan},
commentstyle=\color{base1},
stringstyle=\color{blue},
numberstyle=\color{violet},
showstringspaces=false,
breakatwhitespace=true,
basicstyle=\ttfamily\scriptsize,
%lineskip={-1.75pt},
columns=fullflexible,
frame=tb
}
\makeatletter
\def\hlinewd#1{%
\noalign{\ifnum0=`}\fi\hrule \@height #1 %
\futurelet\reserved@a\@xhline}
\makeatother
\definecolor{pureyellow}{HTML}{FFFF00}
\newcommand*{\yellowemph}[1]{%
\tikz\node[rectangle, fill=pureyellow, rounded corners, inner sep=0.3mm]{#1};%
}
\newenvironment{changemargin}[2]{%
\begin{list}{}{%
\setlength{\topsep}{0pt}%
\setlength{\leftmargin}{#1}%
\setlength{\rightmargin}{#2}%
\setlength{\listparindent}{\parindent}%
\setlength{\itemindent}{\parindent}%
\setlength{\parsep}{\parskip}%
}%
\item[]}{\end{list}}
\newcommand{\tab}{\hspace*{2em}}
\newcommand*\oldmacro{}%
\let\oldmacro\insertshorttitle%
\renewcommand*\insertshorttitle{%
\oldmacro\hfill%
\insertframenumber\,/\,\inserttotalframenumber}
\title{How to Read the Matrix -- x86 Crash Course}
\subtitle{With a focus on Linux and a glance to x86\_64}
\author[NECSTLab]{Mario Polino, Armando Bellante, Lorenzo Binosi}
\institute{DEIB, Politecnico di Milano}
\date{\today}
\begin{document}
\begin{frame}
\titlepage
\end{frame}
% \begin{frame}{Credits --- contributors as of \today}
% \begin{itemize}
% \item Main contributor: Andrea Mambretti (wrote the first version)
% \item Mario Polino
% \item Marcello Pogliani
% \item Stefano Zanero
% \item Federico Maggi
% \end{itemize}
% \end{frame}
%\section{Crash course on x86 Assembly}
\begin{frame}
\frametitle{How to Read the Matrix!}
\centering
\includegraphics[width=0.75\paperwidth]{./images/pwndbg_small.png}
\end{frame}
\begin{frame}
\frametitle{Outline}
\tableofcontents
\end{frame}
\begin{frame}{Assembly and Machine Code}
Assembly language: specific to each ISA, mapped to binary code
\begin{figure}
\includegraphics[width=\linewidth]{images/asm-disasm}
\end{figure}
For simplicity, we don't deal with the \emph{linking} process.
\end{frame}
\section{The x86 Architecture}
\subsection{Overview on the common 32-bit Intel Architecture (IA)}
\begin{frame}
\frametitle{Instruction Set Architecture (ISA)}
\begin{itemize}
\item ``Logical'' specification of a computer architecture
\item Concerned with programming concepts
\begin{itemize}
\item instructions, registers, interrupts, memory architecture, \dots
\end{itemize}
\item May differ (widely) from the actual microarchitecture
\item Examples:
\begin{itemize}
\item x86 (IA-32 and x86\_64)
\item ARM (mobile devices)
\item MIPS (embedded devices, e.g.,~consumer routers)
\item AVR, SPARC, Power, RISC V, \dots
\end{itemize}
\end{itemize}
\end{frame}
\begin{frame}{The x86 ISA}
\begin{itemize}
\item Born in 1978, 16-bit ISA (Intel 8086)
\item Evolved to a 32-bit ISA (1985, Intel 80386)
\item Evolved to a 64-bit ISA (2003, AMD Opteron)
\item CISC design (e.g.,~string operations)
\item Many legacy features (e.g,~segmentation)
\item We'll see the basics of the ``core'' ISA
\begin{itemize}
\item There is also the floating point unit, processor-specific features, and extensions such as SIMD (MMX, SSE, SSE2) with their own instructions and registers\footnote{Complete reference: Intel Software Developer's Manual, about 5,000 pages (\url{https://software.intel.com/en-us/articles/intel-sdm})}
\end{itemize}
\end{itemize}
\end{frame}
\begin{frame}
\frametitle{Von Neumann Architecture}
%https://docs.google.com/drawings/d/1dDiokZZ4ba9V6rM9EnOCCIvbmxemN7DxXvL0ipsYPnE/edit?usp=sharing
\begin{figure}
\includegraphics[height=2.3in]{images/Von-Neumann-architecture.pdf}
\end{figure}
\end{frame}
\begin{frame}
\frametitle{IA-32: Registers}
\begin{columns}
\begin{column}{2in}
\begin{figure}
\centering
\includegraphics[width=\textwidth]{images/x86-isa}
\end{figure}
\end{column}
\begin{column}{2.7in}
\begin{itemize}
\item General-purpose registers
\begin{itemize}
\item {\tt EAX}, {\tt EBX}, {\tt ECX}, {\tt EDX}
\item {\tt ESI}, {\tt EDI} (source and destination index for string operations)
\item {\tt EBP} (base pointer)
\item {\tt ESP} (stack pointer)
\end{itemize}
\item Instruction pointer: {\tt EIP}
\begin{itemize}
\item No explicit access
\item Modified by {\tt jmp}, {\tt call}, {\tt ret}
\item Read through the stack (saved IP)
\end{itemize}
\item Program status and control: {\tt EFLAGS}
\item (segment registers)
\end{itemize}
\end{column}
\end{columns}
\end{frame}
\begin{frame}
\frametitle{IA-32: {\tt EFLAGS} register}
\begin{figure}
\includegraphics[height=2.8in]{images/intel-manual-eflags.png}
\end{figure}
\end{frame}
\begin{frame}
\frametitle{IA-32: {\tt EFLAGS} register}
\begin{itemize}
\item 32-bits register, boolean flags
\item \textbf{Program status}: overflow, sign, zero, auxiliary carry (BCD), parity, carry
\begin{itemize}
\item Indicate the result of arithmetic instructions
\item Extremely important for control flow
\end{itemize}
\item \textbf{Program control}: direction flag
\begin{itemize}
\item controls string instructions (auto-increment or auto-decrement)
\end{itemize}
\item \textbf{System}: control operating-system operations
\end{itemize}
\end{frame}
\begin{frame}{Fundamental data types}
\begin{description}
\item[byte] 8 bits
\item[word] 2 bytes
\item[dword] Doubleword, 4 bytes (32 bits)
\item[qword] Quadword, 8 bytes (64 bits)
\end{description}
\end{frame}
\begin{frame}{Assembly: Syntax}
Two main syntaxes:
\begin{itemize}
\item \textbf{Intel}: default in most Windows programs (e.g.,~IDA)
\item \textbf{AT\&T}: default in most UNIX tools (e.g.,~{\tt gdb}, {\tt objdump})
\end{itemize}
Beware: The order of the operands is \textbf{different} \\[.5em]
We will use the Intel syntax
\end{frame}
\begin{frame}{Assembly: Syntax}
\begin{center}
\emph{move the value 0 to \texttt{EAX}}
\par\vskip 0.8em\begin{tabular}{ccc}
\textbf{Intel} & $\qquad$ & \textbf{AT\&T} \\
\texttt{mov eax, 0h} & & \texttt{movl \$0x0,\%eax}\end{tabular}
\end{center}
\vskip 1em
\begin{center}
\emph{move the value 0 to the address contained in \texttt{EBX}+4}
\par\vskip 0.8em\begin{tabular}{ccc}
\textbf{Intel} & $\qquad$ & \textbf{AT\&T} \\
\texttt{mov \textcolor{gray}{DWORD PTR} [ebx+4h],0h} & & \texttt{movl \$0x0,0x4(\%ebx)}\end{tabular}
\end{center}
\end{frame}
\begin{frame}{x86: data movement}
\begin{block}{Examples}
\begin{tabular}{ll}
\multicolumn{2}{l}{Immediate to register:}\\
{\tt mov eax, 4h} & {\small EAX = 4} \\[.4em]
\multicolumn{2}{l}{Register to register:}\\
{\tt mov eax, ebx} & {\small EAX = EBX} \\[.4em]
\multicolumn{2}{l}{Memory to register (and register to memory):}\\
{\tt mov eax, [ebx]} & {\small EAX = *EBX} \\
{\tt mov eax, [ebx + 4h]} & {\small EAX = *(EBC + 4)} \\
{\tt mov eax, [edx + ebx*4 + 8]} & {\small EAX = *(EDX + EBX * 4 + 8)}
\end{tabular}
\end{block}
Note: memory to memory is an \textbf{invalid} combination\footnote{Except in some instructions, such as {\tt movs} (move from string to string).}
\end{frame}
\begin{frame}{x86 Assembly and Machine Code}
Instruction = opcode + operand
\begin{block}{Example}
\centering
\includegraphics[width=.95\linewidth]{images/asm-machine-map}
\end{block}
Beware: in x86, instructions have \textbf{variable length}.
\end{frame}
\subsection{Basic Instructions}
%\begin{frame}
% \frametitle{Instructions}
% \begin{itemize}
% \item{Every processor has a large instruction set (see, e.g., Intel Software Developer's Manual\footnote{\url{https://software.intel.com/en-us/articles/intel-sdm}})}
% \item{A subset of the whole instruction set is usually processor dependent}
% \item{We will focus on a small subset of the general purpose x86 instructions}
% \item{We will use the Intel syntax}
% \item Very good reference: \url{http://ref.x86asm.net/geek32.html}
% \end{itemize}
%\end{frame}
\begin{frame}{Basic instructions}
\begin{itemize}
\item \textbf{Data Transfer}: {\tt mov}, {\tt push}, {\tt pop}, {\tt xchg}, {\tt lea}
\item \textbf{Integer Arithmetic}: {\tt add}, {\tt sub}, {\tt mul}, {\tt imul}, {\tt div}, {\tt idiv}, {\tt inc}, {\tt dec}
\item \textbf{Logical}: {\tt and}, {\tt or}, {\tt not}, {\tt xor}
\item \textbf{Control Transfer}: {\tt jmp}, {\tt jne}, {\tt call}, {\tt ret}
\item and lots more\dots
\end{itemize}
\end{frame}
\begin{frame}{Data Transfer: {\tt mov}}
\begin{itemize}
\item {\tt mov} \underline{\textbf{destination}}, \underline{\textbf{source}}\\
\textbf{source}: immediate, register, memory location\\
\textbf{destination}: register or memory location\\
\item Basic load/store operations
\begin{itemize}
\item Register to register, register to memory, immediate to register, immediate to memory
\item Memory to memory is INVALID (in every instruction)
\end{itemize}
\end{itemize}
\begin{block}{Examples}
\centering
\begin{tabular}{c|c|c}
{\tt MOV eax, ebx} & {\tt MOV eax, FFFFFFFFh} & {\tt MOV ax, bx}\\[.4em]
{\tt MOV [eax],ecx} & {\tt MOV [eax],[ecx]} \color{red}NO!!! & {\tt MOV al, FFh}\\
\end{tabular}
\end{block}
\end{frame}
\begin{frame}{Integer Arithmetics: {\tt add} and {\tt sub}}
\begin{table}
\centering
\begin{tabular}{l|l}
{\tt add} \underline{\textbf{destination}}, \underline{\textbf{source}} & {\tt sub} \underline{\textbf{destination}}, \underline{\textbf{source}} \\
dest $\leftarrow$ dest $+$ source & dest $\leftarrow$ dest $-$ source \\
\end{tabular}
\end{table}
\begin{itemize}
\item Addressing: \\
\textbf{source}: immediate, register, memory location\\
\textbf{destination}: register or memory location\\
(the destination has to be at least as large as the source)
\item Negate a value: {\tt neg} [op]
\item Bitwise operations: {\tt and}, {\tt or}, {\tt xor}, {\tt not} work similarly
\end{itemize}
\begin{block}{Examples}
\centering
\begin{tabular}{c|c|c}
{\tt add esp, 44h} & {\tt add edx, cx} & {\tt add al, dh} \\[.4em]
{\tt sub esp, 33h} & {\tt sub eax, ebx} & {\tt sub [eax], 1h} \\
\end{tabular}
\end{block}
\end{frame}
\begin{frame}{Integer Arithmetics: unsigned multiply ({\tt mul})}
\begin{itemize}
\item{{\tt mul} \underline{\textbf{source}}}\\
\textbf{source}: register or memory location
\item dest $\leftarrow$ implied\_op $\times$ source
\item \textbf{Implied operands} according to the size of \textbf{source}
\begin{itemize}
\item First operand: {\tt AL}, {\tt AX}, or {\tt EAX}
\item Destination: {\tt AX}, {\tt DX:AX}, {\tt EDX:EAX} (double the size of \textbf{source})
\end{itemize}
\item Signed multiply: {\tt imul}
\end{itemize}
\begin{block}{Example}
\begin{itemize}
\item {\tt mul ebx}: {\tt EDX:EAX $\leftarrow$ EAX * EBX}
\begin{itemize}
\item most significant bits of the result in {\tt EDX}
\item least significant bits of the result in {\tt EAX}
\end{itemize}
\item {\tt mul cx}: {\tt DX:AX $\leftarrow$ AX * CX}
\item {\tt mul cl}: {\tt AX $\leftarrow$ AL * CL}
\end{itemize}
\end{block}
\end{frame}
\begin{frame}{Integer Arithmetics: unsigned divide ({\tt div})}
\begin{itemize}
\item{{\tt div} \underline{\textbf{source}}}\\
\textbf{source}: register or a memory location
\item Computes quotient and remainder
\item Implied operand: {\tt EDX:EAX} (according to the size of \textbf{source})
\item Signed divide: {\tt idiv}
\end{itemize}
\begin{block}{Examples}
\begin{itemize}
\item {\tt div ebx} (4 bytes)
\begin{itemize}
\item {\tt EAX $\leftarrow$ EDX:EAX / EBX}
\item {\tt EDX $\leftarrow$ EDX:EAX \% EBX}
\end{itemize}
\item {\tt div bx} (2 bytes)
\begin{itemize}
\item {\tt AX $\leftarrow$ DX:AX / BX} $\qquad$ {\tt DX = DX:AX \% BX}
\end{itemize}
\item {\tt div bl} (1 byte)
\begin{itemize}
\item {\tt AL $\leftarrow$ AX / BX} $\qquad$ {\tt AH = AX \% BX}
\end{itemize}
\end{itemize}
\end{block}
\end{frame}
\begin{frame}{Integer Arithmetics: {\tt cmp} and {\tt test}}
\begin{table}
\centering
\begin{tabular}{l|l}
{\tt cmp} \underline{\textbf{op1}}, \underline{\textbf{op2}} & {\tt test} \underline{\textbf{op1}}, \underline{\textbf{op2}} \\
Computes op1 $-$ op2 & Computes op1 $\&$ op2 \\
\end{tabular}
\end{table}
\begin{itemize}
\item Sets the flags ({\tt ZF},{\tt CF}, {\tt OF}, \dots)
\item Discards the result
\end{itemize}
\begin{block}{Examples}
\centering
\begin{tabular}{c|c|c}
{\tt cmp eax, ebx} & {\tt cmp eax, 44BBCCDDh} & {\tt cmp al, dh}\\[.4em]
{\tt cmp al, 44h}&{\tt cmp ax,FFFFh} & {\tt cmp [eax],4h}\\
\end{tabular}
\end{block}
\end{frame}
\begin{frame}
\frametitle{Control-Flow Instructions: unconditional jump {\tt jmp}}
\begin{columns}
\begin{column}{0.4\columnwidth}
\begin{figure}
\includegraphics[width=1.1\textwidth]{images/x86-jmp.pdf}
\label{Control Flow JMP}
\end{figure}
\end{column}
\begin{column}{0.6\columnwidth}
\begin{itemize}
\item{{\tt jmp} \underline{\textbf{address}} or \underline{\textbf{offset}}}\\
\item Unconditional jump: just set the {\tt EIP} to \textbf{address}
\item Can be also \emph{relative}: increment or decrement {\tt EIP} by an offset
\end{itemize}
\end{column}
\end{columns}
\end{frame}
\begin{frame}
\frametitle{Control-Flow Instructions: conditional jumps}
{\tt j<cc>} \underline{\textbf{address}} or \underline{\textbf{offset}}
Jump to \textbf{address} if and only if a certain condition is verified
\begin{columns}
\begin{column}{2.5in}
\begin{figure}
\includegraphics[width=\textwidth]{images/x86-jcc.pdf}
\end{figure}
\end{column}
\begin{column}{2.5in}
{\tt <cc>}: condition
\begin{itemize}
\item O,NO,S,NS,E,Z,NE, \dots
\item based on one or more status flags of {\tt EFLAGS}
\end{itemize}
Examples:
\begin{itemize}
\item {\tt jz} = jump if zero
\item {\tt jg} = jump if greater than
\item {\tt jlt} = jump if less than
\end{itemize}
Reference: \url{http://www.unixwiz.net/techtips/x86-jumps.html}
\end{column}
\end{columns}
\end{frame}
\begin{frame}[fragile]{A very simple example (what does it do?)}
Assume that the input is in registers: {\tt ECX} and {\tt EDX}; output: {\tt EAX}
\begin{lstlisting}[language={[x86masm]Assembler}]
mov eax, ecx
mov ebx, edx
cmp ebx, 0
jz label
loop:
cmp ebx, 1
jle ret
mul ecx
sub ebx, 1
jmp loop
label:
mov eax, 1
ret:
...
\end{lstlisting}
\end{frame}
\begin{frame}{Load effective address ({\tt lea})}
\begin{itemize}
\item{{\tt lea} \underline{\textbf{destination}}, \underline{\textbf{source}}}\\
\textbf{source}: memory location\\
\textbf{destination}: register\\
\item{Like a {\tt mov}, but it is storing the pointer, not the value}
\item \alert{It does NOT access memory}
\end{itemize}
\begin{block}{Example}
\par\noindent{}
\centerline{\includegraphics[width=0.7\textwidth]{images/x86-lea}}
\centerline{\tt lea eax, [ebx + 8] $\rightarrow$ \texttt{EAX = 0x00403A48}}
\centerline{\textbf{N.B.}: with {\tt mov eax, [ebx+8]} $\rightarrow$ {\tt EAX = 0x0012C140}}
\end{block}
\end{frame}
\begin{frame}{Basic Instructions: {\tt nop}}
\begin{itemize}
\item{{\tt nop} = \textbf{No Operation}. Just move to next instruction.}
\item{The opcode is pretty famous and is {\tt 0x90}}
\item{Really useful in exploitation (we will see!)}
\end{itemize}
\end{frame}
\begin{frame}{Interrupts and Syscalls}
\begin{itemize}
\item {\tt int} \underline{\textbf{value}}
\begin{itemize}
\item \textbf{value}: software interrupt number to generate (0-255)
\item Every OS has its set of interrupt numbers (e.g., {\tt 80h} for Linux system calls)
\end{itemize}
\item {\tt syscall} used for Linux 64-bit
\item {\tt sysenter} used by Microsoft Windows
\end{itemize}
\end{frame}
\subsection{x86\_64}
\begin{frame}{The x86\_64 ISA}
\centering
\includegraphics[width=1\linewidth]{images/x86-64}
\end{frame}
\begin{frame}
\frametitle{Let's Read the Matrix!}
\centering
\includegraphics[width=0.75\paperwidth]{./images/pwndbg_small.png}
\end{frame}
\begin{frame}
\frametitle{Endianness}
\textbf{Endianness}: convention that specifies in which order the bytes of a data word are lined up sequentially in memory.
\begin{block}{Big-endian (left)}
\begin{columns}
\begin{column}{0.6\textwidth}
Systems in which the \emph{most significant byte} of the word is stored in the \emph{smallest address} given.
\end{column}
\begin{column}{0.25\textwidth}
\includegraphics[width=\textwidth]{images/Big-Endian.pdf}
\end{column}
\end{columns}
\end{block}
\begin{block}{Little-endian}
\begin{columns}
\begin{column}{0.25\columnwidth}
\includegraphics[width=\textwidth]{images/Little-Endian.pdf}
\label{Little-Endian (right)}
\end{column}
\begin{column}{0.6\columnwidth}
Systems in
which the \emph{least significant} byte is stored in the
\emph{smallest address}. \alert{IA-32 is ``little endian''}.
\end{column}
\end{columns}
\end{block}
\end{frame}
%\begin{frame}
% \frametitle{Endianness cont.}
% The \textbf{endianness} is a convention that specifies in which order the bytes of a data word are lined up sequentially in memory.
% \begin{columns}
% \begin{column}{0.5\columnwidth}
% \includegraphics[width=\textwidth]{images/Endianessmap1.pdf}
% \label{Big-Endian}
% \end{column}
% \begin{column}{0.5\columnwidth}
% \includegraphics[width=0.6\textwidth]{images/Endianessmap2.pdf}
% \label{Little-Endian}
% \end{column}
% \end{columns}
%\end{frame}
\section{Program Layout}
\subsection{Memory Layout}
\begin{frame}
\frametitle{Binary File Formats}
\begin{itemize}
\item{\textbf{PE (Portable Executable):} used by Microsoft binary executables}
\item{\textbf{ELF:} common binary format for Unix, Linux, FreeBSD and others}
\item In both cases, we are interested in how each executable is
mapped into memory, rather than how it is organized on disk.
\end{itemize}
\end{frame}
\begin{frame}
\begin{changemargin}{-1cm}{-1cm}
\frametitle{How an executable is mapped to memory in Linux (ELF)}
\begin{table}[l]
\scalebox{0.7}{
\begin{tabular}{ll}
\toprule
\textbf{Executable}&\textbf{Description}\\
\midrule
.plt& This section holds stubs which are responsible of \\
&external functions linking.\\\midrule
.text&This section holds the "text," or executable instructions, of a program.\\\midrule
.rodata& This section holds read-only data that contribute to \\
&the program's memory image\\\midrule
.data& This section holds initialized data that contribute to \\
&the program's memory image\\\midrule
.bss&This section holds uninitialized data that contributes to the program's memory image.\\
&By definition, the system initializes the data with zeros when the program begins to run.\\\midrule
.debug&This section holds information symbolic debugging.\\\midrule
.init&This section holds executable instructions that contribute to the process initialization code.\\
&That is, when a program starts to run, the system arranges to execute the code in this\\
§ion before calling the main program entry point (called main for C programs).\\
\midrule
.got&This section holds the global offset table.\\
\bottomrule
\end{tabular}
}
\end{table}
\end{changemargin}
\end{frame}
\begin{frame}
\begin{changemargin}{-1cm}{-1cm}
\frametitle{How an executable is mapped to memory in Windows (PE)}
\begin{table}[l]
\scalebox{0.9}{
\begin{tabular}{ll}
\toprule
\textbf{Executable}&\textbf{Description}\\\midrule
.text&Contains the executable code\\\midrule
.rdata&Holds read-only data that is globally accessible within \\
&the program\\\midrule
.data&Stores global data accessed throughout the program\\\midrule
.idata&stores the import function information;\\\midrule
.edata&stores the export function information;\\\midrule
.pdata&Present only in 64-bit executables and stores\\
& execption-handling information\\\midrule
.rsrc&Stores resources needed by the executable\\\midrule
.reloc&Contains information for relocation of library files\\\bottomrule
\end{tabular}
}
\end{table}
\end{changemargin}
\end{frame}
\begin{frame}[fragile]{Simplified program memory layout}
\begin{center}
{\footnotesize Low addresses ({\tt 0x80000000})}
\begin{tikzpicture}[scale=0.6, font=\footnotesize]
\cell{Shared libraries}
\cell{{\tt .text}}
\cell{{\tt .bss}}
\cell{Heap (grows $\downarrow$)}
\padding{2}{\dots}
\cell{Stack (grows $\uparrow$)}
\cell{{\tt env}}
\cell{{\tt argv}}
\end{tikzpicture}
{\footnotesize High addresses ({\tt 0xbfffffff})}
\end{center}
\end{frame}
\subsection{The Stack}
\begin{frame}{The Stack}
\begin{itemize}
\item LIFO (last in first out) data structure
\item Used to manage functions
\begin{itemize}
\item local variables
\item return addresses
\item ...
\end{itemize}
\item Handled through the register {\tt ESP} (stack pointer)
\item Remember: the stack grows \textbf{toward lower addresses} (downward the address space)
\end{itemize}
\end{frame}
\begin{frame}[fragile]{Stack Management Instructions: {\tt push}}
\par\noindent{}
\centerline{{\tt push} \underline{{\bf immediate}} or \underline{\bf register}}
\par\noindent Stores the immediate or register value at the top of the stack and decrements the {\tt ESP} of the operand size
\begin{block}{Example}
\begin{columns}
\begin{column}{0.45\textwidth}
\only<-2>{Initial}\only<3->{Final} condition: {\tt EAX} = $30$ \\[.5em]
{\tt push eax} \\[1em]
is equivalent to: \\[1em]
\alert<2>{{\small\tt sub esp, 4} \\}
\alert<3>{{\small\tt mov DWORD PTR [esp], eax}}
\end{column}
\begin{column}{0.4\textwidth}
\centering\par
{\scriptsize Low addresses ({\tt 0x80000000})}\\[.5em]
\begin{drawstack}[scale=0.7]
\cell{}
\only<2->{\bcell{\only<3->{$30$}}\cellptr{{\tt ESP}}}
\only<-1>\cell{}
\bcell{$100$} \only<1>{\cellptr{{\tt ESP}}}
\end{drawstack}
{\scriptsize High addresses ({\tt 0xbfffffff})}
\end{column}
\end{columns}
\end{block}
\end{frame}
\begin{frame}[fragile]{Stack Management Instructions: {\tt pop}}
\par\noindent{}
\centerline{{\tt pop} \underline{{\bf destination}}}
\par\noindent Loads to the destination a word off the top of the stack, then it increases {\tt ESP} of the operand's size.
\begin{block}{Example}
\begin{columns}
\begin{column}{0.45\textwidth}
\only<-2>{Initial}\only<3->{Final} condition: {\tt EAX} = \only<-1>{???}\only<2->{$30$} \\[.5em]
{\tt pop eax} \\[1em]
is equivalent to: \\[1em]
\alert<2>{{\tt mov eax, DWORD PTR [esp]} \\}
\alert<3>{{\tt add esp, 4} \\}
\end{column}
\begin{column}{0.4\textwidth}
\centering\par
{\scriptsize Low addresses ({\tt 0x80000000})}\\[.5em]
\begin{drawstack}[scale=0.7]
\cell{}
\only<-2>{\bcell{$30$}\cellptr{{\tt ESP}}}
\only<3->{\cell{$30$}}
\bcell{$100$}\only<3->{\cellptr{{\tt ESP}}}
\end{drawstack}
{\scriptsize High addresses ({\tt 0xbfffffff})}
\end{column}
\end{columns}
\end{block}
\end{frame}
\section{Functions}
\begin{frame}[fragile]{Calling a function}
Instruction {\tt call}:
\begin{itemize}
\item Push to the stack the address of the next instruction
\item Move the address of the first instruction of the callee into {\tt EIP}
\end{itemize}
\begin{block}{Example: Let's call {\tt func}, located at 0x800bff00}
\vskip 0.4em
\begin{columns}
\begin{column}{0.45\textwidth}
Equivalent to:
\begin{itemize}
\item \alert<2>{\tt push address(of the instruction after the call!)}
\item \alert<3>{\tt jmp func}
\end{itemize}
(reminder: we can't read or set {\tt EIP} directly!)
\end{column}
\begin{column}{0.4\textwidth}
{\scriptsize Low addresses ({\tt 0x80000000})}\\[.5em]
\begin{drawstack}[scale=0.7, font=\footnotesize]
\cell{}
\cell{\only<2->{0x8001025}}\only<2->{\cellptr{\tt ESP}}
\bcell{Stack top}\only<-1>{\cellptr{\tt ESP}}
\end{drawstack}
{\scriptsize High addresses ({\tt 0xbfffffff})}
\only<-2>{EIP = 0x8001020}
\only<3->{EIP = 0x800bff00}
\end{column}
\end{columns}
\end{block}
\end{frame}
\begin{frame}[fragile]{Returning from a function}
Instruction {\tt ret}:
\begin{itemize}
\item Restores the return address saved by {\tt call} from the top of the stack
\end{itemize}
\begin{block}{Example: let's return from {\tt func}}
\vskip 0.4em
\begin{columns}
\begin{column}{0.45\textwidth}
Equivalent to:
\begin{itemize}
\item \alert<2>{\tt pop eip}
\end{itemize}
(reminde: we can't read or set {\tt EIP} directly!)
\end{column}
\begin{column}{0.4\textwidth}
{\scriptsize Low addresses ({\tt 0x80000000})}\\[.5em]
\begin{drawstack}[scale=0.7, font=\footnotesize]
\cell{}
\cell{\only<-1>{0x8001025}}\only<-1>{\cellptr{\tt ESP}}
\bcell{Stack top}\only<2->{\cellptr{\tt ESP}}
\end{drawstack}
{\scriptsize High addresses ({\tt 0xbfffffff})}
\only<2->{EIP = 0x8001025}
\only<-1>{EIP = 0x800bff00}
\end{column}
\end{columns}
\end{block}
\end{frame}
\renewcommand{\finishframe}[1]{
\draw[snake=brace, line width=0.6pt, segment amplitude=7pt]
(-2,\value{cellnb}-0.5) -- (-2,\value{startframe}-0.5);
\draw (-3cm,\value{cellnb}*0.5+\value{startframe}*0.5-0.5) node {#1};
}
\begin{frame}[fragile]{Functions and Stack Frames}
\begin{columns}
\begin{column}{0.45\linewidth}
\begin{itemize}
\item Stack frame = stack area allocated to a function
\item {\tt EBP} register: pointer to the beginning (base) of a function's frame
\item At the beginning of a function:
\begin{itemize}
\item Save EBP to stack
\item Set {\tt EBP} to the address of the function's frame
\end{itemize}
\end{itemize}
\end{column}
\begin{column}{0.4\linewidth}
\par
{\scriptsize Low addresses ({\tt 0x80000000})}\\[.5em]
\begin{tikzpicture}[scale=.7, font=\footnotesize]
\stacktop{}
\startframe
\cell{Local variables}\cellptr{{\tt ESP}}
\cell{Saved {\tt EBP}}\cellptr{{\tt EBP}} \coordinate (p1) at (currentcell.east);
\finishframe{\rotatebox{90}{callee's frame}}
\startframe
\bcell{Return addr (s{\tt EIP})}
\bcell{Arguments}
\bcell{Local variables}
\bcell{Saved {\tt EBP}} \coordinate (p2) at (currentcell.east);
\finishframe{\rotatebox{90}{caller's frame}}
\bcell{Return addr (s{\tt EIP})}
\stackbottom
\draw[->] (p1) to [bend left=90] (p2);
\end{tikzpicture}
{\scriptsize High addresses ({\tt 0xbfffffff})}
\end{column}
\end{columns}
\end{frame}
\begin{frame}[fragile]{Entering a function}
\begin{block}{Example: We've just called {\tt func}, located at 0x800bff00}
\vskip 0.4em
\begin{columns}
\begin{column}{0.35\textwidth}
Setup the stack frame
\begin{itemize}
\item \alert<2>{\tt push ebp}
\item \alert<3>{\tt mov ebp, esp}
\end{itemize}
\end{column}
\begin{column}{0.4\textwidth}
{\scriptsize Low addresses ({\tt 0x80000000})}\\[.5em]
\begin{drawstack}[scale=0.7, font=\footnotesize]
\startframe
\cell{\only<2->{Saved EBP}}\only<2->{\cellptr{\tt ESP}}\only<3->{\cellptr{\tt EBP}}
\cell{0x8001025}\only<1>{\cellptr{\tt ESP}}
\finishframe{\rotatebox{90}{{\tt func}'s frame}}
\bcell{\dots}
\bcell{caller's sEBP}\only<-2>{\cellptr{\tt EBP}}
\end{drawstack}
{\scriptsize High addresses ({\tt 0xbfffffff})}
\end{column}
\end{columns}
\end{block}
\end{frame}
\begin{frame}[fragile]{Leaving a function}
Instruction {\tt leave}: restores the caller's base pointer
\begin{block}{Example: We're about to return from {\tt func}}
\vskip 0.4em
\begin{columns}
\begin{column}{0.35\textwidth}
Equivalent to:
\begin{itemize}
\item \alert<2>{\tt mov esp, ebp}
\item \alert<3>{\tt pop ebp}
\end{itemize}
\end{column}
\begin{column}{0.4\textwidth}
{\scriptsize Low addresses ({\tt 0x80000000})}\\[.5em]
\begin{drawstack}[scale=0.7, font=\footnotesize]
\startframe
\cell{(... locals ...)}\only<1>{\cellptr{\tt ESP}}
\cell{{Saved EBP}}\only<-2>{\cellptr{\tt EBP}}\only<2>{\cellptr{\tt ESP}}
\cell{0x8001025}\only<3->{\cellptr{\tt ESP}}
\finishframe{\rotatebox{90}{{\tt func}'s frame}}
\bcell{\dots}
\bcell{caller's sEBP}\only<3>{\cellptr{\tt EBP}}
\end{drawstack}
{\scriptsize High addresses ({\tt 0xbfffffff})}
\end{column}
\end{columns}
\end{block}
\end{frame}
\subsection{Calling Conventions}
\begin{frame}{Calling Conventions}
\begin{itemize}
\item Defines
\begin{itemize}
\item how to pass parameters (stack, registers or both, and who is responsible to clean them up)
\item how to return values
\item caller-saved or callee-saved registers
\end{itemize}
\item The high-level language, the compiler, the OS, and the target architecture all together ``implement'' and ``agree upon'' a certain calling convention
\begin{itemize}
\item it's part of the \alert{ABI}, the Application Binary Interface
\end{itemize}
\end{itemize}
\end{frame}
\begin{frame}{Calling Conventions: {\tt cdecl} (C declaration)}
\begin{itemize}
\item Default calling convention used by most x86 C compilers
\begin{itemize}
\item Can be forced with the modifier \alert{\tt \_cdecl}
\end{itemize}
\item Arguments: passed \textbf{through the stack}, right to left order
\item Cleanup: the \textbf{caller removes} the parameters from the stack \emph{after} the called function completes
\item Return: register {\tt RAX}
\item Caller-saved registers: {\tt EAX}, {\tt ECX}, {\tt EDX} (other are callee-saved)
\end{itemize}
\end{frame}
\begin{frame}[fragile]
\frametitle{{\tt cdecl}: Example}
\begin{lstlisting}[language=C]
void demo_cdecl(int a, int b, int c, int z);
//...
demo_cdecl(1, 2, 3, 4); //calling
\end{lstlisting}
\begin{lstlisting}[language={[x86masm]Assembler}]
; ...
push 4 ; push last parameter value
push 3 ; push third parameter value
push 2 ; ...
push 1
call demo_cdecl ; call the subroutine
add esp, 16 ; clean up the stack
\end{lstlisting}