Browse Source

ported to python3

Marius Schwarz 4 years ago
parent
commit
b4e46f6d2a
1 changed files with 14 additions and 12 deletions
  1. 14 12
      code/padd0r.py

+ 14 - 12
code/padd0r.py

@@ -3,7 +3,7 @@ from base64 import b64encode, b64decode
 from copy import deepcopy
 import logging
 import random
-
+from functools import reduce
 
 class Encoding:
     ''' Encoding Enum '''
@@ -35,7 +35,8 @@ class PaddingOracle(object):
         # if no BS is give, determine the BS
         if BS == None:
             self.BS = self.get_blocksize(self.ct)
-
+        else:
+            self.BS = BS
         self.num_blocks = self._ct_init()
 
         self.blocks = self.split_blocks(self.ct)
@@ -90,13 +91,16 @@ class PaddingOracle(object):
         splits the ciphertext in bs-large blocks
     '''
     def split_blocks(self, ct):
-        return [bytearray(ct[i:i+self.BS]) for i in xrange(0, len(ct), self.BS)]
+        return [bytearray(ct[i:i+self.BS]) for i in range(0, len(ct), self.BS)]
 
     '''
         merges the blocks together and return ciphertext
     '''
     def merge_blocks(self, blocks):
-        return "".join([str(b) for b in blocks])
+        out = bytearray()
+        for ba in blocks:
+            out.extend(ba)
+        return out
 
 
     def last_word_oracle(self, y):
@@ -136,18 +140,18 @@ class PaddingOracle(object):
         first_run = True
 
         # byte_index is counting backwards in the second-to-last block
-        for byte_index in xrange(self.BS-1, -1, -1):
+        for byte_index in range(self.BS-1, -1, -1):
 
             padding = self.BS - byte_index
 
             # gets jumped over during the first run
             if byte_index < 15:
-                for prev_byte_index in xrange(15, byte_index, -1):
+                for prev_byte_index in range(15, byte_index, -1):
                     #print("[-] Adjusting byte @ offset %d [Padding: %x]" % (prev_byte_index, padding))
                     pre_ct[prev_byte_index] = it_block[prev_byte_index] ^ padding
 
 
-            for guess in xrange(256):
+            for guess in range(256):
                 # iterate the bytes @ byte_index position
                 pre_ct[byte_index] = guess
 
@@ -212,7 +216,7 @@ class PaddingOracle(object):
         for idx in range(num_new_blocks-1, 0, -1):
             logging.info("[*] changing index %d" % idx)
             it_block = self.get_it_block(idx, local_blocks)
-            local_blocks[idx-1] = ''.join(map(lambda (x,y): chr(x^y), zip(new_blocks[idx], it_block)))
+            local_blocks[idx-1] = ''.join(map(lambda xy: chr(xy[0]^xy[1]), zip(new_blocks[idx], it_block)))
 
         x = self.merge_blocks(local_blocks)
 
@@ -295,7 +299,7 @@ class PaddingOracle(object):
         num_blocks = len(self.blocks)
         pt_blocks = []
         logging.info("[*] decrypting all %d blocks" % num_blocks)
-        for idx in xrange(num_blocks, 1, -1):
+        for idx in range(num_blocks, 1, -1):
             logging.info("\n-----[ decrypting block %d ]-----\n" %(idx))
             it_block, pt_block = self.crack_last_block(local_blocks[:idx])
             pt_blocks.append(pt_block)
@@ -334,11 +338,9 @@ class PaddingOracle(object):
         src = str(src)
         FILTER = ''.join([(len(repr(chr(x))) == 3) and chr(x) or '.' for x in range(256)])
         lines = []
-        for c in xrange(0, len(src), length):
+        for c in range(0, len(src), length):
             chars = src[c:c+length]
             hex = ' '.join(["%02x" % ord(x) for x in chars])
             printable = ''.join(["%s" % ((ord(x) <= 127 and FILTER[ord(x)]) or '.') for x in chars])
             lines.append("%04x  %-*s  %s\n" % (c, length*3, hex, printable))
         return ''.join(lines)
-
-