|
| 1 | +require 'nokogiri' |
| 2 | +require 'net/http' |
| 3 | + |
| 4 | +class ChatTest |
| 5 | + |
| 6 | + attr_accessor :url |
| 7 | + |
| 8 | + BOTTESTERS = [ 'l0bsteryumyum1', 'bottyp0', 'popo0', 'pdiddy1', 'thatsinn3rguy', 'viper2000', 'the1jboss', '1337hackerizme' ] |
| 9 | + |
| 10 | + def check_chat_bot |
| 11 | + #print_status("Checking chat bot as #{bot_tester}...") |
| 12 | + rv = false |
| 13 | + begin |
| 14 | + php_sid = login_chat |
| 15 | + rescue Exception => e |
| 16 | + raise e.message |
| 17 | + end |
| 18 | + |
| 19 | + # Check to make sure the bot responds to greetings |
| 20 | + (1..5).each do |i| |
| 21 | + greeting = ['hi', 'hello', 'yo', 'hey', 'hola', 'sup', 'howdy', 'hiya'].sample |
| 22 | + res = message_bot(php_sid, greeting) |
| 23 | + |
| 24 | + if res.match(/aloha\!/) |
| 25 | + rv = true |
| 26 | + break |
| 27 | + else |
| 28 | + if i == 5 |
| 29 | + rv = false |
| 30 | + break |
| 31 | + end |
| 32 | + end |
| 33 | + |
| 34 | + # Wait before we try to talk to the bot again |
| 35 | + sleep(2) |
| 36 | + end |
| 37 | + |
| 38 | + # Check to make sure the bot is outputting the correct Base64 encoded flag |
| 39 | + flag_file = File.open(File.join(File.expand_path(File.dirname(__FILE__)),'..','..','files','flags','ace_of_clubs_b64.txt'), 'r') |
| 40 | + b64_string = flag_file.readline() |
| 41 | + |
| 42 | + (1..3).each do |i| |
| 43 | + message = 'ace of clubs' |
| 44 | + res = message_bot(php_sid, message) |
| 45 | + if res.match(/#{b64_string}/) |
| 46 | + rv = true |
| 47 | + break |
| 48 | + else |
| 49 | + if i == 5 |
| 50 | + rv = false |
| 51 | + break |
| 52 | + end |
| 53 | + end |
| 54 | + |
| 55 | + # Wait before we try to talk to the bot again |
| 56 | + sleep(2) |
| 57 | + end |
| 58 | + rv |
| 59 | + end |
| 60 | + |
| 61 | + def send_get_request(url, vars_get={}) |
| 62 | + uri = URI(url) |
| 63 | + uri.query = URI.encode_www_form(vars_get) |
| 64 | + Net::HTTP.get_response(uri) |
| 65 | + end |
| 66 | + |
| 67 | + def send_post_request(url, cookie, vars_post={}) |
| 68 | + uri = URI(url) |
| 69 | + req = Net::HTTP::Post.new(uri) |
| 70 | + req['Cookie'] = cookie |
| 71 | + req.set_form_data(vars_post) |
| 72 | + http = Net::HTTP.new(uri.host, uri.port) |
| 73 | + http.request(req) |
| 74 | + end |
| 75 | + |
| 76 | + def login_chat |
| 77 | + begin |
| 78 | + res = send_get_request(@url) |
| 79 | + rescue Exception => e |
| 80 | + raise e.message |
| 81 | + end |
| 82 | + |
| 83 | + if res && res.body !~ /<title>Metasploitable3 Chatroom/i |
| 84 | + raise 'Chatroom not found' |
| 85 | + end |
| 86 | + |
| 87 | + unless res.header['Set-Cookie'] |
| 88 | + raise 'No Cookie found from the chat app' |
| 89 | + end |
| 90 | + |
| 91 | + php_sid = res.header['Set-Cookie'].scan(/PHPSESSID=(\w+)/).flatten.first || '' |
| 92 | + |
| 93 | + if php_sid.empty? |
| 94 | + raise 'No PHP session ID found from the chat app' |
| 95 | + end |
| 96 | + |
| 97 | + res = send_post_request("#{@url}index.php", "PHPSESSID=#{php_sid}", {'name'=>bot_tester, 'enter'=>'Enter'}) |
| 98 | + |
| 99 | + unless res.header['Set-Cookie'] |
| 100 | + raise 'Chatroom did not set name while logging in' |
| 101 | + end |
| 102 | + |
| 103 | + php_sid |
| 104 | + end |
| 105 | + |
| 106 | + def bot_tester |
| 107 | + @tester ||= BOTTESTERS.sample |
| 108 | + end |
| 109 | + |
| 110 | + def get_last_bot_response |
| 111 | + res = send_get_request("#{@url}/read_log.php") |
| 112 | + html = Nokogiri::HTML(res.body) |
| 113 | + res = html.search('div[@class="msgln"]').select { |e| e.children[1].text =~ /Papa Smurf/ }.reverse.first |
| 114 | + |
| 115 | + raise 'No response from bot' unless res |
| 116 | + raise 'No conversation yet' if res.previous.nil? |
| 117 | + previous_message_handle = res.previous.children[1].text |
| 118 | + |
| 119 | + if previous_message_handle == bot_tester |
| 120 | + msg = res.children[2].text.scan(/: (.+)/).flatten.first || '' |
| 121 | + #print_status("Chat bot replies with: \"#{msg}\"") |
| 122 | + return msg |
| 123 | + end |
| 124 | + |
| 125 | + raise 'Empty response from bot' |
| 126 | + end |
| 127 | + |
| 128 | + def message_bot(php_sid, message) |
| 129 | + |
| 130 | + #print_status("Greeting bot with \"#{greeting}\"") |
| 131 | + res = send_post_request("#{@url}post.php", "name=#{bot_tester}; PHPSESSID=#{php_sid}", {'text'=>message}) |
| 132 | + |
| 133 | + attempts = 0 |
| 134 | + res = '' |
| 135 | + begin |
| 136 | + res = get_last_bot_response |
| 137 | + return res |
| 138 | + rescue Exception => e |
| 139 | + if res.empty? && attempts < 5 |
| 140 | + attempts += 1 |
| 141 | + sleep(2) |
| 142 | + retry |
| 143 | + end |
| 144 | + end |
| 145 | + |
| 146 | + res |
| 147 | + end |
| 148 | + |
| 149 | + def initialize(ip) |
| 150 | + @url = "http://#{ip}/chat/" |
| 151 | + end |
| 152 | +end |
0 commit comments