Skip to main content

A malware classifier template with built-in logging.

Project description

MalwareClassifier

MalwareClassifier is a Python package that provides a template for building a malware classification system with a built-in logging system and configurable settings. It is designed to be modular, extensible, and easy to install using pip.


Table of Contents


Installation

Option A: Install using pip

pip install MalwareClassifier

Option B: Standard installation

# Clone the repository
git clone git@github.com:cchunhuang/MalwareClassifier.git
cd MalwareClassifier

# Install
pip install .

# Install additional dependencies if needed
pip install -r requirements.txt

Quick Start

from MalwareClassifier import MalwareClassifier, setup_logging, get_logger

class subMalwareClassifier(MalwareClassifier):
    def __init__(self, config_path="./config.json"):
        super().__init__(config_path)
        setup_logging(log_dir=self.config.folder.log)
        self.logger = get_logger(__name__)

    def getFeature(self):
        self.logger.info("Extracting features.")
    
    def getVector(self):
        self.logger.info("Vectorizing.")

    def getModel(self, action:str="train"):
        self.logger.info(f"Using model for action: {action}.")
    
    def getPrediction(self):
        self.logger.info("Predicting.")
    
if __name__ == "__main__":
    classifier = subMalwareClassifier()
    classifier.getFeature()
    classifier.getVector()
    classifier.getModel()
    classifier.getPrediction()
  • The MalwareClassifier class in MalwareClassifier.py defines the workflow skeleton. Subclass it to override getFeature(), getVector(), getModel(), and getPrediction().
  • You can specify your own config_path.

Configuration

The package includes a default config.json:

{
    "file": {
        "label": "./dataset/label.csv"
    },
    "folder": {
        "log": "./output/log/",
        "dataset": "./dataset/",
        "feature": "./output/feature/",
        "vectorize": "./output/vectorize/",
        "model": "./output/model/",
        "predict": "./output/predict/"
    },
    "params": {
        "detection": true
    },
    "feature": {
        "save": true,
        "load": false
    },
    "vectorize": {
        "save": true,
        "load": false
    },
    "model": {
        "save": true,
        "load": false
    },
    "predict": {
        "save": true,
        "load": false
    }
}

After calling super().__init__()

  • You can directly access the data using dot notation, such as self.config.file.label or self.config.feature.save.
  • The contents of this config.json are fully customizable.
    • For example, if you add "new_dic": { "new_val": 123 } under params, you can access it via self.config.params.new_dic.new_val.
    • Likewise, you can access nested values with dot notation, e.g. self.config.dic1.dic2.dic3.val.
  • Folders in folder will be created automatically (or using self.mkdir()).

Logging Usage

The logging system is defined in src/MalwareClassifier/Logging.py.

Available functions

  • setup_logging(config=None, reset_handlers=True, log_dir=None) Initialize logging with optional config overrides.
  • It is recommended to use setup_logging(log_dir=self.config.folder.log)
  • get_logger(name=None) Retrieve a logger for any module.

Default behavior

  • Logs are written both to console and file.
  • Log files are automatically named as: malware_classifier-YYYYMMDD-HHMMSS.log

Environment variables

Variable Description Example
MALCLASS_LOG_LEVEL Set log level DEBUG, INFO
MALCLASS_LOG_FILE Full path for the log file /tmp/log.txt
MALCLASS_LOG_DIR Directory for log files ./output/log
MALCLASS_LOG_FORMATTER Choose formatter: basic, verbose, json verbose

Note: JSON logging requires installing python-json-logger.

Example usage in modules

from MalwareClassifier import setup_logging, get_logger

setup_logging()
logger = get_logger(__name__)
logger.info("This is an info message")
logger.debug("This is a debug message")

Publishing

Check version

pip install -e .
python -c "import MalwareClassifier as MC; print(MC.__version__)"

Publish to PyPI

git tag v0.1.0  # Replace the version
git push origin v0.1.0
# git action will publish it automatically.

Publish to PyPI (Manual)

pip install build twine
python -m build
twine upload dist/*

License

This project is licensed under the terms of the MIT License.


Contact

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

malwareclassifier-0.1.3.tar.gz (12.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

malwareclassifier-0.1.3-py3-none-any.whl (9.0 kB view details)

Uploaded Python 3

File details

Details for the file malwareclassifier-0.1.3.tar.gz.

File metadata

  • Download URL: malwareclassifier-0.1.3.tar.gz
  • Upload date:
  • Size: 12.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.13

File hashes

Hashes for malwareclassifier-0.1.3.tar.gz
Algorithm Hash digest
SHA256 a8ef7c8dfcbdcac4057a3b8ef12be5b5c2a6c3b3a3a7e9e0901a67f6c29148b4
MD5 2832e5be1f96cab051272315b82d710e
BLAKE2b-256 a949b00f46f139cb5f139c25568290082eef6626d3b41e08e33f936fd02bec78

See more details on using hashes here.

File details

Details for the file malwareclassifier-0.1.3-py3-none-any.whl.

File metadata

File hashes

Hashes for malwareclassifier-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 ae7019362c71f43e6f208d6b8727b1f1dbada496dac6d83235564b354f767e0d
MD5 7b4a49633fcc42c2558557a3c1a95f57
BLAKE2b-256 6e790c9c75c6c0e35a522e024a7bd1cda6c646a19106c8885a591f8737fe320e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page