DEV Community

Olatunji Ayodele Abidemi
Olatunji Ayodele Abidemi

Posted on

Python antivirus or security operation solution and potentially machine learning.

import hashlib

List of known malware hashes (for demonstration purposes)

known_malware_hashes = {
'md5': ['5f4dcc3b5aa765d61d8327deb882cf99', '...'],
'sha1': ['aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d', '...'],
'sha256': ['9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08', '...']
}

def check_file_hash(file_path):
# Calculate the file's hashes
with open(file_path, 'rb') as file:
file_content = file.read()
md5_hash = hashlib.md5(file_content).hexdigest()
sha1_hash = hashlib.sha1(file_content).hexdigest()
sha256_hash = hashlib.sha256(file_content).hexdigest()

# Check if any of the file's hashes match known malware hashes
if md5_hash in known_malware_hashes['md5'] or \
sha1_hash in known_malware_hashes['sha1'] or \
sha256_hash in known_malware_hashes['sha256']:
return True # The file is infected
else:
return False # The file is clean
Enter fullscreen mode Exit fullscreen mode




Example usage

file_to_check = '/path/to/your/file'
is_infected = check_file_hash(file_to_check)
print(f"The file is {'infected' if is_infected else 'clean'}.")

Top comments (0)