author | Christian Urban <christian dot urban at kcl dot ac dot uk> |
Mon, 22 Feb 2016 22:09:44 +0000 | |
changeset 451 | b6eb8142dec1 |
parent 443 | 67d7d239c617 |
child 452 | 3447cc029e2f |
permissions | -rw-r--r-- |
156
3b831b9dc616
added some initial handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
1 |
\documentclass{article} |
3b831b9dc616
added some initial handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
2 |
\usepackage{../style} |
206
0105257429f3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
3 |
\usepackage{../langs} |
258
8cf5450bb2aa
added plot
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
257
diff
changeset
|
4 |
\usepackage{../graphics} |
8cf5450bb2aa
added plot
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
257
diff
changeset
|
5 |
\usepackage{../data} |
156
3b831b9dc616
added some initial handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
6 |
|
3b831b9dc616
added some initial handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
7 |
\begin{document} |
390
92a8dad2cc86
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
381
diff
changeset
|
8 |
\fnote{\copyright{} Christian Urban, 2014, 2015} |
156
3b831b9dc616
added some initial handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
9 |
|
3b831b9dc616
added some initial handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
10 |
\section*{Handout 3 (Buffer Overflow Attacks)} |
3b831b9dc616
added some initial handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
11 |
|
3b831b9dc616
added some initial handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
12 |
By far the most popular attack method on computers are buffer |
237
b784175a69dc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
13 |
overflow attacks or variations thereof. The first Internet |
b784175a69dc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
14 |
worm (Morris) exploited exactly such an attack. The popularity |
b784175a69dc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
15 |
is unfortunate because we nowadays have technology in place to |
229
ea921d6a1819
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
16 |
prevent them effectively. But these kind of attacks are still |
ea921d6a1819
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
17 |
very relevant even today since there are many legacy systems |
ea921d6a1819
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
18 |
out there and also many modern embedded systems often do not |
391
a612dd3ddc81
updated slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
390
diff
changeset
|
19 |
take any precautions to prevent such attacks. The plot below |
a612dd3ddc81
updated slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
390
diff
changeset
|
20 |
shows the percentage of buffer overflow attacks listed in the |
a612dd3ddc81
updated slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
390
diff
changeset
|
21 |
US National Vulnerability Database.\footnote{Search for |
a612dd3ddc81
updated slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
390
diff
changeset
|
22 |
``Buffer errors'' at |
259
f96d3e48ed3d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
258
diff
changeset
|
23 |
\url{http://web.nvd.nist.gov/view/vuln/statistics}.} |
258
8cf5450bb2aa
added plot
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
257
diff
changeset
|
24 |
|
8cf5450bb2aa
added plot
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
257
diff
changeset
|
25 |
\begin{center} |
8cf5450bb2aa
added plot
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
257
diff
changeset
|
26 |
\begin{tikzpicture} |
8cf5450bb2aa
added plot
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
257
diff
changeset
|
27 |
\begin{axis}[ |
8cf5450bb2aa
added plot
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
257
diff
changeset
|
28 |
xlabel={year}, |
8cf5450bb2aa
added plot
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
257
diff
changeset
|
29 |
ylabel={\% of total attacks}, |
259
f96d3e48ed3d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
258
diff
changeset
|
30 |
ylabel style={yshift=-1em}, |
258
8cf5450bb2aa
added plot
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
257
diff
changeset
|
31 |
enlargelimits=false, |
381
036a762b02cf
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
377
diff
changeset
|
32 |
xtick={1997,1999,2001,...,2015}, |
259
f96d3e48ed3d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
258
diff
changeset
|
33 |
xmin=1996.5, |
381
036a762b02cf
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
377
diff
changeset
|
34 |
xmax=2016, |
259
f96d3e48ed3d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
258
diff
changeset
|
35 |
ymax=21, |
263
8a42736cce27
updated 5th handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
259
diff
changeset
|
36 |
ytick={0,5,...,20}, |
258
8cf5450bb2aa
added plot
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
257
diff
changeset
|
37 |
scaled ticks=false, |
8cf5450bb2aa
added plot
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
257
diff
changeset
|
38 |
axis lines=left, |
259
f96d3e48ed3d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
258
diff
changeset
|
39 |
width=12cm, |
258
8cf5450bb2aa
added plot
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
257
diff
changeset
|
40 |
height=5cm, |
8cf5450bb2aa
added plot
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
257
diff
changeset
|
41 |
ybar, |
259
f96d3e48ed3d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
258
diff
changeset
|
42 |
nodes near coords= |
f96d3e48ed3d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
258
diff
changeset
|
43 |
{\footnotesize |
f96d3e48ed3d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
258
diff
changeset
|
44 |
$\pgfmathprintnumber[fixed,fixed zerofill,precision=1,use comma]{\pgfkeysvalueof{/data point/y}}$}, |
f96d3e48ed3d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
258
diff
changeset
|
45 |
x tick label style={font=\footnotesize,/pgf/number format/1000 sep={}}] |
258
8cf5450bb2aa
added plot
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
257
diff
changeset
|
46 |
\addplot |
8cf5450bb2aa
added plot
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
257
diff
changeset
|
47 |
table [x=Year,y=Percentage] {bufferoverflows.data}; |
8cf5450bb2aa
added plot
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
257
diff
changeset
|
48 |
\end{axis} |
8cf5450bb2aa
added plot
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
257
diff
changeset
|
49 |
\end{tikzpicture} |
8cf5450bb2aa
added plot
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
257
diff
changeset
|
50 |
\end{center} |
8cf5450bb2aa
added plot
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
257
diff
changeset
|
51 |
|
451
b6eb8142dec1
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
443
diff
changeset
|
52 |
\noindent This statistics shows that in the last seven years or so the |
b6eb8142dec1
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
443
diff
changeset
|
53 |
number of buffer overflow attacks is around 10\% of all attacks |
b6eb8142dec1
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
443
diff
changeset
|
54 |
(whereby the absolute numbers of attacks grow each year). So you can |
b6eb8142dec1
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
443
diff
changeset
|
55 |
see buffer overflow attacks are very relevant today. For example, very |
b6eb8142dec1
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
443
diff
changeset
|
56 |
recently (February 2016) a buffer overflow attack was discovered in the glibc |
b6eb8142dec1
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
443
diff
changeset
|
57 |
library:\footnote{\url{http://arstechnica.com/security/2016/02/extremely-severe-bug-leaves-dizzying-number-of-apps-and-devices-vulnerable/}} |
b6eb8142dec1
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
443
diff
changeset
|
58 |
|
b6eb8142dec1
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
443
diff
changeset
|
59 |
\begin{quote}\it |
b6eb8142dec1
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
443
diff
changeset
|
60 |
``Since 2008, vulnerability has left apps and hardware open to remote |
b6eb8142dec1
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
443
diff
changeset
|
61 |
hijacking. Researchers have discovered a potentially catastrophic flaw in |
b6eb8142dec1
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
443
diff
changeset
|
62 |
one of the Internet's core building blocks that leaves hundreds or |
b6eb8142dec1
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
443
diff
changeset
|
63 |
thousands of apps and hardware devices vulnerable to attacks that can take |
b6eb8142dec1
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
443
diff
changeset
|
64 |
complete control over them. The vulnerability was introduced in 2008 in |
b6eb8142dec1
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
443
diff
changeset
|
65 |
GNU C Library, a collection of open source code that powers thousands of |
b6eb8142dec1
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
443
diff
changeset
|
66 |
standalone applications and most distributions of Linux, including those |
b6eb8142dec1
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
443
diff
changeset
|
67 |
distributed with routers and other types of hardware. A function known as |
b6eb8142dec1
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
443
diff
changeset
|
68 |
getaddrinfo() that performs domain-name lookups contains a buffer overflow |
b6eb8142dec1
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
443
diff
changeset
|
69 |
bug that allows attackers to remotely execute malicious code. It can be |
b6eb8142dec1
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
443
diff
changeset
|
70 |
exploited when vulnerable devices or apps make queries to |
b6eb8142dec1
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
443
diff
changeset
|
71 |
attacker-controlled domain names or domain name servers or when they're |
b6eb8142dec1
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
443
diff
changeset
|
72 |
exposed to man-in-the-middle attacks where the adversary has the ability |
b6eb8142dec1
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
443
diff
changeset
|
73 |
to monitor and manipulate data passing between a vulnerable device and the |
b6eb8142dec1
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
443
diff
changeset
|
74 |
open Internet. All versions of glibc after 2.9 are vulnerable.'' |
b6eb8142dec1
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
443
diff
changeset
|
75 |
\end{quote} |
259
f96d3e48ed3d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
258
diff
changeset
|
76 |
|
156
3b831b9dc616
added some initial handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
77 |
|
211
e6e160c7ea33
added files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
209
diff
changeset
|
78 |
To understand how buffer overflow attacks work, we have to have |
156
3b831b9dc616
added some initial handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
79 |
a look at how computers work ``under the hood'' (on the |
3b831b9dc616
added some initial handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
80 |
machine level) and also understand some aspects of the C/C++ |
3b831b9dc616
added some initial handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
81 |
programming language. This might not be everyday fare for |
3b831b9dc616
added some initial handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
82 |
computer science students, but who said that criminal hackers |
374
7d8ad963ad24
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
366
diff
changeset
|
83 |
restrict themselves to everyday fare? ...not to mention the |
156
3b831b9dc616
added some initial handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
84 |
free-riding script-kiddies who use this technology without |
227
7807863c4196
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
218
diff
changeset
|
85 |
even knowing what the underlying ideas are. If you want to be |
374
7d8ad963ad24
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
366
diff
changeset
|
86 |
a good security engineer who needs to defend against such attacks, |
7d8ad963ad24
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
366
diff
changeset
|
87 |
then better you get to know the details too. |
156
3b831b9dc616
added some initial handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
88 |
|
206
0105257429f3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
89 |
For buffer overflow attacks to work, a number of innocent |
395
60f64793266f
added assembly programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
391
diff
changeset
|
90 |
design decisions, which are really benign on their own, have |
60f64793266f
added assembly programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
391
diff
changeset
|
91 |
to come together. All these decisions were taken at a time |
60f64793266f
added assembly programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
391
diff
changeset
|
92 |
when there was no Internet: C was introduced around 1973; the |
60f64793266f
added assembly programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
391
diff
changeset
|
93 |
Internet TCP/IP protocol was standardised in 1982 by which |
229
ea921d6a1819
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
94 |
time there were maybe 500 servers connected (and all users |
ea921d6a1819
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
95 |
were well-behaved, mostly academics); Intel's first 8086 CPUs |
ea921d6a1819
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
96 |
arrived around 1977. So nobody of the ``forefathers'' can |
ea921d6a1819
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
97 |
really be blamed, but as mentioned above we should already be |
ea921d6a1819
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
98 |
way beyond the point that buffer overflow attacks are worth a |
443
67d7d239c617
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
403
diff
changeset
|
99 |
thought. Unfortunately, this is far from the reality. I let you |
229
ea921d6a1819
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
100 |
ponder why? |
206
0105257429f3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
101 |
|
0105257429f3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
102 |
One such ``benign'' design decision is how the memory is laid |
0105257429f3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
103 |
out into different regions for each process. |
204
8fe0dc898c73
added example1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
196
diff
changeset
|
104 |
|
206
0105257429f3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
105 |
\begin{center} |
0105257429f3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
106 |
\begin{tikzpicture}[scale=0.7] |
0105257429f3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
107 |
%\draw[step=1cm] (-3,-3) grid (3,3); |
0105257429f3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
108 |
\draw[line width=1mm] (-2, -3) rectangle (2,3); |
0105257429f3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
109 |
\draw[line width=1mm] (-2,1) -- (2,1); |
0105257429f3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
110 |
\draw[line width=1mm] (-2,-1) -- (2,-1); |
0105257429f3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
111 |
\draw (0,2) node {\large\tt text}; |
0105257429f3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
112 |
\draw (0,0) node {\large\tt heap}; |
0105257429f3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
113 |
\draw (0,-2) node {\large\tt stack}; |
0105257429f3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
114 |
|
0105257429f3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
115 |
\draw (-2.7,3) node[anchor=north east] {\tt\begin{tabular}{@{}l@{}}lower\\ address\end{tabular}}; |
0105257429f3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
116 |
\draw (-2.7,-3) node[anchor=south east] {\tt\begin{tabular}{@{}l@{}}higher\\ address\end{tabular}}; |
0105257429f3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
117 |
\draw[->, line width=1mm] (-2.5,3) -- (-2.5,-3); |
0105257429f3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
118 |
|
0105257429f3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
119 |
\draw (2.7,-2) node[anchor=west] {\tt grows}; |
0105257429f3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
120 |
\draw (2.7,-3) node[anchor=south west] {\tt\footnotesize older}; |
0105257429f3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
121 |
\draw (2.7,-1) node[anchor=north west] {\tt\footnotesize newer}; |
0105257429f3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
122 |
\draw[|->, line width=1mm] (2.5,-3) -- (2.5,-1); |
0105257429f3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
123 |
\end{tikzpicture} |
0105257429f3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
124 |
\end{center} |
0105257429f3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
125 |
|
0105257429f3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
126 |
\noindent The text region contains the program code (usually |
0105257429f3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
127 |
this region is read-only). The heap stores all data the |
0105257429f3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
128 |
programmer explicitly allocates. For us the most interesting |
0105257429f3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
129 |
region is the stack, which contains data mostly associated |
227
7807863c4196
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
218
diff
changeset
|
130 |
with the control flow of the program. Notice that the stack |
230
603cbd28e988
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
131 |
grows from higher addresses to lower addresses (i.e.~from the |
603cbd28e988
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
132 |
back to the front). That means that older items on the stack |
395
60f64793266f
added assembly programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
391
diff
changeset
|
133 |
are stored behind, or after, newer items. Let's look a bit |
230
603cbd28e988
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
134 |
closer what happens with the stack when a program is running. |
603cbd28e988
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
135 |
Consider the following simple C program. |
206
0105257429f3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
136 |
|
0105257429f3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
137 |
\lstinputlisting[language=C]{../progs/example1.c} |
0105257429f3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
138 |
|
229
ea921d6a1819
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
139 |
\noindent The \code{main} function calls in Line 7 the |
ea921d6a1819
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
140 |
function \code{foo} with three arguments. \code{Foo} creates |
ea921d6a1819
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
141 |
two (local) buffers, but does not do anything interesting with |
ea921d6a1819
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
142 |
them. The only purpose of this program is to illustrate what |
ea921d6a1819
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
143 |
happens behind the scenes with the stack. The interesting |
395
60f64793266f
added assembly programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
391
diff
changeset
|
144 |
question is what will the stack look like after Line 3 has |
397
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
145 |
been executed? The answer is illustrated in Figure~\ref{stack}. |
206
0105257429f3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
146 |
|
397
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
147 |
\begin{figure} |
206
0105257429f3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
148 |
\begin{center} |
0105257429f3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
149 |
\begin{tikzpicture}[scale=0.65] |
0105257429f3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
150 |
\draw[gray!20,fill=gray!20] (-5, 0) rectangle (-3,-1); |
0105257429f3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
151 |
\draw[line width=1mm] (-5,-1.2) -- (-5,0.2); |
0105257429f3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
152 |
\draw[line width=1mm] (-3,-1.2) -- (-3,0.2); |
0105257429f3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
153 |
\draw (-4,-1) node[anchor=south] {\tt main}; |
0105257429f3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
154 |
\draw[line width=1mm] (-5,0) -- (-3,0); |
0105257429f3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
155 |
|
0105257429f3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
156 |
\draw[gray!20,fill=gray!20] (3, 0) rectangle (5,-1); |
0105257429f3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
157 |
\draw[line width=1mm] (3,-1.2) -- (3,0.2); |
0105257429f3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
158 |
\draw[line width=1mm] (5,-1.2) -- (5,0.2); |
0105257429f3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
159 |
\draw (4,-1) node[anchor=south] {\tt main}; |
0105257429f3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
160 |
\draw[line width=1mm] (3,0) -- (5,0); |
0105257429f3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
161 |
|
0105257429f3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
162 |
%\draw[step=1cm] (-3,-1) grid (3,8); |
0105257429f3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
163 |
\draw[gray!20,fill=gray!20] (-1, 0) rectangle (1,-1); |
0105257429f3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
164 |
\draw[line width=1mm] (-1,-1.2) -- (-1,7.4); |
0105257429f3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
165 |
\draw[line width=1mm] ( 1,-1.2) -- ( 1,7.4); |
0105257429f3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
166 |
\draw (0,-1) node[anchor=south] {\tt main}; |
0105257429f3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
167 |
\draw[line width=1mm] (-1,0) -- (1,0); |
0105257429f3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
168 |
\draw (0,0) node[anchor=south] {\tt arg$_3$=3}; |
0105257429f3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
169 |
\draw[line width=1mm] (-1,1) -- (1,1); |
0105257429f3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
170 |
\draw (0,1) node[anchor=south] {\tt arg$_2$=2}; |
0105257429f3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
171 |
\draw[line width=1mm] (-1,2) -- (1,2); |
0105257429f3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
172 |
\draw (0,2) node[anchor=south] {\tt arg$_1$=1}; |
0105257429f3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
173 |
\draw[line width=1mm] (-1,3) -- (1,3); |
0105257429f3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
174 |
\draw (0,3.1) node[anchor=south] {\tt ret}; |
0105257429f3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
175 |
\draw[line width=1mm] (-1,4) -- (1,4); |
0105257429f3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
176 |
\draw (0,4) node[anchor=south] {\small\tt last sp}; |
0105257429f3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
177 |
\draw[line width=1mm] (-1,5) -- (1,5); |
0105257429f3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
178 |
\draw (0,5) node[anchor=south] {\tt buf$_1$}; |
0105257429f3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
179 |
\draw[line width=1mm] (-1,6) -- (1,6); |
0105257429f3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
180 |
\draw (0,6) node[anchor=south] {\tt buf$_2$}; |
0105257429f3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
181 |
\draw[line width=1mm] (-1,7) -- (1,7); |
0105257429f3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
182 |
|
0105257429f3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
183 |
\draw[->,line width=0.5mm] (1,4.5) -- (1.8,4.5) -- (1.8, 0) -- (1.1,0); |
0105257429f3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
184 |
\draw[->,line width=0.5mm] (1,3.5) -- (2.5,3.5); |
0105257429f3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
185 |
\draw (2.6,3.1) node[anchor=south west] {\tt back to main()}; |
0105257429f3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
186 |
\end{tikzpicture} |
397
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
187 |
\end{center} |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
188 |
\caption{The stack layout for a program where the main |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
189 |
function calls an auxiliary function with three arguments |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
190 |
(1,2 and 3). The auxiliary function has two local |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
191 |
buffer variables {\tt buf}$_1$ and {\tt buf}$_2$.\label{stack}} |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
192 |
\end{figure} |
206
0105257429f3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
193 |
|
397
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
194 |
On the left is the stack before \code{foo} is called; on the |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
195 |
right is the stack after \code{foo} finishes. The function |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
196 |
call to \code{foo} in Line 7 (in the C program above) pushes |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
197 |
the arguments onto the stack in reverse order---shown in the |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
198 |
middle. Therefore first 3 then 2 and finally 1. Then it pushes |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
199 |
the return address onto the stack where execution should |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
200 |
resume once \code{foo} has finished. The last stack pointer |
229
ea921d6a1819
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
201 |
(\code{sp}) is needed in order to clean up the stack to the |
ea921d6a1819
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
202 |
last level---in fact there is no cleaning involved, but just |
263
8a42736cce27
updated 5th handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
259
diff
changeset
|
203 |
the top of the stack will be set back to this address. So the |
8a42736cce27
updated 5th handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
259
diff
changeset
|
204 |
last stack pointer also needs to be stored. The two buffers |
8a42736cce27
updated 5th handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
259
diff
changeset
|
205 |
inside \pcode{foo} are on the stack too, because they are |
8a42736cce27
updated 5th handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
259
diff
changeset
|
206 |
local data within \code{foo}. Consequently the stack in the |
397
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
207 |
middle of Figure~\ref{stack} is a snapshot after Line 3 has |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
208 |
been executed. |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
209 |
|
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
210 |
In case you are familiar with assembly instructions you can |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
211 |
also read off this behaviour from the machine code that the |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
212 |
\code{gcc} compiler generates for the program |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
213 |
above:\footnote{You can make \pcode{gcc} generate assembly |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
214 |
instructions if you call it with the \pcode{-S} option, for |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
215 |
example \pcode{gcc -S out in.c}\;. Or you can look at this |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
216 |
code by using the debugger. How to do this will be explained |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
217 |
in the last section.} It generates the following code for the |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
218 |
\pcode{main} and \pcode{foo} functions. |
206
0105257429f3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
219 |
|
227
7807863c4196
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
218
diff
changeset
|
220 |
\begin{center}\small |
396
2f4296a0ab21
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
395
diff
changeset
|
221 |
\begin{tabular}[t]{p{11cm}} |
227
7807863c4196
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
218
diff
changeset
|
222 |
{\lstinputlisting[language={[x86masm]Assembler}, |
7807863c4196
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
218
diff
changeset
|
223 |
morekeywords={movl},xleftmargin=5mm] |
395
60f64793266f
added assembly programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
391
diff
changeset
|
224 |
{../progs/example1a.s}} |
60f64793266f
added assembly programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
391
diff
changeset
|
225 |
\end{tabular} |
60f64793266f
added assembly programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
391
diff
changeset
|
226 |
\end{center} |
397
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
227 |
|
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
228 |
\noindent Again you can see how the function \pcode{main} |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
229 |
prepares in Lines 2 to 7 the stack before calling the function |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
230 |
\pcode{foo}. You can see that the numbers 3, 2, 1 are stored |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
231 |
on the stack (the register \code{\%esp} refers to the top of |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
232 |
the stack; \pcode{$0x1}, \pcode{$0x2} \pcode{$0x3} are the |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
233 |
hexadecimal encodings for \pcode{1} to \pcode{3}). The code |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
234 |
for the foo function is as follows: |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
235 |
|
395
60f64793266f
added assembly programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
391
diff
changeset
|
236 |
\begin{center}\small |
396
2f4296a0ab21
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
395
diff
changeset
|
237 |
\begin{tabular}[t]{p{11cm}} |
227
7807863c4196
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
218
diff
changeset
|
238 |
{\lstinputlisting[language={[x86masm]Assembler}, |
7807863c4196
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
218
diff
changeset
|
239 |
morekeywords={movl,movw},xleftmargin=5mm] |
7807863c4196
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
218
diff
changeset
|
240 |
{../progs/example1b.s}} |
7807863c4196
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
218
diff
changeset
|
241 |
\end{tabular} |
7807863c4196
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
218
diff
changeset
|
242 |
\end{center} |
7807863c4196
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
218
diff
changeset
|
243 |
|
397
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
244 |
\noindent You can see how the function \pcode{foo} stores |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
245 |
first the last stack pointer onto the stack and then |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
246 |
calculates the new stack pointer to have enough space for the |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
247 |
two local buffers (Lines 2 - 4). Then it puts the two local |
263
8a42736cce27
updated 5th handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
259
diff
changeset
|
248 |
buffers onto the stack and initialises them with the given |
397
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
249 |
data (Lines 5 to 9). Since there is no real computation going |
263
8a42736cce27
updated 5th handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
259
diff
changeset
|
250 |
on inside \pcode{foo}, the function then just restores the |
397
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
251 |
stack to its old state (Line 10) and crucially sets the return |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
252 |
address where the computation should resume (Line 10). The |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
253 |
instruction \code{ret} then transfers control back to the |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
254 |
function \pcode{main} to the instruction just after the call |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
255 |
to \pcode{foo}, that is Line 10. |
206
0105257429f3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
256 |
|
230
603cbd28e988
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
257 |
Another part of the ``conspiracy'' of buffer overflow attacks |
603cbd28e988
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
258 |
is that library functions in C look typically as follows: |
206
0105257429f3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
259 |
|
0105257429f3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
260 |
\begin{center} |
0105257429f3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
261 |
\lstinputlisting[language=C,numbers=none]{../progs/app5.c} |
0105257429f3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
262 |
\end{center} |
0105257429f3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
263 |
|
0105257429f3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
264 |
\noindent This function copies data from a source \pcode{src} |
209
fd43a9cd9c07
updates
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
206
diff
changeset
|
265 |
to a destination \pcode{dst}. The important point is that it |
fd43a9cd9c07
updates
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
206
diff
changeset
|
266 |
copies the data until it reaches a zero-byte (\code{"\\0"}). |
230
603cbd28e988
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
267 |
This is a convention of the C language which assumes all |
603cbd28e988
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
268 |
strings are terminated by such a zero-byte. |
206
0105257429f3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
269 |
|
227
7807863c4196
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
218
diff
changeset
|
270 |
The central idea of the buffer overflow attack is to overwrite |
230
603cbd28e988
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
271 |
the return address on the stack. This address decides where |
603cbd28e988
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
272 |
the control flow of the program should resume once the |
603cbd28e988
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
273 |
function at hand has finished its computation. So if we |
603cbd28e988
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
274 |
can control this address, then we can modify the control |
603cbd28e988
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
275 |
flow of a program. To launch an attack we need |
603cbd28e988
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
276 |
somewhere in a function a local a buffer, say |
227
7807863c4196
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
218
diff
changeset
|
277 |
|
7807863c4196
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
218
diff
changeset
|
278 |
\begin{center} |
7807863c4196
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
218
diff
changeset
|
279 |
\code{char buf[8];} |
7807863c4196
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
218
diff
changeset
|
280 |
\end{center} |
7807863c4196
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
218
diff
changeset
|
281 |
|
230
603cbd28e988
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
282 |
\noindent which is filled by some user input. The |
603cbd28e988
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
283 |
corresponding stack of such a function will look as follows |
227
7807863c4196
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
218
diff
changeset
|
284 |
|
7807863c4196
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
218
diff
changeset
|
285 |
\begin{center} |
7807863c4196
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
218
diff
changeset
|
286 |
\begin{tikzpicture}[scale=0.65] |
7807863c4196
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
218
diff
changeset
|
287 |
%\draw[step=1cm] (-3,-1) grid (3,8); |
397
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
288 |
\draw[line width=1mm] (-1,1.2) -- (-1,6.4); |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
289 |
\draw[line width=1mm] ( 1,1.2) -- ( 1,6.4); |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
290 |
\draw (0,2) node[anchor=south] {\ldots}; |
227
7807863c4196
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
218
diff
changeset
|
291 |
\draw[line width=1mm] (-1,3) -- (1,3); |
7807863c4196
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
218
diff
changeset
|
292 |
\draw (0,3.1) node[anchor=south] {\tt ret}; |
7807863c4196
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
218
diff
changeset
|
293 |
\draw[line width=1mm] (-1,4) -- (1,4); |
7807863c4196
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
218
diff
changeset
|
294 |
\draw (0,4) node[anchor=south] {\small\tt last sp}; |
7807863c4196
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
218
diff
changeset
|
295 |
\draw[line width=1mm] (-1,5) -- (1,5); |
229
ea921d6a1819
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
296 |
\draw (0,5.1) node[anchor=south] {\tt buf}; |
227
7807863c4196
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
218
diff
changeset
|
297 |
\draw[line width=1mm] (-1,6) -- (1,6); |
7807863c4196
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
218
diff
changeset
|
298 |
\draw (2,5.1) node[anchor=south] {\code{$esp}}; |
7807863c4196
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
218
diff
changeset
|
299 |
\draw[<-,line width=0.5mm] (1.1,6) -- (2.5,6); |
7807863c4196
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
218
diff
changeset
|
300 |
|
7807863c4196
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
218
diff
changeset
|
301 |
\draw[->,line width=0.5mm] (1,4.5) -- (2.5,4.5); |
7807863c4196
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
218
diff
changeset
|
302 |
\draw (2.6,4.1) node[anchor=south west] {\code{??}}; |
7807863c4196
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
218
diff
changeset
|
303 |
|
7807863c4196
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
218
diff
changeset
|
304 |
\draw[->,line width=0.5mm] (1,3.5) -- (2.5,3.5); |
7807863c4196
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
218
diff
changeset
|
305 |
\draw (2.6,3.1) node[anchor=south west] {\tt jump to \code{\\x080483f4}}; |
7807863c4196
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
218
diff
changeset
|
306 |
\end{tikzpicture} |
7807863c4196
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
218
diff
changeset
|
307 |
\end{center} |
7807863c4196
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
218
diff
changeset
|
308 |
|
229
ea921d6a1819
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
309 |
\noindent We need to fill this buffer over its limit of 8 |
ea921d6a1819
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
310 |
characters so that it overwrites the stack pointer and then |
ea921d6a1819
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
311 |
also overwrites the return address. If, for example, we want |
ea921d6a1819
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
312 |
to jump to a specific address in memory, say, |
ea921d6a1819
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
313 |
\pcode{\\x080483f4} then we can fill the buffer with the data |
227
7807863c4196
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
218
diff
changeset
|
314 |
|
7807863c4196
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
218
diff
changeset
|
315 |
\begin{center} |
7807863c4196
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
218
diff
changeset
|
316 |
\code{char buf[8] = "AAAAAAAABBBB\\xf4\\x83\\x04\\x08";} |
7807863c4196
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
218
diff
changeset
|
317 |
\end{center} |
7807863c4196
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
218
diff
changeset
|
318 |
|
229
ea921d6a1819
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
319 |
\noindent The first eight \pcode{A}s fill the buffer to the |
ea921d6a1819
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
320 |
rim; the next four \pcode{B}s overwrite the stack pointer |
ea921d6a1819
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
321 |
(with what data we overwrite this part is usually not |
ea921d6a1819
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
322 |
important); then comes the address we want to jump to. Notice |
ea921d6a1819
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
323 |
that we have to give the address in the reverse order. All |
ea921d6a1819
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
324 |
addresses on Intel CPUs need to be given in this way. Since |
ea921d6a1819
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
325 |
the string is enclosed in double quotes, the C convention is |
ea921d6a1819
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
326 |
that the string internally will automatically be terminated by |
ea921d6a1819
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
327 |
a zero-byte. If the programmer uses functions like |
ea921d6a1819
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
328 |
\pcode{strcpy} for filling the buffer \pcode{buf}, then we can |
ea921d6a1819
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
329 |
be sure it will overwrite the stack in this manner---since it |
ea921d6a1819
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
330 |
will copy everything up to the zero-byte. Notice that this |
397
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
331 |
overwriting of the buffer only works since the newer |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
332 |
item---the buffer---is stored on the stack before the older |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
333 |
items, like return address and arguments. If it had be the |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
334 |
other way around, then such an overwriting by overflowing a |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
335 |
local buffer would just not work. Had the designers of C |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
336 |
been able to foresee what headaches their way of |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
337 |
arranging the stack will cause, how different could be |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
338 |
the IT-World today? |
227
7807863c4196
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
218
diff
changeset
|
339 |
|
7807863c4196
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
218
diff
changeset
|
340 |
What the outcome of such an attack is can be illustrated with |
7807863c4196
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
218
diff
changeset
|
341 |
the code shown in Figure~\ref{C2}. Under ``normal operation'' |
229
ea921d6a1819
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
342 |
this program ask for a login-name and a password. Both of |
ea921d6a1819
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
343 |
which are stored in \code{char} buffers of length 8. The |
ea921d6a1819
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
344 |
function \pcode{match} tests whether two such buffers contain |
230
603cbd28e988
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
345 |
the same content. If yes, then the function lets you ``in'' |
603cbd28e988
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
346 |
(by printing \pcode{Welcome}). If not, it denies access (by |
229
ea921d6a1819
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
347 |
printing \pcode{Wrong identity}). The vulnerable function is |
ea921d6a1819
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
348 |
\code{get_line} in Lines 11 to 19. This function does not take |
ea921d6a1819
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
349 |
any precautions about the buffer of 8 characters being filled |
397
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
350 |
beyond its 8-character-limit. Let us suppose the login name is |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
351 |
\pcode{test}. Then the buffer overflow can be triggered with a |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
352 |
specially crafted string as password (remember |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
353 |
\pcode{get\_line} requires a \pcode{\\n} at the end of the |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
354 |
input): |
227
7807863c4196
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
218
diff
changeset
|
355 |
|
7807863c4196
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
218
diff
changeset
|
356 |
\begin{center} |
7807863c4196
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
218
diff
changeset
|
357 |
\code{AAAAAAAABBBB\\x2c\\x85\\x04\\x08\\n} |
7807863c4196
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
218
diff
changeset
|
358 |
\end{center} |
7807863c4196
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
218
diff
changeset
|
359 |
|
229
ea921d6a1819
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
360 |
\noindent The address at the end happens to be the one for the |
ea921d6a1819
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
361 |
function \pcode{welcome()}. This means even with this input |
ea921d6a1819
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
362 |
(where the login name and password clearly do not match) the |
ea921d6a1819
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
363 |
program will still print out \pcode{Welcome}. The only |
230
603cbd28e988
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
364 |
information we need for this attack to work is to know where |
603cbd28e988
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
365 |
the function \pcode{welcome()} starts in memory. This |
603cbd28e988
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
366 |
information can be easily obtained by starting the program |
603cbd28e988
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
367 |
inside the debugger and disassembling this function. |
227
7807863c4196
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
218
diff
changeset
|
368 |
|
7807863c4196
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
218
diff
changeset
|
369 |
\begin{lstlisting}[numbers=none,language={[x86masm]Assembler}, |
7807863c4196
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
218
diff
changeset
|
370 |
morekeywords={movl,movw}] |
7807863c4196
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
218
diff
changeset
|
371 |
$ gdb C2 |
7807863c4196
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
218
diff
changeset
|
372 |
GNU gdb (GDB) 7.2-ubuntu |
7807863c4196
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
218
diff
changeset
|
373 |
(gdb) disassemble welcome |
7807863c4196
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
218
diff
changeset
|
374 |
\end{lstlisting} |
7807863c4196
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
218
diff
changeset
|
375 |
|
229
ea921d6a1819
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
376 |
\noindent \pcode{C2} is the name of the program and |
ea921d6a1819
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
377 |
\pcode{gdb} is the name of the debugger. The output will be |
ea921d6a1819
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
378 |
something like this |
227
7807863c4196
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
218
diff
changeset
|
379 |
|
7807863c4196
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
218
diff
changeset
|
380 |
\begin{lstlisting}[numbers=none,language={[x86masm]Assembler}, |
7807863c4196
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
218
diff
changeset
|
381 |
morekeywords={movl,movw}] |
7807863c4196
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
218
diff
changeset
|
382 |
0x0804852c <+0>: push %ebp |
7807863c4196
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
218
diff
changeset
|
383 |
0x0804852d <+1>: mov %esp,%ebp |
7807863c4196
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
218
diff
changeset
|
384 |
0x0804852f <+3>: sub $0x4,%esp |
7807863c4196
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
218
diff
changeset
|
385 |
0x08048532 <+6>: movl $0x8048690,(%esp) |
7807863c4196
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
218
diff
changeset
|
386 |
0x08048539 <+13>: call 0x80483a4 <puts@plt> |
7807863c4196
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
218
diff
changeset
|
387 |
0x0804853e <+18>: movl $0x0,(%esp) |
7807863c4196
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
218
diff
changeset
|
388 |
0x08048545 <+25>: call 0x80483b4 <exit@plt> |
7807863c4196
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
218
diff
changeset
|
389 |
\end{lstlisting} |
7807863c4196
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
218
diff
changeset
|
390 |
|
7807863c4196
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
218
diff
changeset
|
391 |
\noindent indicating that the function \pcode{welcome()} |
229
ea921d6a1819
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
392 |
starts at address \pcode{0x0804852c} (top address in the |
ea921d6a1819
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
393 |
left column). |
227
7807863c4196
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
218
diff
changeset
|
394 |
|
213
9c2fa54c7c2d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
212
diff
changeset
|
395 |
\begin{figure}[p] |
9c2fa54c7c2d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
212
diff
changeset
|
396 |
\lstinputlisting[language=C]{../progs/C2.c} |
403
92c49c160b24
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
400
diff
changeset
|
397 |
\caption{A vulnerable login implementation. The use of the |
92c49c160b24
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
400
diff
changeset
|
398 |
`own' \pcode{get\_line} function makes this program |
92c49c160b24
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
400
diff
changeset
|
399 |
vulnerable. The developer should have used \emph{safe} |
92c49c160b24
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
400
diff
changeset
|
400 |
library functions instead.\label{C2}} |
213
9c2fa54c7c2d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
212
diff
changeset
|
401 |
\end{figure} |
9c2fa54c7c2d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
212
diff
changeset
|
402 |
|
227
7807863c4196
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
218
diff
changeset
|
403 |
This kind of attack was very popular with commercial programs |
7807863c4196
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
218
diff
changeset
|
404 |
that needed a key to be unlocked. Historically, hackers first |
7807863c4196
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
218
diff
changeset
|
405 |
broke the rather weak encryption of these locking mechanisms. |
7807863c4196
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
218
diff
changeset
|
406 |
After the encryption had been made stronger, hackers used |
7807863c4196
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
218
diff
changeset
|
407 |
buffer overflow attacks as shown above to jump directly to |
7807863c4196
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
218
diff
changeset
|
408 |
the part of the program that was intended to be only available |
229
ea921d6a1819
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
409 |
after the correct key was typed in. |
227
7807863c4196
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
218
diff
changeset
|
410 |
|
238
6ba55ba5b588
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
411 |
\subsection*{Payloads} |
227
7807863c4196
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
218
diff
changeset
|
412 |
|
7807863c4196
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
218
diff
changeset
|
413 |
Unfortunately, much more harm can be caused by buffer overflow |
7807863c4196
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
218
diff
changeset
|
414 |
attacks. This is achieved by injecting code that will be run |
7807863c4196
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
218
diff
changeset
|
415 |
once the return address is appropriately modified. Typically |
230
603cbd28e988
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
416 |
the code that will be injected starts a shell. This gives the |
603cbd28e988
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
417 |
attacker the ability to run programs on the target machine and |
397
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
418 |
to have a good look around in order to obtain also full root |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
419 |
access (normally the program that is attacked would run with |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
420 |
lesser rights and any shell injected would also only run with |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
421 |
these lesser access rights). If the attacked program was |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
422 |
already running as root, then the attacker can congratulate |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
423 |
him or herself to another computer under full control\ldots |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
424 |
no more work to be done. |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
425 |
|
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
426 |
In order to be send as part of the string that is overflowing |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
427 |
the buffer, we need the code for starting the shell to be |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
428 |
represented as a sequence of characters. For example |
227
7807863c4196
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
218
diff
changeset
|
429 |
|
7807863c4196
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
218
diff
changeset
|
430 |
\lstinputlisting[language=C,numbers=none]{../progs/o1.c} |
7807863c4196
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
218
diff
changeset
|
431 |
|
229
ea921d6a1819
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
432 |
\noindent These characters represent the machine code for |
ea921d6a1819
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
433 |
opening a shell. It seems obtaining such a string requires |
397
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
434 |
``higher-education'' in the architecture of the target system. |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
435 |
But it is actually relatively simple: First there are many |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
436 |
such strings ready-made---just a quick Google query away. |
399
6d552ef3b435
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
397
diff
changeset
|
437 |
A nice selection of ready-made shell-codes can be found |
6d552ef3b435
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
397
diff
changeset
|
438 |
for example at |
6d552ef3b435
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
397
diff
changeset
|
439 |
|
6d552ef3b435
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
397
diff
changeset
|
440 |
\begin{center} |
6d552ef3b435
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
397
diff
changeset
|
441 |
\url{http://shellblade.net/shellcode.html} |
6d552ef3b435
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
397
diff
changeset
|
442 |
\end{center} |
6d552ef3b435
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
397
diff
changeset
|
443 |
|
6d552ef3b435
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
397
diff
changeset
|
444 |
|
397
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
445 |
Second, tools like the debugger can help us again. We can just |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
446 |
write the code we want in C, for example this would be the |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
447 |
program for starting a shell: |
227
7807863c4196
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
218
diff
changeset
|
448 |
|
7807863c4196
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
218
diff
changeset
|
449 |
\lstinputlisting[language=C,numbers=none]{../progs/shell.c} |
7807863c4196
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
218
diff
changeset
|
450 |
|
400
f05368d007dd
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
399
diff
changeset
|
451 |
\noindent Once compiled, we can use the debugger to obtain the |
f05368d007dd
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
399
diff
changeset
|
452 |
machine code, or even get the ready-made encoding as character |
227
7807863c4196
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
218
diff
changeset
|
453 |
sequence. |
7807863c4196
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
218
diff
changeset
|
454 |
|
400
f05368d007dd
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
399
diff
changeset
|
455 |
\lstinputlisting[language=C,numbers=none]{../progs/o2.c} |
f05368d007dd
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
399
diff
changeset
|
456 |
|
403
92c49c160b24
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
400
diff
changeset
|
457 |
\noindent |
397
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
458 |
While not too difficult, obtaining this string is not entirely |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
459 |
trivial using \pcode{gdb}. Remember the functions in C that |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
460 |
copy or fill buffers work such that they copy everything until |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
461 |
the zero byte is reached. Unfortunately the ``vanilla'' output |
400
f05368d007dd
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
399
diff
changeset
|
462 |
from the debugger for the shell-program above contains such |
f05368d007dd
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
399
diff
changeset
|
463 |
zero bytes. So a post-processing phase is needed to rewrite |
f05368d007dd
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
399
diff
changeset
|
464 |
the machine code in a way that it does not contain any zero |
f05368d007dd
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
399
diff
changeset
|
465 |
bytes. This is like some works of literature that have been |
f05368d007dd
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
399
diff
changeset
|
466 |
written so that the letter e, for example, is avoided. The |
f05368d007dd
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
399
diff
changeset
|
467 |
technical term for such a literature work is |
263
8a42736cce27
updated 5th handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
259
diff
changeset
|
468 |
\emph{lipogram}.\footnote{The most famous example of a |
8a42736cce27
updated 5th handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
259
diff
changeset
|
469 |
lipogram is a 50,000 words novel titled Gadsby, see |
397
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
470 |
\url{https://archive.org/details/Gadsby}, which avoids the |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
471 |
letter `e' throughout.} For rewriting the machine code, you |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
472 |
might need to use clever tricks like |
227
7807863c4196
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
218
diff
changeset
|
473 |
|
7807863c4196
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
218
diff
changeset
|
474 |
\begin{lstlisting}[numbers=none,language={[x86masm]Assembler}] |
7807863c4196
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
218
diff
changeset
|
475 |
xor %eax, %eax |
7807863c4196
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
218
diff
changeset
|
476 |
\end{lstlisting} |
7807863c4196
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
218
diff
changeset
|
477 |
|
230
603cbd28e988
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
478 |
\noindent This instruction does not contain any zero-byte when |
603cbd28e988
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
479 |
encoded as string, but produces a zero-byte on the stack when |
603cbd28e988
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
480 |
run. |
227
7807863c4196
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
218
diff
changeset
|
481 |
|
230
603cbd28e988
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
482 |
Having removed the zero-bytes we can craft the string that |
603cbd28e988
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
483 |
will be send to the target computer. This of course requires |
603cbd28e988
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
484 |
that the buffer we are trying to attack can at least contain |
603cbd28e988
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
485 |
the shellcode we want to run. But as you can see this is only |
399
6d552ef3b435
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
397
diff
changeset
|
486 |
47 bytes, which is a very low bar to jump over. Actually there |
6d552ef3b435
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
397
diff
changeset
|
487 |
are optimised versions which only need 24 bytes. More |
230
603cbd28e988
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
488 |
formidable is the choice of finding the right address to jump |
603cbd28e988
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
489 |
to. The string is typically of the form |
227
7807863c4196
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
218
diff
changeset
|
490 |
|
7807863c4196
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
218
diff
changeset
|
491 |
\begin{center} |
230
603cbd28e988
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
492 |
\begin{tikzpicture}[scale=0.6] |
227
7807863c4196
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
218
diff
changeset
|
493 |
\draw[line width=1mm] (-2, -1) rectangle (2,3); |
7807863c4196
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
218
diff
changeset
|
494 |
\draw[line width=1mm] (-2,1.9) -- (2,1.9); |
7807863c4196
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
218
diff
changeset
|
495 |
\draw (0,2.5) node {\large\tt shell code}; |
7807863c4196
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
218
diff
changeset
|
496 |
\draw[line width=1mm,fill=black] (0.3, -1) rectangle (2,-0.7); |
7807863c4196
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
218
diff
changeset
|
497 |
\draw[->,line width=0.3mm] (1.05, -1) -- (1.05,-1.7) -- |
7807863c4196
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
218
diff
changeset
|
498 |
(-3,-1.7) -- (-3, 3.7) -- (-1.9, 3.7) -- (-1.9, 3.1); |
228
4f7c7997b68b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
227
diff
changeset
|
499 |
\draw (-2, 3) node[anchor=north east] {\LARGE \color{codegreen}{``}}; |
4f7c7997b68b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
227
diff
changeset
|
500 |
\draw ( 2,-0.9) node[anchor=west] {\LARGE\color{codegreen}{''}}; |
227
7807863c4196
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
218
diff
changeset
|
501 |
\end{tikzpicture} |
7807863c4196
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
218
diff
changeset
|
502 |
\end{center} |
7807863c4196
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
218
diff
changeset
|
503 |
|
230
603cbd28e988
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
504 |
\noindent where we need to be very precise with the address |
397
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
505 |
with which we will overwrite the buffer (indicated as a black |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
506 |
rectangle). It has to be precisely the first byte of the |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
507 |
shellcode. While this is easy with the help of a debugger (as |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
508 |
seen before), we typically cannot run anything, including a |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
509 |
debugger, on the machine yet we target. And the address is |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
510 |
very specific to the setup of the target machine. One way of |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
511 |
finding out what the right address is is to try out one by one |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
512 |
every possible address until we get lucky. With the large |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
513 |
memories available today, however, the odds are long. And if |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
514 |
we try out too many possible candidates too quickly, we might |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
515 |
be detected by the system administrator of the target system. |
228
4f7c7997b68b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
227
diff
changeset
|
516 |
|
397
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
517 |
We can improve our odds considerably by making use of a very |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
518 |
clever trick. Instead of adding the shellcode at the beginning |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
519 |
of the string, we should add it at the end, just before we |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
520 |
overflow the buffer, for example |
228
4f7c7997b68b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
227
diff
changeset
|
521 |
|
4f7c7997b68b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
227
diff
changeset
|
522 |
\begin{center} |
230
603cbd28e988
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
523 |
\begin{tikzpicture}[scale=0.6] |
603cbd28e988
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
524 |
\draw[gray!50,fill=gray!50] (-2,0.3) rectangle (2,3); |
228
4f7c7997b68b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
227
diff
changeset
|
525 |
\draw[line width=1mm] (-2, -1) rectangle (2,3); |
230
603cbd28e988
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
526 |
\draw[line width=1mm] (-2,0.3) -- (2,0.3); |
603cbd28e988
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
527 |
\draw[line width=1mm] (-2,-0.7) -- (2,-0.7); |
603cbd28e988
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
528 |
\draw (0,-0.2) node {\large\tt shell code}; |
228
4f7c7997b68b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
227
diff
changeset
|
529 |
\draw[line width=1mm,fill=black] (0.3, -1) rectangle (2,-0.7); |
232
abc45724b267
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
230
diff
changeset
|
530 |
\draw [line width=0.5,decoration={brace,amplitude=2mm},decorate] |
abc45724b267
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
230
diff
changeset
|
531 |
(2.3,3) -- (2.3,0.3); |
abc45724b267
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
230
diff
changeset
|
532 |
\draw[line width=0.3mm] (1.05, -1) -- (1.05,-1.7) -- |
abc45724b267
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
230
diff
changeset
|
533 |
(3,-1.7) -- (3,1.65) -- (2.6, 1.65); |
228
4f7c7997b68b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
227
diff
changeset
|
534 |
\draw (-2, 3) node[anchor=north east] {\LARGE \color{codegreen}{``}}; |
4f7c7997b68b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
227
diff
changeset
|
535 |
\draw ( 2,-0.9) node[anchor=west] {\LARGE\color{codegreen}{''}}; |
4f7c7997b68b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
227
diff
changeset
|
536 |
\end{tikzpicture} |
4f7c7997b68b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
227
diff
changeset
|
537 |
\end{center} |
227
7807863c4196
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
218
diff
changeset
|
538 |
|
238
6ba55ba5b588
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
539 |
\noindent Then we can fill up the grey part of the string with |
230
603cbd28e988
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
540 |
\pcode{NOP} operations. The code for this operation is |
397
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
541 |
\code{\\0x90} on Intel CPUs. It is available on every |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
542 |
architecture and its purpose in a CPU is to do nothing apart |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
543 |
from waiting a small amount of time. If we now use an address |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
544 |
that lets us jump to any address in the grey area we are done. |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
545 |
The target machine will execute these \pcode{NOP} operations |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
546 |
until it reaches the shellcode. That is why this NOP-part is |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
547 |
often called \emph{NOP-sledge}. A moment of thought should |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
548 |
convince you that this trick can hugely improve our odds of |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
549 |
finding the right address---depending on the size of the |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
550 |
buffer, it might only take a few tries to get the shellcode to |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
551 |
run. And then we are in. The code for such an attack is shown |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
552 |
in Figure~\ref{C3}. It is directly taken from the original |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
553 |
paper about ``Smashing the Stack for Fun and Profit'' (see |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
554 |
pointer given at the end). |
230
603cbd28e988
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
555 |
|
603cbd28e988
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
556 |
\begin{figure}[p] |
232
abc45724b267
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
230
diff
changeset
|
557 |
\lstinputlisting[language=C]{../progs/C3.c} |
abc45724b267
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
230
diff
changeset
|
558 |
\caption{Overwriting a buffer with a string containing a |
400
f05368d007dd
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
399
diff
changeset
|
559 |
payload. Lines 14 and 15 write the address of the buffer |
f05368d007dd
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
399
diff
changeset
|
560 |
into \pcode{large\_string}. The payload is copied in |
f05368d007dd
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
399
diff
changeset
|
561 |
Lines 17 and 18. Line 20 copies the (too large) string |
f05368d007dd
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
399
diff
changeset
|
562 |
into the buffer.\label{C3}} |
230
603cbd28e988
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
563 |
\end{figure} |
229
ea921d6a1819
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
564 |
|
397
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
565 |
By the way you might naw have the question how do attackers |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
566 |
find out about vulnerable systems in the first place? Well, |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
567 |
the automated version uses \emph{fuzzers}, which throw |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
568 |
randomly generated user input at applications and observe the |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
569 |
behaviour. If an application segfaults (throws a segmentation |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
570 |
error) then this is a good indication that a buffer overflow |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
571 |
vulnerability can be exploited. |
237
b784175a69dc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
572 |
|
b784175a69dc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
573 |
|
232
abc45724b267
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
230
diff
changeset
|
574 |
\subsubsection*{Format String Attacks} |
abc45724b267
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
230
diff
changeset
|
575 |
|
237
b784175a69dc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
576 |
Another question might arise, where do we get all this |
b784175a69dc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
577 |
information about addresses necessary for mounting a buffer |
b784175a69dc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
578 |
overflow attack without having yet access to the system? The |
b784175a69dc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
579 |
answer are \emph{format string attacks}. While technically |
b784175a69dc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
580 |
they are programming mistakes (and they are pointed out as |
b784175a69dc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
581 |
warning by modern compilers), they can be easily made and |
b784175a69dc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
582 |
therefore an easy target. Let us look at the simplest version |
b784175a69dc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
583 |
of a vulnerable program. |
232
abc45724b267
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
230
diff
changeset
|
584 |
|
abc45724b267
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
230
diff
changeset
|
585 |
\lstinputlisting[language=C]{../progs/C4.c} |
abc45724b267
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
230
diff
changeset
|
586 |
|
397
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
587 |
\noindent The intention of this program is to print out the |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
588 |
first argument given on the command line. The ``secret |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
589 |
string'' is never to be printed. The problem is that the C |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
590 |
function \pcode{printf} normally expects a format string---a |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
591 |
schema that directs how a string should be printed. This would |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
592 |
be for example a proper invocation of this function: |
233
5a5729358afc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
232
diff
changeset
|
593 |
|
5a5729358afc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
232
diff
changeset
|
594 |
\begin{lstlisting}[numbers=none,language=C] |
5a5729358afc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
232
diff
changeset
|
595 |
long n = 123456789; |
5a5729358afc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
232
diff
changeset
|
596 |
printf("This is a long %lu!", n); |
5a5729358afc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
232
diff
changeset
|
597 |
\end{lstlisting} |
5a5729358afc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
232
diff
changeset
|
598 |
|
5a5729358afc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
232
diff
changeset
|
599 |
\noindent In the program above, instead, the format string |
5a5729358afc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
232
diff
changeset
|
600 |
has been forgotten and only \pcode{argv[1]} is printed. |
5a5729358afc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
232
diff
changeset
|
601 |
Now if we give on the command line a string such as |
5a5729358afc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
232
diff
changeset
|
602 |
|
5a5729358afc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
232
diff
changeset
|
603 |
\begin{center} |
5a5729358afc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
232
diff
changeset
|
604 |
\code{"foo \%s"} |
5a5729358afc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
232
diff
changeset
|
605 |
\end{center} |
5a5729358afc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
232
diff
changeset
|
606 |
|
5a5729358afc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
232
diff
changeset
|
607 |
\noindent then \pcode{printf} expects a string to |
5a5729358afc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
232
diff
changeset
|
608 |
follow. But there is no string that follows, and how |
5a5729358afc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
232
diff
changeset
|
609 |
the argument resolution works in C will in fact print out |
5a5729358afc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
232
diff
changeset
|
610 |
the secret string! This can be handily exploited by |
5a5729358afc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
232
diff
changeset
|
611 |
using the format string \code{"\%x"}, which reads out the |
5a5729358afc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
232
diff
changeset
|
612 |
stack. So \code{"\%x....\%x"} will give you as much |
5a5729358afc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
232
diff
changeset
|
613 |
information from the stack as you need and over the |
5a5729358afc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
232
diff
changeset
|
614 |
Internet. |
5a5729358afc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
232
diff
changeset
|
615 |
|
5a5729358afc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
232
diff
changeset
|
616 |
While the program above contains clearly a programming |
5a5729358afc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
232
diff
changeset
|
617 |
mistake (forgotten format string), things are not as simple |
5a5729358afc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
232
diff
changeset
|
618 |
when the application reads data from the user and prompts |
236
40efc28963af
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
619 |
responses containing the user input. Consider the slight |
40efc28963af
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
620 |
variant of the program above |
40efc28963af
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
621 |
|
40efc28963af
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
622 |
\lstinputlisting[language=C]{../progs/C5.c} |
233
5a5729358afc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
232
diff
changeset
|
623 |
|
263
8a42736cce27
updated 5th handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
259
diff
changeset
|
624 |
\noindent Here the programmer actually tried to take extra |
8a42736cce27
updated 5th handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
259
diff
changeset
|
625 |
care to not fall pray to a buffer overflow attack, but in the |
8a42736cce27
updated 5th handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
259
diff
changeset
|
626 |
process made the program susceptible to a format string |
8a42736cce27
updated 5th handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
259
diff
changeset
|
627 |
attack. Clearly the \pcode{printf} function in Line 7 contains |
8a42736cce27
updated 5th handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
259
diff
changeset
|
628 |
now an explicit format string, but because the commandline |
8a42736cce27
updated 5th handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
259
diff
changeset
|
629 |
input is copied using the function \pcode{snprintf} the result |
8a42736cce27
updated 5th handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
259
diff
changeset
|
630 |
will be the same---the string can be exploited by embedding |
8a42736cce27
updated 5th handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
259
diff
changeset
|
631 |
format strings into the user input. Here the programmer really |
8a42736cce27
updated 5th handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
259
diff
changeset
|
632 |
cannot be blamed (much) because by using \pcode{snprintf} he |
8a42736cce27
updated 5th handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
259
diff
changeset
|
633 |
or she tried to make sure only 10 characters get copied into |
8a42736cce27
updated 5th handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
259
diff
changeset
|
634 |
the local buffer---in this way avoiding the obvious buffer |
8a42736cce27
updated 5th handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
259
diff
changeset
|
635 |
overflow attack. |
236
40efc28963af
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
636 |
|
40efc28963af
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
637 |
\subsubsection*{Caveats and Defences} |
40efc28963af
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
638 |
|
263
8a42736cce27
updated 5th handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
259
diff
changeset
|
639 |
How can we defend against these attacks? Well, a reflex could |
8a42736cce27
updated 5th handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
259
diff
changeset
|
640 |
be to blame programmers. Precautions should be taken by them |
8a42736cce27
updated 5th handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
259
diff
changeset
|
641 |
so that buffers cannot been overfilled and format strings |
8a42736cce27
updated 5th handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
259
diff
changeset
|
642 |
should not be forgotten. This might actually be slightly |
397
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
643 |
simpler to achieve by programmers nowadays since safe versions |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
644 |
of the library functions exist, which always specify the |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
645 |
precise number of bytes that should be copied. Compilers also |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
646 |
nowadays provide warnings when format strings are omitted. So |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
647 |
proper education of programmers is definitely a part of a |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
648 |
defence against such attacks. However, if we leave it at that, |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
649 |
then we have the mess we have today with new attacks |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
650 |
discovered almost daily. |
237
b784175a69dc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
651 |
|
b784175a69dc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
652 |
There is actually a quite long record of publications |
b784175a69dc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
653 |
proposing defences against buffer overflow attacks. One method |
b784175a69dc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
654 |
is to declare the stack data as not executable. In this way it |
b784175a69dc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
655 |
is impossible to inject a payload as shown above which is then |
b784175a69dc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
656 |
executed once the stack is smashed. But this needs hardware |
b784175a69dc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
657 |
support which allows one to declare certain memory regions to |
b784175a69dc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
658 |
be not executable. Such a feature was not introduced before |
b784175a69dc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
659 |
the Intel 386, for example. Also if you have a JIT |
b784175a69dc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
660 |
(just-in-time) compiler it might be advantageous to have |
b784175a69dc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
661 |
the stack containing executable data. So it is definitely a |
b784175a69dc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
662 |
trade-off. |
b784175a69dc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
663 |
|
b784175a69dc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
664 |
Anyway attackers have found ways around this defence: they |
b784175a69dc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
665 |
developed \emph{return-to-lib-C} attacks. The idea is to not |
b784175a69dc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
666 |
inject code, but already use the code that is present at the |
b784175a69dc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
667 |
target computer. The lib-C library, for example, already |
b784175a69dc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
668 |
contains the code for spawning a shell. With |
b784175a69dc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
669 |
\emph{return-to-lib-C} one just has to find out where this |
397
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
670 |
code is located. But attackers can make good guesses. |
232
abc45724b267
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
230
diff
changeset
|
671 |
|
397
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
672 |
Another defence is called \emph{stack canaries}. The advantage |
237
b784175a69dc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
673 |
is that they can be automatically inserted into compiled code |
b784175a69dc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
674 |
and do not need any hardware support. Though they will make |
b784175a69dc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
675 |
your program run slightly slower. The idea behind \emph{stack |
397
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
676 |
canaries} is to push a random number onto the stack just |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
677 |
before local data is stored. For our very first function |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
678 |
\pcode{foo} the stack would with a \emph{stack canary} look as |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
679 |
follows |
237
b784175a69dc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
680 |
|
b784175a69dc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
681 |
\begin{center} |
b784175a69dc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
682 |
\begin{tikzpicture}[scale=0.65] |
b784175a69dc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
683 |
%\draw[step=1cm] (-3,-1) grid (3,8); |
b784175a69dc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
684 |
\draw[gray!20,fill=gray!20] (-1, 0) rectangle (1,-1); |
b784175a69dc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
685 |
\draw[line width=1mm] (-1,-1.2) -- (-1,7.4); |
b784175a69dc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
686 |
\draw[line width=1mm] ( 1,-1.2) -- ( 1,7.4); |
b784175a69dc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
687 |
\draw (0,-1) node[anchor=south] {\tt main}; |
b784175a69dc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
688 |
\draw[line width=1mm] (-1,0) -- (1,0); |
b784175a69dc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
689 |
\draw (0,0) node[anchor=south] {\tt arg$_3$=3}; |
b784175a69dc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
690 |
\draw[line width=1mm] (-1,1) -- (1,1); |
b784175a69dc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
691 |
\draw (0,1) node[anchor=south] {\tt arg$_2$=2}; |
b784175a69dc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
692 |
\draw[line width=1mm] (-1,2) -- (1,2); |
b784175a69dc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
693 |
\draw (0,2) node[anchor=south] {\tt arg$_1$=1}; |
b784175a69dc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
694 |
\draw[line width=1mm] (-1,3) -- (1,3); |
b784175a69dc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
695 |
\draw (0,3.1) node[anchor=south] {\tt ret}; |
b784175a69dc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
696 |
\draw[line width=1mm] (-1,4) -- (1,4); |
b784175a69dc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
697 |
\draw (0,4) node[anchor=south] {\small\tt last sp}; |
b784175a69dc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
698 |
\draw[line width=1mm] (-1,5) -- (1,5); |
b784175a69dc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
699 |
\draw (0,5.1) node[anchor=south] {\tt\small\textcolor{red}{\textbf{random}}}; |
b784175a69dc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
700 |
\draw[line width=1mm] (-1,6) -- (1,6); |
b784175a69dc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
701 |
\draw (0,6) node[anchor=south] {\tt buf}; |
b784175a69dc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
702 |
\draw[line width=1mm] (-1,7) -- (1,7); |
b784175a69dc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
703 |
\end{tikzpicture} |
b784175a69dc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
704 |
\end{center} |
206
0105257429f3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
705 |
|
237
b784175a69dc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
706 |
\noindent The idea behind this random number is that when the |
b784175a69dc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
707 |
function finishes, it is checked that this random number is |
b784175a69dc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
708 |
still intact on the stack. If not, then a buffer overflow has |
397
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
709 |
occurred. Although this is quite effective, it requires |
237
b784175a69dc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
710 |
suitable support for generating random numbers. This is always |
b784175a69dc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
711 |
hard to get right and attackers are happy to exploit the |
b784175a69dc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
712 |
resulting weaknesses. |
b784175a69dc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
713 |
|
b784175a69dc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
714 |
Another defence is \emph{address space randomisation}. This |
b784175a69dc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
715 |
defence tries to make it harder for an attacker to guess |
b784175a69dc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
716 |
addresses where code is stored. It turns out that addresses |
b784175a69dc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
717 |
where code is stored is rather predictable. Randomising the |
b784175a69dc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
718 |
place where programs are stored mitigates this problem |
b784175a69dc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
719 |
somewhat. |
b784175a69dc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
720 |
|
b784175a69dc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
721 |
As mentioned before, modern operating systems have these |
b784175a69dc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
722 |
defences enabled by default and make buffer overflow attacks |
397
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
723 |
harder, but not impossible. Indeed, I---as an amateur |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
724 |
attacker---had to explicitly switch off these defences. |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
725 |
A real attacker would be more knowledgeable and not need this |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
726 |
shortcut. |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
727 |
|
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
728 |
To work I run my example under an Ubuntu version ``Maverick |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
729 |
Meerkat'' from October 2010 and the gcc 4.4.5. I have not |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
730 |
tried whether newer versions would work as well. I tested all |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
731 |
examples inside a virtual |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
732 |
box\footnote{\url{https://www.virtualbox.org}} insulating my |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
733 |
main system from any harm. When compiling the programs I |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
734 |
called the compiler with the following options: |
237
b784175a69dc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
735 |
|
b784175a69dc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
736 |
\begin{center} |
b784175a69dc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
737 |
\begin{tabular}{l@{\hspace{1mm}}l} |
b784175a69dc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
738 |
\pcode{/usr/bin/gcc} & \pcode{-ggdb -O0}\\ |
b784175a69dc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
739 |
& \pcode{-fno-stack-protector}\\ |
b784175a69dc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
740 |
& \pcode{-mpreferred-stack-boundary=2}\\ |
b784175a69dc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
741 |
& \pcode{-z execstack} |
b784175a69dc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
742 |
\end{tabular} |
b784175a69dc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
743 |
\end{center} |
206
0105257429f3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
744 |
|
237
b784175a69dc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
745 |
\noindent The first two are innocent as they instruct the |
b784175a69dc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
746 |
compiler to include debugging information and also produce |
b784175a69dc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
747 |
non-optimised code (the latter makes the output of the code a |
b784175a69dc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
748 |
bit more predictable). The third is important as it switches |
397
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
749 |
off defences like the stack canaries. The fourth again makes |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
750 |
it a bit easier to read the code. The final option makes the |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
751 |
stack executable, thus the example in Figure~\ref{C3} works as |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
752 |
intended. While this might be considered cheating....since I |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
753 |
explicitly switched off all defences, I hope I was able convey |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
754 |
the point that this is actually not too far from realistic |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
755 |
scenarios. I have shown you the classic version of the buffer |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
756 |
overflow attacks. Updated and more advanced variants do exist. |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
757 |
|
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
758 |
With the standard defences switched on, you might want to |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
759 |
argue buffer-overflow attacks have been solved on computers |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
760 |
(desktops and servers) but the computing landscape of today is |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
761 |
much wider than that. The main problem today are embedded |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
762 |
systems against which attacker can equally cause a lot of harm |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
763 |
and which are much less defended. Anthony Bonkoski makes a |
93affa1ebd6f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
764 |
similar argument in his security blog: |
206
0105257429f3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
765 |
|
237
b784175a69dc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
766 |
\begin{center} |
b784175a69dc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
767 |
\url{http://jabsoft.io/2013/09/25/are-buffer-overflows-solved-yet-a-historical-tale/} |
b784175a69dc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
768 |
\end{center} |
b784175a69dc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
769 |
|
b784175a69dc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
770 |
|
b784175a69dc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
771 |
There is one more rather effective defence against buffer |
b784175a69dc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
772 |
overflow attacks: Why not using a safe language? Java at its |
b784175a69dc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
773 |
inception was touted as a safe language because it hides |
b784175a69dc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
774 |
all explicit memory management from the user. This definitely |
b784175a69dc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
775 |
incurs a runtime penalty, but for bog-standard user-input |
b784175a69dc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
776 |
processing applications, speed is not of such an essence |
b784175a69dc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
777 |
anymore. There are of course also many other programming |
b784175a69dc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
778 |
languages that are safe, i.e.~immune to buffer overflow |
b784175a69dc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
779 |
attacks. |
b784175a69dc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
780 |
\bigskip |
b784175a69dc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
781 |
|
263
8a42736cce27
updated 5th handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
259
diff
changeset
|
782 |
\subsubsection*{Further Reading} |
8a42736cce27
updated 5th handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
259
diff
changeset
|
783 |
|
8a42736cce27
updated 5th handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
259
diff
changeset
|
784 |
If you want to know more about buffer overflow attacks, the |
8a42736cce27
updated 5th handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
259
diff
changeset
|
785 |
original Phrack article ``Smashing The Stack For Fun And |
8a42736cce27
updated 5th handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
259
diff
changeset
|
786 |
Profit'' by Elias Levy (also known as Aleph One) is an |
237
b784175a69dc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
787 |
engaging read: |
196
22f027da67ec
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
191
diff
changeset
|
788 |
|
22f027da67ec
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
191
diff
changeset
|
789 |
\begin{center} |
22f027da67ec
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
191
diff
changeset
|
790 |
\url{http://phrack.org/issues/49/14.html} |
22f027da67ec
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
191
diff
changeset
|
791 |
\end{center} |
206
0105257429f3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
792 |
|
0105257429f3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
793 |
\noindent This is an article from 1996 and some parts are |
0105257429f3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
794 |
not up-to-date anymore. The article called |
0105257429f3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
795 |
``Smashing the Stack in 2010'' |
0105257429f3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
796 |
|
0105257429f3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
797 |
\begin{center} |
0105257429f3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
798 |
\url{http://www.mgraziano.info/docs/stsi2010.pdf} |
0105257429f3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
799 |
\end{center} |
0105257429f3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
800 |
|
0105257429f3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
801 |
\noindent updates, as the name says, most information to 2010. |
240
4b2eeb0ddd81
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
238
diff
changeset
|
802 |
There is another Phrack article about return-into-lib(c) exploits |
4b2eeb0ddd81
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
238
diff
changeset
|
803 |
from 2012: |
4b2eeb0ddd81
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
238
diff
changeset
|
804 |
|
4b2eeb0ddd81
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
238
diff
changeset
|
805 |
\begin{center} |
4b2eeb0ddd81
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
238
diff
changeset
|
806 |
\url{http://phrack.org/issues/58/4.html} |
4b2eeb0ddd81
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
238
diff
changeset
|
807 |
\end{center} |
4b2eeb0ddd81
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
238
diff
changeset
|
808 |
|
4b2eeb0ddd81
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
238
diff
changeset
|
809 |
\noindent |
4b2eeb0ddd81
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
238
diff
changeset
|
810 |
The main topic is about getting around the non-executability of stack |
4b2eeb0ddd81
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
238
diff
changeset
|
811 |
data (in case it is protected). This article gives some further |
4b2eeb0ddd81
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
238
diff
changeset
|
812 |
pointers into the recent literature about buffer overflow attacks. |
4b2eeb0ddd81
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
238
diff
changeset
|
813 |
|
4b2eeb0ddd81
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
238
diff
changeset
|
814 |
Buffer overflow attacks are not just restricted to Linux and |
4b2eeb0ddd81
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
238
diff
changeset
|
815 |
``normal'' computers. There is a book |
4b2eeb0ddd81
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
238
diff
changeset
|
816 |
|
4b2eeb0ddd81
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
238
diff
changeset
|
817 |
\begin{quote}\rm |
4b2eeb0ddd81
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
238
diff
changeset
|
818 |
``iOS Hacker's Handbook'' by Miller et al, Wiley, 2012 |
4b2eeb0ddd81
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
238
diff
changeset
|
819 |
\end{quote} |
4b2eeb0ddd81
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
238
diff
changeset
|
820 |
|
4b2eeb0ddd81
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
238
diff
changeset
|
821 |
\noindent |
4b2eeb0ddd81
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
238
diff
changeset
|
822 |
which seem to describe buffer overflow attacks on iOS. A book from the |
4b2eeb0ddd81
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
238
diff
changeset
|
823 |
same publisher exists also for Android (from 2014) which seem to also |
4b2eeb0ddd81
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
238
diff
changeset
|
824 |
feature buffer overflow attacks. Alas I do not own copies of these |
4b2eeb0ddd81
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
238
diff
changeset
|
825 |
books. |
237
b784175a69dc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
826 |
|
b784175a69dc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
827 |
|
b784175a69dc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
828 |
\subsubsection*{A Crash-Course for GDB} |
b784175a69dc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
829 |
|
b784175a69dc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
830 |
If you want to try out the examples from KEATS it might be |
b784175a69dc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
831 |
helpful to know about the following commands of the GNU |
b784175a69dc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
832 |
Debugger: |
b784175a69dc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
833 |
|
b784175a69dc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
834 |
\begin{itemize} |
b784175a69dc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
835 |
\item \texttt{(l)ist n} -- lists the source file from line |
b784175a69dc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
836 |
\texttt{n}, the number can be omitted |
b784175a69dc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
837 |
\item \texttt{disassemble fun-name} -- show the assembly code |
b784175a69dc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
838 |
of a function |
400
f05368d007dd
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
399
diff
changeset
|
839 |
\item \texttt{info registers} -- prints out the current |
f05368d007dd
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
399
diff
changeset
|
840 |
content of all registers |
237
b784175a69dc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
841 |
\item \texttt{run args} -- starts the program, potential |
b784175a69dc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
842 |
arguments can be given |
b784175a69dc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
843 |
\item \texttt{(b)reak line-number} -- sets break point |
b784175a69dc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
844 |
\item \texttt{(c)ontinue} -- continue execution until next |
b784175a69dc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
845 |
breakpoint |
b784175a69dc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
846 |
\item \texttt{x/nxw addr} -- prints out \texttt{n} words starting |
b784175a69dc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
847 |
from address \pcode{addr}, the address could be \code{$esp} |
b784175a69dc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
848 |
for looking at the content of the stack |
b784175a69dc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
849 |
\item \texttt{x/nxb addr} -- prints out \texttt{n} bytes |
400
f05368d007dd
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
399
diff
changeset
|
850 |
\item \texttt{q} -- quits the debugger |
237
b784175a69dc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
851 |
\end{itemize} |
b784175a69dc
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
852 |
|
196
22f027da67ec
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
191
diff
changeset
|
853 |
|
443
67d7d239c617
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
403
diff
changeset
|
854 |
\end{document} |
156
3b831b9dc616
added some initial handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
855 |
|
3b831b9dc616
added some initial handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
856 |
%%% Local Variables: |
3b831b9dc616
added some initial handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
857 |
%%% mode: latex |
3b831b9dc616
added some initial handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
858 |
%%% TeX-master: t |
3b831b9dc616
added some initial handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
859 |
%%% End: |