Skip to content

Implementing Sensitive File Change Detection

This guide demonstrates how to use Scheck to check sensitive files with a Lua script.

  • Version: 1.0.7-7-g251eead
  • Release Date: 2023-04-06 11:17:57
  • Supported OS: linux/arm, linux/arm64, linux/386, linux/amd64

Prerequisites

  • Scheck is already installed.

Development Steps

  1. Go to the installation directory and edit the configuration file scheck.conf. Set the enable field to true:
...
[scoutput]
   # ## Security check process messages can be sent to local, HTTP, or Alibaba Cloud SLS.
   # ## Remote server, e.g., http(s)://your.url
  [scoutput.http]
    enable = true
    output = "http://127.0.0.1:9529/v1/write/security"
  [scoutput.log]
    # ## Can be configured for local storage
    enable = false
    output = "/var/log/scheck/event.log"
...
  1. In the directory /usr/local/scheck/custom.rules.d (the directory for custom user scripts), create a new manifest file files.manifest with the following content:
id       = 'check-file'
category = 'system'
level    = 'warn'
title    = 'Monitor file changes'
desc     = 'File {{.File}} has changed'
cron     = '*/10 * * * *' # Execute the Lua script every 10 seconds
os_arch  = ["Linux"]
  1. In the same directory as the manifest file, create a new script file files.lua with the following content:
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
  1. When a sensitive file is modified, the next 10-second interval will detect the change and trigger the trigger function, which sends the event to the file /var/log/scheck/event.log. A new line is appended, for example:
check-file-01,category=security,level=warn,title=Monitor file changes message="File /etc/passwd has changed" 1617262230001916515

Feedback

Is this page helpful?