-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsmtp_timing.py
138 lines (112 loc) · 4.58 KB
/
smtp_timing.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
import socket
import time
import statistics
# SMTP server
SMTP_SERVER = 'mail.server.com'
SMTP_PORT = 25
# Lists of valid and invalid email addresses (without domain)
valid_users = [
'info',
'sales',
'contact'
'joe',
'dave'
]
invalid_users = [
'ksdfkjasjfk',
'flforkmdkd',
'lkapwoekropkf',
'lkso',
'lkasdflkjas',
'ksjfljalf',
'kjfoiewjfsdjf',
'lokjfopwkfsadk',
'skljfklasdjfkl',
'skfdjlksdjflsj'
]
# Domain(s)
domains = ['server.com']
# Number of tests per email address
TEST_COUNT = 10
# Tolerance for timing differences (in seconds)
TIMING_THRESHOLD = 0.1
# Function to send commands and capture the response time
def send_smtp_command(sock, command):
sock.send(command.encode())
return sock.recv(1024).decode()
# Measuring the average response times for valid addresses
valid_avg_times = []
for domain in domains:
for user in valid_users:
email = f'{user}@{domain}'
times = []
responses = [] # List to store all server responses
status_codes = [] # List to store all status codes
print(f"Measuring response times for {email} (valid address)...")
for i in range(TEST_COUNT):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((SMTP_SERVER, SMTP_PORT))
send_smtp_command(sock, 'HELO fingonlab.tech\r\n')
send_smtp_command(sock, 'MAIL FROM:<[email protected]>\r\n')
start_time = time.time()
response = send_smtp_command(sock, f'RCPT TO:<{email}>\r\n')
end_time = time.time()
times.append(end_time - start_time)
responses.append(response)
# Extracting the status code
status_code = int(response.split()[0])
status_codes.append(status_code)
send_smtp_command(sock, 'QUIT\r\n')
sock.close()
time.sleep(1)
# Calculate the average time for this address
valid_avg_times.append(statistics.mean(times))
print(f"Server responses for {email}:")
for response, status_code in zip(responses, status_codes):
if status_code > 299:
print(f"\033[91mResponse: {response.strip()}, Status Code: {status_code}\033[0m")
else:
print(f"Response: {response.strip()}, Status Code: {status_code}")
# Measuring the average response times for invalid addresses
invalid_avg_times = []
for domain in domains:
for user in invalid_users:
email = f'{user}@{domain}'
times = []
responses = [] # List to store all server responses
status_codes = [] # List to store all status codes
print(f"Measuring response times for {email} (invalid address)...")
for i in range(TEST_COUNT):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((SMTP_SERVER, SMTP_PORT))
send_smtp_command(sock, 'HELO fingonlab.tech\r\n')
send_smtp_command(sock, 'MAIL FROM:<[email protected]>\r\n')
start_time = time.time()
response = send_smtp_command(sock, f'RCPT TO:<{email}>\r\n')
end_time = time.time()
times.append(end_time - start_time)
responses.append(response) # Storing server response
# Extracting the status code
status_code = int(response.split()[0])
status_codes.append(status_code)
send_smtp_command(sock, 'QUIT\r\n')
sock.close()
time.sleep(1)
# Calculate the average time for this address
invalid_avg_times.append(statistics.mean(times))
# Display server responses and status codes
print(f"Server responses for {email}:")
for response, status_code in zip(responses, status_codes):
print(f"Response: {response.strip()}, Status Code: {status_code}")
# Calculate the overall averages for valid and invalid email addresses
mean_valid = statistics.mean(valid_avg_times)
mean_invalid = statistics.mean(invalid_avg_times)
# Display results
print("\nResults:")
print(f"Overall average time for valid addresses: {mean_valid:.4f} seconds")
print(f"Overall average time for invalid addresses: {mean_invalid:.4f} seconds")
# Check for timing attack indication
if abs(mean_valid - mean_invalid) > TIMING_THRESHOLD:
print(f"Possible timing attack detected! Difference: {abs(mean_valid - mean_invalid):.4f} seconds")
else:
print("No timing attack detected.")