从bash4.1 版本开始,bash开始支持Rsyslog,所以我们需要下载bash4.1以后版本,这里以bash4.4为例
bash源码下载地址: https://ftp.gnu.org/gnu/bash/
共计需要修改两处源码:
该源码文件和linux history记录处理相关
#if defined (SYSLOG_HISTORY)
#define SYSLOG_MAXLEN 600
extern char *shell_name;
#ifndef OPENLOG_OPTS
#define OPENLOG_OPTS 0
#endif
void
bash_syslog_history (line)
const char *line;
{
char trunc[SYSLOG_MAXLEN];
static int first = 1;
if (first)
{
openlog (shell_name, OPENLOG_OPTS, SYSLOG_FACILITY);
first = 0;
}
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 (TRUNCATED): PID=%d UID=%d User=%s CMD=%s", getpid(), current_user.uid, current_user.user_name, trunc);
}
}
#endif
两个syslog分别为771和776行
其中PID表示当前执行linux命令的bash进程ID,使用函数getpid()获取;UID表示执行命令用户的ID,用current_user.uid 表示;User表示执行命令的用户名,用 current_user.user_name表示;CMD表示执行的历史命令内容。需要修改771行和776行,修改内容,如上所示
/* 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_USER
# define SYSLOG_LEVEL LOG_INFO
# define OPENLOG_OPTS LOG_PID
#endif
这里定义syslog的FACILITY为 user (用户级别的日志)去掉116行中的/**/,去掉之后,如上所示
日志级别为info
./configure --prefix=/usr/local/bash && make && make install
root:x:0:0:root:/root:/usr/local/bash/bin/bash
daizy:x:1000:1000::/home/daizy:/usr/local/bash/bin/bash
创建
bash log
的本地存储文件
touch /var/log/bash.log chmod 664 /var/log/bash.log |
vim /etc/rsyslog.conf
在末尾添加如下内容
:
# collect bash history command info user.info /var/log/bash.log |
如果是远程手机syslog,则可以更改rsyslog.conf成如下内容:
syslog,则可以更改rsyslog.conf成如下内容:
|
service rsyslog restart
最后查看本地syslog日志
tail -f /var/log/bash.log
Jul 25 16:22:15 localhost bash[18540]: PID=18540 UID=0 User=root Cmd=tail -f /var/log/bash.log
Jul 25 16:22:21 localhost bash[19033]: PID=19033 UID=0 User=root Cmd=whoami
禁用其他shell,并且注意编译的bash与目标版本一致。
# chmod 750 /bin/csh
# chmod 750 /bin/tcsh
# chmod 750 /bin/dash
参考文章:
https://paper.tuisec.win/detail/85467b303b03c99
http://vinc.top/2017/08/03/%E3%80%90%E4%BC%81%E4%B8%9A%E5%AE%89%E5%85%A8%E5%AE%9E%E6%88%98%E3%80%91%E8%BF%90%E7%BB%B4bash%E5%91%BD%E4%BB%A4%E5%AE%A1%E8%AE%A1/