Search code examples
pythonreverse-engineering

Reverse Bitwise Operators


Reverse engineering:

Is it possible to write some python code to get the unknown c variable value !
Equation: (((ord(c) << 5) | (ord(c) >> 3)) ^ 111) & 255 = 233

This was my logic:

  1. ord(c)<<5 = a this will give us ord(c) = a >> 5 , then c = chr(a >> 5)
  2. normally (ord(c) << 5) | (ord(c) >> 3)) will return (ord(c) << 5)
  3. & does a "bitwise and", but & is not reversible.

I will be so happy if someone help me, to figure out and solve the equation.
And this is the whole problem pastebin link


Solution

  • Yes, it is possible to reverse the equation, but only if the original value <=255.

    def fwd(c):
        return (((ord(c) << 5) | (ord(c) >> 3)) ^ 111) & 255
    
    
    def rev(ans):
        i = ans ^ 111   # perform the xor first, then the bit-shifts after
        return ((i << 3) | (i >> 5)) & 255
    
    print(fwd(chr(0xa5)))  # sample byte to test this out with =>219
    
    print(rev(219))  # can we reverse 219 to get back to 0xa5 or 165?
    
    print(rev(233))  # now for the value from the OP
    

    Output:

    219
    165
    52
    

    It seems that the unknown c was 52 (or the character '4' in ascii)

    Just in case the pastebin link ever goes away, it looks like the OP was trying to reverse engineer a password:

    secret = [233, 129, 9, 5, 130, 194, 195, 39, 75, 229]
    inp = ''.join(chr(rev(s)) for s in secret)
    print(inp)
    

    Output:

    4w3SomeB!T