找回密码
 立即注册
首页 资源区 代码 .net项目防止盗链的几种实现方案

.net项目防止盗链的几种实现方案

幌斛者 2025-5-29 10:58:31
项目背景
甲方本地化上线了我们系统之后,进行安全漏洞扫描
发现了一个问题:
我们的附件路径 直接通过站点 访问的 ,在未授权的模式下,可以直接随意替换路径里的文件内容,通过浏览器拼接链接的方式打开系统里的一些附件和图片内容
因为系统内部 站点 呈现附件 也都是通过这个方式拼接呈现的。
 
 
快速调整方案一:用了 授权认证 的方式,控制附件图片的访问 
需要在.net 代码的webconfig 文件里 添加 用户控制,这里deny 拒绝所有用户
  1.   
  2.   <location path="Upload">
  3.     <system.web>
  4.       
  5.         <deny users="?" />
  6.       </authorization>
  7.     </system.web>
  8.    
  9.     <system.webServer>
  10.       <httpProtocol>
  11.         <customHeaders>
  12.          
  13.         </customHeaders>
  14.       </httpProtocol>
  15.     </system.webServer>
  16.   </location>
  17.   
  18.   <location path="Upload/BackgroundImg">
  19.     <system.web>
  20.       
  21.         
  22.       </authorization>
  23.     </system.web>
  24.   </location>
  25.   
  26.   <location path="Upload/LogoIcon">
  27.     <system.web>
  28.       
  29.         
  30.       </authorization>
  31.     </system.web>
  32.   </location>
复制代码
  同时webcofig 里的 module 节点里 添加以下代码,以保证上面的文件访问规则,能正常执行。
  1.       <remove name="UrlAuthorization" />
  2.       
  3.       <remove name="DefaultAuthentication" />
  4.       
复制代码
  
通过上述两段配置,可以快速实现 用户未登录的情况下,无法直接通过图片路径打卡图片,防止恶意获取信息。
方案一缺点:但其实这个不是最完善的方式,对于已经登录的用户,还是可以通过拼接链接,修改链接中的参数,直接渲染其他图片。
 
加强方案二:我们可以通过添加httphandler  对所有请求的 Referer 来源进行判断来 控制访问权限
      可以结合方案一使用
  1. using System;
  2. using System.Web;
  3. public class AntiLeechHandler : IHttpHandler
  4. {
  5.     private const string RefererKey = "Referer";
  6.     private const string AllowedReferer = "http://www.yoursite.com";
  7.     public void Dispose()
  8.     {
  9.     }
  10.     public void ProcessRequest(HttpContext context)
  11.     {
  12.         string referer = context.Request.Headers[RefererKey];
  13.         // 如果 Referer 为空或者不匹配允许的站点,则进行处理
  14.         if (string.IsNullOrEmpty(referer) || !referer.StartsWith(AllowedReferer))
  15.         {
  16.             context.Response.Clear();
  17.             context.Response.StatusCode = (int)System.Net.HttpStatusCode.NotFound; // 设置状态码为 404
  18.             context.Response.End();
  19.         }
  20.         else
  21.         {
  22.             // 合法请求,继续处理
  23.             string filePath = context.Server.MapPath(context.Request.Path);
  24.             if (System.IO.File.Exists(filePath))
  25.             {
  26.                 context.Response.ContentType = GetContentType(filePath);
  27.                 context.Response.WriteFile(filePath);
  28.             }
  29.             else
  30.             {
  31.                 context.Response.StatusCode = (int)System.Net.HttpStatusCode.NotFound;
  32.             }
  33.         }
  34.     }
  35.     private string GetContentType(string filePath)
  36.     {
  37.         string extension = System.IO.Path.GetExtension(filePath).ToLower();
  38.         switch (extension)
  39.         {
  40.             case ".jpg":
  41.             case ".jpeg":
  42.                 return "image/jpeg";
  43.             case ".png":
  44.                 return "image/png";
  45.             case ".gif":
  46.                 return "image/gif";
  47.             case ".pdf":
  48.                 return "application/pdf";
  49.             // 可根据需要添加更多文件类型的 MIME 类型
  50.             default:
  51.                 return "application/octet-stream";
  52.         }
  53.     }
  54.     public bool IsReusable
  55.     {
  56.         get { return false; }
  57.     }
  58. } 
复制代码
在 web.config 中添加以下配置,将所有请求指向该处理程序
  1.       <remove name="UrlAuthorization" />
  2.       
  3.       <remove name="DefaultAuthentication" />
  4.                   
复制代码
  
也可以自定义 HTTP 模块判断Referer
 
自定义 HTTP 模块可以在请求处理管道的早期介入,进行更复杂的逻辑判断,如验证签名、检查时间戳等,以确保请求的合法性和安全性。示例代码如下
  1. using System;
  2. using System.Web;
  3. public class WebHotlinkProtectionModule : IHttpModule
  4. {
  5.     private const string RefererKey = "Referer";
  6.     private const string AllowedReferer = "http://www.yoursite.com";
  7.     public void Dispose()
  8.     {
  9.     }
  10.     public void Init(HttpApplication context)
  11.     {
  12.         context.PreSendRequestHeaders += (sender, e) =>
  13.         {
  14.             string referer = context.Request.Headers[RefererKey];
  15.             // 如果 Referer 为空或者不匹配允许的站点,则进行处理
  16.             if (string.IsNullOrEmpty(referer) || !referer.StartsWith(AllowedReferer))
  17.             {
  18.                 context.Response.Clear();
  19.                 context.Response.StatusCode = (int)System.Net.HttpStatusCode.NotFound; // 设置状态码为 404
  20.                 context.Response.End();
  21.             }
  22.         };
  23.     }
  24. }
复制代码
  在 web.config 中注册自定义模块
  1.       <remove name="UrlAuthorization" />
  2.       
  3.       <remove name="DefaultAuthentication" />
  4.                   
复制代码
  
方案二缺点:因为可能存在 Referer  伪造,所以还是可能存在风险
 
 
加强方案三:对链接添加token ,通过失效控制和token 解析 防止盗链
        可以结合方案一和方案二使用
       方案三缺点:这个逻辑 目前考虑下来,可能对现有系统的调整会比较大,固还没有尝试。
 

来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
您需要登录后才可以回帖 登录 | 立即注册