Thursday, November 25, 2010

Captcha Prompter

  • This tool is written using PyGTK2. It takes an image file name as input, shows the image to the user and asks him to type the text shown in the image. The goal is to use it inside the Unix scripts. For example, one could do 'CAPTCHA_TEXT=$(python prompt_captcha.py captcha.jpg)' in Bash and perform further actions with $CAPTCHA_TEXT. This little tool will accept png, jpg, gif and most other commonimage formats.

#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
prompts user to solve captcha image.

This tool is written using PyGTK2. It takes an image file name as input, shows the image to the user and asks him to type the text shown in the image. The goal is to use it inside the Unix scripts. For example, one could do 'CAPTCHA_TEXT=$(python prompt_captcha.py captcha.jpg)' in Bash and perform further actions with $CAPTCHA_TEXT. This little tool will accept png, jpg, gif and most other common image formats.

[*] usage: python prompt_captcha.py captcha_image
"""

__author__ =  'Babil (Golam Sarwar)'
__version__=  '0.1'

import sys
import time
import Image
import gtk
import pygtk


pygtk.require('2.0')

class getCaptcha(object):
    """performs all the GTK2 gui actions"""

    def enter_callback(self, widget, textbox):
        """callback funcion for when user presses the enter key"""
 textbox_text = textbox.get_text()
        print textbox_text
 gtk.main_quit()

    def __init__(self, captcha=None):
        #
 if captcha == None:
  print >> sys.stderr, "[*] usage: python prompt_captcha.py captcha_image"
  sys.exit(-1)

 width, height = Image.open(captcha).size
 
 window = gtk.Window(gtk.WINDOW_TOPLEVEL)
 window.set_geometry_hints(window,width+15,height+15,width+30,height+30)
        window.set_size_request(width, height)
 window.resize(width+15, height+15)
        window.set_title("GTK textbox")
        window.connect("delete_event", lambda w,e: gtk.main_quit())
 window.set_title("Enter Captcha")
 window.set_keep_above(True)
 window.set_urgency_hint(True)
 window.move(gtk.gdk.screen_width()/2 - (width+15)/2, gtk.gdk.screen_height()/2 - (height+15)/2)


        vbox = gtk.VBox(False, 0)
        window.add(vbox)

 image = gtk.Image()
 image.set_from_file(captcha)

 button = gtk.Button()
 button.set_image(image)
 button.set_relief(gtk.RELIEF_HALF)
 #button.set_tooltip_text("Type captcha then press enter \n or click the image.")
        vbox.pack_start(button, True, True, 0)

 textbox = gtk.Entry()
        textbox.set_max_length(50)
        textbox.connect("activate", self.enter_callback, textbox)
 textbox.set_text("Enter captcha & then click on the image :-) ")
        textbox.select_region(0, len(textbox.get_text()))
 vbox.pack_start(textbox, True, True, 10)


 button.connect("clicked", self.enter_callback, textbox)
 window.show()
        vbox.show()
 button.show()
 image.show()
 textbox.show()
 textbox.grab_focus()



def main():
    gtk.main()
    return 0

if __name__ == "__main__":
 if len(sys.argv) >=2: 
  getCaptcha(sys.argv[1])
 else: 
  getCaptcha()
 main()

1 comments:

Post a Comment