惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

V2EX - 技术
V2EX - 技术
L
LangChain Blog
IT之家
IT之家
S
SegmentFault 最新的问题
博客园 - 三生石上(FineUI控件)
H
Hackread – Cybersecurity News, Data Breaches, AI and More
T
The Blog of Author Tim Ferriss
Blog — PlanetScale
Blog — PlanetScale
N
Netflix TechBlog - Medium
U
Unit 42
B
Blog RSS Feed
GbyAI
GbyAI
Microsoft Security Blog
Microsoft Security Blog
博客园 - 司徒正美
Apple Machine Learning Research
Apple Machine Learning Research
T
Threatpost
C
CERT Recently Published Vulnerability Notes
Cisco Talos Blog
Cisco Talos Blog
The Register - Security
The Register - Security
Vercel News
Vercel News
S
Schneier on Security
Spread Privacy
Spread Privacy
C
Cyber Attacks, Cyber Crime and Cyber Security
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
博客园 - 叶小钗
雷峰网
雷峰网
博客园_首页
人人都是产品经理
人人都是产品经理
P
Palo Alto Networks Blog
The Hacker News
The Hacker News
T
Tor Project blog
L
Lohrmann on Cybersecurity
Know Your Adversary
Know Your Adversary
D
Darknet – Hacking Tools, Hacker News & Cyber Security
C
Cybersecurity and Infrastructure Security Agency CISA
P
Privacy International News Feed
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
T
Tenable Blog
V
Vulnerabilities – Threatpost
大猫的无限游戏
大猫的无限游戏
博客园 - 【当耐特】
V
V2EX
Security Latest
Security Latest
A
About on SuperTechFans
Cloudbric
Cloudbric
S
Security Affairs
MongoDB | Blog
MongoDB | Blog
Y
Y Combinator Blog
Martin Fowler
Martin Fowler
TaoSecurity Blog
TaoSecurity Blog

博客园 - Edison.Feng

Objective-读Xml [转]iPhone Mapkit 之在地图加入坐标点 使用MKAnnotation和MKAnnotationView [转]Android:使用URL和URLConnection(多线程下载) 【转】httpModules 与 httpHandlers 【转】挣脱浏览器的束缚(2) - 别让脚本引入坏了事 求任何一个正数的组合,组合的规则是这个数等于1或2的整数幂之和,请列出组合的情况。 C#利用反射获取对象属性的修改情况 C#利用反射获取对象属性值 Clone:Xml序列化反序列克隆对象 [转]Linux slab 分配器剖析 [转]Linux对I/O端口资源的管理 [转]linux内核分析-初始化分析 start_kernel paging_init [转]Linux TCP/IP 协议栈的关键数据结构Socket Buffer(sk_buff ) [转]Linux系统内核接收以太帧的处理程序 母牛生小牛(递归) MS SQL:Funcation拆分行 我使用WebService:Function.asmx的惊叹!!!
[转]asp.net中利用ashx实现图片防盗链
Edison.Feng · 2010-09-10 · via 博客园 - Edison.Feng

盗链原理:看下面用httpwatch截获的http发送的数据
GET /Img.ashx?img=svn_work.gif HTTP/1.1
Accept: */*
Referer: http://www.svnhost.cn/
Accept-Language: zh-cn
UA-CPU: x86
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; CIBA)
Host: http://www.svnhost.cn/
Connection: Keep-Alive

该数据包表示请求http://www.svnhost.cn/Img.ashx?img=svn_work.gif文件。我们可以看到Referer表示上一页请求页面地址,也就是文件来源。Host表示当前请求的主机地址。
下面是一个盗链的数据包

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->GET /Img.ashx?img=svn_work.gif HTTP/1.1
Accept: */*
Referer: http://745.cc/
Accept-Language: zh-cn
UA-CPU: x86
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; CIBA)
Host: http://www.svnhost.cn/
Connection: Keep-Alive

我们可以看到,上面两个数据,表示对于同一个文件:http://www.svnhost.cn/Img.ashx?img=svn_work.gif的请求过程,这里的不同就是Referer,也就是都是请求同一个文件,但是请求的来源是不同的。因此我们可以在程序里判断是否是来源于当前服务器,来判断是否是盗链。明白原理以后,实现防盗链就非常简单了。下面以图片防盗链来实现一个演示。ASP.NET中添加一个img.ashx文件,然后后台代码如下:

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->using System;

using System.Collections;

using System.Data;

using System.Web;

using System.Web.Services;

using System.Web.Services.Protocols;

namespace GetImage

{

    ///

    /// $codebehindclassname$ 的摘要说明

    ///

    [WebService(Namespace = "http://tempuri.org/")]

    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

    public class Img : IHttpHandler

    {

        public void ProcessRequest(HttpContext context)

        {

            context.Response.ContentType = "image/jpg";

            if (context.Request.UrlReferrer != null && context.Request.UrlReferrer.Host.Equals(context.Request.Url.Host, StringComparison.InvariantCultureIgnoreCase))

                context.Response.WriteFile(context.Server.MapPath("~/" + context.Request.QueryString["img"]));

            else

                context.Response.WriteFile(context.Server.MapPath("~/logo.gif"));

        }

        public bool IsReusable

        {

            get

            {

                return false;

            }

        }

    }

}

表示如果来源不为空,并且来源的服务器和当前服务器一致,那就表示是正常访问,非盗链。正常访问文件内容。
否则就是盗链,返回网站LOGO。
你甚至可以做成随机返回正确的图片,随机返回错误图片,或者定时返回正确图片,定时返回错误图片。
然后就是图片的使用了,这时使用图片就不是直接了,而是,就是说通过img,ashx来读取图片。