Working Around Hot Linking Restrictions

Posted April 27, 2006 at 1:14 PM

Tags: ColdFusion

I was testing out my new functions JREGetNoCase() and JREGet() (uses Java regular expressions to return all matching substrings of a given string) by attempting to grab IMG tags off of random web sites:

 Launch code in new window » Download code as text file »

<!--- Get the images for this page. --->
<cfset arrImages = JREGetNoCase(

objHttpRequest.FileContent,
"<a[^>]+href=""?([^"">]+)[0-9]+\.jpg""?[^>]*>[\s]*<img[^>]+src=""?([^"">]+)[0-9]+\.jpg""?[^>]*[\s]*</a>"
) />

This gets all A tags that have an IMG as the only child element. The functions work perfectly. I am actually totally excited about them. But, as I was dumping out the data, I realized that only some of the images worked on my page; however, when I pasted the captured IMG source into another window, the image loaded just fine.

Very curious. After some research, I see that Apache can block access (and maybe IIS can too) to files based on header data (among other criteria). It seems that some sites were blocking my file "grabs" since they were coming from my site.

To get around this, I had to create a sepparate page that would grab the img binary using a falsified CGI value (http referer) and stream that binary data to the browser:

 Launch code in new window » Download code as text file »

<!--- Kill extra output. --->
<cfsilent>

 

<!--- Set page settings. --->
<cfsetting showdebugoutput="false" />

<!--- Param url variables. --->
<cfparam name="URL.src" type="string" default="" />

 

<!--- Get the domain of the image. --->
<cfset strDomain = REReplace( URL.src, "(\.(com|net)).+", "\1", "ONE" ) />

 

<!--- Grab the source image. --->
<cfhttp
url="#URL.src#"
method="GET"
useragent="ua"
getasbinary="yes"
result="objHttp">

<!--- Set referrer params. --->
<cfhttpparam type="CGI" name="http_referer" value="#strDomain#" encoded="false" />
</cfhttp>

</cfsilent>

 

<cfset GetPageContext().GetOut().ClearBuffer()
/><cfcontent
type="image/jpg"
variable="#objHttp.FileContent#"
/>

As you can see above, I grab the site Domain information from the actual SRC value, then I set that domain information as the CGI.http_referer for the CFHttp Get. This works like a charm (95% of the time). It doesn't have any error checking, but that could easily be worked in via the Status of the CFHttp return data.

Download Code Snippet ZIP File

Post Comment  |  Ask Ben  |  Print Page



Learning ColdFusion 9 - ColdFusion 9 tutorials, samples, examples, demos

Reader Comments

Feb 3, 2007 at 1:14 AM // reply »
165 Comments

Hmm, that's somewhat unethical, since people disable hotlinking for a reason (bandwidth costs, etc.).


Feb 3, 2007 at 1:16 AM // reply »
165 Comments

Also, the regexes could be improved. ;-)


Feb 4, 2007 at 12:48 PM // reply »
7,539 Comments

Unethical and down right impractical. If you were to hotlink images, it means that you have to put processing and the data transfer time into every single image that you display.

I don't ever see this type of thing being used to "Steal" content but rather to download content such as by an Offline-Explorer / archiving type of application.


Mar 21, 2008 at 12:24 PM // reply »
7 Comments

thanks for the code.


Post Comment  |  Ask Ben

Recent Blog Comments
Mar 18, 2010 at 6:34 PM
Exploring ColdFusion Component Runtime Class Properties And Serialization
@Ben Very useful analyses. Thank you @Elliot Thanks for additional clarification Though, it's quite a shame that getBust() failed...not defined ;) ... read »
Mar 18, 2010 at 5:35 PM
Exploring ColdFusion Component Runtime Class Properties And Serialization
Saving private properties is necessary so that you can "reconstitute" an object on the other side of the wire, or load up a serialized object you saved to disk. If it didn't save the private state o ... read »
Mar 18, 2010 at 4:04 PM
jQuery's Event Triggering, Order Of Default Behavior, And triggerHandler()
Tks! You saved-me today. it can be chained into one statement: $("#x).attr("checked","checked").triggerHandler('click'); ... read »
Mar 18, 2010 at 1:18 PM
Finally Finished Ayn Rand's Atlas Shrugged Audio Book
@joaopft, Not disputing what you say - but... If I understand you correctly, you are saying that Positivism is based on sense experience (what I experience is what is), but Quantum theory states tha ... read »
Mar 18, 2010 at 11:48 AM
Duplicate() Much Faster Than ColdFusion Query-of-Queries
I am working on a massive xml parsing, qofq app to create 2 seperate xml files. I just don't understand the concept/purpose of duplicate function, are you duplicating the data or the row, into a new ... read »
Mar 18, 2010 at 11:22 AM
Exploring ColdFusion Component Runtime Class Properties And Serialization
@Zarko, Ha ha, you know ColdFusion is my first love ;) ... read »
Mar 18, 2010 at 11:15 AM
Exploring ColdFusion Component Runtime Class Properties And Serialization
Hi Ben, nice to have you back! I already gave up on you, thinking you'll write about jQuery and iPhone for the rest our our lives! :) ... read »
Mar 18, 2010 at 10:36 AM
Ask Ben: Javascript Replace And Multiple Lines / Line Breaks
@Ben Nadel, Hey Ben, thanks for you're response. It works!! However.. if you could please kindly look at http://edeals.zzl.org/divchange2.php where I am trying it out you will see that with the " ... read »