-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmalware_analysis.py
More file actions
80 lines (69 loc) · 2.79 KB
/
malware_analysis.py
File metadata and controls
80 lines (69 loc) · 2.79 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
pip install requests pefile
import requests
import hashlib
import pefile
import sys
import os
# Insira sua chave de API do VirusTotal aqui
VIRUSTOTAL_API_KEY = 'SUA_CHAVE_DE_API'
# Função para calcular o hash SHA-256 de um arquivo
def calculate_sha256(file_path):
sha256_hash = hashlib.sha256()
with open(file_path, "rb") as f:
for byte_block in iter(lambda: f.read(4096), b""):
sha256_hash.update(byte_block)
return sha256_hash.hexdigest()
# Função para verificar o hash no VirusTotal
def check_virustotal(file_hash):
url = f"https://www.virustotal.com/api/v3/files/{file_hash}"
headers = {
"x-apikey": VIRUSTOTAL_API_KEY
}
response = requests.get(url, headers=headers)
if response.status_code == 200:
json_response = response.json()
positives = json_response["data"]["attributes"]["last_analysis_stats"]["malicious"]
total = json_response["data"]["attributes"]["last_analysis_stats"]["total"]
return positives, total
else:
print("Erro ao consultar o VirusTotal:", response.status_code)
return None, None
# Função para realizar análise estática em um executável PE
def analyze_pe_file(file_path):
try:
pe = pefile.PE(file_path)
print("\n--- Informações do Executável PE ---")
print("Número de seções:", len(pe.sections))
for section in pe.sections:
print(f"Seção: {section.Name.decode().strip()}, Tamanho: {section.SizeOfRawData}")
print("Entrypoint:", hex(pe.OPTIONAL_HEADER.AddressOfEntryPoint))
print("Imagem Base:", hex(pe.OPTIONAL_HEADER.ImageBase))
except pefile.PEFormatError:
print("O arquivo não é um executável PE válido.")
# Função principal para realizar a análise de malware
def analyze_file(file_path):
if not os.path.isfile(file_path):
print("Arquivo não encontrado.")
return
print("Iniciando análise de:", file_path)
# Calcula o hash SHA-256 do arquivo
file_hash = calculate_sha256(file_path)
print("Hash SHA-256:", file_hash)
# Verifica o hash no VirusTotal
positives, total = check_virustotal(file_hash)
if positives is not None:
print(f"Detecções no VirusTotal: {positives}/{total}")
else:
print("Não foi possível verificar o hash no VirusTotal.")
# Realiza análise estática no caso de executáveis PE
if file_path.endswith(('.exe', '.dll')):
analyze_pe_file(file_path)
else:
print("Análise estática disponível apenas para arquivos PE (EXE/DLL).")
# Uso do script: python script.py <caminho_do_arquivo>
if __name__ == "__main__":
if len(sys.argv) != 2:
print("Uso: python script.py <caminho_do_arquivo>")
else:
file_path = sys.argv[1]
analyze_file(file_path)