peass-ng/PEASS-ng · error · Win32Exception

Win32Exception

Error message

Win32Exception

What it means

NetServerEnum wraps the Netapi32 NetServerEnum call and throws Win32Exception(ret) for any nonzero return code at NetServerEnum.cs:104. Common codes are ERROR_ACCESS_DENIED (5), ERROR_NO_BROWSER_SERVERS_FOUND (6118), and ERROR_INVALID_LEVEL — meaning the server/network enumeration itself failed before any results were produced.

Source

Thrown at winPEAS/winPEASexe/winPEAS/TaskScheduler/TaskEditor/Native/NetServerEnum.cs:104

        public static IEnumerable<NetworkComputerInfo> GetNetworkComputerInfo(ServerTypes serverTypes = ServerTypes.Workstation | ServerTypes.Server, string domain = null) =>
            NetServerEnum<NetworkComputerInfo>(serverTypes, domain, 101);

        public static T[] NetServerEnum<T>(ServerTypes serverTypes = ServerTypes.Workstation | ServerTypes.Server, string domain = null, int level = 0) where T : struct
        {
            if (level == 0)
                level = int.Parse(System.Text.RegularExpressions.Regex.Replace(typeof(T).Name, @"[^\d]", ""));

            IntPtr bufptr = IntPtr.Zero;
            try
            {
                int entriesRead, totalEntries;
                IntPtr resumeHandle = IntPtr.Zero;

                int ret = Netapi32.NetServerEnum(null, level, out bufptr, MAX_PREFERRED_LENGTH, out entriesRead, out totalEntries, serverTypes, domain, resumeHandle);
                if (ret == 0)
                    return InteropUtil.ToArray<T>(bufptr, entriesRead);
                throw new System.ComponentModel.Win32Exception(ret);
            }
            finally
            {
                Netapi32.NetApiBufferFree(bufptr);
            }
        }

        public static T NetServerGetInfo<T>(string serverName, int level = 0) where T : struct
        {
            if (level == 0)
                level = int.Parse(System.Text.RegularExpressions.Regex.Replace(typeof(T).Name, @"[^\d]", ""));

            IntPtr ptr = IntPtr.Zero;
            try
            {
                int ret = Netapi32.NetServerGetInfo(serverName, level, out ptr);
                if (ret != 0)
                {

View on GitHub (pinned to 53fb989abc)

Solutions

  1. Check Win32Exception.NativeErrorCode: 6118 means no browser servers — ensure the Computer Browser/Server services and NetBIOS are available
  2. Run with sufficient privileges and confirm access isn't blocked by policy
  3. Fall back to directory-based discovery (AD lookup) when browser enumeration fails
  4. Catch Win32Exception and return an empty/partial server list instead of failing

Example fix

// before
var servers = NetServerEnum.GetServerList(serverTypes, domain);
// after
try { servers = NetServerEnum.GetServerList(serverTypes, domain); }
catch (Win32Exception ex) { Log($"NetServerEnum failed {ex.NativeErrorCode}"); servers = new ServerInfo[0]; }
Defensive patterns

Strategy: fallback

Validate before calling

var sc = new System.ServiceProcess.ServiceController("LanmanServer");
if (sc.Status != System.ServiceProcess.ServiceControllerStatus.Running) { Log("Server service not running"); return; }

Try / catch

try { servers = NetServerEnum.GetServerList(types, domain); } catch (Win32Exception ex) { Log($"NetServerEnum {ex.NativeErrorCode}"); servers = Enumerable.Empty<ServerInfo>(); }

Prevention

When it happens

Trigger: Enumerating servers of a given type in a domain/workgroup where the browser service is unavailable, the caller lacks permission, the domain parameter is wrong, or the Server service / Computer Browser is disabled on the network.

Common situations: Discovering network servers in hardened environments where browsing is blocked by firewalls; Workstation/Server/Browser services stopped; enumerating in a workgroup with no master browser; group policy blocking enumeration.

Related errors


AI-assisted analysis of peass-ng/PEASS-ng@53fb989abc (2026-09-02). Data as JSON: /api/errors/792b5588932cebca. Report an issue: GitHub.