Jump to content

how can i improve this function?


Ayon

Recommended Posts

i have this line on the top of ever document to look for injection attempts...

<?php (safe_get()) ? exit() : false; ?>

 

but i'm sure there's some way to improve this to go even a little bit further

 

function safe_get()
{
if ($_GET) {
	foreach($_GET as $check) {
		$pattern = '/(DELETE|ALTER|SELECT|INSERT|UNION|CHAR|CONSTCHAR|INTO OUTFILE|DROP|TRUNCATE(.*))+/';
		if (!preg_match($pattern,$check)) { 
			$result = false;
		} else {
			$result = true;
			break;
		}
	}
	return $result;
}
}

 

Any suggestions are very much appreciated...

 

Thanks in advance :)

- Ayon

Link to comment
Share on other sites

So I assume you're not ever posting SQL statements, or if you are you are only doing it through POST?  You should also clean POST input, because someone can inject via POST just as easily as they can into GET.

Link to comment
Share on other sites

yeah i know it's just as easy to send injections trough $_POST as it is trough $_GET...

 

but the one i posted there is only for the $_GET, and no I never send any SQL statements trough the $_GET :) Is it secure enough or am I missing something?

 

as for the $_POST i only know about the htmlspecialchars() and mysql_real_escape_string()... is there anything else i should do besides those?

 

Thanks for the reply :)

Link to comment
Share on other sites

Think about this:

 

SELECT * FROM users WHERE name = '{$name}';

 

 

Now, for $name to be dangerous, it would have to terminate the string encapsulation, yes?

 

The only way for that to happen is a ' character (over simplifying and lying, but not important here).

 

 

So, as long as ' is not present, unescaped, the variable is safe.

 

mysql_real_escape_string is the only thing needed to make a string safe (edit: for inserting into a MySQL database).  I don't know what this scanning for DELETE and what not non sense is.

 

 

 

 

But yes, if you're trying to detect, not prevent, then you should be fine.  You aren't doing anything when you do detect them though, so I don't see the point.

 

 

 

Also, that function would be easy to get around.  It would not get "delete" since only "DELETE" is matched.  (Perhaps use a i modifier?)

 

 

Also, the function does not recursively check the GET array.  What if there is a sub array element that is unsafe?

Link to comment
Share on other sites

I use a data cage class to sanitize any user submitted data...

 

However your situation is better solved using the pear DB package which does its magic to prevent sql with minimal effort.

 

it isn't efficient to run this on every page and on every variable passed to the page...

 

you will be far better off just using mysql_real_escape_string on the variables when you submit them to the query...

 

One point to note there is that utf-8 codes could be in the string and when those are submitted they are evaluated so they will allow an injection attack http://ilia.ws/archives/103-mysql_real_escape_string-versus-Prepared-Statements.html

 

 

 

Link to comment
Share on other sites

Also, that function would be easy to get around.  It would not get "delete" since only "DELETE" is matched.  (Perhaps use a i modifier?)

 

Also, the function does not recursively check the GET array.  What if there is a sub array element that is unsafe?

 

Hmm, I thought only uppercase commands were used, but i guess i'm wrong then :) i modifier added.

 

But what you mean with sub array element? could you give a quick example?

 

I use a data cage class to sanitize any user submitted data...

 

However your situation is better solved using the pear DB package which does its magic to prevent sql with minimal effort.

 

it isn't efficient to run this on every page and on every variable passed to the page...

 

you will be far better off just using mysql_real_escape_string on the variables when you submit them to the query...

 

One point to note there is that utf-8 codes could be in the string and when those are submitted they are evaluated so they will allow an injection attack http://ilia.ws/archives/103-mysql_real_escape_string-versus-Prepared-Statements.html

 

I guess you're talking about $_POST in your reply right? Atleast I can't see any $_GET relevant info :P might be me beeing stupid tho hehe :)

 

And thanks for the links.. Will read and see what I understand :P You might get a couple of questions tho :) hehe

 

 

 

@both of you..thanks for great replies! Very much appreciated!

Link to comment
Share on other sites

could you explain the concept about a data cage?

 

and also another question... i have made a fully custom mysql class running baisicly everything trough my own object's and methods... now i just took a really quick look at mysqli... is that baisicly a mysql object only pre-made kinda like a framework?

 

probably a noobish question :P hehe

Link to comment
Share on other sites

Also, that function would be easy to get around.  It would not get "delete" since only "DELETE" is matched.  (Perhaps use a i modifier?)

 

Also, the function does not recursively check the GET array.  What if there is a sub array element that is unsafe?

 

Hmm, I thought only uppercase commands were used, but i guess i'm wrong then :) i modifier added.

 

But what you mean with sub array element? could you give a quick example?

 

I use a data cage class to sanitize any user submitted data...

 

However your situation is better solved using the pear DB package which does its magic to prevent sql with minimal effort.

 

it isn't efficient to run this on every page and on every variable passed to the page...

 

you will be far better off just using mysql_real_escape_string on the variables when you submit them to the query...

 

One point to note there is that utf-8 codes could be in the string and when those are submitted they are evaluated so they will allow an injection attack http://ilia.ws/archives/103-mysql_real_escape_string-versus-Prepared-Statements.html

 

I guess you're talking about $_POST in your reply right? Atleast I can't see any $_GET relevant info :P might be me beeing stupid tho hehe :)

 

And thanks for the links.. Will read and see what I understand :P You might get a couple of questions tho :) hehe

 

 

 

@both of you..thanks for great replies! Very much appreciated!

 

 

Traditionally and syntactically, only upper case commands are used, but every SQL dialect I've ever come across hasn't cared about case.

 

 

 

As the array thing:

 

somepage.php?something[]=DELETE FROM table;

Link to comment
Share on other sites

well i'm rewriting my mysql object now to work with prepared statements... haven't been doing this for to long. atleast not heavy stuff like this..

 

so my code is starting to get a little bit confusing now that i've passed 300 lines XD but i think i'm getting there :) very happy i'm solid on arrays :P

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.