-
Notifications
You must be signed in to change notification settings - Fork 610
/
driver_command.rb
173 lines (143 loc) · 4.17 KB
/
driver_command.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
##
# driver_command.rb
# Created August 29, 2015
# By Ron Bowes
#
# See: LICENSE.md
##
require 'shellwords'
require 'drivers/command_packet'
require 'drivers/driver_command_commands'
require 'drivers/driver_command_tunnels'
class DriverCommand
include DriverCommandCommands
include DriverCommandTunnels
attr_reader :stopped
@@mutex = Mutex.new()
def request_id()
id = @request_id
@request_id += 1
return id
end
def _get_pcap_window()
id = "cmdpcap#{@window.id}"
if(SWindow.exists?(id))
return SWindow.get(id)
end
return SWindow.new(@window, false, {
:id => id,
:name => "dnscat2 command protocol window for session #{@window.id}",
:noinput => true,
})
end
def _send_request(request, callback)
# Make sure this is synchronous so threads don't fight
@@mutex.synchronize() do
if(callback)
@handlers[request.get(:request_id)] = {
:request => request,
:proc => callback
}
end
if(Settings::GLOBAL.get("packet_trace"))
window = _get_pcap_window()
window.puts("OUT: #{request}")
end
out = request.serialize()
out = [out.length, out].pack("Na*")
@outgoing += out
end
end
def initialize(window, settings)
@window = window
@settings = settings
@outgoing = ""
@incoming = ""
@request_id = 0x0001
@commander = Commander.new()
@handlers = {}
@stopped = false
_register_commands()
_register_commands_tunnels()
@window.on_input() do |data|
# This replaces any $variables with
data = @settings.do_replace(data)
begin
@commander.feed(data)
rescue ArgumentError => e
@window.puts("Error: #{e}")
@commander.educate(data, @window)
end
end
@window.puts("This is a command session!")
@window.puts()
@window.puts("That means you can enter a dnscat2 command such as")
@window.puts("'ping'! For a full list of clients, try 'help'.")
@window.puts()
# This creates the window early for a slightly better UX (it doesn't pop up after their first command)
if(Settings::GLOBAL.get("packet_trace"))
window = _get_pcap_window()
end
end
def _handle_incoming(command_packet)
if(Settings::GLOBAL.get("packet_trace"))
window = _get_pcap_window()
window.puts("IN: #{command_packet}")
end
if(command_packet.get(:is_request))
tunnel_data_incoming(command_packet)
return
end
handler = @handlers.delete(command_packet.get(:request_id))
if(handler.nil?)
@window.puts("Received a response that we have no record of sending:")
@window.puts("#{command_packet}")
@window.puts()
@window.puts("Here are the responses we're waiting for:")
@handlers.each_pair do |request_id, the_handler|
@window.puts("#{request_id}: #{the_handler[:request]}")
end
return
end
if(command_packet.get(:command_id) == CommandPacket::COMMAND_ERROR)
@window.puts("Client returned an error: #{command_packet.get(:status)} :: #{command_packet.get(:reason)}")
return
end
if(handler[:request].get(:command_id) != command_packet.get(:command_id))
@window.puts("Received a response of a different packet type (that's really weird, please report if you can reproduce!")
@window.puts("#{command_packet}")
@window.puts()
@window.puts("The original packet was:")
@window.puts("#{handler}")
return
end
handler[:proc].call(handler[:request], command_packet)
end
def feed(data)
@incoming += data
loop do
if(@incoming.length < 4)
break
end
# Try to read a length + packet
length, data = @incoming.unpack("Na*")
# If there isn't enough data, give up
if(data.length < length)
break
end
# Otherwise, remove what we have from @data
length, data, @incoming = @incoming.unpack("Na#{length}a*")
_handle_incoming(CommandPacket.parse(data))
end
# Return the queue and clear it out
result = @outgoing
@outgoing = ''
return result
end
def request_stop()
@stopped = true
end
def shutdown()
tunnels_stop()
end
end