# cyberforce/pesidious architecture

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

- Interactive diagram: https://gitdiagram.com/cyberforce/pesidious
- Repository: https://github.com/cyberforce/pesidious

## Overview

##### Main Components
- Data Sources and Storage:
– The “Data” directory holds both benign and malware sample binaries. This is the input data source for the classifier and mutation processes.
- Feature Extraction and Preprocessing:
– Scripts like “extract_features.py” and RL_Features files (e.g., adversarial_imports_set.pk) feed the machine learning components.
- Deep Reinforcement Learning Environment:
– The “gym_malware” package (with its “envs” and “utils” subdirectories) adapts an OpenAI Gym-style environment for malware mutation. This contains the simulation environment where actions taken by the RL agent (e.g., modifying imports and sections) are defined.
– “models/rl-model.pt” and “Saved_models/GAN_model.model” indicate stored weights for RL and GAN modules.
- Generative Adversarial Networks (GAN) Components:
– The “malgan” package holds code for the generator, discriminator, and a detector. These components are responsible for producing adversarial samples and learning the distribution of feature mutations.
- Executable Mutation and PE Manipulation:
– Various builder scripts like “binary_builder.py”, “mutated_builder.py”, and “mutate.py” drive the modification of executables.
– The “portable-executable” folder contains C++ libraries (using PE Bliss) and precompiled executables (e.g., project-add-imports and project-add-sections) that actually perform the low-level PE file manipulations.
- Classifier and Evaluation:
– “classifier.py” is used to score both original and mutated malware files, providing feedback for training and evaluation.
- Supporting Tools and Configuration:
– The “pip_requirements” directory, “pesidious.config” file, and “setup.py” help configure the environment.
– Additional scripts such as “main_malgan.py” and “rl_train.py” likely drive experiments and training routines.

##### Relationships and Data Flow
- Data Flow:
– Input malware binaries (from the Data folder) are first processed through the classifier (using “classifier.py”) and feature extraction modules.
– The extracted features (and adversarial sets in RL_Features) feed into the RL agent running inside the gym_malware environment.
– The RL agent, using policies trained with PyTorch models (loaded from “Saved_models” or “models”), decides on specific modifications.
– The GAN (via “malgan”) introduces additional adversarial perturbations.
– The chosen modifications are then applied to the binary using builder scripts and C++ PE manipulation tools located in “portable-executable”.
– Finally, mutated binaries are re-evaluated using the classifier.
- Component Interactions:
– The RL environment interacts directly with the feature extractor and the mutation scripts.
– The GAN components (generator and discriminator) work in parallel or in sequence with the RL process to guide the mutation process.
– All these components are coordinated via driver scripts (like “mutate.py” and “main_malgan.py”) that orchestrate the execution pipeline.

##### Architectural Patterns and Technologies
- Modularity:
– The separation of concerns is clear: machine learning components (RL & GAN) are segregated from the executable manipulation layer.
- Client-Server (or Pipeline) Design:
– A “data ingestion–processing–mutation–evaluation” pipeline is evident.
- Use of Environment Abstraction:
– The gym_malware module leverages the OpenAI Gym interface, emphasizing simulation and iterative learning.
- Key Technologies:
– Python 3.6 (with potential version-specific dependencies).
– PyTorch for the deep learning models.
– LIEF and PE Bliss for handling the PE file binary structure and mutation.
– The use of virtual environments and specific pip versions (indicated in the README) for consistent dependency management.

