NetworkAPI.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /* Copyright (C) 2014-2017 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved.
  2. *
  3. * You can redistribute this program and/or modify it under the terms of
  4. * the GNU Lesser Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. */
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Runtime.InteropServices;
  10. using Utilities;
  11. namespace SMBLibrary.Win32.Security
  12. {
  13. public class NetworkAPI
  14. {
  15. [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
  16. public struct USER_INFO_0
  17. {
  18. [MarshalAs(UnmanagedType.LPWStr)]
  19. public String Username;
  20. }
  21. [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
  22. public struct USER_INFO_1
  23. {
  24. [MarshalAs(UnmanagedType.LPWStr)]
  25. public string Username;
  26. [MarshalAs(UnmanagedType.LPWStr)]
  27. public string Password;
  28. public uint PasswordAge;
  29. public uint Priv;
  30. [MarshalAs(UnmanagedType.LPWStr)]
  31. public string Home_Dir;
  32. [MarshalAs(UnmanagedType.LPWStr)]
  33. public string Comment;
  34. public uint Flags;
  35. [MarshalAs(UnmanagedType.LPWStr)]
  36. public string ScriptPath;
  37. }
  38. [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
  39. public struct LOCALGROUP_USERS_INFO_0
  40. {
  41. public string groupname;
  42. }
  43. // NetUserEnum - Obtains a list of all users on local machine or network
  44. [DllImport("Netapi32.dll")]
  45. 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);
  46. // NetAPIBufferFree - Used to clear the Network buffer after NetUserEnum
  47. [DllImport("Netapi32.dll")]
  48. public extern static int NetApiBufferFree(IntPtr Buffer);
  49. [DllImport("Netapi32.dll")]
  50. public extern static int NetUserGetLocalGroups([MarshalAs(UnmanagedType.LPWStr)]string servername,[MarshalAs(UnmanagedType.LPWStr)] string username, int level, int flags, out IntPtr bufptr, int prefmaxlen, out int entriesread, out int totalentries);
  51. public static List<string> EnumerateGroups(string userName)
  52. {
  53. List<string> result = new List<string>();
  54. int entriesRead;
  55. int totalEntries;
  56. IntPtr bufPtr;
  57. const int Level = 0;
  58. NetUserGetLocalGroups(null, userName, Level, 0, out bufPtr, 1024, out entriesRead, out totalEntries);
  59. if (entriesRead > 0)
  60. {
  61. LOCALGROUP_USERS_INFO_0[] RetGroups = new LOCALGROUP_USERS_INFO_0[entriesRead];
  62. IntPtr iter = bufPtr;
  63. for (int i = 0; i < entriesRead; i++)
  64. {
  65. RetGroups[i] = (LOCALGROUP_USERS_INFO_0)Marshal.PtrToStructure(iter, typeof(LOCALGROUP_USERS_INFO_0));
  66. iter = new IntPtr(iter.ToInt64() + Marshal.SizeOf(typeof(LOCALGROUP_USERS_INFO_0)));
  67. result.Add(RetGroups[i].groupname);
  68. }
  69. NetApiBufferFree(bufPtr);
  70. }
  71. return result;
  72. }
  73. public static List<string> EnumerateAllUsers()
  74. {
  75. List<string> result = new List<string>();
  76. int entriesRead;
  77. int totalEntries;
  78. int resume;
  79. IntPtr bufPtr;
  80. const int Level = 0;
  81. const int FILTER_NORMAL_ACCOUNT = 2;
  82. NetUserEnum(null, Level, FILTER_NORMAL_ACCOUNT, out bufPtr, -1, out entriesRead, out totalEntries, out resume);
  83. if (entriesRead > 0)
  84. {
  85. USER_INFO_0[] Users = new USER_INFO_0[entriesRead];
  86. IntPtr iter = bufPtr;
  87. for (int i = 0; i < entriesRead; i++)
  88. {
  89. Users[i] = (USER_INFO_0)Marshal.PtrToStructure(iter, typeof(USER_INFO_0));
  90. iter = new IntPtr(iter.ToInt64() + Marshal.SizeOf(typeof(USER_INFO_0)));
  91. result.Add(Users[i].Username);
  92. }
  93. NetApiBufferFree(bufPtr);
  94. }
  95. return result;
  96. }
  97. public static List<string> EnumerateEnabledUsers()
  98. {
  99. List<string> result = new List<string>();
  100. int entriesRead;
  101. int totalEntries;
  102. int resume;
  103. IntPtr bufPtr;
  104. const int Level = 1;
  105. const int FILTER_NORMAL_ACCOUNT = 2;
  106. const int UF_ACCOUNTDISABLE = 2;
  107. NetUserEnum(null, Level, FILTER_NORMAL_ACCOUNT, out bufPtr, -1, out entriesRead, out totalEntries, out resume);
  108. if (entriesRead > 0)
  109. {
  110. USER_INFO_1[] Users = new USER_INFO_1[entriesRead];
  111. IntPtr iter = bufPtr;
  112. for (int i = 0; i < entriesRead; i++)
  113. {
  114. Users[i] = (USER_INFO_1)Marshal.PtrToStructure(iter, typeof(USER_INFO_1));
  115. iter = new IntPtr(iter.ToInt64() + Marshal.SizeOf(typeof(USER_INFO_1)));
  116. if ((Users[i].Flags & UF_ACCOUNTDISABLE) == 0)
  117. {
  118. result.Add(Users[i].Username);
  119. }
  120. }
  121. NetApiBufferFree(bufPtr);
  122. }
  123. return result;
  124. }
  125. public static List<string> EnumerateNetworkUsers()
  126. {
  127. List<string> result = new List<string>();
  128. List<string> users = EnumerateEnabledUsers();
  129. foreach (string userName in users)
  130. {
  131. List<string> groups = EnumerateGroups(userName);
  132. if (groups.Contains("Users") || groups.Contains("Administrators") || groups.Contains("Guests"))
  133. {
  134. result.Add(userName);
  135. }
  136. }
  137. return result;
  138. }
  139. }
  140. }