Welcome to WebmasterWorld Guest from 54.227.5.198

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

preg replace enough to preventing SQL injection?

     

Kahless

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

10+ Year Member



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?

enigma1

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

WebmasterWorld Senior Member 5+ Year Member



You shouldn't need htmlentities as there will be no html to do anything with.

grallis

12:47 pm on May 13, 2009 (gmt 0)

5+ Year Member



Well it depends - how are you limiting the input to upper and lower case alpha chars only?

rocknbil

2:33 pm on May 13, 2009 (gmt 0)

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



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 . . .

Kahless

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

10+ Year Member




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]

rocknbil

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

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



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

Kahless

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

10+ Year Member




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]

 

Featured Threads

Hot Threads This Week

Hot Threads This Month