コンテンツにスキップ

システムユーザー変更の監視

ここでは、Scheck を使用して機密ファイルをチェックする Lua スクリプトの実装方法を紹介します。

  • バージョン:1.0.7-7-g251eead
  • リリース日:2023-04-06 11:17:57
  • 対応OS:linux/arm, linux/arm64, linux/386, linux/amd64

前提条件

開発手順

  1. インストールディレクトリに移動し、設定ファイル scheck.confenable フィールドを 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"
...
  1. ディレクトリ /usr/local/scheck/custom.rules.d(ユーザー定義スクリプト用ディレクトリ)に、マニフェストファイル files.manifest を作成し、次のように編集します。
id         = 'users-checker'
category   = 'system'
level      = 'warn'
title      = 'システムユーザー変更の監視'
desc       = '{{.Content}}'
cron       = '*/10 * * * *'
instanceId = 'id-xxx'
os_arch    = ["Linux"]
  1. マニフェストファイルと同じディレクトリに、スクリプトファイル users.lua を作成し、次のように編集します。
local function check()
    local cache_key="current_users"
    local currents=users()

    local old=get_cache(cache_key)
    if not old then
        set_cache(cache_key, currents)
        return
    end

    local adds={}
    for i,v in ipairs(currents) do
        local exist=false
        for ii,vv in ipairs(old) do
            if vv["username"] == v["username"] then
                exist = true
                break
            end
        end
        if not exist then
            table.insert(adds, v["username"])
        end
    end

    local dels={}
    for i,v in ipairs(old) do
        local exist=false
        for ii,vv in ipairs(currents) do
            if vv["username"] == v["username"] then
                exist = true
                break
            end
        end
        if not exist then
            table.insert(dels, v["username"])
        end
    end

    local content=''
    if #adds > 0 then
        content=content..'新規ユーザー: '..table.concat(adds, ',')
    end
    if #dels > 0 then
        if content ~= '' then content=content..'; ' end
        content=content..'削除されたユーザー: '..table.concat(dels, ',')
    end
    if content ~= '' then
        trigger({Content=content})
        set_cache(cache_key, currents)
    end
end

check()
  1. ユーザーが追加された場合、次の 10 秒後に検出され、trigger 関数が呼び出されてイベントがファイル /var/log/scheck/event.log に送信されます。例として、次のような 1 行のデータが追加されます。
users-checker,category=system,level=warn,title=システムユーザー変更の監視 message="新規ユーザー: xxx" 1617262230001916515

フィードバック

このページは役に立ちましたか?