Showing posts with label python. Show all posts
Showing posts with label python. Show all posts

Saturday, 30 March 2013

[Python] URL Un-shorten-er

Hi all,

I was playing with the BeautifulSoup library of Python. It is a very good library to parse and extract data out of HTML documents, even the ones that are poorly coded. So, while I was playing, I thought why not make something interesting. I looked around for inspiration and while browsing my Twitter feed, I came across many shortened URLs, why not demystify them? That's why, I wrote a small script to Un-shorten those URLs. I used a website, URLXray, to resolve the URLs. This is very naive script, I know. But still, that helped me in learning BeautifulSoup.

I'm a naive in Python, therefore, script may look a bit long.

Here's the code:


  1. #!/usr/bin/env python
  2. # Simple script to un-shorten the shortened URLs using the website - URLXray.com
  3. # Author: Rahul Binjve (@RahulBinjve)
  4. # Usage: ./urlDecode.py URL
  5. import urllib2
  6. from bs4 import BeautifulSoup
  7. import sys
  8. def main():
  9.     if len(sys.argv) < 2:
  10.         print "\nUsage: urlDecoder.py \"URL You Want to decode\""
  11.         sys.exit(1)
  12.    
  13.     result = decode(sys.argv[1])
  14.     print "\nDecoded URL is -> ", result
  15. #Decode function, all our work will be done here.
  16. def decode(userArg):
  17.    
  18.     url = "http://urlxray.com/display.php?url=" + userArg
  19.     print "\nUser provided URL -> ", userArg
  20.    
  21.     webPage = urllib2.urlopen(url)
  22.     tastySoup = BeautifulSoup(webPage)
  23.     div = str(tastySoup.find_all("div", class_ = "resultURL2"))
  24.     tastySoup = BeautifulSoup(div)
  25.     for a in tastySoup.findAll('a'):
  26.         if a.has_key('href'):
  27.             decoded = a['href']
  28.     if decoded:
  29.         return decoded
  30. #Standard Python Boilerplate
  31. if __name__ == '__main__':
  32.     main()








Thanks for reading.
Cheers.

Tuesday, 5 February 2013

HackIM Programming 100, 200, 300, 400 Write-up

HackIM Programming 100, 200, 300, 400 Write-up

This is the write-up for Programming Level (100, 200, 300, 400), Nullcon HackIM CTF 2013. All the scripts below are written in quick n' dirty manner.
Authors: Rohit Sharma (SH_Rohit)
              Rahul Binjve (RahulB)

1.    Big Fib
The resultant term was very large, so we used python. The key had 31,348 digits.

Code:
Key: is very large. Run the code to see.


2.    Lazy Baba
This one was very interesting. :)

Code:


And then we put the Crontab entry as:
*/1 * * * * /usr/bin/python script.py >> ~/key.txt
i.e. Run script.py after every 1 minute and redirect its output in key.txt.

Key: ABRAKADABRAGILIGILIGILI

3.    Harmony in Series
C++ never gave us correct answer. So we rewrote it in C and Python. Both gave us answer. Here's the python snippet.

Code:


Key: 311180.65

4.    Funky Text
This one was really confusing. So we used, sed to add a space between each character and our bash script did the calculation. Here's our very raw solution.
We had the funky text in a file named 'text'.
Code:

Key: 557

Kudos to the Madprops, for creating awesome challenges.