Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class BackupRestoreUsers
- {
- private List<string> usernames;
- public BackupRestoreUsers()
- {
- }
- public static void Main(string[] args)
- {
- BackupRestoreUsers test = new BackupRestoreUsers();
- test.usernames=test.GetUsers();
- foreach (string name in test.usernames)
- {
- Console.WriteLine(name);
- test.BackupUser(name);
- }
- Console.Read();
- }
- public List<string> GetUsers()
- {
- int EntriesRead;
- int TotalEntries;
- int Resume;
- IntPtr bufPtr;
- List<string> users = new List<string>();
- NetworkAPI.NetUserEnum(null, 0, 2, out bufPtr, -1, out EntriesRead, out TotalEntries, out Resume);
- if (EntriesRead > 0)
- {
- NetworkAPI.USER_INFO_0[] Users = new NetworkAPI.USER_INFO_0[EntriesRead];
- IntPtr iter = bufPtr;
- for (int i = 0; i < EntriesRead; i++)
- {
- Users[i] = (NetworkAPI.USER_INFO_0)Marshal.PtrToStructure(iter, typeof(NetworkAPI.USER_INFO_0));
- iter = (IntPtr)((int)iter + Marshal.SizeOf(typeof(NetworkAPI.USER_INFO_0)));
- users.Add(Users[i].usri0_name);
- }
- NetworkAPI.NetApiBufferFree(bufPtr);
- }
- return users;
- }
- public bool BackupUser(string username)
- {
- const int levelOfInfo=4;
- IntPtr bufPtr;
- NetworkAPI.NetUserGetInfo(null, username, levelOfInfo, out bufPtr);
- NetworkAPI.USER_INFO_4 userDetails = (NetworkAPI.USER_INFO_4)Marshal.PtrToStructure(bufPtr, typeof(NetworkAPI.USER_INFO_4));
- Console.WriteLine(userDetails.usri4_full_name);
- Console.WriteLine(userDetails.usri4_country_code);
- Console.WriteLine(userDetails.usri4_code_page);
- Console.WriteLine(userDetails.usri4_auth_flags);
- Console.WriteLine(userDetails.usri4_home_dir_drive);
- Console.WriteLine();
- NetworkAPI.NetApiBufferFree(bufPtr);
- return true;
- }
- }
- public class NetworkAPI
- {
- // USER_INFO_0 - Structure to hold Just Usernames
- [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
- public struct USER_INFO_0
- {
- public String usri0_name;
- }
- // USER_INFO_3 - structure to hold detailed information of user
- [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
- public struct USER_INFO_4
- {
- public string usri4_name;
- public string usri4_password;
- public int usri4_password_age;
- public int usri4_priv;
- public string usri4_home_dir;
- public string usri4_comment;
- public int usri4_flags;
- public string usri4_script_path;
- public int usri4_auth_flags;
- public string usri4_full_name;
- public string usri4_usr_comment;
- public string usri4_parms;
- public string usri4_workstations;
- public int usri4_last_logon;
- public int usri4_last_logoff;
- public int usri4_acct_expires;
- public int usri4_max_storage;
- public int usri4_units_per_week;
- public IntPtr usri4_logon_hours;// PBYTE i.e. byte pointer converts to IntPtr
- public int usri4_bad_pw_count;
- public int usri4_num_logons;
- public string usri4_logon_server;
- public int usri4_country_code;
- public int usri4_code_page;
- public IntPtr usri4_user_sid;// PSID- Pointer to the Security IDentifier structure used to uniqly identify users or groups, generally ignored
- public int usri4_primary_group_id;
- public string usri4_profile;
- public string usri4_home_dir_drive;
- public int usri4_password_expired;
- }
- // NetUserAdd - To Add Users to a local machine or Network
- [DllImport("Netapi32.dll")]
- public extern static int NetUserAdd([MarshalAs(UnmanagedType.LPWStr)] string servername, int level, ref USER_INFO_4 buf, int parm_err);
- // NetUserDel - To delete Users from a local machine or Network
- [DllImport("Netapi32.dll")]
- public extern static int NetUserDel([MarshalAs(UnmanagedType.LPWStr)] string servername, [MarshalAs(UnmanagedType.LPWStr)] string username);
- // NetUserGetInfo - Returns to a struct Information about the specified user
- [DllImport("Netapi32.dll")]
- public extern static int NetUserGetInfo([MarshalAs(UnmanagedType.LPWStr)] string servername, [MarshalAs(UnmanagedType.LPWStr)] string username, int level, out IntPtr bufptr);
- // NetUserSetInfo - Allows us to modify User information
- [DllImport("Netapi32.dll")]
- public extern static int NetUserSetInfo([MarshalAs(UnmanagedType.LPWStr)] string servername, [MarshalAs(UnmanagedType.LPWStr)] string username, int level, ref USER_INFO_4 buf, int error);
- // NetUserChangePassword - Allows us to change a users password providing we have it
- [DllImport("Netapi32.dll")]
- 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);
- // NetUserEnum - Obtains a list of all users on local machine or network
- [DllImport("Netapi32.dll")]
- 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);
- // NetAPIBufferFree - Used to clear the Network buffer after NetUserEnum
- [DllImport("Netapi32.dll")]
- public extern static int NetApiBufferFree(IntPtr Buffer);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement