Advertisement
Guest User

Untitled

a guest
Oct 9th, 2015
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.39 KB | None | 0 0
  1. # Ciphertexts are sent back and forth as ASCII Encoded Hex Strings. 0xFF will be sent as
  2. # "FF" (2 Bytes), not as "\xff" (1 Byte).
  3.  
  4. # You can use python's string.encode('hex') and string.decode('hex') to quickly convert between
  5. # raw data and string representation if you need/want to.
  6.  
  7. from twisted.internet import reactor, protocol
  8. from Crypto.Cipher import AES
  9. import os
  10. import random
  11.  
  12. PORT = 9001
  13.  
  14. KEYSIZE = 16
  15. KEY = "AAA" + "BBB" + "CCC" + '\x01' + "\x80" * 6
  16. IV = "\x00" * KEYSIZE
  17. SECRET = "lolnotflaglol"
  18. prefix = "comment1=wowsuch%20CBC;userdata="
  19. suffix = ";coment2=%20suchsafe%20very%20encryptwowww"
  20.  
  21. key = os.urandom(16)
  22. iv = os.urandom(16)
  23.  
  24. def parse_profile( data):
  25. ptxt = decrypt_cbc(key, iv, data.encode('hex'))
  26. ptxt = ptxt.replace(" ","")
  27. print ptxt
  28. if ";admin=true" in ptxt:
  29. return 1
  30. return 0
  31.  
  32. def mkprofile( email):
  33. if((";" in email)):
  34. return -1
  35. prefix = "comment1=wowsuch%20CBC;userdata="
  36. suffix = ";coment2=%20suchsafe%20very%20encryptwowww"
  37. ptxt = prefix + email + suffix
  38. return encrypt_cbc(key, iv, ptxt)
  39.  
  40.  
  41.  
  42. class bcolors:
  43. HEADER = '\033[95m'
  44. OKBLUE = '\033[94m'
  45. OKGREEN = '\033[92m'
  46. WARNING = '\033[93m'
  47. FAIL = '\033[91m'
  48. ENDC = '\033[0m'
  49. BOLD = '\033[1m'
  50. UNDERLINE = '\033[4m'
  51. CLEAR = '\x1b[2J\x1b[1;1H'
  52.  
  53.  
  54. BANNER = bcolors.OKBLUE + """
  55. .--------------------------------------------.
  56. |+ [ BLACKBOX ] PUBLIC API DOCS v1.33.7 + |
  57. '--------------------------------------------'""" + bcolors.ENDC
  58.  
  59. DOCS = """
  60. | |
  61. | |
  62. |getapikey: Get an Account |
  63. |getflag:<admin_apikey> Get a Flag!!!! |
  64. | |
  65. | |
  66. '--------------------------------------------'
  67. """
  68. def pad(instr, length):
  69. if(length == None):
  70. print "Supply a length to pad to"
  71. elif(len(instr) % length == 0):
  72. print "No Padding Needed"
  73. return instr
  74. else:
  75. return instr + ' ' * (length - (len(instr) % length ))
  76.  
  77. def encrypt_block(key, plaintext):
  78. encobj = AES.new(key, AES.MODE_ECB)
  79. return encobj.encrypt(plaintext).encode('hex')
  80.  
  81. def decrypt_block(key, ctxt):
  82. decobj = AES.new(key, AES.MODE_ECB)
  83. return decobj.decrypt(ctxt).encode('hex')
  84.  
  85. def xor_block(first,second):
  86. '''
  87. Return a string containing a XOR of bytes in first with second
  88. '''
  89. if(len(first) != len(second)):
  90. print "Blocks need to be the same length!"
  91. return -1
  92.  
  93. first = list(first)
  94. second = list(second)
  95. for i in range(0,len(first)):
  96. first[i] = chr(ord(first[i]) ^ ord(second[i]))
  97. return ''.join(first)
  98.  
  99. def encrypt_cbc(key,IV, plaintext):
  100. '''
  101. High Level Function to encrypt things in AES CBC Mode.
  102. 1: Pad plaintext if necessary.
  103. 2: Split plaintext into blocks of length <keysize>
  104. 3: XOR Block 1 w/ IV
  105. 4: Encrypt Blocks, XOR-ing them w/ the previous block.
  106. '''
  107. if(len(plaintext) % len(key) != 0):
  108. plaintext = pad(plaintext,len(key))
  109. blocks = [plaintext[x:x+len(key)] for x in range(0,len(plaintext),len(key))]
  110. for i in range(0,len(blocks)):
  111. if (i == 0):
  112. ctxt = xor_block(blocks[i],IV)
  113. ctxt = encrypt_block(key,ctxt)
  114. else:
  115. tmp = xor_block(blocks[i],ctxt[-1 * (len(key) * 2):].decode('hex')) #len(key) * 2 because ctxt is an ASCII string that we convert to "raw" binary.
  116. print tmp
  117. ctxt = ctxt + encrypt_block(key,tmp)
  118. return ctxt
  119.  
  120. def decrypt_cbc(key,IV,ctxt):
  121. '''
  122. High Level function to decrypt thins in AES CBC mode.
  123. 1: Split Ciphertext into blocks of len(Key)
  124. 2: Decrypt block.
  125. 3: For the first block, xor w/ IV. For the others, xor with last ciphertext block.
  126. '''
  127. ctxt = ctxt.decode('hex') # Plain text
  128. if(len(ctxt) % len(key) != 0):
  129. print "Invalid Key."
  130. return -1
  131. blocks = [ctxt[x:x+len(key)] for x in range(0,len(ctxt),len(key))] # Blocks of plain
  132. for i in range(0,len(blocks)):
  133. if (i == 0): # For first, send plaint vs key
  134. ptxt = decrypt_block(key,blocks[i])
  135. ptxt = xor_block(ptxt.decode('hex'),IV)
  136. else: # For rest, decrypt plain
  137. tmp = decrypt_block(key,blocks[i]) # decrypt the plain w/ key
  138. tmp = xor_block(tmp.decode('hex'),blocks[i-1]) # XOR the decoded stuff and comp vs prev block
  139. ptxt = ptxt + tmp
  140. return ptxt
  141.  
  142. def decrypt(cipher):
  143. cipher = cipher.decode('hex')
  144. if (len(cipher) % 32 != 0):
  145. print len(cipher) % 32
  146. print ("[BLACKBOX] Invalid Length for API Endpoint\n")
  147. return
  148.  
  149. if (parse_profile(cipher) == 1):
  150. print ("Congratulations!\nThe Secret is: ")
  151.  
  152. else:
  153. print("[BLACKBOX] You are a normal user.\n")
  154.  
  155. def encrypt(aString):
  156. return mkprofile(aString)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement