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