LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Security
User Name
Password
Linux - Security This forum is for all security related questions.
Questions, tips, system compromises, firewalls, etc. are all included here.

Notices


Reply
  Search this Thread
Old 06-23-2012, 06:47 AM   #1
dman777
Member
 
Registered: Dec 2010
Distribution: Gentoo
Posts: 232

Rep: Reputation: 8
MySQL Injection With Apostrophe Question


I was reading the tutorial on hardening a php web server. In it they show an exploit with an SQL enjection:

Code:
$check = mysql_query("SELECT Username, Password, UserLevel FROM Users WHERE Username = '".$_POST['username']."' and Password = '".$_POST['password']."'");
If they user types:
Code:
' OR 1=1 #
in the username field box the code would look like:

Code:
SELECT Username, Password FROM Users WHERE Username = '' OR 1=1 #' and Password = ''
What I don't understand is how does just one apostrophe make Username = '".$_POST['username']."' into Username = ''? To me,with variable expansion in a even set of single quotes would make a total of 3 quotes with: Username = '''.
 
Old 06-24-2012, 03:00 AM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
The hash makes it end up as "SELECT Username, Password FROM Users WHERE Username = '' OR 1=1"?
 
Old 06-24-2012, 06:49 AM   #3
Noway2
Senior Member
 
Registered: Jul 2007
Distribution: Gentoo
Posts: 2,125

Rep: Reputation: 781Reputation: 781Reputation: 781Reputation: 781Reputation: 781Reputation: 781Reputation: 781
I am going to add a short addendum to this thread because the code example makes me nervous and I want to be sure that we've properly explained what is happening so that someone doesn't read it and use it the code. This isn't directed so much at the OP, but for anyone who comes along and reads this thread and doesn't fully understand it.

In the original post query, the PHP code had:
Quote:
WHERE Username = '".$_POST['username']."
Obviously the intent is to compare the field Username to the value that the client entered. The client data is read through the use of the superglobal $_POST and this example is meant to demonstrate an easy but serious flaw in site design. The superglobal variables contain the text as entered by the client; it has not been filtered. In this example, the exploit short circuits the query with the ' and then adds an OR TRUE statement. Since the latter part becomes true, the query then becomes select the username and password database.

What the tutorial is undoubtedly going to show you is that you need to change how the superglobal data is handled. For example:

Code:
$clientUsername = $_POST['username'];
$clientPassword = $_POST['password'];
(some verification or filtering process)
if (valid)
{
$check = mysql_query("SELECT Username, Password, UserLevel FROM Users WHERE Username = $clientUsername and Password = $clientPassword");
}
else 
{
//handle bogus data entry
}
Then perform checks on these two fields to verify their legitimacy. This can be handled in several ways and is usually a source of confusion to new PHP programmers. The validations should make sure that it contains only allowed letters, numbers, and symbols, contains no special "escape" sequences, etc, and should also verify and truncate the length to a safe value. This can be done with the filters function, or manually. When possible it is better to compare the user input against expected values and select from those instead of using the input directly. The bottom line is that user input should NEVER be used directly.

In terms of passwords, you should consider using a hash such as an MD5 or SHA1. By saving the hashes, you 1) ensure that only valid alphanumeric values of proper length are stored in your database, 2) don't have the raw passwords for someone to steal, 3) hashing the user input automatically sanitizes it. Just be sure to salt your hash table to thwart the use of rainbow tables.
 
1 members found this post helpful.
Old 06-24-2012, 09:04 AM   #4
dman777
Member
 
Registered: Dec 2010
Distribution: Gentoo
Posts: 232

Original Poster
Rep: Reputation: 8
I would like to know how the php interpreter takes one aprostrophe from input and ends up with 2 aprostrohe's in the end result: Username = ''

*Update: nevermind...I see how it plugs in now and is expanded. Brain must be getting rusty.

Last edited by dman777; 06-25-2012 at 12:24 AM.
 
Old 06-25-2012, 10:16 PM   #5
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,657
Blog Entries: 4

Rep: Reputation: 3938Reputation: 3938Reputation: 3938Reputation: 3938Reputation: 3938Reputation: 3938Reputation: 3938Reputation: 3938Reputation: 3938Reputation: 3938Reputation: 3938
There is one, and as far as I am concerned only one way to deal with this problem: placeholders.

This is a very standard SQL feature. It works like this. You prepare a query that consists of the string: SELECT Username, Password, UserLevel FROM Users WHERE Username = ?; Notice the not-in-quotes question mark.

Now, when you prepare the query for execution (by whatever means, in whatever language), you separately provide a value to correspond to each placeholder. (The number of placeholders referred-to in the query, and the number of values that you supply, of course must match exactly or the attempt will fail.)

The SQL execution engine first examines the SQL-string to decide how it will do the work (the so-called execution plan). The plan refers to the placeholder values as variables that will be supplied when the query actually runs. (An added benefit of this scheme is that, having prepared the query one time, you can execute it any number of times, supplying different placeholder-values each time. The SQL-string itself is constant.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
MySql.com SQL injection vulnerability exploited (oh, the irony) win32sux Linux - Security 1 03-29-2011 07:34 AM
LXer: MySQL allegedly hacked - via SQL injection LXer Syndicated Linux News 0 03-28-2011 09:40 PM
mysql root password apostrophe prohibits phpmyadmin login gr33d Linux - Newbie 4 11-09-2009 09:31 AM
LXer: Protecting your MySQL database from SQL injection attacks with GreenSQL LXer Syndicated Linux News 0 08-25-2008 07:00 PM
LXer: MySQL addresses SQL injection vulnerability LXer Syndicated Linux News 0 06-02-2006 07:54 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Security

All times are GMT -5. The time now is 06:04 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration