7

What is a good way to count up in hex and append the result to the end of a larger hex string? I am trying to format a string to be used in a padding oracle attack. The strings will be concatenated together to form an HTTP request.

I have a two 32 character hex strings. 'g' is a guess, pad is the padding oracle. Basically what I need to do is have the last byte of g count up in hex from 0x00 to 0xff.The code I have so far is:

split = [value[x:x+32] for x in range (0, len(value), 32)] #Split the CT into 16 byte chunks IV = unhexlify(split[0]) c0 = unhexlify(split[1]) c1 = unhexlify(split[2]) c2 = unhexlify(split[3]) g = unhexlify("00000000000000000000000000000000") pad = unhexlify("00000000000000000000000000000001") pad_xor_guess = xorb(g, pad) c1_prime = xorb(pad_xor_guess, c1) attack = str(hexlify(c1_prime + c2).decode()) 

'attack' will be passed into the query method that will append the attack string to the web address. Now the part I am stuck on is that I have to basically send up to 256 HTTP requests to guess one byte of the plaintext. How can I use a for loop to "count up" from 00 to ff, appending the result to g in such a way that it can be xor'd with the pad and the chosen ciphertext block? SO far I have been going down this path, but I am stuck on how to make this work with the hex strings.

for i in range(0, 20): #g = bytes([i]) print(bytes([i]),end=' ') #print(g, end=' ') 
1
  • 4
    for i in range(255): hex(i) Commented Feb 10, 2015 at 13:29

2 Answers 2

7

For given int value, hex function will give you the hex string preceded with 0x, so hex(i)[2:] gives you the hex number itself, zfill will make sure you get two digits for the single digits numbers

for i in range(256): print(hex(i)[2:].zfill(2)) 

You might also want to consider making it all caps, since some parsers rely on hex being written in capital letters, so the example will be:

for i in range(256): print(hex(i)[2:].zfill(2).upper()) 

And if you just need the full string, you don't need to append them one by one, you can create the string in one go:

hex_str = "".join([hex(i)[2:].zfill(2).upper() for i in range(256)]) 
Sign up to request clarification or add additional context in comments.

Comments

5

I guess you mean something like:

>>> for i in range(256): print "{:02x}".format(i) # or X for uppercase 00 01 02 ... fd fe ff 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.