-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhackerbot.rb
505 lines (421 loc) · 17.5 KB
/
hackerbot.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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
require 'cinch'
require 'nokogiri'
require 'nori'
require './print.rb'
require 'open3'
require 'programr'
require 'getoptlong'
require 'thwait'
def check_output_conditions(bot_name, bots, current, lines, m)
condition_met = false
bots[bot_name]['attacks'][current]['condition'].each do |condition|
if !condition_met && condition.key?('output_matches') && lines =~ /#{condition['output_matches']}/m
condition_met = true
m.reply "#{condition['message']}"
end
if !condition_met && condition.key?('output_not_matches') && lines !~ /#{condition['output_not_matches']}/m
condition_met = true
m.reply "#{condition['message']}"
end
if !condition_met && condition.key?('output_equals') && lines == condition['output_equals']
condition_met = true
m.reply "#{condition['message']}"
end
if condition_met
# Repeated logic for trigger_next_attack
if condition.key?('trigger_next_attack')
# is this the last one?
if bots[bot_name]['current_attack'] < bots[bot_name]['attacks'].length - 1
bots[bot_name]['current_attack'] += 1
bots[bot_name]['current_quiz'] = nil
current = bots[bot_name]['current_attack']
sleep(1)
# prompt for current hack
if bots[bot_name]['messages'].key?('show_attack_numbers')
m.reply "** ##{current + 1} **"
end
m.reply bots[bot_name]['attacks'][current]['prompt']
else
m.reply bots[bot_name]['messages']['last_attack'].sample
end
end
if condition.key?('trigger_quiz')
m.reply bots[bot_name]['attacks'][current]['quiz']['question']
m.reply bots[bot_name]['messages']['say_answer']
bots[bot_name]['current_quiz'] = 0
end
# stop processing conditions, once we meet one
break
end
end
unless condition_met
if bots[bot_name]['attacks'][current]['else_condition']
m.reply bots[bot_name]['attacks'][current]['else_condition']['message']
end
end
current
end
def read_bots (irc_server_ip_address)
bots = {}
Dir.glob("config/*.xml").each do |file|
print "#{file}"
begin
doc = Nokogiri::XML(File.read(file))
rescue
Print.err "Failed to read hackerbot file (#{file})"
print "Failed to read hackerbot file (#{file})"
exit
end
#
# # TODO validate scenario XML against schema
# begin
# xsd = Nokogiri::XML::Schema(File.read(schema_file))
# xsd.validate(doc).each do |error|
# Print.err "Error in bot config file (#{file}):"
# Print.err ' ' + error.message
# exit
# end
# rescue Exception => e
# Print.err "Failed to validate bot config file (#{file}): against schema (#{schema_file})"
# Print.err e.message
# exit
# end
# remove xml namespaces for ease of processing
doc.remove_namespaces!
doc.xpath('/hackerbot').each_with_index do |hackerbot|
bot_name = hackerbot.at_xpath('name').text
Print.debug bot_name
bots[bot_name] = {}
chatbot_rules = hackerbot.at_xpath('AIML_chatbot_rules').text
Print.debug "Loading chatbot ai from #{chatbot_rules}"
bots[bot_name]['chat_ai'] = ProgramR::Facade.new
bots[bot_name]['chat_ai'].learn([chatbot_rules])
get_shell = hackerbot.at_xpath('get_shell').text
Print.debug get_shell
bots[bot_name]['get_shell'] = get_shell
bots[bot_name]['messages'] = Nori.new.parse(hackerbot.at_xpath('//messages').to_s)['messages']
Print.debug bots[bot_name]['messages'].to_s
bots[bot_name]['attacks'] = []
hackerbot.xpath('//attack').each do |attack|
bots[bot_name]['attacks'].push Nori.new.parse(attack.to_s)['attack']
end
bots[bot_name]['current_attack'] = 0
bots[bot_name]['current_quiz'] = nil
Print.debug bots[bot_name]['attacks'].to_s
bots[bot_name]['bot'] = Cinch::Bot.new do
configure do |c|
c.nick = bot_name
c.server = irc_server_ip_address
# joins a channel named after the bot, and #bots
c.channels = ["##{bot_name}", '#bots']
end
on :message, /hello/i do |m|
m.reply "Hello, #{m.user.nick} (#{m.user.host})."
m.reply bots[bot_name]['messages']['greeting']
current = bots[bot_name]['current_attack']
# prompt for the first attack
if bots[bot_name]['messages'].key?('show_attack_numbers')
m.reply "** ##{current + 1} **"
end
m.reply bots[bot_name]['attacks'][current]['prompt']
m.reply bots[bot_name]['messages']['say_ready'].sample
end
on :message, /help/i do |m|
m.reply bots[bot_name]['messages']['help']
end
on :message, 'next' do |m|
m.reply bots[bot_name]['messages']['next'].sample
# is this the last one?
if bots[bot_name]['current_attack'] < bots[bot_name]['attacks'].length - 1
bots[bot_name]['current_attack'] += 1
bots[bot_name]['current_quiz'] = nil
current = bots[bot_name]['current_attack']
# prompt for current hack
if bots[bot_name]['messages'].key?('show_attack_numbers')
m.reply "** ##{current + 1} **"
end
m.reply bots[bot_name]['attacks'][current]['prompt']
m.reply bots[bot_name]['messages']['say_ready'].sample
else
m.reply bots[bot_name]['messages']['last_attack'].sample
end
end
on :message, /^(goto|attack) [0-9]+$/i do |m|
m.reply bots[bot_name]['messages']['goto'].sample
requested_index = m.message.chomp().split[1].to_i - 1
Print.debug "requested_index = #{requested_index}, bots[bot_name]['attacks'].length = #{bots[bot_name]['attacks'].length}"
# is this a valid attack number?
if requested_index < bots[bot_name]['attacks'].length
bots[bot_name]['current_attack'] = requested_index
bots[bot_name]['current_quiz'] = nil
current = bots[bot_name]['current_attack']
# prompt for current hack
if bots[bot_name]['messages'].key?('show_attack_numbers')
m.reply "** ##{current + 1} **"
end
m.reply bots[bot_name]['attacks'][current]['prompt']
m.reply bots[bot_name]['messages']['say_ready'].sample
else
m.reply bots[bot_name]['messages']['invalid']
end
end
on :message, /^(the answer is|answer):? .+$/i do |m|
answer = m.message.chomp().split[1].to_i - 1
answer = m.message.chomp().match(/(?:the )?answer(?: is)?:? (.+)$/i)[1]
# current_quiz = bots[bot_name]['current_quiz']
current = bots[bot_name]['current_attack']
quiz = nil
# is there ONE quiz question?
if bots[bot_name]['attacks'][current].key?('quiz') && bots[bot_name]['attacks'][current]['quiz'].key?('answer')
quiz = bots[bot_name]['attacks'][current]['quiz']
# multiple quiz questions?
# elsif bots[bot_name]['attacks'][current]['quiz'][current_quiz].key?('answer')
# quiz = bots[bot_name]['attacks'][current]['quiz'][current_quiz]
end
if quiz != nil
correct_answer = quiz['answer']
if bots[bot_name]['attacks'][current].key?('post_command_output')
correct_answer.gsub!(/{{post_command_output}}/, (bots[bot_name]['attacks'][current]['post_command_output']||''))
end
if bots[bot_name]['attacks'][current].key?('get_shell_command_output')
correct_answer.gsub!(/{{shell_command_output_first_line}}/, (bots[bot_name]['attacks'][current]['get_shell_command_output']||'').split("\n").first)
end
if bots[bot_name]['attacks'][current].key?('get_shell_command_output')
correct_answer.gsub!(/{{pre_shell_command_output_first_line}}/, (bots[bot_name]['attacks'][current]['get_shell_command_output']||'').split("\n").first)
end
correct_answer.chomp!
if answer.match(/#{correct_answer}/i)
m.reply bots[bot_name]['messages']['correct_answer']
m.reply quiz['correct_answer_response']
# Repeated logic for trigger_next_attack
if quiz.key?('trigger_next_attack')
if bots[bot_name]['current_attack'] < bots[bot_name]['attacks'].length - 1
bots[bot_name]['current_attack'] += 1
bots[bot_name]['current_quiz'] = nil
current = bots[bot_name]['current_attack']
sleep(1)
# prompt for current hack
if bots[bot_name]['messages'].key?('show_attack_numbers')
m.reply "** ##{current + 1} **"
end
m.reply bots[bot_name]['attacks'][current]['prompt']
m.reply bots[bot_name]['messages']['say_ready'].sample
else
m.reply bots[bot_name]['messages']['last_attack'].sample
end
end
else
m.reply "#{bots[bot_name]['messages']['incorrect_answer']} (#{answer})"
end
else
m.reply bots[bot_name]['messages']['no_quiz']
end
end
on :message, 'previous' do |m|
m.reply bots[bot_name]['messages']['previous'].sample
# is this the last one?
if bots[bot_name]['current_attack'] > 0
bots[bot_name]['current_attack'] -= 1
bots[bot_name]['current_quiz'] = nil
current = bots[bot_name]['current_attack']
# prompt for current hack
m.reply bots[bot_name]['attacks'][current]['prompt']
m.reply bots[bot_name]['messages']['say_ready'].sample
else
m.reply bots[bot_name]['messages']['first_attack'].sample
end
end
on :message, 'list' do |m|
bots[bot_name]['attacks'].each_with_index {|val, index|
uptohere = ''
if index == bots[bot_name]['current_attack']
uptohere = '--> '
end
m.reply "#{uptohere}attack #{index+1}: #{val['prompt']}"
}
end
# fallback to AIML ALICE chatbot responses
on :message do |m|
# Only process messages not related to controlling attacks
if m.message !~ /hello|help|next|previous|list|^(goto|attack) [0-9]|(the answer is|answer)/
reaction = ''
begin
reaction = bots[bot_name]['chat_ai'].get_reaction(m.message.gsub /([^a-z0-9\- ]+)/i, '')
rescue Exception => e
puts e.message
puts e.backtrace.inspect
reaction = ''
end
if reaction != ''
m.reply reaction
else
if m.message.include?('?')
m.reply bots[bot_name]['messages']['non_answer']
end
end
end
end
on :message, 'ready' do |m|
m.reply bots[bot_name]['messages']['getting_shell'].sample
current = bots[bot_name]['current_attack']
if bots[bot_name]['attacks'][current].key?('pre_shell')
pre_shell_cmd = bots[bot_name]['attacks'][current]['pre_shell'].to_s.clone
pre_shell_cmd.gsub!(/{{chat_ip_address}}/, m.user.host.to_s)
pre_output = `#{pre_shell_cmd}`
unless bots[bot_name]['attacks'][current].key?('suppress_command_output_feedback')
m.reply "FYI: #{pre_output}"
end
bots[bot_name]['attacks'][current]['get_shell_command_output'] = pre_output
current = check_output_conditions(bot_name, bots, current, pre_output, m)
end
# use bot-wide method for obtaining shell, unless specified per-attack
if bots[bot_name]['attacks'][current].key?('get_shell')
shell_cmd = bots[bot_name]['attacks'][current]['get_shell'].to_s.clone
else
shell_cmd = bots[bot_name]['get_shell'].clone
end
if shell_cmd != 'false'
# substitute special variables
shell_cmd.gsub!(/{{chat_ip_address}}/, m.user.host.to_s)
# add a ; to ensure it is run via bash
shell_cmd << ';'
Print.debug shell_cmd
Open3.popen2e(shell_cmd) do |stdin, stdout_err, wait_thr|
# check whether we have shell by echoing "shelltest"
got_shell = false
lines = ''
post_lines = ''
i = 0
while i < 60 and not got_shell # retry for a while
i += 1
Print.debug i.to_s
stdin.puts "echo shelltest\n"
sleep(5)
# non-blocking read from buffer
begin
while ch = stdout_err.read_nonblock(1)
lines << ch
end
rescue # continue consuming until input blocks
end
bots[bot_name]['attacks'][current]['get_shell_command_output'] = lines
Print.debug lines
if lines =~ /shelltest/i
got_shell = true
Print.debug 'Got shell!'
else
Print.debug 'Still trying to get shell...'
m.reply '...'
end
end
Print.debug got_shell.to_s
if got_shell
m.reply bots[bot_name]['messages']['got_shell'].sample
post_cmd = bots[bot_name]['attacks'][current]['post_command']
if post_cmd
Print.debug post_cmd
post_cmd.gsub!(/{{chat_ip_address}}/, m.user.host.to_s)
stdin.puts "#{post_cmd}\n"
end
sleep(3)
# non-blocking read from buffer
begin
while ch = stdout_err.read_nonblock(1)
post_lines << ch
end
rescue # continue consuming until input blocks
end
begin
Timeout.timeout(15) do # timeout 10 sec
stdin.close # no more input, end the program
post_lines << stdout_err.read.chomp()
end
rescue Timeout::Error
wait_thr.kill
begin
while ch = stdout_err.read_nonblock(1)
post_lines << ch
end
rescue # continue consuming until input blocks
end
end
bots[bot_name]['attacks'][current]['post_command_output'] = post_lines
unless bots[bot_name]['attacks'][current].key?('suppress_command_output_feedback')
m.reply "FYI: #{post_lines}"
end
Print.debug post_lines
current = check_output_conditions(bot_name, bots, current, post_lines, m)
else
Print.debug("Shell failed...")
# shell fail message will use the default message, unless specified for the attack
if bots[bot_name]['attacks'][current].key?('shell_fail_message')
m.reply bots[bot_name]['attacks'][current]['shell_fail_message']
else
m.reply bots[bot_name]['messages']['shell_fail_message']
end
# under specific situations reveal the error message to the user
if lines =~ /command not found/
m.reply "Looks like there is some software missing: #{lines}"
end
end
# ensure any child processes are not left running (without this msfconsole is left running)
`kill -9 $(ps -o pid --no-headers --ppid #{wait_thr.pid})`
wait_thr.kill
end
end
if bots[bot_name]['attacks'][current].key?('post_shell')
post_shell_cmd = bots[bot_name]['attacks'][current]['post_shell'].to_s.clone
post_shell_cmd.gsub!(/{{chat_ip_address}}/, m.user.host.to_s)
post_output = `#{post_shell_cmd}`
unless bots[bot_name]['attacks'][current].key?('suppress_command_output_feedback')
m.reply "FYI: #{post_output}"
end
# bots[bot_name]['attacks'][current]['get_shell_command_output'] = post_output
current = check_output_conditions(bot_name, bots, current, post_output, m)
end
m.reply bots[bot_name]['messages']['repeat'].sample
end
end
end
end
bots
end
def start_bots(bots)
threads = []
bots.each do |bot_name, bot|
threads << Thread.new {
Print.std "Starting bot: #{bot_name}\n"
bot['bot'].start
}
end
ThreadsWait.all_waits(threads)
end
def usage
Print.std 'ruby hackerbot.rb [--irc-server host]'
end
# -- main --
Print.std '~'*47
Print.std ' '*19 + 'Hackerbot'
Print.std '~'*47
irc_server_ip_address = 'localhost'
# Get command line arguments
opts = GetoptLong.new(
[ '--help', '-h', GetoptLong::NO_ARGUMENT ],
[ '--irc-server', '-i', GetoptLong::REQUIRED_ARGUMENT ],
)
# process option arguments
opts.each do |opt, arg|
case opt
# Main options
when '--help'
usage
when '--irc-server'
irc_server_ip_address = arg;
else
Print.err "Argument not valid: #{arg}"
usage
exit
end
end
bots = read_bots(irc_server_ip_address)
start_bots(bots)