Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from PIL import Image
- import os
- import time, random, string
- import qrtools
- import glob
- import requests
- import shutil
- import urllib, urllib2
- class qrcode:
- pies1 = []
- pies2 = []
- imgs = [0,1,2,3,4,5,6,7,8]
- # pos = [(0,0), (82,0), (82*2,0), (0,82), (82*2,82), (0,82*2), (82,82*2), (82*2,82*2)]
- # arenas = [(0, 0, 82, 82), (82, 0, 82*2, 82), (82*2, 0, 82*3, 82), (0, 82, 82, 82*2), (82*2, 82, 82*3, 82*2), (0, 82*2, 82, 82*3), (82, 82*2, 82*2, 82*3), (82*2, 82*2, 82*3, 82*3), (82, 82, 82*2, 82*2)]
- position = [(0, 0), (82, 0), (164, 0), (0, 82), (82, 82), (164, 82), (0, 164), (82, 164), (164, 164)]
- arenas = [(0, 0, 82, 82), (82, 0, 164, 82), (164, 0, 246, 82), (0, 82, 82, 164), (164, 82, 246, 164), (0, 164, 82, 246), (82, 164, 164, 246), (164, 164, 246, 246), (82, 82, 164, 164)]
- pos = [(0,0) , (123,0), (245,0), (0,123), (245,123), (0,245), (123,245), (245,245)]
- qrImg = Image.new('RGB', (246,246))
- @classmethod
- def __init__(self, color):
- self.color = color
- # pass
- def printX(self):
- print self.pies1
- print self.pies2
- print self.pie3
- print self.imgs
- def addPies1(self, img):
- self.pies1.append(img)
- def addPies2(self, img):
- self.pies2.append(img)
- def addPie3(self, img):
- self.pie3 = img
- def setImg(self, img, index):
- self.imgs[index] = img
- def toQrCode(self):
- for i in xrange(len(self.imgs)):
- self.qrImg.paste(self.imgs[i], self.position[i])
- self.qrImg.save("qrcode.png")
- def decode(self):
- qr = qrtools.QR()
- qr.decode("qrcode.png")
- print qr.data
- return qr.data
- def rotate(self, img, x):
- return img.rotate(x)
- def cropByIndex(self, img,i):
- return self.cropImage(img, self.arenas[i])
- def cropImage(self, img, area):
- return img.crop(area)
- def find_bottom_right(self, pix):
- return pix[10,28]==black
- def checkBottom(self, pix1, pix2):
- for i in xrange(82):
- if pix1[i,81]!=pix2[i,0]:
- return False
- return True
- def checkTop(self, pix1, pix2):
- for i in xrange(82):
- if pix1[i,0]!=pix2[i,81]:
- return False
- return True
- def checkRight(self, pix1, pix2):
- for i in xrange(82):
- if pix1[81,i]!=pix2[0,i]:
- return False
- return True
- def checkLeft(self, pix1, pix2):
- for i in xrange(82):
- if pix1[0,i]!=pix2[81,i]:
- return False
- return True
- def sort(self):
- for img in self.pies2:
- if self.checkLeft(self.imgs[8].load(), img.load()):
- self.setImg(img,7)
- self.pies2.remove(img)
- for img in self.pies2:
- if self.checkTop(self.imgs[8].load(), img.rotate(90).load()):
- self.setImg(img.rotate(90),5)
- self.pies2.remove(img)
- for i in xrange(0, 360, 90):
- if self.checkTop(self.imgs[7].load(),self.pie3.rotate(i).load()):
- self.setImg(self.pie3.rotate(i), 4)
- for img in self.pies2:
- if self.checkTop(self.imgs[4].load(), img.rotate(180).load()):
- self.setImg(img.rotate(180),1)
- self.pies2.remove(img)
- for img in self.pies2:
- if self.checkLeft(self.imgs[4].load(), img.rotate(270).load()):
- self.setImg(img.rotate(270),3)
- self.pies2.remove(img)
- for img in self.pies1:
- if self.checkLeft(self.imgs[1].load(), img.rotate(180).load()):
- self.setImg(img.rotate(180),0)
- self.pies1.remove(img)
- for img in self.pies1:
- if self.checkRight(self.imgs[1].load(), img.rotate(90).load()):
- self.setImg(img.rotate(90),2)
- self.pies1.remove(img)
- for img in self.pies1:
- if self.checkLeft(self.imgs[7].load(), img.rotate(270).load()):
- self.setImg(img.rotate(270),6)
- self.pies1.remove(img)
- def save(self):
- for i in xrange(len(self.pies1)):
- self.pies1[i].save("1_%d.png" % i)
- for i in xrange(len(self.pies2)):
- self.pies2[i].save("2_%d.png" % i)
- self.pie3.save("3.png")
- self.imgs[8].save("4.png")
- def crop(self, files):
- for file in files:
- img = Image.open(file)
- pix = img.load()
- for n in xrange(len(self.pos)):
- i,j = self.pos[n]
- if pix[i,j] == self.color:
- cropped_img = self.cropByIndex(img, n)
- if n in [0,2,5,7]:
- if n==0:
- cropped_img = self.rotate(cropped_img, 180)
- if n==2:
- cropped_img = self.rotate(cropped_img, 270)
- if n==5:
- cropped_img = self.rotate(cropped_img, 90)
- if self.find_bottom_right(cropped_img.load()):
- self.setImg(cropped_img, 8)
- else:
- self.addPies1(cropped_img)
- continue
- if n in [1,3,4,6]:
- if n==1:
- cropped_img = self.rotate(cropped_img, 180)
- if n==3:
- cropped_img = self.rotate(cropped_img, 90)
- if n==4:
- cropped_img = self.rotate(cropped_img, 270)
- self.addPies2(cropped_img)
- check = True
- for i in xrange(83,width-82):
- if pix[i,83] not in (black, self.color):
- check = False
- break
- if check:
- cropped_img = self.cropByIndex(img, 8)
- self.addPie3(cropped_img)
- def download(url,filename):
- response = urllib2.urlopen(url).read()
- open(filename,"wb").write(response)
- def getImages(url):
- url = url.replace(":33654/",":33654/images/")
- for i in ["R","L","U","D","F","B"]:
- download(url+"_"+i+".png", "qr/"+i+".png")
- files = ['qr/R.png', 'qr/L.png', 'qr/U.png', 'qr/D.png', 'qr/F.png', 'qr/B.png']
- black = (0,0,0)
- width = 245
- colors = [(0, 81, 186), (255, 213, 0), (255,255,255), (196, 30, 58), (255, 88, 0), (0, 158, 96)]
- url = "http://qubicrube.pwn.seccon.jp:33654/02c286df1bbd7923d1f7"
- while 1:
- getImages(url)
- for color in colors:
- qr = qrcode(color)
- qr.crop(files)
- qr.sort()
- qr.toQrCode()
- decoded = qr.decode()
- if "SECCON{" in decoded:
- break
- if "http://qubicrube.pwn.seccon.jp:33654/" in decoded:
- url = decoded
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement