python-bitcoin-blockchain-parser icon indicating copy to clipboard operation
python-bitcoin-blockchain-parser copied to clipboard

Handling XOR required

Open MEZTech-LLC opened this issue 5 months ago • 0 comments

XOR obfuscation has been implemented in Bitcoin Core v28 (See: https://github.com/bitcoin/bitcoin/pull/28052)

This breaks most of the functionality contained in this lib.

The fix is pretty simple:

Either A.) Require users to use -blocksxor=0 during IBD. B.) Extract the XOR key from xor.dat within the blocks directory and use it to reverse the XOR that's applied during IBD, something like:


    xor_file = os.path.join('./bitcoin-data/blocks','xor.dat')
    with open(xor_file, 'rb') as tmp:
        xor_key = tmp.read()
    
    # Cache blockfile 
    with open(blockfile,'rb') as tmp:
        raw_data = tmp.read()
    
    def undo_xor(block_data, xor_key, truncate_at_length=None):
        chunks = len(raw_data) // len(xor_key) # TODO: Parse remainder
        csize = len(xor_key)
        d = b''
        for c in range(chunks):
            b_data = block_data[csize*c:csize*(c+1)]
            for a,b in zip(xor_key,b_data):
                d += (a ^ b).to_bytes(1)
            print(f'completed {c+1}/{chunks}', end='\r')
            if truncate_at_length is not None:
                if c > truncate_at_length:
                    return d
        return d
    
    raw = undo_xor(raw_data,xor_key, truncate_at_length=100000)

Using an optimized lib like: https://github.com/GDPSApp/xor-cipher-python for this purpose is probably necessary, as iteratively applying the XOR cipher in Python is prohibitively slow.

MEZTech-LLC avatar Jul 01 '25 17:07 MEZTech-LLC