NetworkAPI.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /* Copyright (C) 2014 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 System.Text;
  11. using Utilities;
  12. namespace SMBLibrary.Authentication.Win32
  13. {
  14. public class NetworkAPI
  15. {
  16. [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
  17. public struct USER_INFO_0
  18. {
  19. [MarshalAs(UnmanagedType.LPWStr)]
  20. public String Username;
  21. }
  22. [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
  23. public struct USER_INFO_1
  24. {
  25. [MarshalAs(UnmanagedType.LPWStr)]
  26. public string Username;
  27. [MarshalAs(UnmanagedType.LPWStr)]
  28. public string Password;
  29. public uint PasswordAge;
  30. public uint Priv;
  31. [MarshalAs(UnmanagedType.LPWStr)]
  32. public string Home_Dir;
  33. [MarshalAs(UnmanagedType.LPWStr)]
  34. public string Comment;
  35. public uint Flags;
  36. [MarshalAs(UnmanagedType.LPWStr)]
  37. public string ScriptPath;
  38. }
  39. [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
  40. public struct LOCALGROUP_USERS_INFO_0
  41. {
  42. public string groupname;
  43. }
  44. // NetUserEnum - Obtains a list of all users on local machine or network
  45. [DllImport("Netapi32.dll")]
  46. 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);
  47. // NetAPIBufferFree - Used to clear the Network buffer after NetUserEnum
  48. [DllImport("Netapi32.dll")]
  49. public extern static int NetApiBufferFree(IntPtr Buffer);
  50. [DllImport("Netapi32.dll")]
  51. 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);
  52. public static List<string> EnumerateGroups(string userName)
  53. {
  54. List<string> result = new List<string>();
  55. int entriesRead;
  56. int totalEntries;
  57. IntPtr bufPtr;
  58. const int Level = 0;
  59. NetUserGetLocalGroups(null, userName, Level, 0, out bufPtr, 1024, out entriesRead, out totalEntries);
  60. if (entriesRead > 0)
  61. {
  62. LOCALGROUP_USERS_INFO_0[] RetGroups = new LOCALGROUP_USERS_INFO_0[entriesRead];
  63. IntPtr iter = bufPtr;
  64. for (int i = 0; i < entriesRead; i++)
  65. {
  66. RetGroups[i] = (LOCALGROUP_USERS_INFO_0)Marshal.PtrToStructure(iter, typeof(LOCALGROUP_USERS_INFO_0));
  67. iter = (IntPtr)((int)iter + Marshal.SizeOf(typeof(LOCALGROUP_USERS_INFO_0)));
  68. result.Add(RetGroups[i].groupname);
  69. }
  70. NetApiBufferFree(bufPtr);
  71. }
  72. return result;
  73. }
  74. public static List<string> EnumerateAllUsers()
  75. {
  76. List<string> result = new List<string>();
  77. int entriesRead;
  78. int totalEntries;
  79. int resume;
  80. IntPtr bufPtr;
  81. const int Level = 0;
  82. const int FILTER_NORMAL_ACCOUNT = 2;
  83. NetUserEnum(null, Level, FILTER_NORMAL_ACCOUNT, out bufPtr, -1, out entriesRead, out totalEntries, out resume);
  84. if (entriesRead > 0)
  85. {
  86. USER_INFO_0[] Users = new USER_INFO_0[entriesRead];
  87. IntPtr iter = bufPtr;
  88. for (int i = 0; i < entriesRead; i++)
  89. {
  90. Users[i] = (USER_INFO_0)Marshal.PtrToStructure(iter, typeof(USER_INFO_0));
  91. iter = (IntPtr)((int)iter + Marshal.SizeOf(typeof(USER_INFO_0)));
  92. result.Add(Users[i].Username);
  93. }
  94. NetApiBufferFree(bufPtr);
  95. }
  96. return result;
  97. }
  98. public static List<string> EnumerateEnabledUsers()
  99. {
  100. List<string> result = new List<string>();
  101. int entriesRead;
  102. int totalEntries;
  103. int resume;
  104. IntPtr bufPtr;
  105. const int Level = 1;
  106. const int FILTER_NORMAL_ACCOUNT = 2;
  107. const int UF_ACCOUNTDISABLE = 2;
  108. NetUserEnum(null, Level, FILTER_NORMAL_ACCOUNT, out bufPtr, -1, out entriesRead, out totalEntries, out resume);
  109. if (entriesRead > 0)
  110. {
  111. USER_INFO_1[] Users = new USER_INFO_1[entriesRead];
  112. IntPtr iter = bufPtr;
  113. for (int i = 0; i < entriesRead; i++)
  114. {
  115. Users[i] = (USER_INFO_1)Marshal.PtrToStructure(iter, typeof(USER_INFO_1));
  116. iter = (IntPtr)((int)iter + Marshal.SizeOf(typeof(USER_INFO_1)));
  117. if ((Users[i].Flags & UF_ACCOUNTDISABLE) == 0)
  118. {
  119. result.Add(Users[i].Username);
  120. }
  121. }
  122. NetApiBufferFree(bufPtr);
  123. }
  124. return result;
  125. }
  126. public static List<string> EnumerateNetworkUsers()
  127. {
  128. List<string> result = new List<string>();
  129. List<string> users = EnumerateEnabledUsers();
  130. foreach (string userName in users)
  131. {
  132. List<string> groups = EnumerateGroups(userName);
  133. if (groups.Contains("Users") || groups.Contains("Administrators") || groups.Contains("Guests"))
  134. {
  135. result.Add(userName);
  136. }
  137. }
  138. return result;
  139. }
  140. }
  141. }