C# - CreateProcess API 在用于生成任务管理器时返回 740

问题描述

我正在尝试使用 CreateProcess API 从 C# 生成任务管理器。 不幸的是,我总是从 CreateProcess 得到代码 740,有点谷歌搜索代码 740 是:ERROR_ELEVATION_required。 我使用的创建标志是 CreateSuspended、CreateDetachedProcess、CreateNowindow 和 CreateUnicodeEnvironment(以防万一我的父进程在 lpEnvironment 上有 Unicode)。正常情况下,任务管理器不需要管理员权限吧?

这是我使用的代码

    [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,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()));
        }
    }

有人知道如何解决这个问题吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)