-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSSH-Frank.da
More file actions
266 lines (195 loc) · 10.9 KB
/
SSH-Frank.da
File metadata and controls
266 lines (195 loc) · 10.9 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
"""
Secure Shell (SSH) Transport Layer Protocol
Source: T. Ylonen
"RFC: 4253 - The Secure Shell (SSH) Transport Layer Protocol"
January 2006
SSH Key Exchange Protocol Diagram (RFC 4253, p. 17-p. 21)
1. Client -> Server : Algorithm Packets
2. *Client -> Server : Guess Attempt
3. Server -> Client : Algorithm Packets
4. *Server -> Client : Guess Attempt
5. Client -> Server : Key Exchange Request
6. Server -> Client : Key Exchange
"""
from sa.secalgo import *
from sa.sec_algo_pycrypto import *
from Crypto.Hash import SHA256
configure(verify_returns = 'bool')
configure(benchmark = True)
#Message Numbers
SSH_MSG_KEXINIT = 20
SSH_MSG_KexDH_INIT = 30 # Not Explicitly showed in the paper
SSH_MSG_KexDH_REPLY = 31 # Not Explicitly showed in the paper
SSH_MSG_NEWKEYS = 32 # Not Explicitly showed in the paper
# If there is no guess attempt or the guess attempt is wrong, use this method to iterate through each namelist containing protocols
# The first parameter SHOULD always be client's list. The second parameter SHOULD always be server's list
# This method compares the CLIENTLIST to the SERVERLIST! NOT the other way around!
def iterate_protocol(client_list, server_list):
for protocol_client in client_list:
if protocol_client in server_list :
return protocol_client
return None
#Here, the client and server basically does the same thing. After they both exchange protocol namelists, they start iterating through the namelists. The whole process ends when both server and client has a series of protocol to use
#Also, we assume that both client and server will use Diffie-Hellman Key Exchange
class ssh_client(process):
def setup(kex_algo_client,
server_host_key_algo_client,
encryption_ctos_client,
encryption_stoc_client,
mac_ctos_client,
compression_ctos_client,
conpression_stoc_client,
languages_ctos_client,
language_stoc_client,
guess_client, #Boolean value. Determines if a guess attempt is made
host_key_server_public, #Server Public Key
server):
pass
def run():
cookie_client = keygen('random', 16)
client_list = (kex_algo_client,
server_host_key_algo_client,
encryption_ctos_client,
encryption_stoc_client,
mac_ctos_client,
compression_ctos_client,
conpression_stoc_client,
languages_ctos_client,
language_stoc_client,
host_key_server_public)
I_C = (SSH_MSG_KEXINIT, cookie_client, client_list, guess_client, 0) #The payload of the SSH_MSG_KEXINIT message. Will be used later
print("SSH starts")
send((SSH_MSG_KEXINIT, (cookie_client, client_list, guess_client, 0)), to = server)
print("Client: Sends the first message(SSH_MSG_KEXINIT)")
await(some(received((_SSH_MSG_KEXINIT, cookie_server, server_list, guess_server, reserved_server), from_ = server)))
print("Client: Recevied the second message from the server")
if (guess_server & guess_client):
print("Client: Guess Attempt not implemented. Start iterating name-lists...")
else:
print("Client: No Guess attempt made. Start iterating name-lists...")
agreed_algorithms = [] #Save the algorithms that will be used later
both_lists = zip(client_list, server_list)
for pair in both_lists:
agreed_algorithms.append(iterate_protocol(pair[0], pair[1]))
print("Client: Done iterating protocols. Agreed algorithms saved for late use")
x = int.from_bytes(keygen('random', 16), byteorder = 'little')
g = modp_groups[5]["g"]
p = modp_groups[5]["p"]
e = pow(g,x,p)
send((SSH_MSG_KexDH_INIT, e), to = server)
print("Client: Sends the third message to the server")
await(some(received((_SSH_MSG_KexDH_REPLY, K_S, f, sign_H), from_ = server)))
print("Client: Received the fourth message from the server")
I_S = (SSH_MSG_KEXINIT, cookie_server, server_list, guess_server, reserved_server) # The payload of
k = pow(f,x,p)
hash = (self, server, I_C, I_S, K_S, e, f, k)
if verify((hash, sign_H), K_S):
send(SSH_MSG_NEWKEYS, to = server)
print("Client: Sends the fifth message to the server")
await(some(received(_SSH_MSG_NEWKEYS, from_ = server)))
print("Client: Key exchange and algorithm negotiation completed. New key will be used from now on. ")
else:
print("Verification failed. Client and Server disconnected")
class ssh_server(process):
def setup(kex_algo_server,
server_host_key_algo_server,
encryption_ctos_server,
encryption_stoc_server,
mac_ctos_server,
compression_ctos_server,
conpression_stoc_server,
languages_ctos_server,
language_stoc_server,
guess_server,
host_key_server_private,
host_key_server_public):
pass
def run():
await(False)
def receive(msg=(_SSH_MSG_KEXINIT, (cookie_client, client_list, guess_client, reserved_client)), from_=client):
print("Server: Received the first message from client")
cookie_server = keygen('random', 16)
I_C = (SSH_MSG_KEXINIT, cookie_client, client_list, guess_client, reserved_client)
server_list = (kex_algo_server,
server_host_key_algo_server,
encryption_ctos_server,
encryption_stoc_server,
mac_ctos_server,
compression_ctos_server,
conpression_stoc_server,
languages_ctos_server,
language_stoc_server)
print("Server: Sends the second message to the client")
send((SSH_MSG_KEXINIT, cookie_server, server_list, guess_server, 0), to = client)
I_S = (SSH_MSG_KEXINIT, cookie_server, server_list, guess_server, 0)
agreed_algorithms = []
both_lists = zip(client_list, server_list)
for pair in both_lists:
agreed_algorithms.append(iterate_protocol(pair[0], pair[1]))
await(some(received((_SSH_MSG_KexDH_INIT, e), from_ = server)))
print("Server: Received third message from the client")
y = int.from_bytes(keygen('random', 16), byteorder = 'little')
g = modp_groups[5]["g"]
p = modp_groups[5]["p"]
f = pow(g,y,p)
k = pow(e,y,p)
hash = (client, self, I_C, I_S, host_key_server_public, e, f, k)
s = sign(hash, key = host_key_server_private)
send((SSH_MSG_KexDH_REPLY, host_key_server_public, f, s), to = client)
print("Server: Sends fourth message to the clinet")
await(some(received(_SSH_MSG_NEWKEYS, from_ = client)))
print("Server: Received the fifth message from the client")
send(SSH_MSG_NEWKEYS, to = client)
print("Server: Key exchange and algorithm negotiation completed. New key will be used from now on. ")
def main():
host_key_server_private, host_key_server_public = keygen('public')
kex_algo_client = ["DF"]
server_host_key_algo_client = ["DSA"]
encryption_ctos_client = ["AES"]
encryption_stoc_client = ["AES"]
mac_ctos_client = ["SHA-1"]
compression_ctos_client = ["none"]
conpression_stoc_client = ["none"]
languages_ctos_client = []
language_stoc_client = []
guess_client = False
kex_algo_server = ["DF"]
server_host_key_algo_server = ["DSA"]
encryption_ctos_server = ["AES"]
encryption_stoc_server = ["AES"]
mac_ctos_server = ["SHA-1"]
compression_ctos_server = ["none"]
conpression_stoc_server = ["none"]
languages_ctos_server = []
language_stoc_server = []
guess_server = False
client = new(ssh_client)
server = new(ssh_server)
setup(client,
(kex_algo_client,
server_host_key_algo_client,
encryption_ctos_client,
encryption_stoc_client,
mac_ctos_client,
compression_ctos_client,
conpression_stoc_client,
languages_ctos_client,
language_stoc_client,
guess_client,
host_key_server_public,
server))
setup(server,
(kex_algo_server,
server_host_key_algo_server,
encryption_ctos_server,
encryption_stoc_server,
mac_ctos_server,
compression_ctos_server,
conpression_stoc_server,
languages_ctos_server,
language_stoc_server,
guess_server,
host_key_server_private,
host_key_server_public))
start(server)
start(client)