1. 程式人生 > >PCB 模擬Windows管理員域帳號安裝軟體

PCB 模擬Windows管理員域帳號安裝軟體

在我們PCB行業中,區域網的電腦一般都會加入域控的,這樣可以方便集中管理使用者許可權,並可以對訪問網路資源可以進行許可權限制等.

由於加入了域控對帳號許可權的管理,這樣一來很多人都無許可權安裝軟體,比如:PCB工程人員的電腦沒有安裝防毒軟體,如果要安裝防毒軟體,這位工程師的帳號是無許可權安裝的,如果在不更改這位工程師人員的帳號許可權的前提下,想實現讓這位工程師可以自主安裝防毒軟體,就需要藉助模擬Windows域帳號進行操作了,實現的程式碼如下:

一,模擬管理員帳號安裝軟體程式碼

  string cmd = string.Format("{0} {1} {2}", "msiexec"
, "/i", @"\\192.192.1.1\Soft\agent_cloud_x86.msi"); //防毒軟體路徑 new Run().RunAs("域名", "使用者名稱", "密碼", cmd);

二,域帳號受權類程式碼

    public class Run
    {
        /// <summary>
        /// Run a command with specified name, same as "runas /user:&lt;domain&gt;&lt;/user&gt; &lt;cmdline&gt;
        
/// </summary> /// <param name="domain">domain where the user reside</param> /// <param name="user">user name</param> /// <param name="password">password</param> /// <param name="cmdline">the command line to execute</param> /// <returns>
0 if succeed, error code otherwise</returns> public int RunAs(string domain, string user, string password, string cmdline) { StartupInfo si = new StartupInfo(); si.cb = Marshal.SizeOf(typeof(StartupInfo)); si.title = String.Format(@"Running {0} as {1}\{2}", cmdline, domain, user); ProcessInfo pi = new ProcessInfo(); if (CreateProcessWithLogonW(user, domain, password, LogonFlags.LOGON_WITH_PROFILE, null, cmdline, 0, IntPtr.Zero, null, ref si, out pi)) { CloseHandle(pi.hProcess); CloseHandle(pi.hThread); return 0; } else { return Marshal.GetLastWin32Error(); } } /// <summary> /// Logon flags definition /// </summary> [Flags] enum LogonFlags { LOGON_WITH_PROFILE = 0x00000001, LOGON_NETCREDENTIALS_ONLY = 0x00000002 } /// <summary> /// Creation flag definition /// </summary> [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, } /// <summary> /// Process info definition /// </summary> [StructLayout(LayoutKind.Sequential)] struct ProcessInfo { public IntPtr hProcess; public IntPtr hThread; public uint dwProcessId; public uint dwThreadId; } /// <summary> /// Startup info definition /// </summary> [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; } /// <summary> /// API declaration – CreateProcessWithLogonW() /// </summary> /// <param name="principal">user name</param> /// <param name="authority">domain name in AD enviroment or "." for local machine</param> /// <param name="password">password</param> /// <param name="logonFlags">whether user profile is loaded before executing the command</param> /// <param name="appName">application name, null if the application name is included in the command line</param> /// <param name="cmdLine">the command line – may include application path</param> /// <param name="creationFlags">creation flag</param> /// <param name="environmentBlock">enviroment block</param> /// <param name="currentDirectory">current directory for the application</param> /// <param name="startupInfo">startup information</param> /// <param name="processInfo">process information</param> /// <returns></returns> [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); /// <summary> /// Close the handle /// </summary> /// <param name="h">the handle to close</param> /// <returns>true if success</returns> [DllImport("kernel32.dll")] static extern bool CloseHandle(IntPtr h); //使用該函式無法獲取被啟動程序的PID,這在一些需要該ID的場合不太方便。以下函式通過查詢系統當前程序表並比對啟動時間獲取新啟動程序的PID。引數"app"檔案映像名稱。如%WINDIR%system32cmd.exe, app需要指定為:cmd.exe。不需要包含路徑,但需要包含副檔名。 /// <summary> /// Get the latest started process ID by given application name /// </summary> /// <param name="app">the name of the application. MUST with extension.</param> /// <returns>The PID of the latest started process</returns> public int GetLastestPidByApplicationName(string app) { Process[] procs = Process.GetProcesses(); int lpid = -1; if ((procs != null) && (procs.Length > 0)) { DateTime latest = new DateTime(1970, 1, 1); try { for (int i = 0; i < procs.Length; i++) { if (procs[i].MainModule.FileName.ToLower().EndsWith("\\" + app.ToLower()) == false) { continue; } if ((latest != null) && (latest < procs[i].StartTime)) { latest = procs[i].StartTime; lpid = procs[i].Id; Console.WriteLine(latest.ToString()); } } } catch (Win32Exception) { } } return lpid; } }
View Code