sudoの後に別のユーザーに実行されたコマンドを追跡する


8

sudoような別のユーザーになるために、10人のユーザーに提供しましたnsup

どのユーザーがどのコマンドを実行したかを追跡したいnsup。ログファイルを共通のファイルに保存する方法があれば、それは素晴らしいことです。

私は見てみました/var/log/secureが、そこからどのユーザーがどのコマンドを実行したかがわかりませんnsup。これは、どのユーザーがになる ためのコマンドを実行したかだけを示しnsup、それ以降は何も示しません。


2
そうです、sudoを使用して新しいシェルを開いた場合、シェルで実行されたアクションはログに記録されません。それらをログに記録する方法がある場合、それがsudoを介して行われるとは思いません。また、「自発的」ではない(つまり、ユーザーが上書きできない)ログ記録を行う方法については聞いたことがありません。「自発的」ロギングの場合、シェルの開始時に/ var / log / secureから最新の行を取得し、それを通常のシェル履歴と組み合わせるスクリプトを作成できます。またはunix.stackexchange.com/questions/6554/…を
dubiousjim

欠陥もあるかもしれません。2人のユーザーが同時にログインし、nzsupユーザーになり、いくつかのコマンドの実行を開始したとします.nzsupにsudoした後にどのユーザーがどのコマンドを実行したかを確認する方法。実行されたコマンドは、nzsupの履歴ファイルのみにあります。
Venom 2012

シェルセッションが最初に元のユーザーが誰であるかを決定するだけだと想像していました。しかし、はい、2人のユーザーが同時に新しいシェルを同時にsudoすると競合状態が発生します。私がリンクしたスレッドは、元のユーザーが誰であるかを判別する別の方法について説明しています。
dubiousjim 2012

回答:


5

ユーザーがbashを使用している場合は、/ etc / bash.bash_logoutスクリプトを使用して、履歴の追加のコピーをタイムスタンプ付きの形式で保存できます。

たとえば、私は次のように書いて、(複数のsudoユーザーがいるサーバーで)誰が何をいつ行ったかの監査証跡を提供し、マシンが侵入した場合の履歴を保存します。

#! /bin/bash

# /etc/bash.bash_logout
#
# Time-stamped bash history logging
# by Craig Sanders <cas@taz.net.au> 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

1
これは、ユーザーが設定しない限り機能しますHISTFILE=/dev/null...
2012

1
ユーザーがHISTFILEを何に設定するかに関係なく機能します。それがそれを書くことのすべてのポイントでした。スクリプトを読み取り、history -a "$HISTLOG"履歴を$ HISTLOGに追加します。$ HISTFILEを使用または気にしません。
cas

1
あるいは、もっと単純なバージョンをユーザーnsupに入れることもできます~/.bash_logout
cas

4
これは明らかに安全なログではないことに注意してください。安全なロギングが必要な場合は、監査ツールを使用してください。
クリスダウン

「HISTTIMEFORMATはバグがあるようです」について-この形式はプレゼンテーション用であり、ストレージ用ではありません。たとえば発行時に使用されますhistory 10。ストレージの場合、HISTTIMEFORMATは、タイムスタンプを保存するか(設定されている場合)、まったく保存しないか(設定されていない場合)のみを示します。エントリは%sとしてのみ保存されます。
kubanczyk 2015年

0

このように実装しました。

rsylog.confファイルに、追跡する以下の行を追加しました

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

/etc/skel/.bashrcファイルに、以下の行を追加しました。

ここに画像の説明を入力してください

これが役に立てば幸い

弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.