Topics


Blogs


Forums


Samples


Media


Labs


Resources

 




DevCentral > Weblogs > Lori MacVittie - Two Different Socks
 Brandalize your content with network-side scripting
posted on Wednesday, February 11, 2009 4:41 AM

There's something about bacon that just makes some folks happy (me included). Alan is our resident "bacon makes everything better" team member, and so as a team we're always on the lookout for interesting bacon sites. Twitter, too, is full of baconites, and I often find interesting nuggets there on, shall we say, nothing if not eccentric bacon-oriented sites.

After finding Bacolicio.us, however, I became technically intrigued. Given a domain as part of the URI, the site will overlay a very tasty and realistic image of bacon over the requested site. Part proxy, part prank, the site was an amusing diversion into the capabilities of HTML these days.

clip_image002

Where this gets interesting is how the site works. It strips off the domain sent as part of the URI and then builds a page that includes the requested site in an IFRAME with a DIV overlay of the bacon image (which is hosted on S3, by the way, in case you were looking for yet another use case for cloud computing environments).

Sound familiar? Perhaps a bit like modern phishing attacks but in reverse? That's because this entertaining site is very much like current attack models that inject the same type of code into a site, usually via XSS or SQL injection, and use the overlay to hide the fact that they're capturing user names and passwords and silently sending them off to be collected where they will later be used to send Nigerian princes all your money.

The reason I bring that tidbit up is that while the technique of overlaying data/information/images on a page has a variety of actual, real business use, it's not exactly secure. In fact, depending on the implementation it can be quite dangerous, especially if it's implemented in a way that's reusable and easily turned on and off. Any piece of code that includes content external to the document via HTTP or language-specific "include" mechanisms is a potential security hole and should be treated as a possible attack vector.

But there are valid uses for such techniques: important announcements that are only meant to be displayed for specific periods of time, branding, and temporary site status information are among the possible valid uses of such techniques. I'm sure there are a plethora of other examples; that's part of the awesomeness that is the Internet and technology - someone is always coming up with a way to implement something cool using existing technology in a way that's not been thought of before.

A much better, more secure option is to use network-side scripting to insert the overlay. This removes the need to use more dangerous techniques like IFRAMEs and other methods of dynamically including content from an external, possibly (probably) untrusted, source.

The actual implementation of inserting an overlay is a fairly simple thing: find the end of the document and insert the appropriate HTML/CSS code to display the overlay into the document, then return to the user.

I'm including the network-side scripting code I implemented to insert in an overlay on every page served by my local BIG-IP. It finds the </body> tag and replaces it with a DIV containing an image and text for the overlay. The advantage of using network-side scripting to insert the overlay is that it doesn't rely on external content and therefore doesn't open up your application to potential exploitation. It is, in a nutshell, safer than the alternatives.

You should be able to use any network-side scripting technology, such as mod_rewrite, to implement a similar solution. As long as you are able to fully inspect the HTTP payload, and transform it, you, too, can "brandalize" your content.

Or put an image of bacon over every page. As an April Fool's Day joke, of course.

 

Thanks to many of the users here at DevCentral, especially Blondie, who wrote the core find/replace code in the forums for this iRule. You can see the actual iRule running on our (Don and I) BIG-IP here. Note the overlay of the "powered by BIG-IP" and the F5 logo; that's the piece that's inserted by the iRule.

when HTTP_RESPONSE {
HTTP::collect 4294967295
}

when HTTP_RESPONSE_DATA {
set find ""
set replace
[your image and/or message here]
"
set offset 0
set diff [expr [string length $replace] - [string length $find]]

set indices [regexp -all -inline -indices $find [HTTP::payload]]
puts "Got Payload \n[HTTP::payload]"
if { $indices ne "" } {
puts "iRule: wsu - Search Found $indices"
} else {
puts "iRule: wsu - Search Not Found"
}

foreach idx $indices {
set start [expr [lindex $idx 0] + $offset]
set end [expr [lindex $idx 1] + $offset]
set len [expr {$end - $start + 1}]

# replace the instance of find with the contents of replace
HTTP::payload replace $start $len $replace
puts "\nNew Payload \n[HTTP::payload]"

# modify offset if the replace string is larger or smaller
# than find.
incr offset $diff
}
}


Follow me on Twitter View Lori's profile on SlideShare friendfeedicon_facebook AddThis Feed Button Bookmark and Share



 
      

Feedback


2/11/2009 4:58 AM
Gravatar Argh. System does not like CSS/HTML content inside code formatted tags.

set replace "[stuff should go here"] is where the overlay CSS/DHTML code goes.


Lori MacVittie

2/11/2009 6:27 AM
Gravatar Mighty big script indeed. How long did it take you to write it? Are users happy with such compexity for simple find/replace? I could see a 3 liner like:

$body = read.html.response ()
$body = regex ($body, "search", "replace", "g")
send.html.response ($body)

JC

2/11/2009 6:39 AM
Gravatar @JC

It took about 15 minutes. I'm sure that users here have better/more efficient ways of writing the functionality (I am just an egg).

Yes, very short. Abstraction has disadvantages in other areas, however. Easier for the implementer, harder on the system to execute. Hence the speed of C over C++, and C/C++ over Java/C#/PHP/ASP/etc...

The more layers you add, the more computationally expensive it gets.






Lori MacVittie

2/11/2009 8:30 AM
Gravatar There are other ways to write this functionality but using search with index replacement causes lower CPU usage that a more simpler iRule. Part of writing iRules is taking into consideration that it will likely be run 100's of 1000's of times per second and every CPU instruction you can save will likely mean more connections you can handle. Heck, this could have been written with a single regsub but that would have been very costly.

Also, since I don't know what you were using for your "find" string, this could be wrong, but a regexp might be able to be replaced with a string command equivalent.

Another thing to keep in mind that the stream profile on the BIG-IP is a very simple, and in most cases, a more optimal way to do search and replace.

Great article Lori - this one should have been a Tech Tip!

-Joe
Joe Pruitt

2/11/2009 8:38 AM
Gravatar One more thing to consider everyone. Network scripting can do more than visual additions. I wrote a tech tip a while ago on automating the addition of Google Analytics into your web applications in my article titled "Automated Web Analytics iRule Style" at http://devcentral.f5.com/Default.aspx?tabid=63&articleType=ArticleView&articleId=174

In that article, I also used an alternative to regexp for those that care.

-Joe
Joe Pruitt

2/11/2009 8:28 PM
Gravatar Lori and Joe, you are right, network scripting is a lot more. Having the ability to not just modify HTML but manipulate images on the fly is another step forward. With proper tools at the disposal of developers when it comes to ADCs, sky is the limit. From fairly basic HTML manipulations described above one can also look into watermarking images as they pass ADC. Or inserting content received from another source and combining it into a document before returning it to the client. Imagine how cool it is having the ability to secretly (or openly) watermark images with specific user information?
Izzy

2/16/2009 6:58 AM
Gravatar A stream profile and iRule would provide similar functionality with much lower overhead compared with buffering the response data. It would also be a simpler iRule. You'd probably also want to restrict the replacement to text based responses to avoid unnecessarily collecting binary content.

Aaorn
Aaron

2/23/2009 10:51 AM
Gravatar Aaron, how would one go about manipulating images with iRule? Let's say I want to timestamp each GIF that I serve?
Izzy
 Leave Feedback
Title  
Name  
Email
Url
Comments   
Please add 6 and 4 and type the answer here: