[SCTF ez_cython]
-
简单分析需要调用一个cy库,找到文件"cy.cp38-win_amd64.pyd"
-
pyd文件生成:
-
编写pyx文件
\#test.pyx def say_hello_world(name): print("Hello world" % name)
-
编写setup
\#setup.py from distutils.core import setup from Cython.Build import cythonize setup(name='Hello world app', ext_modules=cythonize("test.pyx"))
-
生成pyd文件终端执行
python3 .\setup.py build_ext --inplace
-
-
终端执行报错
error: Microsoft Visual C++ 14.0 or greater is required. Get it with "Microsoft C++ Build Tools": https://visualstudio.microsoft.com/visual-cpp-build-tools/
- 分析目的:目的就是安装一个Visual Studio Build Tools,他可以在不需要完整Visual Studio IDE的情况下获得编译和构建环境.
这时候就有一个问题,我又不是没有python或者c的编译环境,为什么会这样? - 原因: 笔者安装的是Mingw环境,而报错安装所需要的环境是MSVC.而安装MSVC的最好方法就是使用Visual Studio Build Tools.,并且在Windows上,Python 官方发行版是使用 MSVC 编译的,所以setup.py编译拓展模块(c)一般需要MSVC.
- 分析目的:目的就是安装一个Visual Studio Build Tools,他可以在不需要完整Visual Studio IDE的情况下获得编译和构建环境.
-
最后生成文件test.cp311-win_amd64.pyd
-
-
拿到"cy.cp38-win_amd64.pyd"后可以进行操作分析内容
-
将.pyd文件放在当前工作目录便可以import,或者将.pyd文件放在lib/site-packages.如果无法运行,可能是缺少库导致的,或者版本不匹配(检测库的时候会显示python版本,更改版本即可)
import pefilepe = pefile.PE("your_file.pyd")for entry in pe.DIRECTORY_ENTRY_IMPORT:print(entry.dll.decode("utf-8"))
-
代码(IDE),而终端不需要加print(),因为解释器,按步骤解释会将内容输出
import cyhelp(cy)a = cy.QOOQOOQOOQOOOQ() //cy.QOOQOOQOOQOOOQ()表示生成一个instance,而cy.QOOQOOQOOQOOOQ则是一个将a当作引用print(dir(a))print(a.get_key())
-
当dir(cy.QOOQOOQOOQOOOQ)时会有输出,函数名都存在,但是好像缺少了变量
-
-
通过注入cy类,获取运算过程
import cyclass Symbol: def __init__(self, name): self.name = name def __repr__(self): return self.name def __rshift__(self, other): if isinstance(other, Symbol): expression = Symbol(f"({self.name} >> {other.name})") else: expression = Symbol(f"({self.name} >> {other})") return expression def __lshift__(self, other): if isinstance(other, Symbol): expression = Symbol(f"({self.name} << {other.name})") else: expression = Symbol(f"({self.name} << {other})") return expression def __rxor__(self, other): if isinstance(other, Symbol): expression = Symbol(f"({self.name} ^ {other.name})") else: expression = Symbol(f"({self.name} ^ {other})") return expression def __xor__(self, other): if isinstance(other, Symbol): expression = Symbol(f"({self.name} ^ {other.name})") else: expression = Symbol(f"({self.name} ^ {other})") return expression def __add__(self, other): if isinstance(other, Symbol): expression = Symbol(f"({self.name} + {other.name})") else: expression = Symbol(f"({self.name} + {other})") return expression def __and__(self, other): if isinstance(other, Symbol): expression = Symbol(f"({self.name} & {other.name})") else: expression = Symbol(f"({self.name} & {other})") return expressionclass AList: def __init__(self, nums): self.nums = [Symbol(str(num)) for num in nums] def __getitem__(self, key): return self.nums[key] def copy(self): return AList(self.nums) def __len__(self): return len(self.nums) def __setitem__(self, key, value): print(f"new_{self.nums[key]} = {value}") self.nums[key] = Symbol(f"new_{self.nums[key].name}") def __eq__(self, other): print(f"{self.nums} == {other}") return self.nums == otherinp = AList([f"a[{i}]" for i in range(32)])res = cy.sub14514(inp)if __name__ == '__main__': print(res)
- 分析(........)