Lesson 05: Linux 筆記 - Log Server, MariaDB, nmap
Log Server 實作
MariaDB 權限與加密, nmap 檢測工具介紹
User ID Report
aureport -u -i --failed | less
1.Write message to syslog
BIOS/UEF ➡ grub2 ➡ kernel ➡ systemd ➡ journalctl
rsyslog
: 如果選 minimal installation,err不會由rsyslog接,改由journalctl
接error接收
將訊息寫入系統紀錄中
@Server
|
|
|
|
|
|
|
|
|
|
將journalctl
紀錄持久化
推書時間: 實戰Linux系統數位鑑識 (江湖海) (Practical Linux Forensics: A guide for digital investigators, by Bruce Nikkel)
|
|
尋找 “hello World”,會發現找不到
|
|
找得到 hello World
|
|
|
|
|
|
|
|
|
|
找出"hello World"
自訂記錄等級與標籤
@Server
在紀錄中設定class
Tag
|
|
root# grep class /var/log/messages
root# journalctl -t class
在紀錄中,設定嚴重等級
root# logger -t class -p 'user.info' "Test log messages"
root# grep class /var/log/messages
使用journalctl
找出tag為class
的記錄
|
|
(screen1)
使用 journalctl
找出tag為class
且嚴重性為’err’的記錄
|
|
(screen2)
2. 建立簡單 Rsyslog Server
安裝 rsyslog 套件
@Desktop, @Server
|
|
@Desktop
|
|
設定接收 rsyslog
資訊
|
|
module(load="imudp") # needs to be done just once
input(type="imudp" port="514")
module(load="imtcp") # needs to be done just once
input(type="imtcp"
port="514")
|
|
|
|
@Server 設定 sysrlog client
|
|
設定將 log 使用 TCP 協定傳送到 Desktop
root# vi client.conf
*.* @@172.16.1.X:514
(screenshot3)
|
|
|
|
@Desktop, @Server
|
|
sudo su -/-i
跟sudo su
的差別:少了減號就不會登入(真正變成)那個user DigitalOcean: How To Use Journalctl to View and Manipulate Systemd Logs NXLog
3. Syslog 應用
建主 syslog Server
@Desktop
|
|
|
|
|
|
module(load="imudp") # needs to be done just once
input(type="imudp" port="514")
module(load="imtcp") # needs to be done just once
input(type="imtcp" port="514")
$template RemoteLogs,"/var/log/%FROMHOST-IP%/%HOSTNAME%-%$NOW%/%PROGRAMNAME%.log"
*.* ?RemoteLogs
Ref: https://www.rsyslog.com/doc/master/configuration/properties.html
|
|
|
|
設定用戶端 Client
@Server
|
|
|
|
|
|
#*.* @172.16.1.58:514 # Use @ for UDP protocol
*.* @@172.16.1.58:514 # Use @@ for TCP protocol
& ~
|
|
測試
@Server
|
|
|
|
@Desktop
|
|
讀取指定檔案內容
@Server
|
|
設定 log 檔案,位於 /tmp/applog.txt
root# touch /tmp/applog.txt
root# vi 01_watch_log.conf
module(load="imfile")
input(type="imfile"
File="/tmp/applog.txt"
Tag="mytag:"
Facility="local0")
取消 STOP
|
|
#*.* @172.16.1.X:514 # Use @ for UDP protocol
*.* @@172.16.1.X:514 # Use @@ for TCP protocol
測試寫入檔案到 log
|
|
查看 Log 是否寫入
|
|
💤允許特定網段/IP通過
|
|
firewall-cmd 的小史💥
[firewalld]
↓
[firewall-cmd]
↙↘
(EL7) (EL8/9)
↙ ↘
[iptables] [nftables]
↘ ↙
[kernel+notfilter]
4. MariaDB 帳號管理
MariaDB/MySQL 資料庫實體檔位置 ➡
/var/lib/mysql/
@Desktop
安裝資料庫
|
|
|
|
密碼設為
rocky
連線資料庫
|
|
建立資料庫
|
|
建立使用者
MariaDB> GRANT ALL ON classdb.* TO 'dbuser'@'172.16.2.58' IDENTIFIED BY 'rocky' REQUIRE SSL;
MariaDB> GRANT ALL ON classdb.* TO 'user1'@'172.16.2.58' IDENTIFIED BY 'rocky';
列出帳號與資庫資訊
MariaDB> SELECT User, Db, Host from mysql.db;
+--------+---------+---------------+
| User | Db | Host |
+--------+---------+---------------+
| dbuser | classdb | 172.16.2.58 |
+--------+---------+---------------+
1 row in set (0.004 sec)
列出使用者權限
show grants for 'dbuser'@'172.16.2.58';
+-------------------------------------------------------------------------------------------------------------------------------+
| Grants for [email protected] |
+-------------------------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO `dbuser`@`172.16.2.58` IDENTIFIED BY PASSWORD '*128977E278358FF80A246B5046F51043A2B1FCED' REQUIRE SSL |
| GRANT ALL PRIVILEGES ON `classdb`.* TO `dbuser`@`172.16.2.58` |
+-------------------------------------------------------------------------------------------------------------------------------+
2 rows in set (0.002 sec)
@Server
|
|
|
|
|
|
@Desktop
產生稽核語法
|
|
MariaDB> select distinct concat('SHOW GRANTS FOR ', QUOTE(user), '@', QUOTE(host), ';') as query from mysql.user;
+-------------------------------------------+
| query |
+-------------------------------------------+
| SHOW GRANTS FOR 'root'@'127.0.0.1'; |
| SHOW GRANTS FOR 'dbuser'@'172.16.2.58'; |
| SHOW GRANTS FOR 'root'@'::1'; |
| SHOW GRANTS FOR 'root'@'localhost'; |
+-------------------------------------------+
4 rows in set (0.001 sec)
5. MariaDB packet interception
從 Server 查看封包
Server
@Terminal-1
|
|
|
|
Desktop
|
|
連線資料庫
@Server @Terminal-2
|
|
@Terminal-2
|
|
@Desktop
查看 tcpdump
結果
6. MariaDB SSL連線加密
@Desktop
|
|
Build CA
建立 RootCA
|
|
Common Name 設為
DB CA
Build server certificate
建立 Server 用憑證
|
|
Common Name 設為
DB Server
簽署憑證
|
|
Build client certificate
建立用戶端憑證
|
|
Common Name 設為
DB Client
簽署憑證
root# openssl x509 -req -in client.csr -days 3650 -CA ca.crt -CAkey ca.key -set_serial 01 -out client.crt
驗證
將簽好的憑證複製到相關目錄
|
|
|
|
設定 MariaDB 憑證
@Desktop
|
|
套用 Server 憑證
|
|
重新啟動 MariaDB
|
|
檢查憑證套用成功
|
|
Client 端連線
@Server
|
|
@Desktop
|
|
@Server
|
|
|
|
|
|
|
|
在 [client] 區段中新增
...
[client]
ssl-ca=/etc/my.cnf.d/ssl/ca.crt
ssl-cert=/etc/my.cnf.d/ssl/client.crt
ssl-key=/etc/my.cnf.d/ssl/client.key
root# mysql -u dbuser -h 172.16.1.X -p
MariaDB> status;
--------------
mysql Ver 15.1 Distrib 10.3.28-MariaDB, for Linux (x86_64) using readline 5.1
Connection id: 14
Current database:
Current user: [email protected]
SSL: Cipher in use is TLS_AES_256_GCM_SHA384
Current pager: stdout
Using outfile: ''
Using delimiter: ;
Server: MariaDB
Server version: 10.3.28-MariaDB MariaDB Server
Protocol version: 10
Connection: 172.16.62.5 via TCP/IP
Server characterset: latin1
Db characterset: latin1
Client characterset: utf8
Conn. characterset: utf8
TCP port: 3306
Uptime: 24 min 23 sec
Threads: 6 Questions: 18 Slow queries: 0 Opens: 18 Flush tables: 1 Open tables: 11 Queries per second avg: 0.012
--------------
連線時指定憑證位置
|
|
在 [client] 區段中移除
...
[client]
#ssl-ca=/etc/my.cnf.d/ssl/ca.crt
#ssl-cert=/etc/my.cnf.d/ssl/client.crt
#ssl-key=/etc/my.cnf.d/ssl/client.key
|
|
|
|
參考 5.MariaDB 封包截取 驗證加密後結果。
7. MariaDB 密碼原則
參考文件
Desktop
啟用模組
修改 /etc/my.cnf.d/mariadb-server.cnf,在 [mysqld] 區段中,填入
plugin_load_add = simple_password_check
(screen_shot)
查看規則
@Desktop
|
|
|
|
驗證規則
|
|
8. MariaDB 稽核
參考文件
啟用稽核模組與設定
@Desktop
root# cp -a /etc/my.cnf.d/mariadb-server.cnf{,_backup}
root# vi /etc/my.cnf.d/mariadb-server.cnf
在 mysqld 區段加入
...
plugin_load_add = server_audit
server_audit_logging = ON
server_audit=FORCE_PLUS_PERMANENT
server_audit_events=connect,query
server_audit_file_path=/var/lib/mysql/server_audit.log
|
|
測試
@Server
|
|
@Desktop
|
|
9. Port 掃描
-
掃描 TCP
root# nmap -p- server58
-
掃描 UDP
root# nmap -sU -p- server58
這個掃描會很久
-
猜測作業系統版本
root# nmap -O server58
(…Nmap scan report..)
-
掃描 server58 中開了哪些 1024 以下的 TCP Port,並列出作業系統
1
root# nmap -p0-1024 -O server58
Teleport服務_跳板機
Teleport(https://goteleport.com)