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/"
  },
  "parameter": {
    "general": {
        "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.parameter.feature.save.
  • The contents of this config.json are fully customizable. For example, if you add "new_dic": { "new_val": 123 } under general, you can access it via self.config.parameter.general.new_dic.new_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.2.tar.gz (10.9 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.2-py3-none-any.whl (9.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: malwareclassifier-0.1.2.tar.gz
  • Upload date:
  • Size: 10.9 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.2.tar.gz
Algorithm Hash digest
SHA256 dcef525067936edda53757c4cf396a5edd67aec08e4447533e5ca2dc8aa92dff
MD5 6f3855876fdc104feef6680eef9fd829
BLAKE2b-256 567c95c5924004b4bc3cb12ed69f6e4f72a7d02d9af0ccebe1da8a31318ed86e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for malwareclassifier-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 b40ed6d24c04a9904f4b231b70d360755aef79fe458852aa1bcf81da177a8b20
MD5 7a7d44f96dd836c12a9bd2ad06395cc5
BLAKE2b-256 f6ade5f37cc44a826f4fb8b57e44bb5e17f54e611fbf4acb1d75c62ad856fac5

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