2005年4月19日星期二

预防SQL注入漏洞函数

仅仅代表我的观点.不怕见笑.有问题请大家指教!我想如果你是牛人,那这个已经不是值得你看的内容,只是觉得对与很多刚入门的ASP程序员来说还是有点实际意义,所以不怕被大家笑话,写了贴在这里!

 

<%
Function checkStr(str)

if isnull(str) then

checkStr = ""

exit function

end if

checkStr=replace(str," ","")

checkStr=replace(str,"'","'")

checkStr=replace(str,";","'")

checkStr=replace(str,"--","'")

checkStr=replace(str,"(","'")

checkStr=replace(str,"[","'")

checkStr=replace(str,"$","'")

end function

%>


相关函数
Left(string, length)
 返回指定数目的从字符串的左边算起的字符
 
Asc(string)
 返回与字符串的第一个字母对应的 ANSI 字符代码。
 
Mid(string, start[, length])
 从字符串中返回指定数目的字符。
 
***********************************
我自己的做法是把字符串限定在8个字符内,呵!(千万条数据啊,没谁有这样大的记录吧?99,999,999呵!不够用,才怪了!除非你的数据从来不更新删出,那也没办法,问题是sql到了这样的时会是怎么样的速度)


---<%
if len(request.querystring("ddd"))> 8 then
response.write(黑我啊,不要了。少来)
response.end '最好有这句

'''初步是判断是否是数字=======IsNumeric 函数
if IsNumeric(request.querystring("ddd")) then

Execute("select * from [table]")

....

else

response.write(黑我啊,不要了。少来)
response.end '最好有这句

%>

当然了,加上上面的函数,在你的SQL过程里,效果就非常完美了!

呵!!!在变态点做个函数。

---<%
Function checkStr(str)

if isnull(str) then

checkStr = ""

exit function

end if

checkStr=replace(str," ","")

checkStr=replace(str,"'","'")

checkStr=replace(str,";","'")

checkStr=replace(str,"--","'")

checkStr=replace(str,"(","'")

checkStr=replace(str,"[","'")

checkStr=replace(str,"$","'")

checkStr=replace(str,"asc'," ")

checkStr=replace(str,"mid"," ")

checkStr=replace(str,"delete"," ")

checkStr=replace(str,"drop"," ")

'''呵!!我这里没屏蔽select,count,哈!想起来我就笑,太变态了,那其不是我什么都不用了不是更更安全啊!!!呵!!~^)^~
end function

%>


足够了,这个函数加载到sql选取记录集的地方。
如:rsql="select * from table where xxx="&checkstr(request.querystring("xxyy"))&""
或者来就判断字符串

说的有点林乱,但是就是这些了,对于普通的"黑客"已经足够他毫些时间了。但是对于老到的真正意义的黑客,这些都不是万能的东西,人家连服务器都黑,你能怎么样啊?嘿!!

看了些资料,结合自己的经验,写在这里。算是自己复习一下,看到的朋友也可以一起交流!