-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPasswd.ps1
More file actions
216 lines (178 loc) · 5.23 KB
/
Passwd.ps1
File metadata and controls
216 lines (178 loc) · 5.23 KB
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
<#
.Synopsis
Generates a pseudo-random ASCII string
.DESCRIPTION
By default excludes extended and control characters
.EXAMPLE
New-Password
Entropy Password Length
------- -------- ------
2.94770277922009 koC!;CmBD 9
.EXAMPLE
New-Password -Length 16 -MinEntropy 2
.EXAMPLE
New-Password -MinEntropy 7
Entropy Password Length
------- -------- ------
2.8073549220576 SH!*GI6 7
#>
function New-Password
{
[CmdletBinding()]
#[Alias(npwd)]
[OutputType([object])]
Param(# Param1 help description
[Parameter(Mandatory=$false,
Position=0)]
[int]
$Length,
# Param2 help description
[Parameter(Position=1)]
[ValidateRange(0,5)]
[double]
$MinEntropy = 0,
# Param3 help description
[Parameter(Position=2)]
[ValidateSet("ASCII","IncludeControl","IncludeExtended","IncludeCE")]
[String]
$CharSet = "ASCII"
)
Begin
{
function Get-Entropy
{
Param ([Parameter(Mandatory = $True)]
[ValidateNotNullOrEmpty()]
[Byte[]]
$Bytes
)
$FrequencyTable = @{}
foreach($Byte in $Bytes){
$FrequencyTable[$Byte]++
}
$Entropy = 0.0
foreach($Byte in 0..255){
$ByteProbability = ([Double]$FrequencyTable[[Byte]$Byte])/$Bytes.Length
if($ByteProbability -gt 0){
$Entropy += -$ByteProbability * [Math]::Log($ByteProbability, 2)
}
}
$Entropy
}
function Get-CryptoRand{
Param ([Parameter(Mandatory = $false)]
[ValidateNotNullOrEmpty()]
$Rolls = 20,
[Parameter(Mandatory = $false)]
[ValidateNotNullOrEmpty()]
$Bound= 32..126
)
Begin{
function Randomize-List{
Param([array]$InputList)
return $InputList | Get-Random -SetSeed ([Environment]::TickCount) -Count $InputList.Count;
}
function rand{
$bytes = new-object "System.Byte[]" $Rolls
$rnd = new-object System.Security.Cryptography.RNGCryptoServiceProvider
$rnd.GetBytes($bytes)
$bytes = $bytes | sort -Unique | Where-Object {$_ -in $Bound}
$bytes
}
}
Process{
$TMP = @()
while($TMP.count -le $Rolls){
$TMP += rand
Write-Verbose $TMP.Length
}
}
End{
$Out = @()
$out = Randomize-List -InputList $TMP
$Out = $Out |Select-Object -First $Rolls
$Out
}
}
switch ($CharSet){
"IncludeControl" {$Bound = 0..126}
"IncludeExtended" {$Bound = 32..254}
"IncludeCE" {$Bound = 0..254}
default {$Bound = 32..126}
}
#initilize array for storing chars for password
$PasswordString = @()
}
Process{
Write-Verbose "before 0 test length $Length"
if($Length -eq 0){$Length = Get-Random -SetSeed ([Environment]::TickCount) -Minimum 8 -Maximum 16}
#Sets loop count
$LoopCount = $Length * 10
Write-Verbose "after 0 test length $Length"
do{
#iterates
$LoopCount = $LoopCount - 1
#Gets pseudo random char, puts in PasswordString array #FIX
$PasswordASCIIDEC = (Get-CryptoRand -Bound $Bound -Rolls $Length)
foreach($ASCIIDec in $PasswordASCIIDEC){
Write-Verbose "ascii dec $ASCIIDec"
$PasswordString += [char]$ASCIIDec
}
#concatenates $PasswordString
$Passwd = $PasswordString -join ''
#Finds Entropy of passwd
$enc = [system.Text.Encoding]::UTF8
[double]$PasswdEntropy = Get-Entropy $enc.GetBytes($passwd)
Write-Verbose " passwd entropy $PasswdEntropy"
#If MinEntropy isn't set allows all possible values
if($MinEntropy -eq 0){$MinEntropy = $PasswdEntropy}
Write-Verbose " min entropy inside do loop $MinEntropy"
Write-Verbose "loop count $LoopCount"
}
until((($PasswdEntropy -ge $MinEntropy) -and ($Passwd.length -le $Length)) -or ($LoopCount -lt 0))
if($LoopCount -le 0){Write-Error "Unable to generate password of length $Length and entropy $MinEntropy, increase Length, or reduce complexity requirements"}
#convert to secure string
#TODO Add export to file
$SecureStringPassword = $Passwd | ConvertTo-SecureString -AsPlainText -Force
$Password = New-Object -TypeName psobject -Property @{
'Password'=$passwd;
'Length'=$passwd.length;
'Entropy'=$PasswdEntropy;
}
#Tests object, outputs to screen and clipboard
if($LoopCount -gt 0){$Password}
# $Password.password|clip
#Add-Type -AssemblyName System.speech
#$speak = New-Object System.Speech.Synthesis.SpeechSynthesizer
#$speak.Speak("$passwd")
#$myinvocation.mycommand.name
#$myinvocation.mycommand.path
#$myinvocation.MyCommand
#TODO set AD password?
#Say outloud?
}
End
{
$Length = $null
$MinEntropy = $null
$Bytes = $null
$FrequencyTable = $null
$Entropy = $null
$ByteProbability = $null
$Rolls = $null
$Bound = $null
$out = $null
$bytes = $null
$rnd = $null
$MAXOut = $null
$SelectRandomOut = $null
$MaxLength = $null
$PasswordString = $null
$LoopCount = $null
$charOut = $null
$Passwd = $null
$Password = $null
}
}
New-Password -Verbose