[PHP] Anti SQL Injection Script

Page 1 of 3 123 LastLast
Results 1 to 25 of 68
  1. #1
    Account Upgraded | Title Enabled! [N]asser is offline
    MemberRank
    Nov 2004 Join Date
    1,040Posts

    [PHP] Anti SQL Injection Script

    Well, to start things off, this is a modded version of one that was posted by someone else...I forgot his name, but if you search for it, I'm sure you could find it. I've added more things to it to make it more secure and reliable. Here they are:

    PHP Code:
    // Anti-SQL Injection
    function check_inject()
      {
        
    $badchars = array(";""'""\"""*""DROP""SELECT""UPDATE""DELETE""-");
      
        foreach(
    $_POST as $value)
        {
          if(
    in_array($value$badchars))
          {
            die(
    "SQL Injection Detected\n<br />\nIP: ".$_SERVER['REMOTE_ADDR']);
          **
          else
          {
            
    $check preg_split("//"$value, -1PREG_SPLIT_OFFSET_CAPTURE);
            foreach(
    $check as $char)
            {
              if(
    in_array($char$badchars))
              {
                die(
    "SQL Injection Detected\n<br />\nIP: ".$_SERVER['REMOTE_ADDR']);
              **
            **
          **
        **
      ** 
    This also works faster by a few mili seconds. This is the first of many PHP releases that I will be making to RaGEZONE. I use this very same script on the KolieMU site (soon to come). I hope you enjoy it.

    [N]asser` ~ Out
    Last edited by [N]asser; 05-06-05 at 08:48 PM. Reason: Added a new thing


  2. #2
    Account Upgraded | Title Enabled! Dorin1 is offline
    MemberRank
    Apr 2005 Join Date
    LondonLocation
    215Posts
    wow tnx

  3. #3
    Account Upgraded | Title Enabled! [N]asser is offline
    MemberRank
    Nov 2004 Join Date
    1,040Posts
    No problem.

    [N]asser` ~ Out

  4. #4
    Account Upgraded | Title Enabled! [N]asser is offline
    MemberRank
    Nov 2004 Join Date
    1,040Posts
    I've also newly added a new character into the array..."-". If you have this script check for injection on your stats adder, it should stop the - bug.

    [N]asser` ~ Out

  5. #5
    Account Upgraded | Title Enabled! DataMatrix is offline
    MemberRank
    Aug 2004 Join Date
    Liverpool, EnglandLocation
    659Posts
    I don't know why people continue using this type of anti-sql-inject, I use this:
    Code:
    if ((eregi("[^a-zA-Z0-9_-]", $memb___id)) || (eregi("[^a-zA-Z0-9_-]", $memb__pwd))) {
    	echo("SQL Injection Detected");
    **
    Last edited by DataMatrix; 05-06-05 at 09:23 PM.

  6. #6
    Account Upgraded | Title Enabled! themad is offline
    MemberRank
    Dec 2004 Join Date
    BulgariaLocation
    1,018Posts
    Quote Originally Posted by DataMatrix
    I don't know why people continue using this type of anti-sql-inject, I use this:
    Code:
    if ((eregi("[^a-zA-Z0-9_-]", $memb___id)) || (eregi("[^a-zA-Z0-9_-]", $memb__pwd))) {
    	echo("SQL Injection Detected");
    **
    hahhahaha hahahaha that Echo will not protect a thing :)

  7. #7
    Account Upgraded | Title Enabled! Nemesiz is offline
    MemberRank
    Mar 2004 Join Date
    LithuaniaLocation
    204Posts
    we can change echo("SQL Injection Detected"); to die("SQL Injection Detected");

  8. #8
    Apprentice messmaker is offline
    MemberRank
    Apr 2005 Join Date
    21Posts
    But i Didnt Understand How i put this :( Where need to do somthing?

  9. #9
    Account Upgraded | Title Enabled! DataMatrix is offline
    MemberRank
    Aug 2004 Join Date
    Liverpool, EnglandLocation
    659Posts
    Hmm, it echo does the job on my server, but thanks for the tip, i'll change to die.

    Quote Originally Posted by themad
    hahhahaha hahahaha that Echo will not protect a thing :)
    PS: Was there any need in that big gay ass laugh?

  10. #10
    Account Upgraded | Title Enabled! [N]asser is offline
    MemberRank
    Nov 2004 Join Date
    1,040Posts
    Yea, it was fun. By the way, this script is very effective, even though very simple.

    [N]asser` ~ Out

  11. #11
    Account Upgraded | Title Enabled! Nemesiz is offline
    MemberRank
    Mar 2004 Join Date
    LithuaniaLocation
    204Posts
    to protect from sql injection i think need to make check of length of login and password

  12. #12
    Account Upgraded | Title Enabled! [N]asser is offline
    MemberRank
    Nov 2004 Join Date
    1,040Posts
    Quote Originally Posted by Nemesiz
    to protect from sql injection i think need to make check of length of login and password
    Why? You know what injection is right? Here's an example...in a form that submits to your db, for example your login you put this:

    Username: bob; DROP TABLE Character;

    Yea, that's bascially it and that's what my script protects you against.

    [N]asser` ~ Out

  13. #13
    Apprentice fancy is offline
    MemberRank
    Oct 2004 Join Date
    7Posts
    Quote Originally Posted by DataMatrix
    I don't know why people continue using this type of anti-sql-inject, I use this:
    Code:
    if ((eregi("[^a-zA-Z0-9_-]", $memb___id)) || (eregi("[^a-zA-Z0-9_-]", $memb__pwd))) {
    	echo("SQL Injection Detected");
    **
    don't u think u should u use exit() ?
    Code:
    if ((eregi("[^a-zA-Z0-9_-]", $memb___id)) || (eregi("[^a-zA-Z0-9_-]", $memb__pwd))) {
    	echo("SQL Injection Detected");
            exit();
    **
    exit() stops the script. So after detecting sql injection script stops and nothing happens :P~

  14. #14
    Account Upgraded | Title Enabled! Nemesiz is offline
    MemberRank
    Mar 2004 Join Date
    LithuaniaLocation
    204Posts
    [N]asser "bob; DROP TABLE Character;" its 26 simbols. Username max is 10 simbols. Event is player enter his name "mylonglongname" its 14 simbols. SQL insert only 10. To protect from sql injection and bugs username need to be checked using eregi("[^a-z0-9_-]", $memb___id) (is you use big letters you make a bug).
    If player want to use username "drop" your $badchars = array(";", "'", "\"", "*", "DROP", "SELECT", "UPDATE", "DELETE", "-"); dont helps to him.

    fancy want do you think die() do ?

  15. #15
    Account Upgraded | Title Enabled! DataMatrix is offline
    MemberRank
    Aug 2004 Join Date
    Liverpool, EnglandLocation
    659Posts
    die stops the script too, like echo then exit.

  16. #16
    Account Upgraded | Title Enabled! themad is offline
    MemberRank
    Dec 2004 Join Date
    BulgariaLocation
    1,018Posts
    or just this
    PHP Code:
    if ((eregi("[^a-zA-Z0-9_-]"$memb___id)) || (eregi("[^a-zA-Z0-9_-]"$memb__pwd))) {
        die(
    "<font color=red><b>SQL Injection Detected</font</b>");
    ** 

  17. #17
    Apprentice messmaker is offline
    MemberRank
    Apr 2005 Join Date
    21Posts
    [I REPEAT]But i Didnt Understand How i put this Where need to do somthing?

  18. #18
    Account Upgraded | Title Enabled! Nemesiz is offline
    MemberRank
    Mar 2004 Join Date
    LithuaniaLocation
    204Posts
    Try to learn php

  19. #19
    Account Upgraded | Title Enabled! [N]asser is offline
    MemberRank
    Nov 2004 Join Date
    1,040Posts
    Quote Originally Posted by messmaker
    [I REPEAT]But i Didnt Understand How i put this Where need to do somthing?
    Put the code I gave you in a page and save it as functions.php...Then on whichever page you want to call the script, use this:

    PHP Code:
    include_once('functions.php');
    check_inject(); 
    That's about it.

    [N]asser` ~ Out

  20. #20
    Apprentice messmaker is offline
    MemberRank
    Apr 2005 Join Date
    21Posts
    Quote:
    Originally Posted by messmaker
    [I REPEAT]But i Didnt Understand How i put this Where need to do somthing?


    Put the code I gave you in a page and save it as functions.php...Then on whichever page you want to call the script, use this:

    PHP Code:
    include_once('functions.php');
    check_inject();
    But tell me I understand i put in a page with that script whit named functions Php but.... this i didn't understand
    PHP Code:
    include_once('functions.php');
    check_inject();

  21. #21
    Account Upgraded | Title Enabled! [N]asser is offline
    MemberRank
    Nov 2004 Join Date
    1,040Posts
    You put that code on any page that you want to check for injection.

    [N]asser` ~ Out

    PS - themad, you mean this, right? :

    PHP Code:
    if ((eregi("[^a-zA-Z0-9_-]"$memb___id)) || (eregi("[^a-zA-Z0-9_-]"$memb__pwd))) {
        die(
    "<font color=red><b>SQL Injection Detected</b></font>");
    ** 
    Just to let you know, this isn't a PHP tip, it's html...you should try to end the tag that you started last first...like this: <font><b></b></font> and not <font<b></font></b>
    Last edited by [N]asser; 06-06-05 at 10:17 PM.

  22. #22
    Account Upgraded | Title Enabled! Nemesiz is offline
    MemberRank
    Mar 2004 Join Date
    LithuaniaLocation
    204Posts
    Warning !
    ((eregi("[^a-zA-Z0-9_-]", $memb___id)) == bug in your database
    need to use only low letter and digits
    ((eregi("[^a-z0-9_-]", $memb___id))

    Becouse in one table A != a and in another A == a

  23. #23
    Account Upgraded | Title Enabled! [N]asser is offline
    MemberRank
    Nov 2004 Join Date
    1,040Posts
    Be specific. Which tables?

    [N]asser` ~ Out

  24. #24
    Account Upgraded | Title Enabled! DataMatrix is offline
    MemberRank
    Aug 2004 Join Date
    Liverpool, EnglandLocation
    659Posts
    It seemed to work perfectly with uppercase on my server...

  25. #25
    Member solteykr is offline
    MemberRank
    Jun 2004 Join Date
    Zamboanga CityLocation
    69Posts
    First problem:
    Characters with the negative sign in his/her character name will not be able to get in your website.

    Second Problem:
    If I use lowercase, I can still inject. (I think :P)

    FIX:
    (";", "'", "\"", "*", "DROP", "SELECT", "UPDATE", "DELETE", "drop", "select", "update", "delete", "WHERE", "where", "-1", "-2", "-3" "-4", "-5", "-6", "-7", "-8", "-9",);

    Now its really secured, plus characters name with "-" can still log in your site, and your stats adder is still protected.

    P.S.
    Thanks for the credit thingy u posted in your
    [Release][SQL] Negative Money Fix
    I Love You MAN! :D
    Last edited by solteykr; 07-06-05 at 10:34 AM.



Page 1 of 3 123 LastLast

Advertisement