handouts/ho03.tex
author Christian Urban <christian dot urban at kcl dot ac dot uk>
Thu, 09 Oct 2014 23:12:10 +0100
changeset 229 ea921d6a1819
parent 228 4f7c7997b68b
child 230 603cbd28e988
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
229
ea921d6a1819 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 228
diff changeset
    10
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
    11
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
    12
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
    13
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
    14
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
    15
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
    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, 
229
ea921d6a1819 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 228
diff changeset
    26
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
    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
229
ea921d6a1819 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 228
diff changeset
    30
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
    31
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
    32
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
    33
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
    34
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
    35
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
    36
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
    37
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
    38
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
    39
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
 
229
ea921d6a1819 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 228
diff changeset
    78
\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
    79
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
    80
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
    81
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
    82
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
    83
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
    84
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
    85
 
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
    86
\begin{center} 
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
    87
 \begin{tikzpicture}[scale=0.65]
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
    88
  \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
    89
  \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
    90
  \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
    91
  \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
    92
  \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
    93
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
    94
  \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
    95
  \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
    96
  \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
    97
  \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
    98
  \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
    99
 
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   100
   %\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
   101
  \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
   102
  \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
   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 (0,-1) node[anchor=south] {\tt main};
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   105
  \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
   106
  \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
   107
  \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
   108
  \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
   109
  \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
   110
  \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
   111
  \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
   112
  \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
   113
  \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
   114
  \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
   115
  \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
   116
  \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
   117
  \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
   118
  \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
   119
  \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
   120
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   121
  \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
   122
  \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
   123
  \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
   124
\end{tikzpicture}
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   125
\end{center} 
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   126
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   127
\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
   128
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
   129
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
   130
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
   131
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
   132
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
   133
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
   134
(\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
   135
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
   136
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
   137
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
   138
\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
   139
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
   140
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
   141
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
   142
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
   143
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
   144
\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
   145
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
   146
Or you can look at this code by using the debugger. How to do
ea921d6a1819 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 228
diff changeset
   147
this will be explained later.}.
206
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   148
227
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   149
\begin{center}\small
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   150
\begin{tabular}[t]{@{}c@{\hspace{8mm}}c@{}}
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   151
{\lstinputlisting[language={[x86masm]Assembler},
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   152
  morekeywords={movl},xleftmargin=5mm]
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   153
  {../progs/example1a.s}} &
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   154
{\lstinputlisting[language={[x86masm]Assembler},
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   155
  morekeywords={movl,movw},xleftmargin=5mm]
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   156
  {../progs/example1b.s}}  
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   157
\end{tabular}
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   158
\end{center}
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   159
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   160
\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
   161
\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
   162
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
   163
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
   164
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
   165
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
   166
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
   167
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
   168
\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
   169
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
   170
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
   171
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
   172
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
   173
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
   174
 
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   175
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
   176
in C look typically as follows:
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
\begin{center}
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   179
\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
   180
\end{center} 
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   181
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   182
\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
   183
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
   184
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
   185
227
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   186
The central idea of the buffer overflow attack is to overwrite
229
ea921d6a1819 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 228
diff changeset
   187
the return address on the stack which designates where the
ea921d6a1819 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 228
diff changeset
   188
control flow of the program should resume once the function at
ea921d6a1819 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 228
diff changeset
   189
hand has finished its computation. So if we have somewhere in
ea921d6a1819 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 228
diff changeset
   190
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
   191
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   192
\begin{center}
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   193
\code{char buf[8];}
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   194
\end{center}
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   195
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   196
\noindent
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   197
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
   198
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   199
\begin{center}
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   200
 \begin{tikzpicture}[scale=0.65]
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   201
  %\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
   202
  \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
   203
  \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
   204
  \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
   205
  \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
   206
  \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
   207
  \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
   208
  \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
   209
  \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
   210
  \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
   211
  \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
   212
  \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
   213
  \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
   214
  \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
   215
  \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
   216
  \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
   217
  \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
   218
  \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
   219
  \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
   220
  \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
   221
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   222
  \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
   223
  \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
   224
  
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   225
  \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
   226
  \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
   227
