Cronwerks MCCode/MCCodes Forums

Please login or register.

Login with username, password and session length

News:

Have errors in your coding or want something specific added to your game? Check out our paid support here.


This forum is now closed and has moved to a new location! Click here to find out why.
Pages: [1] 2

AuthorTopic: Anti-SQL injection function  (Read 3841 times)

Arson

  • Global Moderator
  • Basic Member
  • *****
  • Reputation Power: 99
  • Arson barely matters.Arson barely matters.
  • Offline Offline
  • Posts: 75
    • View Profile
Anti-SQL injection function
« on: May 13, 2009, 01:06:50 AM »
This function will automatically secure all $_POST and $_GET variables in your mccodes v1 or v2 game with ease.

For MCCodes v2 you will put this function in globals.php (i think, or some other thing that loads on EVERY page of your game)
For MCCodes v1 you will put this function in header.php inside the startheaders function.

Code: [Select]

function anti_inject($campo)
{
    foreach($campo as $key => $val)
    {
        //remove words that contains syntax sql
        $val = preg_replace(sql_regcase("/(from|select|insert|delete|where|drop table|show tables|#|\*|--|\\\\)/"),"",$val);

        //Removes tags html/php
        $val = strip_tags($val);

        //Add slashes
        $val = addslashes($val);

        // store it back into the array
        $campo[$key] = $val;
    }
    return $campo; //Returns the the var clean


//the next two lines make sure all post and get vars are filtered through this function
$_POST = anti_inject($_POST);
$_GET = anti_inject($_GET);


Note that this just stops members from injecting through the variables, if the variable is supposed to be a number, you will still need to do something like:
Code: [Select]
$_POST['variable'] = abs($_POST['variable']);
That will keep members from being able to put numbers like 20E+20.
Logged
List of the Best Text Games on the Interwebs!
If you own a game, you need to list it on BestTextGames.com

Miniman

  • Basic Member
  • *
  • Reputation Power: 1
  • Miniman has no influence.
  • Offline Offline
  • Posts: 7
    • View Profile
Re: Anti-SQL injection function
« Reply #1 on: May 13, 2009, 01:45:18 AM »
What is it with people taking the easy way out?
Why don't you just secure every page? There are plenty of help sites out there...
Logged

Arson

  • Global Moderator
  • Basic Member
  • *****
  • Reputation Power: 99
  • Arson barely matters.Arson barely matters.
  • Offline Offline
  • Posts: 75
    • View Profile
Re: Anti-SQL injection function
« Reply #2 on: May 13, 2009, 01:47:20 AM »
What is it with people taking the easy way out?
Why don't you just secure every page? There are plenty of help sites out there...

That doesn't really make a lot of sense.
The less code you have packed into your scripts the better.

Also, why would you do something the hard way if there is an easy way that is more efficient?
My way, if you make something and forget to secure a variable, no biggie, its already secure.
Logged
List of the Best Text Games on the Interwebs!
If you own a game, you need to list it on BestTextGames.com

Miniman

  • Basic Member
  • *
  • Reputation Power: 1
  • Miniman has no influence.
  • Offline Offline
  • Posts: 7
    • View Profile
Re: Anti-SQL injection function
« Reply #3 on: May 13, 2009, 01:53:07 AM »
But it's not secure, fair enough it may be a tiny bit of an adjustment to the code that could work if you had a 5 year old rooting around for some fun on your site.
But, Dropping tables and manipulating the WHERE clause isn't the only thing you can do, or gain access with. You need to escape some of the data being input not just match it and make sure there is no certain words in there :)
Logged

Arson

  • Global Moderator
  • Basic Member
  • *****
  • Reputation Power: 99
  • Arson barely matters.Arson barely matters.
  • Offline Offline
  • Posts: 75
    • View Profile
Re: Anti-SQL injection function
« Reply #4 on: May 13, 2009, 01:55:08 AM »
Code: [Select]


function anti_inject($campo)
{
    foreach($campo as $key => $val)
    {
        //escape the var
        $val = mysql_real_escape_string($val);

        //Removes tags html/php
        $val = strip_tags($val);

        //Add slashes
        $val = addslashes($val);

        // store it back into the array
        $campo[$key] = $val;
    }
    return $campo; //Returns the the var clean
}

//the next two lines make sure all post and get vars are filtered through this function
$_POST = anti_inject($_POST);
$_GET = anti_inject($_GET);


Yeah I noticed that Miniman.
Fixed.
Logged
List of the Best Text Games on the Interwebs!
If you own a game, you need to list it on BestTextGames.com

Arson

  • Global Moderator
  • Basic Member
  • *****
  • Reputation Power: 99
  • Arson barely matters.Arson barely matters.
  • Offline Offline
  • Posts: 75
    • View Profile
Re: Anti-SQL injection function
« Reply #5 on: May 13, 2009, 01:57:11 AM »
You could also probably just do this:

Code: [Select]
function anti_inject($campo)
{
    foreach($campo as $key => $val)
    {
        $val = mysql_real_escape_string($val);
        // store it back into the array
        $campo[$key] = $val;
    }
    return $campo; //Returns the the var clean
}

//the next two lines make sure all post and get vars are filtered through this function
$_POST = anti_inject($_POST);
$_GET = anti_inject($_GET);
Logged
List of the Best Text Games on the Interwebs!
If you own a game, you need to list it on BestTextGames.com

Miniman

  • Basic Member
  • *
  • Reputation Power: 1
  • Miniman has no influence.
  • Offline Offline
  • Posts: 7
    • View Profile
Re: Anti-SQL injection function
« Reply #6 on: May 13, 2009, 01:57:32 AM »
htmlentities()

I think it could be used as a nice reassurance, like you said if I missed something then this would still be there working in the background.
Also good for someone who doesn't know how to secure their scripts  :P
Logged

Arson

  • Global Moderator
  • Basic Member
  • *****
  • Reputation Power: 99
  • Arson barely matters.Arson barely matters.
  • Offline Offline
  • Posts: 75
    • View Profile
Re: Anti-SQL injection function
« Reply #7 on: May 13, 2009, 01:58:31 AM »
Any way you want to do it.
I'm just trying to help some people out.
Logged
List of the Best Text Games on the Interwebs!
If you own a game, you need to list it on BestTextGames.com

Miniman

  • Basic Member
  • *
  • Reputation Power: 1
  • Miniman has no influence.
  • Offline Offline
  • Posts: 7
    • View Profile
Re: Anti-SQL injection function
« Reply #8 on: May 13, 2009, 02:00:40 AM »
function anti_inject($campo)
{
   return htmlentities(mysql_real_escape_string(trim($campo)));
}

$_POST = anti_inject($_POST);
$_GET = anti_inject($_GET);
Logged

Arson

  • Global Moderator
  • Basic Member
  • *****
  • Reputation Power: 99
  • Arson barely matters.Arson barely matters.
  • Offline Offline
  • Posts: 75
    • View Profile
Re: Anti-SQL injection function
« Reply #9 on: May 13, 2009, 02:01:17 AM »
 ;)
Logged
List of the Best Text Games on the Interwebs!
If you own a game, you need to list it on BestTextGames.com

Miniman

  • Basic Member
  • *
  • Reputation Power: 1
  • Miniman has no influence.
  • Offline Offline
  • Posts: 7
    • View Profile
Re: Anti-SQL injection function
« Reply #10 on: May 13, 2009, 02:08:28 AM »
There we go, Now we have something  ::)
Logged

Miniman

  • Basic Member
  • *
  • Reputation Power: 1
  • Miniman has no influence.
  • Offline Offline
  • Posts: 7
    • View Profile
Re: Anti-SQL injection function
« Reply #11 on: May 13, 2009, 02:14:05 AM »
I just tried it, and they both failed to do anything (used on well known injections)
Might need urlencode()

function anti_inject($campo)
{
    foreach($campo as $key => $val)
    {
        $val = mysql_real_escape_string(htmlentities(urlencode($val)));
        // store it back into the array
        $campo[$key] = $val;
    }
    return $campo; //Returns the the var clean
}

//the next two lines make sure all post and get vars are filtered through this function
$_POST = anti_inject($_POST);
$_GET = anti_inject($_GET)



That works :)
Logged

strats

  • Active Member
  • **
  • Reputation Power: 13
  • strats has no influence.
  • Offline Offline
  • Posts: 109
    • View Profile
Re: Anti-SQL injection function
« Reply #12 on: May 13, 2009, 04:13:41 AM »
You sure? lol
And that's the code to put in globes?
Where is the best place for me to place it?
top of the page?
Logged

Arson

  • Global Moderator
  • Basic Member
  • *****
  • Reputation Power: 99
  • Arson barely matters.Arson barely matters.
  • Offline Offline
  • Posts: 75
    • View Profile
Re: Anti-SQL injection function
« Reply #13 on: May 13, 2009, 04:44:42 AM »
You sure? lol
And that's the code to put in globes?
Where is the best place for me to place it?
top of the page?

This works.

Code: [Select]
function anti_inject($campo)
{
    foreach($campo as $key => $val)
    {
        $val = mysql_real_escape_string($val);
        // store it back into the array
        $campo[$key] = $val;
    }
    return $campo; //Returns the the var clean
}

//the next two lines make sure all post and get vars are filtered through this function
$_POST = anti_inject($_POST);
$_GET = anti_inject($_GET);

put it by itself, not in something elses brackets. in globals.php should be fine, u can put it at the very bottom of the script right before ?> if that makes it easier :)

I wouldnt use miniman's thing since it has urlencode in it which changes spaces to + signs. you would have to change the plus signs back into spaces before doing the query i think..which is pointless.
mysql_real_escape_string has always worked for me.
Logged
List of the Best Text Games on the Interwebs!
If you own a game, you need to list it on BestTextGames.com

Miniman

  • Basic Member
  • *
  • Reputation Power: 1
  • Miniman has no influence.
  • Offline Offline
  • Posts: 7
    • View Profile
Re: Anti-SQL injection function
« Reply #14 on: May 13, 2009, 09:30:28 AM »
Worked for me with no 1 test ;)
BUT,  I will figure something better :)
Logged
Pages: [1] 2
« previous next »
 


This forum is now closed and has moved to a new location! Click here to find out why.