Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Reversing the code
#1
Hello,I'm new to python and I'm starting to study it,but I have this code that it's been used to unpack a certain compressed file.

This file needs to be compressed back If I want to apply my changes to a game,so I'm trying to understand how I can make it so that instead of decompress,it does the opposite.

Is it possible to revert this code?

If someone can help me through this I would be very grateful,thanks.

Code is attached.
.py   DeobfuscateGameBootstrap.py (Size: 2.69 KB / Downloads: 57)

--------------------
import sys

def DeObfuscate(pInput, pOutput):

    inpOffset = 0
    outOffset = 0

    # Loop and deobfuscate.
    blockId = pInput[inpOffset]
    inpOffset += 1
    if blockId == 0:
        pass
        
    if blockId >= 2:
        print("block id: 0x%x" % blockId)
        while blockId != 0:
        
            pBlockPtr = inpOffset + 1
            while blockId >= 2:
            
                if (blockId & 1) == 1:
                
                    pOutput[outOffset] = pInput[inpOffset]
                    blockId = blockId >> 1
                    inpOffset = pBlockPtr
                    
                    pBlockPtr += 1
                    outOffset += 1
                    
                else:
                
                    a = pInput[inpOffset]
                    b = pInput[pBlockPtr]
                    
                    c = (a << 8) | b
                    d = c & 0x7FF
                    e = c >> 11
                    
                    if d == 0:
                        d = 0x800
                        
                    pCopyPtr = outOffset - d
                    
                    d = e & 0x1F
                    if d == 0:
                        d = 0x20
                        
                    inpOffset = pBlockPtr + 1
                    blockId = blockId >> 1
                    pBlockPtr += 2
                    
                    for i in range(d):
                        if outOffset >= len(pOutput) or pCopyPtr >= len(pOutput):
                            print("%d (%d) | %d (%d) | %d (%d)" % (outOffset, len(pOutput), pCopyPtr, len(pOutput), i, d))
                    
                        pOutput[outOffset] = pOutput[pCopyPtr]
                        pCopyPtr += 1
                        outOffset += 1
                    
            blockId = pInput[inpOffset]
            inpOffset = pBlockPtr
            
            print("block id: 0x%x" % blockId)
            
        print("bytes copied: %d" % outOffset)
        
    return outOffset


def main():

    # Open the input file and read the file into memory.
    f = open(sys.argv[1], 'rb')
    unk = f.read(4)
    inputData = f.read()
    f.close()
    
    # Deobfuscate the input buffer.
    outputData = bytearray(len(inputData) * 4)
    length = DeObfuscate(inputData, outputData)
    
    # Write the deobfuscated data to file.
    f = open(sys.argv[1] + "_dec", 'wb')
    f.write(outputData[0:length])
    f.close()
    
    
if __name__ == '__main__':
    main()
    
Larz60+ write Feb-12-2024, 11:02 PM:
Please don't attach code, instead please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply


Messages In This Thread
Reversing the code - by chocolatedisco - Feb-12-2024, 11:46 PM
RE: Reversing the code - by Baroscope - Feb-23-2024, 04:01 AM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020