Skip to content

Instantly share code, notes, and snippets.

@github-shakti
Created August 24, 2015 22:05
Show Gist options
  • Select an option

  • Save github-shakti/b1b0ff1d8de4b70da1df to your computer and use it in GitHub Desktop.

Select an option

Save github-shakti/b1b0ff1d8de4b70da1df to your computer and use it in GitHub Desktop.
Code : Captcha Bypass Script (PHP Version)
from mechanize import Browser
from bs4 import BeautifulSoup as bs
import pytesser
import random
enteredpnr = raw_input('Please input your 10 PNR no. : ')
while len(enteredpnr) != 10 or not enteredpnr.isdigit():
print('You have not entered a 10 digit numerical value! Please try again.')
enteredpnr = raw_input('Please input your 10 digit book no. : ')
print "PNR entered by you is "+enteredpnr
raw_input('Press Enter to Continue if correct OR restart the Script')
print "PNR Loaded into Script..."
randominteger=random.randrange(100,10000)
randomstring=str(randominteger)
print 'randomstring = '+randomstring
br=Browser()
br.set_handle_robots(False)
br.set_handle_referer(False)
br.set_handle_refresh(False)
br.addheaders = [('User-agent', 'Firefox')]
webdata = br.open('http://www.indianrail.gov.in/pnr_Enq.html')
check=br.request
soup=bs(webdata.get_data())
img=soup.find('img',id='captchaimg')
#print "__________________________________"
#print "src =" +img['src']
modsrc="http://www.indianrail.gov.in/captcha_code_file.php?rand="+randomstring
#print "modified src =" +modsrc
image_response = br.open_novisit(modsrc)
image=image_response.read()
with open( "myfile.tiff", "wb" ) as code :
code.write(image)
print pytesser.image_file_to_string('myfile.tiff')
import numpy
import cv2
import numpy as np
image=cv2.imread('myfile.tiff')
image.size
image.shape
image[0,0][0]
data=np.array(image)
r=data.shape[0]
c=data.shape[1]
for i in range(r):
for j in range(c):
k=data[i,j][0]
l=data[i,j][1]
m=data[i,j][2]
if k < 100 and l < 100 and m<100:
data[i,j][0]=255
for i in range(r):
for j in range(c):
k=data[i,j][0]
l=data[i,j][1]
m=data[i,j][2]
if l < 100 and m<100:
data[i,j][1]=255
for i in range(r):
for j in range(c):
k=data[i,j][0]
l=data[i,j][1]
m=data[i,j][2]
if m<100:
data[i,j][2]=255
cv2.imwrite('myimage2.tiff',data)
pytesscode = pytesser.image_file_to_string('myimage2.tiff')
filtercode = ''.join(e for e in pytesscode if e.isalnum())
print "pytesscode = "+pytesscode
print "filtercode = "+filtercode
br.select_form(name="pnr_stat")
br.form["lccp_pnrno1"]=enteredpnr
br.form["answer"]=filtercode
finalx = br.submit()
#print "______first submission executed_________"
#print finalx.info()
#print "_________________________________________"
#print br.title()
#print "_ _ _ _______first form has been loaded and forwaded to processing_________ _ _ _"
#print br.response().read()
br.select_form(name="MyForm")
import warnings
with warnings.catch_warnings():
warnings.filterwarnings("ignore",category=UserWarning)
br.set_handle_gzip(True)
br.addheaders = [('user-agent', ' Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.3) Gecko/20100423 Ubuntu/10.04 (lucid) Firefox/3.6.3'),
('accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'),('Accept-Language','en-US,en;q=0.5'),('Accept-Encoding', 'gzip, deflate'),('Referer','http://www.indianrail.gov.in/valid.php'),('Connection','keep-alive')]
finaly = br.submit()
#print "________"
#print finaly
#print "______"
#print finaly.info()
#print "______"
#print br.title()
soup2=bs(br.response().read())
datastring=soup2.findAll('h2',{'align':'center'})
laststring=str(datastring)
print "Result = " + laststring[20:][:-6]
print 'Train Number :'+str(soup2.findAll('td',{'class':'table_border_both'})[0])[30:][:-5]
print 'Train Name :'+str(soup2.findAll('td',{'class':'table_border_both'})[1])[30:][:-5]
print 'Travel Date :'+str(soup2.findAll('td',{'class':'table_border_both'})[2])[30:][:-5]
print 'Travel From :'+str(soup2.findAll('td',{'class':'table_border_both'})[3])[30:][:-5]
print 'Travel To :'+str(soup2.findAll('td',{'class':'table_border_both'})[4])[30:][:-5]
print 'Travel Class :'+str(soup2.findAll('td',{'class':'table_border_both'})[7])[30:][:-5]
print 'Passenger :'+str(soup2.findAll('td',{'class':'table_border_both'})[8])[33:][:-9]
print 'Coach Position:'+str(soup2.findAll('td',{'class':'table_border_both'})[9])[33:][:-9]
print 'Status :'+str(soup2.findAll('td',{'class':'table_border_both'})[10])[33:][:-9]
#Space Reseved for Bugs / New Filter and Processor Ideas
#8130470719
#Structure the elements into function to ease process in case of captcha failure.
#States
#-----------------------------------
#1.no internet timeout
#2.no form - captch error
#3.inavlid/flush pnr works well
#4.actual pnr gets the data but still pops processing error.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment