-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJackysApp.cpp
More file actions
444 lines (406 loc) · 13.4 KB
/
Copy pathJackysApp.cpp
File metadata and controls
444 lines (406 loc) · 13.4 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
#include "framework.h"
#include "JackysApp.h"
// Global Variables:
HINSTANCE hInst; // Current Instance
HWND hTestButton, hTextField, hProcesses;
TCHAR szTitle[MAX_LOADSTRING]; // Title Text
TCHAR szWindowClass[MAX_LOADSTRING]; // ClassName of the Main window
TCHAR g_szFileName[MAX_LOADSTRING];
std::map<DWORD,Process> processList = {};
int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPWSTR lpCmdLine,
_In_ int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
// TODO: Add code (Template)
#ifdef DEBUG
AllocConsole();
freopen_s((FILE**)stdout, "CONOUT$", "w", stdout);
#endif
// Load Ressource Strings by Template
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_JACKYSAPP, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_JACKYSAPP));
MSG msg;
// Message Loop
while (GetMessage(&msg, nullptr, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return (int) msg.wParam;
}
//
// function: MyRegisterClass()
//
// Purpose: Register Window-Class.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_JACKYSAPP));
wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
wcex.hbrBackground = CreateSolidBrush(RGB(160, 160, 160));
wcex.lpszMenuName = MAKEINTRESOURCE(IDC_JACKYSAPP);
//wcex.lpszMenuName = NULL;
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
return RegisterClassEx(&wcex);
}
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
hInst = hInstance;
HWND hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW & ~WS_MAXIMIZEBOX & ~WS_MINIMIZEBOX,
CW_USEDEFAULT, CW_USEDEFAULT, DEFAULT_SIZE_X, DEFAULT_SIZE_Y, nullptr, nullptr, hInstance, nullptr);
if (!hWnd)
{
return FALSE;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_CREATE:
CreateMainLayout(hWnd);
DragAcceptFiles(hWnd, TRUE);
ListProcesses(processList);
UpdateProcessBox(processList);
break;
case WM_GETMINMAXINFO:
{
LPMINMAXINFO lpMMI = (LPMINMAXINFO)lParam;
lpMMI->ptMinTrackSize.x = DEFAULT_SIZE_X;
lpMMI->ptMinTrackSize.y = DEFAULT_SIZE_Y;
lpMMI->ptMaxTrackSize.x = DEFAULT_SIZE_X;
lpMMI->ptMaxTrackSize.y = DEFAULT_SIZE_Y;
break;
}
case WM_COMMAND:
{
int wmId = LOWORD(wParam);
switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
case IDM_BUTTON_SEARCHDLL:
//print(_T("Button SearchDLL pressed\n"));
OpenFileSelector(hWnd);
SetWindowText(hTextField, g_szFileName);
break;
case IDM_COMBOBOX:
//print(_T("COMBOBOX Command: %x"), HIWORD(wParam));
if (HIWORD(wParam) == CBN_DROPDOWN) {
ListProcesses(processList);
UpdateProcessBox(processList);
}
//print(_T("SelectedIndex: %i"), (DWORD)SendMessage(hProcesses, CB_GETCURSEL, 0, 0));
break;
case IDM_INJECT_BUTTON:
InjectButtonHandler(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
}
break;
case WM_DROPFILES:
print(_T("File Dropped :)\n"));
HDROP droppedFile;
droppedFile = reinterpret_cast<HDROP>(wParam);
if (DragQueryFile(droppedFile, 0xFFFFFFFF, NULL, 0) != 1) {
MessageBox(hWnd, _T("More than 1 File per Drag and Drop is not allowed!"),
_T("Warning"), MB_ICONWARNING);
return 1;
}
DragQueryFile(droppedFile, 0, g_szFileName, MAX_LOADSTRING);
if (string(g_szFileName).find(_T(".dll")) == -1) {
MessageBox(hWnd, _T("Only DLL-Files are allowed!"),
_T("Warning"), MB_ICONWARNING);
return 1;
}
SetWindowText(hTextField, g_szFileName);
print(_T("File: %s\n"), g_szFileName);
break;
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hWnd, &ps);
// TODO: Pain things
EndPaint(hWnd, &ps);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
//Aboutfield Callback
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
UNREFERENCED_PARAMETER(lParam);
switch (message)
{
case WM_INITDIALOG:
return (INT_PTR)TRUE;
case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return (INT_PTR)TRUE;
}
break;
}
return (INT_PTR)FALSE;
}
void CreateMainLayout(HWND hWnd) {
hTestButton = CreateWindow(
_T("BUTTON"), // Predefined class;
_T("SearchFile"), // Button text
WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, // Styles
15, // x position
60 + 20 + DEFAULT_MARGIN, // y position
100, // Button width
20, // Button height
hWnd, // Parent window
(HMENU)IDM_BUTTON_SEARCHDLL, // ID.
(HINSTANCE)GetWindowLongPtr(hWnd, GWLP_HINSTANCE),
NULL); // Pointer not needed.
hTextField = CreateWindow(
_T("Edit"),
NULL,
WS_VISIBLE | WS_CHILD | WS_BORDER | ES_LEFT | ES_AUTOHSCROLL,
15,
15 + 35 + DEFAULT_MARGIN,
DEFAULT_SIZE_X - 50,
20,
hWnd,
NULL,
NULL,
NULL);
CreateWindow(
_T("Static"),
_T("Select DLL-File or drop a DLL File into this Application: "),
WS_VISIBLE | WS_CHILD,
15,
15,
DEFAULT_SIZE_X - 50,
35,
hWnd,
NULL,
NULL,
NULL);
CreateWindow(
_T("Static"),
_T("Select Process or write ProcessName: "),
WS_VISIBLE | WS_CHILD,
15,
90 + 20 + DEFAULT_MARGIN * 3, // 140 +35
DEFAULT_SIZE_X - 50,
35,
hWnd,
NULL,
NULL,
NULL);
hProcesses = CreateWindow(
_T("ComboBox"),
_T(""),
WS_VISIBLE | WS_CHILD | CBS_DROPDOWN| CBS_HASSTRINGS | CBS_AUTOHSCROLL | WS_VSCROLL,
15,
140 + 35 + DEFAULT_MARGIN,
DEFAULT_SIZE_X - 50,
200,
hWnd,
(HMENU)IDM_COMBOBOX,
(HINSTANCE)GetWindowLongPtr(hWnd, GWLP_HINSTANCE),
NULL);
CreateWindow(
_T("Button"),
_T("Start Injecting"),
WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,
15,
210 + DEFAULT_MARGIN,
100,
20,
hWnd,
(HMENU)IDM_INJECT_BUTTON,
NULL,
NULL);
}
void OpenFileSelector(HWND hWnd) {
OPENFILENAME ofn;
TCHAR filePath[MAX_LOADSTRING];
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = hWnd;
ofn.lpstrFilter = _T("Dynamic Link Libraries: *.dll\0*.dll\0All Files: *.*\0*.*\0");
ofn.lpstrFile = filePath;
ofn.lpstrFile[0] = '\0';
ofn.nMaxFile = sizeof(filePath);
ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
ofn.lpstrDefExt = _T("dll");
GetOpenFileName(&ofn);
if (string(filePath) == _T("")) {
return;
}
if (string(filePath).find(_T(".dll")) == -1) {
MessageBox(hWnd, _T("Only DLL-Files are allowed here!"),
_T("Warning"), MB_ICONWARNING);
return;
}
tstrcpy_s(g_szFileName,filePath);
SetWindowText(hTextField, g_szFileName);
}
std::map < DWORD, Process > ListProcesses(std::map<DWORD,Process>& pList) {
pList.clear();
DWORD procID = NULL;
PROCESSENTRY32 entry;
entry.dwSize = sizeof(PROCESSENTRY32);
HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (snapshot == INVALID_HANDLE_VALUE) {
print(_T("Could not get a Snapshot\n"));
return pList;
}
if (Process32First(snapshot, &entry)) { // Init Process here
while (Process32Next(snapshot, &entry)) {
pList.insert({ entry.th32ProcessID, { entry.th32ProcessID ,entry.szExeFile } });
}
}
CloseHandle(snapshot);
return pList;
}
void UpdateProcessBox(std::map<DWORD, Process>& pList) {
SendMessage(hProcesses, CB_RESETCONTENT, 0, 0);
for (auto process = pList.begin(); process != pList.end(); process++) {
string s = process->second.pName.c_str();
s.append(_T(" ("));
s.append(std::to_string(process->first));
s.append(_T(")"));
//print(_T("Name is: %s\n"), s.c_str());
SendMessage(hProcesses, CB_ADDSTRING, NULL, (LPARAM)s.c_str());
}
}
DWORD GetProcIDByDropBoxText(string text) {
DWORD retVal = NULL;
size_t init = text.find(_T("(")) + 1;
size_t length = text.find(_T(")")) - init;
print(_T("Init: %i\tLength:%i\n"), init, length);
try {
string sub = text.substr(init, length);
retVal = stoul(sub);
print(_T("Substring PID = %i\n"), retVal);
}
catch (std::exception e) {
retVal = GetProcessIDByName(text);
}
return retVal;
}
DWORD GetProcessIDByName(string processName) {
DWORD procID = NULL;
PROCESSENTRY32 entry;
entry.dwSize = sizeof(PROCESSENTRY32);
HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (snapshot == INVALID_HANDLE_VALUE) {
print(_T("Could not get a Snapshot\n"));
return procID;
}
if (Process32First(snapshot, &entry)) {
do {
if (!std::string(entry.szExeFile).compare(processName)) {
print(_T("Found the Process with ProcessID: %u\n"), entry.th32ProcessID);
print(_T("Name is: %s\n"), entry.szExeFile);
procID = entry.th32ProcessID;
break;
}
} while (Process32Next(snapshot, &entry));
}
CloseHandle(snapshot);
return procID;
}
void InjectButtonHandler(HWND hWnd) {
TCHAR bufferPath[MAX_LOADSTRING];
GetWindowText(hTextField, bufferPath, MAX_LOADSTRING);
print(_T("Selected DLL-Path: %s\n"), bufferPath);
if (!PathFileExists(bufferPath)) {
MessageBox(hWnd, _T("DLL File does noch exist!\n"), _T("Error"), MB_ICONERROR);
return;
}
TCHAR str[MAX_LOADSTRING];
GetWindowText(hProcesses, str, MAX_LOADSTRING);
print(_T("Selected Process: %s\n"), str);
DWORD pid = 0;
pid = GetProcIDByDropBoxText(str);
if (pid == NULL) {
MessageBox(hWnd, _T("Process PID could not be found with the given Process Name or Selection"),
_T("Error: PID Not found"), MB_ICONERROR);
return;
}
//InjectDLLWithPath(pid, buffer);
if (InjectDLLWithPath(pid, bufferPath) == FALSE) {
MessageBox(hWnd, _T("Something went wrong, when trying to inject the dll!"),
_T("Error"), MB_ICONERROR);
}
else {
MessageBox(hWnd, _T("DLL injected sucessfully!"),
_T("Info"), MB_ICONINFORMATION);
}
}
BOOL InjectDLLWithPath(DWORD procID, const TCHAR* dllPath) {
HANDLE processHandle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, procID);
if (processHandle == INVALID_HANDLE_VALUE) {
print("Could Not OpenProcess with ID: %u\n", procID);
return FALSE;
}
print("Handle Opened Successfully\n");
LPVOID memLoc = VirtualAllocEx(processHandle, 0, MAX_PATH, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
if (memLoc == nullptr) {
print("Could Not Allocate Mem\n");
CloseHandle(processHandle);
return FALSE;
}
BOOL WPM = WriteProcessMemory(processHandle, memLoc, dllPath, strlen(dllPath) + 1, 0);
if (!WPM) {
print("Could Not Write in the ProcessMemory\n");
CloseHandle(processHandle);
return FALSE;
}
print("Allocated PathName Success\n");
HANDLE remoteThread = CreateRemoteThread(processHandle, 0, 0, (LPTHREAD_START_ROUTINE)LoadLibraryA, memLoc, 0, 0);
if (!remoteThread) {
print("Error in Creating RemoteThread (LoadLibraryA)\n");
VirtualFree(memLoc, NULL, MEM_RELEASE);
CloseHandle(processHandle);
return FALSE;
}
print("DLL Injected\n");
CloseHandle(remoteThread);
VirtualFree(memLoc, NULL, MEM_RELEASE);
CloseHandle(processHandle);
return TRUE;
}