[CODE] Calling the Python disassembler from C menu

User Tag List

Results 1 to 2 of 2
  1. #1
    GliderPro's Avatar Member
    Reputation
    -1
    Join Date
    Mar 2009
    Posts
    93
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    [CODE] Calling the Python disassembler from C

    Here is a snippet of the code I use to disassemble Python code objects.

    Code:
    PyObject *disassemble(PyObject *obj)
    {
      PyObject *dismod = NULL;
      PyObject *disfunc = NULL;
      PyObject *disresult = NULL;
    
      dismod = PyImport_ImportModule("dis");
      //printf("dismod: 0x%08X\n",dismod);
      if( dismod != NULL )
      {
        Py_INCREF( dismod );
        disfunc = PyObject_GetAttrString(dismod,"dis");
        //printf("disfunc: 0x%08X\n",disfunc);
        if( disfunc != NULL )
        {
          Py_INCREF( disfunc );
          disresult = PyEval_CallFunction(disfunc,"(O)",obj);
        }
      }
    
      Py_XDECREF( disresult );
      Py_XDECREF( disfunc );
      Py_XDECREF( dismod );
    
      return disresult;
    }

    [CODE] Calling the Python disassembler from C
  2. #2
    GliderPro's Avatar Member
    Reputation
    -1
    Join Date
    Mar 2009
    Posts
    93
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This new version will replicate the directory structure of the original file and redirect the disassembled output to that file.

    Code:
    PyObject *disassemble(PyObject *obj)
    {
      static char buf[MAX_PATH], cmd[MAX_PATH<<1];
    
      PyObject *dismod = NULL;
      PyObject *disfunc = NULL;
      PyObject *disresult = NULL;
      
      PyCodeObject *co = (PyCodeObject *)obj;
      char *filename = PyString_AsString(co->co_filename);
    
      sprintf_s(buf,sizeof(buf),
        "c:\\%s.dis",filename+3);
      
      // make the slashes uniform
      char *p = NULL;
      p = strchr(buf,'/');
      while(p != NULL )
      {
        *p = '\\';
        p = strchr(p,'/');
      }
    
      // make sure the directory exists (by trying to creating it)
      p = strchr(buf,'\\');
      while( p != NULL )
      {
        *p = 0;
        CreateDirectoryA(buf,NULL);
        *p = '\\';
        p = strchr(p+1,'\\');
      }
    
      // double up the slashes in the string
      p = strchr(buf,'\\');
      while(p != NULL )
      {
        memmove(p+1,p,sizeof(buf)-(p-buf-1));
        p = strchr(p+2,'\\');
      }
    
      sprintf_s(cmd,sizeof(cmd),
      "sys.stdout = open(\"%s\",'a')\n",buf);
    
      PyRun_SimpleString("import sys\n");
      PyRun_SimpleString(cmd);
    
      dismod = PyImport_ImportModule("dis");
      //printf("dismod: 0x%08X\n",dismod);
      if( dismod != NULL )
      {
        Py_INCREF( dismod );
        disfunc = PyObject_GetAttrString(dismod,"dis");
        //printf("disfunc: 0x%08X\n",disfunc);
        if( disfunc != NULL )
        {
          Py_INCREF( disfunc );
          disresult = PyEval_CallFunction(disfunc,"(O)",obj);
        }
      }
    
      PyRun_SimpleString("sys.stdout = sys.__stdout__\n");
    
      Py_XDECREF( disresult );
      Py_XDECREF( disfunc );
      Py_XDECREF( dismod );
    
      return disresult;
    }

Similar Threads

  1. [Guide] Preventing the other team from calling a Timeout
    By Kenneth in forum Counter-Strike Global Offensive
    Replies: 4
    Last Post: 03-06-2017, 05:24 AM
  2. [CODE] Dumping Python Modules (from an injected DLL)
    By GliderPro in forum Programming
    Replies: 0
    Last Post: 04-08-2009, 02:20 PM
  3. Who Stole the Horde Flag from WSG!?
    By matswurld in forum WoW PvP & Battlegrounds
    Replies: 24
    Last Post: 12-10-2007, 02:35 PM
  4. Call the future
    By Elephant in forum Community Chat
    Replies: 5
    Last Post: 10-11-2007, 09:33 PM
  5. WOW the gold standard (FROM RED GUIDES)
    By Elites360 in forum World of Warcraft Guides
    Replies: 4
    Last Post: 10-24-2006, 12:50 PM
All times are GMT -5. The time now is 11:03 AM. Powered by vBulletin® Version 4.2.3
Copyright © 2024 vBulletin Solutions, Inc. All rights reserved. User Alert System provided by Advanced User Tagging (Pro) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
Digital Point modules: Sphinx-based search