Assembly Load学习
Assembly Load学习
看到文章《利用Assembly Load & LoadFile绕过Applocker的分析总结》,想到不仅仅可以绕过applock,还可以用在一些免杀中,自己简单地看了下原理,就写个记录,后期还会有一些C#的一些利用手法。
原理
在.Net 中,程序集(Assembly)中保存了元数据(MetaData)信息,因此就可以通过分析元数据来获取程序集中的内容,比如类,方法,属性等,这大大方便了在运行时去动态创建实例。
主要用途:
动态加载DLL,实现插件机制。
实例化DLL中的类型。
执行后期绑定,访问在运行时创建的类型的方法。
动态加载EXE,实现动态调用EXE中函数方法,实现内存加载。
加载dll执行
新建.net类库项目
代码如下
using System;
namespace DllDemo
{
public class ClassGreenerycn
{
public void Hello()
{
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.FileName = "c:\\windows\\system32\\calc.exe";
p.Start();
}
}
}
然后新建个控制台应用项目调用这个dll
using System.Reflection;
namespace ReflectionDllDemo
{
class Program
{
static void Main(string[] args)
{
var asm = Assembly.LoadFile(@"C:\Users\f1veT\Desktop\1.dll");
var type = asm.GetType("DllDemo.ClassGreenerycn");
var instance = asm.CreateInstance("DllDemo.ClassGreenerycn");
var method = type.GetMethod("Hello");
method.Invoke(instance, null);
}
}
}
这是正常C#动态加载程序集的过程,那么我们可以吧shellcode编译成dll,然后利用exe加载也可以。
还有就是利用powershell加载exe/dll文件,达到绕过白名单的作用
powershell绕过白名单利用过程
加载C#代码
首先创建一个空白项目,这里使用.net2.0
using System;
using System.Collections.Generic;
using System.Text;
public class Program
{
public static void Main()
{
Console.WriteLine("Hey There From Main()");
}
}
public class aaa
{
public static void start()
{
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.FileName = "c:\\windows\\system32\\calc.exe";
p.Start();
}
}
接下来是有两种加载方法,一种是利用C#加载,还有就是使用powershell加载
Assembly Load:https://docs.microsoft.com/zh-cn/dotnet/api/system.reflection.assembly.load?view=netcore-3.1
Assembly Loadfile:https://docs.microsoft.com/zh-cn/dotnet/api/system.reflection.assembly.loadfile?view=netcore-3.1
https://3gstudent.github.io/3gstudent.github.io/%E5%88%A9%E7%94%A8Assembly-Load-&-LoadFile%E7%BB%95%E8%BF%87Applocker%E7%9A%84%E5%88%86%E6%9E%90%E6%80%BB%E7%BB%93/
https://bohops.com/2018/01/07/executing-commands-and-bypassing-applocker-with-powershell-diagnostic-scripts/