.net - Brug af efterligning til at starte processer (indirekte) i C #

Indlæg af Hanne Mølgaard Plasc

Problem



Jeg arbejder på en applikation, der bruger Selen til at styre en browser. Selen lancerer browseren, når den initialiseres, og jeg kalder derfor aldrig Process.Start direkte på browserens exe. Jeg vil gerne, at Selen og alle dets barnprocesser kører under en anden bruger end min ansøgning (da jeg ikke ' t vil have adgang til min kilde). Ved hjælp af den efterligningsprøve, der findes her, forsøger jeg at pakke Selenekoden med efterligning, men det ser ud til, at alle børneprocesser bliver lanceret af brugeren, der lancerede deres overordnede proces. [2]


Kender nogen en måde at pakke en blok kode med brugerens efterligning og få alle børneprocesser til at starte med de eftergivne brugeres tilladelser? Hvis ikke, hvad ville være den bedste taktik for at opnå dette? Kør alle Selenielogikken i en anden proces og på en eller anden måde rør kommandoer til det?

Bedste reference


Prøv dette .


Denne klasse giver dig mulighed for at spise frokost som bruger,


her bruger jeg Explorer-sessionen.


using System.Runtime.InteropServices;
using System;
using System.Diagnostics;


[StructLayout(LayoutKind.Sequential)]
internal struct PROCESS\_INFORMATION
{
    public IntPtr hProcess;
    public IntPtr hThread;
    public uint dwProcessId;
    public uint dwThreadId;
 }



 [StructLayout(LayoutKind.Sequential)]
 internal struct SECURITY\_ATTRIBUTES
 {
    public uint nLength;
    public IntPtr lpSecurityDescriptor;
    public bool bInheritHandle;
  }


  [StructLayout(LayoutKind.Sequential)]
  public struct STARTUPINFO
  {
      public uint cb;
      public string lpReserved;
      public string lpDesktop;
      public string lpTitle;
      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 short wShowWindow;
      public short cbReserved2;
      public IntPtr lpReserved2;
      public IntPtr hStdInput;
      public IntPtr hStdOutput;
      public IntPtr hStdError;

  }

   internal enum SECURITY\_IMPERSONATION\_LEVEL
   {
       SecurityAnonymous,
       SecurityIdentification,
       SecurityImpersonation,
       SecurityDelegation
    }

    internal enum TOKEN\_TYPE
    {
         TokenPrimary = 1,
         TokenImpersonation
     }

 public class ProcessAsUser
 {

[DllImport("advapi32.dll", SetLastError = true)]
private static extern bool CreateProcessAsUser(
    IntPtr hToken,
    string lpApplicationName,
    string lpCommandLine,
    ref SECURITY\_ATTRIBUTES lpProcessAttributes,
    ref SECURITY\_ATTRIBUTES lpThreadAttributes,
    bool bInheritHandles,
    uint dwCreationFlags,
    IntPtr lpEnvironment,
    string lpCurrentDirectory,
    ref STARTUPINFO lpStartupInfo,
    out PROCESS\_INFORMATION lpProcessInformation);


[DllImport("advapi32.dll", EntryPoint = "DuplicateTokenEx", SetLastError = true)]
private static extern bool DuplicateTokenEx(
    IntPtr hExistingToken,
    uint dwDesiredAccess,
    ref SECURITY\_ATTRIBUTES lpThreadAttributes,
    Int32 ImpersonationLevel,
    Int32 dwTokenType,
    ref IntPtr phNewToken);


[DllImport("advapi32.dll", SetLastError = true)]
private static extern bool OpenProcessToken(
    IntPtr ProcessHandle,
    UInt32 DesiredAccess,
    ref IntPtr TokenHandle);

[DllImport("userenv.dll", SetLastError = true)]
private static extern bool CreateEnvironmentBlock(
        ref IntPtr lpEnvironment,
        IntPtr hToken,
        bool bInherit);


[DllImport("userenv.dll", SetLastError = true)]
private static extern bool DestroyEnvironmentBlock(
        IntPtr lpEnvironment);

[DllImport("kernel32.dll", SetLastError = true)]
private static extern bool CloseHandle(
    IntPtr hObject);

private const short SW\_SHOW = 5;
private const uint TOKEN\_QUERY = 0x0008;
private const uint TOKEN\_DUPLICATE = 0x0002;
private const uint TOKEN\_ASSIGN\_PRIMARY = 0x0001;
private const int GENERIC\_ALL\_ACCESS = 0x10000000;
private const int STARTF\_USESHOWWINDOW = 0x00000001;
private const int STARTF\_FORCEONFEEDBACK = 0x00000040;
private const uint CREATE\_UNICODE\_ENVIRONMENT = 0x00000400;


private static bool LaunchProcessAsUser(string cmdLine, IntPtr token, IntPtr envBlock)
{
    bool result = false;


    PROCESS\_INFORMATION pi = new PROCESS\_INFORMATION();
    SECURITY\_ATTRIBUTES saProcess = new SECURITY\_ATTRIBUTES();
    SECURITY\_ATTRIBUTES saThread = new SECURITY\_ATTRIBUTES();
    saProcess.nLength = (uint)Marshal.SizeOf(saProcess);
    saThread.nLength = (uint)Marshal.SizeOf(saThread);

    STARTUPINFO si = new STARTUPINFO();
    si.cb = (uint)Marshal.SizeOf(si);


    //if this member is NULL, the new process inherits the desktop 
    //and window station of its parent process. If this member is 
    //an empty string, the process does not inherit the desktop and 
    //window station of its parent process; instead, the system 
    //determines if a new desktop and window station need to be created. 
    //If the impersonated user already has a desktop, the system uses the 
    //existing desktop. 

    si.lpDesktop = @"WinSta0Default"; //Modify as needed 
    si.dwFlags = STARTF\_USESHOWWINDOW | STARTF\_FORCEONFEEDBACK;
    si.wShowWindow = SW\_SHOW;
    //Set other si properties as required. 

    result = CreateProcessAsUser(
        token,
        null,
        cmdLine,
        ref saProcess,
        ref saThread,
        false,
        CREATE\_UNICODE\_ENVIRONMENT,
        envBlock,
        null,
        ref si,
        out pi);


    if (result == false)
    {
        int error = Marshal.GetLastWin32Error();
        string message = String.Format("CreateProcessAsUser Error: {0}", error);
        Debug.WriteLine(message);

    }

    return result;
}


private static IntPtr GetPrimaryToken(int processId)
{
    IntPtr token = IntPtr.Zero;
    IntPtr primaryToken = IntPtr.Zero;
    bool retVal = false;
    Process p = null;

    try
    {
        p = Process.GetProcessById(processId);
    }

    catch (ArgumentException)
    {

        string details = String.Format("ProcessID {0} Not Available", processId);
        Debug.WriteLine(details);
        throw;
    }


    //Gets impersonation token 
    retVal = OpenProcessToken(p.Handle, TOKEN\_DUPLICATE, ref token);
    if (retVal == true)
    {

        SECURITY\_ATTRIBUTES sa = new SECURITY\_ATTRIBUTES();
        sa.nLength = (uint)Marshal.SizeOf(sa);

        //Convert the impersonation token into Primary token 
        retVal = DuplicateTokenEx(
            token,
            TOKEN\_ASSIGN\_PRIMARY | TOKEN\_DUPLICATE | TOKEN\_QUERY,
            ref sa,
            (int)SECURITY\_IMPERSONATION\_LEVEL.SecurityIdentification,
            (int)TOKEN\_TYPE.TokenPrimary,
            ref primaryToken);

        //Close the Token that was previously opened. 
        CloseHandle(token);
        if (retVal == false)
        {
            string message = String.Format("DuplicateTokenEx Error: {0}", Marshal.GetLastWin32Error());
            Debug.WriteLine(message);
        }

    }

    else
    {

        string message = String.Format("OpenProcessToken Error: {0}", Marshal.GetLastWin32Error());
        Debug.WriteLine(message);

    }

    //We'll Close this token after it is used. 
    return primaryToken;

}

private static IntPtr GetEnvironmentBlock(IntPtr token)
{

    IntPtr envBlock = IntPtr.Zero;
    bool retVal = CreateEnvironmentBlock(ref envBlock, token, false);
    if (retVal == false)
    {

        //Environment Block, things like common paths to My Documents etc. 
        //Will not be created if "false" 
        //It should not adversley affect CreateProcessAsUser. 

        string message = String.Format("CreateEnvironmentBlock Error: {0}", Marshal.GetLastWin32Error());
        Debug.WriteLine(message);

    }
    return envBlock;
}

public static bool Launch(string appCmdLine /*,int processId*/)
{

    bool ret = false;

    //Either specify the processID explicitly 
    //Or try to get it from a process owned by the user. 
    //In this case assuming there is only one explorer.exe 

    Process[] ps = Process.GetProcessesByName("explorer");
    int processId = -1;//=processId 
    if (ps.Length > 0)
    {
        processId = ps[0].Id;
    }

    if (processId > 1)
    {
        IntPtr token = GetPrimaryToken(processId);

        if (token != IntPtr.Zero)
        {

            IntPtr envBlock = GetEnvironmentBlock(token);
            ret = LaunchProcessAsUser(appCmdLine, token, envBlock);
            if (envBlock != IntPtr.Zero)
                DestroyEnvironmentBlock(envBlock);

            CloseHandle(token);
        }

    }
    return ret;
}


}