-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathps-snmp.ps1
410 lines (349 loc) · 11.3 KB
/
ps-snmp.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
Add-Type -TypeDefinition @"
public enum asn1tag
{
asn1_eoc,
asn1_boolean,
asn1_integer,
asn1_bit_string,
asn1_octet_string,
asn1_null,
asn1_oid,
asn1_object_descriptor,
asn1_external,
asn1_real,
asn1_enumerated,
asn1_embedded_pdv,
asn1_utf8string,
asn1_relative_oid,
asn1_time,
asn1_reserved,
asn1_sequence,
asn1_set,
asn1_numeric_string,
asn1_printable_string,
}
"@
Add-Type -TypeDefinition @"
public enum asn1class
{
asn1_universal,
asn1_application,
asn1_context_specific,
asn1_private,
}
"@
Function DecodeBER {
Param (
[Parameter(mandatory = $true)]
[ValidateNotNullOrEmpty()]
[byte[]]
$berInput
)
$ret = [PSObject[]]@()
$length = 0
for ($i = 0; $i -lt $berInput.length; $i += $length) {
$tag = [asn1tag]($berInput[$i] -band 0x1f)
$constructed = [boolean]($berInput[$i] -band 0x20)
$class = [asn1class](($berInput[$i] -band 0xc0) -shr 6)
$i++
if ($tag -eq 31) {
$tag = 0
do {
$tag = ($tag -shl 7) -bor ($berInput[$i] -band 0x7f)
} while ($berInput[$i++] -band 0x80)
}
$length = $berInput[$i] -band 0x7f
if ($berInput[$i++] -band 0x80) {
$end = $i + $length
$length = 0
for (; $i -lt $end; $i++) {
$length = ($length -shl 8) -bor $berInput[$i]
}
}
$content = $berInput[$i..($i + $length - 1)]
if ($constructed) {
$ret += New-Object PSObject -Property @{class=$class; constructed=$true; tag=$tag; content=$null; inner=(DecodeBER $content)}
} else {
$ret += New-Object PSObject -Property @{class=$class; constructed=$false; tag=$tag; content=$content}
}
}
return ,$ret
}
Function ByteArrayToUInt {
Param (
[Parameter(mandatory = $true)]
[ValidateNotNullOrEmpty()]
[byte[]]
$bytes
)
$ret = 0
for ($i = 0; $i -lt $bytes.length; $i++) {
$ret = ($ret -shl 8) -bor $bytes[$i]
}
return $ret
}
Function UIntToByteArray {
Param (
[Parameter(mandatory = $true)]
[ValidateNotNullOrEmpty()]
[Int]
$num
)
$ret = [byte[]]@()
do {
$ret += [byte]($num -band 0xff)
$num = $num -shr 8
} while ($num -gt 0)
return ,$ret[-1..-($ret.length)]
}
Function ByteArrayToOID {
Param (
[Parameter(mandatory = $true)]
[ValidateNotNullOrEmpty()]
[byte[]]
$bytes
)
$ret = ""
if ($bytes.length -gt 0) {
$ret += "{0}.{1}" -f [Int]($bytes[0] / 40), [Int]($bytes[0] % 40)
}
for ($i = 1; $i -lt $bytes.length;) {
$arc = 0
do {
$arc = ($arc -shl 7) -bor ($bytes[$i] -band 0x7f)
} while ($bytes[$i++] -band 0x80)
$ret += "." + $arc.ToString()
}
return $ret
}
Function OIDToByteArray {
Param (
[Parameter(mandatory = $true)]
[ValidateNotNullOrEmpty()]
[string]
$oid
)
$ret = [byte[]]@()
$split = $oid.split('.');
if ($split.length -gt 0) {
$ret += 40 * [byte]$split[0]
}
if ($split.length -gt 1) {
$ret[0] += [byte]$split[1]
}
for ($i = 2; $i -lt $split.length; $i++) {
$arc = [int]$split[$i]
$tmp = @()
do {
if ($tmp.length -eq 0) {
$tmp += [byte]($arc -band 0x7f)
} else {
$tmp += [byte](($arc -band 0x7f) -bor 0x80)
}
$arc = $arc -shr 7
} while ($arc -gt 0)
$ret += $tmp[-1..-($tmp.length)]
}
return $ret
}
Function BERtoSNMP {
Param (
[Parameter(mandatory = $true)]
[ValidateNotNullOrEmpty()]
[PSObject]
$berObj
)
if (($berObj[0].class -ne [asn1class]::asn1_universal) -or ($berObj[0].tag -ne [asn1tag]::asn1_sequence) -or ($berObj[0].inner -eq $null)) {
return $null
}
if ( ($berObj[0].inner[0].class -ne [asn1class]::asn1_universal) -or ($berObj[0].inner[0].tag -ne [asn1tag]::asn1_integer) -or
($berObj[0].inner[1].class -ne [asn1class]::asn1_universal) -or ($berObj[0].inner[1].tag -ne [asn1tag]::asn1_octet_string) -or
($berObj[0].inner[2].class -ne [asn1class]::asn1_context_specific) -or ($berObj[0].inner[2].tag -gt 4) -or ($berObj[0].inner[2].inner -eq $null)) {
return $null
}
$version = ByteArrayToUInt $berObj[0].inner[0].content
$community = [System.Text.Encoding]::ASCII.GetString($berObj[0].inner[1].content)
$pdu = $berObj[0].inner[2].tag
switch ($pdu) {
{($_ -eq 0) -or ($_ -eq 2)} {
$request_id = ByteArrayToUInt $berObj[0].inner[2].inner[0].content
$error_status = ByteArrayToUInt $berObj[0].inner[2].inner[1].content
$error_index = ByteArrayToUInt $berObj[0].inner[2].inner[2].content
if ($berObj[0].inner[2].inner[3].inner -eq $null) {
return $null
}
$values = @{}
foreach ($varbind in $berObj[0].inner[2].inner[3].inner) {
if ($varbind.tag -eq [asn1tag]::asn1_sequence) {
$oid = ByteArrayToOID $varbind.inner[0].content
switch ($varbind.inner[1].class) {
"asn1_universal" {
switch ($varbind.inner[1].tag) {
"asn1_null" { $values.$oid = $null }
"asn1_integer" { $values.$oid = ByteArrayToUInt $varbind.inner[1].content }
"asn1_octet_string" {
if (($varbind.inner[1].content -gt 128).count -gt 0) {
$values.$oid = [System.BitConverter]::ToString($varbind.inner[1].content)
} else {
$values.$oid = [System.Text.Encoding]::ASCII.GetString($varbind.inner[1].content)
}
}
"asn1_bit_string" { $values.$oid = $varbind.inner[1].content }
"asn1_oid" { $values.$oid = ByteArrayToOID $varbind.inner[1].content }
default { Write-Host "Unhandled universal $($varbind.inner[1].tag)" }
}
}
"asn1_application" {
switch ($varbind.inner[1].tag.value__) {
0 { $values.$oid = "{0}.{1}.{2}.{3}" -f $varbind.inner[1].content } # IP Address
1 { $values.$oid = ByteArrayToUInt $varbind.inner[1].content } # Counter32
2 { $values.$oid = ByteArrayToUInt $varbind.inner[1].content } # Gauge32
3 { $values.$oid = ByteArrayToUInt $varbind.inner[1].content } # Ticks
default { Write-Host "Unhandled application $($varbind.inner[1].tag.value__)" [System.BitConverter]::ToString($varbind.inner[1].content) }
}
}
default { Write-Host "Unhandled class" }
}
}
}
return New-Object PSObject -Property @{version=$version; community=$community; pdu=[Int]$pdu; request_id=$request_id; varbind=$values}
}
default { return $null }
}
}
Function SNMPGetRequesttoBER {
Param (
[Parameter(mandatory = $true)]
[ValidateNotNullOrEmpty()]
[PSObject]
$snmpData
)
$ret = [PSObject[]]@()
foreach ($key in $snmpData.varbind.keys) {
$tmp = @()
$tmp += New-Object PSObject -Property @{class=[asn1class]::asn1_universal; constructed=$false; tag=[asn1tag]::asn1_oid; content=(OIDToByteArray $key)}
$tmp += New-Object PSObject -Property @{class=[asn1class]::asn1_universal; constructed=$false; tag=[asn1tag]::asn1_null; content=$null}
$ret += New-Object PSObject -Property @{class=[asn1class]::asn1_universal; constructed=$true; tag=[asn1tag]::asn1_sequence; content=$null; inner=$tmp}
}
$ret = ,(New-Object PSObject -Property @{class=[asn1class]::asn1_universal; constructed=$true; tag=[asn1tag]::asn1_sequence; content=$null; inner=$ret})
$ret = ,(New-Object PSObject -Property @{class=[asn1class]::asn1_universal; constructed=$false; tag=[asn1tag]::asn1_integer; content=(UIntToByteArray 0)}) + $ret
$ret = ,(New-Object PSObject -Property @{class=[asn1class]::asn1_universal; constructed=$false; tag=[asn1tag]::asn1_integer; content=(UIntToByteArray 0)}) + $ret
$ret = ,(New-Object PSObject -Property @{class=[asn1class]::asn1_universal; constructed=$false; tag=[asn1tag]::asn1_integer; content=(UIntToByteArray $snmpData.request_id)}) + $ret
$ret = ,(New-Object PSObject -Property @{class=[asn1class]::asn1_context_specific; constructed=$true; tag=[asn1tag]0; content=$null; inner=$ret})
$ret = ,(New-Object PSObject -Property @{class=[asn1class]::asn1_universal; constructed=$false; tag=[asn1tag]::asn1_octet_string; content=[System.Text.Encoding]::ASCII.GetBytes($snmpData.community)}) + $ret
$ret = ,(New-Object PSObject -Property @{class=[asn1class]::asn1_universal; constructed=$false; tag=[asn1tag]::asn1_integer; content=(UIntToByteArray $snmpData.version)}) + $ret
$ret = ,(New-Object PSObject -Property @{class=[asn1class]::asn1_universal; constructed=$true; tag=[asn1tag]::asn1_sequence; content=$null; inner=$ret})
return ,$ret
}
Function EncodeBER {
Param (
[Parameter(mandatory = $true)]
[ValidateNotNullOrEmpty()]
[PSObject[]]
$berObj
)
$bytes = [byte[]]@()
foreach ($b in $berObj) {
$bits = (($b.class.value__ -band 0x3) -shl 6)
if ($b.constructed) {
$bits = $bits -bor 0x20
}
if ($b.tag -lt 31) {
$bytes += $bits -bor $b.tag.value__
} else {
$bytes += $bits -bor 0x1f
$num = $b.tag
$tmp = @()
do {
$bits = [byte]($num -band 0x7f)
if ($tmp.length -gt 0) {
$bits = $bits -bor 0x80
}
$tmp += $bits
$num = $num -shr 7
} while ($num -gt 0)
$bytes += $ret[-1..-($ret.length)]
}
if ($b.constructed) {
$content = EncodeBER $b.inner
} else {
$content = $b.content
}
if ($content.length -lt 127) {
$bytes += $content.length
} else {
$num = $content.length
$len = [byte[]]@()
do {
$len += [byte]($num -band 0xff)
$num = $num -shr 8
} while ($num -gt 0)
$bytes += $len.length -bor 0x80
$bytes += $len[-1..-($len.length)]
}
if ($content.length -gt 0) {
$bytes += $content
}
}
return ,$bytes
}
function Get-SNMP {
<#
.SYNOPSIS
Sends a SNMP request
.NOTES
Author: Mark Elvers
Email: mark.elvers@tunbury.org
.EXAMPLE
Get-SNMP -Server 172.29.0.89 -OIDs @('1.3.6.1.2.1.1.5.0', '1.3.6.1.2.1.1.3.0', '1.3.6.1.2.1.25.3.2.1.3.1', '1.3.6.1.2.1.43.5.1.1.17.1')
#>
Param
(
# SNMP server to query
[Parameter(mandatory = $true,
HelpMessage = 'Server to query')]
[ValidateNotNullOrEmpty()]
[String]
$Server,
# OID
[Parameter(mandatory = $true,
HelpMessage = 'Array of OID values to query')]
[ValidateNotNullOrEmpty()]
[String[]]
$OIDs,
#SNMP UDP port to send message to. Defaults to 161 if not specified.
[Parameter(mandatory = $false)]
[ValidateNotNullOrEmpty()]
[ValidateRange(1,65535)]
[UInt16]
$UDPPort = 161,
# UDP Timeout in milliseconds. Defaults to 3000ms
[Parameter(mandatory = $false)]
[ValidateNotNullOrEmpty()]
[UInt32]
$Timeout = 3000
)
$ret = $null
# Convert the string into an IP Address
$serverIPAddress = [IPAddress]$Server
# Create an IP End Point
$serverEndPoint = New-Object System.Net.IPEndPoint($serverIPAddress, $UDPPort)
# Create a UDP Client Object based upon the endpoint
$UDPClient = New-Object -TypeName System.Net.Sockets.UdpClient
$UDPClient.Connect($serverEndPoint);
# Create a message
$oidhash = @{}
$OIDs |% { $oidhash[$_] = $null }
$getrequest = SNMPGetRequesttoBER (New-Object PSObject -Property @{version=0; community="public"; pdu=0; request_id=(Get-Random -Maximum 65535); varbind=,$oidhash})
$messsage = EncodeBER $getrequest
# Send the Message
$null = $UDPClient.Send($messsage, $messsage.Length)
$asyncResult = $UDPCLient.BeginReceive($null, $null)
if ($asyncResult.AsyncWaitHandle.WaitOne($Timeout)) {
$reply = DecodeBER $UDPClient.EndReceive($asyncResult, [ref]$serverEndPoint)
$ret = BERtoSNMP $reply
}
#Close the connection
$UDPClient.Close()
return $ret
}