ASP的网站,代码怎么写算是严谨的呢?需要注意什么安全问题?

如题所述

1、所有提交的数据,要进行严格的前后台双重验证、长度限制,特殊符号检测,先使用replace函数 依次替换不安全字符‘%<>&等以及SQL语句exec delete ……,再进行其他验证。
2、使用图片上传组件要防注入。图片上传目录不要给可执行权限。
3、在CONN数据库连接文件中加入这个:http://www.onlinedown.net/soft/485266.htm
4、后台登录验证用Session。

5、重要的地方要验证数据是否是从本站提交。
6、*.mdb数据库要做防下载处理。
7、服务器不要返回具体的错误提示。
8、各种参数尽量用整数,进行是否为整数判断,或是否是你设定的具体参数。
9、所有提交尽量使用POST取值时用 request.Form
如:
POST: ?id=1331&type=1&user='liwei'
如:ID和type先进行非法字符替换,再进行是否为整数判断。
type 后续可以用case
s_type=request.Form(type) 先进行非法字符替换,再进行是否为整数判断。
select case s_type
case "1"
'进行相应操作
case "2"
'进行相应操作
case else
ErrText"出错了,你自己看着办吧!","javascript:history.back(-1)"
end select

下面是一些常用函数,好久不写了,比较乱,自己整合一下。

'定义过滤html标签
function htmlencode(hstring)
if not isnull(hstring) then
hstring=Replace(hstring,"<","<")
hstring=Replace(hstring,">",">")
hstring=Replace(hstring,chr(32)," ")
hstring=Replace(hstring,chr(9)," ")
hstring=Replace(hstring,chr(13),"<br>")
hstring=Replace(hstring,chr(39),"'")
hstring=Replace(hstring,chr(34),""")
hstring=Replace(hstring,chr(10)&chr(10),"<p></p>")
htmlencode=hstring
end if
end function

'去除html标签正则

Function RemoveHTML(strHTML)
Dim objRegExp, Match, Matches
Set objRegExp = New Regexp

objRegExp.IgnoreCase = True
objRegExp.Global = True
'取闭合的<>
objRegExp.Pattern = "<.+?>"
'进行匹配
Set Matches = objRegExp.Execute(strHTML)

' 遍历匹配集合,并替换掉匹配的项目
For Each Match in Matches
strHtml=Replace(strHTML,Match.Value,"")
Next
RemoveHTML=strHTML
Set objRegExp = Nothing
End Function

'判断长度。
Function LangText(str,ln)
LangText=ClearHtml(NoSqlHack(trim(str)))
if len(LangText)>ln then LangText=""
End Function

'输出提示,及页面跳转。
Function ErrText(str,gurl)
Response.write"<script>alert('"&str&"');location.href="""&gurl&""";</script>"
response.end
End Function

Function NoSqlHack(FS_inputStr)
Dim f_NoSqlHack_AllStr,f_NoSqlHack_Str,f_NoSqlHack_i,Str_InputStr
Str_InputStr=FS_inputStr
f_NoSqlHack_AllStr="*|and |exec |insert |select |delete |update |count |master |truncate |declare |and |exec |insert |select |delete |update |count |master |truncate |declare |char(|mid(|chr(|and[|exec[|insert[|select[|delete[|update[|count[|master[|truncate[|declare[|set[|set |set |where[|where |where "
f_NoSqlHack_Str = Split(f_NoSqlHack_AllStr,"|")

For f_NoSqlHack_i=LBound(f_NoSqlHack_Str) To Ubound(f_NoSqlHack_Str)
If Instr(LCase(Str_InputStr),f_NoSqlHack_Str(f_NoSqlHack_i))<>0 Then
If f_NoSqlHack_Str(f_NoSqlHack_i)="'" Then f_NoSqlHack_Str(f_NoSqlHack_i)=" \' "
Response.Write "<html><title>警告</title><body bgcolor=""EEEEEE"" leftmargin=""60"" topmargin=""30""><font style=""font-size:16px;font-weight:bolder;color:blue;""><li>您提交的数据有恶意字符</li></font><font style=""font-size:14px;font-weight:bolder;color:red;""><br><li>您的数据已经被记录!</li><br><li>您的IP:"&Request.ServerVariables("Remote_Addr")&"</li><br><li>操作日期:"&Now&"</li></font></body></html><!--Powered by Foosun Inc.,AddTime:"&now&"-->"
Response.End
End if
Next
NoSqlHack = Replace(Str_InputStr,"'","''")
End Function

'清除HTML
Function ClearHtml(Content)
Content=Zxj_ReplaceHtml("&#[^>]*;", "", Content)
Content=Zxj_ReplaceHtml("</?marquee[^>]*>", "", Content)
Content=Zxj_ReplaceHtml("</?object[^>]*>", "", Content)
Content=Zxj_ReplaceHtml("</?param[^>]*>", "", Content)
Content=Zxj_ReplaceHtml("</?embed[^>]*>", "", Content)
Content=Zxj_ReplaceHtml("</?table[^>]*>", "", Content)
Content=Zxj_ReplaceHtml(" ","",Content)
Content=Zxj_ReplaceHtml("</?tr[^>]*>", "", Content)
Content=Zxj_ReplaceHtml("</?th[^>]*>","",Content)
Content=Zxj_ReplaceHtml("</?p[^>]*>","",Content)
Content=Zxj_ReplaceHtml("</?a[^>]*>","",Content)
Content=Zxj_ReplaceHtml("</?img[^>]*>","",Content)
Content=Zxj_ReplaceHtml("</?tbody[^>]*>","",Content)
Content=Zxj_ReplaceHtml("</?li[^>]*>","",Content)
Content=Zxj_ReplaceHtml("</?span[^>]*>","",Content)
Content=Zxj_ReplaceHtml("</?div[^>]*>","",Content)
Content=Zxj_ReplaceHtml("</?th[^>]*>", "", Content)
Content=Zxj_ReplaceHtml("</?td[^>]*>", "", Content)
Content=Zxj_ReplaceHtml("</?script[^>]*>", "", Content)
Content=Zxj_ReplaceHtml("(javascript|jscript|vbscript|vbs):", "", Content)
Content=Zxj_ReplaceHtml("on(mouse|exit|error|click|key)", "", Content)
Content=Zxj_ReplaceHtml("<\\?xml[^>]*>", "", Content)
Content=Zxj_ReplaceHtml("<\/?[a-z]+:[^>]*>", "", Content)
Content=Zxj_ReplaceHtml("</?font[^>]*>", "", Content)
Content=Zxj_ReplaceHtml("</?b[^>]*>","",Content)
Content=Zxj_ReplaceHtml("</?u[^>]*>","",Content)
Content=Zxj_ReplaceHtml("</?i[^>]*>","",Content)
Content=Zxj_ReplaceHtml("</?strong[^>]*>","",Content)
Content=Zxj_ReplaceHtml(" ","",Content)
Content=Zxj_ReplaceHtml("CHR(32)","",Content)
Content=Zxj_ReplaceHtml("CHR(10)","",Content)
Content=Zxj_ReplaceHtml(chr(13)&chr(10),"",Content)
Content=Zxj_ReplaceHtml("<br />","",Content)
Content=Zxj_ReplaceHtml("<p>","",Content)
Content=Zxj_ReplaceHtml("</p>","",Content)
Content=Zxj_ReplaceHtml(" ","",Content)
Content=Zxj_ReplaceHtml(CHR(10) & CHR(10),"",Content)
ClearHtml=Content
End Function
Function Zxj_ReplaceHtml(patrn, strng,content)
IF IsNull(content) Then
content=""
End IF
Set regEx = New RegExp ' 建立正则表达式。
regEx.Pattern = patrn ' 设置模式。
regEx.IgnoreCase = true ' 设置忽略字符大小写。
regEx.Global = True ' 设置全局可用性。
Zxj_ReplaceHtml=regEx.Replace(content,strng) ' 执行正则匹配
End Function
温馨提示:答案为网友推荐,仅供参考
第1个回答  2014-05-11
防注入,上传格式控制,后台隐蔽,
相似回答