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¶
- Go to the installation directory and edit the configuration file
scheck.conf. Set theenablefield totrue:
...
[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"
...
- In the directory
/usr/local/scheck/custom.rules.d(the directory for custom user scripts), create a new manifest filefiles.manifestwith 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"]
- In the same directory as the manifest file, create a new script file
files.luawith 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
- When a sensitive file is modified, the next 10-second interval will detect the change and trigger the
triggerfunction, which sends the event to the file/var/log/scheck/event.log. A new line is appended, for example: