我正在運行一個 debian etch 伺服器,用戶將透過 ssh 登入(希望)chroot 監獄。我如何以無法刪除或阻止的方式記錄他們執行的命令?
答案1
安裝史努比。如果您只想記錄一個用戶,請執行一些系統日誌過濾。
答案2
我編寫了一種方法,將所有“bash”命令/內建命令記錄到文字檔案或“syslog”伺服器中,而無需使用補丁或特殊的可執行工具。
它非常容易部署,因為它是一個簡單的 shell 腳本,需要在「bash」初始化時呼叫一次。 (例如,只需從 .bashrc 中「獲取」它)它是基於使用 bash DEBUG 陷阱的想法。也可以看看superuser.com 上的這篇文章
declare -rx HISTCONTROL="" #does not ignore spaces or duplicates
declare -rx HISTIGNORE="" #does not ignore patterns
declare -rx AUDIT_LOGINUSER="$(who -mu | awk '{print $1}')"
declare -rx AUDIT_LOGINPID="$(who -mu | awk '{print $6}')"
declare -rx AUDIT_USER="$USER" #defined by pam during su/sudo
declare -rx AUDIT_PID="$$"
declare -rx AUDIT_TTY="$(who -mu | awk '{print $2}')"
declare -rx AUDIT_SSH="$([ -n "$SSH_CONNECTION" ] && echo "$SSH_CONNECTION" | awk '{print $1":"$2"->"$3":"$4}')"
declare -rx AUDIT_STR="[audit $AUDIT_LOGINUSER/$AUDIT_LOGINPID as $AUDIT_USER/$AUDIT_PID on $AUDIT_TTY/$AUDIT_SSH]"
set +o functrace #disable trap DEBUG inherited in functions, command substitutions or subshells, normally the default setting already
shopt -s extglob #enable extended pattern matching operators
function audit_DEBUG() {
if [ "$BASH_COMMAND" != "$PROMPT_COMMAND" ] #avoid logging unexecuted commands after 'ctrl-c or 'empty+enter'
then
local AUDIT_CMD="$(history 1)" #current history command
if ! logger -p user.info -t "$AUDIT_STR $PWD" "${AUDIT_CMD##*( )?(+([0-9])[^0-9])*( )}"
then
echo error "$AUDIT_STR $PWD" "${AUDIT_CMD##*( )?(+([0-9])[^0-9])*( )}"
fi
fi
}
function audit_EXIT() {
local AUDIT_STATUS="$?"
logger -p user.info -t "$AUDIT_STR" "#=== bash session ended. ==="
exit "$AUDIT_STATUS"
}
declare -fr +t audit_DEBUG
declare -fr +t audit_EXIT
logger -p user.info -t "$AUDIT_STR" "#=== New bash session started. ===" #audit the session openning
#when a bash command is executed it launches first the audit_DEBUG(),
#then the trap DEBUG is disabled to avoid a useless rerun of audit_DEBUG() during the execution of pipes-commands;
#at the end, when the prompt is displayed, re-enable the trap DEBUG
declare -rx PROMPT_COMMAND="trap 'audit_DEBUG; trap DEBUG' DEBUG"
declare -rx BASH_COMMAND #current command executed by user or a trap
declare -rx SHELLOPT #shell options, like functrace
trap audit_EXIT EXIT
請參閱此處詳細解釋的方法: http://blog.pointsoftware.ch/index.php/howto-bash-audit-command-logger
弗朗索瓦·舍雷爾歡呼
答案3
你可以嘗試蒂普德。它比你想要的要多,因為它會記錄整個 tty。
我自己沒有使用過它,但它的工作方式(在核心中)使得用戶無法更改日誌。
答案4
您可以啟用系統審核。