面试题答案
一键面试实现步骤
1. 限制特定用户组内用户在指定目录下的操作
- 创建指定目录
/data/secure
,并设置合适的权限。 - 使用
setfacl
命令来设置访问控制列表,限制特定用户组内用户在该目录下的操作。
2. 防止普通用户修改系统关键配置文件权限
- 使用
chattr
命令给关键配置文件(如/etc/passwd
)添加i
属性,防止普通用户修改权限。 - 可以编写一个脚本来定期检查这些文件的
i
属性是否被移除,并重新设置。
3. 定期检查可执行文件权限
- 使用
find
命令查找系统中所有可执行文件。 - 使用
stat
命令获取文件权限信息,并与安全标准进行对比,不符合的进行调整。 - 使用
crontab
来设置每周一凌晨2点执行该检查脚本。
Bash脚本
1. 限制特定用户组在指定目录操作的脚本
#!/bin/bash
# 创建指定目录
mkdir -p /data/secure
# 设置目录权限
chmod 770 /data/secure
# 设置访问控制列表,允许特定用户组创建文件,但限制删除和重命名
# 假设特定用户组为 'securegroup'
setfacl -R -m g:securegroup:rwx /data/secure
setfacl -R -m d:g:securegroup:rwx /data/secure
2. 防止普通用户修改关键配置文件权限的脚本
#!/bin/bash
# 关键配置文件列表
files=("/etc/passwd" "/etc/shadow" "/etc/group")
for file in "${files[@]}"; do
# 检查并设置i属性
if [ ! -L "$file" ]; then
chattr +i "$file"
fi
done
3. 定期检查可执行文件权限的脚本
#!/bin/bash
# 查找所有可执行文件
find / -type f -executable -print0 | while read -d $'\0' file; do
# 获取文件权限
perms=$(stat -c "%a" "$file")
# 提取所有者、用户组和其他用户的权限
owner_perms=${perms:0:1}
group_perms=${perms:1:1}
other_perms=${perms:2:1}
# 检查权限是否符合标准
if [ $owner_perms -lt 1 ] || [ $group_perms -gt 0 ] || [ $other_perms -gt 0 ]; then
# 调整权限
chmod u+x,g-x,o-x "$file"
fi
done
设置定时任务
- 编辑
root
用户的crontab
:
crontab -e
- 添加以下内容,设置每周一凌晨2点执行可执行文件权限检查脚本:
0 2 * * 1 /path/to/check_executable_permissions.sh
同时,为了确保防止普通用户修改关键配置文件权限的脚本定期运行,可以添加类似的定时任务,例如每天凌晨1点运行:
0 1 * * * /path/to/protect_key_files.sh