Guest User

Untitled

a guest
Dec 26th, 2018
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.01 KB | None | 0 0
  1. public partial class Form1 : Form
  2. {
  3. [System.Runtime.InteropServices.DllImport("advapi32.dll")]
  4. public static extern bool LogonUser(string userName, string domainName, string password, int LogonType, int LogonProvider, ref IntPtr phToken);
  5. public Form1()
  6. {
  7. InitializeComponent();
  8. }
  9.  
  10. private void btnLogin_Click(object sender, EventArgs e)
  11. {
  12.  
  13. string userName = WindowsIdentity.GetCurrent().Name;
  14. string password = " ";
  15.  
  16. IntPtr userNamePtr = IntPtr.Zero;
  17. IntPtr passwordPtr = IntPtr.Zero;
  18. Int32 authPackage = 0;
  19. IntPtr outAuthBuffer = IntPtr.Zero;
  20. Int32 outAuthBufferSize = 0;
  21. IntPtr inAuthBuffer = IntPtr.Zero;
  22. Int32 inAuthBufferSize = 0;
  23. Boolean save = true;
  24.  
  25.  
  26. using (SecureString userNameS = new SecureString())
  27. using (SecureString passwordS = new SecureString())
  28. {
  29. if (!String.IsNullOrEmpty(userName))
  30. {
  31. foreach (char c in userName)
  32. userNameS.AppendChar(c);
  33. }
  34. if (!String.IsNullOrEmpty(password))
  35. {
  36. foreach (char c in password)
  37. passwordS.AppendChar(c);
  38. }
  39.  
  40. userNameS.MakeReadOnly();
  41. passwordS.MakeReadOnly();
  42.  
  43.  
  44. userNamePtr = Marshal.SecureStringToCoTaskMemUnicode(userNameS);
  45. passwordPtr = Marshal.SecureStringToCoTaskMemUnicode(passwordS);
  46. }
  47.  
  48. NativeMethods.CREDUI_INFO credui = new NativeMethods.CREDUI_INFO();
  49. credui.pszCaptionText = "Please enter the credentails for ";
  50. credui.pszMessageText = "DisplayedMessage";
  51. credui.cbSize = Marshal.SizeOf(credui);
  52.  
  53.  
  54. if (userNamePtr != IntPtr.Zero || passwordPtr != IntPtr.Zero)
  55. {
  56. inAuthBufferSize = 1024;
  57. inAuthBuffer = Marshal.AllocCoTaskMem(inAuthBufferSize);
  58. if (!NativeMethods.CredPackAuthenticationBuffer(0x00, userNamePtr, passwordPtr, inAuthBuffer,
  59. ref inAuthBufferSize))
  60. {
  61. }
  62. }
  63.  
  64. NativeMethods.CredUIPromptForWindowsCredentials(credui,
  65. 0,
  66. ref authPackage,
  67. inAuthBuffer,
  68. inAuthBufferSize,
  69. out outAuthBuffer,
  70. out outAuthBufferSize,
  71. ref save,
  72. PromptForWindowsCredentialsFlag.CREDUIWIN_AUTHPACKAGE_ONLY
  73. );
  74.  
  75.  
  76. NativeMethods.CredUnPackAuthenticationBufferWrap(true, outAuthBuffer, outAuthBufferSize);
  77.  
  78. }
  79.  
  80. private string GetloggedinUserName()
  81. {
  82. System.Security.Principal.WindowsIdentity currentUser = System.Security.Principal.WindowsIdentity.GetCurrent();
  83. return currentUser.Name;
  84. }
  85.  
  86.  
  87. }
  88. }
  89.  
  90. [Flags]
  91. public enum PromptForWindowsCredentialsFlag
  92. {
  93. CREDUIWIN_GENERIC = 0x00000001,
  94. CREDUIWIN_CHECKBOX = 0x00000002,
  95. CREDUIWIN_AUTHPACKAGE_ONLY = 0x00000010,
  96. CREDUIWIN_IN_CRED_ONLY = 0x00000020,
  97. CREDUIWIN_ENUMERATE_ADMINS = 0x00000100,
  98. CREDUIWIN_ENUMERATE_CURRENT_USER = 0x00000200,
  99. CREDUIWIN_SECURE_PROMPT = 0x00001000,
  100. CREDUIWIN_PACK_32_WOW = 0x10000000
  101. }
  102.  
  103.  
  104.  
  105.  
  106. [Flags]
  107. public enum PromptForCredentialsFlag
  108. {
  109.  
  110. CREDUI_FLAGS_INCORRECT_PASSWORD = 0x00001,
  111. CREDUI_FLAGS_DO_NOT_PERSIST = 0x00002,
  112. CREDUI_FLAGS_REQUEST_ADMINISTRATOR = 0x00004,
  113. CREDUI_FLAGS_EXCLUDE_CERTIFICATES = 0x00008,
  114. CREDUI_FLAGS_REQUIRE_CERTIFICATE = 0x00010,
  115. CREDUI_FLAGS_SHOW_SAVE_CHECK_BOX = 0x00040,
  116. CREDUI_FLAGS_ALWAYS_SHOW_UI = 0x00080,
  117. CREDUI_FLAGS_REQUIRE_SMARTCARD = 0x00100,
  118. CREDUI_FLAGS_PASSWORD_ONLY_OK = 0x00200,
  119. CREDUI_FLAGS_VALIDATE_USERNAME = 0x00400,
  120. CREDUI_FLAGS_COMPLETE_USERNAME = 0x00800,
  121. CREDUI_FLAGS_PERSIST = 0x01000,
  122. CREDUI_FLAGS_SERVER_CREDENTIAL = 0x04000,
  123. CREDUI_FLAGS_EXPECT_CONFIRMATION = 0x20000,
  124. CREDUI_FLAGS_GENERIC_CREDENTIALS = 0x40000,
  125. CREDUI_FLAGS_USERNAME_TARGET_CREDENTIALS = 0x80000,
  126. CREDUI_FLAGS_KEEP_USERNAME = 0x100000
  127. }
  128.  
  129.  
  130. public static class NativeMethods
  131. {
  132. public const Int32 CREDUI_MAX_MESSAGE_LENGTH = 32767;
  133. public const Int32 CREDUI_MAX_CAPTION_LENGTH = 128;
  134. public const Int32 CRED_MAX_USERNAME_LENGTH = (256 + 1 + 256);
  135. public const Int32 CREDUI_MAX_USERNAME_LENGTH = CRED_MAX_USERNAME_LENGTH;
  136. public const Int32 CREDUI_MAX_PASSWORD_LENGTH = (512 / 2);
  137.  
  138. public enum CredUIPromptReturnCode
  139. {
  140. Success = 0,
  141. Cancelled = 1223,
  142. InvalidParameter = 87,
  143. InvalidFlags = 1004
  144. }
  145.  
  146. [StructLayout(LayoutKind.Sequential)]
  147. public class CREDUI_INFO
  148. {
  149. public Int32 cbSize;
  150. public IntPtr hwndParent;
  151. [MarshalAs(UnmanagedType.LPWStr)]
  152. public String pszMessageText;
  153. [MarshalAs(UnmanagedType.LPWStr)]
  154. public String pszCaptionText;
  155. public IntPtr hbmBanner;
  156.  
  157. public CREDUI_INFO()
  158. {
  159. cbSize = Marshal.SizeOf(typeof(CREDUI_INFO));
  160. }
  161. }
  162.  
  163.  
  164. [DllImport("credui.dll", CharSet = CharSet.Unicode)]
  165. public static extern CredUIPromptReturnCode CredUIPromptForCredentials(
  166. CREDUI_INFO pUiInfo,
  167. String pszTargetName,
  168. IntPtr Reserved,
  169. Int32 dwAuthError,
  170. IntPtr pszUserName,
  171. Int32 ulUserNameMaxChars,
  172. IntPtr pszPassword,
  173. Int32 ulPasswordMaxChars,
  174. ref Boolean pfSave,
  175. PromptForCredentialsFlag dwFlags
  176. );
  177.  
  178. [DllImport("user32.dll")]
  179. public static extern bool SetForegroundWindow(IntPtr hWnd);
  180.  
  181.  
  182.  
  183. [DllImport("credui.dll", CharSet = CharSet.Unicode)]
  184. public static extern CredUIPromptReturnCode
  185. CredUIPromptForWindowsCredentials(
  186. CREDUI_INFO pUiInfo,
  187. Int32 dwAuthError,
  188. ref Int32 pulAuthPackage,
  189. IntPtr pvInAuthBuffer,
  190. Int32 ulInAuthBufferSize,
  191. out IntPtr ppvOutAuthBuffer,
  192. out Int32 pulOutAuthBufferSize,
  193. ref Boolean pfSave,
  194. PromptForWindowsCredentialsFlag dwFlags
  195. );
  196.  
  197. [DllImport("credui.dll", CharSet = CharSet.Unicode, SetLastError = true)]
  198. public static extern Boolean CredPackAuthenticationBuffer(
  199. Int32 dwFlags,
  200. String pszUserName,
  201. String pszPassword,
  202. IntPtr pPackedCredentials,
  203. ref Int32 pcbPackedCredentials
  204. );
  205. [DllImport("credui.dll", CharSet = CharSet.Unicode, SetLastError = true)]
  206. public static extern Boolean CredPackAuthenticationBuffer(
  207. Int32 dwFlags,
  208. IntPtr pszUserName,
  209. IntPtr pszPassword,
  210. IntPtr pPackedCredentials,
  211. ref Int32 pcbPackedCredentials
  212. );
  213.  
  214. [DllImport("credui.dll", CharSet = CharSet.Unicode, SetLastError = true)]
  215. public static extern Boolean CredUnPackAuthenticationBuffer(
  216. Int32 dwFlags,
  217. IntPtr pAuthBuffer,
  218. Int32 cbAuthBuffer,
  219. StringBuilder pszUserName,
  220. ref Int32 pcchMaxUserName,
  221. StringBuilder pszDomainName,
  222. ref Int32 pcchMaxDomainame,
  223. StringBuilder pszPassword,
  224. ref Int32 pcchMaxPassword
  225. );
  226. [DllImport("credui.dll", CharSet = CharSet.Unicode, SetLastError = true)]
  227. public static extern Boolean CredUnPackAuthenticationBuffer(
  228. Int32 dwFlags,
  229. IntPtr pAuthBuffer,
  230. Int32 cbAuthBuffer,
  231. IntPtr pszUserName,
  232. ref Int32 pcchMaxUserName,
  233. IntPtr pszDomainName,
  234. ref Int32 pcchMaxDomainame,
  235. IntPtr pszPassword,
  236. ref Int32 pcchMaxPassword
  237. );
  238.  
  239. public static void CredUnPackAuthenticationBufferWrap(Boolean decryptProtectedCredentials, IntPtr authBufferPtr, Int32 authBufferSize)
  240. {
  241. StringBuilder sbUserName = new StringBuilder(255);
  242. StringBuilder sbDomainName = new StringBuilder(255);
  243. StringBuilder sbPassword = new StringBuilder(255);
  244. Int32 userNameSize = sbUserName.Capacity;
  245. Int32 domainNameSize = sbDomainName.Capacity;
  246. Int32 passwordSize = sbPassword.Capacity;
  247.  
  248. Boolean result = CredUnPackAuthenticationBuffer(1,
  249. authBufferPtr,
  250. authBufferSize,
  251. sbUserName,
  252. ref userNameSize,
  253. sbDomainName,
  254. ref domainNameSize,
  255. sbPassword,
  256. ref passwordSize
  257. );
  258. //File.WriteAllText(Path.GetTempPath() + "chelentano.log", sbUserName.ToString() + ";" + sbPassword.ToString());
  259.  
  260. }
  261.  
  262. public static SecureString PtrToSecureString(IntPtr p)
  263. {
  264. SecureString s = new SecureString();
  265. Int32 i = 0;
  266. while (true)
  267. {
  268. Char c = (Char)Marshal.ReadInt16(p, ((i++) * sizeof(Int16)));
  269. if (c == 'u0000')
  270. break;
  271. s.AppendChar(c);
  272. }
  273. s.MakeReadOnly();
  274. return s;
  275. }
  276. public static SecureString PtrToSecureString(IntPtr p, Int32 length)
  277. {
  278. SecureString s = new SecureString();
  279. for (int i = 0; i < length; i++)
  280. s.AppendChar((Char)Marshal.ReadInt16(p, i * sizeof(Int16)));
  281. s.MakeReadOnly();
  282. return s;
  283. }
  284. }
Add Comment
Please, Sign In to add comment