0

my query written in php is:

$sql="SELECT * FROM ".TABLE_PREFIX."login WHERE username ='".$username."' 
      AND password='".$password."'";
$res_id = mysql_query($sql);
$num_rows = mysql_num_rows($res_id);
echo $num_rows;

When i enter a valid user name and password from a form it works ok.

When i input some sql injection code the query output is:

SELECT * FROM form_login WHERE username ='' or '1'='1' AND password='' or '1'='1'

Which is a valid sql statement.But it gives an output(ie number of rows) as 0(zero).

If I write the same output sql statement in the program itself as-

$sql="SELECT * FROM form_login WHERE username ='' or '1'='1' 
   AND password='' or '1'='1'";

it works fine and it gives some result(for eg 3).

How can i get the correct result by inputing the sql injection code?

1
  • What is the exact text you are entering in the username and password boxes? Commented Jul 1, 2010 at 7:31

4 Answers 4

1

Try echoing out the SQL statement. It may not be what you think it is, especially if magic_quotes_gpc is enabled.

Sign up to request clarification or add additional context in comments.

1 Comment

i echoed the sql statement and my sql statement is as i have given in the question. SELECT * FROM form_login WHERE username ='' or '1'='1' AND password='' or '1'='1' but the result is not comming.
1

Or better yet, NEVER CONCATENATE SQL!

Try using the PDO library with prepared sql statements

Values are inherently safe in this mode.

Comments

0

Before using any variable in a query, pass it through mysql_real_escape_string.

2 Comments

I think in this case, he WANTS to have a SQL-Injection ;)
Not "any variable", but variable, enclosed in quotes only. So, there must be something for the rest.
0

You need to enter ' OR 1=1; -- (note that space after -- ) into the username.
After that it doesn't matter what you enter into the password, because it will be commented out.

This will translate into the following SQL statement:

SELECT * FROM form_login WHERE username ='' or 1 = 1; -- AND password= 'whatever'

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.