Week 7 Discussions - SQL Injection

profileclements89
week_7.zip

Discussion-not-secure/index.php

<?php //File: index.php //Author: Jason Grimard //Course: UMUC SDEV 300 //Project: Week 7 Discussion Post //Due Date: 07/03/2016 //Description: A form that takes login information from the user_error //and passes it to processLogin.php. This could be an HTML file however //LEO alters HTML files, so a PHP file was used. ?> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Week 7 Discussion</title> </head> <body> <div align="center"> <h3>SDEV300<br> Week 7 Discussion<br> Jason Grimard </h3> <br> <br> <h4> Please enter login information then click login </h4> <form action="processLogin.php" method="POST"> <table border="0"> <tbody> <tr> <td>UserName</td> <td><input type="text" name="userName"/></td> </tr> <tr> <td>Password</td> <td><input type="password" name="password"/></td> </tr> <td colspan="2" align="center"> <input type="submit" value="Login" /> </td> </tr> </tbody> </table> </form> </div> </body> </html>

Discussion-not-secure/processLogin.php

<?php //NOT SECURE VERSION //File: processLogin.php //Author: Jason Grimard //Course: UMUC SDEV 300 //Project: Week 7 Discission Post //Due Date: 07/03/2016 //Description: Page that receives POSTed data from the form page, //access MySQL database, and logs user in. // //Create connection to database $SQLservername = "localhost"; $SQLusername = "sdev_owner"; $SQLpassword = "sdev300"; $SQLdatabase = "sdev"; $conn = mysqli_connect($SQLservername, $SQLusername, $SQLpassword, $SQLdatabase); // Check connection if (!$conn) { die("MySQL Connection failed: " . mysqli_connect_error()); } //Define variables $fName = ""; //first name $lName = ""; //last name $userName = ""; //user name $password = ""; //password //Retrieve POST data if (!empty($_POST["userName"])) { $userName = $_POST["userName"]; } if (!empty($_POST["password"])) { $password = $_POST["password"]; } //Check login against database $sql = "SELECT * FROM WebUsers WHERE UserID = '$userName' AND Password = '$password'"; //Perform the query and store the results $result = mysqli_query($conn, $sql); //If the query failed kill the page and display error if ($result === false) { die("SQL Error"); } //close connection since we are done with it. mysqli_close($conn); //Create an associative array using the results row $row = mysqli_fetch_array($result, MYSQLI_ASSOC); $count = mysqli_num_rows($result); //If count == 1 then the username and password match the database and 1 row has been returned //The user is logged in if ($count == 1) { $fName = $row['FirstName']; $lName = $row['LastName']; ?> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Week 7 Discussion</title> </head> <body> <div align="center"> <h3>Welcome <?php echo "$fName $lName!"; ?></h3><br> You are Logged in as <?php echo $userName ?><br><br> This page shows super secret confidential information... </h3> </div> </body> </html> <?php } else { // Show the login form again. include('index.php'); echo "Sorry, the username and password did not match. Try Again."; } ?>

Discussion-not-secure/Week7Discussion.sql

-- Week7Discussion.sql -- June 27, 2016 -- Jason Grimard -- UMUC SDEV300 -- - -- Create a table of users and passwords for Week 7 Discussion post -- Use the sdev database USE sdev; -- Delete the table if it already exists DROP TABLE IF EXISTS WebUsers; -- Create table - WebUsers CREATE TABLE IF NOT EXISTS WebUsers ( UserID VARCHAR(30) PRIMARY KEY, Password VARCHAR(100), FirstName VARCHAR(30), LastName VARCHAR(30) ); -- Insert WebUser into table INSERT INTO WebUsers VALUES ('admin','SuPeR_StRoNg_PaSsWoRd_jkh234','Jason','Grimard'); INSERT INTO WebUsers VALUES ('bfranklin','SuPeR_StRoNg_PaSsWoRd_jasd3234dsa','Ben','Franklin');

Discussion-secure/index.php

<?php //File: index.php //Author: Jason Grimard //Course: UMUC SDEV 300 //Project: Week 7 Discussion Post //Due Date: 07/03/2016 //Description: A form that takes login information from the user_error //and passes it to processLogin.php. This could be an HTML file however //LEO alters HTML files, so a PHP file was used. ?> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Week 7 Discussion</title> </head> <body> <div align="center"> <h3>SDEV300<br> Week 7 Discussion<br> Jason Grimard </h3> <br> <br> <h4> Please enter login information then click login </h4> <form action="processLogin.php" method="POST"> <table border="0"> <tbody> <tr> <td>UserName</td> <td><input type="text" name="userName"/></td> </tr> <tr> <td>Password</td> <td><input type="password" name="password"/></td> </tr> <td colspan="2" align="center"> <input type="submit" value="Login" /> </td> </tr> </tbody> </table> </form> </div> </body> </html>

Discussion-secure/processLogin.php

<?php //SECURE VERSION //File: processLogin.php //Author: Jason Grimard //Course: UMUC SDEV 300 //Project: Week 7 Discission Post //Due Date: 07/03/2016 //Description: Page that receives POSTed data from the login form page, //access MySQL database, and logs user in. // //Create connection to database $SQLservername = "localhost"; $SQLusername = "sdev_owner"; $SQLpassword = "sdev300"; $SQLdatabase = "sdev"; $conn = new mysqli($SQLservername, $SQLusername, $SQLpassword, $SQLdatabase); // Check connection if ($conn->connect_error) { die("MySQL Connection failed: " . mysqli_connect_error()); } //Define variables $fName = ""; //first name $lName = ""; //last name $userName = ""; //user name $password = ""; //password //Retrieve POST data if (!empty($_POST["userName"])) { $userName = $_POST["userName"]; } if (!empty($_POST["password"])) { $password = $_POST["password"]; } //Check login against database //Prepare statement and bind if ($stmt = $conn->prepare("SELECT UserID, FirstName, LastName FROM WebUsers WHERE UserID = ? AND Password = ?")) { $stmt->bind_param("ss", $userName, $password); //Execute the query and bind the results $stmt->execute(); $stmt->bind_result($userName, $fName, $lName); //store results so we can access number of rows returned $stmt->store_result(); $numOfRows = $stmt->num_rows; //if 1 row returned then user name and password were correct, display secret data if ($numOfRows == 1) { $stmt->fetch() ?> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Week 7 Discussion</title> </head> <body> <div align="center"> <h3>Welcome <?php echo "$fName $lName!"; ?></h3><br> You are Logged in as <?php echo $userName ?><br><br> This page shows super secret confidential information... </h3> </div> </body> </html> <?php } else { // Show the login form again. include('index.php'); echo "Sorry, the username and password did not match. Try Again."; } $stmt->close(); }//end if stmt $conn->close(); ?>

Discussion-secure/Week7Discussion.sql

-- Week7Discussion.sql -- June 27, 2016 -- Jason Grimard -- UMUC SDEV300 -- - -- Create a table of users and passwords for Week 7 Discussion post -- Use the sdev database USE sdev; -- Delete the table if it already exists DROP TABLE IF EXISTS WebUsers; -- Create table - WebUsers CREATE TABLE IF NOT EXISTS WebUsers ( UserID VARCHAR(30) PRIMARY KEY, Password VARCHAR(100), FirstName VARCHAR(30), LastName VARCHAR(30) ); -- Insert WebUser into table INSERT INTO WebUsers VALUES ('admin','SuPeR_StRoNg_PaSsWoRd_jkh234','Jason','Grimard'); INSERT INTO WebUsers VALUES ('bfranklin','SuPeR_StRoNg_PaSsWoRd_jasd3234dsa','Ben','Franklin');