长春理工大学网络中心

2012 年 9 月 22 日5860

文件上传过滤

<%@LANGUAGE='VBSCRIPT' CODEPAGE='936'%>

<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>

<%


'先作一个文件名检验函数。过虑掉所有有危险的东西。
Function chkfileExt(savefilename)
feifaExt="html|htm|php|php2|php3|php4|php5|phtml|pwml|inc|asp|aspx|
ascx|jsp|cfm|cfc|pl|bat|exe|com|dll|vbs|js|reg|cgi|htaccess|asis"
fExt = Split(feifaExt, "|")

For i = 0 To UBound(fExt)
If instr(savefilename,fExt(i))>0 Then '检测是否存在非法字符。
response.write("非法的文件格式!")
Response.End
End If
Next

End Function



set upload=new upload_file
if upload.form("act")="uploadfile" then


filepath=trim(upload.form("filepath"))
filelx=trim(upload.form("filelx"))
filepath=replace(filepath,chr(0),"")


'注意这里。替换掉chr(0)。该程序是把木马名称放在这里的。如果你只检验扩展名是没有用的。程序会传一个变量就是diy.asp&chr(0) 这个chr(0)是什么我不太清楚好像是一个停止符号。ASCII码表中对应的是“NUT”。大家可以试一下 response.write "abc.asp"&chr(0)&"03i320923.jpg" 显示的结果就是“abc.asp” 该程序就是通过这种办法绕过去的。



i=0
for each formName in upload.File
set file=upload.File(formName)

fileExt=lcase(file.FileExt) '得到的文件扩展名不含有.

if file.filesize>(1024*1024) then
response.write "<spanfont-family: 宋体; font-size: 9pt"">最大只能上传 1M 的文件! [ <a href=# onclick=history.go(-1)>重新上传</a> ]</span>"
response.end
end if


randomize
ranNum=int(90000*rnd)+10000
filename=filepath&year(now)&"_"&month(now)&"_"&day(now)&"_"&hour(now)&"_"&minute(now)&"_"&second(now)&"_"&ranNum&"."&fileExt

if file.FileSize>0 then

call chkfileExt(FileName)


'这里我们检验的是整个保存地址。如果存在非法字符就报警并停止。

file.SaveToFile Server.mappath(FileName)
response.write "<script>"
response.write "window.opener.document."&upload.form("FormName")&"."&upload.form("EditName")&".value='"&FileName&"';"
response.write "window.alert(""文件上传成功!请不要修改生成的链接地址!"");"
response.write "window.close();"
response.write "</script>"

end if

set file=nothing
next
set upload=nothing


end if
%>

0 0