Introduction to SQL injection and XSS attacks in PHP

Source: Internet
Author: User
Tags sql injection attack
    1. mysql_connect ("localhost", "root", "123456") or Die ("Database connection failed!");
    2. mysql_select_db ("Test1");
    3. $user =$_post[' uid '];
    4. $pwd =$_post[' pass ';
    5. if (mysql_query ("SELECT * from where
    6. Admin
    7. = ' username ' = ' $user ' or ' password ' = ' $pwd ') {
    8. echo "user successfully logged in.";
    9. } eles {
    10. echo "Error in User name or password";
    11. }
    12. ?>
Copy Code

The above code is used to detect whether the user name or password is correct, but some malicious attackers to submit some sensitive code, the consequences are conceivable.

There are 2 ways to inject post judgment . 1, in the Form form text box input "or ' 1 ' = 1" or "and 1=1" in the query database statement should be: SELECT admin from where login = ' user ' = ' or ' 1 ' =1 ' or ' pass ' = ' XXXX ' certainly does not appear to be anything wrong, because or in the statement of SQL represents and, or meaning. Of course the error is also indicated. At that time, we had discovered that we could query all the information of the current table after executing the SQL statement. For example: Correct administrator account and password for login intrusion.

Fix It 1: Use JavaScript scripts to filter special characters (not recommended) If the attacker disables JavaScript or can make a SQL injection attack.

Fix it 2: Use MySQL's own function to filter.

    1. Omitting operations such as connecting to a database
    2. $user =mysql_real_escape_string ($_post[' user ');
    3. mysql_query ("select * from admin whrer ' username ' = ' $user '");
    4. ?>
Copy Code

2, XSS attacks and prevention. Submit a form:

Copy Code

Receive file:

    1. if (Empty ($_post[' Sub '])) {
    2. echo $_post[' test '];
    3. }
Copy Code

The code is simple, just simulating the next usage scenario.

3. Join the attacker to submit the cookie information of the current page should be displayed on the returned page. We can apply to some of the message boards (not filtered in advance), and then when the administrator reviews the change of information to steal cookie information, and sent to the attacker's space or mailbox. An attacker could use a cookie modifier to log in and invade.

The following describes one of the most common workarounds. Fix Scenario 1: Use JavaScript for escape repair Scenario 2: Escaping with PHP built-in functions

    1. if (Empty ($_post[' Sub '])) {
    2. $str =$_post[' test '];
    3. Htmlentities ($SRT);
    4. Echo $srt;
    5. }
Copy Code
  • 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.