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)
logging_config = 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
MalwareClassifierclass inMalwareClassifier.pydefines the workflow skeleton. Subclass it to overridegetFeature(),getVector(),getModel(), andgetPrediction(). - 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": {
"mode": "detection",
"column": {
"detection": { "file_name": "file_name", "label": "label" },
"classification": { "file_name": "file_name", "label": "family" }
}
},
"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.labelorself.config.feature.save. - The contents of this
config.jsonare fully customizable.- For example, if you add
"new_dict": { "new_val": 123 }underparams, you can access it viaself.config.params.new_dict.new_val. - Likewise, you can access nested values with dot notation, e.g.
self.config.dict1.dict2.dict3.val.
- For example, if you add
- Folders in
folderwill be created automatically (or usingself.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) - Return: logging config (dict)
# Global variables: Process start time captured once per interpreter run START_TIME = datetime.now().strftime("%Y%m%d-%H%M%S") _DEFAULT_CONFIG: Dict[str, Any] = { "version": 1, "disable_existing_loggers": False, "formatters": { "basic": { "format": "[%(levelname)s] %(name)s %(filename)s: %(message)s", "datefmt": "%Y-%m-%d %H:%M:%S", }, "verbose": { "format": "%(asctime)s [%(levelname)s] %(name)s %(filename)s:%(lineno)d - %(message)s", "datefmt": "%Y-%m-%d %H:%M:%S", }, "json": { "class": "pythonjsonlogger.jsonlogger.JsonFormatter", "format": "%(asctime)s %(levelname)s %(name)s %(message)s", }, }, "handlers": { "console": { "class": "logging.StreamHandler", "level": "INFO", "formatter": "basic", "stream": "ext://sys.stdout", }, "file": { "class": "logging.FileHandler", "level": "DEBUG", "formatter": "verbose", "filename": f"malware_classifier-{START_TIME}.log", "encoding": "utf-8", }, }, "root": { "level": "INFO", "handlers": ["console", "file"], }, "start_time": START_TIME }
- It is recommended to use
get_logger(name)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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file malwareclassifier-0.1.4.tar.gz.
File metadata
- Download URL: malwareclassifier-0.1.4.tar.gz
- Upload date:
- Size: 13.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d7e469c99c67f5e54437b5ef990c74dab4d8d5073f0e2ffe0d41025c2294563e
|
|
| MD5 |
d8e221cf298b34568a262d527ef60444
|
|
| BLAKE2b-256 |
294421d8c36d33c76ed7b525dca1ad46b72b3da120cad5c2cc285a012da38fd9
|
File details
Details for the file malwareclassifier-0.1.4-py3-none-any.whl.
File metadata
- Download URL: malwareclassifier-0.1.4-py3-none-any.whl
- Upload date:
- Size: 9.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3f252363dd142d07eb3afa3b5353367221943266336d4c4a333a7ba373e00e18
|
|
| MD5 |
eb3c246d7c0cfb3209968352cf615cbd
|
|
| BLAKE2b-256 |
c940f8c491c42ca861df2669093137f142239a92c509d9a4ef7c198d960be49d
|