2010年12月7日 星期二

ASP防止盜鏈或防止下載的方法

資料來源:http://xiong.blog.51cto.com/811/17586

我們在管理網站文件時,可以把擴展名一樣的文件放在同一個目錄下,起一個比較特別名字,例如放pdf文件目錄為the_pdf_file_s,把下面代碼另存為down.asp,他的網上路徑為 [url]http://www.xx.com/down.asp[/url],我們就可以用[url]http://www.xx.com /down.asp?FileName=51windows.pdf[/url]來下載這個文件了,而且下載者無法看到這個文件實際下載路徑的!在 down.asp中我們還可以設置下載文件是否需要登陸,判斷下載的來源頁是否為外部網站,從而可以做到防止文件被盜鏈。
 
      示例代碼:
以下是引用片段:
以下是引用片段:
  <% 
  From_url = Cstr(Request.ServerVariables("HTTP_REFERER")) 
  Serv_url = Cstr(Request.ServerVariables("SERVER_NAME")) 
  if mid(From_url,8,len(Serv_url)) <> Serv_url then 
  response.write "非法鏈接!" '防止盜鏈 
  response.end 
  end if 
   
  if Request.Cookies("Logined")="" then 
  response.redirect "/login.asp" '需要登陸! 
  end if 
  Function GetFileName(longname)'/folder1/folder2/file.asp=>file.asp 
  while instr(longname,"/") 
  longname = right(longname,len(longname)-1) 
  wend 
  GetFileName = longname 
  End Function 
  Dim Stream 
  Dim Contents 
  Dim FileName 
  Dim TrueFileName 
  Dim FileExt 
  Const adTypeBinary = 1 
  FileName = Request.QueryString("FileName") 
  if FileName = "" Then 
   Response.Write "無效文件名!" 
   Response.End 
  End if 
  FileExt = Mid(FileName, InStrRev(FileName, ".") + 1) 
  Select Case UCase(FileExt) 
   Case "ASP", "ASA", "ASPX", "ASAX", "MDB" 
   Response.Write "非法操作!" 
   Response.End 
  End Select 
  Response.Clear 
  if lcase(right(FileName,3))="gif" or lcase(right(FileName,3))="jpg" or lcase(right(FileName,3))="png" then 
  Response.ContentType = "image/*" '對圖像文件不出現下載對話框 
  else 
  Response.ContentType = "application/ms-download" 
  end if 
  Response.AddHeader "content-disposition", "attachment; filename=" & GetFileName(Request.QueryString("FileName")) 
  Set Stream = server.CreateObject("ADODB.Stream") 
  Stream.Type = adTypeBinary 
  Stream.Open 
  if lcase(right(FileName,3))="pdf" then '設置pdf類型文件目錄 
  TrueFileName = "/the_pdf_file_s/"&FileName 
  end if 
  if lcase(right(FileName,3))="doc" then '設置DOC類型文件目錄 
  TrueFileName = "/my_D_O_C_file/"&FileName 
  end if 
  if lcase(right(FileName,3))="gif" or lcase(right(FileName,3))="jpg" or lcase(right(FileName,3))="png" then 
  TrueFileName = "/all_images_/"&FileName '設置圖像文件目錄 
  end if 
  Stream.LoadFromFile Server.MapPath(TrueFileName) 
  While Not Stream.EOS 
   Response.BinaryWrite Stream.Read(1024 * 64) 
  Wend 
  Stream.Close 
  Set Stream = Nothing 
  Response.Flush 
  Response.End 
  %> 
本地圖片,音樂等ASP防盜鏈代碼(asp)

     
        以下是引用片段:
以下是引用片段:
<% 
  '定義函數,用ADODB.Stream讀取二進制數據 
  Function ReadBinaryFile(FileName) 
   Const adTypeBinary = 1 
   Dim BinaryStream 
   Set BinaryStream = CreateObject("ADODB.Stream") 
   BinaryStream.Type = adTypeBinary 
   BinaryStream.Open 
   BinaryStream.LoadFromFile FileName 
   ReadBinaryFile = BinaryStream.Read 
  End Function 
   
  Response.AddHeader "Content-Disposition", "attachment;filename=2.gif"'文件名 
  Response.ContentType = "image/GIF" 』設置(1) 
  response.Binarywrite ReadBinaryFile(server.mappath("2.gif"))'就是你讀取存在本地的文件,防止被
別人知道真實路徑盜連的。 
   
  %>

 (1)下面的示例將 ContentType 屬性設置為其他的常見值。
  text/HTML 這個就不說了
  image/GIF gif圖片
  image/JPEG jpg圖片
  application/x-cdf cdf文檔
  application/wma 就是西瓜哪個音樂類型了
  具體可以參照 Web 瀏覽器文檔或當前的 HTTP 規格說明
 
  這樣再利用asp的儲存session,cookies,以及讀取HTTP頭等特殊功能就可以完全真正的實現防盜連,這裡
沒有設置緩存,如果訪問量巨大,我想設置下就會更好吧。

asp下載防盜鏈代碼
第一種:
終於對下載系統做了個防盜鏈措施,在下載的頁面頭部做了如下代碼,相關代碼如下:
以下是引用片段:
以下是引用片段:
<% 
From_url = Cstr(Request.ServerVariables("HTTP_REFERER")) 
Serv_url = Cstr(Request.ServerVariables("SERVER_NAME")) 
if mid(From_url,8,len(Serv_url)) <> Serv_url and mid(From_url,8,len(Serv_url))<>"ITstudy.cn" and mid(From_url,8,len(Serv_url))<>"http://www.gc888.cn/" then 
response.write "您下載的軟件來自IT學習網,請直接從主頁下載,謝謝<br>" 』防止盜鏈 
response.write "<a href=http://www.gc888.cn>IT學習網[url]http://www.gc888.cn</a>[/url]" 』防止盜鏈 
response.end 
end if 
%> 
第二種:
以下是引用片段:
以下是引用片段:
<%  
  』定義函數,用ADODB.Stream讀取二進制數據  
  Function ReadBinaryFile(FileName)  
   Const adTypeBinary = 1  
   Dim BinaryStream  
   Set BinaryStream = CreateObject("ADODB.Stream")  
   BinaryStream.Type = adTypeBinary  
   BinaryStream.Open  
   BinaryStream.LoadFromFile FileName  
   ReadBinaryFile = BinaryStream.Read  
  End Function  
    
  Response.AddHeader "Content-Disposition", "attachment;filename=2.gif"』文件名  
  Response.ContentType = "image/GIF" 』設置(1)  
  response.Binarywrite ReadBinaryFile(server.mappath("2.gif"))』就是你讀取存在本地的文件,防止被 
別人知道真實路徑盜連的。  
    
  %>
  下面的示例將 ContentType 屬性設置為其他的常見值。

 text/HTML 這個就不說了
  image/GIF gif圖片
  image/JPEG jpg圖片
  application/x-cdf cdf文檔
  application/wma 就是西瓜哪個音樂類型了
  具體可以參照 Web 瀏覽器文檔或當前的 HTTP 規格說明
  
  這樣再利用asp的儲存session,cookies,以及讀取HTTP頭等特殊功能就可以完全真正的實現防盜連,這裡
沒有設置緩存,如果訪問量巨大,我想設置下就會更好吧。

第三種:
最簡單的用Active Server Pages防站外提交表單、跨站提交表單、防盜鏈……
方法:Request.SeverVariables("HTTP_REFERER")
解釋:當某人通過鏈接到達當前頁,HTTP_REFERER 就保存了這個用戶的來源(來路)
舉個例子,這個例子很簡單,只是拋磚引玉而已,大家可以增加更多的功能。
如下,只有首先從「 [url]http://www.gc888.cn[/url]」登陸才能看到文件內容。
源碼:index.asp
以下是引用片段:
以下是引用片段:
<html> 
<head><title>最簡單的用asp防盜鏈</title></head> 
<body> 
<% 
Option.Explicit 
Response.Buffer=Ture 
%> 
<% 
CheckUrl([url]http://www.gc888.cn[/url]) 
%> 
<% 
Function CheckUrl(url) 
 Dim Where:Where=Request.SeverVariables("HTTP_REFERER") 
 If Where=url Then 
  Call main() 
 Else 
  Response.write("很抱歉,您必須從"&url&"訪問才能進來!") 
 End if 
End Function 
%> 
<% 
Sub main() 
 Response.write("這兒是你要顯示的網頁內容") 
End sub 
%> 
</body> 
</html>

該方法對防止盜鏈文章、站外提交表單、跨站提交表單還比較有效,對於軟件盜鏈比如.rar.zip.exe等倒沒什麼作用。
不知各位讀者是否有好的主意,呵呵。
還有一種方法就是用判斷服務器及上一頁的地址來完成。
以下是引用片段:
以下是引用片段:
<% 
dim from, local 
from = request.ServerVariables("HTTP_REFERER") 
local = request.ServerVariables("SERVER_NAME") 
If mid(from, 8, local)<>Len(local) Then 
  response.write "不要從外部提交數據" 
else 
  call main() 
end if 
sub main() 
』你的主體內容 
end sub 
%> 

沒有留言: