ISSessions CTF 2021 Programming - Image1
· 1 min read
Solving a CAPTCHA challenge programmatically using Tesseract OCR and OpenCV in Python.
Image1
Bad CAPTCHA, I’m a robot.
Solution
For this, I knew I had to use some sort of OCR that was smart enough to handle the graphic changing and inputting a raw image.
Tesseract it is!
import pytesseract
import requests
import cv2
from lxml import html
import hashlib
def image1():
s = requests.session()
pytesseract.pytesseract.tesseract_cmd = r'F:\Program Files\Tesseract-OCR\tesseract.exe'
url = "http://progimage.ctf.issessions.ca:15000/"
page = s.get(url)
tree = html.fromstring(page.content)
htmlimg = tree.xpath('///img[@alt="captch challenge graphic"]/@src')
imgfile = requests.get(url + htmlimg[0])
file = open("prog1.gif", "wb")
file.write(imgfile.content)
file.close()
img = cv2.VideoCapture("prog1.gif")
ret, frame = img.read()
text = (pytesseract.image_to_string(frame)).strip()
print(text)
resp = s.get(url + "?answer=" + text)
print(resp.content)
This python code works as follows:
- Initialize Tesseract’s home
- Get the full page contents
- Strip out the src attribute from the image
- Download the image
- Write it to a file locally
- Create an OpenCV video capture, since this is a gif file, and grab the first frame only
- Print out the text read from the image by Tesseract, just as a debug
- Send it back to the server
- Profit — aka, Flag.