-
Notifications
You must be signed in to change notification settings - Fork 444
/
Copy pathAttachDialog.cpp
321 lines (269 loc) · 7.69 KB
/
AttachDialog.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
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
#include "AttachDialog.h"
#include <Psapi.h>
#include <string>
#ifdef OLLY1
#include "..\ScyllaHideOlly1Plugin\resource.h"
#elif OLLY2
#include "..\ScyllaHideOlly2Plugin\resource.h"
#include <ollydbg2/plugin.h>
#elif __IDP__
#include "..\ScyllaHideIDAProPlugin\resource.h"
#include <idp.hpp>
#elif X64DBG
#include "..\ScyllaHideX64DBGPlugin\resource.h"
#endif
#define BULLSEYE_CENTER_X_OFFSET 15
#define BULLSEYE_CENTER_Y_OFFSET 18
typedef void(__cdecl * t_AttachProcess)(DWORD dwPID);
t_AttachProcess _AttachProcess = 0;
extern HINSTANCE hinst;
#ifdef OLLY1
extern HWND hwmain; // Handle of main OllyDbg window
#elif OLLY2
HWND hwmain = hwollymain;
#elif __IDP__
HWND hwmain = GetForegroundWindow();
#elif X64DBG
extern HWND hwndDlg;
HWND hwmain;
#endif
HBITMAP hBitmapFinderToolFilled = NULL;
HBITMAP hBitmapFinderToolEmpty = NULL;
HCURSOR hCursorPrevious = NULL;
HCURSOR hCursorSearchWindow = NULL;
BOOL bStartSearchWindow = FALSE;
HWND hwndFoundWindow = NULL;
wchar_t title[256];
wchar_t pidTextHex[9];
wchar_t pidTextDec[11];
DWORD pid = NULL;
//toggles the finder image
void SetFinderToolImage(HWND hwnd, BOOL bSet)
{
HBITMAP hBmpToSet = NULL;
if (bSet)
{
hBmpToSet = hBitmapFinderToolFilled;
}
else
{
hBmpToSet = hBitmapFinderToolEmpty;
}
SendDlgItemMessage(hwnd, IDC_ICON_FINDER, STM_SETIMAGE, (WPARAM)IMAGE_BITMAP, (LPARAM)hBmpToSet);
}
//centers cursor in bullseye. adds to the illusion that the bullseye can be dragged out
void MoveCursorPositionToBullsEye(HWND hwnd)
{
HWND hwndToolFinder = NULL;
RECT rect;
POINT screenpoint;
hwndToolFinder = GetDlgItem(hwnd, IDC_ICON_FINDER);
if (hwndToolFinder)
{
GetWindowRect(hwndToolFinder, &rect);
screenpoint.x = rect.left + BULLSEYE_CENTER_X_OFFSET;
screenpoint.y = rect.top + BULLSEYE_CENTER_Y_OFFSET;
SetCursorPos(screenpoint.x, screenpoint.y);
}
}
//does some sanity checks on a possible found window
BOOL CheckWindowValidity(HWND hwnd, HWND hwndToCheck)
{
HWND hwndTemp = NULL;
if (hwndToCheck == NULL)
{
return FALSE;
}
if (IsWindow(hwndToCheck) == FALSE)
{
return FALSE;
}
//same window as previous?
if (hwndToCheck == hwndFoundWindow)
{
return FALSE;
}
//debugger window is not a valid one
if (hwndToCheck == hwmain)
{
return FALSE;
}
// It also must not be the "Search Window" dialog box itself.
if (hwndToCheck == hwnd)
{
return FALSE;
}
// It also must not be one of the dialog box's children...
hwndTemp = GetParent(hwndToCheck);
if ((hwndTemp == hwnd) || (hwndTemp == hwmain))
{
return FALSE;
}
hwndFoundWindow = hwndToCheck;
return TRUE;
}
void DisplayExe(HWND hwnd, DWORD dwPid)
{
WCHAR filepath[MAX_PATH] = { 0 };
HANDLE hProc = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, dwPid);
if (hProc)
{
GetModuleFileNameExW(hProc, NULL, filepath, _countof(filepath));
CloseHandle(hProc);
if (wcslen(filepath) > 0)
{
SetDlgItemTextW(hwnd, IDC_EXEPATH, wcsrchr(filepath, L'\\') + 1);
}
else
{
SetDlgItemTextW(hwnd, IDC_EXEPATH, L"UNKNOWN");
}
}
else
{
SetDlgItemTextW(hwnd, IDC_EXEPATH, L"UNKNOWN");
}
}
//attach dialog proc
INT_PTR CALLBACK AttachProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
wchar_t buf[20] = { 0 };
switch (message)
{
case WM_INITDIALOG:
{
#ifdef X64DBG
hwmain = hwndDlg;
#endif
hBitmapFinderToolFilled = LoadBitmap(hinst, MAKEINTRESOURCE(IDB_FINDERFILLED));
hBitmapFinderToolEmpty = LoadBitmap(hinst, MAKEINTRESOURCE(IDB_FINDEREMPTY));
hCursorSearchWindow = LoadCursor(hinst, MAKEINTRESOURCE(IDC_CURSOR_SEARCH_WINDOW));
break;
}
case WM_CLOSE:
{
EndDialog(hWnd, NULL);
}
break;
case WM_COMMAND:
{
switch (LOWORD(wParam)) {
case IDOK: { //attach
if (pid != NULL) {
EndDialog(hWnd, NULL);
if (_AttachProcess != 0)
{
_AttachProcess(pid);
}
else
{
MessageBoxW(0, L"Developer!!! You forgot something _AttachProcess!!!!!", L"ERROR", 0);
}
}
break;
}
case IDCANCEL: {
EndDialog(hWnd, NULL);
break;
}
case IDC_PIDHEX: {
if (0 < GetDlgItemTextW(hWnd, IDC_PIDHEX, buf, _countof(buf))) {
if (wcscmp(buf, pidTextHex) != 0) {
wcscpy(pidTextHex, buf);
swscanf(pidTextHex, L"%X", &pid);
wsprintfW(pidTextDec, L"%d", pid);
SetDlgItemTextW(hWnd, IDC_PIDDEC, pidTextDec);
DisplayExe(hWnd, pid);
SetDlgItemTextW(hWnd, IDC_TITLE, L"");
}
}
break;
}
case IDC_PIDDEC:
{
if (0 < GetDlgItemTextW(hWnd, IDC_PIDDEC, buf, _countof(buf))) {
if (wcscmp(buf, pidTextDec) != 0) {
wcscpy(pidTextDec, buf);
swscanf(pidTextDec, L"%d", &pid);
wsprintfW(pidTextHex, L"%X", pid);
SetDlgItemTextW(hWnd, IDC_PIDHEX, pidTextHex);
DisplayExe(hWnd, pid);
SetDlgItemTextW(hWnd, IDC_TITLE, L"");
}
}
break;
}
case IDC_ICON_FINDER: {
bStartSearchWindow = TRUE;
SetFinderToolImage(hWnd, FALSE);
MoveCursorPositionToBullsEye(hWnd);
// Set the screen cursor to the BullsEye cursor.
if (hCursorSearchWindow)
{
hCursorPrevious = SetCursor(hCursorSearchWindow);
}
else
{
hCursorPrevious = NULL;
}
//redirect all mouse events to this AttachProc
SetCapture(hWnd);
ShowWindow(hwmain, SW_HIDE);
break;
}
}
break;
}
case WM_MOUSEMOVE:
{
if (bStartSearchWindow)
{
POINT screenpoint;
HWND hwndCurrentWindow = NULL;
GetCursorPos(&screenpoint);
hwndCurrentWindow = WindowFromPoint(screenpoint);
if (CheckWindowValidity(hWnd, hwndCurrentWindow))
{
//get some info about the window
GetWindowThreadProcessId(hwndFoundWindow, &pid);
DisplayExe(hWnd, pid);
if (GetWindowTextW(hwndCurrentWindow, title, _countof(title)) > 0)
{
SetDlgItemTextW(hWnd, IDC_TITLE, title);
}
else
{
SetDlgItemTextW(hWnd, IDC_TITLE, L"");
}
wsprintfW(pidTextHex, L"%X", pid);
wsprintfW(pidTextDec, L"%d", pid);
SetDlgItemTextW(hWnd, IDC_PIDHEX, pidTextHex);
SetDlgItemTextW(hWnd, IDC_PIDDEC, pidTextDec);
}
}
break;
}
case WM_LBUTTONUP:
{
if (bStartSearchWindow)
{
// restore cursor
if (hCursorPrevious)
{
SetCursor(hCursorPrevious);
}
SetFinderToolImage(hWnd, TRUE);
// release the mouse capture.
ReleaseCapture();
ShowWindow(hwmain, SW_SHOWNORMAL);
bStartSearchWindow = FALSE;
}
break;
}
default:
{
return FALSE;
}
}
return 0;
}