简体   繁体   English

如何捕获任何用户在Unix / Linux中输入的所有命令?

[英]How to capture all the commands typed in Unix/Linux by any user?

I would like to capture all the commands typed in Unix/Linux by any user. 我想捕获任何用户在Unix / Linux中输入的所有命令。 There are few alternatives like using script command or acct utility. 有很多替代方法,比如使用脚本命令或acct实用程序。 But the problem with them is they dumb everything from the terminal to a file or just provide the summary of the commands. 但是他们的问题是他们从终端到文件的所有内容都是愚蠢的,或者只是提供命令的摘要。 I am looking for a utility where it will provide me all the commands typed by any user with the arguments for the commands. 我正在寻找一个实用程序,它将为我提供由任何用户键入的所有命令和命令的参数。 Is it possible? 可能吗? Are there any alternatives like making a hook into system calls to get this? 是否有任何替代方案,如挂钩系统调用来获得这个?

I know this is old, but I think the script command might be what he was looking for? 我知道这是旧的,但我认为脚本命令可能是他想要的?

> script my_output_file
Script started, file is my_output_file

http://www-users.cs.umn.edu/~gini/1901-07s/files/script.html http://www-users.cs.umn.edu/~gini/1901-07s/files/script.html

There seems to be quite a good article on shell auditing at http://administratosphere.wordpress.com/2011/05/20/logging-every-shell-command/ . http://administratosphere.wordpress.com/2011/05/20/logging-every-shell-command/上似乎有一篇关于shell审计的文章。

This considers things like reliability of user history files (and provides info on improving that), but also discusses explicit auditing features built into shells. 这考虑了诸如用户历史文件的可靠性之类的内容(并提供了有关改进的信息),还讨论了内置于shell中的显式审计功能。 It may be that whatever environment you're using doesn't have the shells compiled with auditing features enabled, but if you have the source and configuration for your builds available (as you would do at least for any Linux distribution), it shouldn't be too hard to enable the auditing feature while keeping rest of the configuration as it is in the default distribution. 可能是您使用的任何环境都没有启用审计功能编译的shell,但是如果您的构建的源和配置可用(至少对于任何Linux发行版都是如此),它应该是'太难以启用审计功能,同时保留配置的其余部分,因为它在默认分发中。

What this approach still would leave open is the commands executed through some other command - or operating system functionality called from within some program. 这种方法仍然会打开的是通过其他命令执行的命令 - 或者从某些程序中调用的操作系统功能。 So, f.ex. 所以,f.ex。 if you have perl, or any other programming language interpreter available on the machine, while you possibly can audit the execution of perl, you cannot tell what the user had told the perl interpreter to do. 如果您在机器上有perl或任何其他编程语言解释器,而您可能可以审计perl的执行,则无法告诉用户告诉perl解释器要做什么。 On the other hand, even with shell auditing, I'm not certain whether the perl execution would be seen if it was executed f.ex. 另一方面,即使使用shell审计,我也不确定如果执行f.ex,是否会看到perl执行。 from within some editor (like vi) as a filter to process whatever had been written within the editor. 从某些编辑器(如vi)中作为过滤器来处理编辑器中编写的任何内容。

So, while shell auditing will provide you one layer of auditing, the gain is not that great unless your environment is really tightened against other paths of execution than the shell. 因此,虽然shell审计将为您提供一层审计,但增益并不是那么好,除非您的环境真的比其他执行路径更紧密而不是shell。

You should consider whether the users to be audited actually need shell access - and if not, provide them with something more limited, with auditing capabilities. 您应该考虑要审核的用户是否确实需要shell访问 - 如果没有,请为他们提供更有限的内容,并具有审核功能。 A small text-based menu system, perhaps? 也许是一个基于文本的小菜单系统?

You can use Snoopy Logger 您可以使用Snoopy Logger

What it is: A simple library that inserts itself between process and execv/e() syscalls by means of LD preloading. 它是什么:一个简单的库,它通过LD预加载在进程和execv / e()系统调用之间插入它自己。 It logs all executed commands to syslog. 它将所有执行的命令记录到syslog中。

What it is not: Security/auditing solution - it can be easily circumvented. 它不是什么:安全/审计解决方案 - 它可以很容易地被规避。 It does not log built-in shell commands (as they are internal and when called, shell does not create new process - echo vs /bin/echo for example). 它不记录内置shell命令(因为它们是内部的,并且在调用时,shell不会创建新进程 - 例如echo vs / bin / echo)。

Disclosure: current snoopy maintainer here. 披露:这里的当前snoopy维护者。

Grab the bash source. 抓住bash源。 Add a logger around the exec invocation. 在exec调用周围添加一个记录器。 Compile it. 编译它。 Run this as your first command after logging in. 登录后将其作为第一个命令运行。

Everything else really wants root powers. 其他一切都非常需要根源。

Grab /home/victim/.bash_history or /home/victim/.config/fish/fish_history 抓住/home/victim/.bash_history/home/victim/.config/fish/fish_history

These will let you see all bash and fish shell commands with args that ws entered by the user. 这些将允许您使用用户输入的args查看所有bash和fish shell命令。

There is enabling audit, tty recording, hacks and free tools to do what you want, however, depending on the scale of the environment you are trying to control, you may be better off by using both Audit and products focused on dealing with the challenge you want to tackle. 有启用审计,tty记录,黑客和免费工具来做你想做的事情,但是,根据你试图控制的环境规模,你可能会更好地使用审计和专注于应对挑战的产品你想要解决。 Some quite popular ones, used on a few financial services clients are [ Observe-IT , Centrify and PowerBroker 在一些金融服务客户端上使用的一些非常受欢迎的是[ Observe-ITCentrifyPowerBroker

Hope this helps 希望这可以帮助

For sporadic recording I usually run 对于零星录音,我经常跑

ssh localhost | ssh localhost | tee -a recorded-session.log tee -a recorded-session.log

This works quite well. 这非常有效。

The easiest way to solve this, is if you are root and in the position to change the system files by redirect the console itself that way: 解决此问题的最简单方法是,如果您是root用户并且能够通过以下方式重定向控制台本身来更改系统文件:

If you use eg /bin/sh as default console, move it to /bin/hs and create a file like this under /bin/sh : 如果您使用eg /bin/sh作为默认控制台,请将其移至/bin/hs并在/bin/sh下创建如下文件:

#!/bin/hs

ORIGSHELL=/bin/hs
LOGFILE=/var/log/whatyoulike

OPTIONS="$@"

USER=`whoami`
WEBUSER=web
WILD=NO
WARN=NO



if [ "$USER" = "$WEBUSER" ]
then

  #Ok then - are we doing something wild?
  for ARG in $@
  do
    case "$ARG" in
    *\/lynx)
      WILD=YES
      ;;
    *\/wget)
      WILD=YES
      WARN=YES
      ;;
    *\/curl)
      WILD=YES
      WARN=YES
      ;;
    *\/links)
      WILD=YES
      WARN=YES
      ;;
    *\/fetch)
      WILD=YES
      WARN=YES
      ;;
    esac
  done

  #Are we wild?
  if [ "$WILD" = "YES" ]
  then
    HOST=`hostname`
    IPADDR=`resolveip -s $HOST`
    NETSTAT=`/usr/bin/nighthawk -ape --numeric-hosts --numeric-ports --tcp | grep 'ESTABLISHED web'`

    # Log it.
    echo "`date` [$USER] $IPADDR "$@"" >> $LOGFILE
    echo "$NETSTAT" >> $LOGFILE
  fi
  #Are we REALLY wild?
  if [ "$WARN" = "YES" ]
  then
    # Mail it!
    mail -s 'HACKATTACK' youremail@domain.com < $LOGFILE &
  fi
fi

# Now, do it.
exec $OPERATION "$@"

#we never come here...
exit 0

This is just an example, how it can be used to track everything transparent. 这只是一个例子,它如何用于跟踪透明的一切。 You can do what you want to check the input. 您可以执行要检查输入的操作。 The script above is used to find even the originator of the current shell, so you can react on it. 上面的脚本用于查找当前shell的发起者,因此您可以对其做出反应。 Of course the above case checks are not the ones we really use ;) - but a good sample. 当然上面的案例检查不是我们真正使用的;) - 但是一个很好的样本。

Hope it helps, Jimmy 希望它有所帮助,吉米

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM