forked from iagox86/dnscat2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
encryptor.rb
287 lines (233 loc) · 9.69 KB
/
encryptor.rb
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
##
# encryptor.rb
# Created October, 2015
# By Ron Bowes
#
# See: LICENSE.md
#
##
require 'ecdsa'
require 'salsa20'
require 'securerandom'
require 'sha3'
require 'controller/crypto_helper'
require 'controller/encryptor_sas'
require 'libs/dnscat_exception'
require 'libs/swindow'
class Encryptor
include EncryptorSAS
ECDH_GROUP = ECDSA::Group::Nistp256
@@window = SWindow.new(WINDOW, false, { :noinput => true, :id => "crypto-debug", :name => "Debug window for crypto stuff"})
@@window.puts("This window is for debugging encryption problems!")
@@window.puts("In general, you can ignore it. :)")
@@window.puts()
@@window.puts("One thing to note: you'll see a lot of meaningless errors here,")
@@window.puts("because of retransmissions and such. They don't necessarily mean")
@@window.puts("anything!")
@@window.puts()
@@window.puts("But if you ARE having crypto problems, please send me these")
@@window.puts("logs! Don't worry too much about the private keys; they're")
@@window.puts("session-specific and won't harm anything in the future")
@@window.puts()
class Error < StandardError
end
def _create_key(key_name)
return SHA3::Digest::SHA256.digest(CryptoHelper.bignum_to_binary(@keys[:shared_secret]) + key_name)
end
def _create_authenticator(name, preshared_secret)
return SHA3::Digest::SHA256.digest(name +
CryptoHelper.bignum_to_binary(@keys[:shared_secret]) +
CryptoHelper.bignum_to_binary(@keys[:their_public_key].x) +
CryptoHelper.bignum_to_binary(@keys[:their_public_key].y) +
CryptoHelper.bignum_to_binary(@keys[:my_public_key].x) +
CryptoHelper.bignum_to_binary(@keys[:my_public_key].y) +
preshared_secret
)
end
def initialize(preshared_secret)
@@window.puts("Creating Encryptor with secret: #{preshared_secret}")
@preshared_secret = preshared_secret
@authenticated = false
# Start with encryption turned off
@keys = {
:my_nonce => -1,
:their_nonce => -1,
:my_private_key => nil,
:my_public_key => nil,
:their_public_key => nil,
:shared_secret => nil,
:their_authenticator => nil,
:my_authenticator => nil,
:their_write_key => nil,
:their_mac_key => nil,
:my_write_key => nil,
:my_mac_key => nil,
}
@old_keys = nil
end
# Returns true if something was changed
def set_their_public_key(their_public_key_x, their_public_key_y)
# Check if we're actually changing anything
if(@keys[:their_public_key_x] == their_public_key_x && @keys[:their_public_key_y] == their_public_key_y)
@@window.puts("Attempted to set the same public key!")
return false
end
@old_keys = @keys
@keys = {
:my_nonce => -1,
:their_nonce => -1,
}
if(ready?())
@@window.puts("Wow, this session is old (or the client is needy)! Key re-negotiation requested!")
end
@keys[:my_private_key] = 1 + SecureRandom.random_number(ECDH_GROUP.order - 1)
@keys[:my_public_key] = ECDH_GROUP.generator.multiply_by_scalar(@keys[:my_private_key])
@keys[:their_public_key_x] = their_public_key_x
@keys[:their_public_key_y] = their_public_key_y
@keys[:their_public_key] = ECDSA::Point.new(ECDH_GROUP, their_public_key_x, their_public_key_y)
@keys[:shared_secret] = @keys[:their_public_key].multiply_by_scalar(@keys[:my_private_key]).x
@keys[:their_authenticator] = _create_authenticator("client", @preshared_secret)
@keys[:my_authenticator] = _create_authenticator("server", @preshared_secret)
@keys[:their_write_key] = _create_key("client_write_key")
@keys[:their_mac_key] = _create_key("client_mac_key")
@keys[:my_write_key] = _create_key("server_write_key")
@keys[:my_mac_key] = _create_key("server_mac_key")
@@window.puts("Setting their public key: #{CryptoHelper.bignum_to_text(@keys[:their_public_key_x])} #{CryptoHelper.bignum_to_text(@keys[:their_public_key_y])}")
@@window.puts("Setting my public key: #{CryptoHelper.bignum_to_text(@keys[:my_public_key].x)} #{CryptoHelper.bignum_to_text(@keys[:my_public_key].y)}")
return true
end
def set_their_authenticator(their_authenticator)
if(!@keys[:their_authenticator])
@@window.puts("Tried to set an authenticator too early")
raise(DnscatException, "We weren't ready to set an authenticator!")
end
if(@keys[:their_authenticator] != their_authenticator)
@@window.puts("Tried to set a bad authenticator")
@@window.puts("Expected: #{@keys[:their_authenticator].unpack("H*")}")
@@window.puts("Received: #{their_authenticator.unpack("H*")}")
raise(Encryptor::Error, "Authenticator (pre-shared secret) doesn't match!")
end
@@window.puts("Successfully authenticated the session")
@authenticated = true
end
def to_s(keys = nil)
keys = keys || @keys
out = []
out << "My private key: #{CryptoHelper.bignum_to_text(@keys[:my_private_key])}"
out << "My public key [x]: #{CryptoHelper.bignum_to_text(@keys[:my_public_key].x)}"
out << "My public key [y]: #{CryptoHelper.bignum_to_text(@keys[:my_public_key].y)}"
out << "Their public key [x]: #{CryptoHelper.bignum_to_text(@keys[:their_public_key].x)}"
out << "Their public key [y]: #{CryptoHelper.bignum_to_text(@keys[:their_public_key].y)}"
out << "Shared secret: #{CryptoHelper.bignum_to_text(@keys[:shared_secret])}"
out << ""
out << "Their authenticator: #{@keys[:their_authenticator].unpack("H*")}"
out << "My authenticator: #{@keys[:my_authenticator].unpack("H*")}"
out << ""
out << "Their write key: #{@keys[:their_write_key].unpack("H*")}"
out << "Their mac key: #{@keys[:their_mac_key].unpack("H*")}"
out << "My write key: #{@keys[:my_write_key].unpack("H*")}"
out << "My mac key: #{@keys[:my_mac_key].unpack("H*")}"
out << ""
out << "SAS: #{get_sas()}"
return out.join("\n")
end
def my_public_key_x()
return @keys[:my_public_key].x
end
def my_public_key_x_s()
return CryptoHelper.bignum_to_binary(@keys[:my_public_key].x)
end
def my_public_key_y()
return @keys[:my_public_key].y
end
def my_public_key_y_s()
return CryptoHelper.bignum_to_binary(@keys[:my_public_key].y)
end
def my_nonce()
return @keys[:my_nonce] += 1
end
# We use this special internal function so we can try decrypting with different keys
def _decrypt_packet_internal(keys, data)
# Don't decrypt if we don't have a key set
if(!ready?(keys))
@@window.puts("Not decrypting data (incoming data seemed to be cleartext): #{data.unpack("H*")}")
return data
end
# Parse out the important fields
header, signature, nonce, encrypted_body = data.unpack("a5a6a2a*")
# Put together the data to sign
signed_data = header + nonce + encrypted_body
# Check the signature
correct_signature = SHA3::Digest::SHA256.digest(keys[:their_mac_key] + signed_data)
if(correct_signature[0,6] != signature)
@@window.puts("Couldn't verify packet signature!")
raise(Encryptor::Error, "Invalid signature!")
end
# Check the nonce *after* checking the signature (otherwise, we might update the nonce to a bad value and Bad Stuff happens)
nonce_int = nonce.unpack("n").pop()
if(nonce_int < keys[:their_nonce])
@@window.puts("Client tried to use an invalid nonce: #{nonce_int} < #{keys[:their_nonce]}")
raise(Encryptor::Error, "Invalid nonce!")
end
keys[:their_nonce] = nonce_int
# Decrypt the body
body = Salsa20.new(keys[:their_write_key], nonce.rjust(8, "\0")).decrypt(encrypted_body)
#@@window.puts("Decryption successful")
return header+body
end
# By doing this as a single operation, we can always be sure that we're encrypting data
# with the same key the client use to encrypt data
def decrypt_and_encrypt(data)
## ** Decrypt
keys = @keys
begin
#@@window.puts("Attempting to decrypt with primary key")
data = _decrypt_packet_internal(keys, data)
#@@window.puts("Successfully decrypted with primary key")
# If it was successfully decrypted, make sure the @old_keys will no longer work
@old_keys = nil
rescue Encryptor::Error => e
# Attempt to fall back to old keys
if(@old_keys.nil?)
@@window.puts("No secondary key to fallback to")
raise(e)
end
@@window.puts("Attempting to decrypt with secondary key")
keys = @old_keys
data = _decrypt_packet_internal(@old_keys, data)
@@window.puts("Successfully decrypted with secondary key")
end
# Send the decrypted data up and get the encrypted data back
data = yield(data, ready?(keys))
# If there was an error of some sort, return nothing
if(data.nil? || data == '')
return ''
end
# If encryption is turned off, return unencrypted data
if(!ready?(keys))
@@window.puts("Returning an unencrypted response")
return data
end
## ** Encrypt
#@@window.puts("Encrypting the response")
# Split the packet into a header and a body
header, body = data.unpack("a5a*")
# Encode the nonce properly
nonce = [keys[:my_nonce]].pack("n")
# Encrypt the body
encrypted_body = Salsa20.new(keys[:my_write_key], nonce.rjust(8, "\0")).encrypt(body)
# Sign it
signature = SHA3::Digest::SHA256.digest(keys[:my_mac_key] + header + nonce + encrypted_body)
# Arrange things appropriately
return [header, signature[0,6], nonce, encrypted_body].pack("a5a6a2a*")
end
def my_authenticator()
return @keys[:my_authenticator]
end
def ready?(keys = nil)
return !(keys || @keys)[:shared_secret].nil?
end
def authenticated?()
return @authenticated
end
end