How to Scan Files Against a Malware Hash List in Python
Compare a file's SHA-256 hash against a known malware hash set and report whether it's clean or infected.
Python code
27 linesimport hashlib
from pathlib import Path
# Mock file content (in real usage, read from disk)
MOCK_FILE_CONTENT = b"print('hello world')"
KNOWN_MALWARE_HASHES = {
"8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92",
"5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8",
}
def sha256_hex(data: bytes) -> str:
return hashlib.sha256(data).hexdigest()
def scan_file(file_path: Path, malware_hashes: set[str]) -> str:
content = file_path.read_bytes()
file_hash = sha256_hex(content)
if file_hash in malware_hashes:
return f"MALWARE DETECTED: {file_path} (hash: {file_hash})"
return f"CLEAN: {file_path} (hash: {file_hash})"
if __name__ == "__main__":
# Simulate a file scan
temp_file = Path("mock_sample.py")
temp_file.write_bytes(MOCK_FILE_CONTENT)
print(scan_file(temp_file, KNOWN_MALWARE_HASHES))
temp_file.unlink() # cleanup
Output
CLEAN: mock_sample.py (hash: 8f434346648f6b96df89dda901c5176b10a6d83961dd3c1ac88b59b2dc327aa4)
How it works
The script computes a SHA-256 digest of the file's bytes with hashlib.sha256().hexdigest(), then checks membership in a set of known malware hashes for O(1) lookup. Using a set for the hash list makes repeated scans fast. Path.read_bytes() loads the whole file into memory, which works fine for small files; larger files should be hashed in chunks. The mock content produces a clean hash, demonstrating the normal scan path; swap in a real hash from KNOWN_MALWARE_HASHES to see the detection branch.
Common mistakes
- Hashing the filename string instead of the file bytes
- Using a list instead of a set, causing O(n) lookups on large hash databases
- Forgetting to handle file-not-found or permission errors when scanning real files
Variations
- Hash the file in 64KB chunks with `hashlib.sha256()` update() to avoid loading large files into memory
Real-world use cases
- A CI pipeline that scans uploaded artifacts against a malware blacklist before deploying to production.
- A security crawler that compares downloaded attachments in an email gateway against known threat hashes.
- A file server hook that quarantines any uploaded file matching a virus signature database.
Sponsored
More from Automation & scripting
- Aggregate Log Errors Count by Hour in Python easy
- Automate Tweeting New Blog Posts in Python easy
- Automatically Clean Temporary Files from Applications Using Python medium
- Automatically Download the Latest Software Release from GitHub with Python medium
- Automatically Generate Charts from CSV Files with One Command medium
- Automatically Generate Hardware Inventory Reports in Python easy
Keep learning
Related tutorials and quizzes for this topic.