handouts/ho03.tex
author Christian Urban <christian dot urban at kcl dot ac dot uk>
Fri, 10 Oct 2014 12:38:48 +0100
changeset 233 5a5729358afc
parent 232 abc45724b267
child 236 40efc28963af
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}
232
abc45724b267 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 230
diff changeset
     4
\usetikzlibrary{patterns,decorations.pathreplacing}
156
3b831b9dc616 added some initial handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     5
3b831b9dc616 added some initial handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     6
\begin{document}
3b831b9dc616 added some initial handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     7
3b831b9dc616 added some initial handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     8
\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
     9
3b831b9dc616 added some initial handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    10
By far the most popular attack method on computers are buffer
229
ea921d6a1819 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 228
diff changeset
    11
overflow attacks or variations thereof. The popularity is
ea921d6a1819 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 228
diff changeset
    12
unfortunate because we nowadays have technology in place to
ea921d6a1819 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 228
diff changeset
    13
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
    14
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
    15
out there and also many modern embedded systems often do not
ea921d6a1819 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 228
diff changeset
    16
take any precautions to prevent such attacks.
156
3b831b9dc616 added some initial handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    17
211
e6e160c7ea33 added files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 209
diff changeset
    18
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
    19
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
    20
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
    21
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
    22
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
    23
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
    24
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
    25
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
    26
a good security engineer who needs to defend such attacks, 
229
ea921d6a1819 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 228
diff changeset
    27
then better you get to know the details.
156
3b831b9dc616 added some initial handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    28
 
206
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
    29
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
    30
design decisions, which are really benign on their own, need
229
ea921d6a1819 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 228
diff changeset
    31
to conspire against you. All these decisions were taken at a
ea921d6a1819 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 228
diff changeset
    32
time when there was no Internet: C was introduced around 1973;
ea921d6a1819 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 228
diff changeset
    33
the Internet TCP/IP protocol was standardised in 1982 by which
ea921d6a1819 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 228
diff changeset
    34
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
    35
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
    36
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
    37
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
    38
way beyond the point that buffer overflow attacks are worth a
ea921d6a1819 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 228
diff changeset
    39
thought. Unfortunately, this is far from the truth. I let you
ea921d6a1819 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 228
diff changeset
    40
ponder why?
206
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
    41
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
    42
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
    43
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
    44
 
206
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
    45
\begin{center}
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
    46
  \begin{tikzpicture}[scale=0.7]
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
    47
  %\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
    48
  \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
    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[line width=1mm] (-2,-1) -- (2,-1);
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
    51
  \draw (0,2) node {\large\tt text};
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
    52
  \draw (0,0) node {\large\tt heap};
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
    53
  \draw (0,-2) node {\large\tt stack};
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
    54
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
    55
  \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
    56
  \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
    57
  \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
    58
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
    59
  \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
    60
  \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
    61
  \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
    62
  \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
    63
  \end{tikzpicture}
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
    64
\end{center}
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
    65
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
    66
\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
    67
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
    68
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
    69
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
    70
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
    71
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
    72
back to the front). That means that older items on the stack
603cbd28e988 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 229
diff changeset
    73
will be stored behind, or after, newer items. Let's look a bit
603cbd28e988 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 229
diff changeset
    74
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
    75
Consider the following simple C program.
206
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
    76
 
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
    77
\lstinputlisting[language=C]{../progs/example1.c} 
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
    78
 
229
ea921d6a1819 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 228
diff changeset
    79
\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
    80
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
    81
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
    82
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
    83
happens behind the scenes with the stack. The interesting
ea921d6a1819 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 228
diff changeset
    84
question is what will the stack be after Line 3 has been
ea921d6a1819 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 228
diff changeset
    85
executed? The answer can be illustrated as follows:
206
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
    86
 
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
    87
\begin{center} 
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
    88
 \begin{tikzpicture}[scale=0.65]
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
    89
  \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
    90
  \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
    91
  \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
    92
  \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
    93
  \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
    94
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
    95
  \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
    96
  \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
    97
  \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
    98
  \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
    99
  \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
   100
 
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   101
   %\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
   102
  \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
   103
  \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
   104
  \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
   105
  \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
   106
  \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
   107
  \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
   108
  \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
   109
  \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
   110
  \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
   111
  \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
   112
  \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
   113
  \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
   114
  \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
   115
  \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
   116
  \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
   117
  \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
   118
  \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
   119
  \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
   120
  \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
   121
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   122
  \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
   123
  \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
   124
  \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
   125
\end{tikzpicture}
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   126
\end{center} 
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   127
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   128
\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
   129
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
   130
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
   131
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
   132
Therefore first 3 then 2 and finally 1. Then it pushes the
229
ea921d6a1819 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 228
diff changeset
   133
return address onto the stack where execution should resume
ea921d6a1819 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 228
diff changeset
   134
once \code{foo} has finished. The last stack pointer
ea921d6a1819 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 228
diff changeset
   135
(\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
   136
last level---in fact there is no cleaning involved, but just
ea921d6a1819 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 228
diff changeset
   137
the top of the stack will be set back. So the last stack
ea921d6a1819 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 228
diff changeset
   138
pointer also needs to be stored. The two buffers inside
ea921d6a1819 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 228
diff changeset
   139
\pcode{foo} are on the stack too, because they are local data
ea921d6a1819 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 228
diff changeset
   140
within \code{foo}. Consequently the stack in the middle is a
ea921d6a1819 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 228
diff changeset
   141
snapshot after Line 3 has been executed. In case you are
ea921d6a1819 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 228
diff changeset
   142
familiar with assembly instructions you can also read off this
ea921d6a1819 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 228
diff changeset
   143
behaviour from the machine code that the \code{gcc} compiler
ea921d6a1819 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 228
diff changeset
   144
generates for the program above:\footnote{You can make
ea921d6a1819 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 228
diff changeset
   145
\pcode{gcc} generate assembly instructions if you call it with
ea921d6a1819 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 228
diff changeset
   146
the \pcode{-S} option, for example \pcode{gcc -S out in.c}\;.
ea921d6a1819 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 228
diff changeset
   147
Or you can look at this code by using the debugger. How to do
230
603cbd28e988 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 229
diff changeset
   148
this will be explained later.}
206
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   149
227
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   150
\begin{center}\small
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   151
\begin{tabular}[t]{@{}c@{\hspace{8mm}}c@{}}
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   152
{\lstinputlisting[language={[x86masm]Assembler},
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   153
  morekeywords={movl},xleftmargin=5mm]
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   154
  {../progs/example1a.s}} &
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   155
{\lstinputlisting[language={[x86masm]Assembler},
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   156
  morekeywords={movl,movw},xleftmargin=5mm]
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   157
  {../progs/example1b.s}}  
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   158
\end{tabular}
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   159
\end{center}
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   160
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   161
\noindent On the left you can see how the function
229
ea921d6a1819 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 228
diff changeset
   162
\pcode{main} prepares in Lines 2 to 7 the stack before calling
ea921d6a1819 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 228
diff changeset
   163
the function \pcode{foo}. You can see that the numbers 3, 2, 1
ea921d6a1819 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 228
diff changeset
   164
are stored on the stack (the register \code{$esp} refers to
ea921d6a1819 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 228
diff changeset
   165
the top of the stack). On the right you can see how the
ea921d6a1819 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 228
diff changeset
   166
function \pcode{foo} stores the two local buffers onto the
ea921d6a1819 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 228
diff changeset
   167
stack and initialises them with the given data (Lines 2 to 9).
ea921d6a1819 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 228
diff changeset
   168
Since there is no real computation going on inside
ea921d6a1819 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 228
diff changeset
   169
\pcode{foo}, the function then just restores the stack to its
ea921d6a1819 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 228
diff changeset
   170
old state and crucially sets the return address where the
ea921d6a1819 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 228
diff changeset
   171
computation should resume (Line 9 in the code on the left-hand
ea921d6a1819 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 228
diff changeset
   172
side). The instruction \code{ret} then transfers control back
ea921d6a1819 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 228
diff changeset
   173
to the function \pcode{main} to the the instruction just after
ea921d6a1819 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 228
diff changeset
   174
the call to \pcode{foo}, that is Line 9.
206
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   175
 
230
603cbd28e988 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 229
diff changeset
   176
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
   177
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
   178
 
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   179
\begin{center}
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   180
\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
   181
\end{center} 
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
\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
   184
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
   185
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
   186
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
   187
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
   188
227
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   189
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
   190
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
   191
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
   192
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
   193
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
   194
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
   195
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
   196
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   197
\begin{center}
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   198
\code{char buf[8];}
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   199
\end{center}
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   200
230
603cbd28e988 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 229
diff changeset
   201
\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
   202
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
   203
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   204
\begin{center}
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   205
 \begin{tikzpicture}[scale=0.65]
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   206
  %\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
   207
  \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
   208
  \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
   209
  \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
   210
  \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
   211
  \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
   212
  \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
   213
  \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
   214
  \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
   215
  \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
   216
  \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
   217
  \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
   218
  \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
   219
  \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
   220
  \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
   221
  \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
   222
  \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
   223
  \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
   224
  \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
   225
  \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
   226
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   227
  \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
   228
  \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
   229
  
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   230
  \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
   231
  \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
   232
\end{tikzpicture}
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   233
\end{center}
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   234
229
ea921d6a1819 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 228
diff changeset
   235
\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
   236
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
   237
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
   238
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
   239
\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
   240
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   241
\begin{center}
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   242
\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
   243
\end{center}
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   244
 
229
ea921d6a1819 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 228
diff changeset
   245
\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
   246
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
   247
(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
   248
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
   249
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
   250
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
   251
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
   252
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
   253
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
   254
\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
   255
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
   256
will copy everything up to the zero-byte. Notice that this
ea921d6a1819 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 228
diff changeset
   257
overwriting of the buffer only works since the newer item, the
ea921d6a1819 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 228
diff changeset
   258
buffer, is stored on the stack before the older items, like
ea921d6a1819 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 228
diff changeset
   259
return address and arguments. If it had be the other way
ea921d6a1819 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 228
diff changeset
   260
around, then such an overwriting by overflowing a local buffer
230
603cbd28e988 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 229
diff changeset
   261
would just not work. If the designers of C had just been able
603cbd28e988 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 229
diff changeset
   262
to foresee what headaches their way of arranging the stack
603cbd28e988 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 229
diff changeset
   263
caused in the time where computers are accessible from
603cbd28e988 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 229
diff changeset
   264
everywhere. 
227
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   265
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   266
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
   267
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
   268
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
   269
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
   270
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
   271
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
   272
(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
   273
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
   274
\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
   275
any precautions about the buffer of 8 characters being filled
230
603cbd28e988 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 229
diff changeset
   276
beyond its 8-character-limit. Let us suppose the login name
229
ea921d6a1819 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 228
diff changeset
   277
is \pcode{test}. Then the buffer overflow can be triggered
ea921d6a1819 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 228
diff changeset
   278
with a specially crafted string as password:
227
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   279
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   280
\begin{center}
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   281
\code{AAAAAAAABBBB\\x2c\\x85\\x04\\x08\\n}
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   282
\end{center}
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   283
229
ea921d6a1819 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 228
diff changeset
   284
\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
   285
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
   286
(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
   287
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
   288
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
   289
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
   290
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
   291
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
   292
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   293
\begin{lstlisting}[numbers=none,language={[x86masm]Assembler},
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   294
  morekeywords={movl,movw}]
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   295
$ gdb C2
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   296
GNU gdb (GDB) 7.2-ubuntu
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   297
(gdb) disassemble welcome
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   298
\end{lstlisting}
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   299
229
ea921d6a1819 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 228
diff changeset
   300
\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
   301
\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
   302
something like this
227
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
\begin{lstlisting}[numbers=none,language={[x86masm]Assembler},
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   305
  morekeywords={movl,movw}]
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   306
0x0804852c <+0>:     push   %ebp
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   307
0x0804852d <+1>:     mov    %esp,%ebp
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   308
0x0804852f <+3>:     sub    $0x4,%esp
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   309
0x08048532 <+6>:     movl   $0x8048690,(%esp)
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   310
0x08048539 <+13>:    call   0x80483a4 <puts@plt>
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   311
0x0804853e <+18>:    movl   $0x0,(%esp)
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   312
0x08048545 <+25>:    call   0x80483b4 <exit@plt>
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   313
\end{lstlisting}
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
\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
   316
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
   317
left column).
227
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   318
213
9c2fa54c7c2d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 212
diff changeset
   319
\begin{figure}[p]
9c2fa54c7c2d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 212
diff changeset
   320
\lstinputlisting[language=C]{../progs/C2.c}
230
603cbd28e988 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 229
diff changeset
   321
\caption{A vulnerable login implementation.\label{C2}}
213
9c2fa54c7c2d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 212
diff changeset
   322
\end{figure}
9c2fa54c7c2d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 212
diff changeset
   323
227
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   324
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
   325
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
   326
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
   327
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
   328
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
   329
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
   330
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
   331
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   332
\subsection*{Paylods}
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   333
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   334
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
   335
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
   336
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
   337
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
   338
attacker the ability to run programs on the target machine and
603cbd28e988 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 229
diff changeset
   339
to have a good look around, provided the attacked process was not
603cbd28e988 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 229
diff changeset
   340
already running as root.\footnote{In that case the attacker
603cbd28e988 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 229
diff changeset
   341
would already congratulate him or herself to another
603cbd28e988 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 229
diff changeset
   342
computer under full control.} In order to be send as part of
603cbd28e988 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 229
diff changeset
   343
the string that is overflowing the buffer, we need the code to
603cbd28e988 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 229
diff changeset
   344
be 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
   345
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   346
\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
   347
229
ea921d6a1819 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 228
diff changeset
   348
\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
   349
opening a shell. It seems obtaining such a string requires
ea921d6a1819 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 228
diff changeset
   350
higher-education in the architecture of the target system. But
ea921d6a1819 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 228
diff changeset
   351
it is actually relatively simple: First there are many such
ea921d6a1819 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 228
diff changeset
   352
string ready-made---just a quick Google query away. Second,
ea921d6a1819 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 228
diff changeset
   353
tools like the debugger can help us again. We can just write
ea921d6a1819 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 228
diff changeset
   354
the code we want in C, for example this would be the program
230
603cbd28e988 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 229
diff changeset
   355
for starting a shell:
227
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
\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
   358
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   359
\noindent Once compiled, we can use the debugger to obtain 
229
ea921d6a1819 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 228
diff changeset
   360
the machine code, or even the ready-made encoding as character
227
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   361
sequence. 
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   362
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   363
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
   364
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
   365
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
   366
Unfortunately the ``vanilla'' output from the debugger for the
229
ea921d6a1819 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 228
diff changeset
   367
shell-program above will contain such zero bytes. So a
ea921d6a1819 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 228
diff changeset
   368
post-processing phase is needed to rewrite the machine code in
ea921d6a1819 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 228
diff changeset
   369
a way that it does not contain any zero bytes. This is like
ea921d6a1819 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 228
diff changeset
   370
some works of literature that have been written so that the
230
603cbd28e988 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 229
diff changeset
   371
letter e, for example, is avoided. The technical term for such
603cbd28e988 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 229
diff changeset
   372
a literature work is \emph{lipogram}.\footnote{The most
603cbd28e988 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 229
diff changeset
   373
famous example of a lipogram is a 50,000 words novel titled
603cbd28e988 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 229
diff changeset
   374
Gadsby, see \url{https://archive.org/details/Gadsby}.} For
603cbd28e988 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 229
diff changeset
   375
rewriting the machine code, you might need to use clever
603cbd28e988 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 229
diff changeset
   376
tricks like
227
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   377
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   378
\begin{lstlisting}[numbers=none,language={[x86masm]Assembler}]
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   379
xor %eax, %eax
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   380
\end{lstlisting}
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   381
230
603cbd28e988 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 229
diff changeset
   382
\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
   383
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
   384
run. 
227
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   385
230
603cbd28e988 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 229
diff changeset
   386
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
   387
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
   388
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
   389
the shellcode we want to run. But as you can see this is only
603cbd28e988 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 229
diff changeset
   390
47 bytes, which is a very low bar to jump over. More
603cbd28e988 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 229
diff changeset
   391
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
   392
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
   393
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   394
\begin{center}
230
603cbd28e988 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 229
diff changeset
   395
  \begin{tikzpicture}[scale=0.6]
227
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   396
  \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
   397
  \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
   398
  \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
   399
  \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
   400
  \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
   401
  (-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
   402
  \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
   403
  \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
   404
  \end{tikzpicture}
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   405
\end{center}
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   406
230
603cbd28e988 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 229
diff changeset
   407
\noindent where we need to be very precise with the address
603cbd28e988 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 229
diff changeset
   408
with which we will overwrite the buffer. It has to be
603cbd28e988 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 229
diff changeset
   409
precisely the first byte of the shellcode. While this is easy
603cbd28e988 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 229
diff changeset
   410
with the help of a debugger (as seen before), we typically
603cbd28e988 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 229
diff changeset
   411
cannot run anything, including a debugger, on the machine yet
603cbd28e988 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 229
diff changeset
   412
we target. And the address is very specific to the setup of
603cbd28e988 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 229
diff changeset
   413
the target machine. One way of finding out what the right
603cbd28e988 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 229
diff changeset
   414
address is is to try out one by one every possible
603cbd28e988 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 229
diff changeset
   415
address until we get lucky. With the large memories available
603cbd28e988 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 229
diff changeset
   416
today, however, the odds are long. And if we try out too many
603cbd28e988 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 229
diff changeset
   417
possible candidates too quickly, we might be detected by the
603cbd28e988 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 229
diff changeset
   418
system administrator of the target system.
228
4f7c7997b68b updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 227
diff changeset
   419
229
ea921d6a1819 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 228
diff changeset
   420
We can improve our odds considerably by following a clever 
228
4f7c7997b68b updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 227
diff changeset
   421
trick. Instead of adding the shellcode at the beginning of the
4f7c7997b68b updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 227
diff changeset
   422
string, we should add it at the end, just before we overflow 
229
ea921d6a1819 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 228
diff changeset
   423
the buffer, for example
228
4f7c7997b68b updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 227
diff changeset
   424
4f7c7997b68b updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 227
diff changeset
   425
\begin{center}
230
603cbd28e988 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 229
diff changeset
   426
  \begin{tikzpicture}[scale=0.6]
603cbd28e988 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 229
diff changeset
   427
  \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
   428
  \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
   429
  \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
   430
  \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
   431
  \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
   432
  \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
   433
  \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
   434
    (2.3,3) -- (2.3,0.3);
abc45724b267 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 230
diff changeset
   435
  \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
   436
  (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
   437
  \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
   438
  \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
   439
  \end{tikzpicture}
4f7c7997b68b updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 227
diff changeset
   440
\end{center}
227
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   441
229
ea921d6a1819 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 228
diff changeset
   442
\noindent Then we can fill up the gray part of the string with
230
603cbd28e988 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 229
diff changeset
   443
\pcode{NOP} operations. The code for this operation is
229
ea921d6a1819 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 228
diff changeset
   444
\code{\\0x90}. It is available on every architecture and its
230
603cbd28e988 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 229
diff changeset
   445
purpose in a CPU is to do nothing apart from waiting a small
603cbd28e988 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 229
diff changeset
   446
amount of time. If we now use an address that lets us jump to
603cbd28e988 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 229
diff changeset
   447
any address in the gray area we are done. The target machine
603cbd28e988 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 229
diff changeset
   448
will execute these \pcode{NOP} operations until it reaches the
229
ea921d6a1819 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 228
diff changeset
   449
shellcode. A moment of thought can convince you that this
230
603cbd28e988 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 229
diff changeset
   450
trick can hugely improve our odds of finding the right
603cbd28e988 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 229
diff changeset
   451
address---depending on the size of the buffer, it might only
232
abc45724b267 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 230
diff changeset
   452
take a few tries to get the shellcode to run. And then we are
abc45724b267 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 230
diff changeset
   453
in. The code for such an attack is shown in Figure~\ref{C3}.
abc45724b267 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 230
diff changeset
   454
It is directly taken from the original paper about ``Smashing
abc45724b267 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 230
diff changeset
   455
the Stack for Fun and Profit'' (see pointer given at the end).
230
603cbd28e988 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 229
diff changeset
   456
603cbd28e988 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 229
diff changeset
   457
\begin{figure}[p]
232
abc45724b267 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 230
diff changeset
   458
\lstinputlisting[language=C]{../progs/C3.c}
abc45724b267 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 230
diff changeset
   459
\caption{Overwriting a buffer with a string containing a
abc45724b267 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 230
diff changeset
   460
payload.\label{C3}}
230
603cbd28e988 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 229
diff changeset
   461
\end{figure}
229
ea921d6a1819 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 228
diff changeset
   462
232
abc45724b267 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 230
diff changeset
   463
\subsubsection*{Format String Attacks}
abc45724b267 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 230
diff changeset
   464
abc45724b267 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 230
diff changeset
   465
A question might arise, where do we get all this information
abc45724b267 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 230
diff changeset
   466
about addresses necessary for mounting a buffer overflow
abc45724b267 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 230
diff changeset
   467
attack without having yet access to the system? The answer are
abc45724b267 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 230
diff changeset
   468
\emph{format string attacks}. While technically they are
abc45724b267 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 230
diff changeset
   469
programming mistakes (and they are pointed out as warning by
abc45724b267 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 230
diff changeset
   470
modern compilers), they can be easily made and therefore an
abc45724b267 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 230
diff changeset
   471
easy target. Let us look at the simplest version of a 
abc45724b267 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 230
diff changeset
   472
vulnerable program.
abc45724b267 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 230
diff changeset
   473
abc45724b267 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 230
diff changeset
   474
\lstinputlisting[language=C]{../progs/C4.c}
abc45724b267 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 230
diff changeset
   475
233
5a5729358afc updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 232
diff changeset
   476
\noindent The intention is to print out the first argument
5a5729358afc updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 232
diff changeset
   477
given on the command line. The ``secret string'' is never to
5a5729358afc updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 232
diff changeset
   478
be printed. The problem is that the C function \pcode{printf}
5a5729358afc updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 232
diff changeset
   479
normally expects a format string---a schema that directs how a
5a5729358afc updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 232
diff changeset
   480
string should be printed. This would be for example a proper
5a5729358afc updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 232
diff changeset
   481
invocation of this function:
5a5729358afc updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 232
diff changeset
   482
5a5729358afc updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 232
diff changeset
   483
\begin{lstlisting}[numbers=none,language=C]
5a5729358afc updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 232
diff changeset
   484
long n = 123456789;
5a5729358afc updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 232
diff changeset
   485
printf("This is a long %lu!", n);
5a5729358afc updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 232
diff changeset
   486
\end{lstlisting}
5a5729358afc updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 232
diff changeset
   487
5a5729358afc updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 232
diff changeset
   488
\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
   489
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
   490
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
   491
5a5729358afc updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 232
diff changeset
   492
\begin{center}
5a5729358afc updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 232
diff changeset
   493
\code{"foo \%s"}
5a5729358afc updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 232
diff changeset
   494
\end{center}
5a5729358afc updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 232
diff changeset
   495
5a5729358afc updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 232
diff changeset
   496
\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
   497
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
   498
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
   499
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
   500
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
   501
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
   502
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
   503
Internet.
5a5729358afc updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 232
diff changeset
   504
5a5729358afc updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 232
diff changeset
   505
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
   506
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
   507
when the application reads data from the user and prompts
5a5729358afc updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 232
diff changeset
   508
responses containing the user input. 
5a5729358afc updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 232
diff changeset
   509
232
abc45724b267 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 230
diff changeset
   510
\subsubsection*{Caveats}
abc45724b267 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 230
diff changeset
   511
206
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   512
\bigskip\bigskip
227
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   513
\subsubsection*{A Crash-Course for GDB}
206
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   514
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   515
\begin{itemize}
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   516
\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
   517
\texttt{n}
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   518
\item \texttt{disassemble fun-name}
218
bc1f7c82e1a8 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 213
diff changeset
   519
\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
   520
arguments can be given
206
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   521
\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
   522
\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
   523
breakpoint in a line number
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   524
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   525
\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
   526
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
   527
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
   528
\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
   529
\end{itemize}
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   530
204
8fe0dc898c73 added example1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 196
diff changeset
   531
 
196
22f027da67ec updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 191
diff changeset
   532
\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
   533
buffer overflow attacks, the original Phrack article
22f027da67ec updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 191
diff changeset
   534
``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
   535
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
   536
22f027da67ec updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 191
diff changeset
   537
\begin{center}
22f027da67ec updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 191
diff changeset
   538
\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
   539
\end{center} 
206
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   540
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   541
\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
   542
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
   543
``Smashing the Stack in 2010''
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   544
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   545
\begin{center}
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   546
\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
   547
\end{center}
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   548
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   549
\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
   550
 
156
3b831b9dc616 added some initial handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   551
\end{document}
3b831b9dc616 added some initial handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   552
3b831b9dc616 added some initial handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   553
%%% Local Variables: 
3b831b9dc616 added some initial handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   554
%%% mode: latex
3b831b9dc616 added some initial handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   555
%%% TeX-master: t
3b831b9dc616 added some initial handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   556
%%% End: