Gra*_*lls 6 bash command-history syslog
Bash 版本 4.N 显然能够将命令历史写入 syslog,但我找不到有关如何配置它的信息。
我已经阅读了几页提供使用PROMPT_COMMAND, 和 trap 的黑客攻击的页面,我知道有一个可用的补丁,但这应该是不必要的,因为它现在是内置的。
我知道我可以auditd用来捕获命令,但我想使用 bash/syslog 组合。
slm*_*slm 10
这种方法似乎是您正在寻找的。本文对其进行了讨论,标题为:Improving Bash Forensics Capabilities。
摘抄:
在与他讨论这个话题时,我意识到有一些方法可以微调命令的历史记录以改进取证调查。2009 年,我还写了一篇关于 Bash的博客文章,其中提供了一些将 Bash 命令历史发送到远程 Syslog 服务器的想法。我检查了我的网络日志,这篇博文在过去 30 天内仍然很受欢迎,访问量超过 1000 次!请注意,我的博文已过时:从 4.1 版开始,Bash 本身就支持 Syslog,但在大多数发行版中,它并未启用。要使用此功能,您需要重新编译您的 shell。
我发现这不是很方便,但好处是它不能被用户禁用(除非他将他的 shell 切换到另一个 shell 或另一个 Bash 二进制文件)。你只需要在 config-top.h 中定义“SYSLOG_HISTORY”:
Run Code Online (Sandbox Code Playgroud)$ vi config-top.h #define SYSLOG_HISTORY #if defined (SYSLOG_HISTORY) # define SYSLOG_FACILITY LOG_USER # define SYSLOG_LEVEL LOG_INFO #endif ./configure make install
因此,虽然该功能在 Bash 4.1+ 中可用,但在大多数流行发行版中,它可能不会默认使用 Bash 编译。
我还没有找到一种方法来查看给定 Bash 编译的选项,因此要回答这个问题,您可能需要查看.spec构建 Bash RPM 时使用的上游文件,例如,在 Redhat 发行版上,同样的方法也可以用于基于 Debian 的发行版。
作为确认,我查看.spec了 CentOS 7.2.1511 附带的 Bash文件,但它没有启用此功能:
$ grep configure bash.spec
%configure --with-bash-malloc=no --with-afs
- Use the configure macro instead of calling ./configure directly
Run Code Online (Sandbox Code Playgroud)
以下是我使用上述详细说明的要点来构建自己的 Bash 的步骤。
下载安装源码首先,我下载了可用于 CentOS 7.x 的 Bash源 RPM (SRPM)。下载后,我将其安装到我的rpmbuild目录中:
$ rpmdev-setuptree
$ rpm -ivh bash-4.2.46-20.el7_2.src.rpm
Run Code Online (Sandbox Code Playgroud)
修补
这会将文件解压为rpmbuild/SPEC& rpmbuild/SOURCES。现在我们将tar.gz使用以下步骤复制解压后的 Bash文件的内容:
$ cd rpmbuild/SOURCES
$ tar zxf bash-4.2.tar.gz
$ cp -prf bash-4.2 bash-4.2-orig
$ cd bash-4.2
Run Code Online (Sandbox Code Playgroud)
编辑rpmbuild/SOURCES/bash-4.2/config-top.h并使其看起来像这样:
/* Define if you want each line saved to the history list in bashhist.c:
bash_add_history() to be sent to syslog(). */
#define SYSLOG_HISTORY
#if defined (SYSLOG_HISTORY)
# define SYSLOG_FACILITY LOG_LOCAL1
# define SYSLOG_LEVEL LOG_DEBUG
#endif
Run Code Online (Sandbox Code Playgroud)
编辑rpmbuild/SOURCES/bash-4.2/bashhist.c并使bash_syslog_history函数看起来像这样:
void
bash_syslog_history (line)
const char *line;
{
char trunc[SYSLOG_MAXLEN];
if (strlen(line) < SYSLOG_MAXLEN)
syslog (SYSLOG_FACILITY|SYSLOG_LEVEL, "HISTORY: PID=%d UID=%d USER=%s CMD=%s", getpid(), current_user.uid, current_user.user_name, line);
else
{
strncpy (trunc, line, SYSLOG_MAXLEN);
trunc[SYSLOG_MAXLEN - 1] = '\0';
syslog (SYSLOG_FACILITY|SYSLOG_LEVEL, "HISTORY: PID=%d UID=%d USER=%s CMD(TRUNCATED)=%s", getpid(), current_user.uid, current_user.user_name, trunc);
}
}
Run Code Online (Sandbox Code Playgroud)
现在生成一个.patch文件,其中包含我们对这两个文件的更改:
$ cd $HOME/rpmbuild/SOURCES
$ diff -Npru bash-4.2-orig bash-4.2 > bash_history_rsyslog.patch
Run Code Online (Sandbox Code Playgroud)
将这些行添加到$HOME/rpmbuid/SPEC/bash.spec. 将这些行放入 SPEC 文件中的适当位置:
# history syslog
Patch144: bash_history_rsyslog.patch
...
...
%patch144 -p1 -b .history_rsyslog
Run Code Online (Sandbox Code Playgroud)
建造
现在构建它:
$ cd $HOME/rpmbuild/SPEC
$ rpmbuild -ba bash.spec
Run Code Online (Sandbox Code Playgroud)
此构建的尾部将如下所示:
...
Processing files: bash-debuginfo-4.2.46-20.el7.centos.x86_64
Provides: bash-debuginfo = 4.2.46-20.el7.centos bash-debuginfo(x86-64) = 4.2.46-20.el7.centos
Requires(rpmlib): rpmlib(FileDigests) <= 4.6.0-1 rpmlib(PayloadFilesHavePrefix) <= 4.0-1 rpmlib(CompressedFileNames) <= 3.0.4-1
Checking for unpackaged file(s): /usr/lib/rpm/check-files /home/vagrant/rpmbuild/BUILDROOT/bash-4.2.46-20.el7.centos.x86_64
Wrote: /home/vagrant/rpmbuild/RPMS/x86_64/bash-4.2.46-20.el7.centos.x86_64.rpm
Wrote: /home/vagrant/rpmbuild/RPMS/x86_64/bash-doc-4.2.46-20.el7.centos.x86_64.rpm
Wrote: /home/vagrant/rpmbuild/RPMS/x86_64/bash-debuginfo-4.2.46-20.el7.centos.x86_64.rpm
Executing(%clean): /bin/sh -e /var/tmp/rpm-tmp.nGworU
+ umask 022
+ cd /home/vagrant/rpmbuild/BUILD
+ cd bash-4.2
+ rm -rf /home/vagrant/rpmbuild/BUILDROOT/bash-4.2.46-20.el7.centos.x86_64
+ exit 0
Run Code Online (Sandbox Code Playgroud)
安装
完成后,我们可以安装生成的 RPM:
$ sudo rpm -ivh --force $HOME/rpmbuild/RPMS/x86_64/bash-4.2.46-20.el7.centos.x86_64.rpm
Preparing... ################################# [100%]
Updating / installing...
1:bash-4.2.46-20.el7.centos ################################# [100%]
Run Code Online (Sandbox Code Playgroud)
配置 rsyslog
现在修改 rsyslog 的/etc/rsyslog.conf配置:
$ sudo vim /etc/rsyslog.conf ... ... $ModLoad imudp $UDPServerRun 514? $ModLoad imtcp $InputTCPServerRun 514 ... ... #### 全球指令#### $template IpTemplate,"/var/log/bash-log/%FROMHOST-IP%.log" *.* ?IpTemplate &~ ... ...
然后重启Rsyslog:
$ sudo systemctl restart rsyslog
Run Code Online (Sandbox Code Playgroud)
测试和确认
现在以 root 身份运行一个新的 Bash 实例,并运行几个命令:
$ sudo -Es
$ ls
Run Code Online (Sandbox Code Playgroud)
并拖尾日志文件,/var/log/bash-log/127.0.0.1.log:
$ tail /var/log/bash-log/127.0.0.1.log
2018-07-19T23:23:37.568131-04:00 centos7 bash: HISTORY: PID=12511 UID=1000 USER=vagrant CMD=sudo -Es
2018-07-19T23:23:37.573825-04:00 centos7 sudo: vagrant : TTY=pts/0 ; PWD=/home/vagrant/rpmbuild/SOURCES ; USER=root ; COMMAND=/bin/bash
2018-07-19T23:23:37.589258-04:00 centos7 systemd-logind: Got message type=signal sender=org.freedesktop.DBus destination=n/a object=/org/freedesktop/DBus interface=org.freedesktop.DBus member=NameOwnerChanged cookie=4454 reply_cookie=0 error=n/a
2018-07-19T23:23:37.590633-04:00 centos7 dbus[588]: [system] Activating service name='org.freedesktop.problems' (using servicehelper)
2018-07-19T23:23:37.590806-04:00 centos7 dbus-daemon: dbus[588]: [system] Activating service name='org.freedesktop.problems' (using servicehelper)
2018-07-19T23:23:37.592160-04:00 centos7 dbus[588]: [system] Activated service 'org.freedesktop.problems' failed: Failed to execute program /lib64/dbus-1/dbus-daemon-launch-helper: Success
2018-07-19T23:23:37.592311-04:00 centos7 dbus-daemon: dbus[588]: [system] Activated service 'org.freedesktop.problems' failed: Failed to execute program /lib64/dbus-1/dbus-daemon-launch-helper: Success
2018-07-19T23:23:37.602174-04:00 centos7 systemd-logind: Got message type=signal sender=org.freedesktop.DBus destination=n/a object=/org/freedesktop/DBus interface=org.freedesktop.DBus member=NameOwnerChanged cookie=4455 reply_cookie=0 error=n/a
2018-07-19T23:23:38.520300-04:00 centos7 bash: HISTORY: PID=12585 UID=0 USER=root CMD=ls
2018-07-19T23:23:49.210406-04:00 centos7 bash: HISTORY: PID=12585 UID=0 USER=root CMD=tail /var/log/bash-log/127.0.0.1.log
Run Code Online (Sandbox Code Playgroud)
注意到CMD=...这个日志中的行了吗?这些是我们刚刚运行的命令, a ls& tail。
为了帮助人们解决这个问题,我冒昧地使用 Copr 中的修改构建了 Bash RPM。