#!/usr/bin/env python3 import os import re # 保留exploit的插件(真正有利用价值的) KEEP_EXPLOITS = ['ssh.go', 'redis.go', 'ms17010.go'] # 需要清理exploit的插件目录 PLUGINS_DIR = 'plugins/services' def remove_exploit_function(content, filename): """移除Exploit函数及其相关方法""" print(f"Processing {filename}") # 匹配并删除Exploit函数 exploit_pattern = r'// Exploit.*?\nfunc \([^)]+\) Exploit\([^{]*\{(?:[^{}]++|\{(?:[^{}]++|\{[^{}]*\})*\})*\}\n' content = re.sub(exploit_pattern, '', content, flags=re.MULTILINE | re.DOTALL) # 简化一点,匹配函数定义到下一个函数定义 lines = content.split('\n') output_lines = [] skip_lines = False brace_count = 0 for line in lines: # 检查是否是Exploit函数的开始 if 'func ' in line and 'Exploit(' in line: skip_lines = True brace_count = 0 continue if skip_lines: # 计算花括号 brace_count += line.count('{') brace_count -= line.count('}') # 如果花括号平衡且不在函数内,停止跳过 if brace_count <= 0 and '}' in line: skip_lines = False continue if not skip_lines: output_lines.append(line) return '\n'.join(output_lines) def main(): if not os.path.exists(PLUGINS_DIR): print(f"Directory {PLUGINS_DIR} not found") return for filename in os.listdir(PLUGINS_DIR): if not filename.endswith('.go') or filename in KEEP_EXPLOITS or filename == 'init.go': continue filepath = os.path.join(PLUGINS_DIR, filename) try: with open(filepath, 'r', encoding='utf-8') as f: content = f.read() # 检查是否有Exploit函数 if 'func ' in content and 'Exploit(' in content: # 移除Exploit函数 new_content = remove_exploit_function(content, filename) # 写回文件 with open(filepath, 'w', encoding='utf-8') as f: f.write(new_content) print(f"✅ Cleaned {filename}") else: print(f"⏭️ Skipped {filename} (no Exploit function)") except Exception as e: print(f"❌ Error processing {filename}: {e}") if __name__ == "__main__": main()