Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Your Python - your rules

Your Python - your rules

Roman Bazhin

March 20, 2015
Tweet

More Decks by Roman Bazhin

Other Decks in Research

Transcript

  1. • • def load_module(self, fullname): code = self.get_code(fullname) // get_source

    ispkg = self.is_package(fullname) nmod = imp.new_module(fullname) mod = sys.modules.setdefault(fullname, nmod) mod.__file__ = "<%s>" % self.__class__.__name__ mod.__loader__ = self if ispkg: mod.__path__ = [] mod.__package__ = fullname else: mod.__package__ = fullname.rpartition('.')[0] exec(code, mod.__dict__) return mod PyObject *load_module(PyObject *self, PyObject *args){ char *mod_code, *fullname; PyObject *new_mod, *sys_mod_dict, *mod_dict, *res, *o; PyArg_ParseTuple(args, "s", &fullname); new_mod = PyModule_New(fullname); Py_INCREF(new_mod); sys_mod_dict = PyImport_GetModuleDict(); if (sys_mod_dict != NULL){ PyDict_SetItemString(sys_mod_dict, fullname, new_mod); o = PyDict_GetItemString(sys_mod_dict, "__builtin__"); PyModule_AddObject(new_mod, "__builtins__", o); } mod_code = get_code(fullname); mod_dict = PyModule_GetDict(new_mod); res = PyRun_String(mod_code, Py_file_input, mod_dict, mod_dict);
  2. • • #define STOP_CODE 0 #define POP_TOP 1 #define ROT_TWO

    2 #define ROT_THREE 3 #define DUP_TOP 4 #define ROT_FOUR 5 #define NOP 9 … #define BINARY_POWER 0 #define PRINT_ITEM 1 #define INPLACE_OR 2 #define DUP_TOP 3 #define GET_ITER 4 #define BINARY_MULTIPLY 5 #define BINARY_XOR 9 …
  3. • • typedef struct { PyObject_HEAD int co_argcount; int co_nlocals;

    int co_stacksize; int co_flags; … PyObject *co_consts; PyObject *co_names; PyObject *co_varnames; PyObject *co_freevars; PyObject *co_cellvars; PyObject *co_code; … } PyCodeObject;
  4. • • • • B3 F2 0D 0A 0D F1

    5C 50 63 00 00 00 00 00 00 00 00 06 00 00 00 40 00 00 00 73 16 01 00 00 78 43 00 65 00 00 64 00 00 83 01 00 44 5D 30 00 5A 01 B3 F2 0D 0A 0D F1 5C 50 63 70 F9 79 04 8E 20 00 00 11 06 10 0C 0F 0A 0B 08 02 01 03 00 03 00 02 00 00 00 00 00 04 00 05 0A 20 01 00 00 03 00 01
  5. • PyObject *codestring = PyString_FromString("MARKER"); PyObject *tuple = PyTuple_New(0); PyObject

    *string = PyString_FromString(""); PyCodeObject *co_obj = PyCode_New(0, 0, 0, 0, codestring, tuple, tuple, tuple, tuple, tuple, string, string, 0, string); for (char *ptr = (char *) co_obj; ptr < (char *)co_obj + sizeof(PyCodeObject); ptr += 4) if (*((PyObject **)ptr) == co_obj) print("Gotcha"); •