utility.cpp
10.9 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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
/* Copyright (C) 2010-2012 kaosu (qiupf2000@gmail.com)
* This file is part of the Interactive Text Hooker.
* Interactive Text Hooker is free software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
//#include <ITH\ntdll.h>
#include <ITH\common.h>
#include <ITH\HookManager.h>
#include <ITH\mem.h>
#include <ITH\IHF_SYS.h>
#include <ITH\ITH_TLS.h>
#include "ITH.h"
extern HookManager* man;
DWORD GetProcessPath(HANDLE hProc, LPWSTR path)
{
PROCESS_BASIC_INFORMATION info;
LDR_DATA_TABLE_ENTRY entry;
PEB_LDR_DATA ldr;
PEB peb;
if (NT_SUCCESS(NtQueryInformationProcess(hProc, ProcessBasicInformation, &info, sizeof(info), 0)))
if (info.PebBaseAddress)
if (NT_SUCCESS(NtReadVirtualMemory(hProc, info.PebBaseAddress, &peb,sizeof(peb), 0)))
if (NT_SUCCESS(NtReadVirtualMemory(hProc, peb.Ldr, &ldr, sizeof(ldr), 0)))
if (NT_SUCCESS(NtReadVirtualMemory(hProc, (LPVOID)ldr.InLoadOrderModuleList.Flink,
&entry, sizeof(LDR_DATA_TABLE_ENTRY), 0)))
if (NT_SUCCESS(NtReadVirtualMemory(hProc, entry.FullDllName.Buffer,
path, MAX_PATH * 2, 0))) return 1;
return 0;
}
DWORD GetProcessPath(DWORD pid, LPWSTR path)
{
CLIENT_ID id;
OBJECT_ATTRIBUTES oa = {};
HANDLE hProc;
NTSTATUS status;
id.UniqueProcess = pid;
id.UniqueThread = 0;
oa.uLength = sizeof(oa);
status = NtOpenProcess(&hProc , PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, &oa, &id);
if (NT_SUCCESS(status))
{
DWORD flag = GetProcessPath(hProc, path);
NtClose(hProc);
return flag;
}
else return 0;
};
DWORD GetProcessMemory1(HANDLE hProc, DWORD& size, DWORD& ws)
{
DWORD len = 0x10000, s = 0, retl = 0;
DWORD *buffer = 0;
NTSTATUS status = STATUS_INFO_LENGTH_MISMATCH;
while (status == STATUS_INFO_LENGTH_MISMATCH)
{
delete buffer;
len <<= 1;
buffer = new DWORD[len];
status = NtQueryVirtualMemory(hProc, 0, MemoryWorkingSetList, buffer, len<<2, &retl);
}
if (!NT_SUCCESS(status))
{
delete buffer;
return 0;
}
len = *(DWORD*)buffer;
ws = len << 2;
for (DWORD i = 1; i <= len; i++)
s += (buffer[i] >> 8) & 1; //Hot spot.
size = (len - s) << 2;
delete buffer;
return 1;
}
DWORD GetProcessMemory(HANDLE hProc, DWORD& mem_size, DWORD& ws)
{
DWORD len,retl,s;
LPVOID buffer = 0;
NTSTATUS status;
static const DWORD table[]={0x100,0x100,0x100,0x100};
len = 0x4000;
status = NtAllocateVirtualMemory(NtCurrentProcess(), &buffer, 0, &len, MEM_COMMIT, PAGE_READWRITE);
if (!NT_SUCCESS(status)) return 0;
status = NtQueryVirtualMemory(hProc, 0, MemoryWorkingSetList, buffer, len, &retl);
if (status == STATUS_INFO_LENGTH_MISMATCH)
{
len = *(DWORD*)buffer;
len = ((len << 2) & 0xFFFFF000) + 0x1000;
s = 0;
NtFreeVirtualMemory(NtCurrentProcess(), &buffer, &s, MEM_RELEASE);
buffer = 0;
status = NtAllocateVirtualMemory(NtCurrentProcess(), &buffer, 0, &len, MEM_COMMIT, PAGE_READWRITE);
if (!NT_SUCCESS(status)) return 0;
status = NtQueryVirtualMemory(hProc, 0, MemoryWorkingSetList, buffer, len, &retl);
if (!NT_SUCCESS(status)) return 0;
}
else if (!NT_SUCCESS(status)) return 0;
__asm
{
mov esi,buffer
mov edi,[esi]
mov eax,ws
lea ebx,[edi*4]
mov [eax],ebx
mov ecx,edi
xor edx,edx
bt edi,8
sbb edx,0
shl edx,8
lea edi,[esi+ebx+4]
pxor xmm4,xmm4
pxor xmm5,xmm5
pxor xmm6,xmm6
pxor xmm7,xmm7
lea eax,[esp-0x20]
and al,0xF0
movdqu xmm0,table
movdqa [eax],xmm0
prefetcht0 [eax]
mov ebx,0x40
lea eax,[eax]
sse_calc:
movdqa xmm0,[esi]
movdqa xmm1,[esi+0x10]
movdqa xmm2,[esi+0x20]
movdqa xmm3,[esi+0x30]
pand xmm0,[eax]
pand xmm1,[eax]
pand xmm2,[eax]
pand xmm3,[eax]
paddd xmm4,xmm0
paddd xmm5,xmm1
paddd xmm6,xmm2
paddd xmm7,xmm3
add esi,ebx
cmp esi,edi
jb sse_calc
paddd xmm4,xmm5
paddd xmm6,xmm7
paddd xmm4,xmm6
movaps [eax],xmm4
add edx,[eax]
add edx,[eax+0x4]
add edx,[eax+0x8]
add edx,[eax+0xC]
shr edx,8
sub edx,ecx
neg edx
add edx,edx
mov ecx,mem_size
add edx,edx
mov [ecx],edx
}
s = 0;
NtFreeVirtualMemory(NtCurrentProcess(), &buffer, &s, MEM_RELEASE);
return 1;
}
DWORD GetCode(const HookParam& hp, LPWSTR buffer, DWORD pid)
{
WCHAR c;
LPWSTR ptr=buffer;
if (hp.type&PRINT_DWORD) c=L'H';
else if (hp.type&USING_UNICODE)
{
if (hp.type&USING_STRING) c=L'Q';
else if (hp.type&STRING_LAST_CHAR) c=L'L';
else c=L'W';
}
else
{
if (hp.type&USING_STRING) c=L'S';
else if (hp.type&BIG_ENDIAN) c=L'A';
else if (hp.type&STRING_LAST_CHAR) c=L'E';
else c=L'B';
}
ptr+=swprintf(ptr,L"/H%c",c);
if(hp.type&NO_CONTEXT) *ptr++=L'N';
if (hp.off>>31) ptr+=swprintf(ptr,L"-%X",-(hp.off+4));
else ptr+=swprintf(ptr,L"%X",hp.off);
if (hp.type&DATA_INDIRECT)
{
if (hp.ind>>31) ptr+=swprintf(ptr,L"*-%X",-hp.ind);
else ptr+=swprintf(ptr,L"*%X",hp.ind);
}
if (hp.type&USING_SPLIT)
{
if (hp.split>>31) ptr+=swprintf(ptr,L":-%X",-(4+hp.split));
else ptr+=swprintf(ptr,L":%X",hp.split);
}
if (hp.type&SPLIT_INDIRECT)
{
if (hp.split_ind>>31) ptr+=swprintf(ptr,L"*-%X",-hp.split_ind);
else ptr+=swprintf(ptr,L"*%X",hp.split_ind);
}
if (pid)
{
ProcessRecord *pr = man->GetProcessRecord(pid);
if (pr)
{
MEMORY_BASIC_INFORMATION info;
HANDLE hProc=pr->process_handle;
if (NT_SUCCESS(NtQueryVirtualMemory(hProc,(PVOID)hp.addr,
MemoryBasicInformation,&info,sizeof(info),0)))
{
if (info.Type & MEM_IMAGE)
{
WCHAR path[MAX_PATH];
if (NT_SUCCESS(NtQueryVirtualMemory(hProc,(PVOID)hp.addr,
MemorySectionName,path,MAX_PATH*2,0)))
{
ptr+=swprintf(ptr,L"@%X:%s",hp.addr-(DWORD)info.AllocationBase,wcsrchr(path,L'\\')+1);
return ptr - buffer;
}
}
}
}
}
if (hp.module)
{
ptr+=swprintf(ptr,L"@%X!%X",hp.addr,hp.module);
if (hp.function) ptr+=swprintf(ptr,L"!%X",hp.function);
}
else ptr+=swprintf(ptr,L"@%X",hp.addr);
return ptr - buffer;
}
int UTF8to16len(const char* mb)
{
int len = 0;
char c;
while((c = *mb) != 0)
{
if (c & 0x80)
{
while (c & 0x80)
{
mb++;
c <<= 1;
}
}
else
{
mb++;
}
len++;
}
return len;
}
int UTF8to16(const char* mb, wchar_t* wc)
{
__asm
{
mov esi, mb
mov edi, wc
push edi
_next_char:
movzx eax, byte ptr[esi]
test al,al
jz _finish
test al,0x80
jnz _non_ascii
stosw
inc esi
jmp _next_char
_non_ascii:
test al,0x40
jz _finish
test al,0x20
jz _utf11bit
and al,0xF
mov cl,[esi + 1]
and cl,0x3F
mov dl,[esi + 2]
and dl,0x3F
shl eax,6
or al,cl
shl eax,6
or al,dl
stosw
add esi,3
jmp _next_char
_utf11bit:
and al,0x1F
shl eax,6
movzx ecx,[esi+1]
and cl,0x3F
or eax,ecx
stosw
add esi,2
jmp _next_char
_finish:
pop eax
sub edi,eax
mov eax,edi
shr eax,1
}
}
int UTF8to16_c(const char* mb, wchar_t* wc)
{
int len = 0;
char c;
wchar_t w;
while ((c = *mb) != 0)
{
if (c & 0x80)
{
if (c & 40)
{
if (c & 0x20)
{
if (c & 0x10)
return len;
w = c & 0xF;
w = (w << 6) | (mb[1] & 0x3F);
w = (w << 6) | (mb[2] & 0x3F);
*wc++ = w;
mb += 3;
}
else
{
w = c & 0x1F;
w = (w << 6) | (mb[1] & 0x3F);
*wc++ = w;
mb += 2;
}
}
else return len;
}
else
{
*wc++ = c;
mb++;
}
len++;
}
return len;
}
wchar_t* AllocateUTF16AndConvertUTF8(const char* utf8)
{
int len = UTF8to16len(utf8);
if (len == 0) return 0;
wchar_t* str = new wchar_t[len + 1];
UTF8to16(utf8, str);
str[len] = 0;
return str;
}
void ReleaseUTF16String(wchar_t* str)
{
delete str;
}
int UTF16to8(const wchar_t* wc, char* mb)
{
wchar_t c;
char* start = mb;
while (c = *wc++)
{
if (c >> 7)
{
if (c >> 11)
{
mb[2] = (c & 0x3F) | 0x80;
c >>= 6;
mb[1] = (c & 0x3F) | 0x80;
c >>= 6;
mb[0] = c | 0xE0;
mb += 3;
}
else
{
mb[1] = (c & 0x3F) | 0x80;
mb[0] = (c >> 6) | 0xC0;
mb += 2;
}
}
else
{
*mb++ = c & 0xFF;
}
}
return mb - start;
}
#define MAX_HASH_SIZE 0x20
BYTE hex_table_inverse[0x80] = {
-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,-1,-1, -1,-1,-1,-1,
-1,10,11,12, 13,14,15,-1, -1,-1,-1,-1, -1,-1,-1,-1,
-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
-1,10,11,12, 13,14,15,-1, -1,-1,-1,-1, -1,-1,-1,-1,
-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
};
DWORD StrToHexByte(const char* str)
{
BYTE c0 = str[1], c1 = str[0];
if ((c0 | c1) & 0x80) return -1;
c0 = hex_table_inverse[c0];
c1 = hex_table_inverse[c1];
return (c1 << 4) | c0;
}
void ByteToHexStr(char* hex_str, unsigned char b)
{
static const char hex_table[] = "0123456789ABCDEF";
hex_str[1] = hex_table[b & 0xF];
hex_str[0] = hex_table[b >> 4];
}
bool CheckHashStr(BYTE* value, DWORD size_in_bytes, const char* str)
{
DWORD i;
for (i = 0; i < size_in_bytes; i++)
{
DWORD t = StrToHexByte(str);
if (t == -1) return false;
if (value[i] != (t & 0xFF)) return false;
str += 2;
}
if (*str) return false;
return true;
}
bool CompareHashStr(const char* s1, const char* s2)
{
DWORD c1, c2;
while (*s1)
{
c1 = StrToHexByte(s1);
c2 = StrToHexByte(s2);
if ((c1 | c2) == -1) return false; //Either s1 or s2 contains odd chars or invalid chars.
if (c1 != c2) return false;
s1 += 2;
s2 += 2;
}
return true;
}
static char HTTP_OK[] = "HTTP/1.1 200 OK\r\n";
static char HTTP_END[] = "\r\n\r\n";
struct TitleParam
{
DWORD pid,buffer_len,retn_len;
LPWSTR buffer;
};
BOOL CALLBACK EnumProc(HWND hwnd,LPARAM lParam)
{
TitleParam* p = (TitleParam*)lParam;
CLIENT_ID id;
id.UniqueThread = GetWindowThreadProcessId(hwnd,&id.UniqueProcess);
if (id.UniqueProcess == p->pid)
{
if (GetWindowLong(hwnd,GWL_STYLE) & WS_VISIBLE)
{
p->retn_len = GetWindowTextLength(hwnd);
if (p->retn_len)
{
p->buffer = new WCHAR[p->retn_len + 1];
GetWindowText(hwnd,p->buffer,p->retn_len + 1);
p->buffer[p->retn_len] = 0;
return FALSE;
}
}
}
return TRUE;
}
LPWSTR SaveProcessTitle(DWORD pid)
{
TitleParam p;
p.buffer = 0;
p.pid = pid;
p.buffer_len = 0;
p.retn_len = 0;
EnumWindows(EnumProc, (LPARAM)&p);
return p.buffer;
}