Common Web security and protection principles

Source: Internet
Author: User
Tags html tags sql injection protection csrf attack

1.0 SQL injection

SQL injection principle: by inserting SQL commands into a Web form to submit or entering a query string for a domain name or page request, the result is to spoof the server to execute a malicious SQL command.

SQL Injection Protection:

1. Never trust the user's input, to verify the user's input, you can use regular expressions, or limit the length, the single quotation mark and the double "-" to convert, and so on.

2. Never use dynamically assembled SQL, either using parameterized SQL or directly using stored procedures for data query access.

3. Never use a database connection with administrator rights, and use a separate limited database connection for each app.

4. Do not store confidential information in plaintext, please encrypt or hash out the password and sensitive information.

The principle and precaution of 2.0 XSS

XSS principle: XSS (Cross-site scripting) attack refers to an attacker inserting malicious HTML tags or JavaScript code into a Web page. For example, the attacker put a seemingly secure link in the forum, cheat the user to click, steal the user's private information in the cookie, or the attacker in the forum to add a malicious form, when the user submits the form, but the message to the attacker's server, rather than the user originally thought of the trust site.

How to prevent XSS:

First, the code in the user input places and variables need to carefully check the length and the "<", ">", ";", "" "and other characters to filter, and then any content written to the page must be encode, to avoid accidentally the HTML tag out. This level is well done, at least by blocking more than half of XSS attacks.

First, avoid disclosing user privacy directly in a cookie, such as email, password, and so on. Second, reduce the risk of cookie leaks by making cookies and system IP bindings. This way the attacker gets a cookie that has no real value and cannot be replayed.

If the website does not need to operate the cookie on the browser side, you can add httponly at the end of the Set-cookie to prevent JavaScript code from obtaining cookies directly.

Try to submit a form using post instead of get

3.0 CSRF cross-site request forgery

CSRF principle: In lieu of the user to complete the specified action, need to know the other user page code and data package. To complete a csrf attack, the victim must complete two steps in turn:

① logs on to trusted Web site A and generates cookies locally.

② Visit dangerous website B without logging out a.

CSRF's defenses:

There are many ways to csrf the ① server, but the general idea is consistent, which is to increase the pseudo-random number on the client page.

② Method of verifying code

Common Web security and protection principles

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.