Skip to content

Commit 49eb7ab

Browse files
committed
Added Get-RpcServer from a PID.
1 parent d6c8e87 commit 49eb7ab

1 file changed

Lines changed: 44 additions & 23 deletions

File tree

NtObjectManager/RpcFunctions.ps1

Lines changed: 44 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,8 @@ Path to a serialized representation of the RPC servers.
211211
If private symbols available try and resolve the names of structures and parameters.
212212
.PARAMETER SymSrvFallback
213213
Specify to use a built-in fallback for symbol server resolving when using the system dbghelp DLL. You also need to specify a local cache directory in SymbolPath.
214+
.PARAMETER ProcessId
215+
Specify a process to extract the RPC servers from. This parses all the modules in a process for any available servers.
214216
.INPUTS
215217
string[] List of paths to DLLs.
216218
.OUTPUTS
@@ -240,27 +242,33 @@ Get the list of RPC servers from rpcss.dll, use symbol server fallback with c:\s
240242
function Get-RpcServer {
241243
[CmdletBinding(DefaultParameterSetName = "FromDll")]
242244
Param(
243-
[parameter(Mandatory = $true, Position = 0, ValueFromPipeline, ValueFromPipelineByPropertyName, ParameterSetName = "FromDll")]
245+
[parameter(Mandatory, Position = 0, ValueFromPipeline, ValueFromPipelineByPropertyName, ParameterSetName = "FromDll")]
244246
[alias("Path")]
245247
[string]$FullName,
248+
[parameter(Mandatory, ParameterSetName = "FromSerialized")]
249+
[string]$SerializedPath,
250+
[parameter(Mandatory, ParameterSetName = "FromProcessId")]
251+
[alias("pid")]
252+
[int]$ProcessId,
246253
[parameter(ParameterSetName = "FromDll")]
254+
[parameter(ParameterSetName = "FromProcessId")]
247255
[string]$DbgHelpPath,
248256
[parameter(ParameterSetName = "FromDll")]
257+
[parameter(ParameterSetName = "FromProcessId")]
249258
[string]$SymbolPath,
250259
[parameter(ParameterSetName = "FromDll")]
251-
[switch]$AsText,
252-
[parameter(ParameterSetName = "FromDll")]
253-
[switch]$RemoveComments,
254-
[parameter(ParameterSetName = "FromDll")]
255260
[switch]$ParseClients,
256261
[parameter(ParameterSetName = "FromDll")]
262+
[parameter(ParameterSetName = "FromProcessId")]
257263
[switch]$IgnoreSymbols,
258264
[parameter(ParameterSetName = "FromDll")]
265+
[parameter(ParameterSetName = "FromProcessId")]
259266
[switch]$ResolveStructureNames,
260267
[parameter(ParameterSetName = "FromDll")]
268+
[parameter(ParameterSetName = "FromProcessId")]
261269
[switch]$SymSrvFallback,
262-
[parameter(Mandatory = $true, ParameterSetName = "FromSerialized")]
263-
[string]$SerializedPath
270+
[switch]$AsText,
271+
[switch]$RemoveComments
264272
)
265273

266274
BEGIN {
@@ -291,28 +299,41 @@ function Get-RpcServer {
291299

292300
PROCESS {
293301
try {
294-
if ($PSCmdlet.ParameterSetName -eq "FromDll") {
295-
$FullName = Resolve-Path -LiteralPath $FullName -ErrorAction Stop
296-
Write-Progress -Activity "Parsing RPC Servers" -CurrentOperation "$FullName"
297-
$servers = [NtApiDotNet.Win32.RpcServer]::ParsePeFile($FullName, $DbgHelpPath, $SymbolPath, $ParserFlags)
298-
if ($AsText) {
299-
foreach ($server in $servers) {
300-
$text = $server.FormatAsText($RemoveComments)
301-
Write-Output $text
302+
$servers = switch($PSCmdlet.ParameterSetName) {
303+
"FromDll" {
304+
$FullName = Resolve-Path -LiteralPath $FullName -ErrorAction Stop
305+
Write-Progress -Activity "Parsing RPC Servers" -CurrentOperation "$FullName"
306+
[NtApiDotNet.Win32.RpcServer]::ParsePeFile($FullName, $DbgHelpPath, $SymbolPath, $ParserFlags)
307+
}
308+
"FromSerialized" {
309+
$FullName = Resolve-Path -LiteralPath $SerializedPath -ErrorAction Stop
310+
Use-NtObject($stm = [System.IO.File]::OpenRead($FullName)) {
311+
while ($stm.Position -lt $stm.Length) {
312+
[NtApiDotNet.Win32.RpcServer]::Deserialize($stm) | Write-Output
313+
}
302314
}
303315
}
304-
else {
305-
Write-Output $servers
316+
"FromProcessId" {
317+
$proc = Get-Process -PID $ProcessId
318+
if ($null -eq $proc.SafeHandle) {
319+
throw "Can't open process $ProcessId"
320+
}
321+
$proc.Modules |
322+
% {
323+
Get-RpcServer -FullName $_.FileName -DbgHelpPath $DbgHelpPath -SymbolPath $SymbolPath `
324+
-IgnoreSymbols:$IgnoreSymbols -ResolveStructureNames:$ResolveStructureNames -SymSrvFallback:$SymSrvFallback
325+
}
306326
}
307327
}
308-
else {
309-
$FullName = Resolve-Path -LiteralPath $SerializedPath -ErrorAction Stop
310-
Use-NtObject($stm = [System.IO.File]::OpenRead($FullName)) {
311-
while ($stm.Position -lt $stm.Length) {
312-
[NtApiDotNet.Win32.RpcServer]::Deserialize($stm) | Write-Output
313-
}
328+
if ($AsText) {
329+
foreach ($server in $servers) {
330+
$text = $server.FormatAsText($RemoveComments)
331+
Write-Output $text
314332
}
315333
}
334+
else {
335+
Write-Output $servers
336+
}
316337
}
317338
catch {
318339
Write-Error $_

0 commit comments

Comments
 (0)