Advertisement
Guest User

Untitled

a guest
Sep 11th, 2016
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.56 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4.  
  5. using System.Runtime.InteropServices; // DllImport
  6. using System.Security.Principal; // WindowsImpersonationContext
  7. using System.Security.Permissions; // PermissionSetAttribute
  8.  
  9.  
  10. namespace Inpersonation
  11. {
  12.     class Inpersonater
  13.     {
  14.         // Get Token
  15.         [DllImport("advapi32.dll", SetLastError = true)]
  16.         public static extern bool LogonUser(string pszUsername, string pszDomain, string pszPassword,
  17.             int dwLogonType, int dwLogonProvider, ref IntPtr phToken);
  18.  
  19.         // Return from context
  20.         [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
  21.         public extern static bool CloseHandle(IntPtr handle);
  22.  
  23.         public static void DoWorkUnderImpersonation()
  24.         {
  25.             WindowsImpersonationContext impersonationContext = null;
  26.             IntPtr userHandle = IntPtr.Zero;
  27.             const int LOGON32_PROVIDER_DEFAULT = 0;
  28.             const int LOGON32_LOGON_INTERACTIVE = 2;
  29.             Console.Write("Domain: ");
  30.             string domain = Console.ReadLine();
  31.             Console.Write("Login: ");
  32.             string user = Console.ReadLine();
  33.             Console.Write("Password: ");
  34.             string password = "";
  35.             ConsoleKeyInfo key;
  36.             do
  37.             {
  38.                 key = Console.ReadKey(true);
  39.  
  40.                 if (key.Key != ConsoleKey.Backspace && key.Key != ConsoleKey.Enter)
  41.                 {
  42.                     password += key.KeyChar;
  43.                     Console.Write("*");
  44.                 }
  45.                 else
  46.                 {
  47.                     if (key.Key == ConsoleKey.Backspace && password.Length > 0)
  48.                     {
  49.                         password = password.Substring(0, (password.Length - 1));
  50.                         Console.Write("\b \b");
  51.                     }
  52.                 }
  53.             }
  54.             // Stops Receving Keys Once Enter is Pressed
  55.             while (key.Key != ConsoleKey.Enter);
  56.             Console.WriteLine("");
  57.             try
  58.             {
  59.                 if (domain == "")
  60.                     domain = System.Environment.MachineName;
  61.  
  62.                 bool loggedOn = LogonUser(user,
  63.                                             domain,
  64.                                             password,
  65.                                             LOGON32_LOGON_INTERACTIVE,
  66.                                             LOGON32_PROVIDER_DEFAULT,
  67.                                             ref userHandle);
  68.  
  69.                 if (!loggedOn)
  70.                 {
  71.                     Console.WriteLine("Exception impersonating user, error code: " + Marshal.GetLastWin32Error());
  72.                     return;
  73.                 }
  74.  
  75.                 impersonationContext = WindowsIdentity.Impersonate(userHandle);
  76.  
  77.                 //(WindowsIdentity.GetCurrent().Name);
  78.  
  79.                 string text = System.IO.File.ReadAllText(@"C:\secret.txt");
  80.                 Console.WriteLine(text);
  81.  
  82.             }
  83.             catch (Exception ex)
  84.             {
  85.                 Console.WriteLine("Exception impersonating user: " + ex.Message);
  86.             }
  87.             finally
  88.             {
  89.                 if (impersonationContext != null)
  90.                 {
  91.                     impersonationContext.Undo();
  92.                 }
  93.  
  94.                 if (userHandle != IntPtr.Zero)
  95.                 {
  96.                     CloseHandle(userHandle);
  97.                 }
  98.             }
  99.         }
  100.  
  101.         public static void Main()
  102.         {
  103.             DoWorkUnderImpersonation();
  104.         }
  105.     }
  106.  
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement