linux歷史命令審計


一、添加歷史命令記錄

1.首先在/etc/profile中添加

export HISTORY_FILE=/var/log/Command/Command.log
export PROMPT_COMMAND='{ date "+%Y-%m-%d %T # $(who am i |awk "{print \$1,\$2,\$5}") $(whoami):[$PWD]# $(history 1 | { read x cmd; echo "$cmd"; })"; } >>$HISTORY_FILE'

2.定期備份移動歷史命令,把如下腳本寫到任務計划里。每隔一周執行一次

30 10 * * 6 /bin/bash /var/log/Command/history.sh > /dev/null 2>&1

  

Time=`date +%Y%m%d%H`
logs_path="/var/log/Command/"
logs_name="Command.log"
new_file="$logs_path$logs_name-$Time"
old_file=`find $logs_path -mtime +30 -type f -name "Command.*"`
chattr -a $logs_path$logs_name
mv $logs_path$logs_name $new_file
chattr +a $new_file
touch $logs_path$logs_name
chown -R nobody:nobody $logs_path$logs_name
chmod -R 002 $logs_path$logs_name
chattr +a $logs_path$logs_name
if [ ! -z $old_file ]
then
    echo "delet $old_file $Time" >> /var/log/messages
    chattr -a $old_file
    rm -rf $old_file
fi

3.檢查/var/log/Command/Command.log的中的日志

 

 

二、使用審計工具,splunk或者eak。

完成步驟一基本就完成了審計功能,如果想要圖形化的搜索界面。可以使用步驟二。

方法1.使用splunk

(1).下載splunkforwarder

splunkforwarder類似於zabbix_agent,logstash,filebeat之類的client端。

splunk類似於zabbix_server之類的server端。

splunkforwarder不好找,去splunk官網下載時候。注意右下角有一個old release。選擇自己有lincense的版本。

 

 (2)安裝splunkforwarder

   rpm -ivh splunkforwarder-版本號.rpm
   cd /opt/splunkforwarder/bin
   ./splunk start
   ./splunk add forward-server 192.168.103.31:9997
   ./splunk set deploy-poll 192.168.103.31:8089

   ./splunk add monitor /var/log/lastlog -index secure
   ./splunk add monitor /var/log/lastlog -index secure           #添加索引位置和目錄
   ./splunk add monitor /var/log/lastlog -index secure
   ./splunk add monitor /tmp/wtmp -index secure
   ./splunk add monitor /var/log/Command/Command.log -index history
   ./splunk restart

(3)安裝splunk

   方法同上,適當變通一下包名。啟動web功能。

(4)查看圖形

   命令審計

 

   

     登錄審計(里面的路徑和上面寫的/var/log/Command/Command.log位置不一致,圖片截圖比較早。以前的圖片)

    

 方法2.使用eak,elasticsearch,auditbeat,kibana套裝

  (1)安裝過程不想寫了,百度能搜一堆。大致過程是搜索elk官網,進去下載,選擇rpm包。下載完rpm安裝。或者直接yum install auditbeat

  (2)修改配置文件,添加審計目錄    

auditbeat.modules:
- module: auditd
  audit_rule_files: [ '${path.config}/audit.rules.d/*.conf' ]
  audit_rules: |
    -a always,exit -F arch=b64 -S execve,execveat -k exec
    -w /etc/group -p wa -k identity
    -w /etc/passwd -p wa -k identity
    -w /etc/gshadow -p wa -k identity
    -a always,exit -F arch=b64 -S open,creat,truncate,ftruncate,openat,open_by_handle_at -F exit=-EACCES -k access
    -a always,exit -F arch=b64 -S open,creat,truncate,ftruncate,openat,open_by_handle_at -F exit=-EPERM -k access
- module: file_integrity
  paths:
  - /bin
  - /usr/bin
  - /sbin
  - /usr/sbin
  - /etc
  exclude_files:
  - '(?i)\.sw[nop]$'
  - '~$'
  - '/\.git($|/)'
  scan_rate_per_sec: 50 MiB
  max_file_size: 100 MiB
  recursive: false
- module: system
  datasets:
- module: system
  datasets: 
  period: 30m                  #多久上報一次數據
  state.period: 12h          
  process.hash.scan_rate_per_sec: 50 MiB
  process.hash.max_file_size: 100 MiB
  user.detect_password_changes: true
  login.wtmp_file_pattern: /var/log/wtmp*
  login.btmp_file_pattern: /var/log/btmp*
setup.template.settings:
  index.number_of_shards: 1
setup.template.name: "auditbeat-hxtpay"
setup.template.pattern: "auditbeat-hxtpay-*"
setup.template.enabled: true
setup.ilm.rollover_alias: "auditbeat-hxtpay"
setup.dashboards.directory: /usr/share/auditbeat/kibana/ #導入模板
setup.dashboards.index: auditbeat-*                #默認索引
setup.kibana:
output.elasticsearch:
hosts: ["你的elasticsearch服務器ip:9200"]
username: "elastic"
password: "你的密碼,沒設置賬號密碼的直接注釋這兩行"
processors:
- add_host_metadata: ~
- add_cloud_metadata: ~
- add_docker_metadata: ~

 (3)添加auditeat審計模板到kibana里

/usr/share/auditbeat/bin/auditbeat -c /etc/auditbeat/auditbeat.yml setup --dashboards

   (4)查看kibana的dashboard

文件審計

 

 

 登錄審計

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM