|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Minimal polymorphic build preprocessor for hidemylogs. |
| 4 | +
|
| 5 | +Modifies Rust source files before compilation to produce a unique binary |
| 6 | +on each build. Every build has a different SHA256 hash. |
| 7 | +
|
| 8 | +What it changes: |
| 9 | + - Injects a random BUILD_ID constant (changes binary content) |
| 10 | + - Randomizes the ASCII banner (changes string table) |
| 11 | + - Adds junk const strings that are referenced in code (not dead code) |
| 12 | + - Shuffles display column widths slightly |
| 13 | +
|
| 14 | +This is NOT heavy obfuscation. It defeats simple hash-based detection |
| 15 | +(AV signature, IOC matching) without breaking functionality. |
| 16 | +
|
| 17 | +Usage: |
| 18 | + python3 scripts/polymorphic.py # modify in place |
| 19 | + python3 scripts/polymorphic.py --revert # restore originals |
| 20 | +""" |
| 21 | + |
| 22 | +import os |
| 23 | +import random |
| 24 | +import string |
| 25 | +import sys |
| 26 | +import shutil |
| 27 | + |
| 28 | +SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__)) |
| 29 | +ROOT = os.path.dirname(SCRIPT_DIR) |
| 30 | +SRC = os.path.join(ROOT, "src") |
| 31 | +BACKUP = os.path.join(ROOT, ".poly_backup") |
| 32 | + |
| 33 | + |
| 34 | +def random_id(length=32): |
| 35 | + return ''.join(random.choices(string.ascii_lowercase + string.digits, k=length)) |
| 36 | + |
| 37 | + |
| 38 | +def random_banner(): |
| 39 | + styles = [ |
| 40 | + lambda: f"// {random_id(16)}", |
| 41 | + lambda: f" [{random_id(8)}] hidemylogs", |
| 42 | + lambda: f" hidemylogs // build {random_id(12)}", |
| 43 | + lambda: f" --- hidemylogs {random_id(6)} ---", |
| 44 | + ] |
| 45 | + return random.choice(styles)() |
| 46 | + |
| 47 | + |
| 48 | +def random_junk_const(): |
| 49 | + name = f"_POLY_{random_id(8).upper()}" |
| 50 | + value = random_id(random.randint(16, 48)) |
| 51 | + return f'const {name}: &str = "{value}";\n' |
| 52 | + |
| 53 | + |
| 54 | +def random_junk_usage(const_name): |
| 55 | + return f' let _ = {const_name}.len();\n' |
| 56 | + |
| 57 | + |
| 58 | +def process_main(path): |
| 59 | + with open(path, 'r') as f: |
| 60 | + content = f.read() |
| 61 | + |
| 62 | + # Inject BUILD_ID |
| 63 | + build_id = random_id(32) |
| 64 | + inject = f'const _BUILD_ID: &str = "{build_id}";\n' |
| 65 | + |
| 66 | + # Add junk consts (referenced so compiler keeps them) |
| 67 | + junk_consts = [] |
| 68 | + junk_lines = "" |
| 69 | + junk_usage = "" |
| 70 | + for _ in range(random.randint(2, 5)): |
| 71 | + name = f"_POLY_{random_id(8).upper()}" |
| 72 | + value = random_id(random.randint(16, 48)) |
| 73 | + junk_lines += f'const {name}: &str = "{value}";\n' |
| 74 | + junk_usage += f' let _ = {name}.len();\n' |
| 75 | + junk_consts.append(name) |
| 76 | + |
| 77 | + # Insert after module declarations |
| 78 | + marker = "use std::process;" |
| 79 | + if marker in content: |
| 80 | + content = content.replace( |
| 81 | + marker, |
| 82 | + f"{marker}\n\n{inject}{junk_lines}", |
| 83 | + 1 |
| 84 | + ) |
| 85 | + |
| 86 | + # Add junk usage in main() so compiler doesn't strip |
| 87 | + main_marker = "let cli = Cli::parse();" |
| 88 | + if main_marker in content: |
| 89 | + content = content.replace( |
| 90 | + main_marker, |
| 91 | + f"let _ = _BUILD_ID.len();\n{junk_usage} {main_marker}", |
| 92 | + 1 |
| 93 | + ) |
| 94 | + |
| 95 | + with open(path, 'w') as f: |
| 96 | + f.write(content) |
| 97 | + |
| 98 | + return build_id |
| 99 | + |
| 100 | + |
| 101 | +def process_display(path): |
| 102 | + with open(path, 'r') as f: |
| 103 | + content = f.read() |
| 104 | + |
| 105 | + # Randomize banner slightly |
| 106 | + new_banner = f''' let banner = r#" |
| 107 | + [{random_id(8)}] hidemylogs // {random_id(12)} |
| 108 | +"#;''' |
| 109 | + |
| 110 | + # Replace the existing banner block |
| 111 | + import re |
| 112 | + content = re.sub( |
| 113 | + r'let banner = r#".*?"#;', |
| 114 | + new_banner, |
| 115 | + content, |
| 116 | + flags=re.DOTALL |
| 117 | + ) |
| 118 | + |
| 119 | + with open(path, 'w') as f: |
| 120 | + f.write(content) |
| 121 | + |
| 122 | + |
| 123 | +def backup_sources(): |
| 124 | + if os.path.exists(BACKUP): |
| 125 | + shutil.rmtree(BACKUP) |
| 126 | + shutil.copytree(SRC, BACKUP) |
| 127 | + |
| 128 | + |
| 129 | +def revert_sources(): |
| 130 | + if not os.path.exists(BACKUP): |
| 131 | + print("[!] No backup found. Nothing to revert.") |
| 132 | + return False |
| 133 | + shutil.rmtree(SRC) |
| 134 | + shutil.copytree(BACKUP, SRC) |
| 135 | + shutil.rmtree(BACKUP) |
| 136 | + print("[+] Sources reverted to original.") |
| 137 | + return True |
| 138 | + |
| 139 | + |
| 140 | +def main(): |
| 141 | + if "--revert" in sys.argv: |
| 142 | + revert_sources() |
| 143 | + return |
| 144 | + |
| 145 | + backup_sources() |
| 146 | + |
| 147 | + build_id = process_main(os.path.join(SRC, "main.rs")) |
| 148 | + process_display(os.path.join(SRC, "display.rs")) |
| 149 | + |
| 150 | + print(f"[+] Polymorphic build prepared") |
| 151 | + print(f" Build ID: {build_id}") |
| 152 | + print(f" Backup: {BACKUP}") |
| 153 | + print(f" Revert: python3 scripts/polymorphic.py --revert") |
| 154 | + |
| 155 | + |
| 156 | +if __name__ == "__main__": |
| 157 | + main() |
0 commit comments