-
Notifications
You must be signed in to change notification settings - Fork 5
/
FindWindowAPI.dpr
251 lines (206 loc) · 7.61 KB
/
FindWindowAPI.dpr
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
program FindWindowAPI;
{$APPTYPE CONSOLE}
uses
System.SysUtils, WinAPI.Windows, Generics.Collections, psAPI;
{+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
TFindWindowSignature Class
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++}
type
TFindWindowSignature = class
private
FDescription : String;
FClassName : String;
FWindowName : String;
public
{@C}
constructor Create(ADescription, AClassName, AWindowName : String);
{@G}
property Description : String read FDescription;
property ClassName : String read FClassName;
property WindowName : String read FWindowName;
end;
{-------------------------------------------------------------------------------
___constructor
-------------------------------------------------------------------------------}
constructor TFindWindowSignature.Create(ADescription, AClassName, AWindowName : String);
begin
FDescription := ADescription;
FClassName := AClassName;
FWindowName := AWindowName;
end;
{+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Main
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++}
var LFindWindowSignatures : TObjectList<TFindWindowSignature>;
LEnumWindowsSignatures : TDictionary<String, String>;
{-------------------------------------------------------------------------------
When a Window handle is found it will output to console several information
about spotted process.
-------------------------------------------------------------------------------}
procedure Found(ADescription : String; AHandle : THandle);
const CRLF = #13#10;
var AStdout_TXT : String;
AProcessId : Cardinal;
AProcessHandle : THandle;
ARet : DWORD;
pImagePath : PWideChar;
begin
try
AStdout_TXT := AStdout_TXT + StringOfChar('-', 60) + CRLF;
AStdout_TXT := AStdout_TXT + ADescription + CRLF;
AStdout_TXT := AStdout_TXT + StringOfChar('-', 60) + CRLF;
AStdout_TXT := AStdout_TXT + Format('Handle: %d%s', [AHandle, CRLF]);
GetWindowThreadProcessId(AHandle, @AProcessId);
if (AProcessId > 0) then begin
AProcessHandle := OpenProcess(
(PROCESS_QUERY_INFORMATION or PROCESS_VM_READ),
False,
AProcessId
);
if (AProcessHandle > 0) then begin
AStdout_TXT := AStdout_TXT + Format('Process Id: %d%s', [AProcessId, CRLF]);
pImagePath := nil;
try
GetMem(pImagePath, (MAX_PATH * 2));
ARet := GetModuleFileNameExW(AProcessHandle, 0, pImagePath, (MAX_PATH * 2));
if (ARet > 0) then begin
AStdout_TXT := AStdout_TXT + Format('Process Name: %s%s', [ExtractFileName(String(pImagePath)), CRLF]);
AStdout_TXT := AStdout_TXT + Format('Image Path: %s%s', [ExtractFilePath(String(pImagePath)), CRLF]);
end;
finally
if Assigned(pImagePath) and (ARet > 0) then
FreeMem(pImagePath, ARet);
end;
end;
end;
AStdout_TXT := AStdout_TXT + StringOfChar('-', 60) + CRLF + CRLF;
///
finally
WriteLn(AStdout_TXT);
end;
end;
{-------------------------------------------------------------------------------
Find Debuggers by Window Name or Class Name using FindWindow API
-------------------------------------------------------------------------------}
function Locate_FindWindow() : Boolean;
var AFindWindowSignature : TFindWindowSignature;
i : Integer;
pClassName : Pointer;
pWindowName : Pointer;
AHandle : THandle;
begin
result := False;
///
for i := 0 to LFindWindowSignatures.Count -1 do begin
AFindWindowSignature := LFindWindowSignatures.Items[i];
if NOT Assigned(AFindWindowSignature) then
continue;
///
pClassName := nil;
pWindowName := nil;
if NOT AFindWindowSignature.ClassName.isEmpty then
pClassName := PWideChar(AFindWindowSignature.ClassName);
if NOT AFindWindowSignature.WIndowName.isEmpty then
pWindowName := PWideChar(AFindWindowSignature.WindowName);
AHandle := FindWindowW(pClassName, pWindowName);
if (AHandle > 0) then begin
Found(AFindWindowSignature.Description, AHandle);
///
result := True;
end;
end;
end;
{-------------------------------------------------------------------------------
Find Debuggers by Window Name (via Window Name Pattern) using EnumWindows API
-------------------------------------------------------------------------------}
function EnumWindowProc(AHandle : THandle; AParam : LPARAM) : BOOL; stdcall;
var AMaxCount : Integer;
AWindowName : String;
AOldLen : Cardinal;
APattern : String;
AKey : String;
begin
result := True;
///
if (AHandle = 0) then
Exit();
///
AMaxCount := GetWindowTextLength(AHandle) + 1;
if (AMaxCount = 0) then
Exit();
SetLength(AWindowName, AMaxCount); // Other technique instead of using GetMem / FreeMem a new Pointer.
try
if (GetWindowTextW(AHandle, PWideChar(AWindowName), AMaxCount) = 0) then
Exit();
///
AOldLen := Length(AWindowName);
for AKey {Description} in LEnumWindowsSignatures.keys do begin
if NOT LEnumWindowsSignatures.TryGetValue(AKey, APattern) then
continue;
AWindowName := StringReplace(AWindowName, APattern, '', []);
if (Length(AWindowName) <> AOldLen) then begin
Found(AKey, AHandle);
break;
end;
end;
finally
SetLength(AWindowName, 0);
end;
end;
function Locate_EnumWindows() : Boolean;
begin
EnumWindows(@EnumWindowProc, 0);
end;
{-------------------------------------------------------------------------------
Append FindWindow Technique Signature
-------------------------------------------------------------------------------}
procedure AppendFindWindowSignature(ADescription, AClassName, AWindowName : String);
var AFindWindowSignature : TFindWindowSignature;
begin
if NOT Assigned(LFindWindowSignatures) then
Exit();
///
AFindWindowSignature := TFindWindowSignature.Create(ADescription, AClassName, AWindowName);
LFindWindowSignatures.Add(AFindWindowSignature);
end;
{-------------------------------------------------------------------------------
___entry
-------------------------------------------------------------------------------}
begin
try
LFindWindowSignatures := TObjectList<TFindWindowSignature>.Create();
LEnumWindowsSignatures := TDictionary<String, String>.Create();
try
{
Configure debuggers signatures here for FindWindow API technique.
}
AppendFindWindowSignature('OllyDbg', 'OLLYDBG', '');
AppendFindWindowSignature('x64dbg (x64)', '', 'x64dbg');
AppendFindWindowSignature('x32dbg (x32)', '', 'x32dbg');
// ...
// AppendFindWindowSignature('...', '...', '...');
// ...
{
Configure debuggeers signatures here for EnumWindows API technique.
}
LEnumWindowsSignatures.Add('Immunity Debugger', 'Immunity Debugger');
// ...
// AEnumWindowsSignatures.Add('...', '...');
// ...
{
Fire !!!
}
Locate_FindWindow();
Locate_EnumWindows();
readln;
finally
if Assigned(LFindWindowSignatures) then
FreeAndNil(LFindWindowSignatures);
if Assigned(LEnumWindowsSignatures) then
FreeAndNil(LEnumWindowsSignatures);
end;
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
end.