不需要任何编译工具链,不需要安装额外软件。只要你的内核是 ANCK 6.6 且启用了 Lua-LSM,就可以开始。
示例:只允许 /etc/shadow 文件被访问三次
local kernel = require('kernel')localfunctionlog(fmt, ...)local s = string.format('shadow_protect: ' .. fmt, ...) kernel.printk(s)endlocal functionfile_post_open(file, mask)local path = file:path()if path == "/etc/shadow"thenlocal n = shared.dogs:incr('count')if n > 3 thenlog('%s %d Read deny, %s', current:comm(), n, path)return falseendendendreturn { name = "shadow_protect", file_post_open = file_post_open}
cat shadow_protect.lua > /sys/kernel/security/lua/register
此时对/etc/shadow 的前三次访问会被允许,从第四次开始的访问将被拒绝,返回 Operation not permitted。
小程序卸载:
echo 'shadow_protect' > /sys/kernel/security/lua/unregister
小程序功能
这个示例在 file_post_open 钩子上挂载了一段逻辑:当任何用户任何进程尝试打开包含 /etc/shadow 路径的文件时,会维持一个计数,当计数大于 3 时,拒绝打开操作,即使 root 用户也受该策略控制。
架构一览