Advertisement
Guest User

Untitled

a guest
Nov 17th, 2016
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.70 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Runtime.InteropServices;
  5.  
  6. class Program
  7. {
  8. static void Main(string[] args)
  9. {
  10. foreach (var credential in EnumerateCrendentials())
  11. {
  12. Console.WriteLine(credential);
  13. }
  14. var abc = Console.ReadKey();
  15. }
  16.  
  17. private static Credential ReadCredential(CREDENTIAL credential)
  18. {
  19. string applicationName = Marshal.PtrToStringUni(credential.TargetName);
  20. string userName = Marshal.PtrToStringUni(credential.UserName);
  21. string secret = null ;
  22. if (credential.CredentialBlob != IntPtr.Zero)
  23. {
  24. Console.Write("Here some passwords, dude :)n");
  25. secret = Marshal.PtrToStringUni(credential.CredentialBlob);
  26. }
  27.  
  28. return new Credential(credential.Type, applicationName, userName, secret);
  29. }
  30.  
  31. public static IReadOnlyList<Credential> EnumerateCrendentials()
  32. {
  33. List<Credential> result = new List<Credential>();
  34.  
  35. int count;
  36. IntPtr pCredentials;
  37. bool ret = CredEnumerateW(null, 0, out count, out pCredentials);
  38. if (ret)
  39. {
  40. for (int n = 0; n < count; n++)
  41. {
  42. IntPtr credential = Marshal.ReadIntPtr(pCredentials, n * Marshal.SizeOf(typeof(IntPtr)));
  43. result.Add(ReadCredential((CREDENTIAL)Marshal.PtrToStructure(credential, typeof(CREDENTIAL))));
  44. }
  45. }
  46. else
  47. {
  48. int lastError = Marshal.GetLastWin32Error();
  49. throw new Win32Exception(lastError);
  50. }
  51.  
  52. return result;
  53. }
  54.  
  55. [DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
  56. static extern bool CredEnumerateW(string filter, int flag, out int count, out IntPtr pCredentials);
  57.  
  58. [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
  59. private struct CREDENTIAL
  60. {
  61. public uint Flags;
  62. public CredentialType Type;
  63. public IntPtr TargetName;
  64. public IntPtr Comment;
  65. public System.Runtime.InteropServices.ComTypes.FILETIME LastWritten;
  66. public uint CredentialBlobSize;
  67. public IntPtr CredentialBlob;
  68. public uint Persist;
  69. public uint AttributeCount;
  70. public IntPtr Attributes;
  71. public IntPtr TargetAlias;
  72. public IntPtr UserName;
  73. }
  74. }
  75.  
  76. public enum CredentialType
  77. {
  78. Generic = 1,
  79. DomainPassword,
  80. DomainCertificate,
  81. DomainVisiblePassword,
  82. GenericCertificate,
  83. DomainExtended,
  84. Maximum,
  85. MaximumEx = Maximum + 1000,
  86. }
  87.  
  88. public class Credential
  89. {
  90. private readonly string _applicationName;
  91. private readonly string _userName;
  92. private readonly string _password;
  93. private readonly CredentialType _credentialType;
  94.  
  95. public CredentialType CredentialType
  96. {
  97. get { return _credentialType; }
  98. }
  99.  
  100. public string ApplicationName
  101. {
  102. get { return _applicationName; }
  103. }
  104.  
  105. public string UserName
  106. {
  107. get { return _userName; }
  108. }
  109.  
  110. public string Password
  111. {
  112. get { return _password; }
  113. }
  114.  
  115. public Credential(CredentialType credentialType, string applicationName, string userName, string password)
  116. {
  117. _applicationName = applicationName;
  118. _userName = userName;
  119. _password = password;
  120. _credentialType = credentialType;
  121. }
  122.  
  123. public override string ToString()
  124. {
  125. return string.Format("CredentialType: {0}nApplicationName: {1}nUserName: {2}nPassword: {3}nn", CredentialType, ApplicationName, UserName, Password);
  126. }
  127. }
  128.  
  129. if (credential.CredentialBlob != IntPtr.Zero)
  130. {
  131. Console.Write("Here some passwords, dude :)n");
  132. secret = Marshal.PtrToStringUni(credential.CredentialBlob);
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement