Add class to unload assembly

This commit is contained in:
NYAN CAT 2019-05-30 09:57:21 +03:00
parent a1b2ddb020
commit 54393a3267
3 changed files with 51 additions and 1 deletions

View File

@ -191,6 +191,7 @@
<Compile Include="Helper\IconInjector.cs" />
<Compile Include="Helper\ListViewColumnSorter.cs" />
<Compile Include="Helper\Methods.cs" />
<Compile Include="Helper\ReferenceLoader.cs" />
<Compile Include="MessagePack\BytesTools.cs" />
<Compile Include="MessagePack\MsgPack.cs" />
<Compile Include="MessagePack\MsgPackType.cs" />

View File

@ -9,6 +9,7 @@ using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Reflection;
using AsyncRAT_Sharp.Helper;
namespace AsyncRAT_Sharp
{
@ -58,7 +59,7 @@ namespace AsyncRAT_Sharp
{
try
{
Assembly.LoadFile(O.FileName);
new ReferenceLoader().AppDomainSetup(O.FileName);
isOK = true;
}
catch

View File

@ -0,0 +1,48 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace AsyncRAT_Sharp.Helper
{
public class ReferenceLoader : MarshalByRefObject
{
public string[] LoadReferences(string assemblyPath)
{
try
{
var assembly = Assembly.ReflectionOnlyLoadFrom(assemblyPath);
var paths = assembly.GetReferencedAssemblies().Select(x => x.FullName).ToArray();
return paths;
}
catch { return null; }
}
public void AppDomainSetup(string assemblyPath)
{
try
{
var settings = new AppDomainSetup
{
ApplicationBase = AppDomain.CurrentDomain.BaseDirectory,
};
var childDomain = AppDomain.CreateDomain(Guid.NewGuid().ToString(), null, settings);
var handle = Activator.CreateInstance(childDomain,
typeof(ReferenceLoader).Assembly.FullName,
typeof(ReferenceLoader).FullName,
false, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance, null, null, CultureInfo.CurrentCulture, new object[0]);
var loader = (ReferenceLoader)handle.Unwrap();
//This operation is executed in the new AppDomain
var paths = loader.LoadReferences(assemblyPath);
AppDomain.Unload(childDomain);
return;
}
catch { }
}
}
}