收获

  • 熟悉 python 逆向中 exe 解包工具 pyinstxtractor.py 的使用方法【.exe -> .pyc】

  • 熟悉 python 逆向中反编译工具 uncompyle6 的使用方法【.pyc -> .py】

  • 用反编译后生成的 struct 文件的文件头修复损坏的 .pyc 文件头


【NISACTF】ezpython


思路

查看 exeinfo pe,为 python 逆向

NISACTF-ezpython1.png

pyinstxtractor.py 进行常规解包:python pyinstxtractor.py ez_python.exe

NISACTF-ezpython2.png

根据 [*] Python version: 34 可知该 exe 文件对应的 python 版本为 python 3.4
(如:38 对应 python 3.8;310 对应 python 3.10)

在生成的文件夹下,发现一个没有后缀名的 src 文件

NISACTF-ezpython3.png

这里还存在一个没有后缀名的 struct 文件是 python 逆向后必有的,虽然没有后缀名,但它是一个 .pyc 文件

在 exeinfo pe 下查看 src 文件:

NISACTF-ezpython4.png

无法识别,但是文件头很奇怪,应该又是被修改了

用 struct 文件的文件头修复 src(将文件头第一行替换即可)

NISACTF-ezpython5.png

NISACTF-ezpython6.png

根据 python 版本,需要用 python 3.4 进行编译
得到代码:

import rsa
import base64

key1 = rsa.PrivateKey.load_pkcs1(base64.b64decode(
'LS0tLS1CRUdJTiBSU0EgUFJJVkFURSBLRVktLS0tLQpNSUlFcVFJQkFBS0NBUUVBcVJUZ0xQU3BuT0ZDQnJvNHR1K1FBWXFhTjI2Uk42TzY1bjBjUURGRy9vQ1NJSU00ClNBeEVWaytiZHpSN2FucVNtZ1l5MEhRWGhDZTM2U2VGZTF0ejlrd0taL3UzRUpvYzVBSzR1NXZ4UW5QOWY1cTYKYVFsbVAvVjJJTXB5NFFRNlBjbUVoNEtkNm81ZWRJUlB2SHd6V0dWS09OQ3BpL0taQ082V0tWYkpXcWh3WGpEQgpsSDFNVURzZ1gyVUM4b3Bodnk5dXIyek9kTlBocElJZHdIc1o5b0ZaWWtaMUx5Q0lRRXRZRmlKam1GUzJFQ1RVCkNvcU9acnQxaU5jNXVhZnFvZlB4eHlPb2wwYVVoVGhiaHE4cEpXL3FPSFdYd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mJKV3Fod1hqREJsSDFNClVEc2dYMlVDOG9waHZ5OXVyMnpPZE5QaHBJSWR3SHNaOW9GWllrWjFMeUNJUUV0WUZpSmptRlMyRUNUVUNvcU8KWnJ0MWlOYzV1YWZxb2ZQeHh5T29sMGFVaFRoYmhxOHBKVy9xT0hXWHdMSW13bTZPemFxVXpLODRGMmN1YWRiRApOc3lTb2hJR2h2M3dJQVU4TUpRZzhLYXdWd2Rwc0VoZUl3SURBUUFCCi0tLS0tRU5EIFJTQSBQVUJMSUMgS0VZLS0tLS0K'))

def encrypt1(message):
    crypto_text = rsa.encrypt(message.encode(), key2)
    return crypto_text

def decrypt1(message):
    message_str = rsa.decrypt(message, key1).decode()
    return message_str

def encrypt2(tips, key):
    ltips = len(tips)
    lkey = len(key)
    secret = []
    num = 0
    for each in tips:
        if num >= lkey:
            num = num % lkey
        secret.append(chr(ord(each) ^ ord(key[num])))
        num += 1
    return base64.b64encode(''.join(secret).encode()).decode()

def decrypt2(secret, key):
    tips = base64.b64decode(secret.encode()).decode()
    ltips = len(tips)
    lkey = len(key)
    secret = []
    num = 0
    for each in tips:
        if num >= lkey:
            num = num % lkey
        secret.append(chr(ord(each) ^ ord(key[num])))
        num += 1
    return ''.join(secret)

flag = 'IAMrG1EOPkM5NRI1cChQDxEcGDZMURptPzgHJHUiN0ASDgUYUB4LGQMUGAtLCQcJJywcFmddNno/PBtQbiMWNxsGLiFuLwpiFlkyP084Ng0lKj8GUBMXcwEXPTJrRDMdNwMiHVkCBFklHgIAWQwgCz8YQhp6E1xUHgUELxMtSh0xXzxBEisbUyYGOx1DBBZWPg1CXFkvJEcxO0ADeBwzChIOQkdwXQRpQCJHCQsaFE4CIjMDcwswTBw4BS9mLVMLLDs8HVgeQkscGBEBFSpQFQQgPTVRAUpvHyAiV1oPE0kyADpDbF8AbyErBjNkPh9PHiY7O1ZaGBADMB0PEVwdCxI+MCcXARZiPhwfH1IfKitGOF42FV8FTxwqPzBPAVUUOAEKAHEEP2QZGjQVV1oIS0QBJgBDLx1jEAsWKGk5Nw03MVgmWSE4Qy5LEghoHDY+OQ9dXE44Th0='
key = 'this is key'
try:
    result = input('please input key: ')
    if result == decrypt2('AAAAAAAAAAAfFwwRSAIWWQ==', key):
        print(decrypt1(base64.b64decode(decrypt2(flag, result))))
    elif result == key:
        print('flag{0e26d898-b454-43de-9c87-eb3d122186bc}')
    else:
        print('key is error.')
except Exception:
    e = None
    try:
        pass
    finally:
        e = None
        del e

在代码中插入:

print(decrypt2('AAAAAAAAAAAfFwwRSAIWWQ==', key))

将 key 的输出置于 input 输入之前:

flag = 'IAMrG1EOPkM5NRI1cChQDxEcGDZMURptPzgHJHUiN0ASDgUYUB4LGQMUGAtLCQcJJywcFmddNno/PBtQbiMWNxsGLiFuLwpiFlkyP084Ng0lKj8GUBMXcwEXPTJrRDMdNwMiHVkCBFklHgIAWQwgCz8YQhp6E1xUHgUELxMtSh0xXzxBEisbUyYGOx1DBBZWPg1CXFkvJEcxO0ADeBwzChIOQkdwXQRpQCJHCQsaFE4CIjMDcwswTBw4BS9mLVMLLDs8HVgeQkscGBEBFSpQFQQgPTVRAUpvHyAiV1oPE0kyADpDbF8AbyErBjNkPh9PHiY7O1ZaGBADMB0PEVwdCxI+MCcXARZiPhwfH1IfKitGOF42FV8FTxwqPzBPAVUUOAEKAHEEP2QZGjQVV1oIS0QBJgBDLx1jEAsWKGk5Nw03MVgmWSE4Qy5LEghoHDY+OQ9dXE44Th0='  
key = 'this is key'  

# ------------------------------------------------
print(decrypt2('AAAAAAAAAAAfFwwRSAIWWQ==', key))  
# ------------------------------------------------

try:  
    result = input('please input key: ')  
    if result == decrypt2('AAAAAAAAAAAfFwwRSAIWWQ==', key):  
        print(decrypt1(base64.b64decode(decrypt2(flag, result))))

得到 key 值:this is true key
用得到的 key 值执行程序,即可获得 flag


结果

flag{5236cb7d-f4a7-4080-9bde-8b9e061609ad}

NISACTF-ezpython7.png