asp防sql注入攻擊技巧實例詳解
引言
asp項目,在sql查詢使用字符串拼接情況下,會受到sql注入攻擊,可以使用敏感詞過濾和參數(shù)化語句進行修改。
敏感詞過濾
Dim Fy_Post,Fy_Get,Fy_In,Fy_Inf,Fy_Xh,Fy_db,Fy_dbstr,Kill_IP,WriteSql '自定義需要過濾的字串,用 "|" 分隔 Fy_In = "'|;|and|(|)|exec|insert|select|delete|update|count|*|%|chr|mid|master|truncate|char|declare|exist|drop" Kill_IP=True WriteSql=True '---------------------------------- Fy_Inf = split(Fy_In,"|") '--------POST部份------------------ If Request.Form<>"" Then For Each Fy_Post In Request.Form For Fy_Xh=0 To Ubound(Fy_Inf) If Instr(LCase(Request.Form(Fy_Post)),Fy_Inf(Fy_Xh))<>0 Then Response.Redirect "/index.asp" Response.End End If Next Next End If If Request.QueryString<>"" Then For Each Fy_Get In Request.QueryString For Fy_Xh=0 To Ubound(Fy_Inf) If Instr(LCase(Request.QueryString(Fy_Get)),Fy_Inf(Fy_Xh))<>0 Then Response.Redirect "/index.asp" Response.End End If Next Next End If
對敏感詞URL進行過濾,重定位或進行其他處理
參數(shù)化
Public Function execSqlOpen(connect,cursorType,lockType,args()) set cmdTemp = server.CreateObject("ADODB.Command") cmdTemp.ActiveConnection = connect cmdTemp.Prepared = true cmdTemp.CommandText = args(0) Dim i For i = 1 To UBound(args) set paramTemp = cmdTemp.CreateParameter("",201,1,Len(args(i))+10,args(i)) cmdTemp.Parameters.Append paramTemp Next set rsTemp=server.CreateObject("adodb.recordset") rsTemp.open cmdTemp,,cursorType,lockType set execSqlOpen = rsTemp end function Public Function execSqlExecute(connect,args()) set cmdTemp = server.CreateObject("ADODB.Command") cmdTemp.ActiveConnection = connect cmdTemp.Prepared = true cmdTemp.CommandText = args(0) Dim i For i = 1 To UBound(args) set paramTemp = cmdTemp.CreateParameter("",201,1,Len(args(i))+10,args(i)) cmdTemp.Parameters.Append paramTemp Next set execSqlExecute = cmdTemp.execute end function
封裝這兩個函數(shù),然后進行修改
- 1.使用open的調(diào)用execSqlOpen(需要調(diào)用close,視原代碼是否close決定),使用execute的調(diào)用execSqlExecute(不調(diào)用close)
- 2.需要返回值的用set 一個變量接收,不需要的用call調(diào)用
- 3.表名動態(tài)拼接的,無法使用占位符,使用原始拼接方式
- 4.使用like的,內(nèi)部使用?占位,外部使用字符串拼接前后%("%"&keyword&"%")
例子如下:
open普通查詢
sql="select * from table where column='"&column&"'" set rs=server.CreateObject("ADODB.recordset") rs.Open sql,conn,1,1 => dim args args = Array("select * from table where column = ?",column) set rs = execSqlOpen(conn,1,1,args)
動態(tài)參數(shù)查詢
sql="select * from table where 1=1 and column1='"&request("column1")&"'" if column2<>"" then sql=sql&" and column2 like '%"&column2&"%'" end if if column3<>"" then sql=sql&" and column3 ="&column3&"" end if sql=sql&" order by column4 desc;" Set rs= Server.CreateObject("ADODB.Recordset") rs.open sql,conn,1,1 => dim args args = Array("select * from table where 1=1 and column1=?",request("column1")) if column2<>"" then args(0)=args(0)&" and column2 like ?" ReDim Preserve args(UBound(args)+1) args(UBound(args)) = "%"&column2&"%" end if if column3<>"" then args(0)=args(0)&" and column3 =?" ReDim Preserve args(UBound(args)+1) args(UBound(args)) = column3 end if args(0)=args(0)&" order by column4 desc;" set rs = execSqlOpen(conn,1,1,args)
table動態(tài)
Set Rs_t=Conn.Execute("Select column From "&table&" where column1="&column1) => dim args args = Array("Select column From "&table&" where column1=?",column1) set Rs_t = execSqlExecute(conn,args)
執(zhí)行查詢
set rs=conn.execute("select * from table where column="&request("column")) => dim args args = Array("select * from table where column = ?",request("column")) set rs = execSqlExecute(conn,args)
執(zhí)行更新
conn.execute("update table set column = '"&column&"'") => dim args args = Array("update table set column = ?",column) call execSqlExecute(conn,args)
以上就是asp防sql注入攻擊技巧實例詳解的詳細(xì)內(nèi)容,更多關(guān)于asp防sql注入攻擊的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
asp下用replace非正則實現(xiàn)代碼運行功能的代碼
asp下用replace非正則實現(xiàn)代碼運行功能的代碼...2007-09-09ajax XMLHTTP Post Form時的表單亂碼綜合解決
用XMLHTTP Post Form時的表單亂碼有兩方面的原因——Post表單數(shù)據(jù)時中文亂碼;服務(wù)器Response被XMLHTTP不正確編碼引起的亂碼。換句話說,本文主要解決兩個問題——怎樣正確Post中文內(nèi)容&怎樣正確顯示得到的中文內(nèi)容。2008-05-05面向小白visual studio 2019 添加第三方庫教程(入門)
這篇文章主要介紹了面向小白visual studio 2019 添加第三方庫教程,文中通過圖文介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03實現(xiàn)UTF8轉(zhuǎn)換GB2312國標(biāo)碼的asp代碼
ASP來實現(xiàn)UTF8轉(zhuǎn)換GB2312國標(biāo)碼-GB2312轉(zhuǎn)UTF-8,需要的朋友可以參考下。2010-04-04IIS7.5調(diào)用asp頁面出現(xiàn)800a0e7a的解決辦法
本文給大家分享的是在windows2008R2 64位系統(tǒng)中出現(xiàn)了ADODB.Connection 錯誤 '800a0e7a'的解決辦法,方法很簡單,可是處理過程卻很曲折,這里推薦給大家,有需要的小伙伴可以參考下。2015-05-05SQLServer ADODB.Recordset 錯誤“800a0e78”,對象關(guān)閉時,不允許操作
今天在幫一個客戶維護網(wǎng)站的時候,運行asp提示ADODB.Recordset 錯誤“800a0e78”,對象關(guān)閉時,不允許操作,原來是asp與sqlserver的連接出問題導(dǎo)致2014-07-07asp實現(xiàn)檢查ip地址是否為內(nèi)網(wǎng)或者私有ip地址的代碼分享
這篇文章主要介紹了asp實現(xiàn)檢查ip地址是否為內(nèi)網(wǎng)或者私有ip地址的代碼分享,給同樣在找IP判斷的使用,需要的朋友可以參考下2014-08-08