##### Final Touches
- That every component is labeled with both its functional name and any significant file/module references.
- Dependencies, execution order, and data pipelines are clearly drawn.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Data Sources
    subgraph "Data Sources"
        DS["Data Sources"]:::data
    end

    %% Feature Extraction & Preprocessing
    subgraph "Feature Extraction & Preprocessing"
        EF1["extract_features.py"]:::ml
        EF2["RL_Features"]:::ml
    end

    %% Deep Reinforcement Learning Environment
    subgraph "Deep Reinforcement Learning Environment"
        RL1["gym_malware"]:::ml
        RL2["rl-model.pt"]:::ml
        RL3["rl_train.py"]:::ml
    end

    %% Generative Adversarial Networks
    subgraph "Generative Adversarial Networks"
        GAN1["malgan"]:::gan
        GAN2["GAN_model.model"]:::gan
        GAN3["main_malgan.py"]:::gan
    end

    %% Executable Mutation Engine & PE Manipulation
    subgraph "Executable Mutation Engine & PE Manipulation"
        M1["binary_builder.py"]:::mutate
        M2["mutated_builder.py"]:::mutate
        M3["mutate.py"]:::mutate
        M4["portable-executable"]:::mutate
    end

    %% Classifier & Evaluation
    subgraph "Classifier & Evaluation"
        C1["classifier.py"]:::classifier
    end

    %% Supporting Tools & Configuration
    subgraph "Supporting Tools & Configuration"
        S1["pesidious.config"]:::config
        S2["pip_requirements"]:::config
        S3["setup.py"]:::config
    end

    %% Connections
    DS -->|"feeds"| EF1
    DS -->|"feeds"| EF2
    EF1 -->|"extracted features"| RL1
    EF2 -->|"extracted features"| RL1

    %% Interaction between RL Environment and GAN components
    RL1 -->|"guides"| GAN1
    GAN1 -->|"influences"| RL1

    %% Both RL and GAN influence the mutation process
    RL1 -->|"decision output"| M3
    GAN1 -->|"adversarial input"| M3

    %% Mutation generates modified binaries for evaluation
    M3 -->|"mutated binary"| C1

    %% Feedback loop from Classifier to Feature Extraction
    C1 -->|"feedback"| EF1

    %% Styling
    classDef data fill:#AED6F1,stroke:#1B4F72,stroke-width:2px;
    classDef ml fill:#A9DFBF,stroke:#196F3D,stroke-width:2px;
    classDef gan fill:#F9E79F,stroke:#B7950B,stroke-width:2px;
    classDef mutate fill:#F5CBA7,stroke:#873600,stroke-width:2px;
    classDef classifier fill:#F1948A,stroke:#922B21,stroke-width:2px;
    classDef config fill:#D7BDE2,stroke:#6C3483,stroke-width:2px;

    %% Click Events
    click DS "https://github.com/cyberforce/pesidious/tree/master/Data/"
    click EF1 "https://github.com/cyberforce/pesidious/blob/master/extract_features.py"
    click EF2 "https://github.com/cyberforce/pesidious/tree/master/RL_Features/"
    click RL1 "https://github.com/cyberforce/pesidious/tree/master/gym_malware/"
    click RL2 "https://github.com/cyberforce/pesidious/blob/master/models/rl-model.pt"
    click RL3 "https://github.com/cyberforce/pesidious/blob/master/rl_train.py"
    click GAN1 "https://github.com/cyberforce/pesidious/tree/master/malgan/"
    click GAN2 "https://github.com/cyberforce/pesidious/blob/master/Saved_models/GAN_model.model"
    click GAN3 "https://github.com/cyberforce/pesidious/blob/master/main_malgan.py"
    click M1 "https://github.com/cyberforce/pesidious/blob/master/binary_builder.py"
    click M2 "https://github.com/cyberforce/pesidious/blob/master/mutated_builder.py"
    click M3 "https://github.com/cyberforce/pesidious/blob/master/mutate.py"
    click M4 "https://github.com/cyberforce/pesidious/tree/master/portable-executable/"
    click C1 "https://github.com/cyberforce/pesidious/blob/master/classifier.py"
    click S1 "https://github.com/cyberforce/pesidious/blob/master/pesidious.config"
    click S2 "https://github.com/cyberforce/pesidious/tree/master/pip_requirements/"
    click S3 "https://github.com/cyberforce/pesidious/blob/master/setup.py"
```
