Guest User

Untitled

a guest
Sep 27th, 2016
890
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.03 KB | None | 0 0
  1. from Crypto.Cipher import AES
  2. import base64
  3. import csv
  4.  
  5. BACK = 'back'
  6. SHIFTR = 'sr'
  7. SHIFTL = 'sl'
  8.  
  9. def is51234(a, b):
  10. i = 0
  11. ai = 0
  12. bi = 1
  13. while i < 5:
  14. if a[ai] != b[bi]:
  15. return False
  16. ai = (ai + 1) % 5
  17. bi = (bi + 1) % 5
  18. i = i + 1
  19. return True
  20.  
  21. def is23451(a, b):
  22. i = 0
  23. ai = 1
  24. bi = 0
  25. while i < 5:
  26. if a[ai] != b[bi]:
  27. return False
  28. ai = (ai + 1) % 5
  29. bi = (bi + 1) % 5
  30. i = i + 1
  31. return True
  32.  
  33. def is54321(a, b):
  34. return a[::-1] == b
  35.  
  36. def oneDifferent(a, b):
  37. i = 0
  38. oneDif = False
  39. pos = -1
  40. mode = 0
  41. while i < 5:
  42. if a[i] != b[i]:
  43. if oneDif:
  44. return False
  45. else:
  46. oneDif = True
  47. pos = i
  48. if a[i] == 'Circle' and b[i] == 'Square':
  49. mode = 2 #counterclock
  50. elif a[i] == 'Square' and b[i] == 'Diamond':
  51. mode = 2
  52. elif a[i] == 'Diamond' and b[i] == 'Triangle':
  53. mode = 2
  54. elif a[i] == 'Triangle' and b[i] == 'Circle':
  55. mode = 2
  56. elif b[i] == 'Circle' and a[i] == 'Square':
  57. mode = 1 #clock
  58. elif b[i] == 'Square' and a[i] == 'Diamond':
  59. mode = 1
  60. elif b[i] == 'Diamond' and a[i] == 'Triangle':
  61. mode = 1
  62. elif b[i] == 'Triangle' and a[i] == 'Circle':
  63. mode = 1
  64. else:
  65. print("FAILURE")
  66.  
  67. i = i + 1
  68. return (pos, mode)
  69.  
  70. def getProtocol(a, b):
  71. if is54321(a, b):
  72. return BACK
  73. if is23451(a, b):
  74. return SHIFTL
  75. if is51234(a, b):
  76. return SHIFTR
  77. x = oneDifferent(a, b)
  78. if x:
  79. return x
  80. print("FAILURE2:" + repr(a) + "," + repr(b))
  81. return False
  82.  
  83. clock = {'Circle':'Triangle','Triangle':'Diamond','Diamond':'Square','Square':'Circle'}
  84. cclock = {'Circle':'Square','Square':'Diamond','Diamond':'Triangle','Triangle':'Circle'}
  85. def applyClockMode(c, mode):
  86. if mode == 1:
  87. return clock[c]
  88. return cclock[c]
  89.  
  90. def applyProtocol(a, proto):
  91. if proto == BACK:
  92. return a[::-1]
  93. if proto == SHIFTR:
  94. return a[4:]+a[0:4]
  95. if proto == SHIFTL:
  96. return a[1:5]+a[0:1]
  97. (pos, mode) = proto
  98. (A, b, c, d, e) = a
  99. if pos == 0:
  100. return (applyClockMode(A, mode), b, c, d, e)
  101. if pos == 1:
  102. return (A, applyClockMode(b, mode), c, d, e)
  103. if pos == 2:
  104. return (A, b, applyClockMode(c, mode), d, e)
  105. if pos == 3:
  106. return (A, b, c, applyClockMode(d, mode), e)
  107. if pos == 4:
  108. return (A, b, c, d, applyClockMode(e, mode))
  109. return False
  110.  
  111.  
  112. def extend8(keylist, l):
  113. keys = []
  114. i = 0
  115. while i < l:
  116. keys.append(keylist[i])
  117. i = i + 1
  118.  
  119. i = l
  120. j = 0
  121. while i < 8:
  122. keys.append(keylist[j])
  123. i = i + 1
  124. j = (j + 1) % l
  125. return keys
  126.  
  127. out = []
  128. with open("myfile.csv", 'r') as csvfile:
  129. myreader = csv.reader(csvfile, delimiter=',', quotechar='"')
  130. for row in myreader:
  131. out.append(row)
  132.  
  133. out = out[1:]
  134. mydict = {}
  135. for elt in out:
  136. key = tuple(elt[2:7])
  137. check = ''.join(x[0] for x in key)
  138. if check.lower() != elt[7].lower():
  139. # print('bad!')
  140. continue
  141. if key in mydict:
  142. new = True
  143. for test in mydict[key]:
  144. if ''.join(test[13:17]).lower() == ''.join(elt[13:17]).lower():
  145. new = False
  146. if new:
  147. mydict[key].append(elt)
  148. else:
  149. mydict[key] = [elt]
  150.  
  151. bigcounter = 0
  152. imgs = []
  153.  
  154. def image(keylist, row):
  155. global imgs
  156. global bigcounter
  157. # print('win')
  158. try:
  159. encrypted = row[16]
  160. IV = row[15]
  161. key = b''.join([base64.b64decode(x) for x in keylist])
  162. aes = AES.new(key, AES.MODE_CBC, base64.b64decode(IV))
  163. z = aes.decrypt(base64.b64decode(encrypted))
  164. # print('code start:', base64.b64decode(encrypted)[:20])
  165. # print('iv:', IV)
  166. # print('decode start:', z[:50])
  167. if z not in imgs and z.startswith(b'\xff\xd8'):
  168. print('JPEG found!')
  169. print('key:', keylist)
  170. with open('outputfile_{0}.JPEG'.format(bigcounter), 'wb') as g:
  171. g.write(z)
  172. bigcounter += 1
  173. imgs.append(z)
  174. # if z not in imgs and z.startswith(b'\x00\x00\x00\x18\x66\x74\x79'):
  175. # print('MPEG found!')
  176. # print('key:', keylist)
  177. # with open('outputfile_{0}.mp4'.format(bigcounter), 'wb') as g:
  178. # g.write(z)
  179. # bigcounter += 1
  180. # imgs.append(z)
  181. # elif z not in imgs:
  182. # with open('o/{0}'.format(bigcounter), 'wb') as g:
  183. # g.write(z)
  184. # bigcounter += 1
  185. # imgs.append(z)
  186.  
  187. except Exception as e:
  188. neverusing = 0
  189. # print('bad!!')
  190. # print(e)
  191. # print('==============================================')
  192.  
  193. def dive(key, keylist, depth, startrow, startkey):
  194. if key in mydict.keys():
  195. for mutation in mydict[key]:
  196. newkey = tuple(mutation[8:13])
  197. proto = getProtocol(key, newkey)
  198. if not proto:
  199. continue
  200. newkey2 = applyProtocol(startkey, proto)
  201.  
  202. keylist.append(mutation[14])
  203. #newkeys = extend8(keylist, depth)
  204. #image(newkeys, startrow)
  205. if depth == 8:
  206. image(keylist, startrow)
  207. keylist.pop()
  208. continue
  209. dive(newkey2, keylist, depth+1, startrow, startkey)
  210. keylist.pop()
  211.  
  212. for startkey in mydict.keys():
  213. mutations = mydict[startkey]
  214. for mutation in mutations:
  215. newkey = tuple(mutation[8:13])
  216. dive(newkey, [mutation[14]], 2, mutation, startkey)
Advertisement
Add Comment
Please, Sign In to add comment