-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRunAsImpersonator.cs
94 lines (84 loc) · 3.01 KB
/
RunAsImpersonator.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace RunAsX {
public class RunAsImpersonator {
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
struct StartupInfo {
public int cb;
public string reserved1;
public string desktop;
public string title;
public uint dwX;
public uint dwY;
public uint dwXSize;
public uint dwYSize;
public uint dwXCountChars;
public uint dwYCountChars;
public uint dwFillAttribute;
public uint dwFlags;
public ushort wShowWindow;
public short reserved2;
public int reserved3;
public IntPtr hStdInput;
public IntPtr hStdOutput;
public IntPtr hStdError;
}
[StructLayout(LayoutKind.Sequential)]
struct ProcessInfo {
public IntPtr hProcess;
public IntPtr hThread;
public uint dwProcessId;
public uint dwThreadId;
}
[Flags]
enum LogonFlags {
LOGON_WITH_PROFILE = 0x00000001,
LOGON_NETCREDENTIALS_ONLY = 0x00000002
}
[Flags]
enum CreationFlags {
CREATE_SUSPENDED = 0x00000004,
CREATE_NEW_CONSOLE = 0x00000010,
CREATE_NEW_PROCESS_GROUP = 0x00000200,
CREATE_UNICODE_ENVIRONMENT = 0x00000400,
CREATE_SEPARATE_WOW_VDM = 0x00000800,
CREATE_DEFAULT_ERROR_MODE = 0x04000000,
}
[DllImport("advapi32.dll", CharSet = CharSet.Unicode, ExactSpelling = true, SetLastError = true)]
static extern bool CreateProcessWithLogonW(
string principal,
string authority,
string password,
LogonFlags logonFlags,
string appName,
string cmdLine,
CreationFlags creationFlags,
IntPtr environmentBlock,
string currentDirectory,
ref StartupInfo startupInfo,
out ProcessInfo processInfo);
public static void Run(string file, string domain, string login, string password) {
//var commandLine = string.Format("\"{0}\" {1}", pathToIExploreExe, arguments);
var commandLine = string.Format("\"{0}\"", file);
uint LOGON_NETCREDENTIALS_ONLY = 2;
var lpStartupInfo = new StartupInfo();
ProcessInfo processInformation;
CreateProcessWithLogonW(
login,
domain,
password,
(LogonFlags) LOGON_NETCREDENTIALS_ONLY,
null,
commandLine,
0,
IntPtr.Zero,
null,
ref lpStartupInfo,
out processInformation);
}
}
}