Skip to content

Instantly share code, notes, and snippets.

@jrmoran
Created December 26, 2011 06:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jrmoran/1520612 to your computer and use it in GitHub Desktop.
Save jrmoran/1520612 to your computer and use it in GitHub Desktop.
movistar_captcha

Simple Captcha

This uses ImageMagick and Tesseract to break Movistar's Captcha from their SMS system.

Command Line Process

File movistar.sh.

Raw Image

png

Clean Image

After running convert in.png -fill white -opaque '#c0c0c0' in.tiff

tiff

OCR

After running tesseract in.tiff text we get the text in the file result.txt

$ cat result.txt
IKEUQ

Ruby Script

Check the file movistar.rb. This generates a log file log.html

log

# Jaime Moran
require 'net/http'
def download_captcha file_name
uri = URI.parse('http://www.corporativo.telefonica.com.sv/EnviarSMSSV/faces/' +
'EnviarSMS.jsp?vrClientId=form1:imageEx1')
headers = { 'user-agent' => 'Mozilla/4.0 (Compatible; MSIE 4.0)' }
http = Net::HTTP.start(uri.host, uri.port)
response = http.request_get(uri.path, headers)
# set cookie in the header to be used by the new request
headers['cookie'] = response['set-cookie']
# request captcha image
resimg = http.request_get( uri.path + '?' + uri.query, headers)
# store the image on disk
open( file_name, 'w'){ |file| file.write(resimg.body) }
end
# clean and OCR image.
def process_image file_name
temp_file = '_temp.tiff'
temp_set = '_result'
# convert image to tiff, remove gray color and store text into set
system [ 'convert', file_name, '-fill white -opaque "#c0c0c0"', temp_file,
'&& tesseract', temp_file, temp_set ].join ' '
text = IO.read "#{temp_set}.txt"
# clean up
system "rm #{temp_file} #{temp_set}.txt"
text
end
# append to log
def log file_name, text
open( "log.html", 'a+' ){ |file|
file.write("<p> <img src='#{file_name}'/>#{text.strip}</p> \n")
}
end
# #Running
file_name = "#{Time.now.to_f}.png"
download_captcha(file_name)
text = process_image(file_name)
log(file_name, text)
convert in.png -fill white -opaque '#c0c0c0' in.tiff && tesseract in.tiff text && cat text.txt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment