Advertisement
Guest User

Untitled

a guest
Mar 31st, 2017
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 12.88 KB | None | 0 0
  1. ' ***********************************************************************
  2. ' Author   : Elektro
  3. ' Modified : 11-January-2017
  4. ' ***********************************************************************
  5.  
  6. #Region " Public Members Summary "
  7.  
  8. #Region " Methods "
  9.  
  10. ' Execute(Byte())
  11. ' Execute(Byte(), Object())
  12.  
  13. ' ExecuteSafe(Byte())
  14. ' ExecuteSafe(Byte(), Object())
  15.  
  16. #End Region
  17.  
  18. #End Region
  19.  
  20. #Region " Option Statements "
  21.  
  22. Option Strict On
  23. Option Explicit On
  24. Option Infer Off
  25.  
  26. #End Region
  27.  
  28. #Region " Imports "
  29.  
  30. Imports System.Reflection
  31. Imports System.Security
  32. Imports System.Security.Permissions
  33.  
  34. #End Region
  35.  
  36. #Region " Resource Util "
  37.  
  38. Namespace Resources.Tools
  39.  
  40.     Partial Public NotInheritable Class ResourceUtil
  41.  
  42. #Region " Public Methods "
  43.  
  44.         ''' ----------------------------------------------------------------------------------------------------
  45.         ''' <summary>
  46.         ''' Loads a resource in memory and executes its main entrypoint.
  47.         ''' <para></para>
  48.         ''' The resource must be a .NET assembly.
  49.         ''' </summary>
  50.         ''' ----------------------------------------------------------------------------------------------------
  51.         ''' <example> This is a code example.
  52.         ''' <code>
  53.         ''' Execute(My.Resources.MyProgram)
  54.         ''' </code>
  55.         ''' </example>
  56.         ''' ----------------------------------------------------------------------------------------------------
  57.         ''' <param name="resource">
  58.         ''' The resource to execute.
  59.         ''' </param>
  60.         ''' ----------------------------------------------------------------------------------------------------
  61.         ''' <exception cref="EntryPointNotFoundException">
  62.         ''' Entrypoint not found in the specified resource. Are you sure it is a .NET assembly?
  63.         ''' </exception>
  64.         ''' ----------------------------------------------------------------------------------------------------
  65.         <DebuggerStepThrough>
  66.         Public Shared Sub Execute(ByVal resource As Byte())
  67.             ResourceUtil.Execute(resource, Nothing)
  68.         End Sub
  69.  
  70.         ''' ----------------------------------------------------------------------------------------------------
  71.         ''' <summary>
  72.         ''' Loads a resource in memory and executes its main entrypoint.
  73.         ''' <para></para>
  74.         ''' The resource must be a .NET assembly.
  75.         ''' </summary>
  76.         ''' ----------------------------------------------------------------------------------------------------
  77.         ''' <example> This is a code example.
  78.         ''' <code>
  79.         ''' Execute(My.Resources.MyProgram, {"Parameter 1", "Parameter 2", "etc..."})
  80.         ''' </code>
  81.         ''' </example>
  82.         ''' ----------------------------------------------------------------------------------------------------
  83.         ''' <param name="resource">
  84.         ''' The resource to execute.
  85.         ''' </param>
  86.         '''
  87.         ''' <param name="parameters">
  88.         ''' The parameters to pass to the method executed.
  89.         ''' </param>
  90.         ''' ----------------------------------------------------------------------------------------------------
  91.         ''' <exception cref="EntryPointNotFoundException">
  92.         ''' Entrypoint not found in the specified resource. Are you sure it is a .NET assembly?
  93.         ''' </exception>
  94.         ''' ----------------------------------------------------------------------------------------------------
  95.         <DebuggerStepThrough>
  96.         Public Shared Sub Execute(ByVal resource As Byte(), ByVal parameters As Object())
  97.             Dim helper As New ExecuteHelper()
  98.             helper.LoadAssembly(resource, parameters)
  99.         End Sub
  100.  
  101.         ''' ----------------------------------------------------------------------------------------------------
  102.         ''' <summary>
  103.         ''' Loads a resource in memory and executes its main entrypoint.
  104.         ''' <para></para>
  105.         ''' The resource must be a .NET assembly.
  106.         ''' <para></para>
  107.         ''' If the loaded assembly attempts to force an application termination
  108.         ''' by for example internaly calling <see cref="Environment.Exit"/>, then the call is catched and ignored.
  109.         ''' <para></para>
  110.         ''' The downside is that the loaded assembly will not be able to call (P/Invoke) unmanaged code. You are advised.
  111.         ''' <para></para>
  112.         ''' *** Note that this is a experimental methodology. ***
  113.         ''' </summary>
  114.         ''' ----------------------------------------------------------------------------------------------------
  115.         ''' <example> This is a code example.
  116.         ''' <code>
  117.         ''' ExecuteSafe(My.Resources.MyProgram)
  118.         ''' </code>
  119.         ''' </example>
  120.         ''' ----------------------------------------------------------------------------------------------------
  121.         ''' <param name="resource">
  122.         ''' The resource to execute.
  123.         ''' </param>
  124.         ''' ----------------------------------------------------------------------------------------------------
  125.         ''' <exception cref="EntryPointNotFoundException">
  126.         ''' Entrypoint not found in the specified resource. Are you sure it is a .NET assembly?
  127.         ''' </exception>
  128.         ''' ----------------------------------------------------------------------------------------------------
  129.         <DebuggerStepThrough>
  130.         Public Shared Sub ExecuteSafe(ByVal resource As Byte())
  131.             ResourceUtil.ExecuteSafe(resource, Nothing)
  132.         End Sub
  133.  
  134.         ''' ----------------------------------------------------------------------------------------------------
  135.         ''' <summary>
  136.         ''' Loads a resource in memory and executes its main entrypoint.
  137.         ''' <para></para>
  138.         ''' The resource must be a .NET assembly.
  139.         ''' <para></para>
  140.         ''' If the loaded assembly attempts to force an application termination
  141.         ''' by for example internaly calling <see cref="Environment.Exit"/>, then the call is catched and ignored.
  142.         ''' <para></para>
  143.         ''' The downside is that the loaded assembly will not be able to call (P/Invoke) unmanaged code. You are advised.
  144.         ''' <para></para>
  145.         ''' *** Note that this is a experimental methodology. ***
  146.         ''' </summary>
  147.         ''' ----------------------------------------------------------------------------------------------------
  148.         ''' <example> This is a code example.
  149.         ''' <code>
  150.         ''' ExecuteSafe(My.Resources.MyProgram, {"Parameter 1", "Parameter 2", "etc..."})
  151.         ''' </code>
  152.         ''' </example>
  153.         ''' ----------------------------------------------------------------------------------------------------
  154.         ''' <param name="resource">
  155.         ''' The resource to execute.
  156.         ''' </param>
  157.         '''
  158.         ''' <param name="parameters">
  159.         ''' The parameters to pass to the method executed.
  160.         ''' </param>
  161.         ''' ----------------------------------------------------------------------------------------------------
  162.         ''' <exception cref="EntryPointNotFoundException">
  163.         ''' Entrypoint not found in the specified resource. Are you sure it is a .NET assembly?
  164.         ''' </exception>
  165.         ''' ----------------------------------------------------------------------------------------------------
  166.         <DebuggerStepThrough>
  167.         Public Shared Sub ExecuteSafe(ByVal resource As Byte(), ByVal parameters As Object())
  168.  
  169.             ' This is where the main executable resides. For more info on this, see "Remarks" in
  170.             ' https://msdn.microsoft.com/en-us/library/system.appdomainsetup.applicationbase(v=vs.110).aspx#Anchor_1
  171.             Dim adSetup As New AppDomainSetup()
  172.             adSetup.ApplicationBase = AppDomain.CurrentDomain.BaseDirectory
  173.  
  174.             ' Permissions of the AppDomain/what it can do.
  175.             Dim permission As New PermissionSet(PermissionState.None)
  176.             With permission
  177.                 ' All SecurityPermission flags EXCEPT UnmanagedCode, which is required by Environment.Exit() method.
  178.                 .AddPermission(New SecurityPermission(SecurityPermissionFlag.AllFlags And Not SecurityPermissionFlag.UnmanagedCode))
  179.                 .AddPermission(New EnvironmentPermission(PermissionState.Unrestricted))
  180.                 .AddPermission(New EventLogPermission(PermissionState.Unrestricted))
  181.                 .AddPermission(New FileDialogPermission(PermissionState.Unrestricted))
  182.                 .AddPermission(New FileIOPermission(PermissionState.Unrestricted))
  183.                 .AddPermission(New GacIdentityPermission(PermissionState.Unrestricted))
  184.                 .AddPermission(New IsolatedStorageFilePermission(PermissionState.Unrestricted))
  185.                 .AddPermission(New KeyContainerPermission(PermissionState.Unrestricted))
  186.                 .AddPermission(New PerformanceCounterPermission(PermissionState.Unrestricted))
  187.                 .AddPermission(New PrincipalPermission(PermissionState.Unrestricted))
  188.                 .AddPermission(New ReflectionPermission(PermissionState.Unrestricted))
  189.                 .AddPermission(New RegistryPermission(PermissionState.Unrestricted))
  190.                 .AddPermission(New SiteIdentityPermission(PermissionState.Unrestricted))
  191.                 .AddPermission(New StorePermission(PermissionState.Unrestricted))
  192.                 .AddPermission(New TypeDescriptorPermission(PermissionState.Unrestricted))
  193.                 .AddPermission(New UIPermission(PermissionState.Unrestricted))
  194.                 .AddPermission(New UrlIdentityPermission(PermissionState.Unrestricted))
  195.                 .AddPermission(New ZoneIdentityPermission(PermissionState.Unrestricted))
  196.             End With
  197.  
  198.             ' Our sandboxed AppDomain.
  199.             Dim domain As AppDomain = AppDomain.CreateDomain("SomeGenericName", Nothing, adSetup, permission, Nothing)
  200.  
  201.             Try
  202.                 ' Create an instance of Helper in the sandboxed AppDomain.
  203.                 Dim helper As ExecuteHelper = DirectCast(domain.CreateInstanceAndUnwrap(GetType(ExecuteHelper).Assembly.FullName, GetType(ExecuteHelper).FullName), ExecuteHelper)
  204.                 helper.LoadAssembly(resource, parameters)
  205.  
  206.             Catch ex As TargetInvocationException When (ex.InnerException.GetType() = GetType(SecurityException))
  207.                 ' Some kind of permissions issue occured here, possibly an attempt to call Environment.Exit() or call (P/Invoke) unmanaged code.
  208.                 ' Do Nothing.
  209.  
  210.             Catch ex As Exception
  211.                 Throw
  212.  
  213.             End Try
  214.  
  215.         End Sub
  216.  
  217. #End Region
  218.  
  219.     End Class
  220.  
  221.     ''' ----------------------------------------------------------------------------------------------------
  222.     ''' <summary>
  223.     ''' A class that serves to execute assemblies that are stored as application resource (eg. My.Resources.MyProgram1 )
  224.     ''' </summary>
  225.     ''' ----------------------------------------------------------------------------------------------------
  226.     Friend NotInheritable Class ExecuteHelper : Inherits MarshalByRefObject
  227.  
  228.         ''' ----------------------------------------------------------------------------------------------------
  229.         ''' <summary>
  230.         ''' Loads the assembly.
  231.         ''' </summary>
  232.         ''' ----------------------------------------------------------------------------------------------------
  233.         ''' <param name="bytes">
  234.         ''' The bytes of the assembly/resource to execute.
  235.         ''' </param>
  236.         '''
  237.         ''' <param name="parameters">
  238.         ''' The parameters to pass to the method executed.
  239.         ''' </param>
  240.         ''' ----------------------------------------------------------------------------------------------------
  241.         ''' <exception cref="EntryPointNotFoundException">
  242.         ''' Entrypoint not found in the specified resource. Are you sure it is a .NET assembly?
  243.         ''' </exception>
  244.         ''' ----------------------------------------------------------------------------------------------------
  245.         Public Sub LoadAssembly(ByVal bytes As Byte(), ByVal parameters As Object())
  246.             Dim ass As Assembly = Assembly.Load(bytes)
  247.             Dim method As MethodInfo = ass.EntryPoint
  248.  
  249.             If (method IsNot Nothing) Then
  250.                 Dim instance As Object = ass.CreateInstance(method.Name)
  251.                 method.Invoke(instance, parameters)
  252.                 If (instance IsNot Nothing) AndAlso (instance.GetType().GetInterfaces.Contains(GetType(IDisposable))) Then
  253.                     DirectCast(instance, IDisposable).Dispose()
  254.                 End If
  255.                 instance = Nothing
  256.                 method = Nothing
  257.                 ass = Nothing
  258.  
  259.             Else
  260.                 Throw New EntryPointNotFoundException("Entrypoint not found in the specified resource. Are you sure it is a .NET assembly?")
  261.  
  262.             End If
  263.         End Sub
  264.  
  265.     End Class
  266.  
  267. End Namespace
  268.  
  269. #End Region
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement