Introduction to Software Security
Total Page:16
File Type:pdf, Size:1020Kb
PONG Introduction to software security Goals of this lab: ❖ Get practical experience with manual and automatic code review ❖ Get practical experience with basic exploit development ❖ Get practical experience with protection against exploits ❖ Get practical experience with repairing vulnerable code Prerequisites: A basic understanding of security in general REVISION: 1.97 [2020-11-09] ©2007-2020 DAVID BYERS;ULF KARGÉN TABLE OF CONTENTS Part 1: Using the Linux command line ....................................................................................................................................... 1 The shell .............................................................................................................................................................................................. 1 Paths ..................................................................................................................................................................................................... 1 Commands ......................................................................................................................................................................................... 2 Part 2: The QEMU lab environment ............................................................................................................................................ 4 Setting up the local image ........................................................................................................................................................... 4 Starting the virtual machine ...................................................................................................................................................... 4 Accessing the virtual machine using SSH ............................................................................................................................. 5 Mounting a shared directory ..................................................................................................................................................... 5 Becoming root .................................................................................................................................................................................. 5 Part 3: Introduction to the vulnerable software .................................................................................................................... 6 What is ping and how does it work?....................................................................................................................................... 6 Why might ping be vulnerable? ................................................................................................................................................ 6 Compiling and installing pong, the vulnerable ping ........................................................................................................ 6 Part 4: Manual code review ............................................................................................................................................................ 8 Part 5: Automatic code review ...................................................................................................................................................... 9 Part 6: Exploit pong ......................................................................................................................................................................... 10 The vulnerability .......................................................................................................................................................................... 10 Exploiting the vulnerability ..................................................................................................................................................... 10 Part 7: Prevent pong from causing any harm ...................................................................................................................... 16 Use the compiler to prevent exploitation .......................................................................................................................... 16 Randomize the stack address ................................................................................................................................................. 17 Part 8: Fix pong ................................................................................................................................................................................. 18 Part 9: Exploit pong again (Optional) ...................................................................................................................................... 19 The vulnerability .......................................................................................................................................................................... 19 IDA/ADIT PONG: INTRODUCTION TO SOFTWARE SECURITY MAIN LAB In this lab you will conduct a number of different experiments on vulnerable software, including exploiting it, analyzing it, fixing it, and preventing it from causing harm. Some of the labs can be done entirely on paper. Others require access to a computer, and still others require access to a computer on which you are allowed to run exploits. For the exercises where you run exploits or need system administrator (root) access, we will use virtualized Linux systems. However, to make things a little more interesting, you will not be told what the password for root is; you will have to exploit vulnerable software on the system to gain root access. Part 1: Using the Linux command line If you are familiar with Linux and the command line, you may skip this section. This lab requires you to be able to use the Linux command line to perform basic tasks, such as editing, reading and copying files, as well as some more advanced tasks, such as compiling and debugging programs. The shell The shell in Linux is a program that interprets the commands that you type. There are many different such interpreters, the most common on Linux being bash. The prompt The shell prints a prompt – a string at the beginning of each line – where you can type commands. In Linux, the prompt takes different forms depending on whether the user is a normal user or has administrator privileges. If the prompt ends with a hash mark (#), then the user typically has administrator privileges. Paths A path is the name of a file. In Linux, paths consist of components separated by a forward slash (unlike e.g. Windows, which separates components using a backslash). A path that starts with a forward slash is a complete path, interpreted the same regardless of how it is used. A path that does not start with a slash is relative, and is interpreted relative the current working directory (CWD) of whatever program is executing. In the shell, you manipulate the current working directory using the cd command. Here are some examples of paths and what they mean: Linköping university PONG: INTRODUCTION TO SOFTWARE SECURITY 1 Path CWD Meaning /data/kurs/adit Doesn’t matter The file or directory adit, within the directory kurs, within the directory data, which in turn is a top-level directory. kurs/adit /data The file or directory adit within the directory kurs, within the directory data, which in turn is a top-level directory. kurs/adit /home The file or directory adit within the directory kurs, within the directory home, which in turn is a top-level directory. ../ /kurs/data/adit The same as /kurs/data – the name .. refers to the the directory one step up. ./ /kurs/data/adit/bin The same as /kurs/data/adit/bin – the name “.” Refers to the current working directory. Commands To issue a command, simply type it at the prompt and hit the enter key. There are two kinds of commands in Linux, shell built-ins and regular commands. Shell built-ins are commands that the shell itself implements. Examples include cd, exec, and set. Regular commands are simply programs stored in one of the directories that the shell searches for commands in. Examples include ls, cat and gcc. This makes it easy to add new commands to a Linux system. If a command is not in any of the directories the shell searches for commands in, you can still issue the command by typing a complete or relative path to it that contains at least two components. For example, if the command pong is in the directory /home/user/lab, and the current working directory is /home/user, you can run pong by typing ./lab/pong. Documentation To get documentation about a command, simply use the man command. Command Purpose man topic Show the documentation for topic. man -k keyword Show a list of topics related to keyword. Commands for manipulating files The following commands are useful for manipulating files and directories. Command Purpose touch filename Change the creation date of filename (creating it if necessary). pwd Displays the current working directory. cd directory Changes the current working directory to directory. ls Lists the contents of directory. If directory is omitted, lists the contents of the current working directory. With arguments, can display information about each file (see the manual page). cat filename Display the contents of filename less filename Displays the contents of filename page-by-page (less is a so-called pager). Press the space bar to advance one page; b to go back one page; q to quit; and h for help on all commands in less. rm filename Removes the