Advertisement
Guest User

Untitled

a guest
Feb 19th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.48 KB | None | 0 0
  1. public class BackupRestoreUsers
  2. {
  3. private List<string> usernames;
  4.  
  5. public BackupRestoreUsers()
  6. {
  7. }
  8.  
  9. public static void Main(string[] args)
  10. {
  11. BackupRestoreUsers test = new BackupRestoreUsers();
  12. test.usernames=test.GetUsers();
  13. foreach (string name in test.usernames)
  14. {
  15. Console.WriteLine(name);
  16. test.BackupUser(name);
  17. }
  18. Console.Read();
  19. }
  20.  
  21. public List<string> GetUsers()
  22. {
  23. int EntriesRead;
  24. int TotalEntries;
  25. int Resume;
  26. IntPtr bufPtr;
  27. List<string> users = new List<string>();
  28.  
  29. NetworkAPI.NetUserEnum(null, 0, 2, out bufPtr, -1, out EntriesRead, out TotalEntries, out Resume);
  30. if (EntriesRead > 0)
  31. {
  32. NetworkAPI.USER_INFO_0[] Users = new NetworkAPI.USER_INFO_0[EntriesRead];
  33. IntPtr iter = bufPtr;
  34. for (int i = 0; i < EntriesRead; i++)
  35. {
  36. Users[i] = (NetworkAPI.USER_INFO_0)Marshal.PtrToStructure(iter, typeof(NetworkAPI.USER_INFO_0));
  37. iter = (IntPtr)((int)iter + Marshal.SizeOf(typeof(NetworkAPI.USER_INFO_0)));
  38. users.Add(Users[i].usri0_name);
  39. }
  40. NetworkAPI.NetApiBufferFree(bufPtr);
  41. }
  42. return users;
  43. }
  44.  
  45. public bool BackupUser(string username)
  46. {
  47. const int levelOfInfo=4;
  48. IntPtr bufPtr;
  49.  
  50. NetworkAPI.NetUserGetInfo(null, username, levelOfInfo, out bufPtr);
  51. NetworkAPI.USER_INFO_4 userDetails = (NetworkAPI.USER_INFO_4)Marshal.PtrToStructure(bufPtr, typeof(NetworkAPI.USER_INFO_4));
  52. Console.WriteLine(userDetails.usri4_full_name);
  53. Console.WriteLine(userDetails.usri4_country_code);
  54. Console.WriteLine(userDetails.usri4_code_page);
  55. Console.WriteLine(userDetails.usri4_auth_flags);
  56. Console.WriteLine(userDetails.usri4_home_dir_drive);
  57. Console.WriteLine();
  58. NetworkAPI.NetApiBufferFree(bufPtr);
  59. return true;
  60. }
  61. }
  62.  
  63. public class NetworkAPI
  64. {
  65. // USER_INFO_0 - Structure to hold Just Usernames
  66. [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
  67. public struct USER_INFO_0
  68. {
  69. public String usri0_name;
  70. }
  71.  
  72. // USER_INFO_3 - structure to hold detailed information of user
  73. [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
  74. public struct USER_INFO_4
  75. {
  76. public string usri4_name;
  77. public string usri4_password;
  78. public int usri4_password_age;
  79. public int usri4_priv;
  80. public string usri4_home_dir;
  81. public string usri4_comment;
  82. public int usri4_flags;
  83. public string usri4_script_path;
  84. public int usri4_auth_flags;
  85. public string usri4_full_name;
  86. public string usri4_usr_comment;
  87. public string usri4_parms;
  88. public string usri4_workstations;
  89. public int usri4_last_logon;
  90. public int usri4_last_logoff;
  91. public int usri4_acct_expires;
  92. public int usri4_max_storage;
  93. public int usri4_units_per_week;
  94. public IntPtr usri4_logon_hours;// PBYTE i.e. byte pointer converts to IntPtr
  95. public int usri4_bad_pw_count;
  96. public int usri4_num_logons;
  97. public string usri4_logon_server;
  98. public int usri4_country_code;
  99. public int usri4_code_page;
  100. public IntPtr usri4_user_sid;// PSID- Pointer to the Security IDentifier structure used to uniqly identify users or groups, generally ignored
  101. public int usri4_primary_group_id;
  102. public string usri4_profile;
  103. public string usri4_home_dir_drive;
  104. public int usri4_password_expired;
  105. }
  106.  
  107. // NetUserAdd - To Add Users to a local machine or Network
  108. [DllImport("Netapi32.dll")]
  109. public extern static int NetUserAdd([MarshalAs(UnmanagedType.LPWStr)] string servername, int level, ref USER_INFO_4 buf, int parm_err);
  110.  
  111. // NetUserDel - To delete Users from a local machine or Network
  112. [DllImport("Netapi32.dll")]
  113. public extern static int NetUserDel([MarshalAs(UnmanagedType.LPWStr)] string servername, [MarshalAs(UnmanagedType.LPWStr)] string username);
  114.  
  115. // NetUserGetInfo - Returns to a struct Information about the specified user
  116. [DllImport("Netapi32.dll")]
  117. public extern static int NetUserGetInfo([MarshalAs(UnmanagedType.LPWStr)] string servername, [MarshalAs(UnmanagedType.LPWStr)] string username, int level, out IntPtr bufptr);
  118.  
  119. // NetUserSetInfo - Allows us to modify User information
  120. [DllImport("Netapi32.dll")]
  121. public extern static int NetUserSetInfo([MarshalAs(UnmanagedType.LPWStr)] string servername, [MarshalAs(UnmanagedType.LPWStr)] string username, int level, ref USER_INFO_4 buf, int error);
  122.  
  123. // NetUserChangePassword - Allows us to change a users password providing we have it
  124. [DllImport("Netapi32.dll")]
  125. public extern static int NetUserChangePassword([MarshalAs(UnmanagedType.LPWStr)] string domainname, [MarshalAs(UnmanagedType.LPWStr)] string username, [MarshalAs(UnmanagedType.LPWStr)] string oldpassword, [MarshalAs(UnmanagedType.LPWStr)] string newpassword);
  126.  
  127. // NetUserEnum - Obtains a list of all users on local machine or network
  128. [DllImport("Netapi32.dll")]
  129. public extern static int NetUserEnum(string servername, int level, int filter, out IntPtr bufptr, int prefmaxlen, out int entriesread, out int totalentries, out int resume_handle);
  130.  
  131. // NetAPIBufferFree - Used to clear the Network buffer after NetUserEnum
  132. [DllImport("Netapi32.dll")]
  133. public extern static int NetApiBufferFree(IntPtr Buffer);
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement