-
Notifications
You must be signed in to change notification settings - Fork 82
/
py.cpp
101 lines (90 loc) · 2.95 KB
/
py.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include "py.h"
#include <windows.h>
#include <stdio.h>
#include <psapi.h>
#include <python.h>
extern "C" __declspec(dllexport) void CBMENUENTRY(CBTYPE cbType, PLUG_CB_MENUENTRY* info)
{
switch(info->hEntry)
{
case MENU_OPEN:
DbgCmdExec("OpenScript");
break;
case MENU_ABOUT:
MessageBoxA(hwndDlg, "Made By RealGame(Tomer Zait)", "x64dbg-python Plugin", MB_ICONINFORMATION);
break;
}
}
static bool cbPythonCommand(int argc, char* argv[])
{
if(argc < 2)
{
_plugin_logputs("[PYTHON] Command Example: Python \"print('Hello World')\".");
return false;
}
PyRun_SimpleString(argv[1]);
return true;
}
static bool OpenFileDialog(char Buffer[MAX_PATH])
{
OPENFILENAMEA sOpenFileName = {0};
const char szFilterString[] = "Python files\0*.py\0\0";
const char szDialogTitle[] = "Select script file...";
sOpenFileName.lStructSize = sizeof(sOpenFileName);
sOpenFileName.lpstrFilter = szFilterString;
sOpenFileName.nFilterIndex = 1;
sOpenFileName.lpstrFile = Buffer;
sOpenFileName.nMaxFile = MAX_PATH;
sOpenFileName.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
sOpenFileName.lpstrTitle = szDialogTitle;
sOpenFileName.hwndOwner = GuiGetWindowHandle();
return (FALSE != GetOpenFileNameA(&sOpenFileName));
}
//OpenScript [EntryPointVA]
static bool cbOpenScriptCommand(int argc, char* argv[])
{
char szFileName[MAX_PATH] = "";
if(!OpenFileDialog(szFileName))
return true;
PyObject* PyFileObject = PyFile_FromString(szFileName, "r");
if(PyFileObject == NULL)
{
_plugin_logputs("[PYTHON] Could not open file....");
return false;
}
if(PyRun_SimpleFile(PyFile_AsFile(PyFileObject), szFileName) != EXIT_SUCCESS)
{
_plugin_logputs("[PYTHON] Could not run script....");
return false;
}
_plugin_logputs("[PYTHON] Execution is done!");
return true;
}
void pyInit(PLUG_INITSTRUCT* initStruct)
{
_plugin_logprintf("[PYTHON] pluginHandle: %d\n", pluginHandle);
if(!_plugin_registercommand(pluginHandle, "Python", cbPythonCommand, false))
_plugin_logputs("[PYTHON] error registering the \"Python\" command!");
if(!_plugin_registercommand(pluginHandle, "OpenScript", cbOpenScriptCommand, false))
_plugin_logputs("[PYTHON] error registering the \"OpenScript\" command!");
// Initialize the python environment
Py_Initialize();
PyEval_InitThreads();
PyRun_SimpleString("from x64dbg_python import *\n");
}
void pyStop()
{
_plugin_unregistercommand(pluginHandle, "Python");
_plugin_unregistercommand(pluginHandle, "OpenScript");
_plugin_menuclear(hMenu);
_plugin_menuclear(hMenuDisasm);
_plugin_menuclear(hMenuDump);
_plugin_menuclear(hMenuStack);
// Properly ends the python environment
Py_Finalize();
}
void pySetup()
{
_plugin_menuaddentry(hMenu, MENU_OPEN, "&OpenScript...\tALT+F7");
_plugin_menuaddentry(hMenu, MENU_ABOUT, "&About");
}