注意:

使用稽核稽核稽核稽核稽核 Oracle Linux

簡介

稽核是一種使用者空間系統常駐程式,會在背景中執行,以產生與 Oracle Linux 上執行之不同活動相關的日誌。

本教學課程將引導您完成安裝、組態以及使用稽核功能。

目標

在本實驗室中,您將瞭解:

必要條件

安裝稽核套裝程式

注意:使用免費實驗室環境時,請參閱 Oracle Linux Lab Basics,瞭解連線和其他使用指示。

Oracle Linux 預設會安裝稽核套裝軟體。

如果尚未連接,請開啟終端機並透過 ssh 連線至 ol8-server 系統。

ssh oracle@<ip_address_of_ol8-server>

檢查系統是否已預先安裝 audit 套裝軟體。

sudo dnf list installed "audit"

若不在系統上,請進行安裝。

sudo dnf install -y audit

auditd 常駐程式的預設配置檔案位於 /etc/audit/auditd.conf

管理稽核服務

安裝稽核套裝軟體之後,請啟動 auditd 服務。首先,檢查目前的狀態。

sudo systemctl status auditd

範例輸出:

[oracle@ol8-server ~]$ sudo systemctl status auditd
* auditd.service - Security Auditing Service
   Loaded: loaded (/usr/lib/systemd/system/auditd.service; enabled; vendor pres>
   Active: active (running) since Fri 2022-04-01 23:59:52 GMT; 2min 38s ago
     Docs: man:auditd(8)
           https://github.com/linux-audit/audit-documentation
 Main PID: 1385 (auditd)
    Tasks: 2 (limit: 100140)
   Memory: 2.5M
   CGroup: /system.slice/auditd.service
           `-1385 /sbin/auditd

Apr 01 23:59:52 localhost.localdomain augenrules[1388]: backlog_wait_time 60000
Apr 01 23:59:52 localhost.localdomain augenrules[1388]: enabled 1
Apr 01 23:59:52 localhost.localdomain augenrules[1388]: failure 1
Apr 01 23:59:52 localhost.localdomain augenrules[1388]: pid 1385
Apr 01 23:59:52 localhost.localdomain augenrules[1388]: rate_limit 0
Apr 01 23:59:52 localhost.localdomain augenrules[1388]: backlog_limit 8192
Apr 01 23:59:52 localhost.localdomain augenrules[1388]: lost 0
Apr 01 23:59:52 localhost.localdomain augenrules[1388]: backlog 0
Apr 01 23:59:52 localhost.localdomain augenrules[1388]: backlog_wait_time 60000
Apr 01 23:59:52 localhost.localdomain systemd[1]: Started Security Auditing Ser>
lines 1-21/21 (END)

輸出顯示服務為 (running)enabled

enabled 狀態代表我們將服務設定為在開機時啟動。若不是這種情況,請以 root 使用者身分啟動服務,以收集稽核資訊並寫入日誌。

啟動服務。

sudo service auditd start

auditd 配置為在啟動時啟動。

sudo systemctl enable auditd

注意:只有能適當地與稽核常駐程式互動的方式會使用 service 指令。

service 指令可確保記錄 auid 值。僅將 systemctl 指令與 enablestatus 動作搭配使用。

暫時啟用和停用稽核

稽核控制公用程式 auditctl 會與核心稽核元件互動,以管理規則及控制事件產生程序的許多設定和參數。

檢查核心稽核子系統的狀態。

sudo auditctl -s

範例輸出:

[oracle@ol8-server ~]$ sudo auditctl -s
enabled 1
failure 1
pid 1399
rate_limit 0
backlog_limit 8192
lost 0
backlog 0
backlog_wait_time 60000
loginuid_immutable 0 unlocked

暫時停用 auditd

sudo auditctl -e 0

重新啟用 auditd

sudo auditctl -e 1

注意:pid 顯示 auditd 服務程序 ID。pid0 表示服務未執行。

auditctl -e 啟用旗標也接受鎖定稽核組態的值 2

如果稽核組態以此方式鎖定,就需要重新開機才能解除鎖定。如需詳細資訊,請參閱 man auditctl

尋找稽核規則與日誌

Oracle Linux 預設會將稽核日誌儲存在 /var/log/audit/audit.log 中。

稽核規則位於 /ect/audit/audit.rules。Oracle Linux 會從檔案 /etc/audit/rules.d/audit.rules 產生預設規則集。

sudo cat /etc/audit/audit.rules
sudo cat /etc/audit/rules.d/audit.rules

範例輸出:

## First rule - delete all
-D

## Increase the buffers to survive stress events.
## Make this bigger for busy systems
-b 8192

## This determine how long to wait in burst of events
--backlog_wait_time 60000

## Set failure mode to syslog
-f 1

使用稽核控制公用程式的規則

使用 auditctl 程式來控制行為、取得狀態以及新增或刪除規則。

新增記錄任何讀取或修改 /etc/ssh/sshd_config 檔案嘗試的稽核規則。

sudo auditctl -w /etc/ssh/sshd_config -p rwxa -k sshd_config

其中:

顯示規則。

sudo auditctl -l

新規則會新增至清單底部,但也可以將其新增至頂端。

檢查新規則是否已新增至 /etc/audit/audit.rules 檔案。

sudo cat /etc/audit/audit.rules

範例輸出:

[oracle@ol8-server ~]$ sudo cat /etc/audit/audit.rules
## This file is automatically generated from /etc/audit/rules.d
-D
-b 8192
-f 1
--backlog_wait_time 60000

規則不會出現在檔案中。為什麼?

auditctl 建立的規則不會新增至 audit.rules 檔案。因此,這些變更相當暫態,不會留在系統重新啟動上。

將規則新增至 /etc/audit/rules.d/my.rules 中的自訂規則集檔案,使其成為永久規則。新增規則的格式會比對 auditctl 命令的語法,而不需要使用 auditctl。每一行應撰寫規則並合併以最佳化效能。

sudo tee /etc/audit/rules.d/my.rules > /dev/null <<'EOF'
-w /etc/ssh/sshd_config -p rwxa -k sshd_config
EOF

顯示規則。

sudo cat /etc/audit/rules.d/my.rules

測試規則。

cat /etc/ssh/sshd_config

指令會傳回 cat: /etc/ssh/sshd_config: Permission denied,並在 audit.log 中產生以下事件。

sudo cat /var/log/audit/audit.log | grep sshd_config

範例輸出:

type=CONFIG_CHANGE msg=audit(1648918923.746:266810): auid=1001 ses=15792 
subj=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 op=add_rule key="sshd_config" list=4 res=1AUID="oracle"
type=SYSCALL msg=audit(1648923583.793:268315): arch=c000003e syscall=257 success=no exit=-13 a0=ffffff9c a1=7ffd08b6c575 > a2=0 a3=0 items=1 ppid=3406680 pid=3428336 auid=1001 uid=1001 gid=1001 euid=1001 suid=1001 fsuid=1001 egid=1001 
sgid=1001 fsgid=1001 tty=pts0 ses=15792 comm="cat" exe="/usr/bin/cat" 
subj=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 key="sshd_config"ARCH=x86_64 SYSCALL=openat AUID="oracle" 
UID="oracle" GID="oracle" EUID="oracle" SUID="oracle" FSUID="oracle" EGID="oracle" SGID="oracle" FSGID="oracle"
type=PATH msg=audit(1648923583.793:268315): item=0 name="/etc/ssh/sshd_config" inode=67688941 dev=fc:00 mode=0100600 
ouid=0 ogid=0 rdev=00:00 obj=system_u:object_r:etc_t:s0 nametype=NORMAL cap_fp=0 cap_fi=0 cap_fe=0 cap_fver=0 
cap_frootid=0OUID="root" OGID="root"

搜尋稽核日誌

另一種搜尋日誌的方式是使用 ausearch 命令。

使用此指令取得相同的資訊。

sudo ausearch --key sshd_config

範例輸出:

Email option is specified but /usr/lib/sendmail doesn't seem executable.
----
time->Sat Apr  2 17:02:03 2022
type=CONFIG_CHANGE msg=audit(1648918923.746:266810): auid=1001 ses=15792 
subj=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 op=add_rule key="sshd_config" list=4 res=1
----
time->Sat Apr  2 18:19:43 2022
type=PROCTITLE msg=audit(1648923583.793:268315): proctitle=636174002F6574632F7373682F737368645F636F6E666967
type=PATH msg=audit(1648923583.793:268315): item=0 name="/etc/ssh/sshd_config" inode=67688941 dev=fc:00 mode=0100600 
ouid=0 ogid=0 rdev=00:00 obj=system_u:object_r:etc_t:s0 nametype=NORMAL cap_fp=0 cap_fi=0 cap_fe=0 cap_fver=0 
cap_frootid=0
type=CWD msg=audit(1648923583.793:268315): cwd="/home/oracle"
type=SYSCALL msg=audit(1648923583.793:268315): arch=c000003e syscall=257 success=no exit=-13 a0=ffffff9c a1=7ffd08b6c575 > a2=0 a3=0 items=1 ppid=3406680 pid=3428336 auid=1001 uid=1001 gid=1001 euid=1001 suid=1001 fsuid=1001 egid=1001 
sgid=1001 fsgid=1001 tty=pts0 ses=15792 comm="cat" exe="/usr/bin/cat" 
subj=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 key="sshd_config"

ausearch 的輸出較容易使用,並根據輸出格式加以讀取。新增 -i--interpret 選項,即可輕鬆閱讀日誌。此選項會將數字實體解譯成文字,例如將 uid 轉換為帳戶名稱。

sudo ausearch -i -k sshd_config

範例輸出:

Email option is specified but /usr/lib/sendmail doesn't seem executable.
----
type=CONFIG_CHANGE msg=audit(04/02/2022 17:02:03.746:266810) : auid=oracle ses=15792 
subj=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 op=add_rule key=sshd_config list=exit res=yes 
----
type=PROCTITLE msg=audit(04/02/2022 18:19:43.793:268315) : proctitle=cat /etc/ssh/sshd_config 
type=PATH msg=audit(04/02/2022 18:19:43.793:268315) : item=0 name=/etc/ssh/sshd_config inode=67688941 dev=fc:00 
mode=file,600 ouid=root ogid=root rdev=00:00 obj=system_u:object_r:etc_t:s0 nametype=NORMAL cap_fp=none cap_fi=none 
cap_fe=0 cap_fver=0 cap_frootid=0 
type=CWD msg=audit(04/02/2022 18:19:43.793:268315) : cwd=/home/oracle 
type=SYSCALL msg=audit(04/02/2022 18:19:43.793:268315) : arch=x86_64 syscall=openat success=no exit=EACCES(Permission 
denied) a0=0xffffff9c a1=0x7ffd08b6c575 a2=O_RDONLY a3=0x0 items=1 ppid=3406680 pid=3428336 auid=oracle uid=oracle 
gid=oracle euid=oracle suid=oracle fsuid=oracle egid=oracle sgid=oracle fsgid=oracle tty=pts0 ses=15792 comm=cat exe=/
usr/bin/cat subj=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 key=sshd_config 

如需更多格式選項與搜尋 audit.log 的方式,請參閱 man ausearch

載入預先設定規則

Oracle Linux 提供命令檔,可合併 /etc/audit/rules.d 中找到的所有元件稽核規則檔案。合併之後,新檔案將取代現有的 /etc/audit/audit.rules。此程序檔內建於 auditd 服務檔案中,並在服務啟動時執行。

檢查是否有任何現有的規則變更要載入。

sudo augenrules --check

輸出為 Rules have changed and should be updated。此變更是因為我們先前建立的 my.rules 檔案位於 /etc/audit/rules.d

刪除先前新增的 sshd_config 自訂規則,以輕鬆追蹤新的規則新增。

sudo auditctl -D -k sshd_config

合併 my.rules 自訂規則檔案。

sudo augenrules --load

範例輸出:

[oracle@ol8-server ~]$ sudo augenrules --load
No rules
enabled 1
failure 1
pid 1395
rate_limit 0
backlog_limit 8192
lost 0
backlog 0
backlog_wait_time 60000
enabled 1
failure 1
pid 1395
rate_limit 0
backlog_limit 8192
lost 0
backlog 0
backlog_wait_time 60000
enabled 1
failure 1
pid 1395
rate_limit 0
backlog_limit 8192
lost 0
backlog 0
backlog_wait_time 60000

檢查作用中稽核規則。

sudo auditctl -l

範例輸出:

[oracle@ol-lab-2022-03-23-182415-0 ~]$ sudo auditctl -l
-w /etc/ssh/sshd_config -p rwxa -k sshd_config

新增其他規則至新檔案 new.rules

注意:只有以 .rules 結尾的檔案會由 augenrules 讀取並載入。

sudo tee /etc/audit/rules.d/new.rules > /dev/null <<'EOF'
-w /etc/passwd -p wa -k passwd_changes
-w /etc/selinux/ -p wa -k selinux_changes
EOF

載入新規則。

sudo augenrules --load

重新檢查作用中的規則。

sudo auditctl -l

新規則會載入並合併至 audit.rules 檔案。

sudo cat /etc/audit/audit.rules

系統會將備份檔 /etc/audit/audit.rules.prev 設為合併的一部分。

sudo ls -l /etc/audit

Oracle Linux Auditing System 提供的資訊可協助入侵偵測。

查看顯示公用程式的 man 頁面。然後使用您學習的事項,為特定記錄日誌事件將自訂稽核規則新增至您的系統。

其他相關資訊

請參閱其他相關資源:

其他學習資源

探索 docs.oracle.com/learn 上的其他實驗室,或前往 Oracle Learning YouTube 通道存取更多免費學習內容。此外,請造訪 education.oracle.com/learning-explorer 以成為 Oracle Learning Explorer。

如需產品文件,請瀏覽 Oracle Help Center