Advertisement
Guest User

Untitled

a guest
Oct 11th, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.69 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Runtime.InteropServices;
  5. using Microsoft.Win32.SafeHandles;
  6. using System.Windows.Forms;
  7.  
  8. namespace WindowsFormsApp3
  9. {
  10. public partial class Form1 : Form
  11. {
  12. public Form1()
  13. {
  14. InitializeComponent();
  15.  
  16. txtBody.Text = EnumerateCrendentials();
  17. }
  18.  
  19. [DllImport("Advapi32.dll", EntryPoint = "CredReadW", CharSet = CharSet.Unicode, SetLastError = true)]
  20. static extern bool CredRead(string target, CredentialType type, int reservedFlag, out IntPtr credentialPtr);
  21.  
  22. [DllImport("advapi32", SetLastError = true, CharSet = CharSet.Unicode)]
  23. static extern bool CredEnumerate(string filter, int flag, out int count, out IntPtr pCredentials);
  24.  
  25. [DllImport("Advapi32.dll", EntryPoint = "CredFree", SetLastError = true)]
  26. static extern bool CredFree([In] IntPtr cred);
  27.  
  28. [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
  29. private struct CREDENTIAL
  30. {
  31. public UInt32 Flags;
  32. public CredentialType Type;
  33. public IntPtr TargetName;
  34. public IntPtr Comment;
  35. public System.Runtime.InteropServices.ComTypes.FILETIME LastWritten;
  36. public UInt32 CredentialBlobSize;
  37. public IntPtr CredentialBlob;
  38. public UInt32 Persist;
  39. public UInt32 AttributeCount;
  40. public IntPtr Attributes;
  41. public IntPtr TargetAlias;
  42. public IntPtr UserName;
  43. }
  44.  
  45. public enum CredentialType
  46. {
  47. Generic = 1,
  48. DomainPassword,
  49. DomainCertificate,
  50. DomainVisiblePassword,
  51. GenericCertificate,
  52. DomainExtended,
  53. Maximum,
  54. MaximumEx = Maximum + 1000,
  55. }
  56.  
  57. public static string EnumerateCrendentials()
  58. {
  59. string output = "";
  60.  
  61. int count;
  62. IntPtr pCredentials;
  63. bool ret = CredEnumerate(null, 0, out count, out pCredentials);
  64. if (ret)
  65. {
  66. for (int n = 0; n < count; n++)
  67. {
  68. IntPtr credentialI = Marshal.ReadIntPtr(pCredentials, n * Marshal.SizeOf(typeof(IntPtr)));
  69. CREDENTIAL credential = (CREDENTIAL) Marshal.PtrToStructure(credentialI, typeof(CREDENTIAL));
  70.  
  71. string applicationName = Marshal.PtrToStringUni(credential.TargetName);
  72. string userName = Marshal.PtrToStringUni(credential.UserName);
  73. string secret = null;
  74. if (credential.CredentialBlob != IntPtr.Zero)
  75. {
  76. secret = Marshal.PtrToStringUni(credential.CredentialBlob, (int)credential.CredentialBlobSize / 2);
  77. }
  78.  
  79. output += string.Format("CredentialType: {0}, ApplicationName: {1}, UserName: {2}, Password: {3}", credential.Type, applicationName, userName, secret) + "\r\n";
  80. }
  81. }
  82. else
  83. {
  84. int lastError = Marshal.GetLastWin32Error();
  85. throw new Win32Exception(lastError);
  86. }
  87.  
  88. return output;
  89. }
  90. }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement