-
Notifications
You must be signed in to change notification settings - Fork 1
/
libgestalt.rb
414 lines (339 loc) · 10.7 KB
/
libgestalt.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
require 'socket'
require 'twofish'
require 'pp'
require 'base64'
require 'digest'
require 'rc4'
require 'zlib'
class Gestalt
MSGID_LOGIN = 0x01
MSGID_GET_SETTINGS = 0x0c
MSGID_GET_SITES = 0x2c
MSGID_CHANGE_PASSWORD = 0x4d
MSGID_GET_TER = 0x138 # Trial Extension Request
MSGID_REGISTER_COMMAND = 0x99
MSGID_DELETE_COMMAND = 0x9a
MSGID_GET_DATA = 0x173
MSGID_COMPRESSED = 0xff7f
LOGIN_ETR = 0
LOGIN_LOGGED_IN_USER = 1
LOGIN_WINDOWS = 2
LOGIN_ARCUS = 3
ERRORS = {
0 => "Success",
7 => "Parsing error",
11 => "Login failed",
52 => "Command not found",
65280 => "Command already exists",
}
# As far as I can tell, the protocol doesn't have any way to properly tell
# the type of each field, and instead hardcodes the parsers. Thankfully, they
# have a length field so we can handle unknowns!
TYPES = {
'AMID' => {
name: "Login type",
type: :int32,
values: {
0 => "EFT Authentication",
1 => "Currently logged on user",
2 => "Windows authentication",
3 => "Arcus RDGW",
},
},
'PSWD' => {
name: "Encrypted password",
type: :bytes,
},
'ADLN' => {
name: "Username",
type: :string,
},
}
attr_accessor :s
def self.decrypt_password(b)
# Static 5-byte key for extra security
tf = Twofish.new("tfgry\0\0\0\0\0\0\0\0\0\0\0", :padding => :zero_byte, :mode => :cbc)
# By default, the library randomizes the IV, so set it specifically
tf.iv = "\0" * 16
# We need to append a NUL byte, because this is UTF-16 and zero-padded, which
# means the final character is truncated (lol)
return (tf.decrypt(b) + "\0").force_encoding("UTF-16LE").encode("ASCII-8BIT")
end
def self.encrypt_password(pw)
# Encode as UTF-16
pw = pw.encode("UTF-16LE")
# Static 5-byte key for extra security
tf = Twofish.new("tfgry\0\0\0\0\0\0\0\0\0\0\0", :padding => :zero_byte, :mode => :cbc)
# By default, the library randomizes the IV, so set it specifically
tf.iv = "\0" * 16
return tf.encrypt(pw)
end
def self.encode_type5(name, value)
return [name, 5, value.length, value].pack('a4VVa*')
end
def self.encode_int(name, value, unk1: 5)
return encode_type5(name, [value].pack('V'))
end
def self.encode_str(name, value, unk1: 5, length: nil)
length = length.nil? ? -(value.length + 1) : length
# Create the "data" portion
str = [length, *value.bytes].pack('Vv*')
return encode_type5(name, str)
end
def self.encode_uuid(name, value, unk1: 5)
return encode_type5(name, value)
end
def self.encode_bytes(name, value, unk1: 5, length: nil)
length = length.nil? ? value.length : length
# Create the "data" portion
str = [length, value].pack('Va*')
return encode_type5(name, str)
end
def self.encode_array(name, value, unk1: 5)
# Observed that it starts with a 1 - possibly this is an array size?
out = [1].pack('V')
# Encode the fields
value.each do |v|
case v[:type]
when :string
out.concat([(~v[:value].length) & 0x0FFFFFFFF, *v[:value].bytes].pack('Vv*'))
when :int32
out.concat([v[:value]].pack('V'))
when :uuid_array
out.concat([v[:value].length].pack('V'))
v[:value].each do |uuid|
out.concat([uuid].pack('a16'))
end
else
raise "Unknown type in array: #{ v[:type] }"
end
end
# Prepend the name + unk1 + length
out = [name, unk1, out.length, out].pack('a4VVa*')
return out
end
def self.do_compress(message)
message = Zlib::deflate(message)
return [message.length + 8, MSGID_COMPRESSED, message].pack('VVa*')
end
def self.flip_bits(message, bits)
message = message.unpack('b*').pop
bits.each do |bit|
puts "bit = #{bit}"
if message[bit] == '0'
message[bit] = '1'
else
message[bit] = '0'
end
end
return [message].pack('b*')
end
def self.fuzz(message)
changes = 5 - Math.log10(rand(0..100000)).floor
return flip_bits(message, 1.upto(changes).map { rand(0..message.length) })
end
def attach_header(data, msgid: 1)
message = [data.length + 8, msgid, data].pack('VVa*')
0.upto(@compress - 1) do
if @fuzz
message = Gestalt.fuzz(message)
end
message = Gestalt.do_compress(message)
end
if @fuzz
message = Gestalt.fuzz(message)
end
return message
end
def self.parse_packet(p)
File.write('/tmp/packet', p)
length, msgid, body = p.unpack('VVa*')
# Start building the result
result = {
length: length,
msgid: msgid,
# body: body,
args: {}
}
# 0xff7f is a compressed packet - do what they do and recurse
if msgid == MSGID_COMPRESSED
puts "Compressed message!"
return self.parse_packet(Zlib::inflate(body))
end
# Parse all the parameters
loop do
# Stop when the body length is too short
if body.length < 4
break
end
# Each field appears to start with a 4-byte name then 4-byte type
name, type, body = body.unpack('a4Va*')
# Type 1 = a 4-byte integer
if type == 1
if body.length < 4
raise "Can't parse integer, we ran out of data!"
end
value, body = body.unpack('Va*')
result[:args][name] = {
type: :int,
value: value,
}
# Type 5 = variable types, it seems
elsif type == 5
# There's always a 4-byte length
if body.length < 4
raise "Can't parse byte array length, we ran out of data!"
end
length, body = body.unpack('Va*')
if body.length < length
raise "Can't parse byte array, the size (#{ length }) would go off the end of the remaining body (#{ body.length })!"
end
# Get the data
data, body = body.unpack("a#{length}a*")
# From here, we don't necessarily know what to do next - consult the
# list of stuff we've identified, or just do our best
type = TYPES[name]
if type
if type[:type] == :int32
value, data = data.unpack('Va*')
if type[:values]
result[:args][name] = {
type: type[:type],
length: 4,
data: value,
data_str: type[:values][value] || "Unknown: #{ value }",
name: type[:name],
}
else
result[:args][name] = {
type: type[:type],
length: 4,
data: value,
name: type[:name],
}
end
elsif type[:type] == :string
# These are prefixed with a negative length
other_length, data = data.unpack('Va*')
if (other_length & 0x80000000) == 0
raise "Length is supposed to be negative for name #{name}: 0x#{length.to_s(16)}"
end
other_length = (~other_length) & 0x0FFFFFFFF
# Read the data at UTF16, followed by the rest of the data, then pop it off
value = data.unpack("v#{other_length}a*")
data = value.pop
# Convert to characters
value = value.map(&:chr).join
result[:args][name] = {
type: type[:type],
length: other_length,
data: value,
name: type[:name],
}
elsif type[:type] == :bytes
# This is prefixed with a positive length
other_length, data = data.unpack('Va*')
if (other_length & 0x80000000) != 0
raise "Length is not supposed to be negative for name #{name}: 0x#{other_length.to_s(16)}"
end
# Read the data at UTF16, followed by the rest of the data, then pop it off
value, data = data.unpack("a#{other_length}a*")
result[:args][name] = {
type: type[:type],
length: other_length,
data: value,
name: type[:name],
}
else
raise "We are trying to use an unidentified type for name #{ name }: #{ type[:type] }"
end
if data != ''
$stderr.puts "WARNING: Unparsed data: #{ data.unpack('H*') }"
end
else
# Do our best!
result[:args][name] = {
type: :unknown,
length: length,
data: data,
name: name,
}
end
elsif type == 4 # It seems like a utf-16 array
if body.length < 4
raise "Can't parse UTF16 string length, we ran out of data!"
end
length, body = body.unpack('Va*')
if body.length < length
raise "Can't parse UTF16 string, the size (#{ length }) would go off the end of the remaining body (#{ body.length })!"
end
# Reads the UTF16 string + whatever is after it
data = body.unpack("v#{length}a*")
body = data.pop # Get the last element (the body)
result[:args][name] = {
type: :string,
length: length,
data: data.pack('C*'),
}
else
raise "Unknown value for type: #{ type }"
end
end
return result
end
def receive_message()
header = @s.recv(4)
if !header || header.length != 4
raise "Couldn't receive the header!"
end
length = header.unpack('V').pop
body = @s.recv(length - 4)
if !body || body.length != length - 4
raise "Couldn't receive the body!"
end
# We need to parse it with the length attached, because compressed packets
# want recursion
result = Gestalt.parse_packet(header + body)
return result
end
def send_recv(msgid, packet)
packet = attach_header(packet, msgid: msgid)
puts "Sending: #{ packet.unpack('H*') }"
@s.write(packet)
return receive_message()
end
def get_sites()
data = send_recv(MSGID_GET_SITES, '')
data = data[:args]['USRS'][:data]
length, data = data.unpack('Va*')
puts length
sites = []
1.upto(length) do
site, unk_00000000, data = data.unpack('a16Va*')
sites << site
end
return sites
end
def initialize(host, port = 1100, fuzz: false, compress: 0)
@s = TCPSocket.new(host, port)
@fuzz = fuzz
@compress = compress
return receive_message()
end
def authenticate(username, password, login_type = LOGIN_ETR)
return send_recv(MSGID_LOGIN, '' +
Gestalt.encode_bytes('PSWD', Gestalt.encrypt_password(password)) +
Gestalt.encode_str('ADLN', username) +
Gestalt.encode_int('AMID', login_type)
)
end
def get_ter()
return send_recv(MSGID_GET_TER, '')
end
def close
if @s
@s.close
@s = nil
end
end
end