面试有个师父发来个crackme

Apr 19, 2024

解密后main函数代码:

import CrackMe
while True:
    while True:
        print("(密码由字母大小写和数字组成)")
        print("请输入密码:")
        s = input()
        h = CrackMe.main(s)
        if h == "DaG42eeH7LG3PU!!":
            print("Success")
            break
    print("Fail")
    continue

被修改的base64函数代码

a = main.__code__.replace(co_argcount=1, co_cellvars=(), co_code=b'd\x01}\x01d\x02}\x02d\x03}\x03d\x04}\x04|\x00D\x00]\x1c}\x05|\x05d\x05A\x00}\x05|\x04|\x05\xa0\x00d\x06d\x07\xa1\x02\x17\x00}\x04q\x14|\x04}\x00t\x01d\x02t\x02|\x00\x83\x01d\x08\x83\x03D\x00]\x90}\x05|\x00|\x05|\x05d\x08\x17\x00\x85\x02\x19\x00}\x06d\x01\xa0\x03d\td\n\x84\x00|\x06D\x00\x83\x01\xa1\x01}\x07t\x01d\x02t\x02|\x07\x83\x01d\x0b\x83\x03D\x00]V}\x08|\x07|\x08|\x08d\x0b\x17\x00\x85\x02\x19\x00}\tt\x02|\t\x83\x01d\x0bk\x00r\xc2|\x02d\x0bt\x02|\t\x83\x01\x18\x007\x00}\x02|\td\x0cd\x0bt\x02|\t\x83\x01\x18\x00\x14\x007\x00}\t|\x01|\x03t\x04|\td\r\x83\x02\x19\x007\x00}\x01q~qF|\x01d\x0e|\x02d\r\x1a\x00\x14\x007\x00}\x01t\x01t\x02|\x01\x83\x01d\r\x1a\x00\x83\x01D\x00]L}\x05|\x01|\x05d\r\x14\x00\x19\x00}\n|\x01|\x05d\r\x14\x00d\x06\x17\x00\x19\x00}\x0b|\x01d\x00|\x05d\r\x14\x00\x85\x02\x19\x00|\x0b\x17\x00|\n\x17\x00|\x01|\x05d\r\x14\x00d\r\x17\x00d\x00\x85\x02\x19\x00\x17\x00}\x01q\xf8|\x01S\x00', co_consts=(None, "", 0, "ZQ+U7tSBEKVzyf5coCwb94Dd6raT0eLNin12Hp8mOxFuvMgIPlhRY3WjksqJAXG/", b'', 85, 1, "little", 3, compile("", "", "exec").replace(co_argcount=1, co_cellvars=(), co_code=b'|\x00]\x10}\x01t\x00|\x01d\x00\x83\x02V\x00\x01\x00q\x02d\x01S\x00', co_consts=('08b',
                                                                                                                                                         None), co_filename="", co_firstlineno=19, co_flags=115, co_freevars=(), co_kwonlyargcount=0, co_lnotab=b'', co_name="", co_names=('format', ), co_nlocals=2, co_posonlyargcount=0, co_stacksize=4, co_varnames=('.0',
                                                                                                                                                                                                                                                                                                                                                                         'byte')), "", 6, "0", 2, "!"), co_filename="", co_firstlineno=4, co_flags=67, co_freevars=(), co_kwonlyargcount=0, co_lnotab=b'', co_name="", co_names=('to_bytes',
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 'range',
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 'len',
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 'join',
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 'int'), co_nlocals=12, co_posonlyargcount=0, co_stacksize=7, co_varnames=('data',
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           'encoded_str',
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           'padding',
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           'base64_chars',
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           'ww',
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           'i',
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           'chunk',
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           'binary_str',
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           'j',
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           'six_bits',
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           'a',
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           'b'))
main.__code__ = a

还原代码和解密代码

def encode_data(data):
    # Block 1: Initialization
    encoded_str = ''
    padding = 0
    base64_chars = 'ZQ+U7tSBEKVzyf5coCwb94Dd6raT0eLNin12Hp8mOxFuvMgIPlhRY3WjksqJAXG/'
    ww = b''

    # Block 2: XOR Operation on Data
    for i in data:
        i = i ^ 85  # XOR with 85
        ww += i.to_bytes(1, 'little')

    data = ww  # Update data with modified bytes

    # Block 3: Preparation for Encoding
    # Splitting data into chunks of 3 bytes each and processing each chunk
    for i in range(0, len(data), 3):
        chunk = data[i:i+3]

        # Convert each byte in the chunk to its binary string representation
        binary_str = ''.join(format(byte, '08b') for byte in chunk)

        # Block 4: Base64-like Encoding
        for j in range(0, len(binary_str), 6):
            six_bits = binary_str[j:j+6]
            if len(six_bits) < 6:
                padding += 6 - len(six_bits)
                six_bits = six_bits + '0' * (6 - len(six_bits))
            
            # Map each 6-bit segment to the corresponding character
            index = int(six_bits, 2)
            encoded_str += base64_chars[index]

    # Block 5: Final Adjustment
    # Adjusting the final encoded string based on the calculated padding
    encoded_str += '!' * (padding // 2)

    # Swapping every two characters
    result_str = ''
    for i in range(0, len(encoded_str), 2):
        if i + 1 < len(encoded_str):
            result_str += encoded_str[i+1] + encoded_str[i]
        else:
            result_str += encoded_str[i]

    encoded_str = result_str
    return encoded_str





def decode_data(encoded_str):
    # Block 1: Reverse character swapping
    decoded_str = ""
    for i in range(0, len(encoded_str), 2):
        if i + 1 < len(encoded_str):
            decoded_str += encoded_str[i+1] + encoded_str[i]
        else:
            decoded_str += encoded_str[i]

    # Block 2: Remove padding
    decoded_str = decoded_str.rstrip('!')

    # Block 3: Base64-like Decoding
    base64_chars = 'ZQ+U7tSBEKVzyf5coCwb94Dd6raT0eLNin12Hp8mOxFuvMgIPlhRY3WjksqJAXG/'
    bits_str = ""
    for char in decoded_str:
        index = base64_chars.index(char)
        bits_str += format(index, '06b')

    # Block 4: Convert bits to bytes
    data = bytearray()
    for i in range(0, len(bits_str), 8):
        byte_str = bits_str[i:i+8]
        if len(byte_str) < 8:
            continue  # Ignore incomplete bytes at the end
        byte = int(byte_str, 2)
        data.append(byte)

    # Block 5: XOR Operation to reverse initial XOR
    original_data = bytearray(b ^ 85 for b in data)

    return bytes(original_data)





# 测试加密和解密
original_data = b"123456"
encoded_data = encode_data(original_data)
decoded_data = decode_data(encoded_data)

print("原始数据:", original_data)
print("编码后的数据:", encoded_data)
print("解码后的数据:", decoded_data)
encoded_data ="DaG42eeH7LG3PU!!"
decoded_data = decode_data(encoded_data)
print("解码后的数据:", decoded_data)