LoopUtil.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Runtime.InteropServices;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. /*
  8. * Important - Even though this will compile in the shared projects, it will cause build failures within the mono runtime
  9. */
  10. namespace MediaBrowser.ServerApplication.Native
  11. {
  12. /// <summary>
  13. /// http://blogs.msdn.com/b/fiddler/archive/2011/12/10/fiddler-windows-8-apps-enable-LoopUtil-network-isolation-exemption.aspx
  14. /// </summary>
  15. public class LoopUtil
  16. {
  17. //http://msdn.microsoft.com/en-us/library/windows/desktop/aa379595(v=vs.85).aspx
  18. [StructLayout(LayoutKind.Sequential)]
  19. internal struct SID_AND_ATTRIBUTES
  20. {
  21. public IntPtr Sid;
  22. public uint Attributes;
  23. }
  24. [StructLayoutAttribute(LayoutKind.Sequential)]
  25. internal struct INET_FIREWALL_AC_CAPABILITIES
  26. {
  27. public uint count;
  28. public IntPtr capabilities; //SID_AND_ATTRIBUTES
  29. }
  30. [StructLayoutAttribute(LayoutKind.Sequential)]
  31. internal struct INET_FIREWALL_AC_BINARIES
  32. {
  33. public uint count;
  34. public IntPtr binaries;
  35. }
  36. [StructLayoutAttribute(LayoutKind.Sequential)]
  37. internal struct INET_FIREWALL_APP_CONTAINER
  38. {
  39. internal IntPtr appContainerSid;
  40. internal IntPtr userSid;
  41. [MarshalAs(UnmanagedType.LPWStr)]
  42. public string appContainerName;
  43. [MarshalAs(UnmanagedType.LPWStr)]
  44. public string displayName;
  45. [MarshalAs(UnmanagedType.LPWStr)]
  46. public string description;
  47. internal INET_FIREWALL_AC_CAPABILITIES capabilities;
  48. internal INET_FIREWALL_AC_BINARIES binaries;
  49. [MarshalAs(UnmanagedType.LPWStr)]
  50. public string workingDirectory;
  51. [MarshalAs(UnmanagedType.LPWStr)]
  52. public string packageFullName;
  53. }
  54. // Call this API to load the current list of LoopUtil-enabled AppContainers
  55. [DllImport("FirewallAPI.dll")]
  56. internal static extern uint NetworkIsolationGetAppContainerConfig(out uint pdwCntACs, out IntPtr appContainerSids);
  57. // Call this API to set the LoopUtil-exemption list
  58. [DllImport("FirewallAPI.dll")]
  59. private static extern uint NetworkIsolationSetAppContainerConfig(uint pdwCntACs, SID_AND_ATTRIBUTES[] appContainerSids);
  60. // Use this API to convert a string SID into an actual SID
  61. [DllImport("advapi32.dll", SetLastError = true)]
  62. internal static extern bool ConvertStringSidToSid(string strSid, out IntPtr pSid);
  63. [DllImport("advapi32", /*CharSet = CharSet.Auto,*/ SetLastError = true)]
  64. static extern bool ConvertSidToStringSid(IntPtr pSid, out string strSid);
  65. // Call this API to enumerate all of the AppContainers on the system
  66. [DllImport("FirewallAPI.dll")]
  67. internal static extern uint NetworkIsolationEnumAppContainers(uint Flags, out uint pdwCntPublicACs, out IntPtr ppACs);
  68. // DWORD NetworkIsolationEnumAppContainers(
  69. // _In_ DWORD Flags,
  70. // _Out_ DWORD *pdwNumPublicAppCs,
  71. // _Out_ PINET_FIREWALL_APP_CONTAINER *ppPublicAppCs
  72. //);
  73. //http://msdn.microsoft.com/en-gb/library/windows/desktop/hh968116.aspx
  74. enum NETISO_FLAG
  75. {
  76. NETISO_FLAG_FORCE_COMPUTE_BINARIES = 0x1,
  77. NETISO_FLAG_MAX = 0x2
  78. }
  79. public class AppContainer
  80. {
  81. public String appContainerName { get; set; }
  82. public String displayName { get; set; }
  83. public String workingDirectory { get; set; }
  84. public String StringSid { get; set; }
  85. public List<uint> capabilities { get; set; }
  86. public bool LoopUtil { get; set; }
  87. public AppContainer(String _appContainerName, String _displayName, String _workingDirectory, IntPtr _sid)
  88. {
  89. this.appContainerName = _appContainerName;
  90. this.displayName = _displayName;
  91. this.workingDirectory = _workingDirectory;
  92. String tempSid;
  93. ConvertSidToStringSid(_sid, out tempSid);
  94. this.StringSid = tempSid;
  95. }
  96. }
  97. internal List<LoopUtil.INET_FIREWALL_APP_CONTAINER> _AppList;
  98. internal List<LoopUtil.SID_AND_ATTRIBUTES> _AppListConfig;
  99. public List<AppContainer> Apps = new List<AppContainer>();
  100. internal IntPtr _pACs;
  101. public LoopUtil()
  102. {
  103. LoadApps();
  104. }
  105. public void LoadApps()
  106. {
  107. Apps.Clear();
  108. _pACs = IntPtr.Zero;
  109. //Full List of Apps
  110. _AppList = PI_NetworkIsolationEnumAppContainers();
  111. //List of Apps that have LoopUtil enabled.
  112. _AppListConfig = PI_NetworkIsolationGetAppContainerConfig();
  113. foreach (var PI_app in _AppList)
  114. {
  115. AppContainer app = new AppContainer(PI_app.appContainerName, PI_app.displayName, PI_app.workingDirectory, PI_app.appContainerSid);
  116. app.LoopUtil = CheckLoopback(PI_app.appContainerSid);
  117. Apps.Add(app);
  118. }
  119. }
  120. private bool CheckLoopback(IntPtr intPtr)
  121. {
  122. foreach (SID_AND_ATTRIBUTES item in _AppListConfig)
  123. {
  124. string left, right;
  125. ConvertSidToStringSid(item.Sid, out left);
  126. ConvertSidToStringSid(intPtr, out right);
  127. if (left == right)
  128. {
  129. return true;
  130. }
  131. }
  132. return false;
  133. }
  134. private bool CreateExcemptions(string appName)
  135. {
  136. var hasChanges = false;
  137. foreach (var app in Apps)
  138. {
  139. if ((app.appContainerName ?? string.Empty).IndexOf(appName, StringComparison.OrdinalIgnoreCase) != -1 ||
  140. (app.displayName ?? string.Empty).IndexOf(appName, StringComparison.OrdinalIgnoreCase) != -1)
  141. {
  142. if (!app.LoopUtil)
  143. {
  144. app.LoopUtil = true;
  145. hasChanges = true;
  146. }
  147. }
  148. }
  149. return hasChanges;
  150. }
  151. public static void Run(string appName)
  152. {
  153. var util = new LoopUtil();
  154. util.LoadApps();
  155. var hasChanges = util.CreateExcemptions(appName);
  156. if (hasChanges)
  157. {
  158. util.SaveLoopbackState();
  159. }
  160. }
  161. private static List<SID_AND_ATTRIBUTES> PI_NetworkIsolationGetAppContainerConfig()
  162. {
  163. IntPtr arrayValue = IntPtr.Zero;
  164. uint size = 0;
  165. var list = new List<SID_AND_ATTRIBUTES>();
  166. // Pin down variables
  167. GCHandle handle_pdwCntPublicACs = GCHandle.Alloc(size, GCHandleType.Pinned);
  168. GCHandle handle_ppACs = GCHandle.Alloc(arrayValue, GCHandleType.Pinned);
  169. uint retval = NetworkIsolationGetAppContainerConfig(out size, out arrayValue);
  170. var structSize = Marshal.SizeOf(typeof(SID_AND_ATTRIBUTES));
  171. for (var i = 0; i < size; i++)
  172. {
  173. var cur = (SID_AND_ATTRIBUTES)Marshal.PtrToStructure(arrayValue, typeof(SID_AND_ATTRIBUTES));
  174. list.Add(cur);
  175. arrayValue = new IntPtr((long)(arrayValue) + (long)(structSize));
  176. }
  177. //release pinned variables.
  178. handle_pdwCntPublicACs.Free();
  179. handle_ppACs.Free();
  180. return list;
  181. }
  182. private List<INET_FIREWALL_APP_CONTAINER> PI_NetworkIsolationEnumAppContainers()
  183. {
  184. IntPtr arrayValue = IntPtr.Zero;
  185. uint size = 0;
  186. var list = new List<INET_FIREWALL_APP_CONTAINER>();
  187. // Pin down variables
  188. GCHandle handle_pdwCntPublicACs = GCHandle.Alloc(size, GCHandleType.Pinned);
  189. GCHandle handle_ppACs = GCHandle.Alloc(arrayValue, GCHandleType.Pinned);
  190. //uint retval2 = NetworkIsolationGetAppContainerConfig( out size, out arrayValue);
  191. uint retval = NetworkIsolationEnumAppContainers((Int32)NETISO_FLAG.NETISO_FLAG_MAX, out size, out arrayValue);
  192. _pACs = arrayValue; //store the pointer so it can be freed when we close the form
  193. var structSize = Marshal.SizeOf(typeof(INET_FIREWALL_APP_CONTAINER));
  194. for (var i = 0; i < size; i++)
  195. {
  196. var cur = (INET_FIREWALL_APP_CONTAINER)Marshal.PtrToStructure(arrayValue, typeof(INET_FIREWALL_APP_CONTAINER));
  197. list.Add(cur);
  198. arrayValue = new IntPtr((long)(arrayValue) + (long)(structSize));
  199. }
  200. //release pinned variables.
  201. handle_pdwCntPublicACs.Free();
  202. handle_ppACs.Free();
  203. return list;
  204. }
  205. public bool SaveLoopbackState()
  206. {
  207. var countEnabled = CountEnabledLoopUtil();
  208. SID_AND_ATTRIBUTES[] arr = new SID_AND_ATTRIBUTES[countEnabled];
  209. int count = 0;
  210. for (int i = 0; i < Apps.Count; i++)
  211. {
  212. if (Apps[i].LoopUtil)
  213. {
  214. arr[count].Attributes = 0;
  215. //TO DO:
  216. IntPtr ptr;
  217. ConvertStringSidToSid(Apps[i].StringSid, out ptr);
  218. arr[count].Sid = ptr;
  219. count++;
  220. }
  221. }
  222. if (NetworkIsolationSetAppContainerConfig((uint)countEnabled, arr) == 0)
  223. {
  224. return true;
  225. }
  226. else
  227. { return false; }
  228. }
  229. private int CountEnabledLoopUtil()
  230. {
  231. var count = 0;
  232. for (int i = 0; i < Apps.Count; i++)
  233. {
  234. if (Apps[i].LoopUtil)
  235. {
  236. count++;
  237. }
  238. }
  239. return count;
  240. }
  241. }
  242. }