Implementation and Prevention example of PHP Mysql Injection

Source: Internet
Author: User
Tags mysql injection php mysql

The following articles mainly introduce the implementation and prevention of PHP Mysql injection. In my opinion, the main cause of SQL injection attacks is the following two reasons. 1) The magic_quotes_gpc option in the php configuration file php. ini is disabled.

2). The developer does not check and escape the data type.

But in fact, the second point is the most important. In my opinion, it should be the most basic quality for web programmers to check the data types entered by users and submit the correct data types to MYSQL. But in reality, many small Web developers often forget this, leading to a wide open backdoor.

Why is the implementation and prevention of PHP Mysql injection the second most important? Without the second guarantee, the magic_quotes_gpc option may cause SQL injection attacks, whether on or off. The following describes the technical implementation:

Injection Attack when magic_quotes_gpc = Off

Magic_quotes_gpc = Off is a very insecure option in php. The new php version has changed the default value to On. However, there are still a considerable number of server options that are off. After all, some old servers are also used.

When magic_quotes_gpc = On, it will include all '(single quotes),' (double signs), \ (backslash), and white space characters in the submitted variables, \ is automatically added to the front \. The official instructions for php are as follows:

 
 
  1. magic_quotes_gpc boolean  
  2. Sets the magic_quotes state for GPC (Get/Post/Cookie) operations. 
    When magic_quotes are on, all ‘ (single-quote), ” (double quote), 
    \ (backslash) and NUL’s are escaped with a backslash automatically  

If there is no escape, that is, off, attackers can take advantage of it. The following test script is used as an example:

<?

F (isset ($ _ POST ["f_login"])

{

Connect to the database...

... Code omitted...

Check whether the user exists

 
 
  1. $ T_strUname = $ _ POST ["f_uname"];
  2. $ T_strPwd = $ _ POST ["f_pwd"];
  3. $ T_strSQL = "SELECT * FROM tbl_users WHERE username = '$ t_strUname' AND password = '$ t_strPwd' LIMIT ";
  4. If ($ t_hRes = mysql_query ($ t_strSQL ))
  5. {
  6. // Processing after successful query...
  7. }
  8. }
  9. ?>
  10. <Html>
  11. <Body>
  12. <Form method = post action = "">
  13. Username: <input type = "text" name = "f_uname" size = 30> <br>
  14. Password: <input type = text name = "f_pwd" size = 30> <br>
  15. <Input type = "submit" name = "f_login" value = "Logon">
  16. </Form>
  17. </Body>

The above content describes the implementation and prevention of PHP Mysql injection, hoping to help you in this aspect.

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.