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
https://forums.phpfreaks.com/topic/121772-solved-how-to-stop-this-injection/
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.

^/[\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)

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.

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)

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?

 

\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

\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!

Archived

This topic is now archived and is closed to further replies.

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