[步骤] Linux 软硬链接保护的开启 (防止通过软硬链接执行无权限执行的文件)

步骤一:开启软硬链接保护

1.1 修改 /etc/sysctl.conf 文件

# vim /etc/sysctl.conf

添加以下内容:

......
fs.protected_hardlinks = 1
fs.protected_symlinks = 1

1.2 设置系统正在运行的 fs.protected_symlinks 参数和 fs.protected_hardlinks 参数

# sysctl -w fs.protected_symlinks = 1
# sysctl -w fs.protected_hardlinks = 1

步骤二:显示软硬链接保护的开启状态

2.1 显示 /etc/sysctl.conf 文件里的 fs.protected_symlinks 参数和 fs.protected_hardlinks 参数

# grep "fs\.protected_hardlinks" /etc/sysctl.conf /etc/sysctl.d/*
fs.protected_hardlinks = 1
# grep "fs\.protected_symlinks" /etc/sysctl.conf /etc/sysctl.d/*
fs.protected_symlinks = 1

2.2 显示系统正在运行的 fs.protected_symlinks 参数和 fs.protected_hardlinks 参数

# sysctl fs.protected_symlinks
fs.protected_symlinks = 1
# sysctl fs.protected_hardlinks
fs.protected_hardlinks = 1

[步骤] Linux 加密压缩 (tar 版)

步骤一:创建测试文件

# touch test.txt

(补充:这里以创建 test.txt 文件为例)

步骤二:加密压缩文件或目录

2.1 交互式加密压缩文件或目录

# tar -zcf - test.txt | openssl des3 -salt | dd of=test1.tar.gz
enter des-ede3-cbc encryption password:
Verifying - enter des-ede3-cbc encryption password:
*** WARNING : deprecated key derivation used.
Using -iter or -pbkdf2 would be better.
0+1 records in
0+1 records out
224 bytes copied, 7.04902 s, 0.0 kB/s


补充:
1) 这里以将 test.txt 文件加密压缩成 test1.tar.gz (压缩)包为例
2) 如果要以 bzip2 的格式进行压缩,则将命令中的 -zcf 换成 -jcvf 将 test1.tar.gz 换成 test1.tar.bz2
3) 如果要以 xz 的格式进行压缩,则将命令中的 -zcf 换成 -Jcvf 将 test1.tar.gz 换成 test1.tar.xz

2.2 非交互式加密压缩文件或目录

# tar -zcf - test.txt | openssl des3 -salt -f eternalcenter | dd of=test2.tar.gz
des3: Unrecognized flag f
des3: Use -help for summary.
0+0 records in
0+0 records out
0 bytes copied, 0.00376576 s, 0.0 kB/s


补充:
1) 这里以将 test.txt 文件加密压缩成 test1.tar.gz (压缩)包并且将密码设置为 eternalcenter 为例
2) 如果要以 bzip2 的格式进行压缩,则将命令中的 -zcf 换成 -jcvf 将 test1.tar.gz 换成 test2.tar.bz2
3) 如果要以 xz 的格式进行压缩,则将命令中的 -zcf 换成 -Jcvf 将 test1.tar.gz 换成 test2.tar.xz

步骤三:解压加密文件或目录

3.1 交互式解压加密文件或目录

3.1.1 删除原测试目录和里面的文件
# rm -rf test.txt

(补充:这里以删除 test.txt 文件为例)

3.1.2 交互式解压加密文件或目录
# dd if=test2.tar.gz | openssl des3 -d | tar zxf -
0+1 records in
0+1 records out
224 bytes copied, 0.000589721 s, 380 kB/s
enter des-ede3-cbc decryption password:
*** WARNING : deprecated key derivation used.
Using -iter or -pbkdf2 would be better.


补充:
1) 这里以解压 test2.tar.gz (压缩)包为例
2) 如果是 bzip2 格式的(压缩)包,则将命令中的 -zxf 换成 -jcvf 将 test1.tar.gz 换成 test1.tar.bz2
3) 如果是 xz 格式的(压缩)包,则将命令中的 -zxf 换成 -Jcvf 将 test1.tar.gz 换成 test1.tar.xz

3.2 非交互式解压加密文件或目录

3.2.1 删除原测试目录和里面的文件
# rm -rf test.txt

(补充:这里以删除 test.txt 文件为例)

3.2.2 非交互式解压加密文件或目录
# dd if=test1.tar.gz | openssl des3 -d -k eternalcenter | tar zxf -
0+1 records in
0+1 records out
224 bytes copied, 0.000574539 s, 390 kB/s
*** WARNING : deprecated key derivation used.
Using -iter or -pbkdf2 would be better.


补充:
1) 这里以解压 test1.tar.gz (压缩)包并且解压密码为 eternalcenter 为例
2) 如果是 bzip2 格式的(压缩)包,则将命令中的 -zxf 换成 -jcvf 将 test1.tar.gz 换成 test1.tar.bz2
3) 如果是 xz 格式的(压缩)包,则将命令中的 -zxf 换成 -Jcvf 将 test1.tar.gz 换成 test1.tar.xz

[步骤] Linux 加密压缩 (zip 版)

步骤一:创建测试目录和测试文件

# mkdir test
# touch test/test.txt

(补充:这里以创建 test 目录和里面的 test.txt 文件为例)

步骤二:加密压缩文件或目录

2.1 交互式加密压缩文件或目录

# zip -re test1.zip test
Enter password: 
Verify password: 
  adding: test/ (stored 0%)
  adding: test/test.txt (stored 0%)

(补充:这里以将 test 目录和里面的 test.txt 文件加密压缩成 test1.zip (压缩)包为例)

2.2 非交互式加密解压文件或目录

# zip -rP eternalcenter test2.zip test
  adding: test/ (stored 0%)
  adding: test/test.txt (stored 0%)

(补充:这里以将 test 目录和里面的 test.txt 文件加密压缩成 test2.zip (压缩)包并且将密码设置为 eternalcenter 为例)

步骤三:解压加密文件或目录

3.1 交互式解压加密文件或目录

3.1.1 删除原测试目录和里面的文件
# rm -rf test

(补充:这里以删除 test 目录和里面的文件为例)

3.1.2 交互式解压加密文件或目录
# unzip test2.zip
Archive:  test2.zip
   creating: test/
[test2.zip] test/test.txt password: 
 extracting: test/test.txt

(补充:这里以解压 test2.zip (压缩)包为例)

3.2 非交互式解压加密文件或目录

3.2.1 删除原测试目录和里面的文件
# rm -rf test

(补充:这里以删除 test 目录和里面的文件为例)

3.2.2 非交互式解压加密文件
# unzip -P eternalcenter test1.zip 
Archive:  test1.zip
   creating: test/
 extracting: test/test.txt  

(补充:这里以解压 test2.zip (压缩)包并且解压密码为 eternalcenter 为例)

[步骤] Linux rm 命令的监控

步骤一:将原来的 rm 命令进行备份

# cp /usr/bin/rm  /usr/bin/rm.original

步骤二:创建一个记录 rm 命令使用的脚本

# cat /usr/bin/rm
#!/bin/bash
log=/var/log/rm_command.log
echo "The $$ is calling rm command" >> $log
echo "The full command is $*" >> $log
echo
echo "now use this command to get more information: /bin/ps axwwo user,pid,ppid,%cpu,%mem,vsz,rss,stat,time,cmd" >>$log
/bin/ps axwwo user,pid,ppid,%cpu,%mem,vsz,rss,stat,time,cmd >>$log
/usr/bin/rm.original $*
echo "============================================================" >>$log

步骤三:给记录 rm 命令使用的脚本执行权限

# chmod 755 /usr/bin/rm.original

步骤四:下次使用 rm 命令后就可以监控 /var/log/rm_command.log 日志了

(步骤略)

[步骤] SELinux 的启用 (openSUSE & SLE 版) (不建议)

软件准备:

在 SELinuxProject 的官网上下载 SELinux 策略 UseRefpolicy:

https://github.com/SELinuxProject/refpolicy/wiki/UseRefpolicy

注意:

1) 如果使用此文的方法将 openSUSE & SLE 的 SELinux 设置为 Enforcing 则系统将无法设置 IP 地址
2) 如果使用此文的方法开启了 SELinux 并且将所有的布尔(boolean)值开启,则系统将无法关机,开启所有布尔值的方法:# for i in semanage boolean -l | awk '{print $1}'; do echo $i;setsebool -P $i 1; done

正文:

步骤一:安装 SELinux 组件

# zypper in libselinux1 libsemanage1 libsepol-devel libsepol1 libselinux-devel mcstrans libselinux1-32bit policycoreutils checkpolicy libsemanage-devel setools-tcl setools-libs setools-java setools-devel setools-console selinux-tools python3-policycoreutils python3-selinux python3-semanage python3-setools restorecond

步骤二:安装 SELinux 策略

2.1 解压包含 SELinux 策略的压缩包

# tar -xvf refpolicy-2.20210203.tar.bz2

(补充:这里以解压 refpolicy-2.20210203.tar.bz2 压缩包为例)

2.2 将 SELinux 策略移动到 SELinux 配置文件的位置

# mv refpolicy /etc/selinux/

2.3 进入到和 SELinux 策略相同目录下

# cd /etc/selinux/refpolicy/

2.4 显示 SELinux 策略的安装手册

# cat INSTALL

2.5 创建 SELinux 策略的配置文件

# make conf

2.6 创建 SELinux 策略

# make policy

2.7 编译 SELinux 策略

# make install

2.8 安装 SELinux 策略

# make load

步骤三:配置 SELinux 配置文件

3.1 在 SELinux 配置文件中将 SELinux 设置为 Permissive 状态

# vim /etc/selinux/config

创建以下内容:

# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=permissive
# SELINUXTYPE= can take one of three values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected.
#     mls - Multi Level Security protection.
SELINUXTYPE=refpolicy

3.2 在系统内核中禁止使用 AppArmor 使用 SELinux 并且将 SELinux 状态设置为 Permissive

3.2.1 设置 /etc/default/grub 配置文件
# vim /etc/default/grub

在这一行里:

GRUB_CMDLINE_LINUX_DEFAULT="......"
添加以下内容:
GRUB_CMDLINE_LINUX_DEFAULT="...... security=selinux selinux=1 enforcing=0"
3.2.2 让刚刚设置的 /etc/default/grub 配置文件生效
# grub2-mkconfig -o /boot/grub2/grub.cfg

3.3 刷新系统内所有文件的标签

# restorecon -Rp /

步骤四:重启系统让 SELinux 生效

# reboot

参考文献:

https://documentation.suse.com/sles/15-SP2/html/SLES-all/cha-selinux.html