|
1 | | -// Copyright 2020 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 | | -using NtApiDotNet; |
16 | | -using NtApiDotNet.Win32; |
17 | | -using System; |
18 | | -using System.Collections.Generic; |
19 | | -using System.Linq; |
20 | | -using System.Management.Automation; |
21 | | - |
22 | | -namespace NtObjectManager.Cmdlets.Accessible |
23 | | -{ |
24 | | - /// <summary> |
25 | | - /// <para type="description">Access check result for an event trace.</para> |
26 | | - /// </summary> |
27 | | - public class EventTraceAccessCheckResult : CommonAccessCheckResult |
28 | | - { |
29 | | - /// <summary> |
30 | | - /// The ID of the event trace provider. |
31 | | - /// </summary> |
32 | | - public Guid Id => Provider.Id; |
33 | | - |
| 1 | +// Copyright 2020 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 | +using NtApiDotNet; |
| 16 | +using NtApiDotNet.Win32; |
| 17 | +using System; |
| 18 | +using System.Collections.Generic; |
| 19 | +using System.Linq; |
| 20 | +using System.Management.Automation; |
| 21 | + |
| 22 | +namespace NtObjectManager.Cmdlets.Accessible |
| 23 | +{ |
| 24 | + /// <summary> |
| 25 | + /// <para type="synopsis">Get a list of ETW providers accessible by a specified token.</para> |
| 26 | + /// <para type="description">This cmdlet checks all ETW providers and tries to determine |
| 27 | + /// if one or more specified tokens can access them. If no tokens are specified then the |
| 28 | + /// current process token is used.</para> |
| 29 | + /// </summary> |
| 30 | + /// <remarks>This will only work if run as an administrator.</remarks> |
| 31 | + /// <example> |
| 32 | + /// <code>Get-AccessibleEventTrace</code> |
| 33 | + /// <para>Check all accessible ETW providers for the current process token.</para> |
| 34 | + /// </example> |
| 35 | + /// <example> |
| 36 | + /// <code>Get-AccessibleEventTrace -ProcessIds 1234,5678</code> |
| 37 | + /// <para>>Check all accessible ETW providers for the process tokens of PIDs 1234 and 5678</para> |
| 38 | + /// </example> |
| 39 | + /// <example> |
| 40 | + /// <code>$token = Get-NtToken -Primary -Duplicate -IntegrityLevel Low
Get-AccessibleEventTrace -Tokens $token</code> |
| 41 | + /// <para>Get all ETW providers which can be accessed by a low integrity copy of current token.</para> |
| 42 | + /// </example> |
| 43 | + [Cmdlet(VerbsCommon.Get, "AccessibleEventTrace", DefaultParameterSetName = "All")] |
| 44 | + [OutputType(typeof(CommonAccessCheckResult))] |
| 45 | + public class GetAccessibleEventTraceCmdlet : CommonAccessBaseWithAccessCmdlet<TraceAccessRights> |
| 46 | + { |
34 | 47 | /// <summary> |
35 | | - /// The source of the event trace provider. |
36 | | - /// </summary> |
37 | | - public EventTraceProviderSource Source => Provider.Source; |
38 | | - |
| 48 | + /// <para type="description">Specify list of ETW provider GUID to check.</para> |
| 49 | + /// </summary> |
| 50 | + [Parameter(ParameterSetName = "FromId")] |
| 51 | + public Guid[] ProviderId { get; set; } |
| 52 | + |
39 | 53 | /// <summary> |
40 | | - /// The event trace provider. |
41 | | - /// </summary> |
42 | | - public EventTraceProvider Provider { get; } |
43 | | - |
44 | | - internal EventTraceAccessCheckResult(EventTraceProvider provider, |
45 | | - NtType type, AccessMask granted_access, |
46 | | - SecurityDescriptor sd, TokenInformation token_info) |
47 | | - : base(string.IsNullOrEmpty(provider.Name) ? provider.Id.ToString() : provider.Name, |
48 | | - type.Name, granted_access, |
49 | | - type.GenericMapping, sd, |
50 | | - type.AccessRightsType, false, token_info) |
51 | | - { |
52 | | - Provider = provider; |
53 | | - } |
54 | | - } |
55 | | - |
56 | | - /// <summary> |
57 | | - /// <para type="synopsis">Get a list of ETW providers accessible by a specified token.</para> |
58 | | - /// <para type="description">This cmdlet checks all ETW providers and tries to determine |
59 | | - /// if one or more specified tokens can access them. If no tokens are specified then the |
60 | | - /// current process token is used.</para> |
61 | | - /// </summary> |
62 | | - /// <remarks>This will only work if run as an administrator.</remarks> |
63 | | - /// <example> |
64 | | - /// <code>Get-AccessibleEventTrace</code> |
65 | | - /// <para>Check all accessible ETW providers for the current process token.</para> |
66 | | - /// </example> |
67 | | - /// <example> |
68 | | - /// <code>Get-AccessibleEventTrace -ProcessIds 1234,5678</code> |
69 | | - /// <para>>Check all accessible ETW providers for the process tokens of PIDs 1234 and 5678</para> |
70 | | - /// </example> |
71 | | - /// <example> |
72 | | - /// <code>$token = Get-NtToken -Primary -Duplicate -IntegrityLevel Low
Get-AccessibleEventTrace -Tokens $token</code> |
73 | | - /// <para>Get all ETW providers which can be accessed by a low integrity copy of current token.</para> |
74 | | - /// </example> |
75 | | - [Cmdlet(VerbsCommon.Get, "AccessibleEventTrace", DefaultParameterSetName = "All")] |
76 | | - [OutputType(typeof(CommonAccessCheckResult))] |
77 | | - public class GetAccessibleEventTraceCmdlet : CommonAccessBaseWithAccessCmdlet<TraceAccessRights> |
78 | | - { |
79 | | - /// <summary> |
80 | | - /// <para type="description">Specify list of ETW provider GUID to check.</para> |
81 | | - /// </summary> |
82 | | - [Parameter(ParameterSetName = "FromId")] |
83 | | - public Guid[] ProviderId { get; set; } |
84 | | - |
85 | | - /// <summary> |
86 | | - /// <para type="description">Specify list of ETW provider names to check.</para> |
87 | | - /// </summary> |
88 | | - [Parameter(ParameterSetName = "FromName")] |
89 | | - public string[] Name { get; set; } |
90 | | - |
91 | | - private protected override void RunAccessCheck(IEnumerable<TokenEntry> tokens) |
92 | | - { |
93 | | - NtType type = NtType.GetTypeByType<NtEtwRegistration>(); |
94 | | - AccessMask access_rights = type.GenericMapping.MapMask(Access); |
95 | | - var providers = EventTracing.GetProviders(); |
96 | | - |
97 | | - if (ProviderId != null && ProviderId.Length > 0) |
98 | | - { |
99 | | - HashSet<Guid> guids = new HashSet<Guid>(ProviderId); |
100 | | - providers = providers.Where(p => guids.Contains(p.Id)); |
101 | | - } |
102 | | - else if (Name != null && Name.Length > 0) |
103 | | - { |
104 | | - var names = new HashSet<string>(Name, StringComparer.OrdinalIgnoreCase); |
105 | | - providers = providers.Where(p => names.Contains(p.Name)); |
106 | | - } |
107 | | - |
108 | | - foreach (var provider in providers) |
109 | | - { |
110 | | - var sd = provider.SecurityDescriptor; |
111 | | - if (sd == null) |
112 | | - { |
113 | | - WriteWarning($"Couldn't query security for ETW Provider {provider.Name}. Perhaps run as administrator."); |
114 | | - continue; |
115 | | - } |
116 | | - |
117 | | - foreach (TokenEntry token in tokens) |
118 | | - { |
119 | | - AccessMask granted_access = NtSecurity.GetMaximumAccess(sd, |
120 | | - token.Token, type.GenericMapping); |
121 | | - if (IsAccessGranted(granted_access, access_rights)) |
122 | | - { |
123 | | - WriteObject(new EventTraceAccessCheckResult(provider, type, |
124 | | - granted_access, sd, token.Information)); |
125 | | - } |
126 | | - } |
127 | | - } |
128 | | - } |
129 | | - } |
130 | | -} |
| 54 | + /// <para type="description">Specify list of ETW provider names to check.</para> |
| 55 | + /// </summary> |
| 56 | + [Parameter(ParameterSetName = "FromName")] |
| 57 | + public string[] Name { get; set; } |
| 58 | + |
| 59 | + private protected override void RunAccessCheck(IEnumerable<TokenEntry> tokens) |
| 60 | + { |
| 61 | + NtType type = NtType.GetTypeByType<NtEtwRegistration>(); |
| 62 | + AccessMask access_rights = type.GenericMapping.MapMask(Access); |
| 63 | + var providers = EventTracing.GetProviders(); |
| 64 | + |
| 65 | + if (ProviderId != null && ProviderId.Length > 0) |
| 66 | + { |
| 67 | + HashSet<Guid> guids = new HashSet<Guid>(ProviderId); |
| 68 | + providers = providers.Where(p => guids.Contains(p.Id)); |
| 69 | + } |
| 70 | + else if (Name != null && Name.Length > 0) |
| 71 | + { |
| 72 | + var names = new HashSet<string>(Name, StringComparer.OrdinalIgnoreCase); |
| 73 | + providers = providers.Where(p => names.Contains(p.Name)); |
| 74 | + } |
| 75 | + |
| 76 | + foreach (var provider in providers) |
| 77 | + { |
| 78 | + var sd = provider.SecurityDescriptor; |
| 79 | + if (sd == null) |
| 80 | + { |
| 81 | + WriteWarning($"Couldn't query security for ETW Provider {provider.Name}. Perhaps run as administrator."); |
| 82 | + continue; |
| 83 | + } |
| 84 | + |
| 85 | + foreach (TokenEntry token in tokens) |
| 86 | + { |
| 87 | + AccessMask granted_access = NtSecurity.GetMaximumAccess(sd, |
| 88 | + token.Token, type.GenericMapping); |
| 89 | + if (IsAccessGranted(granted_access, access_rights)) |
| 90 | + { |
| 91 | + WriteObject(new EventTraceAccessCheckResult(provider, type, |
| 92 | + granted_access, sd, token.Information)); |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | +} |
0 commit comments