open core »

TinyURL reversing

TinyURL is a online service that shortens long URL's so that they can save space in services like Twitter and others.

This is definitely a tool of great convenience, but like almost everything in life, has a drawback. Actually, two drawbacks. First, spammers, phishers and alike are using this service to mask the URL of their malicious website so that when someone sees the link, feels comfortable about clicking it, and unaware of what's coming. And second, it's slowly building a "broken" web, because if this kind of services disappear, a good portion of links on the web won't work anymore. And this can't be good.

Against the first drawback, TinyURL has a feature that allows you to send a preview link instead of an instant redirection one, but it's hardly used by anyone.

Some time ago, I developed a small online tool that unveils every tinyurl that is sent to it. But its usefulness is almost none, since no one will bother to visit another website just to unveil the tinyurl's target. So, to simplify that process, I'm developing a Firefox Extension that integrates with the tool, so that when you visit a tinyurl, before that url is loaded, is passed trough the reverser and the target is shown on a confirmation popup or just a tooltip.

While the extension is not ready, I leave you here a small example of how to reverse a tinyurl in PHP. It's very simple, and straightforward:

function reverse_tinyurl($url){
	$url = explode('.com/', $url);
	$url = ''.$url[1];
	$prev = file_get_contents($url);
	preg_match('/redirecturl" href="(.*)">/', $prev, $res);
	return $res[1];

Well, or maybe if you prefer Ruby:

require 'rubygems'
require 'net/http'

def reverse_tinyurl(url)
    url_parts = url.split('.com/')
    preview_url = "{url_parts[1]}"
    response = Net::HTTP.get_response(URI.parse(preview_url))
    original_url = response.body.scan(/redirecturl" href="(.*)">/)[0][0]

Or if you are more of a Python guy/girl:

from lxml import etree

def reverse_tinyurl(url):
    hash = url[url.find(".com/")+5:]
    preview_url = ""+hash
    parser = etree.HTMLParser()
    tree = etree.parse(preview_url, parser)
    elem = tree.findall('.//a[@id="redirecturl"]')
    if len(elem) == 1:
        return elem[0].get("href")
    return None

This examples should get you started on developing something around this.