handouts/ho03.tex
author Christian Urban <christian dot urban at kcl dot ac dot uk>
Thu, 09 Oct 2014 14:41:36 +0100
changeset 227 7807863c4196
parent 218 bc1f7c82e1a8
child 228 4f7c7997b68b
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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}
156
3b831b9dc616 added some initial handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     4
3b831b9dc616 added some initial handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     5
\begin{document}
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
\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
     8
3b831b9dc616 added some initial handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     9
By far the most popular attack method on computers are buffer
211
e6e160c7ea33 added files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 209
diff changeset
    10
overflow attacks or simple variations thereof. The popularity is
212
1d2744383b7a added readme
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 211
diff changeset
    11
unfortunate because we nowadays have technology in place to prevent them
191
f675aa15b6d0 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
    12
effectively. But these kind of attacks are still very relevant
f675aa15b6d0 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
    13
even today since there are many legacy systems out there and
f675aa15b6d0 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
    14
also many modern embedded systems do not take any precautions
f675aa15b6d0 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
    15
to prevent such attacks.
156
3b831b9dc616 added some initial handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    16
211
e6e160c7ea33 added files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 209
diff changeset
    17
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
    18
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
    19
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
    20
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
    21
computer science students, but who said that criminal hackers
3b831b9dc616 added some initial handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    22
restrict themselves to everyday fare? Not to mention the
3b831b9dc616 added some initial handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    23
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
    24
even knowing what the underlying ideas are. If you want to be
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
    25
a good security engineer who needs to defend such attacks, 
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
    26
then better you know the details.
156
3b831b9dc616 added some initial handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    27
 
206
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
    28
For buffer overflow attacks to work, a number of innocent
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
    29
design decisions, which are really benign on their own, need
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
    30
to conspire against you. All these decisions were pretty much
211
e6e160c7ea33 added files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 209
diff changeset
    31
taken at a time when there was no Internet: C was introduced
e6e160c7ea33 added files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 209
diff changeset
    32
around 1973; the Internet TCP/IP protocol was standardised in
e6e160c7ea33 added files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 209
diff changeset
    33
1982 by which time there were maybe 500 servers connected (and
e6e160c7ea33 added files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 209
diff changeset
    34
all users were well-behaved, mostly academics); Intel's first
e6e160c7ea33 added files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 209
diff changeset
    35
8086 CPUs arrived around 1977. So nobody of the
e6e160c7ea33 added files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 209
diff changeset
    36
``forefathers'' can really be blamed, but as mentioned above
e6e160c7ea33 added files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 209
diff changeset
    37
we should already be way beyond the point that buffer overflow
e6e160c7ea33 added files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 209
diff changeset
    38
attacks are worth a thought. Unfortunately, this is far from
227
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
    39
the truth. I let you ponder why?
206
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
    40
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
    41
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
    42
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
    43
 
206
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
    44
\begin{center}
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
    45
  \begin{tikzpicture}[scale=0.7]
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
    46
  %\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
    47
  \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
    48
  \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
    49
  \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
    50
  \draw (0,2) node {\large\tt text};
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
    51
  \draw (0,0) node {\large\tt heap};
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
    52
  \draw (0,-2) node {\large\tt stack};
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
    53
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
    54
  \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
    55
  \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
    56
  \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
    57
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
    58
  \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
    59
  \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
    60
  \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
    61
  \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
    62
  \end{tikzpicture}
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
    63
\end{center}
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
    64
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
    65
\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
    66
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
    67
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
    68
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
    69
with the control flow of the program. Notice that the stack
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
    70
grows from a higher addresses to lower addresses. That means
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
    71
that older items on the stack will be stored behind, or after,
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
    72
newer items. Let's look a bit closer what happens with the
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
    73
stack when a program is running. Consider the following simple
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
    74
C program.
206
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
    75
 
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
    76
\lstinputlisting[language=C]{../progs/example1.c} 
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
    77
 
227
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
    78
\noindent The \code{main} function calls \code{foo} with three
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
    79
arguments. \code{Foo} contains two (local) buffers. The
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
    80
interesting point for us will be what will the stack loke
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
    81
like after Line 3 has been executed? The answer is as follows:
206
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
    82
 
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
    83
\begin{center} 
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
    84
 \begin{tikzpicture}[scale=0.65]
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
    85
  \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
    86
  \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
    87
  \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
    88
  \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
    89
  \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
    90
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
    91
  \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
    92
  \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
    93
  \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
    94
  \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
    95
  \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
    96
 
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
    97
   %\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
    98
  \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
    99
  \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
   100
  \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
   101
  \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
   102
  \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
   103
  \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
   104
  \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
   105
  \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
   106
  \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
   107
  \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
   108
  \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
   109
  \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
   110
  \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
   111
  \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
   112
  \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
   113
  \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
   114
  \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
   115
  \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
   116
  \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
   117
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   118
  \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
   119
  \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
   120
  \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
   121
\end{tikzpicture}
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   122
\end{center} 
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   123
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   124
\noindent On the left is the stack before \code{foo} is
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   125
called; on the right is the stack after \code{foo} finishes.
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   126
The function call to \code{foo} in Line 7 pushes the arguments
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   127
onto the stack in reverse order---shown in the middle.
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   128
Therefore first 3 then 2 and finally 1. Then it pushes the
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   129
return address to the stack where execution should resume once
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   130
\code{foo} has finished. The last stack pointer (\code{sp}) is
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   131
needed in order to clean up the stack to the last level---in
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   132
fact there is no cleaning involved, but just the top of the
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   133
stack will be set back. The two buffers are also on the stack,
227
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   134
because they are local data within \code{foo}. So in the
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   135
middle is a snapshot of the stack after Line 3 has been 
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   136
executed. In case you are familiar with assembly instructions
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   137
you can also read off this behaviour from the machine
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   138
code that the \code{gcc} compiler generates for the program
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   139
above:\footnote{You can make \pcode{gcc} generate assembly 
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   140
instructions if you call it with the \pcode{-S} option, 
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   141
for example \pcode{gcc -S out in.c}\;. Or you can look
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   142
at this code by using the debugger. This will be explained
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   143
later.}.
206
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   144
227
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   145
\begin{center}\small
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   146
\begin{tabular}[t]{@{}c@{\hspace{8mm}}c@{}}
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   147
{\lstinputlisting[language={[x86masm]Assembler},
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   148
  morekeywords={movl},xleftmargin=5mm]
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   149
  {../progs/example1a.s}} &
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   150
{\lstinputlisting[language={[x86masm]Assembler},
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   151
  morekeywords={movl,movw},xleftmargin=5mm]
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   152
  {../progs/example1b.s}}  
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   153
\end{tabular}
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   154
\end{center}
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   155
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   156
\noindent On the left you can see how the function
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   157
\pcode{main} prepares in Lines 2 to 7 the stack, before
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   158
calling the function \pcode{foo}. You can see that the
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   159
numbers 3, 2, 1 are stored on the stack (the register
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   160
\code{$esp} refers to the top of the stack). On the right
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   161
you can see how the function \pcode{foo} stores the two local
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   162
buffers onto the stack and initialises them with the given
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   163
data (Lines 2 to 9). Since there is no real computation
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   164
going on inside \pcode{foo} the function then just restores
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   165
the stack to its old state and crucially sets the return
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   166
address where the computation should resume (Line 9 in the
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   167
code on the left hand side). The instruction \code{ret} then
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   168
transfers control back to the function \pcode{main} to the
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   169
teh instruction just after the call, namely Line 9.
206
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   170
 
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   171
Another part of the ``conspiracy'' is that library functions
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   172
in C look typically as follows:
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   173
 
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   174
\begin{center}
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   175
\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
   176
\end{center} 
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   177
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   178
\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
   179
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
   180
copies the data until it reaches a zero-byte (\code{"\\0"}). 
206
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   181
227
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   182
The central idea of the buffer overflow attack is to overwrite
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   183
the return address on the stack which states where the control
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   184
flow of the program should resume once the function at hand
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   185
has finished its computation. So if we have somewhere in a 
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   186
function a local a buffer, say
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   187
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   188
\begin{center}
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   189
\code{char buf[8];}
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   190
\end{center}
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   191
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   192
\noindent
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   193
then the corresponding stack will look as follows
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   194
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   195
\begin{center}
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   196
 \begin{tikzpicture}[scale=0.65]
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   197
  %\draw[step=1cm] (-3,-1) grid (3,8);
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   198
  \draw[gray!20,fill=gray!20] (-1, 0) rectangle (1,-1);
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   199
  \draw[line width=1mm] (-1,-1.2) -- (-1,6.4);
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   200
  \draw[line width=1mm] ( 1,-1.2) -- ( 1,6.4);
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   201
  \draw (0,-1) node[anchor=south] {\tt main};
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   202
  \draw[line width=1mm] (-1,0) -- (1,0);
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   203
  \draw (0,0) node[anchor=south] {\tt arg$_3$=3};
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   204
  \draw[line width=1mm] (-1,1) -- (1,1);
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   205
  \draw (0,1) node[anchor=south] {\tt arg$_2$=2};
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   206
  \draw[line width=1mm] (-1,2) -- (1,2);
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   207
  \draw (0,2) node[anchor=south] {\tt arg$_1$=1};
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   208
  \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
   209
  \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
   210
  \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
   211
  \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
   212
  \draw[line width=1mm] (-1,5) -- (1,5);
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   213
  \draw (0,5) node[anchor=south] {\tt buf};
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   214
  \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
   215
  \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
   216
  \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
   217
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   218
  \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
   219
  \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
   220
  
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   221
  \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
   222
  \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
   223
\end{tikzpicture}
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   224
\end{center}
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   225
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   226
\noindent We need to fill this over its limit of
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   227
8 characters so that it overwrites the stack pointer
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   228
and then overwrites the return address. If, for example, 
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   229
we want to jump to a specific address in memory, say,
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   230
\pcode{\\x080483f4} then we need to fill the 
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   231
buffer for example as follows
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   232
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   233
\begin{center}
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   234
\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
   235
\end{center}
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   236
 
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   237
\noindent The first 8 \pcode{A}s fill the buffer to the rim;
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   238
the next four \pcode{B}s overwrite the stack pointer (with
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   239
what data we overwrite this part is usually not important);
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   240
then comes the address we want to jump to. Notice that we have
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   241
to give the address in the reverse order. All addresses on
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   242
Intel CPUs need to be given in this way. Since the string is
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   243
enclosed in double quotes, the C convention is that the string
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   244
internally will automatically be terminated by a zero-byte. If
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   245
the programmer uses functions like \pcode{strcpy} for filling
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   246
the buffer \pcode{buf}, then we can be sure it will overwrite
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   247
the stack in this manner---since it will copy everything up
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   248
to the zero-byte.
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   249
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   250
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
   251
the code shown in Figure~\ref{C2}. Under ``normal operation''
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   252
this program ask for a login-name and a password (both are
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   253
represented as strings). Both of which are stored in buffers
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   254
of length 8. The function \pcode{match} tests whether two such 
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   255
strings are equal. If yes, then the function lets you in
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   256
(by printing \pcode{Welcome}). If not, it denies access
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   257
(by printing \pcode{Wrong identity}). The vulnerable function
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   258
is \code{get_line} in Lines 11 to 19. This function does not
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   259
take any precautions about the buffer of 8 characters being
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   260
filled beyond this 8-character-limit. The buffer overflow
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   261
can be triggered by inputing something, like \pcode{foo}, for 
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   262
the login name and then the specially crafted string as 
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   263
password:
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   264
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   265
\begin{center}
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   266
\code{AAAAAAAABBBB\\x2c\\x85\\x04\\x08\\n}
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   267
\end{center}
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   268
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   269
\noindent The address happens to be the one for the function
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   270
\pcode{welcome()}. This means even with this input (where the
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   271
login name and password clearly do not match) the program will
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   272
still print out \pcode{Welcome}. The only information we need
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   273
for this attack is to know where the function
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   274
\pcode{welcome()} starts in memory. This information can be
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   275
easily obtained by starting the program inside the debugger
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   276
and disassembling this function. 
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{lstlisting}[numbers=none,language={[x86masm]Assembler},
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   279
  morekeywords={movl,movw}]
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   280
$ gdb C2
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   281
GNU gdb (GDB) 7.2-ubuntu
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   282
(gdb) disassemble welcome
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   283
\end{lstlisting}
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
\noindent 
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   286
The output will be something like this
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   287
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   288
\begin{lstlisting}[numbers=none,language={[x86masm]Assembler},
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   289
  morekeywords={movl,movw}]
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   290
0x0804852c <+0>:     push   %ebp
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   291
0x0804852d <+1>:     mov    %esp,%ebp
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   292
0x0804852f <+3>:     sub    $0x4,%esp
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   293
0x08048532 <+6>:     movl   $0x8048690,(%esp)
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   294
0x08048539 <+13>:    call   0x80483a4 <puts@plt>
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   295
0x0804853e <+18>:    movl   $0x0,(%esp)
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   296
0x08048545 <+25>:    call   0x80483b4 <exit@plt>
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   297
\end{lstlisting}
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   298
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   299
\noindent indicating that the function \pcode{welcome()}
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   300
starts at address \pcode{0x0804852c}.
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   301
213
9c2fa54c7c2d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 212
diff changeset
   302
\begin{figure}[p]
9c2fa54c7c2d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 212
diff changeset
   303
\lstinputlisting[language=C]{../progs/C2.c}
9c2fa54c7c2d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 212
diff changeset
   304
\caption{A suspicious login implementation.\label{C2}}
9c2fa54c7c2d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 212
diff changeset
   305
\end{figure}
9c2fa54c7c2d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 212
diff changeset
   306
227
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   307
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
   308
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
   309
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
   310
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
   311
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
   312
the part of the program that was intended to be only available
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   313
after the correct key was typed in by the user. 
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
\subsection*{Paylods}
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   316
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   317
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
   318
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
   319
