Advertisement
Guest User

Untitled

a guest
Jun 13th, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.53 KB | None | 0 0
  1. #!/usr/bin/env python2
  2. # -*- coding: utf-8 -*-
  3. """
  4. Created on Tue Jan 16 09:46:22 2018
  5.  
  6. @author: archil
  7. """
  8. from selenium.webdriver.chrome.options import Options
  9. from selenium import webdriver
  10. import selenium.webdriver.chrome.service as service
  11. import json
  12. from flask import Flask,request,Request
  13. app = Flask(__name__)
  14. import datetime
  15. import uuid
  16. from random import randint
  17. #from selenium.webdriver.common import keys
  18. import time
  19. import zipfile
  20. from PIL import Image
  21. import numpy
  22. import pytesseract
  23. import cv2
  24. #from __future__ import print_function
  25. import os
  26. import sys
  27. import tensorflow as tf
  28. import keras.backend.tensorflow_backend as ktf
  29. from keras.models import load_model
  30. dirpath = os.getcwd()
  31. sys.path.append('..')
  32. from utils import display_examples, RotNetDataGenerator, angle_error
  33. #from pyvirtualdisplay.smartdisplay import Display
  34. global driver
  35. global canvas
  36. global svc
  37. #global display
  38. #from pyvirtualdisplay import Display
  39.  
  40. gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.05,
  41. allow_growth=False)
  42. session = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options))
  43. ktf.set_session(session)
  44. pytesseract.pytesseract.tesseract_cmd='/usr/local/bin/tesseract'
  45.  
  46. model_location = os.path.join(dirpath, 'models', 'rotnet_funcaptcha_resnet50.hdf5', )
  47. model = load_model(model_location, custom_objects={'angle_error': angle_error})
  48.  
  49.  
  50. class captcha_status():
  51. SOLVE_CHALLENGE = 0
  52. COMPLETED_FAILURE = 1
  53. COMPLETED_SUCCESSFULLY = 2
  54. OUT_OF_TIME = 3
  55.  
  56.  
  57.  
  58. def findfuncaptchacanvas(screenjson):
  59. global driver
  60. global canvas
  61. global svc
  62. # print (screenjson)
  63. resp = screenjson #json.loads(screenjson)
  64. print resp
  65. proxy = resp['proxy']
  66. useragent = resp['user-agent']
  67. windowsize = resp['window-size']
  68. proxy = proxy.split(':', 3)
  69.  
  70. ip, port, user, pswd = proxy[0], proxy[1], proxy[2], proxy[3].replace('\n', '')
  71. manifest_json = """
  72. {
  73. "version": "1.0.0",
  74. "manifest_version": 2,
  75. "name": "Chrome Proxy",
  76. "permissions": [
  77. "proxy",
  78. "tabs",
  79. "unlimitedStorage",
  80. "storage",
  81. "<all_urls>",
  82. "webRequest",
  83. "webRequestBlocking"
  84. ],
  85. "background": {
  86. "scripts": ["background.js"]
  87. },
  88. "minimum_chrome_version":"22.0.0"
  89. }
  90. """
  91.  
  92. background_js = """
  93. var config = {
  94. mode: "fixed_servers",
  95. rules: {
  96. singleProxy: {
  97. scheme: "http",
  98. host: "%(host)s",
  99. port: parseInt(%(port)d)
  100. },
  101. bypassList: ["foobar.com"]
  102. }
  103. };
  104. chrome.proxy.settings.set({value: config, scope: "regular"}, function() {});
  105. function callbackFn(details) {
  106. return {
  107. authCredentials: {
  108. username: "%(user)s",
  109. password: "%(pass)s"
  110. }
  111. };
  112. }
  113. chrome.webRequest.onAuthRequired.addListener(
  114. callbackFn,
  115. {urls: ["<all_urls>"]},
  116. ['blocking']
  117. );
  118. """ % {
  119. "host": ip,
  120. "port": int(port),
  121. "user": user,
  122. "pass": pswd,
  123. }
  124.  
  125. pluginfile = 'proxy_auth_plugin'+str(uuid.uuid4())+'.zip'
  126.  
  127. with zipfile.ZipFile(pluginfile, 'w') as zp:
  128. zp.writestr("manifest.json", manifest_json)
  129. zp.writestr("background.js", background_js)
  130. # remotedriver = webdriver.Remote(desired_capabilities=webdriver.Chrome)
  131. # display = Display(backend='xephyr',visible=0, size=(1920, 1080))
  132. # display.start()
  133. service_args = ["--verbose","--log-path=fakeapi.log"]#,"--remote-debugging-port=9222"
  134.  
  135. co = Options()
  136. # co.setExperimentalOption("debuggerAddress", "127.0.0.1:9222");
  137. # co.add_argument('--url-base=/wd/hub')
  138. # co.add_argument('--headless')
  139. # co.add_argument('--disable-gpu')
  140. co.add_argument('--no-sandbox')
  141. co.add_argument('--disable-setuid-sandbox')
  142. co.add_argument('--user-data-dir=/tmp/cdriver')
  143. # co.add_argument("--disable-extensions")
  144. # co.add_argument('--no-proxy-server')
  145. # co.add_argument("--disable-infobars");
  146. # co.add_argument('--single-process')
  147. co.add_extension(pluginfile)
  148. co.add_argument(useragent)
  149. co.add_argument(windowsize)
  150. svc = service.Service(executable_path=dirpath+'/chromedriver',service_args=service_args)
  151. svc.start()
  152. driver = webdriver.Remote(command_executor=svc.service_url,desired_capabilities=co.to_capabilities())#,chrome_options=co,service_args=service_args)
  153. # driver.set_window_size(int(windowsize['width']), int(windowsize['height']))
  154. driver.get('file://' + dirpath + '/fun_loader.html')
  155. time.sleep(7.5)
  156.  
  157. canvas = driver.find_element_by_id('CAPTCHA')
  158. os.remove(os.path.join(dirpath,pluginfile))
  159.  
  160.  
  161. def fun_click(x, y):
  162. # global driver
  163. action = webdriver.ActionChains(driver)
  164. action.move_to_element_with_offset(canvas, x, y)
  165. action.click()
  166. action.perform()
  167.  
  168.  
  169. def clockwise_rotate():
  170. # left arrow click rotates img clockwise
  171. fun_click(42, 138)
  172. time.sleep(2)
  173.  
  174.  
  175. def anti_clockwise_rotate():
  176. # right arrow click rotate img anticlockwise
  177. fun_click(255, 146)
  178. time.sleep(2)
  179.  
  180.  
  181. def image_done():
  182. # try again, done, etc
  183. fun_click(150, 225)
  184. time.sleep(2)
  185.  
  186.  
  187. def start_challenge():
  188. fun_click(150, 210)
  189. time.sleep(2)
  190.  
  191. def get_captcha_status(picture):
  192. global driver
  193. # for i in range(1, 5):
  194. # grey scale for best results
  195. canvas_img = picture
  196.  
  197. filename = "{}.png".format(os.getpid())
  198.  
  199. cv2.imwrite(filename, canvas_img)
  200. # img= cv2.imread(filename, cv2.IMREAD_COLOR)
  201. # edges = cv2.Canny(img,100,200)
  202. # cv2.imwrite("img.png", edges)
  203. # img = Image.fromarray(edges)
  204. img = Image.open(filename)
  205. # print filename
  206. text_read = pytesseract.image_to_string(img).lower()
  207. os.remove(filename)
  208.  
  209. print(text_read)
  210.  
  211. if text_read.__contains__('too much time'):
  212. return captcha_status.OUT_OF_TIME
  213. elif text_read.__contains__('the ball'):
  214. return captcha_status.SOLVE_CHALLENGE
  215. elif text_read.__contains__('whoops'):
  216. return captcha_status.COMPLETED_FAILURE
  217.  
  218. return captcha_status.COMPLETED_SUCCESSFULLY
  219.  
  220. def getscreenshot():
  221. size = canvas.size
  222. location = canvas.location
  223. centerX=139
  224. centerY=151
  225. radius=53
  226. centerX1=112
  227. centerY1=112
  228. radius1=110
  229. time.sleep(2)
  230.  
  231.  
  232. firstscreenshotname = str(uuid.uuid4())+'.png'
  233. driver.save_screenshot(firstscreenshotname)
  234.  
  235. im = Image.open(firstscreenshotname) # uses PIL library to open image in memory
  236. os.remove(firstscreenshotname )
  237. left = location['x']
  238. top = location['y']
  239. right = location['x'] + size['width']
  240. bottom = location['y'] + size['height']
  241. im = im.crop((left, top, right, bottom)) # defines crop points
  242. im = numpy.array(im)
  243. # mask = numpy.zeros((im.shape[0],im.shape[1]))
  244. # for i in range(im.shape[0]):
  245. # for j in range(im.shape[1]):
  246. # if (i-centerX)**2 + (j-centerY)**2 < radius**2:
  247. # mask[i,j] = 1
  248. # newIm = numpy.empty(im.shape,dtype='uint8')
  249. # newIm[:,:,:3] = im[:,:,:3]
  250. # newIm[:,:,3] = mask*255
  251.  
  252. im = im[centerX-radius:centerX+radius,centerY-radius:centerY+radius]
  253. im = cv2.resize(im, (224,224))
  254. rows, cols, channels = im.shape
  255. newIm = numpy.empty((rows, cols,3),dtype='uint8')
  256. newIm[:,:,:3] = im[:,:,:3]
  257. for i in range(im.shape[0]):
  258. for j in range(im.shape[1]):
  259. if (i-centerX1)**2 + (j-centerY1)**2 >= radius1**2:
  260. newIm[i,j]= [255,255,255]
  261.  
  262. # rows, cols, channels = newIm.shape
  263. # print('Rows:%s Cols:%s channels:%s',str(rows),str(cols),str(channels))
  264.  
  265. newIma= Image.fromarray(newIm, "RGB")
  266. newIma.save(os.path.join(dirpath,firstscreenshotname))
  267. newIma.save(os.path.join(dirpath,'pictures',firstscreenshotname))
  268. return newIma
  269.  
  270.  
  271. def getfullscreenshot():
  272. firstscreenshotname = str(uuid.uuid4())+'.png'
  273. driver.save_screenshot(firstscreenshotname)
  274.  
  275. pic = Image.open(firstscreenshotname)
  276. os.remove(firstscreenshotname )
  277. return numpy.asarray(pic)
  278.  
  279. def getlog():
  280. entries = driver.get_log("browser")
  281. for entry in entries: # check the logged timestamp if it is logged in last sec
  282. print (datetime.datetime.utcnow() - datetime.timedelta(seconds=30))
  283. print entry["timestamp"]
  284. if (entry["timestamp"] > time.mktime(datetime.datetime.utcnow() - datetime.timedelta(seconds=15))) and (entry["level"]=="INFO") :
  285. return entry["message"]
  286. else:
  287. return None
  288.  
  289. def correctangle(angle):
  290. # print angle
  291. for i in range(0,360,360/7):
  292. # print abs(i-numpy.argmax(angle))
  293. if(abs(i-numpy.argmax(angle, axis=1))<360/14):
  294. return numpy.argmax(angle, axis=1)
  295.  
  296. #def resizepicture(picture):
  297. # cv.imwrite
  298.  
  299. def getclicks (picture):
  300. screenname = str(uuid.uuid4())+'.png'
  301. cv2.imwrite( screenname,numpy.asarray(picture))
  302. picture = cv2.imread(screenname, cv2.IMREAD_COLOR)
  303. os.remove(screenname)
  304. picture = cv2.resize(picture, (224,224))
  305. ## picture = resizepicture(picture)
  306. ## if(request.files['file'] is None):
  307. ## return json.dumps({ "error": "file is not submitted" }), 500
  308. ## if(imghdr.what( request.files['file'])!='png'):
  309. ## return json.dumps({ "error": "file is not a PNG picture" }), 500
  310. rows, cols, channels = picture.shape
  311. # print('Rows:%s Cols:%s channels:%s', str(rows), str(cols), str(channels))
  312. # col = None
  313. angle = model.predict(numpy.expand_dims(picture,axis=0))
  314. print screenname
  315. correctedangle = correctangle(angle)
  316. print correctedangle
  317. return round(correctedangle/(360/7))
  318.  
  319.  
  320. @app.route("/solvefuncaptcha",methods=['POST'])
  321. def hello():
  322.  
  323. print 'start'
  324. screenjson = request.get_json(force=True)
  325. # print screenjson
  326. # disp = Display(backend='xvfb',visible=0,size=(1920, 1080))
  327. # disp.start()
  328. findfuncaptchacanvas(screenjson)
  329. print 'start of method'
  330. start_challenge()
  331. # image_index =0
  332. while True:
  333. screenshot = getfullscreenshot()
  334. # cv2.imwrite (screenshot,'scr.png')'
  335. result = get_captcha_status(screenshot)
  336.  
  337. if result is captcha_status.COMPLETED_SUCCESSFULLY:
  338. print 'completed successfully'
  339. message = getlog()
  340.  
  341. driver.close()
  342. # disp.stop()
  343. if message:
  344. return message
  345. else:
  346. return 'failed'
  347. time.sleep(5)
  348. break
  349.  
  350.  
  351. elif result is captcha_status.SOLVE_CHALLENGE:
  352. print 'start of solve challenge'
  353. picture = getscreenshot()
  354. clicks = randint(1,6) #getclicks(picture)
  355. print clicks
  356. for i in range (0, int(clicks)):
  357. clockwise_rotate()
  358. image_done()
  359. print 'end of solve challenge'
  360.  
  361. elif result is captcha_status.COMPLETED_FAILURE:
  362. print 'completed failure'
  363. image_done()
  364. time.sleep(5)
  365.  
  366. elif result is captcha_status.OUT_OF_TIME:
  367. print 'out of time'
  368. image_done()
  369. time.sleep(5)
  370.  
  371.  
  372.  
  373.  
  374. # findfuncaptchacanvas(screenjson)
  375. #
  376. # checkmodelforpicture (picture)
  377. #
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement