跟踪其他用户在 sudo 后执行了哪些命令

跟踪其他用户在 sudo 后执行了哪些命令

我已经sudo向十个用户提供了成为另一个类似的用户nsup

我想跟踪哪个用户在成为 后执行了哪个命令nsup。如果有一种方法可以将日志文件存储在一个通用文件中,那就太好了。

我尝试查看/var/log/secure,但从那里我无法区分哪个用户在成为 后执行了哪个命令nsup。它仅显示哪个用户执行了命令变得 nsup,除此之外什么也没有。

答案1

如果您的用户使用 bash,您可以使用 /etc/bash.bash_logout 脚本以时间戳格式保存历史记录的额外副本。

例如,我编写了以下内容来提供谁在何时做了什么(在具有多个 sudo 用户的服务器上)的审计跟踪,并且还可以保留历史记录,以防机器被破坏:

#! /bin/bash

# /etc/bash.bash_logout
#
# Time-stamped bash history logging
# by Craig Sanders <[email protected]> 2008
#
# This script is public domain.  Do whatever you want with it.

exec >& /dev/null

# LOGDIR must already exist and must be mode 1777 (same as /tmp)
# put it somewhere easily overlooked by script-kiddies.  /var/log 
# is a bad location because slightly-brighter-than-average SK's will
# often 'rm -rf /var/log' to cover their tracks.
LOGDIR='/var/tmp/.history'

[ -d "$LOGDIR" ] || exit 0

# Get current user name and who they logged in as.
CNAME=$(id -u -n)
LNAME=$(who am i | awk '{print $1}')
NAME="$LNAME--$CNAME"

# Get the TTY
TTY=$(tty)

# get the hostname and ip they logged in from
# short (non-fqdn) hostname:
RHOST_NAME=$(who -m  | awk '{print $5}' | sed -r -e 's/[()]|\..*//g')
# or full hostname:
#RHOST_NAME=$(who -m  | awk '{print $5}' | sed -r -e 's/[()]//g')

# if no RHOST_NAME, then login was on the console.
echo "$RHOST_NAME" | grep -q '[:/]' && RHOST_NAME="console"

# get the IP address
RHOST_IP=$(who -m --ips | awk '{print $5}')
echo "$RHOST_IP" | grep -q '[:/]' && RHOST_IP="console"

RHOST=$(echo "$RHOST_NAME--$RHOST_IP")

WHERE="$RHOST--$TTY"
WHERE=$(echo "$WHERE" | sed -e 's/\//-/g' -e 's/^-//')

# Filenames will be of the form:
# $LOGDIR/cas--root--localhost--127.0.0.1---dev-pts-1
# Ugly, but useful/informative. This example shows I logged in as cas
# from localhost, sudo-ed to root, and my tty was /dev/pts/1
HISTLOG="$LOGDIR/$NAME--$WHERE"


# Optionally rotate HISTLOG on each logout, otherwise new history
# sessions just get appended.
#[ -e "$HISTLOG" ] && savelog -l -c 21 -q $HISTLOG > /dev/null 2>&1

# Log some easily parseable info as a prelude, including the current
# history settings (an unusual HISTFILE or zero HISTSIZE setting is
# suspicious and worthy of investigation)

cat <<__EOF__ >> "$HISTLOG"

### TIME ### $(date +'%a,%Y-%m-%d,%H:%M:%S')
### FROM ### $RHOST_NAME,$RHOST_IP,$TTY
### USER ### $LNAME,$CNAME
### WHOM ### $(who -m)
### HIST ### $HISTFILE,$HISTSIZE

__EOF__


# Setting HISTTIMEFORMAT seems to be buggy. bash man page says it uses
# strftime, but all it seems to care about is whether it's set or not -
# 'history -a' always uses seconds since epoch, regardless of what it is
# set to.

HISTTIMEFORMAT="%s"
history -a "$HISTLOG"


# Now write history as normal (this seems buggy too. bash used to always
# write $HISTFILE anyway, but now it won't do it if you've already run
# 'history -a')

unset HISTTIMEFORMAT
history -w

答案2

我就是这样实现的。

rsyslog.conf文件中我添加了以下几行来跟踪

$umask 0000                 
$FileCreateMode 0666         
local2.info /var/log/usercommands
$umask 0077                 

/etc/skel/.bashrc文件中我添加了以下几行。

myname=`who am i | awk '{print $1}'`
PROMPT_COMMAND='history -a >(logger -p local2.info -i "$myname $USER[$PWD] $SSH_CONNECTION")'

希望这可能有帮助

答案3

11年零1个月前提问

https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/6/html/security_guide/sec-understanding_audit_log_files

指向 RHEL 6,但如果这很重要,请查找最新的了解审核日志文件在红帽。

您想查看uidauid哪个是审核 uid,euid哪个是启动进程的有效 uid。但为了有效地捕获这一点,/var/log/audit/audit.logsystemctl enable auditd需要有一个正确定义的/etc/audit/rules.d/audit.rules文件。对于您在这篇文章的主题行中提出的问题 - 是的,这些都是可跟踪的(可能不容易被人类阅读),但它存在于audit.log中......而11年前,在RHEL/CentOS 7可能不如它之前今天是 RHEL 8+ 以及今天的auditd 的任何版本。

相关内容