From d05cd81eaa884004a588df780c9f45504732d585 Mon Sep 17 00:00:00 2001 From: Bruno <55158870+bruno-canada@users.noreply.github.com> Date: Tue, 28 Dec 2021 12:24:18 -0800 Subject: [PATCH] Alpha release of IntegrityGuard --- .editorconfig | 21 ++++++ .gitignore | 109 +++++++++++++++++++++++++++++++ AUTHORS | 13 ++++ HISTORY | 8 +++ LICENSE | 22 +++++++ MANIFEST.in | 10 +++ README | 21 ++++++ code_of_conduct.md | 134 ++++++++++++++++++++++++++++++++++++++ config.yml | 31 +++++++++ requirements.txt | Bin 0 -> 62 bytes requirements_dev.txt | 12 ++++ setup.cfg | 18 +++++ setup.py | 49 ++++++++++++++ src/__init__.py | 1 + src/data/__init__.py | 1 + src/data/hash | 1 + src/data/hash.py | 26 ++++++++ src/hashreport.py | 42 ++++++++++++ src/helpers/loadconfig.py | 11 ++++ src/integrityguard.py | 1 + src/monitor.py | 24 +++++++ 21 files changed, 555 insertions(+) create mode 100644 .editorconfig create mode 100644 .gitignore create mode 100644 AUTHORS create mode 100644 HISTORY create mode 100644 LICENSE create mode 100644 MANIFEST.in create mode 100644 README create mode 100644 code_of_conduct.md create mode 100644 config.yml create mode 100644 requirements.txt create mode 100644 requirements_dev.txt create mode 100644 setup.cfg create mode 100644 setup.py create mode 100644 src/__init__.py create mode 100644 src/data/__init__.py create mode 100644 src/data/hash create mode 100644 src/data/hash.py create mode 100644 src/hashreport.py create mode 100644 src/helpers/loadconfig.py create mode 100644 src/integrityguard.py create mode 100644 src/monitor.py diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..eb8bd95 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,21 @@ +# http://editorconfig.org + +root = true + +[*] +indent_style = space +indent_size = 4 +trim_trailing_whitespace = true +insert_final_newline = true +charset = utf-8 +end_of_line = lf + +[*.bat] +indent_style = tab +end_of_line = crlf + +[LICENSE] +insert_final_newline = false + +[Makefile] +indent_style = tab diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2a9ea48 --- /dev/null +++ b/.gitignore @@ -0,0 +1,109 @@ +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +env/ +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +*.egg-info/ +.installed.cfg +*.egg + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +.hypothesis/ +.pytest_cache/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# pyenv +.python-version + +# celery beat schedule file +celerybeat-schedule + +# SageMath parsed files +*.sage.py + +# dotenv +.env + +# virtualenv +.venv +venv/ +ENV/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ + +# IDE settings +.vscode/ +.idea/ + +# Sample +/sample/ \ No newline at end of file diff --git a/AUTHORS b/AUTHORS new file mode 100644 index 0000000..19ac8ec --- /dev/null +++ b/AUTHORS @@ -0,0 +1,13 @@ +======= +Credits +======= + +Development Lead +---------------- + +* Bruno Bueno + +Contributors +------------ + +None yet. Why not be the first? diff --git a/HISTORY b/HISTORY new file mode 100644 index 0000000..808c52a --- /dev/null +++ b/HISTORY @@ -0,0 +1,8 @@ +======= +History +======= + +0.1.0 (2021-12-28) +------------------ + +* Alpha release on PyPI. diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..c1d7ee1 --- /dev/null +++ b/LICENSE @@ -0,0 +1,22 @@ +MIT License + +Copyright (c) 2021, Bruno Bueno + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + diff --git a/MANIFEST.in b/MANIFEST.in new file mode 100644 index 0000000..c51448f --- /dev/null +++ b/MANIFEST.in @@ -0,0 +1,10 @@ +include AUTHORS +include HISTORY +include LICENSE +include README + +recursive-include tests * +recursive-exclude * __pycache__ +recursive-exclude * *.py[co] + +recursive-include docs *.rst conf.py Makefile make.bat *.jpg *.png *.gif diff --git a/README b/README new file mode 100644 index 0000000..c4a6275 --- /dev/null +++ b/README @@ -0,0 +1,21 @@ +# IntegrityGuard + +[![Contributor Covenant](https://img.shields.io/badge/Contributor%20Covenant-2.1-4baaaa.svg)](code_of_conduct.md) + +Multiplatform agent for file integrity monitoring (FIM). + +The main motivation for this project is to offer all the minimum features required for a reliable FIM that is independent of any other big monitoring platform. + +IMPORTANT: This project is currently an ALPHA release. Not suitable for production environment, it is still a work in progress. + +# Features highlight + +- Simple and centralized configuration YAML file (`config.yml`) +- Generate logs of any changes in real-time for future auditing +- Push notifications to an API endpoint +- Send email alerts +- Supported hashing methods: "md5", "sha1", "sha224", "sha256", "sha384", "sha512" + +# Call for contributors + + diff --git a/code_of_conduct.md b/code_of_conduct.md new file mode 100644 index 0000000..44aec3e --- /dev/null +++ b/code_of_conduct.md @@ -0,0 +1,134 @@ + +# Contributor Covenant Code of Conduct + +## Our Pledge + +We as members, contributors, and leaders pledge to make participation in our +community a harassment-free experience for everyone, regardless of age, body +size, visible or invisible disability, ethnicity, sex characteristics, gender +identity and expression, level of experience, education, socio-economic status, +nationality, personal appearance, race, caste, color, religion, or sexual +identity and orientation. + +We pledge to act and interact in ways that contribute to an open, welcoming, +diverse, inclusive, and healthy community. + +## Our Standards + +Examples of behavior that contributes to a positive environment for our +community include: + +* Demonstrating empathy and kindness toward other people +* Being respectful of differing opinions, viewpoints, and experiences +* Giving and gracefully accepting constructive feedback +* Accepting responsibility and apologizing to those affected by our mistakes, + and learning from the experience +* Focusing on what is best not just for us as individuals, but for the overall + community + +Examples of unacceptable behavior include: + +* The use of sexualized language or imagery, and sexual attention or advances of + any kind +* Trolling, insulting or derogatory comments, and personal or political attacks +* Public or private harassment +* Publishing others' private information, such as a physical or email address, + without their explicit permission +* Other conduct which could reasonably be considered inappropriate in a + professional setting + +## Enforcement Responsibilities + +Community leaders are responsible for clarifying and enforcing our standards of +acceptable behavior and will take appropriate and fair corrective action in +response to any behavior that they deem inappropriate, threatening, offensive, +or harmful. + +Community leaders have the right and responsibility to remove, edit, or reject +comments, commits, code, wiki edits, issues, and other contributions that are +not aligned to this Code of Conduct, and will communicate reasons for moderation +decisions when appropriate. + +## Scope + +This Code of Conduct applies within all community spaces, and also applies when +an individual is officially representing the community in public spaces. +Examples of representing our community include using an official e-mail address, +posting via an official social media account, or acting as an appointed +representative at an online or offline event. + +## Enforcement + +Instances of abusive, harassing, or otherwise unacceptable behavior may be +reported to the community leaders responsible for enforcement at +integrityguard@fastmail.com. +All complaints will be reviewed and investigated promptly and fairly. + +All community leaders are obligated to respect the privacy and security of the +reporter of any incident. + +## Enforcement Guidelines + +Community leaders will follow these Community Impact Guidelines in determining +the consequences for any action they deem in violation of this Code of Conduct: + +### 1. Correction + +**Community Impact**: Use of inappropriate language or other behavior deemed +unprofessional or unwelcome in the community. + +**Consequence**: A private, written warning from community leaders, providing +clarity around the nature of the violation and an explanation of why the +behavior was inappropriate. A public apology may be requested. + +### 2. Warning + +**Community Impact**: A violation through a single incident or series of +actions. + +**Consequence**: A warning with consequences for continued behavior. No +interaction with the people involved, including unsolicited interaction with +those enforcing the Code of Conduct, for a specified period of time. This +includes avoiding interactions in community spaces as well as external channels +like social media. Violating these terms may lead to a temporary or permanent +ban. + +### 3. Temporary Ban + +**Community Impact**: A serious violation of community standards, including +sustained inappropriate behavior. + +**Consequence**: A temporary ban from any sort of interaction or public +communication with the community for a specified period of time. No public or +private interaction with the people involved, including unsolicited interaction +with those enforcing the Code of Conduct, is allowed during this period. +Violating these terms may lead to a permanent ban. + +### 4. Permanent Ban + +**Community Impact**: Demonstrating a pattern of violation of community +standards, including sustained inappropriate behavior, harassment of an +individual, or aggression toward or disparagement of classes of individuals. + +**Consequence**: A permanent ban from any sort of public interaction within the +community. + +## Attribution + +This Code of Conduct is adapted from the [Contributor Covenant][homepage], +version 2.1, available at +[https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1]. + +Community Impact Guidelines were inspired by +[Mozilla's code of conduct enforcement ladder][Mozilla CoC]. + +For answers to common questions about this code of conduct, see the FAQ at +[https://www.contributor-covenant.org/faq][FAQ]. Translations are available at +[https://www.contributor-covenant.org/translations][translations]. + +[homepage]: https://www.contributor-covenant.org +[v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html +[Mozilla CoC]: https://github.com/mozilla/diversity +[FAQ]: https://www.contributor-covenant.org/faq +[translations]: https://www.contributor-covenant.org/translations + diff --git a/config.yml b/config.yml new file mode 100644 index 0000000..3b49dcc --- /dev/null +++ b/config.yml @@ -0,0 +1,31 @@ +# Hash algorithm type (MD5, SHA1, SHA224, SHA256, SHA384, and SHA512). +hash_type: "MD5" + +# Path to monitor +targetPath: "./sample" + +# Exclusions +toIgnore: + - ./*.log + - ./ignoreTest/* + +# Log path +logPath: "./log" + +# Integrity report format: json or txt +reportType: "json" + +# Target file to store integrity records +integrityReport: "./storage/integrity" + +# Notification method (POST, EMAIL) +notificationMethod: "POST" + +# If POST notification method is defined, config below +postEndPoint: "" + +# If EMAIL notification method is defined, config below +smtpHost: "" +smtpUser: "" +smtpPass: "" +smtpPort: "" \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000000000000000000000000000000000000..5aa27667ee095d18a5d8e900ee295d8fc57a340d GIT binary patch literal 62 zcmezWFMy$vA(Fw7!I!~@!4?S381xtn7=7.0', ] + +test_requirements = [ ] + +setup( + author="Bruno Bueno", + author_email='bbueno_python@fastmail.com', + python_requires='>=3.6', + classifiers=[ + 'Development Status :: 2 - Pre-Alpha', + 'Intended Audience :: Developers', + 'License :: OSI Approved :: MIT License', + 'Natural Language :: English', + 'Programming Language :: Python :: 3', + 'Programming Language :: Python :: 3.6', + 'Programming Language :: Python :: 3.7', + 'Programming Language :: Python :: 3.8', + ], + description="Multiplatform agent for file integrity monitoring. Monitors, generate logs, and notify.", + entry_points={ + 'console_scripts': [ + 'integrityguard=integrityguard.cli:main', + ], + }, + install_requires=requirements, + license="MIT license", + long_description=readme + '\n\n' + history, + include_package_data=True, + keywords='integrityguard', + name='integrityguard', + packages=find_packages(include=['integrityguard', 'integrityguard.*']), + test_suite='tests', + tests_require=test_requirements, + url='https://github.com/bruno-canada/integrityguard', + version='0.1.0', + zip_safe=False, +) diff --git a/src/__init__.py b/src/__init__.py new file mode 100644 index 0000000..ad08ae1 --- /dev/null +++ b/src/__init__.py @@ -0,0 +1 @@ +# Blank \ No newline at end of file diff --git a/src/data/__init__.py b/src/data/__init__.py new file mode 100644 index 0000000..ad08ae1 --- /dev/null +++ b/src/data/__init__.py @@ -0,0 +1 @@ +# Blank \ No newline at end of file diff --git a/src/data/hash b/src/data/hash new file mode 100644 index 0000000..27c8824 --- /dev/null +++ b/src/data/hash @@ -0,0 +1 @@ +[{"path": "C:\\Sites\\tech2ops\\integrityguard\\sample\\test2.txt", "hash": "098f6bcd4621d373cade4e832627b4f6"}, {"path": "C:\\Sites\\tech2ops\\integrityguard\\sample\\text.txt", "hash": "c4ca4238a0b923820dcc509a6f75849b"}] \ No newline at end of file diff --git a/src/data/hash.py b/src/data/hash.py new file mode 100644 index 0000000..0037fc9 --- /dev/null +++ b/src/data/hash.py @@ -0,0 +1,26 @@ +import hashlib + +def hash_file(file, method="md5", blocksize=65536): + + # Preset supported methods by hashlib + supportedMethods = ["md5", "sha1", "sha224", "sha256", "sha384", "sha512"] + + # Handle error if a method requested is not supported + if method not in supportedMethods: + raise ValueError("Hash type *" + method + "* not supported.") + + # Dynamically set method + hashIt = getattr(hashlib, method) + + # Initiate Hashlib method + hasher = hashIt() + + # Open file to read content and generate the proper hash + with open(file, 'rb') as afile: + buf = afile.read(blocksize) + while len(buf) > 0: + hasher.update(buf) + buf = afile.read(blocksize) + + # Return hash in digested mode, not in bytes-like object + return hasher.hexdigest() \ No newline at end of file diff --git a/src/hashreport.py b/src/hashreport.py new file mode 100644 index 0000000..11e7d6d --- /dev/null +++ b/src/hashreport.py @@ -0,0 +1,42 @@ +#!/usr/bin/env python3 + +import os +from helpers.loadconfig import load_config +from data.hash import hash_file +import json +import pathlib + +# Load configuration +config = load_config() + +# Get root path to scan +path = config['targetPath'] + +# Get hash type +hash_type = config['hash_type'].lower() + +# Check if the directory is empty +if len( os.listdir(path) ) == 0: + raise ValueError("The directory is empty.") + +# Initiate hashes variable +hashes = [] + +# Scan path directory recursively +for subdir, dirs, files in os.walk(path): + for file in files: + + file_fullpath = os.path.join(subdir, file) + try: + getHash = hash_file(file_fullpath, hash_type) + hashes.append({ "path": os.path.abspath(file_fullpath), "hash": getHash }) + print(file_fullpath + ";" + getHash) + except ValueError as e: + print("Something went wrong hashing the files. " + e) + +# Store hashes +hash_file_path = pathlib.Path(__file__).parent.resolve() +hash_file_path = str(hash_file_path) + "/data/hashes" +f = open(hash_file_path, "w") +f.write(json.dumps(hashes)) +f.close() \ No newline at end of file diff --git a/src/helpers/loadconfig.py b/src/helpers/loadconfig.py new file mode 100644 index 0000000..d34f6d4 --- /dev/null +++ b/src/helpers/loadconfig.py @@ -0,0 +1,11 @@ +import yaml + +def load_config(configPath=None): + + configPath = "./config.yml" if configPath is None else configPath + + with open(configPath, "r") as stream: + try: + return yaml.safe_load(stream) + except yaml.YAMLError as exc: + raise ValueError('Something went wrong loading the config.yml file. ' + exc) diff --git a/src/integrityguard.py b/src/integrityguard.py new file mode 100644 index 0000000..cb89778 --- /dev/null +++ b/src/integrityguard.py @@ -0,0 +1 @@ +"""Main module.""" diff --git a/src/monitor.py b/src/monitor.py new file mode 100644 index 0000000..6a682fc --- /dev/null +++ b/src/monitor.py @@ -0,0 +1,24 @@ +#!/usr/bin/env python3 + +import sys +import time +import logging +from watchdog.observers import Observer +from watchdog.events import LoggingEventHandler + +if __name__ == "__main__": + logging.basicConfig(filename="", + level=logging.INFO, + format='[%(asctime)s] %(message)s', + datefmt='%Y-%m-%d %H:%M:%S') + path = sys.argv[1] if len(sys.argv) > 1 else '.' + event_handler = LoggingEventHandler() + observer = Observer() + observer.schedule(event_handler, path, recursive=True) + observer.start() + try: + while True: + time.sleep(1) + finally: + observer.stop() + observer.join()