From 54393a32673ab0681116f87e0f6acc6768e57c80 Mon Sep 17 00:00:00 2001 From: NYAN CAT Date: Thu, 30 May 2019 09:57:21 +0300 Subject: [PATCH] Add class to unload assembly --- .../AsyncRAT-Sharp/AsyncRAT-Sharp.csproj | 1 + .../Forms/FormSendFileToMemory.cs | 3 +- .../AsyncRAT-Sharp/Helper/ReferenceLoader.cs | 48 +++++++++++++++++++ 3 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 AsyncRAT-C#/AsyncRAT-Sharp/Helper/ReferenceLoader.cs diff --git a/AsyncRAT-C#/AsyncRAT-Sharp/AsyncRAT-Sharp.csproj b/AsyncRAT-C#/AsyncRAT-Sharp/AsyncRAT-Sharp.csproj index d936dff..cc25964 100644 --- a/AsyncRAT-C#/AsyncRAT-Sharp/AsyncRAT-Sharp.csproj +++ b/AsyncRAT-C#/AsyncRAT-Sharp/AsyncRAT-Sharp.csproj @@ -191,6 +191,7 @@ + diff --git a/AsyncRAT-C#/AsyncRAT-Sharp/Forms/FormSendFileToMemory.cs b/AsyncRAT-C#/AsyncRAT-Sharp/Forms/FormSendFileToMemory.cs index 7a9ef15..df966da 100644 --- a/AsyncRAT-C#/AsyncRAT-Sharp/Forms/FormSendFileToMemory.cs +++ b/AsyncRAT-C#/AsyncRAT-Sharp/Forms/FormSendFileToMemory.cs @@ -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 diff --git a/AsyncRAT-C#/AsyncRAT-Sharp/Helper/ReferenceLoader.cs b/AsyncRAT-C#/AsyncRAT-Sharp/Helper/ReferenceLoader.cs new file mode 100644 index 0000000..2f2f2d9 --- /dev/null +++ b/AsyncRAT-C#/AsyncRAT-Sharp/Helper/ReferenceLoader.cs @@ -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 { } + } + } +}