Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
$_REQUEST filtering for Preventing URL Attacks.!!
Old 05-25-2009, 08:34 AM $_REQUEST filtering for Preventing URL Attacks.!!
Junior Talker

Posts: 1
Name: PoliteBoy
Trades: 0
While reading a source code, a script which was vulnerable to SQL injection attacks. My code is :

PHP Code:
<?
<?        
$en $_REQUEST;

        
$req $en["req"];
if(
$req "search"){
print 
"query";
}
?>
The thing i want is that, only specific characters like, a-z , 0-9 and [] are allowed and nothing else character should be allowed when someone access http://www.mysite.com/index.php?req=search , here if some user enters http://www.mysite.com/index.php?req=1' , then it gives up error like you have an error in your mysql ......... , i want to remove all other characters to be processed.

Any help will be appreciated allot.!!

Best Regards:

PoliteBoy!!
politeboy is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 05-25-2009, 10:29 AM Re: $_REQUEST filtering for Preventing URL Attacks.!!
Extreme Talker

Posts: 181
Name: David Jackson
Trades: 0
PHP Code:
<?php
 
$en 
$_REQUEST;
 
if(
ctype_digit($en['req']) || ctype_alpha('req')){
 
/// its clean
 
}else{
 
/// its dodgy
 
}
 
?>
__________________

Please login or register to view this content. Registration is FREE
davidj is offline
Reply With Quote
View Public Profile
 
Old 05-25-2009, 10:43 AM Re: $_REQUEST filtering for Preventing URL Attacks.!!
wayfarer07's Avatar
Poo on You

Latest Blog Post:
Introducing WowWindow
Posts: 3,987
Name: Abel Mohler
Location: Asheville, North Carolina USA
Trades: 0
mysql_real_escape_string()
__________________
I build web things. I work for the startup
Please login or register to view this content. Registration is FREE
.
wayfarer07 is online now
Reply With Quote
View Public Profile Visit wayfarer07's homepage!
 
Old 05-26-2009, 07:49 AM Re: $_REQUEST filtering for Preventing URL Attacks.!!
EdB
Skilled Talker

Posts: 79
Name: Ed Barnett
Trades: 0
Both posts include things you should look at using in conjunction with any database work you do. However, I believe what you want is to use Regular Expressions (RegEx) to determine whether or not someone has used illegal characters in your variables (you could use 'ctype_alnum()' if you didn't want the square brackets to be legal characters []) .

In ctype_alnum you are only allowed 0-9, a-z and A-Z. Anything other than this (even a blank space --> <--) is illegal and will cause the function to return false.

However, if you NEED to have square brackets in the variables then it is best you use regular expression. This is something that is common to most languages though sadly it's not my strong point. The website below will help detail how it works;

http://www.regular-expressions.info/

You would then need to use the PHP ereg() function to search a string for illegal characters.

Anyone else...?
__________________

Please login or register to view this content. Registration is FREE

Last edited by EdB; 05-26-2009 at 07:53 AM..
EdB is offline
Reply With Quote
View Public Profile Visit EdB's homepage!
 
Old 05-26-2009, 07:51 AM Re: $_REQUEST filtering for Preventing URL Attacks.!!
EdB
Skilled Talker

Posts: 79
Name: Ed Barnett
Trades: 0
Ps. Don't use the $_REQUEST array unless you seriously have to. I've been coding PHP for over a year and have only used it ONCE (and that was to give some flexibility).

If you are expecting a variable to come from the query string then use the $_GET array. If you are expecting a variable from a form submission then use the $_POST array.

It'll lead to much more secure programming.
__________________

Please login or register to view this content. Registration is FREE
EdB is offline
Reply With Quote
View Public Profile Visit EdB's homepage!
 
Old 05-26-2009, 08:04 AM Re: $_REQUEST filtering for Preventing URL Attacks.!!
wayfarer07's Avatar
Poo on You

Latest Blog Post:
Introducing WowWindow
Posts: 3,987
Name: Abel Mohler
Location: Asheville, North Carolina USA
Trades: 0
You are all reading way too deeply into this. If a $_GET var is supposed to be a number, all you need to do is clean it with intval:

website.com?page=1
PHP Code:
$clean_page intval($_GET["page"]); 
If a variable is supposed to be a string, all you need to do is escape it with mysql_real_escape_string:

website.com?page=fancy_name
PHP Code:
$clean_page mysql_real_escape_string($_GET["page"]); 
Now, either example is safe to append to a SQL query.
__________________
I build web things. I work for the startup
Please login or register to view this content. Registration is FREE
.

Last edited by wayfarer07; 05-26-2009 at 08:05 AM..
wayfarer07 is online now
Reply With Quote
View Public Profile Visit wayfarer07's homepage!
 
Old 05-26-2009, 08:10 AM Re: $_REQUEST filtering for Preventing URL Attacks.!!
EdB
Skilled Talker

Posts: 79
Name: Ed Barnett
Trades: 0
Security in depth via redundant safe guards - it's never a bad thing )
__________________

Please login or register to view this content. Registration is FREE
EdB is offline
Reply With Quote
View Public Profile Visit EdB's homepage!
 
Old 05-26-2009, 08:19 AM Re: $_REQUEST filtering for Preventing URL Attacks.!!
wayfarer07's Avatar
Poo on You

Latest Blog Post:
Introducing WowWindow
Posts: 3,987
Name: Abel Mohler
Location: Asheville, North Carolina USA
Trades: 0
sure, but there is no way past intval... Either its a number, or nothing. And I'm not aware of an vulnerabilities in the latter function either. Why do all the heavy lifting? Chances are greater that you'll miss something.

Of course, this is coming from a guy who builds his own encryption functions ... ... so I may be a lousy example
__________________
I build web things. I work for the startup
Please login or register to view this content. Registration is FREE
.

Last edited by wayfarer07; 05-26-2009 at 08:20 AM..
wayfarer07 is online now
Reply With Quote
View Public Profile Visit wayfarer07's homepage!
 
Reply     « Reply to $_REQUEST filtering for Preventing URL Attacks.!!
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.42103 seconds with 12 queries