-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.py
158 lines (141 loc) · 4.8 KB
/
app.py
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
157
158
import socket
import whois
import dns.resolver
import httpx
import ssl
import requests
import ipwhois
import subprocess
from datetime import datetime
from concurrent.futures import ThreadPoolExecutor, as_completed
def get_whois(domain):
try:
return whois.whois(domain)
except Exception as e:
return str(e)
def get_dns_records(domain):
records = {}
for record_type in ['A', 'MX', 'TXT', 'NS', 'SOA']:
try:
answers = dns.resolver.resolve(domain, record_type)
records[record_type] = [str(r) for r in answers]
except Exception as e:
records[record_type] = str(e)
return records
def get_ssl_certificate(domain):
try:
context = ssl.create_default_context()
with socket.create_connection((domain, 443)) as sock:
with context.wrap_socket(sock, server_hostname=domain) as ssock:
cert = ssock.getpeercert()
return {
"issuer": cert.get("issuer"),
"notBefore": cert.get("notBefore"),
"notAfter": cert.get("notAfter")
}
except Exception as e:
return str(e)
def check_dnssec(domain):
try:
answers = dns.resolver.resolve(domain, 'DNSKEY')
return True if answers else False
except Exception as e:
return str(e)
def get_email_security_records(domain):
records = {}
for record_type in ['SPF', 'DMARC']:
try:
answers = dns.resolver.resolve(domain, record_type)
records[record_type] = [str(r) for r in answers]
except Exception as e:
records[record_type] = str(e)
return records
def check_website_content(domain):
try:
response = requests.get(f"http://{domain}")
return response.text[:1000]
except requests.RequestException as e:
return str(e)
def measure_website_performance(domain):
try:
response = requests.get(f"http://{domain}")
timing = response.elapsed.total_seconds()
return {'load_time_seconds': timing}
except requests.RequestException as e:
return str(e)
domain = "example.com"
print("WHOIS:", get_whois(domain))
print("DNS Records:", get_dns_records(domain))
print("SSL Certificate:", get_ssl_certificate(domain))
print("DNSSEC Validation:", check_dnssec(domain))
print("SPF And DMARC Records:", get_email_security_records(domain))
print("Website Content Preview:", check_website_content(domain))
print("Website Performance:", measure_website_performance(domain))
def get_reverse_dns(ip):
try:
return socket.gethostbyaddr(ip)
except socket.herror:
return None
def get_ip_geolocation(ip):
try:
response = httpx.get(f"https://ipinfo.io/{ip}/json")
return response.json()
except Exception as e:
return str(e)
def check_port(ip, port, timeout=1):
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
sock.settimeout(timeout)
if sock.connect_ex((ip, port)) == 0:
return port
return None
def scan_open_ports(ip, ports_range=100, max_threads=50):
ports = range(1, ports_range + 1)
open_ports = []
with ThreadPoolExecutor(max_workers=max_threads) as executor:
futures = [executor.submit(check_port, ip, port) for port in ports]
for future in as_completed(futures):
result = future.result()
if result:
open_ports.append(result)
return open_ports
def get_whois_info(ip):
try:
obj = ipwhois.IPWhois(ip)
results = obj.lookup_rdap(depth=1)
return results
except Exception as e:
return str(e)
def get_ssl_certificate(ip, port=443):
try:
context = ssl.create_default_context()
with socket.create_connection((ip, port)) as sock:
with context.wrap_socket(sock, server_hostname=ip) as ssock:
cert = ssock.getpeercert()
return cert
except Exception as e:
return str(e)
def grab_banner(ip, port, timeout=2):
try:
s = socket.socket()
s.settimeout(timeout)
s.connect((ip, port))
banner = s.recv(1024)
s.close()
return banner.decode().strip()
except Exception as e:
return str(e)
def ping_latency(ip, count=4):
try:
output = subprocess.run(["ping", "-c", str(count), ip], capture_output=True, text=True)
return output.stdout
except subprocess.CalledProcessError as e:
return str(e)
ip = "8.8.8.8"
port = 80
print("Reverse DNS:", get_reverse_dns(ip))
print("Geolocation:", get_ip_geolocation(ip))
print("Open Ports:", scan_open_ports(ip))
print("WHOIS Information:", get_whois_info(ip))
print("SSL Certificate Information:", get_ssl_certificate(ip))
print("Banner:", grab_banner(ip, port))
print("Latency Ping:", ping_latency(ip))