forked from aditya-yadav-007/HashGuard
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashGuard.py
More file actions
156 lines (118 loc) · 4.02 KB
/
Copy pathHashGuard.py
File metadata and controls
156 lines (118 loc) · 4.02 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import hashlib
import requests
import sys
import os
from colorama import init, Fore, Style
init(autoreset=True) # Automatically resets colors after each print
# ---------------------------
# Colors (for CLI rating)
# ---------------------------
class Color:
RED = Fore.RED
YELLOW = Fore.YELLOW
GREEN = Fore.GREEN
RESET = Style.RESET_ALL
# ---------------------------
# Internet Check
# ---------------------------
def check_internet():
try:
requests.get("https://www.google.com", timeout=3)
return True
except:
return False
# ---------------------------
# SHA-256 Hash Function
# ---------------------------
def sha256_file(path):
sha256 = hashlib.sha256()
with open(path, "rb") as f:
for chunk in iter(lambda: f.read(8192), b""):
sha256.update(chunk)
return sha256.hexdigest()
# ---------------------------
# VirusTotal Hash Lookup
# ---------------------------
def vt_lookup(hash_value, api_key):
url = f"https://www.virustotal.com/api/v3/files/{hash_value}"
headers = {"x-apikey": api_key}
session = requests.Session()
session.headers.update(headers)
try:
response = session.get(url)
except requests.exceptions.RequestException:
print("Error: Failed to connect to VirusTotal.")
return None
if response.status_code == 401:
print(Color.RED + "Error: Invalid API Key!" + Color.RESET)
return None
if response.status_code == 404:
print("File not found in VirusTotal dataset.")
print(Color.GREEN + "Very low chances of being a virus." + Color.RESET)
return None
if response.status_code != 200:
print(f"VirusTotal Error: {response.status_code} {response.text}")
return None
return response.json()
# ---------------------------
# Rating System
# ---------------------------
def get_rating(vt_json):
stats = vt_json["data"]["attributes"]["last_analysis_stats"]
malicious = stats["malicious"]
suspicious = stats["suspicious"]
if malicious >= 3:
return "HIGH", Color.RED
if malicious > 0 or suspicious > 0:
return "MEDIUM", Color.YELLOW
return "NONE", Color.GREEN
# ---------------------------
# Pretty Print Summary
# ---------------------------
def show_summary(vt_json):
data = vt_json["data"]["attributes"]
stats = data["last_analysis_stats"]
rating, color = get_rating(vt_json)
print("\n====== VirusTotal Report ======")
print(f"Malicious : {stats['malicious']}")
print(f"Suspicious: {stats['suspicious']}")
print(f"Undetected: {stats['undetected']}")
print(f"Harmless : {stats['harmless']}")
print("\nOverall Rating:", color + rating + Color.RESET)
print("\n--- Engines Flagged ---")
for engine, result in data["last_analysis_results"].items():
if result["category"] in ("malicious", "suspicious"):
print(f"{engine}: {result['result']}")
# ---------------------------
# Main
# ---------------------------
def main():
print("===== VTCheck Antivirus CLI v0 =====")
api_key = input("Enter your VirusTotal API Key: ").strip()
if not api_key:
print("No API Key provided.")
input("\nPress Enter to exit...")
sys.exit(1)
if len(sys.argv) != 2:
print("Drag and drop a file over this EXE to scan it. Do not run directly.")
input("\nPress Enter to exit...") # Wait for user
sys.exit(0)
filepath = sys.argv[1]
if not os.path.isfile(filepath):
print("Invalid File")
input("\nPress Enter to exit...")
sys.exit(1)
if not check_internet():
print("Internet Error – No connection.")
input("\nPress Enter to exit...")
sys.exit(1)
print(f"[*] Hashing: {filepath}")
file_hash = sha256_file(filepath)
print(f"[+] SHA256: {file_hash}")
print("\n[*] Sending hash to VirusTotal...")
vt_data = vt_lookup(file_hash, api_key)
if vt_data:
show_summary(vt_data)
input("\nPress Enter to exit...")
if __name__ == "__main__":
main()