# telepath9000/elf-packer architecture

> Architecture diagram of the GitHub repository telepath9000/elf-packer, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-06-19). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/telepath9000/elf-packer
- Repository: https://github.com/telepath9000/elf-packer

## Overview

##### Project type and purpose
- This is a native CLI tool (elf-packer) written in C and x86_64 assembly.
- Its purpose is to read an existing ELF64 binary, encrypt the .text section, inject a self-decrypting stub, and produce a packed ELF64 executable.

##### File structure
- Top level:
- Makefile: build and test targets
- README.md, LICENSE, .gitignore
- /packer.h: public header defining interfaces
- src/: implementation modules
- encrypt.c     – routines to encrypt/decrypt data
- inject.asm    – assembly stub that will be prepended for runtime decryption
- packer.c      – orchestrates the packing workflow, CLI entry point
- prepare_elf.c – reads/parses the input ELF64, sets up new segments/sections
- util.c        – helper functions (file I/O, error handling, ELF metadata)

##### README for additional insights
- Workflow: `packer [elf64_filepath]`
- Read and parse ELF64 file
- Encrypt .text section
- Inject the decryptor stub (inject.asm)
- Write out a new ELF64 binary
- It warns it only works on older kernels (implying no dynamic loader compatibility logic in source).

a. Main components:
1. CLI Interface (packer.c – main)
2. ELF Parser & Modifier (prepare_elf.c + packer.h)
3. Encryptor Module (encrypt.c)
4. Stub Injector (inject.asm + inject routines in util.c)
5. Utilities (util.c: file operations, logging, error management)
6. Build System (Makefile)
b. Relationships & data flow:
– User invokes CLI → packer.c
– packer.c calls ELF Parser to load input binary
– Parser returns in-memory structures → passed to Encryptor
– Encryptor encrypts .text, updates in-memory image
– Injector attaches the decrypt stub at ELF entry point
– Final image passed to Utilities for writing to disk
c. Architectural patterns / principles:
– Modular separation of concerns: parsing, encryption, injection, utilities
– Linear data pipeline: file input → transformation → file output
– Single-pass packing workflow
d. Key technologies / libraries:
– C language, x86_64 assembly
– ELF64 file format (manual parsing, no external ELF lib)
– Makefile-driven build & test targets

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Input and Output
    Input["Original ELF64"]:::io
    Output["Packed ELF64"]:::io

    %% Core Pipeline
    CLI["CLI Interface\n(src/packer.c)"]:::cModule
    Parser["ELF Parser & Modifier\n(src/prepare_elf.c,\ninclude/packer.h)"]:::cModule
    Encryptor["Encryptor Module\n(src/encrypt.c)"]:::cModule
    Injector["Stub Injector\n(src/inject.asm,\nsrc/util.c)"]:::assembly
    FileWriter["Utilities / File Writer\n(src/util.c)"]:::cModule

    %% Build System
    Build["Build System\n(Makefile)"]:::build

    %% Data Flow
    Input --> CLI --> Parser --> Encryptor --> Injector --> FileWriter --> Output

    %% Click Events
    click CLI "https://github.com/telepath9000/elf-packer/blob/master/src/packer.c"
    click Parser "https://github.com/telepath9000/elf-packer/blob/master/src/prepare_elf.c"
    click Parser "https://github.com/telepath9000/elf-packer/blob/master/include/packer.h"
    click Encryptor "https://github.com/telepath9000/elf-packer/blob/master/src/encrypt.c"
    click Injector "https://github.com/telepath9000/elf-packer/blob/master/src/inject.asm"
    click Injector "https://github.com/telepath9000/elf-packer/blob/master/src/util.c"
    click FileWriter "https://github.com/telepath9000/elf-packer/blob/master/src/util.c"
    click Build "https://github.com/telepath9000/elf-packer/tree/master/Makefile"

    %% Styles
    classDef cModule fill:#dae8fc,stroke:#6c8ebf;
    classDef assembly fill:#f8cecc,stroke:#b85450;
    classDef build fill:#e1e1e1,stroke:#888;
    classDef io fill:#d5e8d4,stroke:#82b366;
```
