
Python based tool for generating Shellcode from PIC C
SHGenOB is a Python-based tool designed to generate shellcode from Position Independent Code (PIC) written in C. It automates the process of compiling C code, extracting the .text section, and optionally encrypting the resulting shellcode using XOR encryption.
This project is based entirely on this by hasherezade.
Important Note for SHGenOB Users: For this shellcode generation tool to work correctly, the C code in the input file must be written as Position Independent Code (PIC). The tool assumes that the provided code adheres to PIC principles and characteristics as described below.
Position Independent Code (PIC) is a type of machine code that can be executed regardless of its absolute memory address. This characteristic makes PIC particularly useful in creating shellcode, shared libraries, and certain types of malware. Key features of PIC include:
PIC is essential in scenarios where the exact load address of the code is not known in advance, making it a crucial concept in shellcode development and certain areas of systems programming. When using SHGenOB, ensure that your input C code follows these PIC principles to generate effective and reliable shellcode.
pip install pefile argparse
Run the script from the command line with the following syntax:
python shgenob.py --code-file <path_to_c_file> [--masm-file <path_to_masm_file>] [--debug] [--xor-key <key>]
Arguments:
--code-file or -cf: (Required) Path to the main.c file containing the PIC code--masm-file or -mf: (Optional) Path to external .masm file to include additional assembly logic in the shellcode--debug or -d: (Optional) Enable debug mode (skips cleanup of intermediate files)--xor-key or -xk: (Optional) XOR key for encrypting the shellcode (format: AA,BB,CC)Examples:
# Basic usage
python shgenob.py --code-file main.c
# With external MASM file
python shgenob.py --code-file main.c --masm-file custom_logic.masm
# With all options
python shgenob.py --code-file main.c --masm-file custom_logic.masm --debug --xor-key AA,BB,CC
save_to_file(content, filename): Saves given content to a file in the current directorycleanup(): Deletes intermediate files (.asm, .exe, .obj, .lnk) in the current directoryxor_encrypt(shellcode, key): Encrypts shellcode using XOR with the provided keyinsert_shellcode_into_loader(formatted_shellcode, loader_file): Inserts shellcode into the loader templateextract_text_section(file_path, key): Extracts the .text section from a PE file and optionally encrypts itfind_file(root_dir, file_name): Searches for a file in the given directory and its subdirectoriesrun_command_with_vcvars(command, vcvars_path): Runs a command with Visual Studio environment variables setcompile_cpp_file(cpp_file, vcvars_path, output_exe): Compiles a C++ file using Visual Studio compilermodify_asm_file(asm_file): Modifies the generated assembly file for shellcode compatibilitymain(cpp_file, args): Main function that orchestrates the shellcode generation processUse the --debug flag to skip the cleanup process and inspect intermediate files. This is useful for troubleshooting and understanding the shellcode generation process.
Contributions to improve SHGenOB are welcome. Please follow these steps: