-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDecode-LPVault.ps1
148 lines (137 loc) · 4.36 KB
/
Decode-LPVault.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
Param (
[Parameter(Mandatory = $false)][string]$vault,
[Parameter(Mandatory = $false)][switch]$textOut,
[Parameter(Mandatory = $false)][switch]$objOut
)
# Make sure the default is the original action
if (-Not ($textOut -or $objOut)) {
$textOut = $true
}
Function Get-OpenFileName {
param([string]$initialDirectory, [string]$Filter)
[System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") | Out-Null
$dialog = New-Object System.Windows.Forms.OpenFileDialog
$dialog.initialDirectory = $initialDirectory
$dialog.filter = $Filter
$dialog.ShowDialog() | Out-Null
$dialog.filename
}
if (-Not $in) {
if (-Not $vault) {
$vault = Get-OpenFileName -Filter "XML (*.xml)| *.xml"
}
[XML]$vaultxml = Get-Content $vault
}
function Get-decodedHex {
Param(
[string]$string
)
$nonASCII = "[^\x20-\x7E]"
$ret = $null
if ($string -and $string.Length -gt 1) {
$code = $string.Substring(0, 2)
try {
$c = [char]([convert]::toInt16($code, 16))
}
catch {
throw "Not a Hex ASCII Character"
}
if ($c -cmatch $nonASCII) {
throw "Non-ASCII Characters"
}
$ret = [string]($c + (Get-DecodedHex -string $string.Substring(2)))
}
$ret
}
function Get-DecodedLPObj {
Param (
[System.Xml.XmlElement]$node
)
$nodeObj = New-Object -TypeName PSObject -Property @{
NodeName = $node.ToString()
}
$node.Attributes.GetEnumerator() | Foreach-Object {
if ($_.Value) {
$nodeObj | Add-Member -MemberType NoteProperty -Name "$($_.Name)" -Value $_.Value
try {
$attrib = Get-DecodedHex($_.Value)
}
catch {
$attrib = $null
}
if ($attrib) {
$nodeObj | Add-Member -MemberType NoteProperty -Name "$($_.Name)Decoded" -Value $attrib
}
}
}
if ($node.HasChildNodes) {
$nodeNames = $node.ChildNodes | Foreach-Object { $_.ToString() } | Sort-Object -Unique
foreach ($name in $nodeNames) {
$ChildNodesList = $node.GetElementsByTagName($name)
if ($ChildNodesList.count -gt 1) {
$nodeObj | Add-Member -NotePropertyMembers @{ $name = @() }
$ChildNodesList.GetEnumerator() | Foreach-Object {
$nodeObj.$name += Get-DecodedLPObj -node $_
}
}
else {
$ChildObj = Get-DecodedLPObj -node $ChildNodesList.Item(0)
$nodeObj | Add-Member -NotePropertyMembers @{ $name = $childObj }
}
}
}
$nodeObj
}
function Get-NodesAsText {
Param (
[PSObject]$node,
[int]$lvl
)
$tabs = "`t" * $lvl
$nodeStr = "$($tabs)NODE: $($node.ToString())`n"
$node.Attributes.GetEnumerator() | Foreach-Object {
if ($_.Value) {
$nodeStr += "$($tabs)`t$($_.Name): $($_.Value)`n"
try {
$attrib = Get-DecodedHex($_.Value)
}
catch {
$attrib = $null
}
if ($attrib) {
$nodeStr += "$($tabs)`t$($_.Name) (Decoded): $($attrib)`n"
}
}
}
$node.ChildNodes | Foreach-Object {
$nodeStr += Get-NodesAsText -node $_ -lvl ($lvl + 1)
}
$nodeStr
}
if ($objOut) {
$vaultObj = New-Object -Typename PSObject -Property @{
NodeName = 'Vault'
}
$nodeNames = $vaultxml.response.ChildNodes | Foreach-Object { $_.ToString() } | Sort-Object -Unique
foreach ($name in $nodeNames) {
$ChildNodesList = $vaultxml.response.GetElementsByTagName($name)
if ($ChildNodesList.count -gt 1) {
$vaultObj | Add-Member -NotePropertyMembers @{ $name = @() }
$ChildNodesList.GetEnumerator() | Foreach-Object {
$vaultObj.$name += Get-DecodedLPObj -node $_
}
}
else {
$ChildObj = Get-DecodedLPObj -node $ChildNodesList.Item(0)
$vaultObj | Add-Member -NotePropertyMembers @{ $name = $childObj }
}
}
}
$VaultAsString = Get-NodesAsText -node $vaultxml.response -lvl 0
if ($ObjOut) {
$vaultObj | Add-Member -NotePropertyName "VaultAsString" -NotePropertyValue $vaultAsString
$vaultObj
}
else {
$VaultAsString
}