DEV Community

Rishi Kumavat
Rishi Kumavat

Posted on Edited on

I Built a File Integrity Checker Using Python and HMAC

As someone learning cybersecurity, I believe the best way to understand security concepts is to implement them through practical projects.

Recently, I completed a project called File Integrity Checker, based on the File Integrity Checker challenge from roadmap.sh.

The idea behind this project is simple: detect whether monitored log files have been modified, deleted, or newly added.

What does it do?

The File Integrity Checker is a Python-based command-line tool that monitors .log files and checks whether their contents have changed since the trusted baseline was created.

It can identify:

  • UNMODIFIED: The file content has not changed.
  • MODIFIED: The file content has been changed.
  • MISSING: A previously monitored file has been deleted.
  • NEW: A new log file has been detected.
  • NOT_MONITORED: The specified file is not part of the baseline.

Technologies used

  • Python
  • SHA-256 hashing
  • HMAC-SHA256 authentication
  • JSON for baseline storage
  • Argparse for the command-line interface
  • Unittest for automated testing

How does it work?

When initializing the tool, it scans the target directory and calculates the SHA-256 hash of each log file.

These hashes are stored in a baseline JSON file. To protect the baseline from unauthorized modification, I implemented HMAC-SHA256 authentication using a separate secret key.

During an integrity check, the tool calculates the current file hashes and compares them with the trusted baseline.

If a file has changed, the tool reports it.

I also implemented protection against missing authentication keys and tampered baseline data.

What I learned

This project helped me understand several important cybersecurity concepts:

  • How SHA-256 hashing works in file integrity monitoring.
  • Why cryptographic hashes are useful for detecting file modifications.
  • How HMAC can authenticate stored data.
  • Why secret keys should be stored separately from the data they protect.
  • How to organize a Python project into separate modules.
  • How to write automated tests using Python's unittest framework.

I also tested the project with different scenarios, including modified files, deleted files, new files, and baseline tampering. The automated test suite passed all 9 tests.

Project submission

I have submitted my implementation to the roadmap.sh File Integrity Checker project.

If you find my implementation interesting, I'd really appreciate your support. Please visit the solution page and give it a vote!

🔗 Vote for my solution:

https://roadmap.sh/projects/file-integrity-checker/solutions?u=6b03f1a3f666a02b517172f2

Final thoughts

This was a good opportunity to move beyond just reading about cybersecurity concepts and actually implement them.

I'm continuing to build practical projects, improve my Python skills, and explore more areas of cybersecurity.

Feedback and suggestions are always welcome!

Top comments (0)