Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Runtime.InteropServices;
- class Program
- {
- static void Main(string[] args)
- {
- foreach (var credential in EnumerateCrendentials())
- {
- Console.WriteLine(credential);
- }
- var abc = Console.ReadKey();
- }
- private static Credential ReadCredential(CREDENTIAL credential)
- {
- string applicationName = Marshal.PtrToStringUni(credential.TargetName);
- string userName = Marshal.PtrToStringUni(credential.UserName);
- string secret = null ;
- if (credential.CredentialBlob != IntPtr.Zero)
- {
- Console.Write("Here some passwords, dude :)n");
- secret = Marshal.PtrToStringUni(credential.CredentialBlob);
- }
- return new Credential(credential.Type, applicationName, userName, secret);
- }
- public static IReadOnlyList<Credential> EnumerateCrendentials()
- {
- List<Credential> result = new List<Credential>();
- int count;
- IntPtr pCredentials;
- bool ret = CredEnumerateW(null, 0, out count, out pCredentials);
- if (ret)
- {
- for (int n = 0; n < count; n++)
- {
- IntPtr credential = Marshal.ReadIntPtr(pCredentials, n * Marshal.SizeOf(typeof(IntPtr)));
- result.Add(ReadCredential((CREDENTIAL)Marshal.PtrToStructure(credential, typeof(CREDENTIAL))));
- }
- }
- else
- {
- int lastError = Marshal.GetLastWin32Error();
- throw new Win32Exception(lastError);
- }
- return result;
- }
- [DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
- static extern bool CredEnumerateW(string filter, int flag, out int count, out IntPtr pCredentials);
- [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
- private struct CREDENTIAL
- {
- public uint Flags;
- public CredentialType Type;
- public IntPtr TargetName;
- public IntPtr Comment;
- public System.Runtime.InteropServices.ComTypes.FILETIME LastWritten;
- public uint CredentialBlobSize;
- public IntPtr CredentialBlob;
- public uint Persist;
- public uint AttributeCount;
- public IntPtr Attributes;
- public IntPtr TargetAlias;
- public IntPtr UserName;
- }
- }
- public enum CredentialType
- {
- Generic = 1,
- DomainPassword,
- DomainCertificate,
- DomainVisiblePassword,
- GenericCertificate,
- DomainExtended,
- Maximum,
- MaximumEx = Maximum + 1000,
- }
- public class Credential
- {
- private readonly string _applicationName;
- private readonly string _userName;
- private readonly string _password;
- private readonly CredentialType _credentialType;
- public CredentialType CredentialType
- {
- get { return _credentialType; }
- }
- public string ApplicationName
- {
- get { return _applicationName; }
- }
- public string UserName
- {
- get { return _userName; }
- }
- public string Password
- {
- get { return _password; }
- }
- public Credential(CredentialType credentialType, string applicationName, string userName, string password)
- {
- _applicationName = applicationName;
- _userName = userName;
- _password = password;
- _credentialType = credentialType;
- }
- public override string ToString()
- {
- return string.Format("CredentialType: {0}nApplicationName: {1}nUserName: {2}nPassword: {3}nn", CredentialType, ApplicationName, UserName, Password);
- }
- }
- if (credential.CredentialBlob != IntPtr.Zero)
- {
- Console.Write("Here some passwords, dude :)n");
- secret = Marshal.PtrToStringUni(credential.CredentialBlob);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement