사용자의 bash 명령을 어떻게 기록합니까?

사용자의 bash 명령을 어떻게 기록합니까?

나는 사용자가 ssh를 통해 chroot 감옥에 로그인하는 데비안 에칭 서버를 실행하고 있습니다. 실행하는 명령을 삭제하거나 방지할 수 없는 방식으로 기록하려면 어떻게 해야 합니까?

답변1

설치하다스누피. 한 명의 사용자만 기록하려면 syslog 필터링 fu를 수행하십시오.

답변2

나는 패치나 특별한 실행 도구를 사용하지 않고 모든 'bash' 명령/기본 기능을 텍스트 파일이나 'syslog' 서버에 기록하는 방법을 작성했습니다.

'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

Francois Scheurer를 응원합니다

답변3

시도해 볼 수도 있습니다ttyrpld. 전체 tty를 기록하므로 원하는 것보다 더 많습니다.
나는 그것을 직접 사용하지 않았지만 그것이 작동하는 방식(커널에서)으로 인해 사용자는 로그를 변경할 수 없습니다.

답변4

시스템 감사를 활성화할 수 있습니다.

관련 정보