-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathportScan.ps1
198 lines (164 loc) · 5.82 KB
/
portScan.ps1
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
<#
.SYNOPSIS
[Incomplete script!] This is a template for new scripts
.DESCRIPTION
[Incomplete script!] This is a template for new scripts
.EXAMPLE
C:\PS>.\template.ps1
This will bring up the new script
.INPUTS
There are no inputs that can be directed to this script
.OUTPUTS
All outputs are sent to the console and logged in the log folder
.NOTES
Author: Robert Weber
Date: Nov 19, 2015
#>
[CmdletBinding()]
param (
$hostCsvPath = "",
$computers = @(),
$OU = "",
[switch]$filter
)
clear
$error.clear()
. .\lib\PSClass.ps1
(gci .\lib) | % { . "$($_.FullName)" }
$netStatClass = new-PSClass netStat{
note -static PsScriptName "netStatScanner"
note -static Description ( ($(((get-help .\template.ps1).Description)) | select Text).Text)
note -private mainProgressBar
note -private gui
note -private hostObj
note -private export $ExportClass.New()
note -private results @()
constructor{
param()
$private.HostObj = $HostsClass.New()
}
method Execute{
param($par)
$hostProgressBar = $progressBarClass.New( @{ "parentId" = 1; "Activity" = "0 / 0 :"; "Status" = ("0% complete" ); "PercentComplete" = 0; "Completed" = $false; "id" = 2 }).Render()
$sql = "select id, port, purpose from commonPort where port = :port"
$properties = @('T/U','L.Add','L.Port','L.Purpose','R.Add','R.Port','R.Purpose','State','Name','PID','User','System')
$currentComputer = 0
foreach($comp in ($private.HostObj.Hosts.keys | sort)){
$currentComputer++
$i = (100*($currentComputer / @(1,$private.HostObj.Hosts.count)[($private.HostObj.Hosts.count -gt 0)]))
$hostProgressBar.Activity("$currentComputer / $($private.HostObj.Hosts.count): Scanning system $comp").Status(("{0:N2}% complete" -f $i)).Percent($i).Render()
$uiClass.writeColor("$($uiClass.STAT_WAIT) Scanning system #green#$($comp)#")
$Result = Get-WmiObject -Class win32_pingstatus -Filter "address='$($comp.Trim())'"
if( ($Result.StatusCode -eq 0 -or $Result.StatusCode -eq $null ) -and $utilities.isBlank($Result.IPV4Address) -eq $false ) {
$processes = @{}
gwmi win32_process -computerName $comp | % {
if( (($_ | gm -memberType Method | select -expand Name ) -contains 'getOwner') -eq $true ){
if($_.handle -ne $null){
try{
$processes[ $_.handle ] = $_.getOwner().user
}catch{
write-host "$($_.handle) could not be resolved to a user"
}
}
}
}
if( ((hostname) -eq $comp)){
$netstat = invoke-command -scriptBlock { $d = & netstat -ano ; return $d }
}else{
$netstat = invoke-command -scriptBlock { $d = & netstat -ano ; return $d } -computerName $comp
}
($netStat -split "`r`n") | Select-String -Pattern '\s+(TCP|UDP)' | ForEach-Object {
$item = $_.line.split(" ",[System.StringSplitOptions]::RemoveEmptyEntries)
if($item ){
if($item[1] -notmatch '^\[::') {
if (($la = $item[1] -as [ipaddress]).AddressFamily -eq 'InterNetworkV6') {
$localAddress = $la.IPAddressToString
$localPort = $item[1].split('\]:')[-1]
} else {
$localAddress = $item[1].split(':')[0]
$localPort = $item[1].split(':')[-1]
}
if (($ra = $item[2] -as [ipaddress]).AddressFamily -eq 'InterNetworkV6') {
$remoteAddress = $ra.IPAddressToString
$remotePort = $item[2].split('\]:')[-1]
} else {
$remoteAddress = $item[2].split(':')[0]
$remotePort = $item[2].split(':')[-1]
}
if( ($filter -eq $false) -or ( $filter -eq $true -and [int]$localPort -le 1024 ) ){
$purpose = $dbClass.Get().query($sql, @{'port' = $localPort ;}).execAssoc();
if( ($utilities.isBlank($purpose) -eq $false) ){
$lpurpose = $purpose.purpose;
}else{
$lpurpose = "";
}
$purpose = $dbClass.Get().query($sql, @{'port' = $remotePort ;}).execAssoc();
if( ($utilities.isBlank($purpose) -eq $false) ){
$rpurpose = $purpose.purpose;
}else{
$rpurpose = "";
}
$r = New-Object PSObject -Property @{
System = $comp
PID = $item[-1]
Name = (Get-Process -Id $item[-1] -ErrorAction SilentlyContinue).Name
User = ($processes[ $($item[-1] ) ] )
'T/U' = $item[0]
'L.Add' = $localAddress
'L.Port' = $localPort
'L.Purpose' = $lpurpose
'R.Add' = $remoteAddress
'R.Port' = $remotePort
'R.Purpose' = $rpurpose
State = if($item[0] -eq 'tcp') {$item[3]} else {$null}
} | Select-Object -Property $properties
$private.results += $r
}
}
}
}
}else{
$uiClass.writeColor("$($uiClass.STAT_ERR) System #green#$($comp)# is #red#offline#")
}
}
$hostProgressBar.Completed($true).Render()
$private.exports()
$uiClass.errorLog()
}
method -private exports{
$private.export.addWorkSheet('Active Ports')
$colHeaders = @("System","TCP/UDP","Local Address","Local Port","Local Purpose","Remote Address","Remote Port","Remote Purpose","State","Process ID","Process Name","Runas User")
$sheetRow = 1
$col = 1
$colHeaders | %{
$private.export.updateCell(1,$col,$_)
$col = $col + 1
}
$private.results | %{
$sheetRow++
$private.export.addRow(
$sheetRow,
@(
$_.system,
$_.'T/U',
$_.'L.Add',
$_.'L.Port',
$_.'L.Purpose',
$_.'R.Add',
$_.'R.Port',
$_.'R.Purpose',
$_.'State',
$_.'PID',
$_.'Name',
$_.'User'
)
)
}
$private.export.autoFilterWorksheet()
$private.export.autofitAllColumns()
$private.export.formatAllFirstRows('Header')
$ts = (get-date -format "yyyyMMddHHmmss")
$private.export.saveAs([System.IO.Path]::GetFullPath("$($pwd.ProviderPath)\results\$($netStatClass.PsScriptName)_$ts.xml"))
}
}
$netStatClass.New().Execute() | out-null