LoopUtil.cs 9.9 KB

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