您现在的位置是:首页 >技术交流 >py逆向-NSSCTF-[NISACTF 2022]ezpython网站首页技术交流

py逆向-NSSCTF-[NISACTF 2022]ezpython

cool breeze☆ 2023-05-17 04:00:02
简介py逆向-NSSCTF-[NISACTF 2022]ezpython

目录

题目:

学到的点:


题目:

之前没遇到过这样的题,看了大佬的wp了解很多,记录一下

放到ida中打开,尝试分析了一下,没头绪

 看了题解知道是一道py逆向的题目,需要用到这个工具pyinstxtractor.py(下载地址:https://sourceforge.net/projects/pyinstallerextractor/z

这个工具是将py文件转换成的exe文件反汇编为pyc文件的,具体介绍参考这篇文章(python py转exe逆向_python逆向exe_IYM的博客-CSDN博客和这篇文章:https://blog.csdn.net/w50feng/article/details/123418775

将本题附件和下载号的pyinstxtractor.py放到同一文件夹下,输入cmd命令,然后再输入指令

pyinstxtractor.py  文件名.exe

这样就可以了,之后会在同目录下看到解析出来的文件

 目录下存在struct.pyc和xxx.pyc文件或者src.pyc文件(其中xxx是与源文件同名的文件)将struct.pyc文件和xxx.pyc(或者src,pyc)文件在winhex中打开,因为这个.pyc文件是没有Magic Number的,我们需要根据Python版本自行补全;具体步骤是:将struct.pyc文件中E3前面的十六进制数复制到xxx.pyc(或者src.pyc)文件E3前面,这样再保存文件

这就形成了一个可以pyc转py的文件,再使用在线反编译网站,这里推荐python反编译 - 在线工具

之后就可以得到python代码

import rsa
import base64
key1 = rsa.PrivateKey.load_pkcs1(base64.b64decode('LS0tLS1CRUdJTiBSU0EgUFJJVkFURSBLRVktLS0tLQpNSUlFcVFJQkFBS0NBUUVBcVJUZ0xQU3BuT0ZDQnJvNHR1K1FBWXFhTjI2Uk42TzY1bjBjUURGRy9vQ1NJSU00ClNBeEVWaytiZHpSN2FucVNtZ1l5MEhRWGhDZTM2U2VGZTF0ejlrd0taL3UzRUpvYzVBSzR1NXZ4UW5QOWY1cTYKYVFsbVAvVjJJTXB5NFFRNlBjbUVoNEtkNm81ZWRJUlB2SHd6V0dWS09OQ3BpL0taQ082V0tWYkpXc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BeEUKVmsrYmR6UjdhbnFTbWdZeTBIUVhoQ2UzNlNlRmUxdHo5a3dLWi91M0VKb2M1QUs0dTV2eFFuUDlmNXE2YVFsbQpQL1YySU1weTRRUTZQY21FaDRLZDZvNWVkSVJQdkh3eldHVktPTkNwaS9LWkNPNldLVmJKV3Fod1hqREJsSDFNClVEc2dYMlVDOG9waHZ5OXVyMnpPZE5QaHBJSWR3SHNaOW9GWllrWjFMeUNJUUV0WUZpSmptRlMyRUNUVUNvcU8KWnJ0MWlOYzV1YWZxb2ZQeHh5T29sMGFVaFRoYmhxOHBKVy9xT0hXWHdMSW13bTZPemFxVXpLODRGMmN1YWRiRApOc3lTb2hJR2h2M3dJQVU4TUpRZzhLYXdWd2Rwc0VoZUl3SURBUUFCCi0tLS0tRU5EIFJTQSBQVUJMSUMgS0VZLS0tLS0K'))

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: ')this is true 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))

这里有一个很像的flag,拿去提交,显示错误,看到题目提示

如果你的key是fake的,那么你的flag也是fake的

那就找正确的key,分析if语句,直接在代码段的最后加上一条语句

运行就可以得到正确的key啦

 再拿正确的key运行,就可以得到正确的flag

学到的点:

  1. 对于将python代码转换成的exe文件,需要转成pyc文件,再反编译成py文件,这样就可以直接看python代码,需要使用pyinstxtractor.py这个工具来转成pyc文件
  2. 转成pyc文件后还需要修改pyc文件,将struct.pyc文件E3前的十六机制数复制到src.pyc文件E3前。这样才可以用在线工具转成py文件

 

 

风语者!平时喜欢研究各种技术,目前在从事后端开发工作,热爱生活、热爱工作。