Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Reading remote windows registry via C#
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Runtime.InteropServices;
- using System.Security.Principal;
- using System.Security.Permissions;
- namespace WindowsFormsApplication1
- {
- class ImpersonateClass
- {
- [DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
- public static extern bool LogonUser(String lpszUsername, String lpszDomain, String lpszPassword,
- int dwLogonType, int dwLogonProvider, ref IntPtr phToken);
- [PermissionSetAttribute(SecurityAction.Demand, Name = "FullTrust")]
- [DllImport("kernel32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto)]
- private unsafe static extern int FormatMessage(int dwFlags, ref IntPtr lpSource,
- int dwMessageId, int dwLanguageId, ref String lpBuffer, int nSize, IntPtr* Arguments);
- [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
- public extern static bool CloseHandle(IntPtr handle);
- [DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
- public extern static bool DuplicateToken(IntPtr ExistingTokenHandle,
- int SECURITY_IMPERSONATION_LEVEL, ref IntPtr DuplicateTokenHandle);
- const int LOGON32_PROVIDER_DEFAULT = 0;
- const int LOGON32_LOGON_INTERACTIVE = 2;
- const int LOGON32_LOGON_NEW_CREDENTIALS = 9;
- private string userName;
- private string computerName;
- private string password;
- private IntPtr tokenHandle = IntPtr.Zero;
- public ImpersonateClass(string aUserName, string aComputerName, string aPassword)
- {
- userName = aUserName;
- computerName = aComputerName;
- password = aPassword;
- }
- public WindowsImpersonationContext impersonateUser()
- {
- if (LogonUser(userName, computerName, password, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, ref tokenHandle))
- {
- Console.WriteLine("Value of Windows NT token: " + tokenHandle);
- WindowsIdentity newId = new WindowsIdentity(tokenHandle);
- return newId.Impersonate();
- }
- else
- {
- return null;
- }
- }
- public void unimpersonateUser(ref WindowsImpersonationContext anImpersonatedUser)
- {
- if (anImpersonatedUser != null)
- {
- anImpersonatedUser.Undo();
- }
- }
- }
- }
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Security.Principal;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- namespace WindowsFormsApplication1
- {
- public partial class MainForm : Form
- {
- public MainForm()
- {
- InitializeComponent();
- }
- private void button_Click(object sender, EventArgs e)
- {
- ImpersonateClass impersonate = new ImpersonateClass(@"admministrator", "localMachine", "2.71828");
- Console.WriteLine("before impersonation: "
- + WindowsIdentity.GetCurrent().Name);
- this.output.Text += WindowsIdentity.GetCurrent().Name + "rn";
- WindowsImpersonationContext tmpid = impersonate.impersonateUser();
- Console.WriteLine("After impersonation: "
- + WindowsIdentity.GetCurrent().Name);
- this.output.Text += WindowsIdentity.GetCurrent().Name + "rn";
- impersonate.unimpersonateUser(ref tmpid);
- Console.WriteLine("After unimpersonation: "
- + WindowsIdentity.GetCurrent().Name);
- this.output.Text += WindowsIdentity.GetCurrent().Name + "rn";
- }
- }
- }
Add Comment
Please, Sign In to add comment