Home  >  Article  >  Database  >  How to prevent SQL injection in web security

How to prevent SQL injection in web security

little bottle
little bottleforward
2019-04-29 13:27:182955browse

SQL injection is to insert SQL commands into Web forms to submit or enter domain names or query strings for page requests, and ultimately trick the server into executing malicious SQL commands. For example, many previous film and television websites leaked VIP membership passwords mostly through Query characters are exposed when submitting a WEB form. This type of form is particularly vulnerable to SQL injection attacks.

SQL injection principle

When an application uses input content to construct a dynamic sql statement to access the database When, a sql injection attack occurs. SQL injection can also occur if the code uses stored procedures that are passed as strings containing unfiltered user input.

SQL injection may allow an attacker to use application login to execute commands in the database. This problem can become serious if an application uses an overprivileged account to connect to the database. In some forms, the content entered by the user is directly used to construct dynamic SQL commands, or is used as input parameters for stored procedures. These forms are particularly vulnerable to SQL injection attacks. When many website programs are written, the legality of user input is not judged or the variables in the program are not handled properly, causing security risks in the application. In this way, the user can submit a database query code and obtain some sensitive information or control the entire server based on the results returned by the program, so SQL injection occurs.

How to prevent SQL injection?

  1. #Never trust user input. To verify the user's input, you can use regular expressions or limit the length; then convert sensitive symbols such as single quotes and double "-".
  2. Do not use dynamic assembly of sql. You can use parameterized sql or directly use stored procedures for data query and access.
  3. Never use database connections with administrator privileges, use separate database connections with limited permissions for each application
  4. Do not store confidential information directly, encrypt or hash passwords and sensitive information .
  5. The application's exception information should give as few hints as possible. It is best to use custom error information to wrap the original error information.

Related tutorials: SQL video tutorial

The above is the detailed content of How to prevent SQL injection in web security. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete