Overclockers Australia Forums

OCAU News - Wiki - QuickLinks - Pix - Sponsors  

Go Back   Overclockers Australia Forums > Software Topics > Programming & Software Development

Notices


Sign up for a free OCAU account and this ad will go away!
Search our forums with Google:
Reply
 
Thread Tools
Old 21st April 2010, 1:00 AM   #1
kthnx Thread Starter
Member
 
kthnx's Avatar
 
Join Date: Apr 2006
Location: NSW
Posts: 1,133
Default Do I need to escape session variables to prevent SQL injection?

Hey guys,

Every time I accept a user supplied variable, I whack it inside a mysql_real_escape_string function because of people like this.



So every time there's a bit of POST data in my PHP, it'll look like this:

PHP Code:
$username=mysql_real_escape_string($_POST['user_name']); 
However, it's recently occured to me that people could launch a similar attack by changing the contents of their session variables. Rather than storing a session ID, I've just got the user storing the username / password in a session and re-authenticating every time a new page loads; that way you can effectively kick a user from an active session by changing their password.

PHP Code:
if (isset($_SESSION['UID'])) {
    
$username $_SESSION['username'];    
    
$password $_SESSION['password'];
    
$result mysql_query("SELECT * FROM users WHERE Username = '$username'");
    
$rowmysql_fetch_array($result);
    
$dbPassword $row['Password'];
    if ((
$dbPassword != "") && ($password == $dbPassword)) { 
        
$authenticated true;
    } else {
        
// Unauthenticated user!
            
session_destroy();
                
session_write_close();
        
header("Location: login.php");
    }

Should I be using a mysql_real_escape_string on each of those session variables too? Is it easy for malicious hackerz to be tampering their session contents to SQL commands?

Thanks for any advice!
__________________
Quote:
Originally Posted by kevmif
Fucking hell... Batman can get ADSL and i'm stuck on dial up.
eBay FS: null | BakeOff Finalist 06 | Google it | 30+ successful trades for total >$1500
kthnx is offline   Reply With Quote

Join OCAU to remove this ad!
Old 21st April 2010, 1:41 AM   #2
Ashpool
Member
 
Ashpool's Avatar
 
Join Date: Feb 2003
Location: Ye Olde Melbourne Town
Posts: 3,245
Default

Quote:
Should I be using a mysql_real_escape_string on each of those session variables too?
Yes, I would

Quote:
Is it easy for malicious hackerz to be tampering their session contents to SQL commands?
Never tried it, but it doesn't sound like it would be hard to do.

If you want it really tight you probably should look at parameterized statements

From the wiki
In PHP version 5 and above, there are multiple choices for using parameterized statements. The PDO database layer is one of them:
Code:
$db = new PDO('pgsql:dbname=database');
$stmt = $db->prepare("SELECT priv FROM testUsers WHERE username=:username AND password=:password");
$stmt->bindParam(':username', $user);
$stmt->bindParam(':password', $pass);
$stmt->execute();
There are also vendor-specific methods; for instance, using the mysqli extension for MySQL 4.1 and above to create parameterized statements:
Code:
$db = new mysqli("localhost", "user", "pass", "database");
$stmt = $db -> prepare("SELECT priv FROM testUsers WHERE username=? AND password=?");
$stmt -> bind_param("ss", $user, $pass);
$stmt -> execute();

Last edited by Ashpool; 21st April 2010 at 1:48 AM.
Ashpool is offline   Reply With Quote
Old 21st April 2010, 8:58 AM   #3
Luke212
Member
 
Join Date: Feb 2003
Location: NSW
Posts: 6,419
Default

to change server side session variables they need access to your system (or file system). in other words worrying about sessions would the least of your concerns.
__________________
Democracy's greatest trick was convincing man he was informed.
Luke212 is offline   Reply With Quote
Old 21st April 2010, 10:31 AM   #4
kthnx Thread Starter
Member
 
kthnx's Avatar
 
Join Date: Apr 2006
Location: NSW
Posts: 1,133
Default

Quote:
Originally Posted by Luke212 View Post
to change server side session variables they need access to your system (or file system). in other words worrying about sessions would the least of your concerns.
Are the examples in my code above server side? I thought all that was handled by the browser :s
__________________
Quote:
Originally Posted by kevmif
Fucking hell... Batman can get ADSL and i'm stuck on dial up.
eBay FS: null | BakeOff Finalist 06 | Google it | 30+ successful trades for total >$1500
kthnx is offline   Reply With Quote
Old 21st April 2010, 10:35 AM   #5
Luke212
Member
 
Join Date: Feb 2003
Location: NSW
Posts: 6,419
Default

Quote:
Originally Posted by kthnx View Post
Are the examples in my code above server side? I thought all that was handled by the browser :s
you will notice all code referring to session variables is in php. php runs on the server. the browser doesnt even know what php is. so yeah session variables are server-side.

something you dont see, i believe a cookie is created on the browser. the cookie does not contain server varables, but it does contain a key so php can match a client to a particular server session.
__________________
Democracy's greatest trick was convincing man he was informed.

Last edited by Luke212; 21st April 2010 at 10:39 AM.
Luke212 is offline   Reply With Quote
Old 21st April 2010, 11:25 AM   #6
mordy
Member
 
mordy's Avatar
 
Join Date: Aug 2001
Location: melb
Posts: 5,116
Default

I'm really confused how the hacker can change the session vars ...

They can change their sessionid ... hence losing their session, but not make a changes to it

(luke, i'm glad your here, i thought i was losing it ... more than usual)
mordy is offline   Reply With Quote
Old 21st April 2010, 12:08 PM   #7
kthnx Thread Starter
Member
 
kthnx's Avatar
 
Join Date: Apr 2006
Location: NSW
Posts: 1,133
Default

Ohh I see! I knew something session related was stored at the client end (either in a cookie or the URL, depending on the browser) but that's just a key to access session variables, which are all stored server side?

That's going to save me five lines of code and an unnescessary SQL query
__________________
Quote:
Originally Posted by kevmif
Fucking hell... Batman can get ADSL and i'm stuck on dial up.
eBay FS: null | BakeOff Finalist 06 | Google it | 30+ successful trades for total >$1500
kthnx is offline   Reply With Quote
Old 21st April 2010, 12:10 PM   #8
kthnx Thread Starter
Member
 
kthnx's Avatar
 
Join Date: Apr 2006
Location: NSW
Posts: 1,133
Default

Quote:
Originally Posted by Ashpool View Post
If you want it really tight you probably should look at parameterized statements

From the wiki
In PHP version 5 and above, there are multiple choices for using parameterized statements. The PDO database layer is one of them:
Code:
$db = new PDO('pgsql:dbname=database');
$stmt = $db->prepare("SELECT priv FROM testUsers WHERE username=:username AND password=:password");
$stmt->bindParam(':username', $user);
$stmt->bindParam(':password', $pass);
$stmt->execute();
There are also vendor-specific methods; for instance, using the mysqli extension for MySQL 4.1 and above to create parameterized statements:
Code:
$db = new mysqli("localhost", "user", "pass", "database");
$stmt = $db -> prepare("SELECT priv FROM testUsers WHERE username=? AND password=?");
$stmt -> bind_param("ss", $user, $pass);
$stmt -> execute();
Trying to set this up now because it looks like a challenge is it likely to put more load on my server than a traditional query? I'm just debugging at the moment but eventually there's supposedly going to be quite heavy traffic once it goes live.
__________________
Quote:
Originally Posted by kevmif
Fucking hell... Batman can get ADSL and i'm stuck on dial up.
eBay FS: null | BakeOff Finalist 06 | Google it | 30+ successful trades for total >$1500
kthnx is offline   Reply With Quote
Old 21st April 2010, 1:11 PM   #9
mordy
Member
 
mordy's Avatar
 
Join Date: Aug 2001
Location: melb
Posts: 5,116
Default

Quote:
Originally Posted by kthnx View Post
Ohh I see! I knew something session related was stored at the client end (either in a cookie or the URL, depending on the browser) but that's just a key to access session variables, which are all stored server side?
yep, its like you ordering from a fast food place and being given token 13 ... the token is not your order, but you can go back to the counter to pick up your order with that token.

Similarly, on every request, the client sends his token to the server, which allows the server to keep track of data throughout the session. Each session cookie has a $_SESSION saved on the server. Otherwise the web would suck as it would be completely stateless ... think web 0.5

Last edited by mordy; 21st April 2010 at 1:15 PM.
mordy is offline   Reply With Quote
Old 22nd April 2010, 1:44 AM   #10
Ashpool
Member
 
Ashpool's Avatar
 
Join Date: Feb 2003
Location: Ye Olde Melbourne Town
Posts: 3,245
Default

Quote:
Originally Posted by kthnx View Post
Trying to set this up now because it looks like a challenge is it likely to put more load on my server than a traditional query? I'm just debugging at the moment but eventually there's supposedly going to be quite heavy traffic once it goes live.
In general parameterized queries are faster! Sometimes a lot faster!

Why? The theory is that the query is compiled only once and the server holds on to the compiled plan for multiple uses.

It's my understanding parameterized queries are the way to go and accepted as current best practices.

Looking back now yes it makes sense that the session variables are server side only. SQL injection is pretty common, imagine if hackers could modify the variables then we would have a shit load more of these attacks.

Last edited by Ashpool; 22nd April 2010 at 1:49 AM.
Ashpool is offline   Reply With Quote
Old 22nd April 2010, 12:17 PM   #11
mordy
Member
 
mordy's Avatar
 
Join Date: Aug 2001
Location: melb
Posts: 5,116
Default

Quote:
Originally Posted by Ashpool View Post
In general parameterized queries are faster! Sometimes a lot faster!

Why? The theory is that the query is compiled only once and the server holds on to the compiled plan for multiple uses.

It's my understanding parameterized queries are the way to go and accepted as current best practices.
It really depends what the search is "select * from table" is not gonna be faster ... it happens when there are complex joins, and the DBMS has to compile a path of execution ... the retrieval is only as good as the indexes and the way you do your joins and the DBMS etc.
mordy is offline   Reply With Quote
Old 22nd April 2010, 12:54 PM   #12
Luke212
Member
 
Join Date: Feb 2003
Location: NSW
Posts: 6,419
Default

Quote:
Originally Posted by mordy View Post
It really depends what the search is "select * from table" is not gonna be faster ... it happens when there are complex joins, and the DBMS has to compile a path of execution ... the retrieval is only as good as the indexes and the way you do your joins and the DBMS etc.
select * from table doesnt have any parameters so there is only one way to do it. parameters will virtually never be slower and there is no reason not to use them.
__________________
Democracy's greatest trick was convincing man he was informed.
Luke212 is offline   Reply With Quote
Old 22nd April 2010, 1:41 PM   #13
Elyzion
Member
 
Elyzion's Avatar
 
Join Date: Oct 2004
Location: Singapore
Posts: 6,255
Default

Quote:
Originally Posted by Ashpool View Post
In general parameterized queries are faster! Sometimes a lot faster!
They are only faster if the DBMS caches the query.

If you are using a DBMS that caches queries and you're not using parameterized queries then it compiles the query every time its executed. Which makes it slower.
__________________
Quote:
Originally Posted by SyN View Post
next time you see a dog, go up close to it and say loudly with furious anger 'ENGLISH MOTHERFUCKER, DO YOU SPEAK IT'
Elyzion is offline   Reply With Quote
Reply

Bookmarks

Sign up for a free OCAU account and this ad will go away!

Thread Tools

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 On
HTML code is Off

Forum Jump


All times are GMT +10. The time now is 2:36 AM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd. -
OCAU is not responsible for the content of individual messages posted by others.
Other content copyright Overclockers Australia.
OCAU is hosted by Internode!