Path: blob/master/category-software/Buffer_Overflow_Server/labenv.tex
3059 views
1% *******************************************2% SECTION3% *******************************************4\section{Lab Environment Setup}56Please download the \texttt{Labsetup.zip} file to your VM from the lab’s website,7unzip it, and you will get a folder called \texttt{Labsetup}. All the files8needed for this lab are included in this folder.91011% -------------------------------------------12% SUBSECTION13% -------------------------------------------14\subsection{Turning off Countermeasures}1516Before starting this lab, we need to make sure the17address randomization countermeasure is turned off; otherwise, the18attack will be difficult.19You can do it using the following command:2021\begin{lstlisting}22$ sudo /sbin/sysctl -w kernel.randomize_va_space=023\end{lstlisting}242526% -------------------------------------------27% SUBSECTION28% -------------------------------------------29\subsection{The Vulnerable Program}30\label{sec:vulnerable_program}3132The vulnerable program used in this lab is called33\texttt{stack.c}, which is in the \texttt{server-code} folder.34This program has a buffer-overflow vulnerability,35and your job is to exploit this vulnerability and gain the root privilege.36The code listed below has some non-essential information removed,37so it is slightly different from what you get from the lab setup file.3839\begin{lstlisting}[language=C, caption={The vulnerable program \texttt{stack.c}}]40#include <stdlib.h>41#include <stdio.h>42#include <string.h>4344/* Changing this size will change the layout of the stack.45* Instructors can change this value each year, so students46* won't be able to use the solutions from the past. */47#ifndef BUF_SIZE48#define BUF_SIZE 10049#endif5051int bof(char *str)52{53char buffer[BUF_SIZE];5455/* The following statement has a buffer overflow problem */56(*@\ifdefined\arm57\ \ \ \ memcpy(buffer, str, 517);58\else59\ \ \ \ strcpy(buffer, str);60\fi@*)6162return 1;63}6465void foo(char *str)66{67...68bof(str);69}7071int main(int argc, char **argv)72{73char str[517];7475int length = fread(str, sizeof(char), 517, stdin);76foo(str);77fprintf(stdout, "==== Returned Properly ====\n");78return 1;79}80\end{lstlisting}8182The above program has a buffer overflow vulnerability. It83reads data from the standard input, and the data are84eventually copied to another buffer in the function {\tt bof()}.85The original input can have a maximum length of \texttt{517} bytes, but the buffer86in {\tt bof()} is only \texttt{BUF\_SIZE} bytes long, which is less than87\texttt{517}.88\ifdefined\arm89When \texttt{memcpy()} copies the data to the target buffer,90\else91Because {\tt strcpy()} does not check boundaries,92\fi93buffer overflow will occur.9495The program will run on a server with the root privilege, and its96standard input will be redirected to a TCP connection between the97server and a remote user.98Therefore, the program actually gets its data from a remote user.99If users can exploit this buffer overflow vulnerability,100they can get a root shell on the server.101102103\paragraph{Compilation.}104To compile the above vulnerable program, we need to105turn off the StackGuard and the non-executable stack protections106using the \texttt{-fno-stack-protector} and \texttt{"-z execstack"} options.107The following is an example of the compilation command (the \texttt{L1} environment108variable sets the value for the \texttt{BUF\_SIZE} constant inside \texttt{stack.c}).109110\begin{lstlisting}111$ gcc -DBUF_SIZE=$(L1) -o stack -z execstack -fno-stack-protector stack.c112\end{lstlisting}113114\ifdefined\arm115\else116We will compile the \texttt{stack} program into both 32-bit and 64-bit117binaries. Our pre-built Ubuntu 20.04 VM is a 64-bit VM, but it118still supports 32-bit binaries. All we need to do is to119use the \texttt{-m32} option in the \texttt{gcc} command.120For 32-bit compilation, we also use \texttt{-static} to generate121a statically-linked binary, which is self-contained and not depending122on any dynamic library, because the 32-bit dynamic libraries123are not installed in our containers.124\fi125126The compilation commands are already provided in \texttt{Makefile}. To compile127the code, you need to type \texttt{make} to execute those commands.128The variables \texttt{L1}, \texttt{L2}, \texttt{L3}, and \texttt{L4} are129set in \texttt{Makefile}; they will be used during the compilation.130After the compilation, we need to copy the binary into131the \texttt{bof-containers} folder, so they can be used by the132containers. The following commands conduct compilation and133installation.134135\begin{lstlisting}136$ make137$ make install138\end{lstlisting}139140141\paragraph{For instructors (customization).}142To make the lab slightly different from the one offered in the past,143instructors can change the value for \texttt{BUF\_SIZE} by requiring144students to compile the server code using different \texttt{BUF\_SIZE} values.145In \texttt{Makefile}, the \texttt{BUF\_SIZE} value is set by146four variables \texttt{L1}, ..., \texttt{L4}.147Instructors should pick the values for these variables based148on the following suggestions:149150\begin{itemize}[noitemsep]151\item \texttt{L1}: pick a number between 100 and 400152\item \texttt{L2}: pick a number between 40 and 200153\item \texttt{L3}: pick a number between 100 and 400154\item \texttt{L4}: pick a number between 20 and 80;155we need to keep this number smaller, to make this level more challenging156than the previous level.157\end{itemize}158159160161\paragraph{The Server Program.}162In the \texttt{server-code} folder, you can find a program called \texttt{server.c}.163This is the main entry point of the server. It listens to port \texttt{9090}.164When it receives a TCP connection, it165invokes the \texttt{stack} program, and sets the TCP connection166as the standard input of the \texttt{stack} program. This way,167when \texttt{stack} reads data from \texttt{stdin}, it actually168reads from the TCP connection, i.e. the data are provided by169the user on the TCP client side. It is not necessary for170students to read the source code of \texttt{server.c}.171172173% -------------------------------------------174% SUBSECTION175% -------------------------------------------176\subsection{Container Setup and Commands}177178%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%179\input{\commonfolder/container/setup}180%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%181182183\paragraph{Note.} It should be noted that before running184\texttt{"docker-compose build"} to build the docker185images, we need to compile and copy the server186code to the \texttt{bof-containers} folder.187This step is described in Section~\ref{sec:vulnerable_program}.188189190191192