once the return address is appropriately modified. Typically
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   320
the code that will be injected is for running a shell. In
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   321
order to be send as part of the string that is overflowing the
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   322
buffer, we need the code to be encoded as a sequence of 
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   323
characters
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   324
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   325
\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
   326
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   327
\noindent These characters represent the machine code
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   328
for opening a shell. It seems obtaining such a string
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   329
requires higher-education in the architecture of the
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   330
target system. But it is actually relatively simple: First
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   331
there are many ready-made strings available---just a quick
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   332
Google query away. Second, tools like the debugger can help 
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   333
us again. We can just write the code we want in C, for 
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   334
example this would be the program to start a shell
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   335
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   336
\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
   337
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   338
\noindent Once compiled, we can use the debugger to obtain 
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   339
the machine code, or even the ready made encoding as character
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   340
sequence. 
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   341
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   342
While easy, obtaining this string is not entirely trivial.
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   343
Remember the functions in C that copy or fill buffers work
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   344
such that they copy everything until the zero byte is reached.
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   345
Unfortunately the ``vanilla'' output from the debugger for the
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   346
shell-program will contain such zero bytes. So a
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   347
post-processing phase is needed to rewrite the machine code
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   348
such that it does not contain any zero bytes. This is like
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   349
some works of literature that have been rewritten so that the
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   350
letter 'i', for example, is avoided. For rewriting the machine
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   351
code you might need to use clever tricks like
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   352
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   353
\begin{lstlisting}[numbers=none,language={[x86masm]Assembler}]
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   354
xor %eax, %eax
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   355
\end{lstlisting}
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   356
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   357
\noindent This instruction does not contain any zero byte when
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   358
encoded, but produces a zero byte on the stack. 
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   359
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   360
Having removed the zero bytes we can craft the string that 
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   361
will be send to our target computer. It is typically of the 
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   362
form
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   363
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   364
\begin{center}
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   365
  \begin{tikzpicture}[scale=0.7]
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   366
  \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
   367
  \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
   368
  \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
   369
  \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
   370
  \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
   371
  (-3,-1.7) -- (-3, 3.7) -- (-1.9, 3.7) -- (-1.9, 3.1);
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   372
  \draw (-2, 3) node[anchor=north east] {\LARGE\tt "};
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   373
  \draw ( 2,-0.9) node[anchor=west] {\LARGE\tt "};
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   374
  \end{tikzpicture}
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   375
\end{center}
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   376
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   377
206
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   378
\bigskip\bigskip
227
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   379
\subsubsection*{A Crash-Course for GDB}
206
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   380
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   381
\begin{itemize}
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   382
\item \texttt{(l)ist n} -- listing the source file from line 
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   383
\texttt{n}
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   384
\item \texttt{disassemble fun-name}
218
bc1f7c82e1a8 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 213
diff changeset
   385
\item \texttt{run args} -- starts the program, potential 
bc1f7c82e1a8 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 213
diff changeset
   386
arguments can be given
206
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   387
\item \texttt{(b)reak line-number} -- set break point
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   388
\item \texttt{(c)ontinue} -- continue execution until next 
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   389
breakpoint in a line number
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   390
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   391
\item \texttt{x/nxw addr} -- print out \texttt{n} words starting 
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   392
from address \pcode{addr}, the address could be \code{$esp} 
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   393
for looking at the content of the stack
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   394
\item \texttt{x/nxb addr} -- print out \texttt{n} bytes 
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   395
\end{itemize}
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   396
204
8fe0dc898c73 added example1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 196
diff changeset
   397
 
196
22f027da67ec updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 191
diff changeset
   398
\bigskip\bigskip \noindent If you want to know more about
22f027da67ec updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 191
diff changeset
   399
buffer overflow attacks, the original Phrack article
22f027da67ec updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 191
diff changeset
   400
``Smashing The Stack For Fun And Profit'' by Elias Levy (also
22f027da67ec updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 191
diff changeset
   401
known as Aleph One) is an engaging read:
22f027da67ec updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 191
diff changeset
   402
22f027da67ec updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 191
diff changeset
   403
\begin{center}
22f027da67ec updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 191
diff changeset
   404
\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
   405
\end{center} 
206
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   406
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   407
\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
   408
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
   409
``Smashing the Stack in 2010''
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   410
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   411
\begin{center}
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   412
\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
   413
\end{center}
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   414
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   415
\noindent updates, as the name says, most information to 2010.
196
22f027da67ec updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 191
diff changeset
   416
 
156
3b831b9dc616 added some initial handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   417
\end{document}
3b831b9dc616 added some initial handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   418
3b831b9dc616 added some initial handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   419
%%% Local Variables: 
3b831b9dc616 added some initial handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   420
%%% mode: latex
3b831b9dc616 added some initial handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   421
%%% TeX-master: t
3b831b9dc616 added some initial handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   422
%%% End: