python字节码检测框架
equip的Python项目详细描述
equip是一个小库,它有助于python字节码检测。它的api 被设计为小而灵活的,以使各种可能的仪器。
仪表是围绕在 要检测的程序的字节码。但是,开发者不需要知道 关于python字节码的任何信息。
下面的示例演示如何编写一个简单的工具来打印所有 程序中调用的方法及其参数:
import sys import equip from equip import Instrumentation, MethodVisitor, SimpleRewriter BEFORE_CODE = """ print ">> START" print "[CALL] {file_name}::{method_name}:{lineno}", {arguments} print "<< END" """ class MethodInstr(MethodVisitor): def __init__(self): MethodVisitor.__init__(self) def visit(self, meth_decl): rewriter = SimpleRewriter(meth_decl) rewriter.insert_before(BEFORE_CODE) instr_visitor = MethodInstr() instr = Instrumentation(sys.argv[1]) if not instr.prepare_program(): return instr.apply(instr_visitor, rewrite=True)
此程序需要到要检测的程序的路径,并将编译源代码 生成要插入的字节码。所有字节码都将加载到其表示中, 并且将对所有方法声明调用MethodInstrvisitor。
当需要更改(即代码实际上需要检测)时, Instrumentation将覆盖pyc文件。
之后运行检测程序不需要任何东西,只要按照您的方式执行它 通常都可以。如果注入的代码具有外部依赖项,则可以简单地修改 PYTHONPATH指向所需的模块。