Thursday, May 3, 2012

Censorship Bypass

After reading a blog post from Kunjan Shah, I decided to write a Python script that translates host names into different forms:
from tkinter import *
from tkinter import messagebox

root = Tk()
textFrame = Frame(root)
entryWidget = Entry(textFrame)

def main():
  root.title("CensorshipBypass")

  entryLabel = Label(textFrame)
  entryLabel["text"] = "Enter the URL:"
  entryLabel.pack(side=LEFT)

  entryWidget["width"] = 50
  entryWidget.insert(0, "http://www.facebook.com")
  entryWidget.pack(side=LEFT)
  textFrame.pack()

  button = Button(root, text="Go", width=10, command=writeToFile)
  button.pack()

  root.mainloop()

def writeToFile():
  f = open('CensorshipBypass.html', 'w')
  global entryWidget
  strURL = entryWidget.get().strip()
  strURLWithoutHTTP = entryWidget.get().strip().replace("http://", "")
  strURLWithoutWWW = entryWidget.get().strip().replace("www.", "")
  strIPv4 = getIP(strURLWithoutHTTP)
  arrIPv4 = strIPv4.split('.')
  intIPv4Decimal = (int(arrIPv4[3]) * 1) + \
                   (int(arrIPv4[2]) * 256) + \
                   (int(arrIPv4[1]) * 65536) + \
                   (int(arrIPv4[0]) * 16777216)
  strIPv4Hex = hex(intIPv4Decimal)
  intIPv4HexDotted = strIPv4Hex[0:4] + ".0x" + \
                     strIPv4Hex[4:6] + ".0x" + \
                     strIPv4Hex[6:8] + ".0x" + \
                     strIPv4Hex[8:10]
  intIPv4OctalDotted = "0" + \
                       oct(int(arrIPv4[0]))[2:] + ".0" + \
                       oct(int(arrIPv4[1]))[2:]+ ".0" + \
                       oct(int(arrIPv4[2]))[2:]+ ".0" + \
                       oct(int(arrIPv4[3]))[2:]
  f.write('<A HREF="' + strURL + '">' + strURL + '</A><BR>')
  f.write('<A HREF="' + strURL.replace("http://", "https://") + \
          '">' + strURL.replace("http://", "https://") + '</A><BR>')
  f.write('<A HREF="' + strURLWithoutWWW + '">' + \
          strURLWithoutWWW + '</A><BR>')
  f.write('<A HREF="' + strURLWithoutWWW + '/">' + \
          strURLWithoutWWW + '/</A><BR>')
  f.write('<A HREF="' + strURLWithoutWWW + '.">' + \
          strURLWithoutWWW + '.</A><BR>')
  f.write('<A HREF="http://' + strIPv4 + '">http://' + \
          strIPv4 + '</A><BR>')
  f.write('<A HREF="http://' + str(intIPv4Decimal) + \
          '">http://' + str(intIPv4Decimal) + '</A><BR>')
  f.write('<A HREF="http://' + strIPv4Hex + '">http://' + \
          strIPv4Hex + '</A><BR>')
  f.write('<A HREF="http://' + intIPv4HexDotted + '">http://' + \
          intIPv4HexDotted + '</A><BR>')
  f.write('<A HREF="http://' + intIPv4OctalDotted + '">http://' + \
          intIPv4OctalDotted + '</A><BR>')

def getIP(host, type=None):
  import socket
  if type=="IPv6":
    ip = socket.getaddrinfo(host, 80, 0, socket.AF_INET6, socket.SOCK_STREAM)
  else:
    ip = socket.getaddrinfo(host, 80, 0, socket.AF_INET, socket.SOCK_STREAM)
  return(str(ip[0][4][0]))

main()

No comments: