-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPythonInjector.cpp
More file actions
executable file
·153 lines (137 loc) · 3.82 KB
/
PythonInjector.cpp
File metadata and controls
executable file
·153 lines (137 loc) · 3.82 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include <string>
#include <stdio.h>
#include <algorithm>
#include "stdafx.h"
#include "windows.h"
using namespace std;
HINSTANCE handle;
HWND submitButton;
HWND editBox;
LRESULT CALLBACK DLLWindowProc(
HWND,
UINT,
WPARAM,
LPARAM
);
int callback(void* code) {
return PyRun_SimpleStringFlags((char*)code, NULL);
}
void removeCarriage(char* str) {
int length = strlen(str);
for (int i = 0; i < length; i++) {
if (str[i] == '\r') {
for (int j = i; j < length; j++) {
str[j] = str[j + 1];
if (str[j] == '\0') {
break;
}
}
i--;
}
}
}
BOOL RegisterDLLWindowClass(wchar_t szClassName[]) {
WNDCLASSEX wclass;
wclass.hInstance = handle;
wclass.lpszClassName = (LPCSTR)szClassName;
wclass.lpfnWndProc = DLLWindowProc;
wclass.style = CS_DBLCLKS;
wclass.cbSize = sizeof(WNDCLASSEX);
wclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wclass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
wclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wclass.lpszMenuName = NULL;
wclass.cbClsExtra = 0;
wclass.cbWndExtra = 0;
wclass.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
if (!RegisterClassEx(&wclass)) {
return 0;
}
}
DWORD WINAPI ThreadProc() {
MSG messages;
RegisterDLLWindowClass(reinterpret_cast<wchar_t *>("InjectedDLLWindowClass"));
HWND hwnd = CreateWindowEx(
0, // Ex Style
(LPCSTR)"InjectedDLLWindowClass", // Class Name
(LPCSTR)"Python Injector", // Window Title Name
WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX, // DW Style
CW_USEDEFAULT, // Position X CW_USEDEFAULT
CW_USEDEFAULT, // Position Y CW_USEDEFAULT
506, // Window Width
528, // Window Height
NULL, // Window Parent
NULL, // Window Menu
handle, // HINSTANCE
NULL
);
ShowWindow(hwnd, SW_SHOWNORMAL);
while (GetMessage(&messages, NULL, 0, 0)) {
TranslateMessage(&messages);
DispatchMessage(&messages);
}
return 1;
}
WNDPROC wndButtonProc;
LRESULT CALLBACK ButtonProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {
switch (message) {
case WM_LBUTTONUP:
int length = GetWindowTextLength(editBox) + 1;
char* buffer = calloc(length, sizeof(char));
SendMessage(editBox, WM_GETTEXT, (WPARAM)length, (LPARAM)buffer);
removeCarriage(buffer);
Py_AddPendingCall(callback, (void*)buffer);
MessageBox(NULL, (LPCSTR)buffer, (LPCSTR)"", MB_OK);
break;
}
return CallWindowProc(wndButtonProc, hwnd, message, wParam, lParam);
}
LRESULT CALLBACK DLLWindowProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {
int result;
switch (message) {
case WM_COMMAND:
break;
case WM_CREATE:
submitButton = CreateWindowEx(
NULL, "Button", "Submit Code",
WS_BORDER | WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
0, 250, 500, 250,
hwnd, NULL, handle, ButtonProc
);
wndButtonProc = (WNDPROC)SetWindowLong(submitButton, GWL_WNDPROC, (LONG)ButtonProc);
editBox = CreateWindowEx(
NULL, "Edit", "",
WS_CHILD | WS_VISIBLE | WS_HSCROLL |
WS_VSCROLL | ES_LEFT | ES_MULTILINE |
ES_AUTOHSCROLL | ES_AUTOVSCROLL,
0, 0, 500, 250,
hwnd, NULL, handle, NULL
);
SendMessage(editBox, EM_LIMITTEXT, WPARAM(2147483646), 0);
break;
case WM_SETFOCUS:
SetFocus(editBox);
break;
case WM_CLOSE:
result = MessageBox(NULL, (LPCSTR)("Do you really want to exit?\r\n"
"(You can not re-open this window unless you restart the process)"),
(LPCSTR)"Exit Python Injector", MB_ICONQUESTION | MB_YESNO);
if (result == IDYES) {
DestroyWindow(hwnd);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, message, wParam, lParam);
}
return 0;
}
BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) {
if(ul_reason_for_call==DLL_PROCESS_ATTACH) {
handle = hModule;
CreateThread(0, NULL, (LPTHREAD_START_ROUTINE)ThreadProc, NULL, NULL, NULL);
}
return TRUE;
}