Filter user input in ASP to improve security

Source: Internet
Author: User
Tags filter
Security | safety | security | Security is important for all applications. Security is especially important when a single error in a simple application can result in unauthorized access to the database or other enterprise resources. A common method of attack is to embed commands in the user's response, and filtering out these illegal characters from user input can prevent this attack.


Allowing users to enter illegal characters increases the chance that a user can cause problems. For example, many applications can accept user-added WHERE clauses in SQL commands. Malicious users execute the code on the database server by adding additional commands to the information they enter. For example, instead of entering "Smith" as a retrieval string, they enter "Smith"; EXEC Master.. xp_cmdshell ' dir *.exe '.

The following code is designed to handle multiple recordsets returned from the server. The user's input will contain an additional, unexpected execution command. When the NextRecordset method is invoked, the hidden malicious code is executed.

This attack can be avoided by filtering out the illegal characters in the user's input information (in the comment section). After doing so, the user's input is still allowed to be processed, but all illegal characters are cleared away.

Dim rst as Recordset
Dim Rst2 as Recordset
Dim Struserinput as String

Struserinput = "Smith"; EXEC Master.. xp_cmdshell ' dir *.exe '

' Filter input for invalid characters
Struserinput = Replace (Struserinput, "<", vbNullString)
Struserinput = Replace (Struserinput, ">", vbNullString)
Struserinput = Replace (Struserinput, "" "", vbNullString)
Struserinput = Replace (Struserinput, "'", vbNullString)
Struserinput = Replace (struserinput, "%", vbNullString)
Struserinput = Replace (Struserinput, ";", vbNullString)
Struserinput = Replace (Struserinput, "(", vbNullString)
Struserinput = Replace (Struserinput, ")", vbNullString)
Struserinput = Replace (Struserinput, "&", vbNullString)
Struserinput = Replace (struserinput, "+", vbNullString)
Struserinput = Replace (Struserinput, "-", vbNullString)

Set rst = New Recordset
Rst. ActiveConnection = "Provider=sqloledb;data source=sqlserver;" & _
"Initial catalog=pubs;integrated Security=sspi"
Rst. Open "SELECT * from authors WHERE au_lname = '" & Struserinput & _
"'", adOpenStatic
' Do something with recordset 1

Set Rst2 = rst. NextRecordset ()
' Do something with recordset 2

Embedding commands in the user's input is also a common tactic for attacking ASP Web applications, also known as Cross-site scripting attacks. Filtering the input and using Server.HTMLEncode and Server.URLEncode will help prevent such problems in your ASP application.



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.