---
title: "防止GET和POST方式引起的SQL注入攻击ASP程序"
author: "ydsakyclguozi"
date: "2010-02-03T19:59:23Z"
tags: []
categories: ["Web前端"]
slug: "get-post-sql-asp"
draft: false
lastmod: "2010-02-03T19:59:23Z"
id: 587016
url: "https://52347.iteye.com/blog/587016"
---

编写通用的SQL防注入程序一般的http请求不外乎get 和 post,所以只要我们在文件中过滤所有post或者get请求中的参数信息中非法字符即可，所以我们实现http 请求信息过滤就可以判断是是否受到SQL注入攻击。

    IIS传递给asp.dll的get 请求是是以字符串的形式，当传递给Request.QueryString数据后，asp解析器会分析Request.QueryString的信息，，然后根据"&"，分出各个数组内的数据所以get的拦截如下：
首先我们定义请求中不能包含如下字符：

|and|exec|insert|select|delete|update|count|*|%|chr|mid|master|truncate|char|declare 

各个字符用"|"隔开，，然后我们判断的得到的Request.QueryString，具体代码如下 ：

dim sql_injdata
SQL_injdata = "'|and|exec|insert|select|delete|update|count|*|%|chr|mid|master|truncate|char|declare"
SQL_inj = split(SQL_Injdata,"|")
If Request.QueryString<>"" Then
For Each SQL_Get In Request.QueryString
For SQL_Data=0 To Ubound(SQL_inj)
if instr(Request.QueryString(SQL_Get),Sql_Inj(Sql_DATA))>0 Then
Response.Write "<Script Language=****>alert('请不要在参数中包含非法字符尝试注入');history.back(-1)</Script>"
Response.end
end if
next
Next
End If 

这样我们就实现了get请求的注入的拦截，但是我们还要过滤post请求，所以我们还得继续考虑request.form,这个也是以数组形式存在的，我们只需要再进一次循环判断即可。代码如下：

If Request.Form<>"" Then
For Each Sql_Post In Request.Form
For SQL_Data=0 To Ubound(SQL_inj)
if instr(Request.Form(Sql_Post),Sql_Inj(Sql_DATA))>0 Then
Response.Write "<Script Language=****>alert('请不要在参数中包含非法字符尝试注入');history.back(-1)</Script>"
Response.end
end if
next
next
end if 

我们已经实现了get和post请求的信息拦截，你只需要在conn.asp之类的打开数据库文件之前引用这个页面即可。放心的继续开发你的程序，不用再考虑是否还会受到SQL注入攻击。