Worm.WhBoy 专杀工具编写:基于3类文件感染特征的Python实现 Worm.WhBoy 专杀工具开发实战基于Python的感染特征检测与修复1. 病毒行为分析与专杀工具设计思路熊猫烧香病毒Worm.WhBoy作为2006年肆虐网络的典型蠕虫病毒其感染机制具有鲜明的特征。该病毒主要通过三种方式传播可执行文件感染在PE文件尾部追加病毒体并添加WhBoy标记Web文件篡改在HTML/ASP等文件尾部插入加密的iframe代码自动传播机制通过U盘autorun.inf和网络共享传播专杀工具核心功能设计应包含以下模块class WormWhBoyKiller: def __init__(self): self.exe_signatures [bWhBoy, bWHBOY] # 可执行文件感染标记 self.web_patterns [ # Web文件特征正则表达式 riframe.*?src.*?.*?, rdocument\.write\(unescape\( ] def scan_disk(self, path): # 磁盘扫描入口 pass def check_exe(self, filepath): # 检测可执行文件感染 pass def check_web(self, filepath): # 检测Web文件感染 pass def clean_exe(self, filepath): # 修复可执行文件 pass def clean_web(self, filepath): # 清理Web文件 pass2. 可执行文件感染检测与修复病毒对PE文件的感染会在文件尾部添加特定结构[病毒体][原始PE文件][WhBoy标记][原始文件名][0x2标记][原始文件大小][0x1标记]检测算法实现def check_exe(self, filepath): with open(filepath, rb) as f: data f.read() # 检查文件尾部标记 for sig in self.exe_signatures: pos data.rfind(sig) if pos ! -1: # 提取原始文件大小 size_pos pos len(sig) if len(data) size_pos 5: orig_size int.from_bytes( data[size_pos1:size_pos5], byteorderlittle ) return { infected: True, marker_pos: pos, original_size: orig_size } return {infected: False}修复流程关键步骤定位病毒标记位置提取原始文件大小信息截取原始PE文件内容重建文件头校验信息def clean_exe(self, filepath): result self.check_exe(filepath) if not result[infected]: return False with open(filepath, rb) as f: data f.read() # 截取原始文件内容 orig_data data[:result[original_size]] # 重建PE头校验 if orig_data.startswith(bMZ): pe_header_offset int.from_bytes(orig_data[0x3C:0x3C4], little) if pe_header_offset 0x200 len(orig_data): # 更新PE头中的SizeOfImage new_size self.calculate_pe_size(orig_data, pe_header_offset) orig_data self.update_pe_header(orig_data, pe_header_offset, new_size) # 写回修复后的文件 with open(filepath, wb) as f: f.write(orig_data) return True3. Web文件感染处理方案病毒对Web文件的感染特征表现为文件尾部添加加密的iframe代码可能包含document.write(unescape(等JavaScript解码语句通常指向恶意网站URL检测与清理实现def clean_web(self, filepath): with open(filepath, r, encodingutf-8, errorsignore) as f: content f.read() cleaned False # 处理iframe注入 for pattern in self.web_patterns: content, count re.subn(pattern, , content, flagsre.I) if count 0: cleaned True # 处理特定加密脚本 malicious_scripts [ var%20s%3Ddocument.createElement, eval(function(p,a,c,k,e,d) ] for script in malicious_scripts: if script in content: content content.replace(script, ) cleaned True if cleaned: with open(filepath, w, encodingutf-8) as f: f.write(content) return cleanedWeb文件修复注意事项不同编码格式的处理UTF-8/GBK多行恶意代码的识别保留原始文件的有效内容特殊字符的转义处理4. 完整专杀工具实现与测试整合各模块后的完整工具架构WormWhBoyKiller/ ├── core/ │ ├── scanner.py # 扫描引擎 │ ├── cleaner.py # 清理模块 │ └── utils.py # 辅助函数 ├── tests/ # 测试用例 │ ├── infected_exe/ │ └── infected_web/ └── main.py # 命令行入口命令行界面设计import argparse def main(): parser argparse.ArgumentParser( descriptionWorm.WhBoy专杀工具 v1.0, formatter_classargparse.RawTextHelpFormatter ) parser.add_argument(path, help要扫描的目录路径) parser.add_argument(-q, --quick, actionstore_true, help快速扫描模式) parser.add_argument(-l, --log, metavarFILE, help指定日志输出文件) args parser.parse_args() killer WormWhBoyKiller() result killer.scan_disk(args.path, quick_modeargs.quick) print(f扫描完成\n f- 已检查文件: {result[total_files]}\n f- 感染文件: {result[infected_files]}\n f- 成功修复: {result[cleaned_files]}) if args.log: with open(args.log, w) as f: json.dump(result, f, indent2) if __name__ __main__: main()测试验证方法准备已知感染样本运行专杀工具扫描验证文件是否被正确修复检查原始功能是否恢复# 测试命令示例 python main.py /test_samples --log scan_report.json性能优化技巧使用多线程加速文件扫描实现文件签名缓存避免重复检查针对大文件实现流式处理添加排除目录支持from concurrent.futures import ThreadPoolExecutor def parallel_scan(self, path, max_workers4): infected_files [] with ThreadPoolExecutor(max_workersmax_workers) as executor: for root, _, files in os.walk(path): for file in files: filepath os.path.join(root, file) future executor.submit(self.check_file, filepath) if future.result(): infected_files.append(filepath) return infected_files5. 防御增强与扩展功能主动防御措施实现注册表监控防止病毒自启动文件系统监控实时防护可疑进程检测机制def enable_protection(self): # 监控关键注册表项 reg_keys [ rSoftware\Microsoft\Windows\CurrentVersion\Run, rSoftware\Microsoft\Windows\CurrentVersion\RunOnce ] for key in reg_keys: self.monitor_registry(key) # 设置文件系统监控 from watchdog.observers import Observer from watchdog.events import FileSystemEventHandler class VirusHandler(FileSystemEventHandler): def on_modified(self, event): if not event.is_directory: self.check_file(event.src_path) observer Observer() observer.schedule(VirusHandler(), path/, recursiveTrue) observer.start()工具扩展方向支持更多变种病毒的检测添加云端特征库更新实现分布式扫描能力开发GUI管理界面最佳实践建议定期更新特征库扫描前创建系统还原点关键目录设置只读权限结合其他安全工具使用在实际部署中发现结合文件哈希校验可以显著提高检测准确率。对于重要系统文件建议维护一个已知安全版本的哈希数据库作为比对基准。