-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinfo_analysis.py
More file actions
87 lines (74 loc) · 3.09 KB
/
info_analysis.py
File metadata and controls
87 lines (74 loc) · 3.09 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
import hashlib
import os
import whois
import requests
from colorama import Fore, Style
def calculate_file_hash(file_path, algorithm='sha256'):
"""
Calcula o hash de um arquivo usando o algoritmo especificado.
"""
print(f"{Fore.CYAN}Calculando o hash {algorithm.upper()} para {file_path}...{Style.RESET_ALL}")
try:
if not os.path.exists(file_path):
print(f"{Fore.RED}Erro: Arquivo não encontrado em {file_path}{Style.RESET_ALL}")
return None
hash_func = hashlib.new(algorithm)
with open(file_path, 'rb') as f:
# Lê o arquivo em blocos para lidar com arquivos grandes
for chunk in iter(lambda: f.read(4096), b''):
hash_func.update(chunk)
file_hash = hash_func.hexdigest()
print(f"{Fore.GREEN}Hash {algorithm.upper()} calculado com sucesso.{Style.RESET_ALL}")
return file_hash
except Exception as e:
print(f"{Fore.RED}Ocorreu um erro ao calcular o hash: {e}{Style.RESET_ALL}")
return None
# Nota: A extração de metadados de arquivos (como PDF, DOCX, Imagens) geralmente requer bibliotecas específicas (ex: PIL, exifread, python-docx, PyPDF2).
# Para manter o escopo inicial e evitar muitas dependências, focaremos no hash e Whois.
# Uma função de metadados básica pode ser adicionada posteriormente.
def whois_lookup(domain):
"""
Realiza uma consulta Whois para um domínio.
"""
print(f"{Fore.CYAN}Realizando consulta Whois para {domain}...{Style.RESET_ALL}")
try:
w = whois.whois(domain)
print(f"{Fore.GREEN}Consulta Whois concluída.{Style.RESET_ALL}")
return w
except Exception as e:
print(f"{Fore.RED}Ocorreu um erro durante a consulta Whois: {e}{Style.RESET_ALL}")
return None
def dns_lookup(domain):
"""
Realiza uma consulta DNS básica para obter o endereço IP.
"""
print(f"{Fore.CYAN}Realizando consulta DNS para {domain}...{Style.RESET_ALL}")
try:
ip_address = socket.gethostbyname(domain)
print(f"{Fore.GREEN}Consulta DNS concluída. IP: {ip_address}{Style.RESET_ALL}")
return ip_address
except socket.gaierror:
print(f"{Fore.RED}Erro: Não foi possível resolver o nome do domínio: {domain}{Style.RESET_ALL}")
return None
except Exception as e:
print(f"{Fore.RED}Ocorreu um erro durante a consulta DNS: {e}{Style.RESET_ALL}")
return None
if __name__ == '__main__':
# Exemplo de uso
# Criar um arquivo de teste
test_file_path = "test_file.txt"
with open(test_file_path, "w") as f:
f.write("Conteúdo de teste para hash.")
# Testar cálculo de hash
sha256_hash = calculate_file_hash(test_file_path)
print(f"Hash SHA256: {sha256_hash}\n")
# Testar Whois
whois_info = whois_lookup("google.com")
if whois_info:
print(f"Registrante: {whois_info.registrar}")
print(f"Data de Criação: {whois_info.creation_date}\n")
# Testar DNS Lookup
ip = dns_lookup("github.com")
print(f"IP do GitHub: {ip}")
# Limpar arquivo de teste
os.remove(test_file_path)