Delphi 恶意软件分析实战:Incaseformat 样本 4 个定时器函数逆向与行为复现

📅 2026/7/8 19:59:02 👤 编程新知 🏷️ 技术资讯
Delphi 恶意软件分析实战:Incaseformat 样本 4 个定时器函数逆向与行为复现 Delphi 恶意软件逆向工程实战Incaseformat 定时器机制与行为模拟1. 逆向分析环境搭建与工具链配置在开始分析Incaseformat样本之前我们需要准备专业的逆向工程环境。对于Delphi编写的恶意软件常规的PE分析工具链需要特殊配置才能发挥最大效用。以下是推荐的实验环境搭建步骤基础工具集安装# IDA Pro 7.7 with Delphi analysis plugins # DeDe 3.50 for Delphi逆向专用工具 # PEiD 0.95 查壳工具需更新特征库 # x64dbg 最新版动态调试器 # Process Monitor 行为监控工具Delphi逆向专用配置在IDA中加载Delphi专用的FLIRT签名库dclrtl.sig/delphi.sig配置DeDe导出MAP文件的路径映射规则安装IDRInteractive Delphi Reconstructor辅助分析VCL组件关键工具对比表工具名称适用场景优势局限性IDA Pro静态反编译强大的反编译能力Delphi库函数识别较差DeDeDelphi专项分析自动识别VCL组件对新版Delphi支持有限IDRRTTI重建还原类结构需要调试信息PEiD快速查壳轻量级快速扫描特征库需手动更新提示分析Delphi程序时务必先通过DeDe生成MAP文件再导入IDA可显著提升函数识别准确率。典型的Delphi程序入口点特征为调用System.InitUnits初始化单元。2. Incaseformat样本静态结构解析通过PEiD确认样本未加壳后我们首先进行静态结构分析。该样本的典型特征如下基础信息{ FileType: PE32 executable (GUI), Compiler: Delphi 6.0-7.0, ImportTable: [ kernel32.dll::CopyFileA, GetDriveTypeA, advapi32.dll::RegSetValueExA, shell32.dll::ShellExecuteA ], Sections: [ {.text: Executable code}, {.data: Initialized data}, {.rsrc: Resource data} ] }Delphi特有结构VCL表单识别通过DeDe可识别出主表单TForm1及其关联的4个定时器组件RTTI信息样本保留了部分运行时类型信息有助于还原类方法调用关系事件处理函数典型的FormCreate和TimerXTimer事件处理函数结构关键函数定位技巧// 典型的Delphi事件处理函数特征 void __fastcall TForm1::Timer1Timer(TObject *Sender) { // 恶意代码逻辑 } // 通过交叉引用查找定时器设置代码 v3 GetTickCount(); SetTimer(hWnd, 1, v3 5000, (TIMERPROC)sub_404ABC);3. 定时器函数逆向与行为解析3.1 FormCreate初始化例程样本执行流程始于FormCreate函数其主要完成三项恶意操作文件自复制push offset Dest ; C:\\windows\\tsay.exe push offset Source ; 当前执行路径 call ds:CopyFileA注册表持久化reg_path SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\RunOnce reg_value msfsa C:\\windows\\tsay.exe RegSetValueExA(HKEY_LOCAL_MACHINE, reg_path, 0, REG_SZ, reg_value)副本链创建if (strcmp(current_exe, tsay.exe) 0) { CopyFileA(tsay.exe, C:\\windows\\ttry.exe, 1); ShellExecuteA(0, 0, ttry.exe, 0, 0, 0); }3.2 Timer1Timer磁盘枚举功能第一个定时器主要实现磁盘驱动器枚举功能行为逻辑流程图调用hpw_getdisksize检测磁盘存在性使用GetDriveTypeA过滤可移动/固定磁盘将有效盘符存入TStringList对象关键代码还原procedure TForm1.hpw_disk_type; var Drive: Char; begin for Drive : C to Z do if hpw_getdisksize(Drive) 0 then if GetDriveType(PChar(Drive :\)) in [DRIVE_REMOVABLE, DRIVE_FIXED] then DriveList.Add(Drive); end;3.3 Timer2Timer文件删除逻辑这是样本最具破坏性的功能模块其执行流程如下日期判断机制# 触发条件年份2009 且 月份3 且 日期∈[1,10,21,29] if (year 2009) and (month 3) and (day in [1,10,21,29]): delete_files()递归删除算法void hpw_delete(char drive) { sprintf(path, %c:\\*.*, drive); hFind FindFirstFile(path, findData); while (FindNextFile(hFind, findData)) { if (!strcmp(findData.cFileName, .) || !strcmp(findData.cFileName, ..)) continue; if (findData.dwFileAttributes FILE_ATTRIBUTE_DIRECTORY) hpw_delete(drive \\ findData.cFileName); else DeleteFile(drive \\ findData.cFileName); } RemoveDirectory(drive); }注意该函数会跳过C盘处理这是样本的自我保护机制3.4 Timer3Timer注册表篡改此定时器专门修改系统关键注册表项修改项列表隐藏已知文件扩展名HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced\HideFileExt 1隐藏系统文件HKCU\...\Advanced\Hidden 2禁用显示隐藏文件选项HKLM\...\SHOWALL\CheckedValue 0注册表操作代码特征mov edx, 80000001h ; HKEY_CURRENT_USER call TRegistry.SetRootKey mov edx, offset subkey call TRegistry.OpenKey mov edx, 1 mov ecx, offset value_name call TRegistry.WriteInteger3.5 Timer4Timer日志文件生成最后一个定时器在受害磁盘根目录创建标记文件def create_log(drives): for drive in drives: with open(f{drive}:\\incaseformat.log, wb) as f: f.write(bYour files have been deleted...)4. 动态行为模拟与验证4.1 Python行为模拟脚本基于逆向分析结果我们可以编写模拟脚本复现核心恶意行为import os import shutil import winreg from datetime import datetime class IncaseformatSimulator: def __init__(self): self.drives [] self.log_paths [] def enumerate_drives(self): 模拟Timer1功能枚举磁盘 for drive in range(ord(C), ord(Z)1): drive chr(drive) if os.path.exists(f{drive}:\\): self.drives.append(drive) return self.drives def check_trigger_date(self): 模拟Timer2日期判断逻辑 now datetime.now() return (now.year 2009 and now.month 3 and now.day in [1, 10, 21, 29]) def simulate_deletion(self, test_modeTrue): 模拟文件删除行为安全模式 for drive in self.drives: if drive C: # 跳过C盘 continue root f{drive}:\\TEST # 安全测试路径 os.makedirs(root, exist_okTrue) # 创建测试文件 with open(f{root}\\test.txt, w) as f: f.write(This is a test file) # 模拟删除 if not test_mode: shutil.rmtree(root) self.log_paths.append(f{drive}:\\incaseformat.log) def modify_registry(self): 模拟注册表修改 try: key winreg.OpenKey( winreg.HKEY_CURRENT_USER, Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Advanced, 0, winreg.KEY_WRITE) winreg.SetValueEx(key, Hidden, 0, winreg.REG_DWORD, 2) winreg.CloseKey(key) except Exception as e: print(fRegistry modification failed: {e}) def create_log_files(self): 创建日志标记文件 for path in self.log_paths: with open(path, w) as f: f.write(SIMULATION: Files would be deleted here)4.2 行为监控技巧使用Process Monitor配置过滤规则可有效捕获样本行为进程创建监控Operation: Process Create Path: tsay.exe OR ttry.exe文件操作监控Operation: WriteFile OR DeleteFile Path: *.log OR *\windows\tsay.exe注册表监控Operation: RegSetValue Path: *RunOnce* OR *Explorer\\Advanced*5. 防御策略与检测方案5.1 特征检测规则YARA规则示例rule Incaseformat_Malware { meta: description Detects Incaseformat malware variants author MalwareAnalyst strings: $delphi1 TForm1 wide $delphi2 Timer1Timer wide $str1 C:\\windows\\tsay.exe wide $str2 incaseformat.log wide $api1 GetDriveTypeA $api2 ShellExecuteA condition: uint16(0) 0x5A4D and 3 of ($str*) and all of ($api*) }5.2 行为阻断建议文件系统防护监控%SystemRoot%目录下的tsay.exe/ttry.exe创建拦截非系统进程对根目录的批量删除操作注册表防护[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced] Hiddendword:00000001 HideFileExtdword:00000000定时器检测# 检测可疑定时器设置 Get-WmiObject -Query SELECT * FROM __TimerEvent WHERE TimerId LIKE %incaseformat%6. 扩展分析与研究方向对于希望深入研究的分析师建议从以下方向扩展变种对比分析使用BinDiff比较不同版本的IMSecsPerDay全局变量差异分析火绒报告中提到的2009 vs 2014变种关键区别传播途径追踪通过VirusTotal检索关联样本分析可能的初始感染载体钓鱼邮件、漏洞利用等自动化分析增强# 使用CAPE沙箱进行自动化行为分析 from cape.core import Cape cape Cape() task_id cape.submit_sample(incaseformat.exe) report cape.get_report(task_id)在实际分析过程中建议在隔离环境中使用以下命令获取样本行为日志procmon.exe /AcceptEula /BackingFile log.pml /Quiet