Beispiel #1
0
 /// <summary>
 /// Dispose method.
 /// </summary>
 public void Dispose()
 {
     _handles.Dispose();
     DataView.Dispose();
     SecurityContext.Dispose();
 }
        /// <summary>
        /// Start the new process
        /// </summary>
        /// <param name="image_path">The image path to the file to execute</param>
        /// <returns>The result of the process creation</returns>
        public CreateUserProcessResult Start(string image_path)
        {
            if (image_path == null)
            {
                throw new System.ArgumentNullException("image_path");
            }

            IntPtr process_params = CreateProcessParameters(ConfigImagePath ?? image_path, DllPath, CurrentDirectory,
                                                            CommandLine, Environment, WindowTitle, DesktopInfo, ShellInfo, RuntimeData, 1);
            DisposableList <ProcessAttribute> attrs = new DisposableList <ProcessAttribute>();

            try
            {
                ProcessCreateInfo create_info = new ProcessCreateInfo();

                attrs.Add(ProcessAttribute.ImageName(image_path));
                SafeStructureInOutBuffer <SectionImageInformation> image_info = new SafeStructureInOutBuffer <SectionImageInformation>();
                attrs.Add(ProcessAttribute.ImageInfo(image_info));
                SafeStructureInOutBuffer <ClientId> client_id = new SafeStructureInOutBuffer <ClientId>();
                attrs.Add(ProcessAttribute.ClientId(client_id));
                attrs.AddRange(AdditionalAttributes);
                if (ParentProcess != null)
                {
                    attrs.Add(ProcessAttribute.ParentProcess(ParentProcess.Handle));
                }

                if (RestrictChildProcess || OverrideRestrictChildProcess)
                {
                    attrs.Add(ProcessAttribute.ChildProcess(RestrictChildProcess, OverrideRestrictChildProcess));
                }

                if (Token != null)
                {
                    attrs.Add(ProcessAttribute.Token(Token.Handle));
                }

                ProcessAttributeList attr_list = new ProcessAttributeList(attrs);

                create_info.Data.InitFlags = InitFlags | ProcessCreateInitFlag.WriteOutputOnExit;
                create_info.Data.ProhibitedImageCharacteristics = ProhibitedImageCharacteristics;
                create_info.Data.AdditionalFileAccess           = AdditionalFileAccess;

                using (ObjectAttributes proc_attr = new ObjectAttributes(null, AttributeFlags.None, (NtObject)null, null, ProcessSecurityDescriptor),
                       thread_attr = new ObjectAttributes(null, AttributeFlags.None, (NtObject)null, null, ThreadSecurityDescriptor))
                {
                    NtStatus status = NtSystemCalls.NtCreateUserProcess(
                        out SafeKernelObjectHandle process_handle, out SafeKernelObjectHandle thread_handle,
                        ProcessDesiredAccess, ThreadDesiredAccess,
                        proc_attr, thread_attr, ProcessFlags,
                        ThreadFlags, process_params, create_info, attr_list);

                    if (!status.IsSuccess() && !ReturnOnError)
                    {
                        // Close handles which come from errors
                        switch (create_info.State)
                        {
                        case ProcessCreateState.FailOnSectionCreate:
                            NtSystemCalls.NtClose(create_info.Data.FileHandle);
                            break;

                        case ProcessCreateState.FailExeName:
                            NtSystemCalls.NtClose(create_info.Data.IFEOKey);
                            break;
                        }

                        status.ToNtException();
                    }

                    if (create_info.State == ProcessCreateState.Success)
                    {
                        return(new CreateUserProcessResult(process_handle, thread_handle,
                                                           create_info.Data, image_info.Result, client_id.Result, TerminateOnDispose));
                    }
                    else
                    {
                        return(new CreateUserProcessResult(status, create_info.Data, create_info.State));
                    }
                }
            }
            finally
            {
                NtRtl.RtlDestroyProcessParameters(process_params);
                attrs.Dispose();
            }
        }
Beispiel #3
0
 /// <summary>
 /// Dispose the safe buffer.
 /// </summary>
 /// <param name="disposing">True if disposing</param>
 protected override void Dispose(bool disposing)
 {
     _resources?.Dispose();
     base.Dispose(disposing);
 }
 protected override bool ReleaseHandle()
 {
     _handles.Dispose();
     return(base.ReleaseHandle());
 }