forked from iagox86/dnscat2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
packet.rb
404 lines (315 loc) · 11.2 KB
/
packet.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
##
# packet.rb
# Created March, 2013
# By Ron Bowes
#
# See: LICENSE.md
#
# Builds and parses dnscat2 packets.
##
require 'controller/crypto_helper'
require 'libs/dnscat_exception'
require 'libs/hex'
module PacketHelper
def at_least?(data, needed)
return (data.length >= needed)
end
def exactly?(data, needed)
return (data.length == needed)
end
end
class Packet
extend PacketHelper
# Message types
MESSAGE_TYPE_SYN = 0x00
MESSAGE_TYPE_MSG = 0x01
MESSAGE_TYPE_FIN = 0x02
MESSAGE_TYPE_PING = 0xFF
MESSAGE_TYPE_ENC = 0x03
OPT_NAME = 0x0001
# OPT_TUNNEL = 0x0002 # Deprecated
# OPT_DATAGRAM = 0x0004 # Deprecated
# OPT_DOWNLOAD = 0x0008 # Deprecated
# OPT_CHUNKED_DOWNLOAD = 0x0010 # Deprecated
OPT_COMMAND = 0x0020
attr_reader :packet_id, :type, :session_id, :body
class SynBody
extend PacketHelper
attr_reader :seq, :options, :name
def initialize(options, params = {})
@options = options || raise(DnscatException, "options can't be nil!")
@seq = params[:seq] || raise(DnscatException, "params[:seq] can't be nil!")
if((@options & OPT_NAME) == OPT_NAME)
@name = params[:name] || raise(DnscatException, "params[:name] can't be nil when OPT_NAME is set!")
else
@name = "(unnamed)"
end
end
def SynBody.parse(data)
at_least?(data, 4) || raise(DnscatException, "Packet is too short (SYN)")
seq, options, data = data.unpack("nna*")
# Parse the option name, if it has one
name = nil
if((options & OPT_NAME) == OPT_NAME)
if(data.index("\0").nil?)
raise(DnscatException, "OPT_NAME set, but no null-terminated name given")
end
name, data = data.unpack("Z*a*")
else
name = "[unnamed]"
end
# Verify that that was the entire packet
if(data.length > 0)
raise(DnscatException, "Extra data on the end of an SYN packet :: #{data.unpack("H*")}")
end
return SynBody.new(options, {
:seq => seq,
:name => name,
})
end
def to_s()
return "[[SYN]] :: isn = %04x, options = %04x, name = %s" % [@seq, @options, @name]
end
def to_bytes()
result = [@seq, @options].pack("nn")
if((@options & OPT_NAME) == OPT_NAME)
result += [@name].pack("Z*")
end
return result
end
end
class MsgBody
extend PacketHelper
attr_reader :seq, :ack, :data
def initialize(options, params = {})
@options = options
@seq = params[:seq] || raise(DnscatException, "params[:seq] can't be nil!")
@ack = params[:ack] || raise(DnscatException, "params[:ack] can't be nil!")
@data = params[:data] || raise(DnscatException, "params[:data] can't be nil!")
end
def MsgBody.parse(options, data)
at_least?(data, 4) || raise(DnscatException, "Packet is too short (MSG norm)")
seq, ack = data.unpack("nn")
data = data[4..-1] # Remove the first four bytes
return MsgBody.new(options, {
:seq => seq,
:ack => ack,
:data => data,
})
end
def MsgBody.header_size(options)
return MsgBody.new(options, {
:seq => 0,
:ack => 0,
:data => '',
}).to_bytes().length()
end
def to_s()
return "[[MSG]] :: seq = %04x, ack = %04x, data = 0x%x bytes" % [@seq, @ack, data.length]
end
def to_bytes()
result = ""
seq = @seq || 0
ack = @ack || 0
result += [seq, ack, @data].pack("nna*")
return result
end
end
class FinBody
extend PacketHelper
attr_reader :reason
def initialize(options, params = {})
@options = options
@reason = params[:reason] || raise(DnscatException, "params[:reason] can't be nil!")
end
def FinBody.parse(options, data)
at_least?(data, 1) || raise(DnscatException, "Packet is too short (FIN)")
reason = data.unpack("Z*").pop
data = data[(reason.length+1)..-1]
if(data.length > 0)
raise(DnscatException, "Extra data on the end of a FIN packet")
end
return FinBody.new(options, {
:reason => reason,
})
end
def to_s()
return "[[FIN]] :: %s" % [@reason]
end
def to_bytes()
[@reason].pack("Z*")
end
end
class PingBody
extend PacketHelper
attr_reader :data
def initialize(options, params = {})
@options = options
@data = params[:data] || raise(DnscatException, "params[:data] can't be nil!")
end
def PingBody.parse(options, data)
at_least?(data, 3) || raise(DnscatException, "Packet is too short (PING)")
data = data.unpack("Z*").pop
return PingBody.new(options, {
:data => data,
})
end
def to_s()
return "[[PING]] :: %s" % [@data]
end
def to_bytes()
[@data].pack("Z*")
end
end
class EncBody
extend PacketHelper
SUBTYPE_INIT = 0x0000
SUBTYPE_AUTH = 0x0001
attr_reader :subtype, :flags
attr_reader :public_key_x, :public_key_y # SUBTYPE_INIT
attr_reader :authenticator # SUBTYPE_AUTH
def initialize(params = {})
@subtype = params[:subtype] || raise(DnscatException, "params[:subtype] is required!")
@flags = params[:flags] || raise(DnscatException, "params[:flags] is required!")
if(@subtype == SUBTYPE_INIT)
@public_key_x = params[:public_key_x] || raise(DnscatException, "params[:public_key_x] is required!")
@public_key_y = params[:public_key_y] || raise(DnscatException, "params[:public_key_y] is required!")
if(!@public_key_x.is_a?(Integer) || !@public_key_y.is_a?(Integer))
raise(DnscatException, "Public keys have to be Integers! (Seen: #{@public_key_x.class} #{@public_key_y.class})")
end
elsif(@subtype == SUBTYPE_AUTH)
@authenticator = params[:authenticator] || raise(DnscatException, "params[:authenticator] is required!")
if(@authenticator.length != 32)
raise(DnscatException, "params[:authenticator] was the wrong size!")
end
else
raise(DnscatException, "Unknown subtype: #{@subtype}")
end
end
def EncBody.parse(data)
at_least?(data, 4) || raise(DnscatException, "ENC packet is too short!")
subtype, flags, data = data.unpack("nna*")
params = {
:subtype => subtype,
:flags => flags,
}
if(subtype == SUBTYPE_INIT)
exactly?(data, 64) || raise(DnscatException, "ENC packet is too short!")
public_key_x, public_key_y, data = data.unpack("a32a32a*")
params[:public_key_x] = CryptoHelper.binary_to_bignum(public_key_x)
params[:public_key_y] = CryptoHelper.binary_to_bignum(public_key_y)
elsif(subtype == SUBTYPE_AUTH)
exactly?(data, 32) || raise(DnscatException, "ENC packet is too short!")
authenticator, data = data.unpack("a32a*")
params[:authenticator] = authenticator
else
raise(DnscatException, "Unknown subtype: #{subtype}")
end
if(data != "")
raise(DnscatException, "Extra data on the end of an ENC packet")
end
return EncBody.new(params)
end
def to_s()
if(@subtype == SUBTYPE_INIT)
return "[[ENC|INIT]] :: flags = 0x%04x, pubkey = %s,%s" % [@flags, CryptoHelper.bignum_to_text(@public_key_x), CryptoHelper.bignum_to_text(@public_key_y)]
elsif(@subtype == SUBTYPE_AUTH)
return "[[ENC|AUTH]] :: flags = 0x%04x, authenticator = %s" % [@flags, @authenticator.unpack("H*").pop()]
else
raise(DnscatException, "Unknown subtype: #{@subtype}")
end
end
def to_bytes()
if(@subtype == SUBTYPE_INIT)
public_key_x = CryptoHelper.bignum_to_binary(@public_key_x)
public_key_y = CryptoHelper.bignum_to_binary(@public_key_y)
return [@subtype, @flags, public_key_x, public_key_y].pack("nna32a32")
elsif(@subtype == SUBTYPE_AUTH)
return [@subtype, @flags, @authenticator].pack("nna32")
else
raise(DnscatException, "Unknown subtype: #{@subtype}")
end
end
end
def ==(other_packet)
return self.to_bytes() == other_packet.to_bytes()
end
# You probably don't ever want to use this, call Packet.parse() or Packet.create_*() instead
def initialize(packet_id, type, session_id, body)
@packet_id = packet_id || rand(0xFFFF)
@type = type || raise(DnscatException, "type can't be nil!")
@session_id = session_id || raise(DnscatException, "session_id can't be nil!")
@body = body
end
def Packet.header_size(options)
return Packet.new(0, 0, 0, nil).to_bytes().length()
end
def Packet.parse_header(data)
at_least?(data, 5) || raise(DnscatException, "Packet is too short (header)")
# (uint16_t) packet_id
# (uint8_t) message_type
# (uint16_t) session_id
packet_id, type, session_id = data.unpack("nCn")
data = data[5..-1]
return packet_id, type, session_id, data
end
def Packet.peek_session_id(data)
_, _, session_id, _ = Packet.parse_header(data)
return session_id
end
def Packet.peek_type(data)
_, type, _, _ = Packet.parse_header(data)
return type
end
def Packet.parse(data, options = nil)
packet_id, type, session_id, data = Packet.parse_header(data)
if(type == MESSAGE_TYPE_SYN)
body = SynBody.parse(data)
elsif(type == MESSAGE_TYPE_MSG)
if(options.nil?)
raise(DnscatException, "Options are required when parsing MSG packets!")
end
body = MsgBody.parse(options, data)
elsif(type == MESSAGE_TYPE_FIN)
if(options.nil?)
raise(DnscatException, "Options are required when parsing FIN packets!")
end
body = FinBody.parse(options, data)
elsif(type == MESSAGE_TYPE_PING)
body = PingBody.parse(nil, data)
elsif(type == MESSAGE_TYPE_ENC)
body = EncBody.parse(data)
else
raise(DnscatException, "Unknown message type: 0x%x" % type)
end
return Packet.new(packet_id, type, session_id, body)
end
def Packet.create_syn(options, params = {})
return Packet.new(params[:packet_id], MESSAGE_TYPE_SYN, params[:session_id], SynBody.new(options, params))
end
def Packet.create_msg(options, params = {})
return Packet.new(params[:packet_id], MESSAGE_TYPE_MSG, params[:session_id], MsgBody.new(options, params))
end
def Packet.create_fin(options, params = {})
return Packet.new(params[:packet_id], MESSAGE_TYPE_FIN, params[:session_id], FinBody.new(options, params))
end
def Packet.create_ping(params = {})
return Packet.new(params[:packet_id], MESSAGE_TYPE_PING, params[:session_id], PingBody.new(nil, params))
end
def Packet.create_enc(params = {})
return Packet.new(params[:packet_id], MESSAGE_TYPE_ENC, params[:session_id], EncBody.new(params))
end
def to_s()
result = "[0x%04x] session = %04x :: %s\n" % [@packet_id, @session_id, @body.to_s]
result += Hex.to_s(to_bytes(), 2)
return result
end
def to_bytes()
result = [@packet_id, @type, @session_id].pack("nCn")
# If we set the body to nil, just return a header (this happens when determining the header length)
if([email protected]?)
result += @body.to_bytes()
end
return result
end
end