I call you SQL injection Defense (PHP syntax)

Source: Internet
Author: User
Tags md5 encryption php and sql injection defense vars

Copyright Notice: Https://github.com/wusuopubupt

Gossip doesn't say, come straight!

Theoretical supplement: 1.http://blog.csdn.net/wusuopubupt/article/details/8752348

2.http://www.cnblogs.com/hkncd/archive/2012/03/31/2426274.html

1. What is SQL injection, poke Wikipedia view

2. Local Test code:

If the form is submitted correctly, print Hello, "username"

Otherwise, print "404 Not found!"

[PHP]View Plaincopy
  1. <?php
  2. require ' config.php ';
  3. $DBConnection = mysql_connect ( "$dbhost", "$dbuser", "$dbpwd");
  4. mysql_select_db ( "$dbdatabase");
  5. if (isset ($_get[' submit ')) && $_get[' Submit ']) {
  6. $sql ="SELECT * from Test where name= '". $_get[' username']. "' and password=' ". $_get[' Password']." ' ";
  7. //echo $sql; exit;
  8. $result =mysql_query ($sql,$DBConnection);
  9. $num =mysql_num_rows ($result);
  10. if ($num >=1)
  11. {
  12. echo "Hello,".   $_get[' username '];
  13. }
  14. else {
  15. echo"404 Not Found";
  16. }
  17. }
  18. ?>
  19. <form action="login.php" method="GET" >
  20. <table>
  21. <tr>
  22. <td>username</td>
  23. <td><input type="textbox" Name="username"/></td>
  24. <td>password</td>
  25. <td><input type= "textbox" name="password" ></td>
  26. <td>submit</td>
  27. <td><input type="Submit" Name="Submit" ></td>
  28. </tr>
  29. </table>
  30. </form>

3. The browser interface displays:

4. Plays, SQL injection:

5. Principle-Why is the user name not correct, but can display hello?

I can echo it:

[PHP]View Plaincopy
    1. <span style="FONT-SIZE:18PX;" >$sql ="SELECT * from Test where name= '". $_get[' username']. "' and password=' ". $_get[' Password']." ' ";
    2. echo $sql; Exit;</span>

Show:

Get the query in my MySQL database:

Can see, actually can find the information, because the SQL statement, the first half of the single quotation marks are closed, the latter half of the single quotation mark is "--" to comment out, in the middle of an ever-established condition "1=1", which causes any character to successfully login results.

6. Summary:

1) In fact, this SQL injection process is very simple, the difficult place is to submit SQL injection statement flexibility above, the use of single quotes is critical, in addition, multi-use echo print debugging is also worth a try ~ ~

2) Get method to submit the form is dangerous, so use the POST Method!

Reference: http://blog.csdn.net/gideal_wang/article/details/4316691

3) Prevent SQL injection: As you can see, SQL injection is the user submits some illegal characters (such as the single quotation mark of this article and the comment number of the SQL statement--and the backslash \ etc.), so we need to escape: htmlspecialchars function, Mysql_read_escape_ String functions can be implemented.

4) JS section verification form, jsp/php and other background to verify the code?

---need, because friebug can disable JS ...

--------------------------------------------------------------------------

Update

The above method, when password through MD5 encryption, can not be injected, then on the username to tamper with:

The content behind the username is commented out. haha ~

Reference: Http://newaurora.pixnet.net/blog/post/166231341-sql-injection-%E7%AF%84%E4%BE%8B (%e7%99%bb%e5%85%a5%e7%af% 84%E4%BE%8B)

by Wusuopubupt

I call you SQL injection Defense (PHP syntax)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.