-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathGet-THR_Handles.psm1
156 lines (115 loc) · 5.27 KB
/
Get-THR_Handles.psm1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
function Get-THR_Handles {
<#
.SYNOPSIS
Gets a list of Handles loaded by all process on a given system.
.DESCRIPTION
Gets a list of Handles loaded by all process on a given system.
.PARAMETER Computer
Computer can be a single hostname, FQDN, or IP address.
.PARAMETER HandlePath
The location of Sysinternals Handle.exe/Handle64.exe. This parameter is manadatory
and is how the function gets the list of handles.
.EXAMPLE
Get-THR_Handles -HandlePath c:\tools\sysinternals
Get-THR_Handles SomeHostName.domain.com -HandlePath "\\server\share\sysinternals"
Get-Content C:\hosts.csv | Get-THR_Handles -HandlePath "\\server\share\sysinternals"
Get-THR_Handles $env:computername -HandlePath "\\server\share\sysinternals"
Get-ADComputer -filter * | Select -ExpandProperty Name | Get-THR_Handles -HandlePath "\\server\share\sysinternals"
.NOTES
Updated: 2018-08-05
Contributing Authors:
Jeremy Arnold
Anthony Phipps
LEGAL: Copyright (C) 2018
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
.LINK
https://github.com/TonyPhipps/THRecon
https://docs.microsoft.com/en-us/sysinternals/downloads/
#>
param(
[Parameter(ValueFromPipeline=$True, ValueFromPipelineByPropertyName=$True)]
$Computer = $env:COMPUTERNAME,
[Parameter(HelpMessage="The folder path of Sysinternals Handle.exe, not including trailing backslash (\).")]
[string]$HandlePath = "C:\Users\$env:UserName\Documents\WindowsPowerShell\Modules\THRecon\Utilities"
)
begin{
$DateScanned = Get-Date -Format u
Write-Information -InformationAction Continue -MessageData ("Started {0} at {1}" -f $MyInvocation.MyCommand.Name, $DateScanned)
$stopwatch = New-Object System.Diagnostics.Stopwatch
$stopwatch.Start()
$total = 0
class Handle
{
[String] $Computer
[string] $DateScanned
[string] $ProcessID
[string] $Process
[string] $Owner
[string] $Location
[string] $HandleType
[string] $Attributes
[string] $String
}
}
process{
$Computer = $Computer.Replace('"', '') # get rid of quotes, if present
$handles = $null
$handles = Invoke-Command -ArgumentList $HandlePath -ComputerName $Computer -ErrorAction SilentlyContinue -ScriptBlock {
$HandlePath = $args[0]
$Is64BitOperatingSystem = [environment]::Is64BitOperatingSystem
if ($Is64BitOperatingSystem){
$tool = 'handle64.exe'
}
else {
$tool = 'handle.exe'
}
$handles = Invoke-Expression "$HandlePath\$tool -a -nobanner -accepteula"
return $handles
}
if ($handles) {
[regex]$regexProcess = '(?<process>\S+)\spid:\s(?<pid>\d+)\s(?<string>.*)'
[regex]$regexHandle = '(?<location>[A-F0-9]+):\s(?<type>\w+)\s{2}(?<attributes>\(.*\))?\s+(?<string>.*)'
[regex]$nullHandle = '([A-F0-9]+):\s(\w+)\s+$'
$handles = $handles | Where-Object {($_.length -gt 0) -and ($_ -notmatch $nullHandle)}
$outputArray = Foreach ($handle in $handles) {
if (($handle -match $regexHandle) -or ($handle -match $regexProcess)){
$output = $null
$output = [Handle]::new()
$output.Computer = $Computer
$output.DateScanned = Get-Date -Format o
$output.ProcessID = $processPID
$output.Process = $process
$output.Owner =$owner
$output.Location = $Matches.location
$output.HandleType = $Matches.type
$output.Attributes = $Matches.attributes
$output.String = $Matches.string
$output
}
}
$total++
return $outputArray
}
else {
$output = $null
$output = [Handle]::new()
$output.Computer = $Computer
$output.DateScanned = Get-Date -Format o
$total++
return $output
}
}
end{
$elapsed = $stopwatch.Elapsed
Write-Verbose ("Total Systems: {0} `t Total time elapsed: {1}" -f $total, $elapsed)
}
}