基本防护

系统安全               服务安全                 数据安全         网络安全        抓包和扫描          监控


一、Linux基本防护

  • 设置帐号有效期

chage  -d 0 用户     #强制修改密码

chage -E yyy-mm-dd   用户

chage -E  -1   用户   #取消过期时间

  • 帐号的锁定/解锁

passwd   -l锁定    -u解锁       -S看状态    用户

(密码已被锁定)1.没有给用户设置密码     2.该用户密码已被锁定         :锁定会无法登陆操作系统

  • 强制定期修改密码

配置文件  /etc/login.defs

对新建的用户有效 

  • 主要控制属性

PASS_MAX_DAYS

PASS_MIN_DAYS

PASS_WARN_AGE

  • 伪装登陆提示

配置文件 

/etc/issue

/etc/issue.net

分别适合本地、远程登陆

默认会提示内核、系统等版本信息

  • 程序和服务控制

禁用非必要的系统服务

使用systemctl、chkconfig工具

  • 锁定/解锁保护文件

EXT3、EXT4的文件属性控制

chattr、lsattr

+、-、= 控制方式

属性i:不可变(immutable)

属性a:仅可追加(append  only)

二、用户切换与提权

  • su切换

su -  普通用户   #root切换普通用户不需密码,普通用户切换普通用户需密码

su - root             #普通用户切换到root需密码

su [不加-]   用户    #用户切换里但环境没变,使用时会有问题

su -  -c "命令"   root      #以root身份使用某个命令  ,需密码

日志文件:/var/log/secure

  • sudo提升执行权限

作用:配置系统的普通用户可以执行root用户的命令

主配置文件:/etc/sudoers

修改配置文件:vim或visodu(编辑时不会变色)   /etc/sudoers

提权配置格式

普通用户名  主机名=命令列表(绝对路径)

%用户组名  主机名=命令列表(绝对路径)

[root@host52 ~]# vim /etc/sudoers

## Allow root to run any commands anywhere 
92行 root    ALL=(ALL)       ALL      #所有主机所有命令

## Allows people in group wheel to run all commands
99行 %wheel  ALL=(ALL)       ALL
 

提权实例:


[root@host52 ~]# which systemctl 
/usr/bin/systemctl
[root@host52 ~]# which vim
/usr/bin/vim

[root@host52 ~]# vim /etc/sudoers
 91 ## Allow root to run any commands anywhere 
 92 root    ALL=(ALL)       ALL
 93 dc localhost,host52=/usr/bin/systemctl  * httpd , /usrbin/vim  /etc/httpd/conf/httpd.conf
 94 tc localhost,host52=/usr/bin/systemctl  * mysqld , /usrbin/vim /etc/my.cnf
 95 nb localhost,host52=/usr/bin/systemctl  * httpd , /usr/bin/vim /etc/httpd/conf/httpd.conf , /usr/bin/systemctl  * mysqld , /usr/bin/vim /etc/my.cnf ,  /sbin/* , !/sbin/ifconfig eth0
 96 ## Allows members of the 'sys' group to run networking, software, 
 97 ## service management apps and more.
 98 # %sys ALL = NETWORKING, SOFTWARE, SERVICES, STORAGE, DELEGATING, PROCESSES, LOCATE, DRIVERS
 99 
100 ## Allows people in group wheel to run all commands

101 #%wheel ALL=(ALL)       ALL

  • 别名设置(给多个值  起一个名字)

命令别名                     Cmnd_Alias 别名=命令列表

主机别名                     Host_Alias  别名=主机名列表

用户别名                     User_Aliaas 别名=用户名列表

注:别名要大写


 

Cmnd_Alias MGMWEB=/usr/bin/systemctl  * httpd , /usrbin/vim  /etc/httpd/conf/httpd.conf
Cmnd_Alias MGMDB=/usr/bin/systemctl  * mysqld , /usrbin/vim /etc/my.cnf
Cmnd_Alias MGMSOFT=/usr/bin/rpm , /usr/bin/yum

Host_Alias MYSER=localhost,host52

dc MYSER=MGMWEB , MGMSOFT
tc MYSER=MGMDB , MGMDOFT
nb MYSER=MGMWEB , MGMDB , MGMSOFT

Defaults logfile="/var/log/sudo.log"                                                          #启用日志,写在最底端

        日志作用:记录普通用户执行过的提权命令

三、ssh访问控制

 

  • ssh服务常用配置

ssh 传输是加密的

[root@host52 ~]# vim /etc/ssh/sshd_config 
Port 2222
#AddressFamily any
ListenAddress 192.168.4.52
#ListenAddress ::

]# systemctl restart sshd

重ssh连接发现连不上,需指端口

[root@root ~]# ssh -X  [email protected] -p 2222
 

  • 黑白名单

 /etc/ssh/sshd_config 

白名单          AllowUsers  用户列表

黑名单         DenyUsers   用户列表

[root@host52 ~]# vim /etc/ssh/sshd_config 
AllowUsers nb [email protected]                         #仅允许root@192.168.4.254(该机)和nb@随便哪台主机都可以连
 

[root@host52 ~]# vim /etc/ssh/sshd_config 
#AllowUsers nb [email protected]        

DenyUsers nb [email protected]                            #拒绝nb用户、192.168.4.254上用root登陆系统

  • 密钥对认证登陆

密钥对认证登陆(公钥加密  私钥解密)

口令(用户密码)

PasswordAuthentication yes
AuthorizedKeysFile      .ssh/authorized_keys
 

[root@host52 ~]# vim /etc/ssh/sshd_config 

PasswordAuthentication no

配置密钥对认证登陆步骤

1.创建密钥对  ssh-keygen

2.把公钥传给目标主机ssh-copy-id   192.168.4.52

四、SELinux

[root@host53 ~]# sestatus 
SELinux status:                 enabled
SELinuxfs mount:                /sys/fs/selinux
SELinux root directory:         /etc/selinux
Loaded policy name:             targeted            #策略
Current mode:                   permissive         #临时模式(命令行设置)
Mode from config file:          permissive         #配置文件里的模式
Policy MLS status:              enabled
Policy deny_unknown status:     allowed
Max kernel policy version:      28
 

[root@host53 ~]# vim /etc/selinux/config 

# 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 two values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected. 
#     mls - Multi Level Security protection.                                        
#多级防护
SELINUXTYPE=targeted
 

[root@host53 ~]# setenforce 1
[root@host53 ~]# getenforce 
Enforcing
 

[root@host53 ~]# sestatus 
SELinux status:                 enabled
SELinuxfs mount:                /sys/fs/selinux
SELinux root directory:         /etc/selinux
Loaded policy name:             targeted
Current mode:                   enforcing
Mode from config file:          permissive
Policy MLS status:              enabled
Policy deny_unknown status:     allowed
Max kernel policy version:      28
 

  • 查看安全上下文

用户:角色:访问类型:选项........

system_u:object_r:passwd_file_t:s0

system_u:object_r:etc_t:s0

system_u:system_r:httpd_t:s0

文件  ls   -Z 文件名

[root@host53 ~]# ls  -lZ /etc/passwd
-rw-r--r--. root root system_u:object_r:passwd_file_t:s0 /etc/passwd
 

目录  ls   -ldZ 目录名
[root@host53 ~]# ls -ldZ  /etc/
drwxr-xr-x. root root system_u:object_r:etc_t:s0       /etc/
 

进程  ps  aux  -Z | grep  -i 进程名

[root@host53 ~]# ps aux -Z | grep -i mysqld
system_u:system_r:mysqld_t:s0   mysql     6411  5.6 17.5 1119220 178668 ?      Sl   17:18   0:00 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid
 

[root@host53 ~]# systemctl start httpd
[root@host53 ~]# ps aux -Z | grep -i httpd
system_u:system_r:httpd_t:s0    root      6531  1.5  0.5 226240  5148 ?        Ss   17:20   0:00 /usr/sbin/httpd -DFOREGROUND
system_u:system_r:httpd_t:s0    apache    6532  0.0  0.3 228324  3152 ?        S    17:20   0:00 /usr/sbin/httpd -DFOREGROUND
system_u:system_r:httpd_t:s0    apache    6533  0.0  0.3 228324  3152 ?        S    17:20   0:00 /usr/sbin/httpd -DFOREGROUND
system_u:system_r:httpd_t:s0    apache    6534  0.0  0.3 228324  3152 ?        S    17:20   0:00 /usr/sbin/httpd -DFOREGROUND
system_u:system_r:httpd_t:s0    apache    6535  0.0  0.3 228324  3152 ?        S    17:20   0:00 /usr/sbin/httpd -DFOREGROUND
system_u:system_r:httpd_t:s0    apache    6536  0.0  0.3 228324  3152 ?        S    17:20   0:00 /usr/sbin/httpd -DFOREGROUND
unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 root 6538 0.0  0.0 112676 992 pts/0 S+ 17:20   0:00 grep --color=auto -i httpd
 

  • 一般操作规律
  1. 移动的文件,原有的上下文属性不变
  2. 复制的文件,自动继承目标位置的上下文
  • 修改安全上下文

chcon  -t   指定访问类型

chcon -R  递归修改

  • 重置安全上下文

restorecon [-R递归修改]    目标文件        #恢复为所在位置的默认上下文属性

/.autorelabel 文件

下次重启后会全部重置

  • 布尔值

  查看布尔值   getsebool -a 

修改布尔值     setsebool -P[永久] 选项 on|off

安装vsftpd

]# vim /etc/vsftpd/vsftpd.conf

anonymous_enable=YES
local_enable=YES
write_enable=YES

 # When SELinux is enforcing check for SE bool allow_ftpd_anon_write, allow_ftpd_full_access
anon_upload_enable=YES
anon_mkdir_write_enable=YES

]# systemctl restart vsftpd
 

访问ftp是否能下载上传文件

安装ftp

ftp   192.168.4.53

Name (192.168.4.53:root): ftp
331 Please specify the password.
Password:    回车

ftp> ls
227 Entering Passive Mode (192,168,4,53,74,243).
150 Here comes the directory listing.
drwxr-xrwx    2 0        0              69 May 28 01:38 pub
drwxr-xrwx    2 0        0             132 May 28 01:24 share
ftp> cd pub
ftp> ls
ftp> lcd /etc/
ftp> put   hosts
ftp> ls
ftp> get hosts          #默认下载到当前路径
ftp> exit

setenforce 1     #把SELinux开启

getenforce    #查看状态

再访问ftp则不能上传 了

getsebool -a   |  grep ftpd            #查看布尔值,找到配置文件里提示的开启

[root@host53 ~]# setsebool -P ftpd_anon_write on

[root@host53 ~]# setsebool -P ftpd_full_access on
此时再访问ftp可以上传了

  • 查看SELinux的日志信息     排错

启用SELiux运行的53主机的http服务,服务使用端口号8077 ,在网页目录编写测试文件 ,在客户端访问

[root@host53 ~]# rpm -q httpd
httpd-2.4.6-67.el7.x86_64
[root@host53 ~]# systemctl restart httpd
[root@host53 ~]# ss -nutlp | grep 80
 

[root@host53 ~]# getenforce 
Enforcing
 

[root@host53 ~]# vim /etc/httpd/conf/httpd.conf 
42行  Listen 8077
[root@host53 ~]# systemctl restart httpd    #报错
[root@host53 ~]# setenforce 0
[root@host53 ~]# systemctl restart httpd    #可以起来服务

[root@host53 ~]# echo "selinuxxxx"   >  /var/www/html/index.html 
[root@host53 ~]# firefox http://192.168.4.53:8077                                 #访问ok


[root@host53 ~]# setenforce 1
[root@host53 ~]# systemctl restart httpd      #报错。启动失败
 

                              

查看该 /var/log/messages日志(重点词setroubleshoot

May 28 09:53:17 host53 setroubleshoot: SELinux is preventing /usr/sbin/httpd from name_bind access on the tcp_socket port 8077. For complete SELinux messages run: sealert -l dd5138f3-8b02-4f4c-b291-c434cc159420
 


[root@host53 ~]# sealert -l dd5138f3-8b02-4f4c-b291-c434cc159420

#查看信息,它让你干嘛就干嘛,执行Do后面的#号后面的命令

If 确定应默认允许 httpd name_bind 访问 port 8077 tcp_socket。
Then 应该将这个情况作为 bug 报告。
可以生成本地策略模块以允许此访问。
Do
allow this access for now by executing:
# ausearch -c 'httpd' --raw | audit2allow -M my-httpd
# semodule -i my-httpd.pp
 

[root@host53 ~]# systemctl restart httpd                 #重启成功
 

发布了67 篇原创文章 · 获赞 13 · 访问量 4071

猜你喜欢

转载自blog.csdn.net/tongzhuo1220/article/details/90597713