Jump to content

[SOLVED] How to stop this injection?


rondog

Recommended Posts

I am building a site and using $_GET to populate some pages.

 

/index.php?do=video&id=1,12

 

If I were to type in the address bar:

 

/index.php?do=video&id=1,' OR id = '12

 

It does practically the same thing!

 

Now I know the example I provided wont really do any harm, but it opens the door to many more threats.

 

How can I trim my id to only contain numbers since thats what really all will ever be in the id?

Link to comment
Share on other sites

How can I trim my id to only contain numbers since thats what really all will ever be in the id?

 

But your first example contains a comma?

 

right yes..numbers and commas I guess hah..instead of doing something like id1=1&id2=12 I am using the commas and exploding them to an array.

Link to comment
Share on other sites

<?php
$id = $_GET["id"];
if(preg_match("/^[0-9]+$/", $id) > 0){
// mysql queries and shit
} else {
echo "errors";
}
?>

 

cool man that seems to be working. Two questions, where in their is it allowing commas and what is the $ sign for?

Link to comment
Share on other sites

^/[\d,]/$

 

Oddly enough, I cringe at this :-P

 

I'm not exactly sure why the ^ and $ are on the outsides, or even why they belong in this RegEx.

 

Are you trying to match anything that starts with EITHER a comma or a digit?

/^[\d,]/

 

Are you trying to see if there is anything that is not a comma OR digit?

/[^\d,]*/

(the * meaning 0 or more times, so if there is at least one character that is not a comma or digit, it will match)

Link to comment
Share on other sites

basically my links can route to three different areas

 

index.php?do=video&id=1

index.php?do=video&id=1,7

index.php?do=video&id=1,7,17

 

those numbers just being examples..I need to make sure that only numbers and commas are in the id string in the address bar.

Link to comment
Share on other sites

Use a preg_match function. it returns either 0/1 (respectively meaning the number of matches at minimum). If it cannot find a match, it returns 0, otherwise 1 (it stops after finding one match).

 

$pattern = "/[^\d,]*/";//this will look for anything that is NOT a digit OR a comma
if(!preg_match($pattern,$_GET["id"])){
die('OMFG! Someone just tried to hax0r j00!');
//you have at least one match
}

 

Just insert that die function, if something's wrong, it stops the script right then and there, otherwise, it's all ez squeezy lemon peasy o_o (to quote someone)

Link to comment
Share on other sites

I respect your guys work and time on helping me out, but what is the best one to use?

 

dezkit's: "/^[0-9\,]+$/"

 

or

 

kratsg's: "/[^\d,]*/"

 

They look similar, but I am having a hard time grasping what these mean..I am thinking the \d means digit so maybe its the same as 0-9?

 

Link to comment
Share on other sites

\d is called a character class: http://evolt.org/RegEx_Basics?from=50&comments_per_page=50

 

\d  matches a digit, same as [0-9]

\D matches a non-digit, same as [^0-9]

\s matches a whitespace character (space, tab, newline, etc.)

\S matches a non-whitespace character

\w matches a word character

\W matches a non-word character

\b matches a word-boundary (NOTE: within a class, matches a backspace)

\B matches a non-wordboundary

Link to comment
Share on other sites

\d is called a character class: http://evolt.org/RegEx_Basics?from=50&comments_per_page=50

 

\d  matches a digit, same as [0-9]

\D matches a non-digit, same as [^0-9]

\s matches a whitespace character (space, tab, newline, etc.)

\S matches a non-whitespace character

\w matches a word character

\W matches a non-word character

\b matches a word-boundary (NOTE: within a class, matches a backspace)

\B matches a non-wordboundary

 

Ahh very helpful man. I think that link could help me out. I appreciate it guys thanks!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.