Welcome to WebmasterWorld Guest from 23.22.140.143

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

preg replace enough to preventing SQL injection?

     
3:38 am on May 13, 2009 (gmt 0)

Junior Member

10+ Year Member

joined:Oct 25, 2004
posts:86
votes: 0


I have form that only allows upper and lower case letters. So I use preg_replace to error out on anything other than a-zA-Z and use mysql_real_escape to query the database.

Am I missing something here or is htmlentities still needed?

9:46 am on May 13, 2009 (gmt 0)

Senior Member

WebmasterWorld Senior Member 5+ Year Member

joined:Apr 30, 2007
posts:1394
votes: 0


You shouldn't need htmlentities as there will be no html to do anything with.
12:47 pm on May 13, 2009 (gmt 0)

Junior Member

5+ Year Member

joined:Sept 29, 2008
posts: 52
votes: 0


Well it depends - how are you limiting the input to upper and lower case alpha chars only?
2:33 pm on May 13, 2009 (gmt 0)

Senior Member

WebmasterWorld Senior Member rocknbil is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Nov 28, 2004
posts:7999
votes: 0


I use preg_replace to error out on anything other than a-zA-Z and use mysql_real_escape to query the database.

Of course you are also including regular punctuation and spaces?

What about this, for example?

yourscript.php?user_id=3273%20OR%201=1

If properly crafted,

select username,password from user_table where user=3273 or 1=1

Since 1=1 is always true, this simple example would display all user names and passwords in the database.

Before completely cleansing you should look for patterns used to inject commands into your input fields, including but not limited to drop, insert, update, or, and . . .

12:23 am on May 14, 2009 (gmt 0)

Junior Member

10+ Year Member

joined:Oct 25, 2004
posts:86
votes: 0



Since 1=1 is always true, this simple example would display all user names and passwords in the database.

But only if I allow the equal = sign, correct?

preg_replace('/[^a-zA-Z0-9\-\_\!\$\#\@\^\&\*\(\)\^\+\ \.\?]/'

[edited by: Kahless at 12:29 am (utc) on May 14, 2009]

3:51 pm on May 14, 2009 (gmt 0)

Senior Member

WebmasterWorld Senior Member rocknbil is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Nov 28, 2004
posts:7999
votes: 0


The equal sign can be encoded, but yeah that should stop that attack . . .but there are many others, as mentioned. If they can figure out your database table names, what about

delete from table
drop table
insert into table

8:31 pm on May 14, 2009 (gmt 0)

Junior Member

10+ Year Member

joined:Oct 25, 2004
posts:86
votes: 0



The equal sign can be encoded, but yeah that should stop that attack . . .but there are many others, as mentioned. If they can figure out your database table names, what about
delete from table
drop table
insert into table

That is what I do not understand. How could they possibly get by that preg_replace and mysql_real_escape? Unless your thinking is space and quotes were being allowed. But even if that were case I believe mysql_real_escape would still stop that from happening, correct?

[edited by: Kahless at 8:32 pm (utc) on May 14, 2009]

 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members