-
Notifications
You must be signed in to change notification settings - Fork 3
/
Unprotect-CiscoPassword7.ps1
89 lines (76 loc) · 2.64 KB
/
Unprotect-CiscoPassword7.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
<#
.Synopsis
Decrypts Cisco "PASSWORD 7" passwords.
.DESCRIPTION
Takes Cisco Password 7 encrypted text from router and switch configs
and decrypts it to get the password back in plain text.
.EXAMPLE
PS C:\> Import-Module c:\downloads\Unprotect-CiscoPassword7.ps1
PS C:\> Unprotect-CiscoPassword7 '025756085F'
1234
.EXAMPLE
PS C:\> Unprotect-CiscoPassword7 -Password7Text '025756085F'
1234
.EXAMPLE
PS C:\> '025756085F' | Unprotect-CiscoPassword7
1234
.EXAMPLE
# Take the lines from your cisco config which contain "Password 7"
# and take the password from the end of them and decrypt them all.
PS C:\> Get-ChildItem C:\CiscoConfigs\ -Recurse |
Select-String -Pattern 'password 7' |
ForEach-Object { Unprotect-CiscoPassword7 $_.Line }
1234
letmein
secretPasswordHere
.. etc.
.INPUTS
A string representing an encrypted password
.OUTPUTS
A string containing the password in plain text form
#>
function Unprotect-CiscoPassword7 {
[CmdletBinding()]
[OutputType([string])]
Param
(
# Encrypted password
[Parameter(Mandatory=$true,
ValueFromPipeline=$true,
Position=0)]
[ValidateScript({
# Check length, starting pattern, and split out entire config line (if necessary).
if ($_ -match 'password 7')
{
$_ = (-split $_)[-1]
}
($_.Length % 2 -eq 0) -and ($_ -match '^[0-9][0-9]') -and (([int]$_.Substring(0, 2)) -le 15)
})]
[string]$Password7Text
)
Begin
{
# Same decryption key for everyone
$key = "dsfd;kfoA,.iyewrkldJKDHSUBsgvca69834ncxv9873254k;fg87"
}
Process
{
# Handle if the input is just the password, or the full config line
if ($Password7Text -match 'password 7')
{
$Password7Text = (-split $Password7Text)[-1]
}
# First two characters' value is the offset into the key where the decryption starts.
$seed = [int]$Password7Text.substring(0, 2)
# Take two characters at a time from the rest of the string
# convert them from hex to decimal, and XOR with the next key position
# (wrapping around the key if needed)
# convert the resulting values to characters
$plainTextBytes = [regex]::Matches($Password7Text.SubString(2), '..').Value |
ForEach-Object {
[char]([convert]::ToInt32($_, 16) -bxor $key[$seed++])
$seed = $seed % $key.Length
}
-join $plainTextBytes
}
}