Jump to content

[SOLVED] sql injection


ngreenwood6

Recommended Posts

Ok I am trying to figure out how to test against my php scripts for sql injection. Say I had this code:

 

$query = "SELECT * FROM login WHERE username='$username'";

 

Now say that I did not use mysql_real_escape_string on the $username variable. How could I test for sql injection?

Link to comment
https://forums.phpfreaks.com/topic/137856-solved-sql-injection/
Share on other sites

The basic technique is to attempt to create user names that will add SQL code to your SQL query, e.g. usernames that append the DROP database command etc.  Hackers will form the username such that it adds onto your SQL and causes dangerous SQL to be executed. 

 

Rgds

Link to comment
https://forums.phpfreaks.com/topic/137856-solved-sql-injection/#findComment-720476
Share on other sites

i tried what you said but it did not work. It just gave me my error. I try the username in the database and it works.

 

my login page looks like this:

 

<?php

$username = $_POST['username'];

$conn = mysql_connect("localhost", "root", "");

mysql_select_db("tests");

$query = "SELECT * FROM users WHERE username='$username'";

$results = mysql_query($query);

$num_rows = mysql_num_rows($results);

if($num_rows == 1)
{
$row = mysql_fetch_assoc($results);
}

if($row['username'] != $username)
{
echo "That username is incorrect.";
}
else
{
session_start();
$_SESSION['logged_in'] = TRUE;
$_SESSION['username'] = $username;
header("location:logged_in.php");
}

?>

Link to comment
https://forums.phpfreaks.com/topic/137856-solved-sql-injection/#findComment-720484
Share on other sites

what the heck does that mean?

If inserted into your SQL, it should give

$query = "SELECT * FROM users WHERE username=''; delete from users";

 

 

Potentially, that can then execute both statements against your database at the line in your code that says

$results = mysql_query($query);

 

First:

SELECT * FROM users WHERE username='''

returning no valid results

 

Then:

delete from users

 

Link to comment
https://forums.phpfreaks.com/topic/137856-solved-sql-injection/#findComment-720518
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.