Guest User

Untitled

a guest
Aug 13th, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.72 KB | None | 0 0
  1. Reading remote windows registry via C#
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. using System.Runtime.InteropServices;
  6. using System.Security.Principal;
  7. using System.Security.Permissions;
  8.  
  9. namespace WindowsFormsApplication1
  10. {
  11. class ImpersonateClass
  12. {
  13. [DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
  14. public static extern bool LogonUser(String lpszUsername, String lpszDomain, String lpszPassword,
  15. int dwLogonType, int dwLogonProvider, ref IntPtr phToken);
  16.  
  17. [PermissionSetAttribute(SecurityAction.Demand, Name = "FullTrust")]
  18. [DllImport("kernel32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto)]
  19. private unsafe static extern int FormatMessage(int dwFlags, ref IntPtr lpSource,
  20. int dwMessageId, int dwLanguageId, ref String lpBuffer, int nSize, IntPtr* Arguments);
  21.  
  22. [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
  23. public extern static bool CloseHandle(IntPtr handle);
  24.  
  25. [DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  26. public extern static bool DuplicateToken(IntPtr ExistingTokenHandle,
  27. int SECURITY_IMPERSONATION_LEVEL, ref IntPtr DuplicateTokenHandle);
  28.  
  29. const int LOGON32_PROVIDER_DEFAULT = 0;
  30. const int LOGON32_LOGON_INTERACTIVE = 2;
  31. const int LOGON32_LOGON_NEW_CREDENTIALS = 9;
  32. private string userName;
  33. private string computerName;
  34. private string password;
  35. private IntPtr tokenHandle = IntPtr.Zero;
  36.  
  37. public ImpersonateClass(string aUserName, string aComputerName, string aPassword)
  38. {
  39. userName = aUserName;
  40. computerName = aComputerName;
  41. password = aPassword;
  42. }
  43.  
  44. public WindowsImpersonationContext impersonateUser()
  45. {
  46. if (LogonUser(userName, computerName, password, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, ref tokenHandle))
  47. {
  48. Console.WriteLine("Value of Windows NT token: " + tokenHandle);
  49. WindowsIdentity newId = new WindowsIdentity(tokenHandle);
  50. return newId.Impersonate();
  51. }
  52. else
  53. {
  54. return null;
  55. }
  56. }
  57.  
  58. public void unimpersonateUser(ref WindowsImpersonationContext anImpersonatedUser)
  59. {
  60. if (anImpersonatedUser != null)
  61. {
  62. anImpersonatedUser.Undo();
  63. }
  64. }
  65. }
  66. }
  67.  
  68. using System;
  69. using System.Collections.Generic;
  70. using System.ComponentModel;
  71. using System.Security.Principal;
  72. using System.Data;
  73. using System.Drawing;
  74. using System.Linq;
  75. using System.Text;
  76. using System.Windows.Forms;
  77.  
  78. namespace WindowsFormsApplication1
  79. {
  80. public partial class MainForm : Form
  81. {
  82. public MainForm()
  83. {
  84. InitializeComponent();
  85. }
  86.  
  87. private void button_Click(object sender, EventArgs e)
  88. {
  89. ImpersonateClass impersonate = new ImpersonateClass(@"admministrator", "localMachine", "2.71828");
  90. Console.WriteLine("before impersonation: "
  91. + WindowsIdentity.GetCurrent().Name);
  92. this.output.Text += WindowsIdentity.GetCurrent().Name + "rn";
  93.  
  94. WindowsImpersonationContext tmpid = impersonate.impersonateUser();
  95. Console.WriteLine("After impersonation: "
  96. + WindowsIdentity.GetCurrent().Name);
  97. this.output.Text += WindowsIdentity.GetCurrent().Name + "rn";
  98.  
  99. impersonate.unimpersonateUser(ref tmpid);
  100. Console.WriteLine("After unimpersonation: "
  101. + WindowsIdentity.GetCurrent().Name);
  102. this.output.Text += WindowsIdentity.GetCurrent().Name + "rn";
  103. }
  104. }
  105. }
Add Comment
Please, Sign In to add comment