Skip to content

Commit d2e6f90

Browse files
committed
Use a list of users
1 parent 897e993 commit d2e6f90

File tree

1 file changed

+31
-68
lines changed

1 file changed

+31
-68
lines changed

modules/post/linux/gather/enum_users_history.rb

Lines changed: 31 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -37,20 +37,23 @@ def run
3737
print_good("\t#{distro[:version]}")
3838
print_good("\t#{distro[:kernel]}")
3939

40-
users = execute('/bin/cat /etc/passwd | cut -d : -f 1')
4140
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?
4243

43-
mount = execute('/bin/mount -l')
44+
vprint_status("Retrieving history for #{users.length} users")
4445
shells = %w{ ash bash csh ksh sh tcsh zsh }
45-
shells.each do |shell|
46-
get_shell_history(users, user, shell)
46+
users.each do |u|
47+
shells.each do |shell|
48+
get_shell_history(u, shell)
49+
end
50+
get_mysql_history(u)
51+
get_psql_history(u)
52+
get_vim_history(u)
4753
end
48-
get_mysql_history(users, user)
49-
get_psql_history(users, user)
50-
get_vim_history(users, user)
54+
5155
last = execute('/usr/bin/last && /usr/bin/lastlog')
5256
sudoers = cat_file('/etc/sudoers')
53-
5457
save('Last logs', last) unless last.blank?
5558
save('Sudoers', sudoers) unless sudoers.blank? || sudoers =~ /Permission denied/
5659
end
@@ -84,84 +87,44 @@ def cat_file(filename)
8487
output
8588
end
8689

87-
def get_shell_history(users, user, shell)
90+
def get_shell_history(user, shell)
8891
return if shell.nil?
89-
if user == 'root' && !users.nil?
90-
users = users.chomp.split
91-
users.each do |u|
92-
vprint_status("Extracting #{shell} history for #{u}")
93-
if u == 'root'
94-
hist = cat_file("/root/.#{shell}_history")
95-
else
96-
hist = cat_file("/home/#{u}/.#{shell}_history")
97-
end
98-
save("#{shell} History for #{u}", hist) unless hist.blank? || hist =~ /No such file or directory/
99-
end
92+
vprint_status("Extracting #{shell} history for #{user}")
93+
if user == 'root'
94+
hist = cat_file("/root/.#{shell}_history")
10095
else
101-
vprint_status("Extracting #{shell} history for #{user}")
10296
hist = cat_file("/home/#{user}/.#{shell}_history")
103-
vprint_status(hist)
104-
save("#{shell} History for #{user}", hist) unless hist.blank? || hist =~ /No such file or directory/
10597
end
98+
save("#{shell} History for #{user}", hist) unless hist.blank? || hist =~ /No such file or directory/
10699
end
107100

108-
def get_mysql_history(users, user)
109-
if user == 'root' && !users.nil?
110-
users = users.chomp.split
111-
users.each do |u|
112-
vprint_status("Extracting MySQL history for #{u}")
113-
if u == 'root'
114-
sql_hist = cat_file('/root/.mysql_history')
115-
else
116-
sql_hist = cat_file("/home/#{u}/.mysql_history")
117-
end
118-
save("MySQL History for #{u}", sql_hist) unless sql_hist.blank? || sql_hist =~ /No such file or directory/
119-
end
101+
def get_mysql_history(user)
102+
vprint_status("Extracting MySQL history for #{user}")
103+
if user == 'root'
104+
sql_hist = cat_file('/root/.mysql_history')
120105
else
121-
vprint_status("Extracting MySQL history for #{user}")
122106
sql_hist = cat_file("/home/#{user}/.mysql_history")
123-
vprint_status(sql_hist) if sql_hist
124-
save("MySQL History for #{user}", sql_hist) unless sql_hist.blank? || sql_hist =~ /No such file or directory/
125107
end
108+
save("MySQL History for #{user}", sql_hist) unless sql_hist.blank? || sql_hist =~ /No such file or directory/
126109
end
127110

128-
def get_psql_history(users, user)
129-
if user == 'root' && !users.nil?
130-
users = users.chomp.split
131-
users.each do |u|
132-
vprint_status("Extracting PostgreSQL history for #{u}")
133-
if u == 'root'
134-
sql_hist = cat_file('/root/.psql_history')
135-
else
136-
sql_hist = cat_file("/home/#{u}/.psql_history")
137-
end
138-
save("PostgreSQL History for #{u}", sql_hist) unless sql_hist.blank? || sql_hist =~ /No such file or directory/
139-
end
111+
def get_psql_history(user)
112+
vprint_status("Extracting PostgreSQL history for #{user}")
113+
if user == 'root'
114+
sql_hist = cat_file('/root/.psql_history')
140115
else
141-
vprint_status("Extracting PostgreSQL history for #{user}")
142116
sql_hist = cat_file("/home/#{user}/.psql_history")
143-
vprint_status(sql_hist) if sql_hist
144-
save("PostgreSQL History for #{user}", sql_hist) unless sql_hist.blank? || sql_hist =~ /No such file or directory/
145117
end
118+
save("PostgreSQL History for #{user}", sql_hist) unless sql_hist.blank? || sql_hist =~ /No such file or directory/
146119
end
147120

148-
def get_vim_history(users, user)
149-
if user == 'root' && !users.nil?
150-
users = users.chomp.split
151-
users.each do |u|
152-
vprint_status("Extracting VIM history for #{u}")
153-
if u == 'root'
154-
vim_hist = cat_file('/root/.viminfo')
155-
else
156-
vim_hist = cat_file("/home/#{u}/.viminfo")
157-
end
158-
save("VIM History for #{u}", vim_hist) unless vim_hist.blank? || vim_hist =~ /No such file or directory/
159-
end
121+
def get_vim_history(user)
122+
vprint_status("Extracting VIM history for #{user}")
123+
if user == 'root'
124+
vim_hist = cat_file('/root/.viminfo')
160125
else
161-
vprint_status("Extracting VIM history for #{user}")
162126
vim_hist = cat_file("/home/#{user}/.viminfo")
163-
vprint_status(vim_hist)
164-
save("VIM History for #{user}", vim_hist) unless vim_hist.blank? || vim_hist =~ /No such file or directory/
165127
end
128+
save("VIM History for #{user}", vim_hist) unless vim_hist.blank? || vim_hist =~ /No such file or directory/
166129
end
167130
end

0 commit comments

Comments
 (0)