Skip to content

Instantly share code, notes, and snippets.

@kexplo
Forked from xor0x35/exe2py.c
Created February 21, 2012 19:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kexplo/1878366 to your computer and use it in GitHub Desktop.
Save kexplo/1878366 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <windows.h>
struct scriptinfo {
int tag;
int optimize;
int unbuffered;
int data_bytes;
char zippath[0];
};
int main(int argc,char *argv[]) {
HMODULE hTarget;
HRSRC hRes;
HGLOBAL hResLoaded;
char *pScript;
struct scriptinfo *p_script_info;
int numScriptBytes;
FILE *file;
// print usage
if(argc < 3)
{
printf("Usage: exe2py target.exe out.marshal\r\n");
printf("you can marshal.load(open(\"out.marshal\")) to load PyCodeObject.\r\n");
return 0;
}
// open target
hTarget = LoadLibrary(argv[1]);
// find and load resource
hRes = FindResource(hTarget, MAKEINTRESOURCE(1), "PYTHONSCRIPT");
hResLoaded = LoadResource(hTarget, hRes);
p_script_info = (struct scriptinfo *)pScript = LockResource(hResLoaded);
// validate script resource
numScriptBytes = p_script_info->data_bytes;
pScript += sizeof(struct scriptinfo);
// let pScript point to the start of the python script resource
pScript += strlen(p_script_info->zippath) + 1;
// out
file = fopen(argv[2],"wb");
fwrite(pScript,numScriptBytes,1,file);
fclose(file);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment