Simple js functions to prevent SQL injection

Source: Internet
Author: User
Tags sql injection

The general idea of SQL injection attacks:

Locate the SQL injection location, determine the server type and background database type, and determine the executable status
For some attackers, SQL injection is generally used. Next I will talk about my thoughts on the SQL injection method.

Injection method:

Theoretically, the authentication webpage has the following types:
Select * from admin where username = 'XXX' and password = 'yyy' statement. If necessary character filtering is not performed before this statement is officially run, it is easy to implement SQL injection.
If you enter abc 'or 1 = 1 in the user name text box-enter: 123 in the password box, the SQL statement is changed:
Select * from admin where username = 'abc' or 1 = 1 and password = '000000' this statement can always be executed correctly regardless of the user name and password entered by the user. You can easily cheat the system, obtain a valid identity.

Guess:

The basic idea is: to guess the names of all databases, guess the names of each table in the warehouse, analyze the table names that store the user name and password, and guess the names of each field in the table, guess the content of each record in the table.
You can also obtain your database name and the name of each table.
Is through the form such as: http: // www.. cn/news? Id = 10' to get your database name and table name through an error!

Front-end js injection prevention solution:

1. URL address anti-injection:

// Filter invalid SQL characters in the URL
Var sUrl = location. search. toLowerCase ();
Var sQuery = sUrl. substring (sUrl. indexOf ("=") + 1 );
Re =/select | update | delete | truncate | join | union | exec | insert | drop | count | '| "|;||<|%/I;
If (re. test (sQuery ))
{
Alert ("Do not enter invalid characters ");
Location. href = sUrl. replace (sQuery ,"");
}

2. Enter the text box to prevent injection:

// Prevents SQL injection
Function AntiSqlValid (oField)
{
Re =/select | update | delete | exec | count | '| "|=||>|||%/ I;
If (re. test (oField. value ))
    {
// Alert ("Please do not enter special characters and SQL keywords in parameters! "); // Note Chinese garbled characters
OField. value = ";
OField. className = "errInfo ";
OField. focus ();
Return false;
}

Add the following method to the input text box that requires anti-injection:

TxtName. Attributes. Add ("onblur", "AntiSqlValid (this)"); // prevents SQL script injection

Anti-SQL injection on backend servers

1. check whether get_magic_quotes_gpc () is enabled at the full site entrance. If get_magic_quotes_gpc () is not enabled, the full site checks the data that comes in for requests (including get and post, especially get, an error is prompted when characters such as add, select, and update are displayed.

2. Turn off the error message, error_reporting (E_ALL &~ E_NOTICE); // no warning is displayed
Ini_set ('display _ errors ', 'off ');
In case of any warning or error message, the website path is displayed.

3. Because the website is not pseudo-static, xxx.com /? Id = number, that is, the possibility of the SQL Injection Vulnerability (blind injection). The solution should first determine whether the get (id) comes in as a number. If it is not a direct error, in this case, intval is converted and mysql_query is performed again (the benefit of mysql_query is that it is automatically disabled after only one query is performed ).

4. Local path leakage caused by page exceptions. Set the jump page when an exception error occurs. Try not to display blank pages or error pages such as 500. Each query must determine the countermeasures if no results exist.

5. Vulnerabilities are always prompted: Cross-site scripting (XSS) attacks ). Follow the steps 1, 2, and 3 above to prompt that the vulnerability exists. Correct the vulnerability immediately and use htmlspecialchars to replace the html code.

6. The search engine is blocked in the background path. When you log on, check whether the ID is correct. If the ID is correct, then use the MD5 encrypted password for query. Two SQL queries are performed to improve security.

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.