Welcome to the dBforums forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions, articles and access our other FREE features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload your own photos and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact support.

If you prefer not to see double-underlined words and corresponding ads, place your cursor
here for ContentLink opt out.

Go Back  dBforums > Data Access, Manipulation & Batch Languages > PHP > SQL Injection ...

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 11-01-07, 20:14
igordonin igordonin is offline
Registered User
 
Join Date: Jul 2006
Posts: 51
SQL Injection ...

Am I protected from SQL Injection by using such a function *only* ?
Should I be using htmlentities also ?

PHP Code:
function validate_input($input) {

    
# Insert the junk
    
$junk = array("select""insert""update""delete""drop"";""--""xp_""*""'"'"'"truncate""schema.");
    
    
# Searchs for invalid inputs in the string and replaces them with empties
    
for ($junk_i 0$junk_i count($junk); $junk_i++) {
        
$input str_ireplace($junk[$junk_i], ""$input); 
    }

    
# Returns the validated string
    
return $input;



The "schema" array value is the Oracle Database schema name I use.

Thanks in advance for any advice.

Last edited by igordonin : 11-01-07 at 20:19.
Reply With Quote
  #2 (permalink)  
Old 11-01-07, 21:14
georgev georgev is offline
SQL Apprentice
 
Join Date: Jan 2007
Location: hiding
Posts: 8,145
What about comments such as
Code:
/*This is a comment */
And they keyword "EXEC" / "EXECUTE"?
__________________
George
You only stop learning when you stop asking questions.
Reply With Quote
  #3 (permalink)  
Old 11-03-07, 04:40
sco08y sco08y is offline
Registered User
 
Join Date: Oct 2002
Location: Fort Polk, LA
Posts: 500
Even if that does protect you, it also destroys perfectly valid input.

Your input is either going to be a string or a non-string value that matches a regular expression. For example, integers should always match /[+-]\d+/. PHP probably has what you need built in.

Strings are easy to escape in SQL. I'm not a PHP guy, but it's as simple as:

Code:
"'" . str_replace("'", "''", $input) . "'"

Now you've got a quoted string. But don't take my word for it, read the spec.

Code:
<character string literal> ::= [ <introducer> <character set specification> ] <quote> [ <character representation> ... ] <quote> [ { <separator> ... <quote> [ <character representation> ... ] <quote> }... ] <quote> ::= ' <character representation> ::= <nonquote character> | <quote symbol> <nonquote characte r> ::= !! See the Syntax rules <quote symbol> ::= <quote> <quote> <separator> ::= { <comment> | <space> | <newline> }... Syntax Rules 1) In a <character string literal> or <national character string literal>, the sequence: <quote> <character representation>... <quote> <separator>... <quote> <character representation>... <quote> is equivalent to the sequence <quote> <character representation>... <character representa- tion>... <quote> Note: The <character representation>s in the equivalent se- quence are in the same sequence and relative sequence as in the original <character string literal>.

Basically, it's saying two things: you can represent a quote with two quotes. (That's the quote symbol rule.) And that if you have two strings next to each other, like 'foo' 'bar' it's the same as saying 'foobar'. This is the way it works in C and other languages, too. (Well, there's more stuff about choosing character sets, but I can't imagine that would work very well...)

Also note that there's no reason that 'a string on two lines

doesn''t work just fine'. And having keywords inside the quote marks doesn't bother SQL in the least... so long as it's properly quoted, it gets turned into a character string literal token by the lexer.
Reply With Quote
  #4 (permalink)  
Old 11-03-07, 04:42
sco08y sco08y is offline
Registered User
 
Join Date: Oct 2002
Location: Fort Polk, LA
Posts: 500
Quote:
Originally Posted by igordonin
Should I be using htmlentities also ?

No. Stop trying to do shotgun programming. HTML entities are for HTML, not SQL.
Reply With Quote
  #5 (permalink)  
Old 11-05-07, 10:02
igordonin igordonin is offline
Registered User
 
Join Date: Jul 2006
Posts: 51
Thank you very much for your help.


Cheers
Reply With Quote
  #6 (permalink)  
Old 11-05-07, 14:21
Frunkie Frunkie is offline
Gives Bad Advice
 
Join Date: Mar 2007
Location: 010101010110100
Posts: 706
Quote:
Originally Posted by sco08y
No. Stop trying to do shotgun programming. HTML entities are for HTML, not SQL.
Hang on there Mister.. There is absolutley nothing wrong with filtering input with htmlentities(). I believe what he is actually looking for though is htmlspecialchars().
__________________
I and many others around the world are of the strong belief that the universe was created by the Flying Spaghetti Monster. It was He who created all that we see and all that we feel. We feel strongly that the overwhelming scientific evidence pointing towards evolutionary processes is nothing but a coincidence, put in place by Him.
Reply With Quote
  #7 (permalink)  
Old 11-11-07, 01:04
sco08y sco08y is offline
Registered User
 
Join Date: Oct 2002
Location: Fort Polk, LA
Posts: 500
Quote:
Originally Posted by fjm1967
Hang on there Mister.. There is absolutley nothing wrong with filtering input with htmlentities(). I believe what he is actually looking for though is htmlspecialchars().

Are you sure you don't mean filtering output?
Reply With Quote
  #8 (permalink)  
Old 12-03-07, 09:40
RBARAER RBARAER is offline
Registered User
 
Join Date: Aug 2004
Location: France
Posts: 754
If you are using Oracle as it seems to be, just use bind variables and you will avoid SQL injection .

http://asktom.oracle.com/pls/asktom/...23863706595353

For even better Oracle programming, use PLSQL stored procedures within packages and there you go .

If you want some more details, feel free to ask.

Regards,

rbaraer
__________________
ORA-000TK : No bind variable detected... Shared Pool Alert code 5 - Nuclear query ready .
Reply With Quote
  #9 (permalink)  
Old 12-03-07, 09:59
Genx Genx is offline
Registered User
 
Join Date: Nov 2007
Posts: 14
You can really use bind variables and prepared statements with most db systems if you have PHP 5, the PDO extension and the necessary adapter installed ... unless it ships with the core install these days.

Probably the best way of preventing sql injection and dealing with sql in php in general.
Reply With Quote
  #10 (permalink)  
Old 12-03-07, 11:23
RBARAER RBARAER is offline
Registered User
 
Join Date: Aug 2004
Location: France
Posts: 754
Quote:
Originally Posted by Genx
Probably the best way of preventing sql injection and dealing with sql in php in general.
Absolutely
__________________
ORA-000TK : No bind variable detected... Shared Pool Alert code 5 - Nuclear query ready .
Reply With Quote
  #11 (permalink)  
Old 12-15-07, 05:36
rajesh_r_r rajesh_r_r is offline
Registered User
 
Join Date: Jan 2004
Location: India
Posts: 168
htmlspecialchars() is also one of the methods used to avoid SQL injection.
__________________
Freelance and Technology Consultant
-------------------
Dreams are for ever
-------------------
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On