Skip to content

Commit 53b6587

Browse files
committed
Pass through access mask to ALPC transport.
1 parent ed01978 commit 53b6587

File tree

9 files changed

+80
-14
lines changed

9 files changed

+80
-14
lines changed

NtApiDotNet/AlpcMessageAttributes.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -707,6 +707,22 @@ public AlpcHandleMessageAttributeEntry(NtObject obj)
707707
{
708708
Flags = AlpcHandleAttrFlags.SameAccess | AlpcHandleAttrFlags.SameAttributes;
709709
Handle = obj.Handle.DangerousGetHandle().ToInt32();
710+
}
711+
712+
/// <summary>
713+
/// Constructor.
714+
/// </summary>
715+
/// <param name="obj">The object to construct the entry from.</param>
716+
/// <param name="desired_access">The desired access for the attribute. If 0 then just copies the access.</param>
717+
public AlpcHandleMessageAttributeEntry(NtObject obj, AccessMask desired_access)
718+
{
719+
Flags = AlpcHandleAttrFlags.SameAttributes;
720+
DesiredAccess = desired_access;
721+
if (DesiredAccess.IsEmpty)
722+
{
723+
Flags |= AlpcHandleAttrFlags.SameAccess;
724+
}
725+
Handle = obj.Handle.DangerousGetHandle().ToInt32();
710726
}
711727
}
712728

NtApiDotNet/Ndr/Marshal/NdrMarshalBuffer.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public class NdrMarshalBuffer
3030
#region Private Members
3131
private readonly MemoryStream _stm;
3232
private readonly BinaryWriter _writer;
33-
private readonly List<NtObject> _handles;
33+
private readonly List<NdrSystemHandle> _handles;
3434
private NdrDeferralStack _deferred_writes;
3535
private int _referent;
3636
private long? _conformance_position;
@@ -190,7 +190,7 @@ public NdrMarshalBuffer(NdrDataRepresentation data_representation)
190190
{
191191
_stm = new MemoryStream();
192192
_writer = new BinaryWriter(_stm, Encoding.Unicode);
193-
_handles = new List<NtObject>();
193+
_handles = new List<NdrSystemHandle>();
194194
_referent = 0x20000;
195195
_deferred_writes = new NdrDeferralStack();
196196
NdrUnmarshalBuffer.CheckDataRepresentation(data_representation);
@@ -200,11 +200,11 @@ public NdrMarshalBuffer(NdrDataRepresentation data_representation)
200200
#endregion
201201

202202
#region Misc Methods
203-
public void WriteSystemHandle<T>(T handle) where T : NtObject
203+
public void WriteSystemHandle<T>(T handle, uint desired_access = 0) where T : NtObject
204204
{
205205
if (handle != null)
206206
{
207-
_handles.Add(handle);
207+
_handles.Add(new NdrSystemHandle(handle, desired_access));
208208
WriteInt32(_handles.Count);
209209
}
210210
else
@@ -1224,7 +1224,7 @@ public void WriteConformantVaryingArray<T>(T[] array, long conformance, long var
12241224

12251225
#region Public Properties
12261226

1227-
public List<NtObject> Handles => _handles;
1227+
public List<NdrSystemHandle> Handles => _handles;
12281228

12291229
public NdrDataRepresentation DataRepresentation { get; }
12301230

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright 2019 Google Inc. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
// NOTE: This file is a modified version of NdrParser.cs from OleViewDotNet
16+
// https://github.com/tyranid/oleviewdotnet. It's been relicensed from GPLv3 by
17+
// the original author James Forshaw to be used under the Apache License for this
18+
// project.
19+
20+
namespace NtApiDotNet.Ndr.Marshal
21+
{
22+
/// <summary>
23+
/// Structure to hold an NDR system handle.
24+
/// </summary>
25+
public readonly struct NdrSystemHandle
26+
{
27+
/// <summary>
28+
/// The object handle.
29+
/// </summary>
30+
public NtObject Handle { get; }
31+
32+
/// <summary>
33+
/// The desired access mask.
34+
/// </summary>
35+
public uint DesiredAccess { get; }
36+
37+
/// <summary>
38+
/// Constructor.
39+
/// </summary>
40+
/// <param name="handle">The object handle.</param>
41+
/// <param name="desired_access">The desired access mask.</param>
42+
public NdrSystemHandle(NtObject handle, uint desired_access)
43+
{
44+
Handle = handle;
45+
DesiredAccess = desired_access;
46+
}
47+
}
48+
}

NtApiDotNet/NtApiDotNet.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
<Compile Include="Ndr\IdlNdrFormatterInternal.cs" />
7777
<Compile Include="Ndr\Marshal\FLAGGED_WORD_BLOB.cs" />
7878
<Compile Include="Ndr\Marshal\NdrUserMarshal.cs" />
79+
<Compile Include="Ndr\Marshal\NdrSystemHandle.cs" />
7980
<Compile Include="Ndr\NdrUtils.cs" />
8081
<Compile Include="Net\Dns\DnsClient.cs" />
8182
<Compile Include="Net\Dns\DnsAddressRecord.cs" />

NtApiDotNet/Win32/Rpc/RpcClientBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ protected RpcClientBase(string interface_id, int major, int minor)
8686
/// <param name="handles">List of handles marshaled into the buffer.</param>
8787
/// <returns>Unmarshal NDR buffer for the result.</returns>
8888
protected RpcClientResponse SendReceive(int proc_num, NdrDataRepresentation data_representation,
89-
byte[] ndr_buffer, IReadOnlyCollection<NtObject> handles)
89+
byte[] ndr_buffer, IReadOnlyCollection<NdrSystemHandle> handles)
9090
{
9191
if (!Connected)
9292
{

NtApiDotNet/Win32/Rpc/RpcClientBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@ private RpcTypeDescriptor GetSystemHandleTypeDescriptor(NdrSystemHandleTypeRefer
428428
{
429429
return new RpcTypeDescriptor(system_handle_type.GetSystemHandleType(),
430430
nameof(NdrUnmarshalBuffer.ReadSystemHandle), marshal_helper, nameof(NdrMarshalBuffer.WriteSystemHandle), system_handle_type, null, null,
431-
new AdditionalArguments(true), new AdditionalArguments(true));
431+
new AdditionalArguments(true, CodeGenUtils.GetPrimitive(system_handle_type.AccessMask)), new AdditionalArguments(true));
432432
}
433433

434434
private RpcTypeDescriptor GetHandleTypeDescriptor(NdrHandleTypeReference handle_type, MarshalHelperBuilder marshal_helper)

NtApiDotNet/Win32/Rpc/Transport/IRpcClientTransport.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public interface IRpcClientTransport : IDisposable
4242
/// <param name="handles">List of handles marshaled into the buffer.</param>
4343
/// <returns>Client response from the send.</returns>
4444
RpcClientResponse SendReceive(int proc_num, Guid objuuid, NdrDataRepresentation data_representation,
45-
byte[] ndr_buffer, IReadOnlyCollection<NtObject> handles);
45+
byte[] ndr_buffer, IReadOnlyCollection<NdrSystemHandle> handles);
4646

4747
/// <summary>
4848
/// Add and authenticate a new security context.

NtApiDotNet/Win32/Rpc/Transport/RpcAlpcClientTransport.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
using NtApiDotNet.Ndr.Marshal;
1616
using System;
1717
using System.Collections.Generic;
18+
using System.Linq;
1819

1920
namespace NtApiDotNet.Win32.Rpc.Transport
2021
{
@@ -153,7 +154,7 @@ private void ClearAttributes(AlpcMessage msg, AlpcReceiveMessageAttributes attri
153154
_client.Send(AlpcMessageFlags.None, msg, attributes.ToContinuationAttributes(flags), NtWaitTimeout.Infinite);
154155
}
155156

156-
private RpcClientResponse SendAndReceiveLarge(int proc_num, Guid objuuid, byte[] ndr_buffer, IReadOnlyCollection<NtObject> handles)
157+
private RpcClientResponse SendAndReceiveLarge(int proc_num, Guid objuuid, byte[] ndr_buffer, IReadOnlyCollection<NdrSystemHandle> handles)
157158
{
158159
LRPC_LARGE_REQUEST_MESSAGE req_msg = new LRPC_LARGE_REQUEST_MESSAGE()
159160
{
@@ -177,7 +178,7 @@ private RpcClientResponse SendAndReceiveLarge(int proc_num, Guid objuuid, byte[]
177178

178179
if (handles.Count > 0)
179180
{
180-
send_attr.AddHandles(handles);
181+
send_attr.AddHandles(handles.Select(h => new AlpcHandleMessageAttributeEntry(h.Handle, h.DesiredAccess)));
181182
}
182183

183184
using (var port_section = _client.CreatePortSection(AlpcCreatePortSectionFlags.Secure, ndr_buffer.Length))
@@ -199,7 +200,7 @@ private RpcClientResponse SendAndReceiveLarge(int proc_num, Guid objuuid, byte[]
199200
}
200201
}
201202

202-
private RpcClientResponse SendAndReceiveImmediate(int proc_num, Guid objuuid, byte[] ndr_buffer, IReadOnlyCollection<NtObject> handles)
203+
private RpcClientResponse SendAndReceiveImmediate(int proc_num, Guid objuuid, byte[] ndr_buffer, IReadOnlyCollection<NdrSystemHandle> handles)
203204
{
204205
LRPC_IMMEDIATE_REQUEST_MESSAGE req_msg = new LRPC_IMMEDIATE_REQUEST_MESSAGE()
205206
{
@@ -221,7 +222,7 @@ private RpcClientResponse SendAndReceiveImmediate(int proc_num, Guid objuuid, by
221222

222223
if (handles.Count > 0)
223224
{
224-
send_attr.AddHandles(handles);
225+
send_attr.AddHandles(handles.Select(h => new AlpcHandleMessageAttributeEntry(h.Handle, h.DesiredAccess)));
225226
}
226227

227228
using (AlpcReceiveMessageAttributes recv_attr = new AlpcReceiveMessageAttributes())
@@ -329,7 +330,7 @@ public void Bind(Guid interface_id, Version interface_version, Guid transfer_syn
329330
/// <param name="handles">List of handles marshaled into the buffer.</param>
330331
/// <returns>Client response from the send.</returns>
331332
public RpcClientResponse SendReceive(int proc_num, Guid objuuid, NdrDataRepresentation data_representation,
332-
byte[] ndr_buffer, IReadOnlyCollection<NtObject> handles)
333+
byte[] ndr_buffer, IReadOnlyCollection<NdrSystemHandle> handles)
333334
{
334335
if (ndr_buffer.Length > 0xF00)
335336
{

NtApiDotNet/Win32/Rpc/Transport/RpcConnectedClientTransport.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -593,7 +593,7 @@ public RpcTransportSecurityContext AddSecurityContext(RpcTransportSecurity trans
593593
/// <param name="handles">List of handles marshaled into the buffer.</param>
594594
/// <returns>Client response from the send.</returns>
595595
public RpcClientResponse SendReceive(int proc_num, Guid objuuid, NdrDataRepresentation data_representation,
596-
byte[] ndr_buffer, IReadOnlyCollection<NtObject> handles)
596+
byte[] ndr_buffer, IReadOnlyCollection<NdrSystemHandle> handles)
597597
{
598598
NdrUnmarshalBuffer.CheckDataRepresentation(data_representation);
599599
return new RpcClientResponse(SendReceiveRequestPDU(proc_num, objuuid,

0 commit comments

Comments
 (0)