updated
authorChristian 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 234 17e0efbec5d0
updated
handouts/ho03.pdf
handouts/ho03.tex
progs/C4.c
Binary file handouts/ho03.pdf has changed
--- a/handouts/ho03.tex	Fri Oct 10 12:17:49 2014 +0100
+++ b/handouts/ho03.tex	Fri Oct 10 12:38:48 2014 +0100
@@ -473,6 +473,40 @@
 
 \lstinputlisting[language=C]{../progs/C4.c}
 
+\noindent The intention is to print out the first argument
+given on the command line. The ``secret string'' is never to
+be printed. The problem is that the C function \pcode{printf}
+normally expects a format string---a schema that directs how a
+string should be printed. This would be for example a proper
+invocation of this function:
+
+\begin{lstlisting}[numbers=none,language=C]
+long n = 123456789;
+printf("This is a long %lu!", n);
+\end{lstlisting}
+
+\noindent In the program above, instead, the format string
+has been forgotten and only \pcode{argv[1]} is printed.
+Now if we give on the command line a string such as
+
+\begin{center}
+\code{"foo \%s"}
+\end{center}
+
+\noindent then \pcode{printf} expects a string to 
+follow. But there is no string that follows, and how
+the argument resolution works in C will in fact print out 
+the secret string! This can be handily exploited by 
+using the format string \code{"\%x"}, which reads out the 
+stack. So \code{"\%x....\%x"} will give you as much 
+information from the stack as you need and over the 
+Internet.
+
+While the program above contains clearly a programming 
+mistake (forgotten format string), things are not as simple
+when the application reads data from the user and prompts
+responses containing the user input. 
+
 \subsubsection*{Caveats}
 
 \bigskip\bigskip
--- a/progs/C4.c	Fri Oct 10 12:17:49 2014 +0100
+++ b/progs/C4.c	Fri Oct 10 12:38:48 2014 +0100
@@ -4,10 +4,8 @@
 // a program that "just" prints the argument
 // on the command line
 
-
 int main(int argc, char **argv)
 {
    char *string = "This is a secret string\n";
-
    printf(argv[1]);
 }