forked from iagox86/dnscat2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
swindow.rb
412 lines (345 loc) · 10.6 KB
/
swindow.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
##
# swindow.rb
# By Ron Bowes
# September, 2015
#
# See LICENSE.md
#
# This implements a fairly simple multi-window buffer.
#
# When included, a thread is created that will listen to stdin and feed the
# input to whichever window is active.
#
# New instances of this class are created to create new windows. The window can
# be switched by calling the activate() or deactivate() functions.
#
# Windows are set up like a tree - when you create a window, you can specify a
# 'parent'. When a window is deactivated or closed, the parent is activated (if
# possible). Typically, you'll want one "master" window, which is the top-most
# window in the tree.
#
# User input is handled by a callback function. The proc that handles user
# input is passed to the on_input() function (which allows it to be changed),
# and it's called each time the user presses <enter>.
#
# The window can be printed to using fairly normal functions - puts, printf,
# print, etc.
#
# Windows are assigned an incremental ID value, and can be referred to as such.
#
# If you want a message to go to a window's parents (or children), a special
# function called with() can be used with a block:
#
# window.with({:to_parent => true}) do
# window.puts("hi")
# end
#
# The following options can be set:
# * :to_parent - sends to the current window and its parent
# * :to_ancestors - sends to the current window, its parent, its parent's parent, etc.
# * :to_children - Sends to the current window, and each of its children
# * :to_descendants - Sends to the current window, its children, its children's children, etc.
#
# Each window also maintains a history of typed comments, up to 1000 lines (by default).
##
require 'readline'
require 'libs/ring_buffer'
class SWindow
attr_accessor :prompt, :name, :noinput
attr_reader :id
@@id = -1
@@active = nil
@@windows = {}
@@history_size = 1000
@@firehose = false
# This function will trap the TSTP signal (suspend, ctrl-z) and, if possible,
# activate the parent window.
def SWindow._catch_suspend()
orig_suspend = Signal.trap("TSTP") do
if(@@active)
@@active.deactivate()
end
end
proc.call()
Signal.trap("TSTP", orig_suspend)
end
@@input_thread = Thread.new() do
begin
# This lets the program load a bit before the initial prompt is printed (a slightly better user experience)
sleep(0.1)
_catch_suspend() do
loop do
begin
while @@active.nil? do
end
if(@@active.noinput)
str = Readline::readline()
else
str = Readline::readline(@@active.prompt, true)
end
# If readline() returns nil, it means the input stream is closed
# (either the file it's reading from is done, or the user pressed
# ctrl-d). Terminate the input thread.
if(str.nil?)
break
end
if(@@active.nil?)
$stderr.puts("WARNING: there is no active window! Input's going nowhere")
$stderr.puts("If you think this might be a bug, please report to")
$stderr.puts("https://github.com/iagox86/dnscat2/issues")
next
end
@@active._incoming(str)
rescue SystemExit
# If something sent an exit request, we want to break, which shuts
# down the thread
break
rescue Exception => e
$stderr.puts("Something bad just happened! You will likely want to report this to")
$stderr.puts("https://github.com/iagox86/dnscat2/issues")
$stderr.puts(e.inspect)
$stderr.puts(e.backtrace.join("\n"))
end
end
end
$stderr.puts("Input thread is over")
rescue StandardError => e
$stderr.puts(e)
$stderr.puts(e.backtrace.join("\n"))
end
end
# Create a new window, with the given parent (use 'nil' for a top-level
# window, though you should try to only do one of those). Optionally, the
# window can also be activated (which means it's brought to the front).
def initialize(parent = nil, activate = false, params = {})
@parent = parent
@children = []
@id = params[:id] || (@@id += 1)
@name = params[:name] || "unnamed"
@prompt = params[:prompt] || ("%s %s> " % [@name, @id.to_s()])
@noinput = params[:noinput] || false
@times_out = params[:times_out] || false
@callback = nil
@history = RingBuffer.new(@@history_size)
@typed_history = []
@closed = false
@pending = false
@to_parent = false
@to_ancestors = false
@to_children = false
@to_descendants = false
if(@parent)
@parent._add_child(self)
end
if(@@active.nil? || activate)
self.activate()
end
if(params[:quiet] != true)
target = @parent ? @parent : self
target.with({:to_descendants => true, :to_ancestors => true}) do
target.puts("New window created: %s" % @id.to_s())
end
end
@@windows[@id.to_s()] = self
end
def _we_just_got_data()
if(@@active == self)
return
end
@pending = true
end
# Yields for each child
def children()
@children.each do |child|
yield child
end
end
# Set the on_input callback - the function that will be called when input is
# received. Very important!
def on_input()
@callback = proc
end
def with(params = {})
# Save the state
to_parent = @to_parent
to_ancestors = @to_ancestors
to_children = @to_children
to_descendants = @to_descendants
# Set the state
@to_parent = params[:to_parent] || @to_parent
@to_ancestors = params[:to_ancestors] || @to_ancestors
@to_children = params[:to_children] || @to_children
@to_descendants = params[:to_descendants] || @to_descendants
yield()
# Restore the state
@to_parent = to_parent
@to_ancestors = to_ancestors
@to_children = to_children
@to_descendants = to_descendants
end
def do_recursion(func, *args)
if(@parent && (@to_parent || @to_ancestors))
@parent.with({:to_parent => false, :to_children => false, :to_descendants => false, :to_ancestors => @to_ancestors}) do
@parent.send(func, *args)
end
end
if(@to_children || @to_descendants)
@children.each do |c|
c.with({:to_descendants => @to_descendants, :to_children => false, :to_parent => false, :to_ancestors => false}) do
c.send(func, *args)
end
end
end
end
# Write to a window, just like $stdout.puts()
def puts(str = "")
if(@@firehose)
$stdout.puts(str)
return
end
_we_just_got_data()
if(@@active == self)
$stdout.puts(str)
end
@history << (str.to_s() + "\n")
do_recursion(:puts, str)
end
# Write to a window, just like $stdout.print()
def print(str = "")
if(@@firehose)
$stdout.print(str)
return
end
_we_just_got_data()
str = str.to_s()
if(@@active == self)
$stdout.print(str)
end
@history << str.to_s()
do_recursion(:print, str)
end
# Write to a window, just like $stdout.printf()
def printf(*args)
print(sprintf(*args))
end
def _add_child(child)
@children << child
end
# Enable a window; re-draws the history, and starts sending user input to
# the specified window (note that this can be a closed window; we don't
# really care)
def activate()
# The user just viewed the window, so data is no longer pending
@pending = false
# Set this window to the activate one
@@active = self
# Re-draw the history
$stdout.puts(@history.join(""))
# It appears that some versions of Readline don't support :clear, so only do this if we can
if(Readline::HISTORY.respond_to?(:clear))
# Fill Readline's buffer with the typed history (this is a bit of a hack,
# but Readline doesn't support multiple history buffers)
Readline::HISTORY.clear()
end
@typed_history.each do |i|
Readline::HISTORY << i
end
end
# Basically, this activates the parent window (if possible)
def deactivate()
if(@parent)
@parent.activate()
else
$stdout.puts("Can't close the main window!")
end
end
def _incoming(str)
if(@noinput)
return
end
@history << @prompt + str + "\n"
if(str != '')
@typed_history << str
end
if(@callback.nil?)
self.puts("Input received, but nothing has registered to receive it")
self.puts("Use ctrl-z to escape if this window isn't taking input!")
return
end
@callback.call(str)
end
# Process some string as if it was coming from the keyboard (this can be used to,
# for example, write scripts)
def fake_input(str)
return _incoming(str)
end
# Set the number of lines of history for the current session. Note that this
# only takes effect after another message is added to the history (lazy
# evaluated, essentially).
def history_size=(size)
@history.max_size = size
end
# Get the number of lines of history for the current session.
def history_size()
return @history.max_size
end
# Set the default history size for new windows that are created. The history
# size for current windows doesn't change.
def SWindow.history_size=(size)
@@history_size = size
end
# Get the default history size.
def SWindow.history_size()
return @@history_size
end
# close the window - closing windows is purely a UI thing, they are still
# available and can receive data like anything else.
def close()
@closed = true
deactivate()
end
# Check if the window has been closed
def closed?()
return @closed
end
# Check if the window has any pending data
def pending?()
return @pending
end
# Check if a window with the given id exists
def SWindow.exists?(id)
return !@@windows[id.to_s()].nil?
end
# Retrieve a window by its id value
def SWindow.get(id)
return @@windows[id.to_s()]
end
# This function blocks until SWindow is totally finished (that is, it has
# received an exit signal or an EOF marker).
def SWindow.wait()
@@input_thread.join()
end
# This is mostly for debugging - all output goes to the same place
def SWindow.set_firehose(value)
@@firehose = value
end
def kick()
@last_seen = Time.now()
end
def to_s()
s = "%s :: %s" % [@id.to_s(), @name]
if(@@active == self)
s += " [active]"
end
if(@pending)
s += " [*]"
end
if(@times_out)
elapsed = Time.now() - @last_seen
if(elapsed > 5)
s += " [idle for #{elapsed.to_i()} seconds]"
end
end
return s
end
end