Skip to content

Commit 7efe0e4

Browse files
committed
Add callback for NtProcess creation.
1 parent 370fc4d commit 7efe0e4

5 files changed

Lines changed: 26 additions & 17 deletions

File tree

NtApiDotNet/CreateUserProcess.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ public CreateUserProcessResult Start(string image_path)
307307
if (image_path == null)
308308
throw new ArgumentNullException("image_path");
309309

310-
using (var process_params = SafeProcessParametersHandle.Create(ConfigImagePath ?? image_path, DllPath, CurrentDirectory,
310+
using (var process_params = SafeProcessParametersBuffer.Create(ConfigImagePath ?? image_path, DllPath, CurrentDirectory,
311311
CommandLine, Environment, WindowTitle, DesktopInfo, ShellInfo, RuntimeData, CreateProcessParametersFlags.Normalize))
312312
{
313313
using (var attrs = new DisposableList<ProcessAttribute>())

NtApiDotNet/NtApiDotNet.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@
7171
<Compile Include="SafeArrayBuffer.cs" />
7272
<Compile Include="SafeHandleListHandle.cs" />
7373
<Compile Include="SafeIoStatusBuffer.cs" />
74-
<Compile Include="SafeProcessParametersHandle.cs" />
74+
<Compile Include="SafeProcessParametersBuffer.cs" />
7575
<Compile Include="SafeStringBuffer.cs" />
7676
<Compile Include="SidIdentifierAuthority.cs" />
7777
<Compile Include="Utilities\Memory\CrossBitnessProcessMemoryReader.cs" />

NtApiDotNet/NtProcess.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,16 +122,20 @@ private static NtProcessCreateResult Create(NtProcessCreateConfig config, string
122122
{
123123
using (var dispose = new DisposableList())
124124
{
125-
var process_params = SafeProcessParametersHandle.Null;
125+
var process_params = SafeProcessParametersBuffer.Null;
126126
if (!fork)
127127
{
128-
var result = dispose.AddResource(SafeProcessParametersHandle.Create(config.ConfigImagePath ?? image_path,
128+
var result = dispose.AddResource(SafeProcessParametersBuffer.Create(config.ConfigImagePath ?? image_path,
129129
config.DllPath, config.CurrentDirectory, config.CommandLine, config.Environment,
130130
config.WindowTitle, config.DesktopInfo, config.ShellInfo, config.RuntimeData,
131131
CreateProcessParametersFlags.Normalize, throw_on_error));
132132
if (!result.IsSuccess)
133133
return new NtProcessCreateResult(result.Status);
134134
process_params = result.Result;
135+
if (config.ProcessParametersCallback != null)
136+
{
137+
process_params = config.ProcessParametersCallback(process_params, dispose);
138+
}
135139
}
136140

137141
ProcessCreateInfo create_info = dispose.AddResource(new ProcessCreateInfo());

NtApiDotNet/NtProcessCreateConfig.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
using System;
1516
using System.Collections.Generic;
1617

1718
namespace NtApiDotNet
@@ -157,6 +158,11 @@ public sealed class NtProcessCreateConfig
157158
/// Capture additional information when NtProcess.Create returns.
158159
/// </summary>
159160
public bool CaptureAdditionalInformation { get; set; }
161+
162+
/// <summary>
163+
/// Specify callback to update process parameters.
164+
/// </summary>
165+
public Func<SafeProcessParametersBuffer, DisposableList, SafeProcessParametersBuffer> ProcessParametersCallback { get; set; }
160166
#endregion
161167

162168
#region Public Methods

NtApiDotNet/SafeProcessParametersHandle.cs renamed to NtApiDotNet/SafeProcessParametersBuffer.cs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,26 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
using Microsoft.Win32.SafeHandles;
1615
using System;
16+
using System.Runtime.InteropServices;
1717

1818
namespace NtApiDotNet
1919
{
2020
#pragma warning disable 1591
21-
22-
public sealed class SafeProcessParametersHandle : SafeHandleZeroOrMinusOneIsInvalid
21+
public sealed class SafeProcessParametersBuffer : SafeBuffer
2322
{
24-
public SafeProcessParametersHandle(IntPtr proc_params, bool owns_handle) : base(owns_handle)
23+
public SafeProcessParametersBuffer(IntPtr proc_params, bool owns_handle) : base(owns_handle)
2524
{
2625
SetHandle(proc_params);
26+
uint size = 0;
27+
if (proc_params != IntPtr.Zero)
28+
size = (uint)Marshal.ReadInt32(proc_params);
29+
Initialize(size);
2730
}
2831

29-
public SafeProcessParametersHandle() : base(true)
30-
{
31-
}
32-
33-
public static SafeProcessParametersHandle Null
32+
public static SafeProcessParametersBuffer Null
3433
{
35-
get => new SafeProcessParametersHandle(IntPtr.Zero, false);
34+
get => new SafeProcessParametersBuffer(IntPtr.Zero, false);
3635
}
3736

3837
protected override bool ReleaseHandle()
@@ -50,7 +49,7 @@ private static UnicodeString GetString(string s)
5049
return s != null ? new UnicodeString(s) : null;
5150
}
5251

53-
public static NtResult<SafeProcessParametersHandle> Create(
52+
public static NtResult<SafeProcessParametersBuffer> Create(
5453
string image_path_name,
5554
string dll_path,
5655
string current_directory,
@@ -65,10 +64,10 @@ public static NtResult<SafeProcessParametersHandle> Create(
6564
{
6665
return NtRtl.RtlCreateProcessParametersEx(out IntPtr ret, GetString(image_path_name), GetString(dll_path), GetString(current_directory),
6766
GetString(command_line), environment, GetString(window_title), GetString(desktop_info), GetString(shell_info),
68-
GetString(runtime_data), flags).CreateResult(throw_on_error, () => new SafeProcessParametersHandle(ret, true));
67+
GetString(runtime_data), flags).CreateResult(throw_on_error, () => new SafeProcessParametersBuffer(ret, true));
6968
}
7069

71-
public static SafeProcessParametersHandle Create(
70+
public static SafeProcessParametersBuffer Create(
7271
string image_path_name,
7372
string dll_path,
7473
string current_directory,

0 commit comments

Comments
 (0)