# 0xfke/malware-detection-and-analysis-using-machine-learning architecture

> Architecture diagram of the GitHub repository 0xfke/malware-detection-and-analysis-using-machine-learning, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-16). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/0xfke/malware-detection-and-analysis-using-machine-learning
- Repository: https://github.com/0xfke/malware-detection-and-analysis-using-machine-learning

## Overview

##### Project Type and Purpose
- This is a full-stack Flask WebApp for Malware Detection and Analysis.
- It combines two primary capabilities:
a. Static Analysis via the VirusTotal API (“VT” component).
b. Machine-Learning-Based Detection of executables using a pre-trained Random Forest model (“ML_model” component).
- There is also scaffolding for future “Dynamic Analysis” and dataset artifacts.

##### File Structure Analysis
- Top-level files
- app.py / appV1.py: main Flask entry points
- requirements.txt: dependencies list
- README.md, CONTRIBUTING.md, LICENSE
- Directories
- VT/ : static-analysis Blueprint
– app.py: Flask routes for VirusTotal uploads
– templates/: HTML views (index, details, result)
- ML_model/ : serialized model artifacts (.pkl)
- Dataset/ : CSVs for model training and feature filtering
- Dynamic_Analysis/: placeholder for dynamic analysis scripts (config.py)
- templates/: fallback or main app HTML (index.html, result.html)
- Image/, Video/: demo assets

##### Key Architectural Insights from README
- Two parallel analysis flows managed by the same Flask server:
a. Static Flow: user → VT blueprint → VirusTotal API → results
b. ML Flow: user → ML route in app.py → feature_extraction.py → RandomForest model → results
- No traditional database; results are transient and rendered in templates.
- Model training and dataset preparation steps are offline; model deployment uses joblib-serialized artifacts.

##### Main Components
a. User Interface
– Browser with two entry points: Static Analysis page and ML Analysis page
– UI templates (index.html, result.html, details.html)
b. Web Server (Flask Monolith)
– app.py (or appV1.py): central server that imports VT blueprint and ML routes
– feature_extraction.py: extracts PE features for ML flow
– Dynamic_Analysis/config.py (future)
c. Static Analysis Module
– VT blueprint: handles file/URL upload, calls VirusTotal API (external service)
– requests library
d. Machine Learning Module
– ML_model directory: holds malwareclassifier.pkl and malwareclassifier-V2.pkl
– scikit-learn, joblib for model loading
e. Dataset & Training (Offline)
– Dataset CSVs used in Jupyter notebook (malware_detection_insa_1.ipynb)
– feature filtering and model training occur outside runtime
f. External Services

##### Relationships and Data Flows
1. User Browser → Flask Server (HTTP Post /upload-static or /upload-ml)
2. Flask → VirusTotal API → Flask
3. Flask → feature_extraction → load model → Flask
4. Flask → render_template → Browser

##### Architectural Patterns & Design Principles
- Monolithic MVC:
- Controllers: Flask routes in app.py and VT/app.py
- Views: Jinja2 templates under templates/ and VT/templates/
- Models:
– Statistical model artifacts (joblib/scikit-learn)
– Configuration for dynamic analysis
- Modular separation via Flask Blueprints for static-analysis
- Clear separation of concerns: static API vs. ML detection

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% User Interface Layer
    subgraph "User Interface" 
        direction TB
        Browser["User Browser"]:::frontend
        UI_Index["templates/index.html"]:::frontend
        UI_Result["templates/result.html"]:::frontend
        VT_UI_Index["VT/templates/index.html"]:::frontend
        VT_UI_Result["VT/templates/result.html"]:::frontend
        VT_UI_Detail["VT/templates/details.html"]:::frontend
    end

    Browser -->|"GET"| UI_Index
    Browser -->|"GET"| VT_UI_Index

    %% Runtime Services Layer
    subgraph "Runtime Services"
        direction TB
        App1["app.py"]:::backend
        App2["appV1.py"]:::backend
        Blueprint["VT Blueprint (VT/app.py)"]:::backend
        Feature["Feature Extractor (feature_extraction.py)"]:::backend
        ModelLoader["ML Model Loader"]:::backend
        DynamicCfg["Dynamic Analysis Config (config.py)"]:::backend
    end

    UI_Index -->|"POST /upload-ml"| App1
    VT_UI_Index -->|"POST /upload-static"| Blueprint
    App1 --> Blueprint
    App2 --> Blueprint

    App1 -->|"calls feature_extraction"| Feature
    Feature -->|"joblib load"| ModelLoader
    ModelLoader -->|"inference"| App1

    %% Rendering Views
    App1 -->|"render_template index/result"| UI_Result
    Blueprint -->|"render_template result"| VT_UI_Result
    Blueprint -->|"render_template details"| VT_UI_Detail

    %% External Services
    subgraph "External Services"
        direction TB
        VTAPI["VirusTotal API"]:::external
    end

    Blueprint -->|"API Call (requests)"| VTAPI
    VTAPI -->|"JSON Response"| Blueprint

    %% Offline Training Components
    subgraph "Offline Training"
        direction TB
        Dataset1["Dataset/dataset_malwares.csv"]:::offline
        Dataset2["Dataset/filtered_important_features_dataset.csv"]:::offline
        Notebook["malware_detection_insa_1.ipynb"]:::offline
        Model1["ML_model/malwareclassifier.pkl"]:::offline
        Model2["ML_model/malwareclassifier-V2.pkl"]:::offline
    end

    Notebook --> Dataset1
    Notebook --> Dataset2
    Notebook --> Model1
    Notebook --> Model2
    Model1 --> ModelLoader
    Model2 --> ModelLoader

    %% Click Events
    click UI_Index "https://github.com/0xfke/malware-detection-and-analysis-using-machine-learning/blob/main/templates/index.html"
    click UI_Result "https://github.com/0xfke/malware-detection-and-analysis-using-machine-learning/blob/main/templates/result.html"
    click VT_UI_Index "https://github.com/0xfke/malware-detection-and-analysis-using-machine-learning/blob/main/VT/templates/index.html"
    click VT_UI_Result "https://github.com/0xfke/malware-detection-and-analysis-using-machine-learning/blob/main/VT/templates/result.html"
    click VT_UI_Detail "https://github.com/0xfke/malware-detection-and-analysis-using-machine-learning/blob/main/VT/templates/details.html"
    click App1 "https://github.com/0xfke/malware-detection-and-analysis-using-machine-learning/blob/main/app.py"
    click App2 "https://github.com/0xfke/malware-detection-and-analysis-using-machine-learning/blob/main/appV1.py"
    click Feature "https://github.com/0xfke/malware-detection-and-analysis-using-machine-learning/blob/main/feature_extraction.py"
    click Blueprint "https://github.com/0xfke/malware-detection-and-analysis-using-machine-learning/blob/main/VT/app.py"
    click Dataset1 "https://github.com/0xfke/malware-detection-and-analysis-using-machine-learning/blob/main/Dataset/dataset_malwares.csv"
    click Dataset2 "https://github.com/0xfke/malware-detection-and-analysis-using-machine-learning/blob/main/Dataset/filtered_important_features_dataset.csv"
    click Notebook "https://github.com/0xfke/malware-detection-and-analysis-using-machine-learning/blob/main/malware_detection_insa_1.ipynb"
    click Model1 "https://github.com/0xfke/malware-detection-and-analysis-using-machine-learning/blob/main/ML_model/malwareclassifier.pkl"
    click Model2 "https://github.com/0xfke/malware-detection-and-analysis-using-machine-learning/blob/main/ML_model/malwareclassifier-V2.pkl"
    click DynamicCfg "https://github.com/0xfke/malware-detection-and-analysis-using-machine-learning/blob/main/Dynamic_Analysis/config.py"

    %% Styles
    classDef frontend fill:#D6EAF8,stroke:#2E86C1;
    classDef backend fill:#D5F5E3,stroke:#27AE60;
    classDef external fill:#FADBD8,stroke:#C0392B;
    classDef offline fill:#FCF3CF,stroke:#B7950B,stroke-dasharray: 5 5;
```
