bashの履歴をsyslogに送信する

bashの履歴をsyslogに送信する

Bashバージョン4.Nでは、システムログにコマンド履歴を記録できるように見えますが、この機能を構成する方法に関する情報が見つかりません。

、およびトラップを使用してハッキングを提供するいくつかのページを読んで、PROMPT_COMMAND利用可能なパッチがあることを知っていますが、今組み込まれているのでこれは必要ありません。

キャプチャコマンドを使用できることを知っていますが、auditdbash / syslogの組み合わせを使用したいと思います。

ベストアンサー1

このアプローチはまさにあなたが探しているようです。これについては、次のタイトルの記事で説明します。Bashフォレンジックの機能強化

抜粋:

彼とこのトピックについて議論したところ、私は法医学調査を改善するために命令記録を細かく調整する方法があることに気づきました。 2009年にも書きました。ブログBashの投稿は、Bashコマンド履歴をリモートSyslogサーバーに送信するいくつかのアイデアを提供します。私のウェブログを見てみると、このブログ投稿は過去30日間に1000回以上の訪問を記録し、まだ人気を集めています!私のブログの投稿は古いです。 Bashはバージョン4.1以降、デフォルトでSyslogをサポートしていますが、ほとんどのディストリビューションでは有効になっていません。この機能を使用するには、シェルを再コンパイルする必要があります。

私はこれがあまり便利ではないと思いますが、利点はユーザーがそれを無効にすることができないことです(シェルを他のシェルまたは別のBashバイナリに切り替えない限り)。 config-top.hで "SYSLOG_HISTORY"を定義するだけです。

$ 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がどのオプションでコンパイルされたかを確認する方法が見つからなかったため、この質問に答えるには、.specBash RPMのビルドに使用されたアップストリームファイル(Redhatディストリビューションなど)を確認することをお勧めします。 Debian ベースのディストリビューションでも使用できます。

確認のために.specCentOS 7.2.1511に付属のBashファイルを見てみましたが、この機能を有効にしません。

$ grep configure bash.spec
%configure --with-bash-malloc=no --with-afs
- Use the configure macro instead of calling ./configure directly

自分のRPMを転がしてみてください

上記の詳細の要旨に基づいて、独自のBashを構築するためにとったステップは次のとおりです。

ソースのダウンロードとインストール

スタート一つダウンロードしましたソース速度(SRPM)CentOS 7.x用のリリース。ダウンロード後、私のrpmbuildディレクトリにインストールしました。

$ rpmdev-setuptree
$ rpm -ivh bash-4.2.46-20.el7_2.src.rpm
修理する

ファイルが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

編集して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

関数を編集し、次のようにしますrpmbuild/SOURCES/bash-4.2/bashhist.cbash_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);
    }
}

これで.patch、2つのファイルに対する変更を含むファイルを作成します。

$ cd $HOME/rpmbuild/SOURCES
$ diff -Npru bash-4.2-orig bash-4.2 > bash_history_rsyslog.patch

$HOME/rpmbuid/SPEC/bash.spec次の行をSPECファイルの適切な場所に追加します。

# history syslog
Patch144: bash_history_rsyslog.patch
...
...
%patch144 -p1 -b .history_rsyslog
立てる

今ビルドしてください。

$ cd $HOME/rpmbuild/SPEC
$ rpmbuild -ba bash.spec

ビルドの終わりは次のとおりです。

...
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
インストールする

完了したら、生成された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%]
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"
*.* ?IP テンプレート
&~

...
...

その後、Rsyslog を再起動します。

$ sudo systemctl restart rsyslog
テストと確認

次に、新しいBashインスタンスをrootとして実行し、いくつかのコマンドを実行します。

$ sudo -Es
$ ls

そしてログファイルを追跡します/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

CMD=...このログには何行が表示されていますか?実行したばかりのコマンドはls&ですtail

事前構築?

この問題を抱えている人を助けるために、私はCoprの修正を使ってBash RPMを自由に構築しました。

おすすめ記事