feat: upgrade to v0.5 - introduce x64 architecture support, type annotations, and robust NOP generation - #21
Open
anonymouschichvy wants to merge 18 commits into
Open
feat: upgrade to v0.5 - introduce x64 architecture support, type annotations, and robust NOP generation#21anonymouschichvy wants to merge 18 commits into
anonymouschichvy wants to merge 18 commits into
Conversation
Introduce metame/x64handler.py implementing a new X64Handler using Keystone with dynamic NOP generation and optimized mnemonic-indexed substitution rules. Update metame/r2parser.py to import and instantiate X64Handler (and improve disassembly error logging), add README notes about the x64 engine and its O(1) mnemonic indexing and dynamic NOP replacement, and delete the legacy metame/x86handler.py. The new handler preserves the replace_fcn_opcodes/assemble_code API and adds runtime NOP placeholder resolution and more robust matching/assembly checks.
Add input file existence check in CLI. Make R2Parser resilient to missing or incomplete 'bin' metadata, surface clearer errors, and read arch/bits safely before validating supported architectures/bits. Improve X64Handler robustness: add normalize_opcode, perform case-insensitive regex matching, normalize opcodes before matching, lowercase captured groups when substituting, shuffle candidate replacements and retry assembly while checking assembled size, and add clearer debug output on failures. Include new unit tests (tests/test_x64handler.py) to exercise initialization, get_nops, normalize_opcode and replace_fcn_opcodes behavior for 32/64-bit handlers.
Extend X64Handler with additional mutation variants and control-flow aliases. Added new random nop/arith substitutions (inc/dec, add/sub with zero/or/xor no-ops), shl/add expansions, and je/jz / jne/jnz swap rules; expanded the mutable mnemonic set for both 32/64-bit modes. assemble_code now accepts an optional addr and passes it to Keystone, and replace_fcn_opcodes uses the operation offset when assembling to preserve correct addresses. Adjusted random-choice branches and size-specific operand sequences to increase mutation diversity. Added test_replace_fcn_opcodes_advanced to verify replacements and size conservation.
Replace prev_ins_size with an explicit addr parameter for get_nops and update NOP generation to favor flag/register-safe multi-byte NOPs and size-preserving patterns. Changes include: using relative jmp targets computed from addr, adding safe NOPs (e.g. xchg ax, ax and nop dword ptr ...), correcting random branch ranges and reg usage, and threading addr into recursive get_nops calls and the nop placeholder resolver. README and tests updated to document and validate the new safe NOP choices and behavior.
metame/x64handler.py: Accept both 'offset' and 'addr' keys by introducing op_offset = op.get('offset', op.get('addr')) and use it when resolving NOP placeholders, assembling code, and recording replacements. Add a traceback print when Keystone assembly fails and debug is enabled to aid troubleshooting.
Revise README to reflect Version 0.5: add centered logo, simplify and reformat the features/comparison section, update the mermaid flowchart, and replace mutation examples with before/after signature analysis and a detection comparison table. Add new image assets under docs/ (logo.png, screen_bef.png, screen_aft.png, Screenshot 2026-07-03 221657.png) and remove legacy screenshots from screens/ (screen1.png, screen2.png).
Replace absolute local file URLs in README with repository-relative paths and update the LICENSE link. Remove a redundant Usage subsection and add a new "Metamorphic Transformation Process" section with explanatory text and an embedded image. Rename the screenshot to docs/metame-process.png for clarity and to match the new reference.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Overview
This pull request upgrades the metamorphic engine to version 0.5. The codebase has been extensively refactored to support both 32-bit (x86) and 64-bit (x64) executables with type-safe handlers, modernized packaging, safer inputs/parsing, and significantly improved NOP generation mechanics. In addition, the documentation has been updated with detailed instructions and a detection comparison catalog.
🛠️ Key Changes
1. Handler Redesign & Static Typing
x86handler.pywith x64handler.py.typing.Dict,List,Tuple, etc.) to the X64Handler class.%string formatting to modern Python f-strings.normalize_opcodeto parse and standardize whitespaces, commas, and casing variations in disassembler outputs.2. Advanced & Safe NOP Generation
(self, size, prev_ins_size=0)to(self, size: int, addr: int = 0)to support absolute instruction addresses, ensuring jumps are resolved correctly at patch time.nop dword ptr [rax]for 64-bit,nop dword ptr [eax]for 32-bit) which perform no operation and guarantee EFLAGS/RFLAGS stability.xchg ax, ax,xor reg, 0; nop, andadd reg, 0to increase metamorphic entropy without affecting register states.3. Radare2 Parser & CLI Enhancements
.get()rather than unsafe direct lookups, raising descriptive exceptions if architecture or bit details are missing.try-exceptblocks to prevent parser crashes on corrupted functions while reporting details.os.path.exists()before launchingradare2.4. Packaging & Documentation Overhaul
0.5and moved from deprecateddistutilstosetuptoolsin setup.py.screens/into a structured/docsdirectory.🧪 Verification Plan
Automated Verification
pip install -e ..metameon 32-bit and 64-bit ELF/PE binaries to ensure successful mutation patches.Manual Verification
metame -i non_existent.exe -o output.exeand verifying that the[ERROR] Input file does not existmessage is displayed cleanly.