Advertisement
phieulang1993

qr.py

Dec 10th, 2017
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.63 KB | None | 0 0
  1. from PIL import Image
  2. import os
  3. import time, random, string
  4. import qrtools
  5. import glob
  6. import requests
  7. import shutil
  8. import urllib, urllib2
  9.  
  10.  
  11.  
  12.  
  13.  
  14. class qrcode:
  15.     pies1 = []
  16.     pies2 = []
  17.    
  18.     imgs = [0,1,2,3,4,5,6,7,8]
  19.     # pos = [(0,0), (82,0), (82*2,0), (0,82), (82*2,82), (0,82*2), (82,82*2), (82*2,82*2)]
  20.     # 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)]
  21.     position = [(0, 0), (82, 0), (164, 0), (0, 82), (82, 82), (164, 82), (0, 164), (82, 164), (164, 164)]
  22.     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)]
  23.     pos = [(0,0) , (123,0), (245,0), (0,123), (245,123), (0,245), (123,245), (245,245)]
  24.     qrImg = Image.new('RGB', (246,246))
  25.     @classmethod
  26.     def __init__(self, color):
  27.         self.color = color
  28.         # pass
  29.     def printX(self):
  30.         print self.pies1
  31.         print self.pies2
  32.         print self.pie3
  33.         print self.imgs
  34.        
  35.     def addPies1(self, img):
  36.         self.pies1.append(img)
  37.    
  38.     def addPies2(self, img):
  39.         self.pies2.append(img)
  40.    
  41.     def addPie3(self, img):
  42.         self.pie3 = img
  43.    
  44.     def setImg(self, img, index):
  45.         self.imgs[index] = img
  46.    
  47.     def toQrCode(self):
  48.         for i in xrange(len(self.imgs)):
  49.             self.qrImg.paste(self.imgs[i], self.position[i])
  50.         self.qrImg.save("qrcode.png")
  51.    
  52.     def decode(self):
  53.         qr = qrtools.QR()
  54.         qr.decode("qrcode.png")
  55.         print qr.data
  56.         return qr.data
  57.        
  58.     def rotate(self, img, x):
  59.         return img.rotate(x)
  60.    
  61.     def cropByIndex(self, img,i):
  62.         return self.cropImage(img, self.arenas[i])
  63.    
  64.     def cropImage(self, img, area):
  65.         return img.crop(area)
  66.        
  67.     def find_bottom_right(self, pix):
  68.         return pix[10,28]==black
  69.        
  70.     def checkBottom(self, pix1, pix2):
  71.         for i in xrange(82):
  72.             if pix1[i,81]!=pix2[i,0]:
  73.                 return False
  74.         return True
  75.    
  76.     def checkTop(self, pix1, pix2):
  77.         for i in xrange(82):
  78.             if pix1[i,0]!=pix2[i,81]:
  79.                 return False
  80.         return True
  81.    
  82.     def checkRight(self, pix1, pix2):
  83.         for i in xrange(82):
  84.             if pix1[81,i]!=pix2[0,i]:
  85.                 return False
  86.         return True
  87.    
  88.     def checkLeft(self, pix1, pix2):
  89.         for i in xrange(82):
  90.             if pix1[0,i]!=pix2[81,i]:
  91.                 return False
  92.         return True
  93.    
  94.     def sort(self):
  95.         for img in self.pies2:
  96.             if self.checkLeft(self.imgs[8].load(), img.load()):
  97.                 self.setImg(img,7)
  98.                 self.pies2.remove(img)
  99.        
  100.         for img in self.pies2:
  101.             if self.checkTop(self.imgs[8].load(), img.rotate(90).load()):
  102.                 self.setImg(img.rotate(90),5)
  103.                 self.pies2.remove(img)
  104.         for i in xrange(0, 360, 90):
  105.             if self.checkTop(self.imgs[7].load(),self.pie3.rotate(i).load()):
  106.                 self.setImg(self.pie3.rotate(i), 4)
  107.                
  108.         for img in self.pies2:
  109.             if self.checkTop(self.imgs[4].load(), img.rotate(180).load()):
  110.                 self.setImg(img.rotate(180),1)
  111.                 self.pies2.remove(img)
  112.         for img in self.pies2:
  113.             if self.checkLeft(self.imgs[4].load(), img.rotate(270).load()):
  114.                 self.setImg(img.rotate(270),3)
  115.                 self.pies2.remove(img)
  116.            
  117.         for img in self.pies1:
  118.             if self.checkLeft(self.imgs[1].load(), img.rotate(180).load()):
  119.                 self.setImg(img.rotate(180),0)
  120.                 self.pies1.remove(img)
  121.        
  122.         for img in self.pies1:
  123.             if self.checkRight(self.imgs[1].load(), img.rotate(90).load()):
  124.                 self.setImg(img.rotate(90),2)
  125.                 self.pies1.remove(img)
  126.        
  127.         for img in self.pies1:
  128.             if self.checkLeft(self.imgs[7].load(), img.rotate(270).load()):
  129.                 self.setImg(img.rotate(270),6)
  130.                 self.pies1.remove(img)
  131.    
  132.     def save(self):
  133.         for i in xrange(len(self.pies1)):
  134.             self.pies1[i].save("1_%d.png" % i)
  135.         for i in xrange(len(self.pies2)):
  136.             self.pies2[i].save("2_%d.png" % i)
  137.         self.pie3.save("3.png")
  138.         self.imgs[8].save("4.png")
  139.        
  140.     def crop(self, files):
  141.         for file in files:
  142.             img = Image.open(file)
  143.             pix = img.load()
  144.             for n in xrange(len(self.pos)):
  145.                 i,j = self.pos[n]
  146.                 if pix[i,j] == self.color:
  147.                     cropped_img = self.cropByIndex(img, n)
  148.                     if n in [0,2,5,7]:
  149.                         if n==0:
  150.                             cropped_img = self.rotate(cropped_img, 180)
  151.                         if n==2:
  152.                             cropped_img = self.rotate(cropped_img, 270)
  153.                         if n==5:
  154.                             cropped_img = self.rotate(cropped_img, 90)
  155.                         if self.find_bottom_right(cropped_img.load()):
  156.                             self.setImg(cropped_img, 8)
  157.                         else:
  158.                             self.addPies1(cropped_img)
  159.                         continue
  160.                     if n in [1,3,4,6]:
  161.                         if n==1:
  162.                             cropped_img = self.rotate(cropped_img, 180)
  163.                         if n==3:
  164.                             cropped_img = self.rotate(cropped_img, 90)
  165.                         if n==4:
  166.                             cropped_img = self.rotate(cropped_img, 270)
  167.                         self.addPies2(cropped_img)
  168.             check = True
  169.             for i in xrange(83,width-82):
  170.                 if pix[i,83] not in (black, self.color):
  171.                     check = False
  172.                     break
  173.             if check:
  174.                 cropped_img = self.cropByIndex(img, 8)
  175.                 self.addPie3(cropped_img)
  176.                
  177.  
  178. def download(url,filename):
  179.     response = urllib2.urlopen(url).read()
  180.     open(filename,"wb").write(response)
  181.  
  182. def getImages(url):
  183.     url = url.replace(":33654/",":33654/images/")
  184.     for i in ["R","L","U","D","F","B"]:
  185.         download(url+"_"+i+".png", "qr/"+i+".png")
  186.  
  187. files = ['qr/R.png', 'qr/L.png', 'qr/U.png', 'qr/D.png', 'qr/F.png', 'qr/B.png']
  188.  
  189. black = (0,0,0)
  190. width = 245
  191. colors = [(0, 81, 186), (255, 213, 0), (255,255,255), (196, 30, 58), (255, 88, 0), (0, 158, 96)]
  192. url = "http://qubicrube.pwn.seccon.jp:33654/02c286df1bbd7923d1f7"
  193. while 1:
  194.     getImages(url)
  195.     for color in colors:
  196.         qr = qrcode(color)
  197.         qr.crop(files)
  198.         qr.sort()
  199.         qr.toQrCode()
  200.         decoded = qr.decode()
  201.         if "SECCON{" in decoded:
  202.             break
  203.         if "http://qubicrube.pwn.seccon.jp:33654/" in decoded:
  204.             url = decoded
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement