CredentialManagement.psm1

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
[HashTable]$ErrorCategory = @{0x80070057 = "InvalidArgument"
0x800703EC = "InvalidData"
0x80070490 = "ObjectNotFound"
0x80070520 = "SecurityError"
0x8007089A = "SecurityError"}

Function Add-StoredCredentials {
    <#
    .SYNOPSIS
    Adds credentials to the Windows Credential Manager
    .DESCRIPTION
    Adds credentials to the users Windows Credential Manager
    .PARAMETER UserName
    User name for the credential
    .PARAMETER Password
    The password for the credential, if not specified a secure entry box will promp for the password
    .PARAMETER Target
    Target of the credentials to add, defaults to the UserName
    .PARAMETER Type
    The type of credential to be stored, Defualts to GENERIC
    Valid values are: "GENERIC", "DOMAIN_PASSWORD", "DOMAIN_CERTIFICATE",
    "DOMAIN_VISIBLE_PASSWORD", "GENERIC_CERTIFICATE", "DOMAIN_EXTENDED",
    "MAXIMUM", "MAXIMUM_EX"
    more information on credentail types can be found at this link
    https://docs.microsoft.com/en-us/windows/win32/api/wincred/ns-wincred-credentiala
    .EXAMPLE
    Add-StoredCreentials -UserName SamDoe -Password $Password1! -Target https://my.account.com
    Stores the user name and password as a generic entry for https://my.account.com
    .EXAMPLE
    Add-StoredCreentials -UserName SamDoe -Target https://my.account.com
    Will prompt for the password then stores the user name and password as a generic entry for https://my.account.com
    .NOTES
    Copyright (C) MosaicMK Software LLC - All Rights Reserved
    Unauthorized copying of this application via any medium is strictly prohibited Proprietary and confidential
    Written by MosaicMK Software LLC (contact@mosaicmk.com)
 
    By using this software you agree to the following:
    Agreement Permission is hereby granted, free of charge, to any person or organization obtaining a copy of this software and associated documentation files (the 'Software'),
    to deal in the Software and the rights to use and distribute the software so long a no licensing and or documentation files are remove, revised or modified
    the Software is furnished to do so, subject to the following conditions:
 
    THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
    DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
    IN THE SOFTWARE.
    Contact: Contact@MosaicMK.com
    Version 1.0.1
    .LINK
    https://www.mosaicmk.com/2019/07/credential-management-module.html
    #>

    PARAM(
        [Parameter(Mandatory=$True)]
        [String]$UserName,
        [string]$Password,
        [string]$Target,
        [ValidateSet("GENERIC","DOMAIN_PASSWORD","DOMAIN_CERTIFICATE","DOMAIN_VISIBLE_PASSWORD","GENERIC_CERTIFICATE","DOMAIN_EXTENDED","MAXIMUM","MAXIMUM_EX")]
        [String]$Type = "GENERIC",
        [String]$Comment
    )
    if(!($Target)){$Target = $UserName}
    #CRED_MAX_DOMAIN_TARGET_NAME_LENGTH
    if($Type -ne "GENERIC" -and $Target.Length -gt 337){Throw "Length of target name is longer then 337 characters"}
    if(!($Comment)){$Comment = "Last edited by $Env:UserDomain\$Env:UserName on $Env:ComputerName"}
    [PsUtils.CredMan+Credential]$Cred = New-Object PsUtils.CredMan+Credential
    switch($Target -eq $UserName -and ($Type -eq "CRED_TYPE_DOMAIN_PASSWORD" -or $Type -eq "CRED_TYPE_DOMAIN_CERTIFICATE"))
    {
        $true  {$Cred.Flags = [PsUtils.CredMan+CRED_FLAGS]::USERNAME_TARGET}
        $false  {$Cred.Flags = [PsUtils.CredMan+CRED_FLAGS]::NONE}
    }

    IF (!($Password)){
        $Crd = Get-Credential -UserName $UserName -Message "Enter a User Name and Password"
        $BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($Crd.Password)
        $Password = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)
    }

    $Cred.Type = $Type
    $Cred.TargetName = $Target
    $Cred.UserName = $UserName
    $Cred.AttributeCount = 0
    $Cred.Persist = "ENTERPRISE"
    $Cred.CredentialBlobSize = [Text.Encoding]::Unicode.GetBytes($Password).Length
    $Cred.CredentialBlob = $Password
    $Cred.Comment = $Comment
    $Password = $null
    try{$Results = [PsUtils.CredMan]::CredWrite($Cred)}catch{Throw $_}
    if($Results){Throw "Failed to write to credentials store for $Target"}
}

Function Remove-StoredCredentials {
    <#
    .SYNOPSIS
    Removes credentials stored in the windows credential manager
    .DESCRIPTION
    Removes credentaials from the users credential manager
    .PARAMETER Target
    Target of the credentials to remove, if no target was specified in the create command the target will be the username
    .PARAMETER Type
    The type of credential to be removed, Defualts to GENERIC
    Valid values are: "GENERIC", "DOMAIN_PASSWORD", "DOMAIN_CERTIFICATE",
    "DOMAIN_VISIBLE_PASSWORD", "GENERIC_CERTIFICATE", "DOMAIN_EXTENDED",
    "MAXIMUM", "MAXIMUM_EX"
    more information on credentail types can be found at this link
    https://docs.microsoft.com/en-us/windows/win32/api/wincred/ns-wincred-credentiala
    .EXAMPLE
    Remove-StoredCredentials -Target https://my.account.com
    .NOTES
    Copyright (C) MosaicMK Software LLC - All Rights Reserved
    Unauthorized copying of this application via any medium is strictly prohibited Proprietary and confidential
    Written by MosaicMK Software LLC (contact@mosaicmk.com)
 
    By using this software you agree to the following:
    Agreement Permission is hereby granted, free of charge, to any person or organization obtaining a copy of this software and associated documentation files (the 'Software'),
    to deal in the Software and the rights to use and distribute the software so long a no licensing and or documentation files are remove, revised or modified
    the Software is furnished to do so, subject to the following conditions:
 
    THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
    DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
    IN THE SOFTWARE.
    Contact: Contact@MosaicMK.com
    Version 1.0.0
    .LINK
    https://www.mosaicmk.com/2019/07/credential-management-module.html
    #>

    Param(
        [Parameter(Mandatory=$true)]
        [String]$Target,
        [ValidateSet("GENERIC","DOMAIN_PASSWORD","DOMAIN_CERTIFICATE","DOMAIN_VISIBLE_PASSWORD","GENERIC_CERTIFICATE","DOMAIN_EXTENDED","MAXIMUM","MAXIMUM_EX")]
        [String]$Type = "GENERIC"
    )
    try{$Results = [PsUtils.CredMan]::CredDelete($Target, $Type)}catch{Throw $_}
    if($Results){Throw "Failed to delete credentials store for $Target"}
}


function Get-StoredCredentials {
    <#
    .SYNOPSIS
    Gets the specified credentails stored in Credential Manager
    .DESCRIPTION
    Gets the specified credentials stored in Credential Manager as a PSCredential object.
    .PARAMETER Target
    Target of the credentials to get, if no target was specified in the create command the target will be the username
    .PARAMETER Type
    The type of credential to be removed, Defualts to GENERIC
    Valid values are: "GENERIC", "DOMAIN_PASSWORD", "DOMAIN_CERTIFICATE",
    "DOMAIN_VISIBLE_PASSWORD", "GENERIC_CERTIFICATE", "DOMAIN_EXTENDED",
    "MAXIMUM", "MAXIMUM_EX"
    more information on credentail types can be found at this link
    https://docs.microsoft.com/en-us/windows/win32/api/wincred/ns-wincred-credentiala
    .EXAMPLE
    $Creds = Get-StoredCredentials -Target https://my.account.com
    Gets the credentials for https://my.account.com and stores them in the $Creds variable
 
    The credenatials can then be used in a command the accepts a PSCredential object like
    New-PSSession -ComputerName MyComputer -Credential $Creds
    .NOTES
    Copyright (C) MosaicMK Software LLC - All Rights Reserved
    Unauthorized copying of this application via any medium is strictly prohibited Proprietary and confidential
    Written by MosaicMK Software LLC (contact@mosaicmk.com)
 
    By using this software you agree to the following:
    Agreement Permission is hereby granted, free of charge, to any person or organization obtaining a copy of this software and associated documentation files (the 'Software'),
    to deal in the Software and the rights to use and distribute the software so long a no licensing and or documentation files are remove, revised or modified
    the Software is furnished to do so, subject to the following conditions:
 
    THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
    DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
    IN THE SOFTWARE.
    Contact: Contact@MosaicMK.com
    Version 1.0.0
    .LINK
    https://www.mosaicmk.com/2019/07/credential-management-module.html
    #>

    Param(
        [Parameter(Mandatory=$true)]
        [String]$Target,
        [ValidateSet("GENERIC","DOMAIN_PASSWORD","DOMAIN_CERTIFICATE","DOMAIN_VISIBLE_PASSWORD","GENERIC_CERTIFICATE","DOMAIN_EXTENDED","MAXIMUM","MAXIMUM_EX")]
        [String]$Type = "GENERIC"
    )

    [PsUtils.CredMan+Credential] $Cred = New-Object PsUtils.CredMan+Credential
    try{$Results = [PsUtils.CredMan]::CredRead($Target, $Type, [ref]$Cred)}catch{Throw $_}

    IF ($Results -eq 0){
        $username = $cred.UserName
        $SecurePassword = $cred.CredentialBlob | ConvertTo-SecureString -AsPlainText -Force
        New-Object System.Management.Automation.PSCredential $UserName, $SecurePassword
    } else {Throw "Error reading credentials for $Target from credentials store : $Results"}
}

function Get-ClearTextStoredCredentials {
    <#
    .SYNOPSIS
    Gets the specified credentails stored in Credential Manager
    .DESCRIPTION
    Gets the specified credentails stored in the users Credential Manager and stores them as clear text in a varbale
    so they can be used by other commands that do not accept a PSCredential object
    .PARAMETER Target
    Target of the credentials to get, if no target was specified in the create command the target will be the username
    .PARAMETER Type
    The type of credential to be removed, Defualts to GENERIC
    Valid values are: "GENERIC", "DOMAIN_PASSWORD", "DOMAIN_CERTIFICATE",
    "DOMAIN_VISIBLE_PASSWORD", "GENERIC_CERTIFICATE", "DOMAIN_EXTENDED",
    "MAXIMUM", "MAXIMUM_EX"
    more information on credentail types can be found at this link
    https://docs.microsoft.com/en-us/windows/win32/api/wincred/ns-wincred-credentiala
    .EXAMPLE
    $Creds = Get-ClearTextStoredCredentials -Target https://my.account.com
    Gets the credentials for https://my.account.com and stores them in the $Creds variable
 
    The credentials can then be used in commands the do not accept PSCredential object like
    Register-ScheduledTask -User $creds.UserName -Password $Creds.Password
    .NOTES
    Copyright (C) MosaicMK Software LLC - All Rights Reserved
    Unauthorized copying of this application via any medium is strictly prohibited Proprietary and confidential
    Written by MosaicMK Software LLC (contact@mosaicmk.com)
 
    By using this software you agree to the following:
    Agreement Permission is hereby granted, free of charge, to any person or organization obtaining a copy of this software and associated documentation files (the 'Software'),
    to deal in the Software and the rights to use and distribute the software so long a no licensing and or documentation files are remove, revised or modified
    the Software is furnished to do so, subject to the following conditions:
 
    THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
    DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
    IN THE SOFTWARE.
    Contact: Contact@MosaicMK.com
    Version 1.0.0
    .LINK
    https://www.mosaicmk.com/2019/07/credential-management-module.html
    #>

    Param(
        [Parameter(Mandatory=$true)]
        [String]$Target,
        [ValidateSet("GENERIC","DOMAIN_PASSWORD","DOMAIN_CERTIFICATE","DOMAIN_VISIBLE_PASSWORD","GENERIC_CERTIFICATE","DOMAIN_EXTENDED","MAXIMUM","MAXIMUM_EX")]
        [String]$Type = "GENERIC"
    )

    [PsUtils.CredMan+Credential] $Cred = New-Object PsUtils.CredMan+Credential
    try{$Results = [PsUtils.CredMan]::CredRead($Target, $Type, [ref]$Cred)}catch{Throw $_}
    IF ($Results -eq 0){
        $Credentials = New-Object -TypeName psobject
        $Credentials | Add-Member -MemberType NoteProperty -Name UserName -Value $cred.UserName
        $Credentials | Add-Member -MemberType NoteProperty -Name Password -Value $cred.CredentialBlob
        $Credentials | Add-Member -MemberType NoteProperty -Name LastModify -Value $cred.LastWritten
        $Credentials | Add-Member -MemberType NoteProperty -Name Type -Value $cred.Type
        $Credentials | Add-Member -MemberType NoteProperty -Name Persist -Value $cred.Persist
        $Credentials | Add-Member -MemberType NoteProperty -Name Comment -Value $cred.Comment
        $Credentials
    } else {Throw "Error reading credentials for $Target from credentials store : $Results"}
}