|
| 1 | +#region Licensing information |
| 2 | +/* |
| 3 | + * Copyright(c) 2020 Vadim Zhukov<zhuk@openbsd.org> |
| 4 | + * |
| 5 | + * Permission to use, copy, modify, and distribute this software for any |
| 6 | + * purpose with or without fee is hereby granted, provided that the above |
| 7 | + * copyright notice and this permission notice appear in all copies. |
| 8 | + * |
| 9 | + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 10 | + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 11 | + * MERCHANTABILITY AND FITNESS.IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 12 | + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 13 | + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 14 | + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 15 | + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 16 | + */ |
| 17 | +#endregion |
| 18 | + |
| 19 | +using System; |
| 20 | +using System.Collections.Generic; |
| 21 | +using System.IO.Pipes; |
| 22 | +using System.Linq; |
| 23 | +using System.Security.Principal; |
| 24 | + |
| 25 | +namespace PipeExplorer.Models |
| 26 | +{ |
| 27 | + readonly struct PipeModel : IEquatable<PipeModel> |
| 28 | + { |
| 29 | + public string Host { get; } |
| 30 | + public string Name { get; } |
| 31 | + public string Hint { get; } |
| 32 | + public int MaxConnections { get; } |
| 33 | + public PipeSecurity Acl { get; } |
| 34 | + public uint ActiveConnections { get; } |
| 35 | + |
| 36 | + public string Path => $@"\\{Host}\{Name}"; |
| 37 | + |
| 38 | + public PipeModel(string host, string name, int maxConn, uint activeConn, PipeSecurity acl) |
| 39 | + { |
| 40 | + Host = host ?? throw new ArgumentNullException(nameof(host)); |
| 41 | + Name = name ?? throw new ArgumentNullException(nameof(name)); |
| 42 | + MaxConnections = maxConn; |
| 43 | + ActiveConnections = activeConn; |
| 44 | + Hint = GetHintFor(name); |
| 45 | + Acl = acl ?? new PipeSecurity(); |
| 46 | + } |
| 47 | + |
| 48 | + public static string GetHintFor(string pipeName) |
| 49 | + { |
| 50 | + switch (pipeName) |
| 51 | + { |
| 52 | + // Source: https://l.wzm.me/_security/internet/_internet/WinServices/ch04s05s03.html |
| 53 | + |
| 54 | + case "atsvc": return "Scheduler service"; |
| 55 | + case "AudioSrv": return "Windows Audio service"; |
| 56 | + case "browser": return "Computer Browser (ntsvcs alias)"; |
| 57 | + case "cert": return "Certificate services"; |
| 58 | + case "Ctx_Winstation_API_Service": return "Terminal Services remote management"; |
| 59 | + case "DAV RPC SERVICE": return "WebDAV client"; |
| 60 | + case "dnsserver": return "DNS Server"; |
| 61 | + case "epmapper": return "RPC endpoint mapper"; |
| 62 | + case "eventlog": return "Eventlog service (ntsvcs alias)"; |
| 63 | + case "HydraLsPipe": return "Terminal Server Licensing"; |
| 64 | + case "InitShutdown": return "(Remote) system shutdown"; |
| 65 | + case "keysvc": return "Cryptographic services"; |
| 66 | + case "locator": return "RPC Locator service"; |
| 67 | + case "llsrpc": return "Licensing Logging service"; |
| 68 | + case "lsarpc": return "LSA access (lsass alias)"; |
| 69 | + case "msgsvc": return "Messenger service (ntsvcs alias)"; |
| 70 | + case "netdfs": return "Distributed File System service"; |
| 71 | + case "netlogon": return "Net Logon service (lsass alias)"; |
| 72 | + case "ntsvcs": return "Plug and Play service"; |
| 73 | + case "policyagent": return "IPSEC Policy Agent (Windows 2000)"; |
| 74 | + case "ipsec": return "IPsec Services"; |
| 75 | + case "ProfMapApi": return "Userenv"; |
| 76 | + case "protected_storage": return "Protected Storage"; |
| 77 | + case "ROUTER": return "Remote Access"; |
| 78 | + case "samr": return "SAM access (lsass alias)"; |
| 79 | + case "scerpc": return "Security Configuration Editor (SCE)"; |
| 80 | + case "SECLOGON": return "Secondary logon service"; |
| 81 | + case "SfcApi": return "Windows File Protection"; |
| 82 | + case "spoolss": return "Spooler service"; |
| 83 | + case "srvsvc": return "Server service (ntsvcs alias)"; |
| 84 | + case "ssdpsrv": return "SSDP service"; |
| 85 | + case "svcctl": return "Services control manager (ntsvcs alias)"; |
| 86 | + case "tapsrv": return "Telephony service"; |
| 87 | + case "trkwks": return "Distributed Link Tracking Client"; |
| 88 | + case "W32TIME (ntsvcs alias)": return "Windows Time (Windows 2000 and XP)"; |
| 89 | + case "W32TIME_ALT": return "Windows Time (Windows Server 2003)"; |
| 90 | + case "winlogonrpc": return "Winlogon"; |
| 91 | + case "winreg": return "Remote registry service"; |
| 92 | + case "winspipe": return "WINS service"; |
| 93 | + case "wkssvc": return "Workstation service (ntsvcs alias)"; |
| 94 | + default: return ""; |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + public override bool Equals(object obj) |
| 99 | + { |
| 100 | + return obj is PipeModel model && Equals(model); |
| 101 | + } |
| 102 | + |
| 103 | + public bool Equals(PipeModel other) |
| 104 | + { |
| 105 | + return Host == other.Host && |
| 106 | + Name == other.Name && |
| 107 | + ActiveConnections == other.ActiveConnections && |
| 108 | + MaxConnections == other.MaxConnections && |
| 109 | + AreAclsEqual(Acl, other.Acl) && |
| 110 | + Hint == other.Hint; |
| 111 | + } |
| 112 | + |
| 113 | + public override int GetHashCode() |
| 114 | + { |
| 115 | + int hashCode = -824410345; |
| 116 | + hashCode = hashCode * -1521134295 + EqualityComparer<string>.Default.GetHashCode(Host); |
| 117 | + hashCode = hashCode * -1521134295 + EqualityComparer<string>.Default.GetHashCode(Name); |
| 118 | + hashCode = hashCode * -1521134295 + ActiveConnections.GetHashCode(); |
| 119 | + hashCode = hashCode * -1521134295 + MaxConnections.GetHashCode(); |
| 120 | + hashCode = hashCode * -1521134295 + GetAclHashCode(Acl); |
| 121 | + hashCode = hashCode * -1521134295 + EqualityComparer<string>.Default.GetHashCode(Hint); |
| 122 | + return hashCode; |
| 123 | + } |
| 124 | + |
| 125 | + public static bool operator ==(PipeModel left, PipeModel right) |
| 126 | + { |
| 127 | + return left.Equals(right); |
| 128 | + } |
| 129 | + |
| 130 | + public static bool operator !=(PipeModel left, PipeModel right) |
| 131 | + { |
| 132 | + return !(left == right); |
| 133 | + } |
| 134 | + |
| 135 | + private static bool AreAclsEqual(PipeSecurity acl1, PipeSecurity acl2) |
| 136 | + { |
| 137 | + var list1 = acl1.GetAccessRules(true, true, typeof(SecurityIdentifier)).OfType<PipeAccessRule>().OrderBy(r => r, aclRuleComparer).ToList(); |
| 138 | + var list2 = acl2.GetAccessRules(true, true, typeof(SecurityIdentifier)).OfType<PipeAccessRule>().OrderBy(r => r, aclRuleComparer).ToList(); |
| 139 | + return list1.SequenceEqual(list2, aclRuleEqualityComparer); |
| 140 | + } |
| 141 | + |
| 142 | + private static int GetAclHashCode(PipeSecurity acl) |
| 143 | + { |
| 144 | + int hashCode = -1622615647; |
| 145 | + foreach (var rule in acl.GetAccessRules(true, true, typeof(SecurityIdentifier)).OfType<PipeAccessRule>().OrderBy(r => r, aclRuleComparer)) |
| 146 | + { |
| 147 | + hashCode = hashCode * -1521134295 + aclRuleEqualityComparer.GetHashCode(); |
| 148 | + } |
| 149 | + return hashCode; |
| 150 | + } |
| 151 | + |
| 152 | + private static readonly IEqualityComparer<PipeAccessRule> aclRuleEqualityComparer = new AclRuleEqualityComparer(); |
| 153 | + private static readonly IComparer<PipeAccessRule> aclRuleComparer = Comparer<PipeAccessRule>.Create(PipeAcccessRuleComparision); |
| 154 | + |
| 155 | + private class AclRuleEqualityComparer : IEqualityComparer<PipeAccessRule> |
| 156 | + { |
| 157 | + public bool Equals(PipeAccessRule x, PipeAccessRule y) |
| 158 | + { |
| 159 | + return x.IdentityReference == y.IdentityReference && |
| 160 | + x.IsInherited == y.IsInherited && |
| 161 | + x.InheritanceFlags == y.InheritanceFlags && |
| 162 | + x.PropagationFlags == y.PropagationFlags && |
| 163 | + x.AccessControlType == y.AccessControlType && |
| 164 | + x.PipeAccessRights == y.PipeAccessRights; |
| 165 | + } |
| 166 | + |
| 167 | + public int GetHashCode(PipeAccessRule rule) |
| 168 | + { |
| 169 | + int hashCode = -228291695; |
| 170 | + hashCode = hashCode * -1521134295 + rule.IdentityReference.GetHashCode(); |
| 171 | + hashCode = hashCode * -1521134295 + rule.IsInherited.GetHashCode(); |
| 172 | + hashCode = hashCode * -1521134295 + rule.InheritanceFlags.GetHashCode(); |
| 173 | + hashCode = hashCode * -1521134295 + rule.PropagationFlags.GetHashCode(); |
| 174 | + hashCode = hashCode * -1521134295 + rule.AccessControlType.GetHashCode(); |
| 175 | + hashCode = hashCode * -1521134295 + rule.PipeAccessRights.GetHashCode(); |
| 176 | + return hashCode; |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + private static int PipeAcccessRuleComparision(PipeAccessRule x, PipeAccessRule y) |
| 181 | + { |
| 182 | + int delta; |
| 183 | + if ((delta = x.IdentityReference.Value.CompareTo(y.IdentityReference.Value)) != 0) |
| 184 | + return delta; |
| 185 | + if ((delta = x.PipeAccessRights.CompareTo(y.PipeAccessRights)) != 0) |
| 186 | + return delta; |
| 187 | + if ((delta = x.AccessControlType.CompareTo(y.AccessControlType)) != 0) |
| 188 | + return delta; |
| 189 | + if ((delta = x.IsInherited.CompareTo(y.IsInherited)) != 0) |
| 190 | + return delta; |
| 191 | + if ((delta = x.InheritanceFlags.CompareTo(y.InheritanceFlags)) != 0) |
| 192 | + return delta; |
| 193 | + if ((delta = x.PropagationFlags.CompareTo(y.PropagationFlags)) != 0) |
| 194 | + return delta; |
| 195 | + return 0; |
| 196 | + } |
| 197 | + } |
| 198 | +} |
0 commit comments