[using pytesser]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | from PIL import Image from pytesser import * import urllib2, urllib def conn(url, post, header): post_data = { 'answer': post } post = urllib.urlencode(post_data) request = urllib2.Request(url, post, header) res = urllib2.urlopen(request) return res.read() def create_image_string(filename): im = Image.open(filename) return image_to_string(im) def change_rgb(filename): im = Image.open(filename) im = im.convert('RGB') x, y = im.size im_pix = im.load() r, g, b = im_pix[0, 0] for i in range(x): for j in range(y): if im_pix[i, j] != (r, g, b): im_pix[i, j] = 0 else: im_pix[i, j] = (255, 255, 255) im.save(filename) if __name__ == "__main__": path = 'C:\\Users\\ohyt0\\PycharmProjects\\untitled1\\HCAMP\\captcha.png' url = 'http://debu.kr:53201/8671d960384a6dc7410701db14af4db2/' header = { 'Cookie': 'CAPTCHA_GAME=1jccsarof6o2cr1vp0tlagjrh1;' } c = 0 while 1: res = urllib2.Request(url, None, header) res = urllib2.urlopen(res).read() if res.find('data:image/png;base64,') == -1: print res break res = res[res.find("data:image/png;base64,") + len("data:image/png;base64,"):] res = res[:res.find("'>")] res = res.decode('base64') with open(path, "wb") as f: f.write(res) change_rgb(path) answer = create_image_string(path)[:-2] res = conn(url, answer, header) print str(c) + ' : ' + answer if res.find("Correct") != -1: print "Correct" c += 1 else: c = 1 print "Wrong" #HCAMP{i_hate_captcha_chal13nge_TT} | cs |
[Using ocr library]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | import urllib, urllib2 import base64, os from PIL import Image def check(answer): url = 'http://debu.kr:53201/8671d960384a6dc7410701db14af4db2/' params = { 'answer': answer } headers = { 'User-Agent': 'Mozilla/5.0', 'Cookie': 'CAPTCHA_GAME=1jccsarof6o2cr1vp0tlagjrh1;' } req = urllib2.Request(url, urllib.urlencode(params).encode(), headers) res = urllib2.urlopen(req).read().decode("utf-8", "ignore") return "Correct" in res def main(): c = 1 while True: url = 'http://debu.kr:53201/8671d960384a6dc7410701db14af4db2/' headers = { 'User-Agent': 'Mozilla/5.0', 'Cookie': 'CAPTCHA_GAME=1jccsarof6o2cr1vp0tlagjrh1;' } req = urllib2.Request(url, None, headers) res = urllib2.urlopen(req).read().decode("utf-8", "ignore") if res.find('data:image/png;base64,') == -1: print res break res = res[res.find("data:image/png;base64,") + len("data:image/png;base64,"):] res = res[:res.find("'>")] imgbin = base64.b64decode(res) f = open("temp.png", "wb") f.write(imgbin) f.close() img = Image.open("temp.png") img.save("temp.bmp") os.system("temp.exe") f = open("temp.txt", "rb") answer = f.read().decode() f.close() print str(c) + ' : ' + answer if check(answer): print "Correct" c += 1 else: c = 1 print "Wrong" if __name__ == '__main__': main() | cs |
이번에 갔었던 17회 해킹캠프에서 출제한 Captcha 문젠데 그때는 위에있는 코드로 플래그를 얻었고 나중에 CTF가 끝나고 다른 ocr 라이브러리 이용해서 문제 푸셨다는 분이 코드를 올려주셔서 좀만 수정해서 사용하였다.
확실히 아래있는 코드가 정확성이 좋다.
'Programming > ▷ Python' 카테고리의 다른 글
python ror, rol source (0) | 2018.03.09 |
---|---|
base64 encode, decode source (0) | 2018.03.09 |
ocr library module PIL, pytesser (0) | 2018.03.01 |