Skip to content

Commit 370fc4d

Browse files
committed
Inherit process handle to TokenViewer.
1 parent 5db2b0f commit 370fc4d

2 files changed

Lines changed: 18 additions & 18 deletions

File tree

NtObjectManager/NtObjectManager.psm1

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2159,20 +2159,19 @@ function Set-ExecutionAlias
21592159
function Start-NtTokenViewer {
21602160
param(
21612161
[Parameter(Mandatory=$true, Position=0)]
2162-
[NtApiDotNet.NtToken]$Token,
2162+
[NtApiDotNet.NtObject]$Handle,
21632163
[string]$Text
21642164
)
21652165

2166-
Use-NtObject($dup_token = $Token.Duplicate()) {
2167-
$dup_token.Inherit = $true
2168-
$cmdline = [string]::Format("TokenViewer --handle={0}", $dup_token.Handle.DangerousGetHandle())
2166+
Use-NtObject($dup_handle = $Handle.Duplicate()) {
2167+
$dup_handle.Inherit = $true
2168+
$cmdline = [string]::Format("TokenViewer --handle={0}", $dup_handle.Handle.DangerousGetHandle())
21692169
if ($Text -ne "") {
21702170
$cmdline += " ""--text=$Text"""
21712171
}
21722172
$config = New-Win32ProcessConfig $cmdline -ApplicationName "$PSScriptRoot\TokenViewer.exe" -InheritHandles
2173-
$config.InheritHandleList.Add($dup_token.Handle.DangerousGetHandle())
2174-
Use-NtObject($p = New-Win32Process -Config $config) {
2175-
}
2173+
$config.InheritHandleList.Add($dup_handle.Handle.DangerousGetHandle())
2174+
Use-NtObject($p = New-Win32Process -Config $config) {}
21762175
}
21772176
}
21782177

@@ -2255,10 +2254,8 @@ function Show-NtToken {
22552254
}
22562255
switch($PSCmdlet.ParameterSetName) {
22572256
"FromProcess" {
2258-
Use-NtObject($t = Get-NtToken -Primary -Process $Process) {
2259-
$text = "$($Process.Name):$($Process.ProcessId)"
2260-
Start-NtTokenViewer $t -Text $text
2261-
}
2257+
$text = "$($Process.Name):$($Process.ProcessId)"
2258+
Start-NtTokenViewer $Process -Text $text
22622259
}
22632260
"FromName" {
22642261
Use-NtObject($ps = Get-NtProcess -Name $Name -Access QueryLimitedInformation) {

TokenViewer/Program.cs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -116,22 +116,25 @@ static Form GetFormFromArgs(string[] args)
116116
}
117117
else if (handle > 0)
118118
{
119-
using (NtToken token = NtToken.FromHandle(new SafeKernelObjectHandle(new IntPtr(handle), true)))
119+
using (var obj = NtObjectUtils.FromHandle(new IntPtr(handle), true))
120120
{
121-
if (token.NtType != NtType.GetTypeByType<NtToken>())
121+
if (obj is NtToken token)
122122
{
123-
throw new ArgumentException("Passed handle is not a token");
123+
return new TokenForm(token.Duplicate(), text);
124124
}
125-
126-
return new TokenForm(token.Duplicate(), text);
125+
else if (obj is NtProcess process)
126+
{
127+
return new TokenForm(new ProcessTokenEntry(process), text, false);
128+
}
129+
throw new ArgumentException("Passed handle is not a token or process.");
127130
}
128131
}
129132
else if (pid > 0)
130133
{
131134
using (NtProcess process = NtProcess.Open(pid, ProcessAccessRights.QueryLimitedInformation))
132135
{
133-
return new TokenForm(process.OpenToken(),
134-
$"{process.Name}:{pid}");
136+
return new TokenForm(new ProcessTokenEntry(process),
137+
$"{process.Name}:{pid}", false);
135138
}
136139
}
137140
}

0 commit comments

Comments
 (0)