-
Notifications
You must be signed in to change notification settings - Fork 236
/
GpuMemoryAbuse.cpp
372 lines (297 loc) · 11.9 KB
/
GpuMemoryAbuse.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
#include <Windows.h>
#pragma warning(disable:6011)
#define CUDACALL __stdcall
typedef struct PCUDE_CONTEXT* CUDA_CONTEXT;
typedef INT(CUDACALL* CUDAMEMORYALLOCATE)(ULONG_PTR, SIZE_T);
typedef INT(CUDACALL* CUDAINIT)(INT);
typedef INT(CUDACALL* CUDAGETDEVICECOUNT)(PINT);
typedef INT(CUDACALL* CUDAGETDEVICE)(PINT, INT);
typedef INT(CUDACALL* CUDACREATECONTEXT)(CUDA_CONTEXT*, DWORD, INT);
typedef INT(CUDACALL* CUDADESTROYCONTEXT)(CUDA_CONTEXT*);
typedef INT(CUDACALL* CUDAMEMORYCOPYTODEVICE)(ULONG_PTR, PVOID, SIZE_T);
typedef INT(CUDACALL* CUDAMEMORYCOPYTOHOST)(PVOID, ULONG_PTR, SIZE_T);
typedef INT(CUDACALL* CUDAMEMORYFREE)(ULONG_PTR);
#define CUDA_SUCCESS 0
typedef struct _NVIDIA_API_TABLE {
HMODULE NvidiaLibary;
CUDAMEMORYALLOCATE CudaMemoryAllocate;
CUDAINIT CudaInit;
CUDAGETDEVICECOUNT CudaGetDeviceCount;
CUDAGETDEVICE CudaGetDevice;
CUDACREATECONTEXT CudaCreateContext;
CUDAMEMORYCOPYTODEVICE CudaMemoryCopyToDevice;
CUDAMEMORYCOPYTOHOST CudaMemoryCopyToHost;
CUDAMEMORYFREE CudaMemoryFree;
CUDADESTROYCONTEXT CudaDestroyContext;
} NVIDIA_API_TABLE, *PNVIDIA_API_TABLE;
SIZE_T StringLengthW(LPCWSTR String)
{
LPCWSTR String2;
for (String2 = String; *String2; ++String2);
return (String2 - String);
}
PWCHAR StringLocateCharW(PWCHAR String, INT Character)
{
do
{
if (*String == Character)
return (PWCHAR)String;
} while (*String++);
return NULL;
}
INT StringCompareStringRegionW(PWCHAR String1, PWCHAR String2, SIZE_T Count)
{
UCHAR Block1, Block2;
while (Count-- > 0)
{
Block1 = (UCHAR)*String1++;
Block2 = (UCHAR)*String2++;
if (Block1 != Block2)
return Block1 - Block2;
if (Block1 == '\0')
return 0;
}
return 0;
}
PWCHAR StringFindSubstringW(PWCHAR String1, PWCHAR String2)
{
PWCHAR pPointer = String1;
DWORD Length = (DWORD)StringLengthW(String2);
for (; (pPointer = StringLocateCharW(pPointer, *String2)) != 0; pPointer++)
{
if (StringCompareStringRegionW(pPointer, String2, Length) == 0)
return (PWCHAR)pPointer;
}
return NULL;
}
PWCHAR StringCopyW(PWCHAR String1, PWCHAR String2)
{
PWCHAR p = String1;
while ((*p++ = *String2++) != 0);
return String1;
}
PWCHAR StringConcatW(PWCHAR String, PWCHAR String2)
{
StringCopyW(&String[StringLengthW(String)], String2);
return String;
}
BOOL IsNvidiaGraphicsCardPresent(VOID)
{
DISPLAY_DEVICEW DisplayDevice; RtlZeroMemory(&DisplayDevice, sizeof(DISPLAY_DEVICEW));
DisplayDevice.cb = sizeof(DISPLAY_DEVICEW);
DWORD dwDeviceId = ERROR_SUCCESS;
while (EnumDisplayDevicesW(NULL, dwDeviceId, &DisplayDevice, 0))
{
if (StringFindSubstringW(DisplayDevice.DeviceString, (PWCHAR)L"NVIDIA") != NULL)
return TRUE;
}
return FALSE;
}
BOOL InitNvidiaCudaAPITable(PNVIDIA_API_TABLE Api)
{
Api->NvidiaLibary = LoadLibraryW(L"nvcuda.dll");
if (Api->NvidiaLibary == NULL)
return FALSE;
Api->CudaCreateContext = (CUDACREATECONTEXT)GetProcAddress(Api->NvidiaLibary, "cuCtxCreate_v2");
Api->CudaGetDevice = (CUDAGETDEVICE)GetProcAddress(Api->NvidiaLibary, "cuDeviceGet");
Api->CudaGetDeviceCount = (CUDAGETDEVICECOUNT)GetProcAddress(Api->NvidiaLibary, "cuDeviceGetCount");
Api->CudaInit = (CUDAINIT)GetProcAddress(Api->NvidiaLibary, "cuInit");
Api->CudaMemoryAllocate = (CUDAMEMORYALLOCATE)GetProcAddress(Api->NvidiaLibary, "cuMemAlloc_v2");
Api->CudaMemoryCopyToDevice = (CUDAMEMORYCOPYTODEVICE)GetProcAddress(Api->NvidiaLibary, "cuMemcpyHtoD_v2");
Api->CudaMemoryCopyToHost = (CUDAMEMORYCOPYTOHOST)GetProcAddress(Api->NvidiaLibary, "cuMemcpyDtoH_v2");
Api->CudaMemoryFree = (CUDAMEMORYFREE)GetProcAddress(Api->NvidiaLibary, "cuMemFree_v2");
Api->CudaDestroyContext = (CUDADESTROYCONTEXT)GetProcAddress(Api->NvidiaLibary, "cuCtxDestroy");
if (!Api->CudaCreateContext || !Api->CudaGetDevice || !Api->CudaGetDeviceCount || !Api->CudaInit || !Api->CudaDestroyContext)
return FALSE;
if (!Api->CudaMemoryAllocate || !Api->CudaMemoryCopyToDevice || !Api->CudaMemoryCopyToHost || !Api->CudaMemoryFree)
return FALSE;
return TRUE;
}
ULONG_PTR RtlAllocateGpuMemory(PNVIDIA_API_TABLE Api, DWORD ByteSize)
{
ULONG_PTR GpuBufferPointer = NULL;
if (ByteSize == 0)
return NULL;
if (Api->CudaMemoryAllocate((ULONG_PTR )&GpuBufferPointer, ByteSize) != CUDA_SUCCESS)
return NULL;
return GpuBufferPointer;
}
INT main(VOID)
{
/********************************************************************
* Variables
********************************************************************/
//Application variables
DWORD dwError = ERROR_SUCCESS;
BOOL bFlag = FALSE;
//NVIDIA related variables
NVIDIA_API_TABLE Api = { 0 };
INT DeviceCount = 0;
INT Device = 0;
CUDA_CONTEXT Context = NULL;;
ULONG_PTR GpuMemory = NULL;
//Subroutine related variables, unimportant to proof-of-concept
WCHAR BinaryPath[MAX_PATH * sizeof(WCHAR)] = { 0 };
HANDLE hHandle = INVALID_HANDLE_VALUE;
PBYTE DataBuffer = NULL;
HANDLE ProcessHeap = GetProcessHeap();
DWORD dwRead = 0;
/********************************************************************
* Start
*********************************************************************
*
* IsNvidiaGraphicsCardPresent() invokes the EnumDisplayDevicesW and
* performs a string comparison on DISPLAY_DEVICEW member
* DeviceString to look for the presence of NVIDIA. If NVIDIA string
* is present IsNvidiaGraphicsCardPresent() returns true else false.
* If IsNvidiaGraphicsCardPresent() returns false the application
* terminates. This proof-of-concept is NVIDIA specific.
*
* If IsNvidiaGraphicsCardPresent() returns true, we make a
* subsequent function call to InitNvidiaCudaAPITable().
* InitNvidiaCudaAPITable() populates a NVIDIA_API_TABLE structure
* whose members are function pointers to NVIDIA-related APIs omit
* the NvidiaLibrary HMODULE member whose value is returned from a
* call to LoadLibrary.
*
* Anyway, enjoy the proof-of-concept.
* With love, smelly__vx
* vx-underground.org
*
********************************************************************/
if (!IsNvidiaGraphicsCardPresent())
goto EXIT_ROUTINE;
if (!InitNvidiaCudaAPITable(&Api))
goto EXIT_ROUTINE;
/********************************************************************
* Unimportant section
*********************************************************************
*
* This section performs trivial tasks unrelated to the core
* concept being illustrated in this proof of concept. The code
* below assembles a path to the desktop by using
* GetEnvironmentVariableW and appending a hardcoded path. The hard
* coded path is a binary present on the desktop (must be created
* by the user).
*
* Once creating the desktop path in memory, we invoke CreateFile
* and get a handle to file on the desktop. Subsequently this code
* gets the file size, allocates it in memory, and reads the content
* into member (type PBYTE).
*
********************************************************************/
if (GetEnvironmentVariableW(L"USERPROFILE", BinaryPath, (MAX_PATH * sizeof(WCHAR))) == 0)
goto EXIT_ROUTINE;
if (StringConcatW(BinaryPath, (PWCHAR)L"\\Desktop\\Demo.txt") == NULL)
goto EXIT_ROUTINE;
hHandle = CreateFileW(BinaryPath, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hHandle == INVALID_HANDLE_VALUE)
goto EXIT_ROUTINE;
dwError = GetFileSize(hHandle, NULL);
if (dwError == INVALID_FILE_SIZE)
goto EXIT_ROUTINE;
DataBuffer = (PBYTE)HeapAlloc(ProcessHeap, HEAP_ZERO_MEMORY, dwError);
if (DataBuffer == NULL)
goto EXIT_ROUTINE;
if (!ReadFile(hHandle, DataBuffer, dwError, &dwRead, NULL))
goto EXIT_ROUTINE;
dwError = ERROR_SUCCESS;
/********************************************************************
* Unimportant code segment end
********************************************************************/
/********************************************************************
* NVIDIA CUDA Code segment
*********************************************************************
*
* The code below copies content from the data file read off the
* desktop onto the GPU, frees the heap, then copies the data back
*
* CUDA_SUCCESS is defined as 0 (zero). Successful CUDA invocations
* return 0 (zero).
*
* CudaInit(0) is required to initialize NVIDIA API interface. The
* subsequent function calls, CudaGetDeviceCount, and
* CudaGetDevice are generic NVIDIA functions to enumerating and
* identifying GPU devices - in the unlikely event more than one
* GPU is present.
*
* The handle returned by CudaGetDevice is used to create a CUDA
* context - the state of the application, how two consecutive
* API calls are related to each other while utilizing the CUDA
* runtime API.
*
* This code forwards to the NVIDIA driver.
*
* After a context is successfully created - we allocate memory
* on the GPU via our wrapper function, RtlAllocateGpuMemory, which
* returns a ULONG_PTR, a pointer to the allocated GPU memory.
* The calls that proceed this first copy data to the GPU, and
* finally, retrive the data back. This is illustrated by this
* proof-of-concept freeing the heap which contained data which was
* previously stored in the DataBuffer variable.
*
********************************************************************/
if (Api.CudaInit(0) != CUDA_SUCCESS)
goto EXIT_ROUTINE;
if (Api.CudaGetDeviceCount(&DeviceCount) != CUDA_SUCCESS || DeviceCount == 0)
goto EXIT_ROUTINE;
if (Api.CudaGetDevice(&Device, DeviceCount - 1) != CUDA_SUCCESS)
goto EXIT_ROUTINE;
if (Api.CudaCreateContext(&Context, 0, Device) != CUDA_SUCCESS)
goto EXIT_ROUTINE;
GpuMemory = RtlAllocateGpuMemory(&Api, dwRead);
if (GpuMemory == NULL)
goto EXIT_ROUTINE;
if (Api.CudaMemoryCopyToDevice(GpuMemory, DataBuffer, dwRead) != CUDA_SUCCESS)
goto EXIT_ROUTINE;
/********************************************************************
* Unimportant section
*********************************************************************
*
* Frees the heap, to illustrate the DataBuffer variable is indeed
* empty, containing no data. We then reallocate the buffer and
* copy the contents from the GPU back onto the hosts heap.
*
********************************************************************/
if (DataBuffer)
HeapFree(ProcessHeap, HEAP_ZERO_MEMORY, DataBuffer);
Sleep(1000);
DataBuffer = (PBYTE)HeapAlloc(ProcessHeap, HEAP_ZERO_MEMORY, dwRead);
if (DataBuffer == NULL)
goto EXIT_ROUTINE;
/********************************************************************
* Unimportant code segment end
********************************************************************/
if (Api.CudaMemoryCopyToHost(DataBuffer, GpuMemory, dwRead) != CUDA_SUCCESS)
goto EXIT_ROUTINE;
/********************************************************************
* EXIT_ROUTINE
*********************************************************************
*
* bFlag is a generic variable. Its value by default, FALSE,
* indicates whether or not the application has entered the
* exit routine normally. If the code has finished execution, then
* bFlag will be set to TRUE indicating there has been no issues.
* Otherwise, bFlag being FALSE indicates failure and GetLastError
* is invoked.
*
* Regardless of failure, each variable is checked to determine if
* it needs to be freed or unloaded.
*
********************************************************************/
bFlag = TRUE;
EXIT_ROUTINE:
if (!bFlag)
dwError = GetLastError();
if (DataBuffer)
HeapFree(ProcessHeap, HEAP_ZERO_MEMORY, DataBuffer);
if (GpuMemory != NULL)
Api.CudaMemoryFree(GpuMemory);
if (Context != NULL)
Api.CudaDestroyContext(&Context);
if (Api.NvidiaLibary)
FreeLibrary(Api.NvidiaLibary);
if (hHandle)
CloseHandle(hHandle);
return dwError;
}