\end{tikzpicture}
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   228
\end{center}
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   229
229
ea921d6a1819 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 228
diff changeset
   230
\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
   231
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
   232
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
   233
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
   234
\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
   235
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   236
\begin{center}
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   237
\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
   238
\end{center}
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   239
 
229
ea921d6a1819 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 228
diff changeset
   240
\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
   241
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
   242
(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
   243
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
   244
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
   245
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
   246
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
   247
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
   248
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
   249
\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
   250
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
   251
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
   252
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
   253
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
   254
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
   255
around, then such an overwriting by overflowing a local buffer
ea921d6a1819 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 228
diff changeset
   256
would just not work.
227
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   257
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   258
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
   259
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
   260
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
   261
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
   262
function \pcode{match} tests whether two such buffers contain
ea921d6a1819 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 228
diff changeset
   263
the same. If yes, then the function lets you ``in'' (by
ea921d6a1819 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 228
diff changeset
   264
printing \pcode{Welcome}). If not, it denies access (by
ea921d6a1819 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 228
diff changeset
   265
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
   266
\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
   267
any precautions about the buffer of 8 characters being filled
ea921d6a1819 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 228
diff changeset
   268
beyond this 8-character-limit. Let us suppose the login name
ea921d6a1819 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 228
diff changeset
   269
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
   270
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
   271
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   272
\begin{center}
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   273
\code{AAAAAAAABBBB\\x2c\\x85\\x04\\x08\\n}
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   274
\end{center}
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   275
229
ea921d6a1819 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 228
diff changeset
   276
\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
   277
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
   278
(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
   279
program will still print out \pcode{Welcome}. The only
ea921d6a1819 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 228
diff changeset
   280
information we need for this attack is to know where the
ea921d6a1819 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 228
diff changeset
   281
function \pcode{welcome()} starts in memory. This information
ea921d6a1819 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 228
diff changeset
   282
can be easily obtained by starting the program inside the
ea921d6a1819 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 228
diff changeset
   283
debugger and disassembling this function. 
227
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   284
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   285
\begin{lstlisting}[numbers=none,language={[x86masm]Assembler},
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   286
  morekeywords={movl,movw}]
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   287
$ gdb C2
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   288
GNU gdb (GDB) 7.2-ubuntu
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   289
(gdb) disassemble welcome
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   290
\end{lstlisting}
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   291
229
ea921d6a1819 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 228
diff changeset
   292
\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
   293
\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
   294
something like this
227
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   295
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   296
\begin{lstlisting}[numbers=none,language={[x86masm]Assembler},
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   297
  morekeywords={movl,movw}]
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   298
0x0804852c <+0>:     push   %ebp
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   299
0x0804852d <+1>:     mov    %esp,%ebp
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   300
0x0804852f <+3>:     sub    $0x4,%esp
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   301
0x08048532 <+6>:     movl   $0x8048690,(%esp)
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   302
0x08048539 <+13>:    call   0x80483a4 <puts@plt>
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   303
0x0804853e <+18>:    movl   $0x0,(%esp)
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   304
0x08048545 <+25>:    call   0x80483b4 <exit@plt>
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   305
\end{lstlisting}
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   306
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   307
\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
   308
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
   309
left column).
227
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   310
213
9c2fa54c7c2d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 212
diff changeset
   311
\begin{figure}[p]
9c2fa54c7c2d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 212
diff changeset
   312
\lstinputlisting[language=C]{../progs/C2.c}
9c2fa54c7c2d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 212
diff changeset
   313
\caption{A suspicious login implementation.\label{C2}}
9c2fa54c7c2d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 212
diff changeset
   314
\end{figure}
9c2fa54c7c2d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 212
diff changeset
   315
227
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   316
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
   317
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
   318
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
   319
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
   320
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
   321
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
   322
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
   323
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   324
\subsection*{Paylods}
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   325
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   326
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
   327
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
   328
once the return address is appropriately modified. Typically
229
ea921d6a1819 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 228
diff changeset
   329
the code that will be injected is for running a shell. This
ea921d6a1819 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 228
diff changeset
   330
gives the attacker the ability to run programs on the target
ea921d6a1819 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 228
diff changeset
   331
machine and have a good look around, provided the attacked
ea921d6a1819 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 228
diff changeset
   332
process was not already running as root.\footnote{In that case
ea921d6a1819 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 228
diff changeset
   333
the attacker would do already congratulate him or herself to
ea921d6a1819 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 228
diff changeset
   334
another computer under full control.} In order to be send as
ea921d6a1819 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 228
diff changeset
   335
part of the string that is overflowing the buffer, we need the
ea921d6a1819 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 228
diff changeset
   336
code to be represented as a sequence of characters. For
ea921d6a1819 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 228
diff changeset
   337
example
227
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   338
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   339
\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
   340
229
ea921d6a1819 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 228
diff changeset
   341
\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
   342
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
   343
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
   344
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
   345
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
   346
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
   347
the code we want in C, for example this would be the program
ea921d6a1819 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 228
diff changeset
   348
for starting a shell
227
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   349
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   350
\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
   351
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   352
\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
   353
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
   354
sequence. 
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   355
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   356
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
   357
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
   358
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
   359
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
   360
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
   361
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
   362
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
   363
some works of literature that have been written so that the
227
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   364
letter 'i', for example, is avoided. For rewriting the machine
229
ea921d6a1819 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 228
diff changeset
   365
code, you might need to use clever tricks like
227
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   366
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   367
\begin{lstlisting}[numbers=none,language={[x86masm]Assembler}]
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   368
xor %eax, %eax
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   369
\end{lstlisting}
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   370
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   371
\noindent This instruction does not contain any zero byte when
229
ea921d6a1819 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 228
diff changeset
   372
encoded, but produces a zero byte on the stack when run. 
227
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   373
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   374
Having removed the zero bytes we can craft the string that 
228
4f7c7997b68b updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 227
diff changeset
   375
will be send to the target computer. It is typically of the 
227
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   376
form
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{center}
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   379
  \begin{tikzpicture}[scale=0.7]
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   380
  \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
   381
  \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
   382
  \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
   383
  \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
   384
  \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
   385
  (-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
   386
  \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
   387
  \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
   388
  \end{tikzpicture}
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   389
\end{center}
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   390
228
4f7c7997b68b updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 227
diff changeset
   391
\noindent This of course requires that the buffer we are
4f7c7997b68b updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 227
diff changeset
   392
trying to attack can at least contain the shellcode we want to
4f7c7997b68b updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 227
diff changeset
   393
run. But as you can see this is only 47 bytes, which is a very
4f7c7997b68b updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 227
diff changeset
   394
low bar to jump over. More formidable is the choice of finding
4f7c7997b68b updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 227
diff changeset
   395
the right address to jump to. As indicated in the picture we
4f7c7997b68b updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 227
diff changeset
   396
need to be very precise with the address with which we will
4f7c7997b68b updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 227
diff changeset
   397
overwrite the buffer. It has to be precisely the first byte of
229
ea921d6a1819 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 228
diff changeset
   398
the shellcode. While this is easy with the help of a debugger
ea921d6a1819 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 228
diff changeset
   399
(as seen before), we typically cannot run anything on the
ea921d6a1819 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 228
diff changeset
   400
machine yet we target. And the address is very specific to the
ea921d6a1819 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 228
diff changeset
   401
setup of the target machine. One way of finding out what the
ea921d6a1819 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 228
diff changeset
   402
right address is is to try out one by one until we get lucky.
ea921d6a1819 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 228
diff changeset
   403
With the large memories available today, however, the odds are
ea921d6a1819 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 228
diff changeset
   404
long. And if we try out too many possible candidates too
ea921d6a1819 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 228
diff changeset
   405
quickly, we might be detected by the system administrator of
ea921d6a1819 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 228
diff changeset
   406
the target system.
228
4f7c7997b68b updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 227
diff changeset
   407
229
ea921d6a1819 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 228
diff changeset
   408
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
   409
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
   410
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
   411
the buffer, for example
228
4f7c7997b68b updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 227
diff changeset
   412
4f7c7997b68b updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 227
diff changeset
   413
\begin{center}
4f7c7997b68b updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 227
diff changeset
   414
  \begin{tikzpicture}[scale=0.7]
4f7c7997b68b updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 227
diff changeset
   415
  \draw[line width=1mm] (-2, -1) rectangle (2,3);
4f7c7997b68b updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 227
diff changeset
   416
  \draw[line width=1mm] (-2,1.9) -- (2,1.9);
4f7c7997b68b updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 227
diff changeset
   417
  \draw (0,2.5) node {\large\tt shell code};
4f7c7997b68b updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 227
diff changeset
   418
  \draw[line width=1mm,fill=black] (0.3, -1) rectangle (2,-0.7);
4f7c7997b68b updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 227
diff changeset
   419
  \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
   420
  \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
   421
  \end{tikzpicture}
4f7c7997b68b updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 227
diff changeset
   422
\end{center}
227
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   423
229
ea921d6a1819 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 228
diff changeset
   424
\noindent Then we can fill up the gray part of the string with
ea921d6a1819 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 228
diff changeset
   425
a \pcode{NOP} operation. The code for this operation is
ea921d6a1819 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 228
diff changeset
   426
\code{\\0x90}. It is available on every architecture and its
ea921d6a1819 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 228
diff changeset
   427
purpose it to to nothing apart from waiting a small amount of
ea921d6a1819 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 228
diff changeset
   428
time. If we now use an address that lets us jump to any
ea921d6a1819 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 228
diff changeset
   429
address in the gray area we are done. The target machine will 
ea921d6a1819 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 228
diff changeset
   430
execute these \pcode{NOP} operations until it reaches the
ea921d6a1819 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 228
diff changeset
   431
shellcode. A moment of thought can convince you that this
ea921d6a1819 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 228
diff changeset
   432
trick can hugely improve our odds of finding the right 
ea921d6a1819 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 228
diff changeset
   433
address---depending on the size of the buffer, it might
ea921d6a1819 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 228
diff changeset
   434
only take a few tries to get the shellcode to run.
ea921d6a1819 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 228
diff changeset
   435
206
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   436
\bigskip\bigskip
227
7807863c4196 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 218
diff changeset
   437
\subsubsection*{A Crash-Course for GDB}
206
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   438
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   439
\begin{itemize}
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   440
\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
   441
\texttt{n}
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   442
\item \texttt{disassemble fun-name}
218
bc1f7c82e1a8 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 213
diff changeset
   443
\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
   444
arguments can be given
206
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   445
\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
   446
\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
   447
breakpoint in a line number
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   448
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   449
\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
   450
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
   451
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
   452
\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
   453
\end{itemize}
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   454
204
8fe0dc898c73 added example1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 196
diff changeset
   455
 
196
22f027da67ec updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 191
diff changeset
   456
\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
   457
buffer overflow attacks, the original Phrack article
22f027da67ec updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 191
diff changeset
   458
``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
   459
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
   460
22f027da67ec updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 191
diff changeset
   461
\begin{center}
22f027da67ec updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 191
diff changeset
   462
\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
   463
\end{center} 
206
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   464
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   465
\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
   466
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
   467
``Smashing the Stack in 2010''
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   468
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   469
\begin{center}
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   470
\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
   471
\end{center}
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   472
0105257429f3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   473
\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
   474
 
156
3b831b9dc616 added some initial handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   475
\end{document}
3b831b9dc616 added some initial handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   476
3b831b9dc616 added some initial handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   477
%%% Local Variables: 
3b831b9dc616 added some initial handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   478
%%% mode: latex
3b831b9dc616 added some initial handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   479
%%% TeX-master: t
3b831b9dc616 added some initial handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   480
%%% End: