機密ファイルの変更検出の実装¶
ここでは、Scheckを使用して機密ファイルの変更をチェックするLuaスクリプトの実装方法を紹介します。
- バージョン:1.0.7-7-g251eead
- リリース日:2023-04-06 11:17:57
- 対応OS:linux/arm,linux/arm64,linux/386,linux/amd64
前提条件¶
- Scheckがインストールされていること
開発手順¶
- インストールディレクトリに移動し、設定ファイル
scheck.confのenableフィールドをtrueに設定します:
...
[scoutput]
# ##セキュリティチェック中に生成されたメッセージは、ローカル、HTTP、Alibaba Cloud SLSに送信できます。
# ##リモートサーバー、例:http(s)://your.url
[scoutput.http]
enable = true
output = "http://127.0.0.1:9529/v1/write/security"
[scoutput.log]
# ##ローカルストレージを設定できます
enable = false
output = "/var/log/scheck/event.log"
...
- ディレクトリ
/usr/local/scheck/custom.rules.d(このディレクトリはユーザー定義スクリプト用です)にマニフェストファイルfiles.manifestを作成し、次のように編集します:
id = 'check-file'
category = 'system'
level = 'warn'
title = '监视文件变动'
desc = '文件 {{.File}} 发生了变化'
cron = '*/10 * * * *' #このLuaスクリプトを10秒ごとに実行することを示します
os_arch = ["Linux"]
- マニフェストファイルと同じディレクトリにスクリプトファイル
files.luaを作成し、次のように編集します:
local files={
'/etc/passwd',
'/etc/group'
}
local function check(file)
local cache_key=file
local hashval = file_hash(file)
local old = get_cache(cache_key)
if not old then
set_cache(cache_key, hashval)
return
end
if old ~= hashval then
trigger({File=file})
set_cache(cache_key, hashval)
end
end
for i,v in ipairs(files) do
check(v)
end
- 機密ファイルが変更されると、次の10秒以内に検出され、trigger関数が呼び出され、イベントがファイル
/var/log/scheck/event.logに送信されます。1行のデータが追加されます。例: