SHGenOB 是一款基于 Python 的工具,旨在从用 C 编写的 Position Independent Code (PIC) 生成 shellcode。它自动化了编译 C 代码、提取 .text 段以及可选地使用 XOR 加密对生成的 shellcode 进行加密的过程。
本项目完全基于 hasherezade 的 此文档。
面向 SHGenOB 用户的重要提示:为了让该 shellcode 生成工具正常工作,输入文件中的 C 代码必须以 Position Independent Code (PIC) 形式编写。该工具假定所提供的代码遵循下文所述的 PIC 原理和特性。
Position Independent Code (PIC) 是一种无论加载到哪个绝对内存地址都能执行的机器代码。这一特性使 PIC 在创建 shellcode、共享库和某些类型的恶意软件时特别有用。PIC 的主要特性包括:
在无法预先知道代码确切加载地址的场景中,PIC 至关重要,因此它是 shellcode 开发和某些系统编程领域的关键概念。使用 SHGenOB 时,请确保你的输入 C 代码遵循这些 PIC 原理,以生成有效且可靠的 shellcode。
pip install pefile argparse
使用以下语法从命令行运行脚本:
python shgenob.py --code-file <path_to_c_file> [--masm-file <path_to_masm_file>] [--debug] [--xor-key <key>]
参数:
--code-file 或 -cf:(必需)包含 PIC 代码的 main.c 文件路径--masm-file 或 -mf:(可选)外部 .masm 文件路径,用于在 shellcode 中包含额外的汇编逻辑--debug 或 -d:(可选)启用调试模式(跳过中间文件的清理)--xor-key 或 -xk:(可选)用于加密 shellcode 的 XOR 密钥(格式:AA,BB,CC)示例:
# 基本用法
python shgenob.py --code-file main.c
# 使用外部 MASM 文件
python shgenob.py --code-file main.c --masm-file custom_logic.masm
# 使用所有选项
python shgenob.py --code-file main.c --masm-file custom_logic.masm --debug --xor-key AA,BB,CC
save_to_file(content, filename):将给定内容保存到当前目录下的文件中cleanup():删除当前目录中的中间文件(.asm、.exe、.obj、.lnk)xor_encrypt(shellcode, key):使用提供的密钥对 shellcode 进行 XOR 加密insert_shellcode_into_loader(formatted_shellcode, loader_file):将 shellcode 插入加载器模板extract_text_section(file_path, key):从 PE 文件中提取 .text 段并可选择对其进行加密find_file(root_dir, file_name):在给定目录及其子目录中搜索文件run_command_with_vcvars(command, vcvars_path):在设置 Visual Studio 环境变量的情况下运行命令compile_cpp_file(cpp_file, vcvars_path, output_exe):使用 Visual Studio 编译器编译 C++ 文件modify_asm_file(asm_file):修改生成的汇编文件以兼容 shellcodemain(cpp_file, args):编排 shellcode 生成过程的主函数使用 --debug 标志可跳过清理过程并检查中间文件。这对于排查故障和理解 shellcode 生成过程非常有用。
欢迎为改进 SHGenOB 做出贡献。请遵循以下步骤: