Sam Houston State University Professor Min Kyung An

Assignment 5 COSC 5335 Database Security

In this assignment, you will create a web page using PHP, and connect it to your database. Then, we will do an SQL injection attack on the web page. For this assignment, you have to start and keep running your Apache server. To complete this assignment, you must submit a document with the requirements (screenshots, and etc). Your writing and formatting of a document will be also graded. This assignment was created referring to the book, “PHP and MySQL by Adrea Tarr”, and http://php.net/

1 First PHP Page

You start with a simple web page written in HTML & PHP codes that looks like Figure 1.

Figure 1

Figure 2 shows the web page’s source code written in a file named index.. The file is currently written using HTML tags only, and later we will embed PHP codes into the HTML code. Write the code using your editor, and save the file (index.php) in the c:\\htdocs\dbclass folder. From now on, always show your name in every web page you create by in- serting it in any source codes, otherwise some points will be deducted. To open the web page written in the code you wrote, open your web browser, and enter the address http://localhost/dbclass/. Then, index.php in the c:\xampp\htdocs\dbclass folder will be loaded on the web browser. This is your first PHP web page.

1. Submit a screenshot showing the loaded web page.

Page 1 Sam Houston State University Professor Min Kyung An

Figure 2

2 Connecting to the Database

In this section, you will connect your web page to the database you created in previous assignments. In order to communicate with a MySQL database, you need to use PHP. What is the PHP? As mentioned in Assignment 4, PHP is a scripting language that is especially suited for web development and its code can be embedded into HTML code. PHP is an object-oriented (not “pure”, though) language providing several classes and their functions (methods). The original way to establish a connection between PHP and a MySQL database was to use a class named . It has been replaced by mysqli in PHP 5. mysqli is an improved, more secure version that takes advantage of features added to new version of MySQL. At this point, some students probably do not understand what an object-oriented language means, and what classes and methods mean. It is strongly recommended for you to self- study the basics of PHP or at least those terms. In case that you still do not understand those, just follow the step-by-step instructions in this assignment. Let’s get started. First of all, you need to know the hostname (which is localhost) of the web server, and username and password of the databse. You create an instance (object) of the class mysqli to establish a connection. The code in Figure 3 connects to MySQL running on localhost and uses the username root and the password 12345. (You must use your password in the code.) The object $connect can be called anything. The Figure 3 shows that the new PHP code is embedded in the previous HTML code. When PHP parses a file, it looks for opening and closing tags, which are which tell PHP to start and stop interpreting the code between them. To learn very basic syntax of PHP, please refer to http://php.net/manual/en/language.basic-syntax.php. If there is an error with the connection, the error is put in the property connect error for the object you just created. Use the if statement to check for errors. The example in Figure 4 displays the error message if there is an error. If you are in a production site, you

Page 2 Sam Houston State University Professor Min Kyung An

Figure 3 should give a message to the user without details because the details could be used to hack the system. If there is no error, a success message is displayed. Remember to change the configuration information to match your setup.

Figure 4

Write the code following the aforementioned instruction, and save it as index.php in the c:\xampp\htdocs\dbclass folder. Load the web page written in the code.

2. Submit a screenshot showing the loaded web page. The web page must be successfully connected to the database and display the ‘successful connection’ message.

3 Selecting Data from a Table in a Database

In this section, you learn how to retrieve data from the database. The SELECT command is arguably the most common command used in PHP codes. It is also one of the most complex, with clauses that enables you to choose what table(s) you use, which columns are returned, what conditions must be met before a row is selected, what order to sort the data in, and whether and how to group and summarize the data. You have already learned how to use this command in previous assignments. In this assignment, you work with a single table at a time. You may want to self-study how to use multiple tables. The database you will use is an menagerie1 of root account. (The name of your database is different from an menagerie1. Remember that you created your

Page 3 Sam Houston State University Professor Min Kyung An

database whose name is (your-last-name) menagerie1, and the pet table in the database in previous assignments.)

3.1 Displaying Tables of a Database

See the code in Figure 5. The mysqli class has a method called query(). You pass it to a MySQL statement and it returns an object of the mysqli result class. You then use the properties and methods of that object to see your results. The sql command to see a list of tables is SHOW TABLES. The sql commands are not case sensitive, but it is standard practice to capitalize them. Assuming that $connection is your connection object, the following code executes the SHOW TALBES command and creates $result as an object based on the mysqli result class: $result = mysqli query($connection, ‘‘SHOW TABLES’’) The mysqli result class property num rows contains the number of rows. Because $result is based on the mysqli result class, it also has num rows as property. $count = mysqli num rows($result); The mysqli result class method fetch array() returns the results in the form of an array for each record, which in this case is each table. The first element in the array contains the table name. $row = $result->fetch array(); echo $row[0]; This finds only the first table in the database. To get a list of all the tables, you use a while loop. The script continues to loop through the results until it reaches the end. while ($row = $result -> fetch array() ) { echo $row[0]. ‘
’; }

Figure 5

3. Submit a screenshot of MySQL Workbench or phpMyAdmin showing the tables of an menagerie1 database.

Page 4 Sam Houston State University Professor Min Kyung An

Write the code following the aforementioned instruction, and save it as index.php in the c:\xampp\htdocs\dbclass folder. Load the web page written in the code.

4. Submit a screenshot showing the newly loaded web page. It must be successfully displaying all the tables of an menagerie1 database.

Next, create a new table named users with the following columns: userid, password, first name, last name, regdate, and permit. You can use MySQL Workbench or php- MyAdmin to create the table. create table users (userid varchar(16) not null primary key, password varchar(41) not null, first name varchar(40) not null, last name varchar(40) not null, regdate date not null, permit tinyint unsigned not null);

5. Submit a screenshot of MySQL Workbench or phpMyAdmin showing the tables of an menagerie1 database.

Load the web page written in the code index.php again.

6. Submit a screenshot showing the web page. It must be successfully displaying all the tables (including the new users table) of an menagerie1 database.

3.2 Displaying Data of a Table Selecting data through a PHP using a MySQL takes the following four steps:

1. Make a connection to the database. 2. Create a safe query with the command. 3. Run the query. 4. Read the results.

The code in Figure 6 displays the data of the pet table. Please self-study the PHP syntax, if needed. Write your index.php code to display the data of the pet table as shown in the figure.

5. Submit a screenshot of MySQL Workbench or phpMyAdmin showing the data of pet table.

Load the web page written in the code index.php again.

6. Submit a screenshot showing the web page. It must be successfully displaying all the data of pet table.

7. Submit the source code, index.php.

Page 5 Sam Houston State University Professor Min Kyung An

Figure 6

4 Inserting Data into a Table in a Database

Save the following six files in the c:\xampp\htdocs\dbclass folder: registi.php, registo.php, login.php, logout.php, db.php, layout.inc Open db.php code on your editor, and find the following statement: $this->db = new mysqli(‘localhost’, ‘root’, ‘your pwd’, ‘your menagerie1’) Change your pwd to your password, and your menagerie1 to your database name.

4.1 Setting Up Forms Before you read this section, open registi.php code on your editor, and have your name in the code (not to lose credits). Load the web page entering http://localhost/dbclass/ registi.php on your web browser.

8. Submit a screenshot showing the web page.

9. Submit a screenshot of MySQL Workbench or phpMyAdmin showing the data of users table (which is possibly having no data yet).

Forms are set up primarily using HTML code. The registi.php has

tag which sets the action that occurs when the [Register] button is clicked. This is the URI of the program that processes the form. It can either be a file separately (in this example, registo.php) or it can be an existing file that checks for whether form data was sent and automatically processes it. The tag also assigns the method (get or post) that is used to send the

Page 6 Sam Houston State University Professor Min Kyung An

form data. In general, use get, which is the default, for inquiries, and use post for database changes or actions that should not be repeated. get data is appended to the end of the URL and post data is not. Watch this video to understand get and post: https://www.youtube.com/watch?v=Un2JTC7tFPI Even though you are still confused with those methods, do not worry and just follow the instruction in this assignment. What you should know now is that the five data (ID, two passwords, first name and last name) entered at registi.php will be sent (by post method) to the registo.php page, and the registo.php page will accept the data (by get method).

4.2 Processing Forms When the form is submitted, the data is passed to either the get or the post variable, depending on the methods attribute. Open registo.php code in your editor. The passed data (sent by post method by registi.php) is read in the registo.php file by get method. The following statements in Figure 7 are used to save the passed data into variables,

Figure 7 and then, the data is inserted using the INSERT command to the users table using the following statement in Figure 8:

Figure 8

As the instructor now believes that you can interpret the statements above or at least can know what/how these statements work, no detailed explanation for the statements will be stated. If needed, please used the discussion board, and self-study.

4.3 Example: New User Registration and Log-in As you may noticed already, these files are used to register a new user for a web site connected to a database. We will get the new user’s information in registi.php, and the information will be inserted into the users table in registo.php. Load registi.php web page on your web browser entering http://localhost/dbclass/ registi.php. Create a new user by entering five data (ID, two passwords, first name and last name). Wait! Before you click [Register] button, please capture the screen so that the ID, first and last names that you are entering in the blanks can be seen.

Page 7 Sam Houston State University Professor Min Kyung An

10. Submit a screenshot showing the web page registi.php and the data you are entering now.

Now, click [Register] button. Then, the registo.php will be automatically loaded, and it will show you a pop-message showing that the registration has been successfully done. If you close the pop-up window, the registo.php will automatically load login.php web page.

11. Submit a screenshot of MySQL Workbench or phpMyAdmin showing the data of users table. Now the table must have the new user you just registered. You can also notice that the password is automatically encrypted in the database.

Go back to the web browser which currently shows login.php web page. Try to Log-in using the ID and password you just created. Suppose that the user name and password are: “user1” and “1qaz”. Then, when you click [Log-in] button, the web browser (login.php) is literally asking the web server’s database “do you have a user with the user name ‘user1’ and the password ‘1qaz’ registered in your database?” which looks like sql statement : select userid from users where userid = ‘user1’ and password = ‘1qaz’

12. Submit a screenshot showing the web page loaded after you successfully logged-in.

Try to register several users if you want to study the source codes.

5 SQL Injection

In this section, we will do an SQL injection attack.

5.1 Retrieving a User’s Data Let’s suppose that we know a user’s user ID, ‘user1’ in this example, but we do not know his/her password. Open the (login.php) on your web browser, and enter

user1’ or ‘1’=‘1

as the user ID. Do NOT enter a password (because you don’t know it), and just click [Log-in] button.

13. Submit a screenshot showing the web page loaded after you successfully logged-in.

How did it work? How was it possible for you to log-in without entering the password? We have already learned that when you click [Log-in] button, the web browser (login.php) is sending a query which looks like: select userid from users where userid = ‘user1’ and password = ‘1qaz’ In this attack, the original query above becomes select userid from users where userid = ‘user1’ or ‘1’=‘1’ and password = ‘’

Page 8 Sam Houston State University Professor Min Kyung An

So, or ‘1’=‘1’ and password = ‘’ is ignored and the row whose userid = ‘user1’ from the table users is retrieved. Can we extract more data, change the table, or delete data? What can we do more? Please try to follow videos shared with classmates in the discussion board. If you have done any successfully, share your experience with classmates. Enjoy!

Page 9