来源: http://www.pcsec.org/ Author: Tr4c3[at]126.com 先看Vbs手册里的描述:
IsNumeric 函数 返回 Boolean 值指明表达式的值是否为数字。
IsNumeric(expression)
expression 参数可以是任意表达式。
说明 如果整个 expression 被识别为数字,IsNumeric 函数返回 True;否则函数返回 False。如果 expression 是日期表达式,IsNumeric 函数返回 False。
一些程序员在写代码的时候比较喜欢用isnumeric来判断参数是否为数字型,依次来杜绝sql注入的产生。但是并不是1这样的数字才符合条件,12e3,1d3,"1000,1000","1000,100"这样的计数法都符合条件,只要下面没用clng或者cint之类强行转换类型的函数,就给我们带来了不一般的用处。 如下面的代码:
Vb 代码复制代码
-
<%
-
Dim id, conn, rs, strSql
-
id = Request("id")
-
-
If IsNumeric(id) = False Or id="" Then
-
Response.Write "非法数据"
-
Response.End
-
'Else
-
'id = CLng(id)
-
End If
-
-
Set conn = CreateObject("Adodb.Connection")
-
connStr = "Provider=SQLOLEDB.1;Password=pass;Persist Security Info=True;User ID=sa;Initial Catalog=pubs;Data Source=localhost"
-
conn.open connStr
-
If id<>"" Then
-
Set rs = CreateObject("Adodb.RecordSet")
-
strSql = "select * from authors where contract=" & id 'id=1
-
rs.open strSql, conn, 1, 1
-
-
While Not rs.bof And Not rs.eof
-
Response.Write rs("au_id") & "<br>"
-
Rs.Movenext
-
Wend
-
Rs.close
-
Set rs = Nothing
-
conn.close
-
Set conn = Nothing
-
End If
-
-
%>
-
<%
Dim id, conn, rs, strSql
id = Request("id")
If IsNumeric(id) = False Or id="" Then
Response.Write "非法数据"
Response.End
'Else
'id = CLng(id)
End If
Set conn = CreateObject("Adodb.Connection")
connStr = "Provider=SQLOLEDB.1;Password=pass;Persist Security Info=True;User ID=sa;Initial Catalog=pubs;Data Source=localhost"
conn.open connStr
If id<>"" Then
Set rs = CreateObject("Adodb.RecordSet")
strSql = "select * from authors where contract=" & id 'id=1
rs.open strSql, conn, 1, 1
While Not rs.bof And Not rs.eof
Response.Write rs("au_id") & "<br>"
Rs.Movenext
Wend
Rs.close
Set rs = Nothing
conn.close
Set conn = Nothing
End If
%>
然后提交http://localhost/sql_t.asp?id=1d3,返回如图所示.

实战测试:

在旁注的时候大家往往喜欢找个sql的下手,毕竟比access的用处要大,这样isnumeric帮了我们很大的忙,我在实战中发现不少站都可以用这个方法粗略的探测数据库类型,clng等函数处理过的不行,偶尔也会出现其他一些错误提示,具体视运气而定。 |