I'm trying to spawn task manager from C# using CreateProcess API.
Unfortunately,i always get code 740 from CreateProcess, a little bit of googling, code 740 is : ERROR_ELEVATION_REQUIRED
.
the creation flags I use are CreateSuspended,CreateDetachedProcess,CreateNoWindow,and CreateUnicodeEnvironment (just in case my parent process has Unicode on lpEnvironment). Normally,Task Manager doesnt need admin privilege right?
This is the code that i use :
[StructLayout(LayoutKind.Sequential)]
public struct STARTUPINFO
{
public uint cb;
public IntPtr lpReserved;
public IntPtr lpDesktop;
public IntPtr lpTitle;
public uint dwX;
public uint dwY;
public uint dwXSize;
public uint dwYSize;
public uint dwXCountChars;
public uint dwYCountChars;
public uint dwFillAttributes;
public uint dwFlags;
public ushort wShowWindow;
public ushort cbReserved;
public IntPtr lpReserved2;
public IntPtr hStdInput;
public IntPtr hStdOutput;
public IntPtr hStdErr;
}
[StructLayout(LayoutKind.Sequential)]
public struct STARTUPINFOEX
{
public STARTUPINFO StartupInfo;
public IntPtr lpAttributeList;
}
[StructLayout(LayoutKind.Sequential)]
public struct PROCESS_INFORMATION
{
public IntPtr hProcess;
public IntPtr hThread;
public int dwProcessId;
public int dwThreadId;
}
[StructLayout(LayoutKind.Sequential)]
public struct SECURITY_ATTRIBUTES
{
public int nLength;
public IntPtr lpSecurityDescriptor;
public int bInheritHandle;
}
[Flags]
public enum CreationFlags
{
CreateSuspended = 0x00000004,
DetachedProcess = 0x00000008,
CreateNoWindow = 0x08000000,
CreateUnicodeEnv = 0x00000400
}
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool CreateProcess(string lpApplicationName, string lpCommandLine, ref SECURITY_ATTRIBUTES lpProcessAttributes, ref SECURITY_ATTRIBUTES lpThreadAttributes, bool bInheritHandles, CreationFlags dwCreationFlags, IntPtr lpEnvironment, string lpCurrentDirectory, [In] ref STARTUPINFOEX lpStartupInfo, out PROCESS_INFORMATION lpProcessInformation);
public static void Main() {
string PathToExecutableForProcess = @"C:\Windows\System32\Taskmgr.exe";
STARTUPINFOEX sInfoEx = new STARTUPINFOEX();
PROCESS_INFORMATION pInfo = new PROCESS_INFORMATION();
sInfoEx.StartupInfo.cb = (uint)Marshal.SizeOf(sInfoEx);
IntPtr lpValue = IntPtr.Zero;
SECURITY_ATTRIBUTES pSec = new SECURITY_ATTRIBUTES();
SECURITY_ATTRIBUTES tSec = new SECURITY_ATTRIBUTES();
pSec.nLength = Marshal.SizeOf(pSec);
tSec.nLength = Marshal.SizeOf(tSec);
CreationFlags flags = CreationFlags.CreateSuspended | CreationFlags.DetachedProcess | CreationFlags.CreateNoWindow | CreationFlags.CreateUnicodeEnv;
// spawn the new process
bool CreateProcessResult = CreateProcess(PathToExecutableForProcess, null, ref pSec, ref tSec, false, flags, (IntPtr)0, null, ref sInfoEx, out pInfo);
if (CreateProcessResult) {
Console.WriteLine("[+] {0}'s process spawned!", PathToExecutableForProcess);
}else {
Console.WriteLine("[-] Failed to spawn the new process because of code {0}!", (Marshal.GetLastWin32Error()));
}
}
Does anybody know how to solve this?