Defense against xss attacks and SQL injection in php

Source: Internet
Author: User
This article briefly describes how to defend against xss attacks and SQL injection in php. For more information, see the introduction. the XSS attack instance code is as follows: arbitrary execution code n... this article briefly describes how to defend against xss attacks and SQL injection in php. For more information, see the introduction.

The XSS attack instance code is as follows:

Arbitrary code execution file and CSRF .}

There are many articles about SQL attacks and various injection scripts, but none of them can solve the fundamental problem of SQL injection.

The instance code is as follows:

 

A simple piece of code is used to check whether the user name or password is correct, but some sensitive code is submitted by some malicious attackers. the consequences can be imagined. There are two methods for post to judge injection.

1. enter "or '1' = 1" or "and 1 = 1" in the form text box"

The statement to query the database should be:

SELECT admin from where login = 'user' = ''or '1' = 1' or 'pass' = 'XXX'

Of course, there will be no errors, because or represents and or in SQL statements. of course, an error will also be prompted.

At that time, we found that all information in the current table can be queried after the SQL statement can be executed. for example, the correct administrator account and password are used for logon intrusion ..

Solution 1:

Use javascript scripts to filter out special characters (not recommended, and the indicator is not cured)

If javascript is disabled, attackers can still launch SQL injection attacks ..

Solution 2:

Use the built-in functions of mysql to filter data.

The instance code is as follows:

 

Now that we have talked about xss attacks, let's talk about XSS attacks and prevention ..

The example code for submitting a form is as follows:

 

The code for receiving a file is as follows:

if(emptyempty($_POST['sub'])){      echo $_POST['test'];  }

A very simple piece of code. here we just simulate the use scenario ..

The code for adding an attacker to submit an instance is as follows:

Script alert (document. cookie); script

The returned page displays the cookie information on the current page.

We can use some message boards (which are not filtered in advance). Then, when the administrator reviews and modifies the information, the COOKIE information is stolen and sent to the attacker's space or mailbox .. attackers can use the cookie modifier for login intrusion ..

Of course, there are many solutions... the most common method is described below.

Solution 1: escape using javascript

Solution 2: Escape using php built-in functions

The instance code is as follows:

if(emptyempty($_POST['sub'])){      $str=$_POST['test'];      htmlentities($srt);      echo $srt;  }

Well, the cases about SQL injection and XSS attacks are similar to the restoration methods.


Article address:

Reprint at will ^ please include the address of this 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.