-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvirus_total_malware_api.py
More file actions
47 lines (39 loc) · 1.44 KB
/
Copy pathvirus_total_malware_api.py
File metadata and controls
47 lines (39 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import hashlib
import os
import requests
def hash_file(filename):
""""This function returns the SHA-256 hash
of the file passed into it"""
# make a hash object
h = hashlib.sha256()
# open file for reading in binary mode
with open(filename,'rb') as file:
# loop till the end of the file
chunk = 0
while chunk != b'':
# read only 1024 bytes at a time
chunk = file.read(1024)
h.update(chunk)
# return the hex representation of digest
return h.hexdigest()
def check_virus_total(file_hash, filename):
url = 'https://www.virustotal.com/api/v3/files/' + file_hash
headers = {
"x-apikey": " API KEY HERE"
}
response = requests.get(url, headers=headers)
if response.status_code == 200:
data = response.json()
if data['data']['attributes']['last_analysis_results']['malicious']:
print(f"File {filename} is malicious. Malware found: {data['data']['attributes']['last_analysis_results']['malware_name']}")
else:
print(f"File {filename} is not malicious")
else:
print(f"Error checking file {filename} on VirusTotal. Status: {response.status_code}")
directory = '.'
# loop through all files in the directory
for filename in os.listdir(directory):
file_path = os.path.join(directory, filename)
if os.path.isfile(file_path):
file_hash = hash_file(file_path)
check_virus_total(file_hash, filename)