系统安全工具软件二进制安全 [2021]AppDomainManager劫持与powershell hook解码 在红队渗透中,有一种持久化技术经常被使用,那就是AppDomainManager劫持,具体原理本章不再叙述,简单来说,在.net目录新建一个同样文件的.config文件,就可以控制.net的appdomainmanger加载,攻击者往往利用这个特性做后门 ```asp ``` 在安全产品对抗中,安全产品时刻面临着powershell的挑战原因很简单,powershell这玩意,简直是小黑和apt组织狂喜,各种花里胡哨的加密与解密去绕过edr/av,并且微软的amsi接口也各种拉胯,甚至是可以被powershell自己绕过,所以就不能指望微软了,要自己干 -powershell的hook ### 如何获取干净的powershell指令 powershell本质上是基于.net的解释器,在powershell脚本代码执行后,会被编译成il代码继续执行,因此为了解决powershell被混淆的问题,我们需要想办法hook powershell的编译代码,好消息是微软的.net相当于开源,所以很快就能找到这个函数: ```asp System.Management.Automation.CompiledScriptBlockData ``` ![](https://key08.com/usr/uploads/2021/11/1354091260.png) 顺便吐槽一下微软的amsi: ![](https://key08.com/usr/uploads/2021/11/2089727795.png) ### 如何hook 在我们劫持了AppDomainManager启动后,我们需要干的第一件事就是监听程序集加载: ```cpp private const BindingFlags anyType = BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic; private static readonly AssemblyLoadEventHandler HookAssemblyLoadEventHandler = new AssemblyLoadEventHandler(Rm_OnAssemblyLoad); private static void Rm_OnAssemblyLoad(object sender, AssemblyLoadEventArgs args) { string assemblyName = args.LoadedAssembly.GetName().Name; string assemblyFullName = args.LoadedAssembly.Location; if (assemblyName == "System.Management.Automation") { ...这里就是程序集加载事件了 ``` 之后,我们要得到此函数的指针 ![](https://key08.com/usr/uploads/2021/11/3051046094.png) ```cpp MethodInfo ReallyCompile = targetMethodClass.GetMethod("ReallyCompile", anyType, null, targetMethodType, null); ... RuntimeHelpers.PrepareMethod(ReallyCompile.MethodHandle); ... IntPtr TargetAddress = ReallyCompile.MethodHandle.GetFunctionPointer(); ``` 然后hook他 ```cpp if (!MinHook.InstallHook(TargetAddress, HookAddress, TrampolineAddress)) { throw new ArgumentNullException("[HUOJI] Min Hook Fail"); //仅供测试,一般要return } ``` 至此,我们就hook了powershell并且拿到了正确信息: ```cpp public void HookReallyCompile(bool optimize) { string Code = this.ToString(); Helper.DbgPrint(Code); .... ``` ![](https://key08.com/usr/uploads/2021/11/1201952192.png) 阅读全文 2021-11-22 huoji 0 条评论