It is company policy for admins to login to the servers via a personal username, and then run sudo -i
to become root. Upon running sudo -i
, sudo will create an environmental variable called SUDO_USER
, which contains the original user's username.
Is there a way to log ALL commands within syslog with something akin to the following syntax:
${TIME/DATE STAMP}: [${REAL_USER}|${SUDO_USER}]: ${CMD}
An example entry would be:
Sat Jan 19 22:28:46 CST 2013: [root|ksoviero]: yum install random-pkg
Obviously it doesn't have to be exactly the above syntax, it just has to include a minimum of the real user (eg. root), the sudo user (eg. ksoviero), and the full command that was run (eg. yum install random-pkg).
I've already tried snoopy
, but it did not include the SUDO_USER
variable.
Update: 2 more things that have popped up in the comments and in follow-up questions:
auditd
this way will dramatically increase your log volume, especially if the system is heavily in use via commandline. Adjust your log retention policy.Auditd
logs on the host where they are created are just as secure as other files on the same box. Forward your logs to a remote log collection server like ELK or Graylog to preserve your logs' integrity. Plus, adding to the point above, it allows to more aggressively delete old logs.As was suggested by Michael Hampton,
auditd
is the correct tool for the job here.I tested this on an Ubuntu 12.10 installation, so your mileage may vary on other systems.
Install
auditd
:apt-get install auditd
Add these 2 lines to
/etc/audit/audit.rules
:These will track all commands run by root (
euid=0
). Why two rules? Theexecve
syscall must be tracked in both 32 and 64 bit code.To get rid of
auid=4294967295
messages in logs, addaudit=1
to the kernel's cmdline (by editing/etc/default/grub
)Place the line
session required pam_loginuid.so
in all PAM config files that are relevant to login (
/etc/pam.d/{login,kdm,sshd}
), but not in the files that are relevant tosu
orsudo
. This will allowauditd
to get the calling user'suid
correctly when callingsudo
orsu
.Restart your system now.
Let's login and run some commands:
This will yield something like this in
/var/log/audit/auditd.log
:The
auid
column contains the calling user'suid
, which allows you filter for commands run by this user withThis will even list commands the user ran as root.
Sources:
Remember that sudo itself logs all sudo commands in the syslog, so all priv'd users should be educated to not simply sudo to get a root shell but to :
The problem with this or any approach I have thought of is that as the
root
user, it is quite difficult to prevent a user from evading any specific type of logging. Thus anything thing you try will be < 100% I am sorry to say.Education, documentation, enforcement and above all trust is what is necessary.
I was once faced with the same problem and had to come up with a quick and dirty solution - each sudo user will have their own history file once they run the command
sudo -i
In
/root/.bashrc
I added the following line -So every user who sudos to root will have a history file .bash_history-username.
Another method -
Add the following code to
/root/.bashrc
and it will append the username, sudo-user, and the command to the log file, where ever the notice level is set, most likely /var/log/messages.Credit to - http://backdrift.org/logging-bash-history-to-syslog-using-traps
A number of establishments actually prohibit the use of auditd because it is resource intensive and may result in an opportunity for denial of service attacks.
One solution is to configure the latest Korn shell (ksh-93, see http://kornshell.com/ for details) to log all commands executed as root to a remote syslog server, and then to require by policy that, except in emergency situations, sysadmins log on with personal accounts and execute the enhanced Korn shell via sudo. Examination of the logs can detect when an admin launches another shell from the approved shell in order to cover their tracks, and the SA can then be educated as necessary.
Sudo has something called sudoreplay when enabled sessions is logged and can be replayed later, works similar as the
script
command that make a typescript of terminal session that later can be replayed with thescriptreplay
command.Not that there's anything wrong with any of the other answers thus far, but if you decide that
sudo
's logging viasyslog
is satisfactory, may I suggest a wrinkle: log it via the network to a remote audit host.That gets around the problem of "now I have become root, I can remove any trace of my malfeasance from the logs". You may now be root on the local box, but you can't call that log packet back from the network, and you (presumably) don't have root privileges on the remote audit host.
I've been doing this with some of the networks I manage for years, and it has two other signal advantages:
Firstly, there's one place on the network to go to check all syslogs, which allows much easier correlation of incidents, and so is a one-stop shop for investigations like "When
juno
was complaining that NFS serverhera
was not responding, was anyone else complaining about the same thing at the same time? If so,hera
is likely to be the problem, let's see what she logged; if not,juno
's network connection is more suspect, let's see what elsejuno
logged at that time.".Secondly, syslog log rotation becomes easier: you don't keep copies of logs on local hosts for more than a few days, but you make sure the audit server has huge amounts of disc space, and keep all syslogs there for several years. Plus if, say, you want to write them to WORM media for eg forensic audit purposes, you only have to buy one WORM drive.
Since version 2.0.0, Snoopy is able to log arbitrary environmental variables. Here is a link to Snoopy's example configuration file, where you can find all about the
%{env:NAME_OF_MY_ENV_VAR_I_WANT_LOGGED}
syntax.However, recent contribution pointed out that logging the owner of the tty is a fairly effective and elegant answer to the question of "Who executed that command, as root?".
Disclosure: I am the maintainer of Snoopy.
Edit: Changed link to point directly to relevant documentation (apparently, certain people can't find it in the repo) + slight rewording.