Cấu hình Auditd xuất log ra Syslog và Remote Server
Bước đầu tiên là chuyển đổi luồng dữ liệu từ Auditd sang định dạng syslog để các công cụ thu thập log (log forwarders) có thể đọc dễ dàng hơn, đồng thời thiết lập gửi log về một máy chủ tập trung.
Tại sao: Mặc định Auditd ghi log trực tiếp vào file binary (`.log`) tại `/var/log/audit/`, điều này gây khó khăn cho việc parse thời gian thực. Chuyển sang syslog giúp tận dụng cơ sở hạ tầng messaging đã có sẵn và giảm tải I/O disk cho server database.
Kết quả mong đợi: Auditd sẽ xuất hiện trong log file `/var/log/messages` hoặc `/var/log/syslog` và đồng bộ về remote server qua UDP/TCP.
Trước tiên, chỉnh sửa file cấu hình chính của Auditd để bật chế độ syslog và định dạng JSON.
sudo nano /etc/audit/auditd.conf
Sửa các dòng sau trong file (thêm hoặc sửa):
log_format = ENRICHED
log_file = /var/log/audit/audit.log
log_group = audit
log_path = /var/log/audit/
log_file_name = audit.log
log_file_max = 50
num_logs = 5
max_log_file_action = ROTATE
flush = INCREMENTAL_ASYNC
action_mail_acct = root
mail_prefix = Audit:
space_left = 75
space_left_action = EMAIL
admin_space_left = 50
admin_space_left_action = SUSPEND
disk_full_action = SUSPEND
disk_error_action = SUSPEND
log_group = audit
log_file = /var/log/audit/audit.log
log_format = ENRICHED
syslog_output = yes
syslog_log_format = JSON
Khởi động lại dịch vụ auditd để áp dụng thay đổi.
sudo systemctl restart auditd
Kiểm tra xem log có xuất hiện trong syslog không bằng lệnh watch.
sudo tail -f /var/log/syslog | grep "audit"
Để gửi log về remote server (giả sử IP là 192.168.1.100, port 514 UDP), cấu hình rsyslog để nhận log từ auditd và forward đi.
sudo nano /etc/rsyslog.d/50-audit-forward.conf
Nhập nội dung hoàn chỉnh cho file forward log:
# Forward Audit logs to remote SIEM server
:msg, contains, "audit" {
&usertmp/audit.log
@@192.168.1.100:514;RSYSLOG_SyslogProtocol23Format
stop
}
Khởi động lại rsyslog và kiểm tra quyền sở hữu file.
sudo systemctl restart rsyslog
Verify kết quả: Tạo một truy cập giả vào file database và xem log có lên remote server không (cần kiểm tra trên server đích).
sudo auditctl -w /var/lib/mysql -p rwxa -k mysql_access
sudo tail -f /var/log/syslog | grep "mysql_access"
Sử dụng bpftrace để parse và chuyển đổi dữ liệu eBPF sang JSON
Sau khi có audit log, ta cần xử lý dữ liệu từ các chương trình eBPF (từ Phần 4 và 5) để chuyển đổi thành định dạng JSON chuẩn cho SIEM.
Tại sao: Dữ liệu thô từ eBPF thường là các số nguyên hoặc cấu trúc C phức tạp. bpftrace có khả năng truy xuất và định dạng lại dữ liệu này thành JSON ngay tại biên (edge) trước khi gửi đi, giúp SIEM dễ dàng index và query.
Kết quả mong đợi: Chạy script bpftrace sẽ in ra dòng log JSON chứa thông tin về hành vi SQL Injection hoặc truy cập trái phép.
Tạo script bpftrace để bắt sự kiện syscall `connect` hoặc `write` của tiến trình MySQL và xuất ra JSON.
sudo nano /usr/local/bin/ebpf-to-json.bpftrace
Nội dung hoàn chỉnh của script bpftrace:
#!/usr/bin/env bpftrace
// Parse eBPF events from MySQL and output as JSON
tracepoint:syscalls:sys_enter_write {
@pid = pid;
@comm = comm;
@uid = uid;
@fd = args->fd;
@len = args->count;
}
tracepoint:syscalls:sys_exit_write {
if (@pid == pid) {
@comm = comm;
@uid = uid;
@fd = args->fd;
@len = args->ret; // Use return value for actual bytes written
printf("{\"timestamp\": %d, \"type\": \"write\", \"pid\": %d, \"comm\": \"%s\", \"uid\": %d, \"fd\": %d, \"bytes\": %d, \"action\": \"monitor\"}\n",
nsecs / 1000000000, pid, comm, uid, fd, args->ret);
}
}
// Example for SQL injection detection logic (simplified)
// In real scenario, this would parse the buffer content
tracepoint:syscalls:sys_enter_connect {
@comm = comm;
@uid = uid;
if (args->family == 2) { // AF_INET
printf("{\"timestamp\": %d, \"type\": \"connect\", \"pid\": %d, \"comm\": \"%s\", \"uid\": %d, \"src_ip\": \"127.0.0.1\", \"dst_port\": %d, \"action\": \"alert\"}\n",
nsecs / 1000000000, pid, comm, uid, args->sport);
}
}
Cấp quyền thực thi cho script.
sudo chmod +x /usr/local/bin/ebpf-to-json.bpftrace
Chạy script để test đầu ra (chạy trong terminal riêng).
sudo /usr/local/bin/ebpf-to-json.bpftrace
Để chạy dưới dạng daemon và ghi log vào file, sử dụng pipe và redirect.
sudo /usr/local/bin/ebpf-to-json.bpftrace | tee /var/log/ebpf-monitor.json &
Verify kết quả: Tạo một request vào database và kiểm tra file log JSON.
mysql -u root -p -e "SELECT VERSION();"
cat /var/log/ebpf-monitor.json | grep "mysql"
Triển khai agent thu thập log (Filebeat) cho eBPF/Audit
Bây giờ ta sẽ cấu hình Filebeat để thu thập cả log từ Auditd (qua syslog) và log JSON từ bpftrace, sau đó gửi về Elastic SIEM.
Tại sao: Filebeat là lightweight agent của Elastic, tối ưu cho việc thu thập log từ nhiều nguồn, hỗ trợ lọc (grok) và gửi data qua Kafka hoặc trực tiếp lên Elasticsearch.
Kết quả mong đợi: Filebeat chạy ổn định, không bị lỗi parse, và dữ liệu từ cả Audit và eBPF đều xuất hiện trong index của Elasticsearch.
Cài đặt Filebeat (ví dụ trên Ubuntu/Debian).
sudo apt-get install filebeat
Cấu hình file chính của Filebeat để đọc từ 2 nguồn: syslog và file json eBPF.
sudo nano /etc/filebeat/filebeat.yml
Nội dung hoàn chỉnh cấu hình filebeat.yml:
filebeat.inputs:
- type: syslog
protocol: udp
host: 0.0.0.0:514
enabled: true
fields_under_root: true
fields:
source: audit_syslog
- type: log
enabled: true
paths:
- /var/log/ebpf-monitor.json
fields_under_root: true
fields:
source: ebpf_bpftrace
# Output to Elasticsearch
output.elasticsearch:
hosts: ["http://192.168.1.100:9200"]
username: "elastic"
password: "your_secure_password"
indices:
- index: "security-audit-%{+yyyy.MM.dd}"
setup.template.name: "security-audit"
setup.template.path: "/etc/filebeat/audit-template.json"
# Logging
logging.level: info
logging.to_files: true
logging.files:
path: /var/log/filebeat
name: filebeat
keepfiles: 7
permissions: 0600
Tạo template cho Elasticsearch để đảm bảo mapping đúng cho JSON eBPF.
sudo nano /etc/filebeat/audit-template.json
Nội dung template JSON:
{
"index_patterns": ["security-audit-*"],
"template": {
"settings": {
"number_of_shards": 1
},
"mappings": {
"properties": {
"timestamp": { "type": "date" },
"type": { "type": "keyword" },
"pid": { "type": "integer" },
"comm": { "type": "keyword" },
"uid": { "type": "integer" },
"action": { "type": "keyword" },
"source": { "type": "keyword" },
"bytes": { "type": "integer" },
"dst_port": { "type": "integer" }
}
}
}
}
Khởi động Filebeat với chế độ setup template.
sudo filebeat setup -e
Khởi động dịch vụ Filebeat.
sudo systemctl enable filebeat
sudo systemctl start filebeat
Verify kết quả: Kiểm tra log của Filebeat để đảm bảo không có lỗi parse.
sudo journalctl -u filebeat -f
Kiểm tra trên Elasticsearch xem data có về không (dùng curl).
curl -u elastic:your_secure_password 'http://192.168.1.100:9200/security-audit-*/_search?pretty' | grep "hits"
Kết nối với SIEM (Elastic) để tạo Dashboard cảnh báo
Sau khi dữ liệu đã vào Elasticsearch, ta cần tạo Dashboard trong Kibana để trực quan hóa các cuộc tấn công.
Tại sao: Dashboard giúp đội vận hành (Ops) và bảo mật (Sec) nhìn thấy bức tranh toàn cảnh, xác định xu hướng tấn công và phản ứng nhanh.
Kết quả mong đợi: Có một Dashboard trong Kibana hiển thị biểu đồ số lượng cảnh báo theo thời gian, phân loại theo loại tấn công (SQL Injection, Unauthorized Access).
Mở Kibana (http://192.168.1.100:5601) và tạo Index Pattern mới.
Truy cập vào Stack Management -> Index Patterns.
# Không cần command, thao tác trên UI
# Tạo Index Pattern với pattern: security-audit-*
# Chọn time field: @timestamp hoặc timestamp
Tạo Visualization đầu tiên: Số lượng cảnh báo theo thời gian.
Vào Discover -> Create Visualization -> Line Chart.
# Thao tác trên UI:
# X-axis: timestamp
# Y-axis: Count of documents
# Filter: action = "alert"
Tạo Visualization thứ hai: Top các User ID (UID) tấn công nhiều nhất.
Vào Create Visualization -> Pie Chart.
# Thao tác trên UI:
# Split Slices: uid
# Filter: action = "alert" AND type = "connect"
Tạo Dashboard mới để gộp các Visualization.
Vào Create Dashboard, kéo thả 2 biểu đồ vừa tạo vào.
Đặt tên Dashboard là "Database Security Monitor".
Thiết lập quy tắc cảnh báo tự động (Alerting Rules)
Bước cuối cùng là cấu hình Alerting trong Kibana để gửi thông báo ngay lập tức khi phát hiện hành vi tấn công.
Tại sao: Dashboard chỉ giúp con người nhìn thấy sự cố, còn Alerting giúp hệ thống tự động gửi cảnh báo qua Email, Slack hoặc Webhook khi có sự cố nghiêm trọng, giúp giảm thời gian phản ứng (MTTR).
Kết quả mong đợi: Khi có một truy cập trái phép hoặc SQL Injection, hệ thống gửi email cảnh báo cho admin.
Cấu hình Alerting Rule trong Kibana (Security Solution hoặc Stack Monitoring).
Vào Stack Management -> Alerting -> Create rule.
Chọn Rule Type: Elasticsearch (Threshold).
Cấu hình Rule với các thông số sau (giả lập bằng cấu hình JSON hoặc mô tả chi tiết UI):
{
"name": "High Severity SQL Injection Detection",
"rule_type_id": "alerting-es-threshold",
"interval": "1m",
"author": "admin",
"consumer": "alerting",
"params": {
"index": "security-audit-*",
"timeField": "@timestamp",
"timeWindowSize": 5,
"timeWindowUnit": "m",
"threshold": 1,
"thresholdOperator": "gt",
"query": "{\"query\": {\"bool\": {\"must\": [{\"term\": {\"action\": \"alert\"}}, {\"term\": {\"type\": \"write\"}}]}}}",
"message": "Cảnh báo: Phát hiện hành vi SQL Injection từ PID $pid, UID $uid tại $timestamp",
"notifyWhen": "onActiveAlert"
}
}
Cấu hình Action để gửi email.
Vào Stack Management -> Alerting -> Connectors.
# Tạo Connector mới loại Email:
# Name: Security Alert Email
# Type: email
# Email address: admin@company.com
# SMTP settings: cấu hình server mail của bạn
Gắn Connector vào Rule vừa tạo.
Verify kết quả: Thực hiện một truy cập giả mạo (như bước verify ở phần 2) và chờ 1 phút.
mysql -u root -p -e "SELECT * FROM users WHERE id = 1 OR '1'='1';"
# Kiểm tra hộp thư email của admin để xem có nhận được cảnh báo không.
Kiểm tra trạng thái alert trong Kibana.
# Vào Stack Management -> Alerting -> Rules
# Tìm rule "High Severity SQL Injection Detection"
# Kiểm tra tab "Recent Alerts" xem có alert mới nhất không.
Điều hướng series:
Mục lục: Series: Triển khai Database chống tấn công với Linux eBPF và Kernel Audit
« Phần 5: Triển khai eBPF để phát hiện và chặn SQL Injection
Phần 7: Tối ưu hóa hiệu năng và xử lý sự cố (Troubleshooting) »