Linux 加固(centos7)

来源:互联网 发布:奥威软件 编辑:程序博客网 时间:2024/05/22 21:29
(1)寻找空密码的用户:
首先root账户建立一个用户: useradd user (这样建立的用户的是不能直接用空密码登陆的)
然后root账户用passwd -d user 清除user的密码(这样就可以用空密码登陆了)
查找空密码的用户 awk -F: '($2==””){print $1}' /etc/shadow
这样我们找到这样的空密码的用户:user
给空密码账户上密码:
passwd user
删除用户:
userdel user
(2)查找uid为0的用户,当一个用户的uid为0的时候,那么这个用户就拥有root权限
查找uid为0的用户:
awk -F: '($3==0){print $1}' /etc/passwd


处理:
在root权限下修改/etc/passwd文件
找到这个用户,将第三个字段改成非0的数字:



这样保存就行


(3)
强制密码策略:
[1]禁止使用旧密码(参考:http://www.jb51.net/LINUXjishu/342330.html)
centos
$ sudo vi /etc/pam.d/system-auth
password sufficient pamunix.so sha512 shadow nullok tryfirstpass useauthtok remember= 记住的数量

[2]设置密码最小长度:
第一种方法:
$ sudo vi /etc/pam.d/system-auth
password sufficient pamunix.so sha512 shadow nullok tryfirstpass useauthtok remember= 5 minlen=最小长度

第二种方法:
vi /etc/login.defs

PASS_MIN_LNE 5
改为:
PASS_MIN_LEN 想要的位数
[3]密码生存周期限制
vi /etc/login.defs
PASS_MAX_DAYS=想要的天数
[4]口令过期提醒:
vi /etc/login.defs 加入:
PASS_WARN_AGE=天数
[5]登陆时间超时强制注销:
编辑 /etc/profile 指令:sudo vi /etc/profile
在histsize=1000

下面加一行 TMOUT=时间(单位为秒)


root账户登陆超时的时候就自动注销




(4)
【1】使用pam禁止用户su为root
vi /etc/pam.d/su 打开这个配置文件
#auth required /lib/security/$ISA/pam_wheel.so use_uid        ←找到此行,去掉行首的“#”

然后普通用户就无法通过su 得到root权限了


将在root用户下用指令 usermod -G wheel niexinming普通用户加入到wheel之后才能用su -提升到root



【2】
禁止用户通过sudo 得到root权限(参考http://bbs.51cto.com/thread-803953-1.html)
执行visudo 这个指令打开配置文件也可以vi /etc/sudoers 这样打开配置文件
加一行
test ALL=(root) /usr/sbin/useradd //新增加用户行
说明:
第一个字段:test为能使用sudo命令的用户;
第二个字段:第一个ALL为允许使用sudo的主机,第二个括号里的ALL为使用sudo后以什么身份(目的用户身份)来执行命令;
第三个字段:/usr/sbin/useradd为以sudo命令允许执行的命令;
上列解释: test    ALL=(root) /usr/sbin/useradd
表示允许test用户从任何主机登录,以root的身份执行/usr/sbin/useradd命令。
也可以将test用户设置为:
test    ALL=(ALL)     ALL
表示test可以使用sudo 任意提升权限

(5)更改文件的执行权(参考http://blog.csdn.net/taiyang1987912/article/details/40651623):
要使某个可执行的文件在root账户下也不具备root权限就可以这样:
chmod u+s 可执行文件



(6)apache降权
vi /etc/httpd/conf/httpd.conf
用/user 找到






然后改成你想要的用户,centos7 不能将用户改成root用户

然后重启apache:service httpd restart





apache禁止命令执行:
打开/etc/php.ini文件,
查找到 disable_functions ,添加需禁用的函数名
比如:phpinfo,eval,passthru,exec,system,chroot,scandir,chgrp,chown,shell_exec,proc_open,proc_get_status,ini_alter,ini_alter,ini_restore,dl,pfsockopen,openlog,syslog,readlink,symlink,popepassthru,stream_socket_server,fsocket,fsockopen
参考链接如下:
http://www.jb51.net/article/29750.htm

修改之后要重启apache才能生效

apache 禁止某个目录的执行脚本:
在httpd.conf中添加以下内容:(httpd.conf 一般在/etc/httpd/conf/httpd.conf)

<Directory "/var/www/linuxyw/Uploads">
<Files ~ ".php">
Order allow,deny
Deny from all
</Files>
</Directory>

禁止目录浏览
编辑文件:sudo vi /etc/httpd/confhttpd.conf
搜索options
找到  Options Indexes FollowSymLink
去掉  Indexes 之后就好

改为: Options FollowSymLink



以下转自 独自等待的博客:http://www.waitalone.cn/php-web-security-for-linux.html

disable_functions = passthru,exec,system,chroot,chgrp,chown,shell_exec,proc_open,proc_get_status,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,stream_socket_server,fsocket,phpinfo #禁用的函数expose_php = off            #避免暴露PHP信息display_errors = off        #关闭错误信息提示register_globals = off      #关闭全局变量enable_dl = off             #不允许调用dlallow_url_include = off     #避免远程调用文件session.cookie_httponly = 1 #http only开启upload_tmp_dir = /tmp       #明确定义upload目录open_basedir = ./:/tmp:/home/wwwroot/ #限制用户访问的目录



(7)
rsync的安全
设置rsync的密码
首先 编辑/etc/rsyncd.conf    命令是:sudo vi /etc/rsyncd.conf
在模块下面添加auth users=用户 (服务器中要有的用户)
然后指定密码文件 secrets file=/etc/rsyncd.secrets

在/etc/ 下面创建一个rsyncd.secrets


内容:


第一个字段是用户名,第二个是密码

(8)redis 密码
在redis.conf中,打开配置文件找到
#requirepass foobared

去掉注释,然后在 requirepass 后面写入自己的密码



然后重启redis,加载配置文件并让其在后台运行(记住不要以root权限运行redis):
.redis-server ../redis.conf &
注:../redis.conf 是配置文件
&是后台运行

连接就是:redis-cli -h 192.168.31.144 -a heheda
-a后面是密码

如果只是让本地访问redis的话就在在配置文件找到bind,然后在后面添加127.0.0.1



 (9)ftp的安全:
[1]安装vsftp:
yum -y install vsftpd
[2]关闭匿名访问:
打开/etc/vsftpd/vsftpd.conf
将anonymous_enable=NO
[3]禁止其他用户访问:
首先建立ftp用户:useradd ftptest -d 指定的家目录路径 -s /sbin/nologin
然后打开/etc/vsftpd/vsftpd.conf 修改:
userlist_deny=YES  
userlist_file=/etc/vsftpd/user_list

如果userlist_deny=YES的话,那么配置文件/etc/vsftpd/user_list里面的内容就是禁止登陆的用户

Ps:添加禁止ftp登陆用户的小窍门:
sudo awk  -F ':'  '{print $1}' /etc/passwd > /tmp/user.txt      #在终端执行这个命令,将所有的用户保存出来

cp /tmp/user.txt /etc/vsftpd/user_list  #复制到/etc/vsftpd/user_list
vi /etc/vsftpd/user_list去掉可以登陆的用户


[4]限制登陆用户在自己的家目录里面:
在配置文件中:/etc/vsftpd/vsftpd.conf
chroot_local_user=YES  #这个代表所有用户都限制在家目录里面
allow_writeable_chroot=YES  #添加这个选项就可以在家目录里面上传东西
[5]打开日志:
在配置文件中:/etc/vsftpd/vsftpd.conf
xferlog_enable=YES #激活日志文件
xferlog_file=/var/log/xferlog 日志文件位置

最后重启ftp:systemctl restart vsftpd.service
(10)samba 4.2.10配置:
安装samba:
yum install samba samba-client
【1】配置匿名访问
编辑文件:
sudo vi /etc/samba/smb.conf
在里面添加:
[global]
workgroup = WORKGROUP
security = user
guest account = niexinming
[allshare]
path = /home/niexinming/share
comment = share allshare
writeable = yes
browseable = yes
public = yes
guest ok=yes


说明:
[allshare]:是共享的名称
path:是要共享的路径
这个配置文件里面一定要加
guest account = niexinming
这个后面的niexinming是linux里面任意的账户,如果不加这个配置就会出现:
NT_STATUS_ACCESS_DENIED listing \*
这个错误
或者出现:
没有权限访问的错误
【2】基于身份认证的访问:
首先你要在系统中添加一个账户用于smb
sudo useradd smbuser -s /sbin/nologin
sudo passwd smbuser

然后把这个账户加入到smb的账户体系里面
pdbedit –a smbuser    #新建Samba账户,必须先存在该系统用户
sudo smbpasswd -a smbuser #新建Samba账户,必须先存在该系统用户
sudo smbpasswd -e smbuser #激活该账户
然后打开:sudo vi /etc/samba/smb.conf  添加:

[work]
path=/home/niexinming/testshare
comment = share allshare
writeable = yes
browseable = yes
public = no
guest=no
valid users = smbuser
admin users=smbuser

说明:
valid users=允许访问该共享的用户
admin users=该共享的管理者
其中valid users是smb用户认证的基础
而缺少admin users则会报错:
NT_STATUS_ACCESS_DENIED listing \*

如果还报这个错就去设置一下selinux 或者 暂时将selinux 关闭:sudo setenforce 0
最后上完整配置文件:

[global]
workgroup = WORKGROUP
security = user
guest account = root
netbios name = workgroup
[allshare]
path = /home/niexinming/share
comment = share allshare
writeable = yes
browseable = yes
public = yes
guest ok=yes
[work]
path=/home/niexinming/testshare
comment = share allshare
writeable = yes
browseable = yes
public = no
guest=no
valid users = smbuser
admin users=smbuser

(11)配置selinux :参考视频(http://www.tudou.com/programs/view/6fX2faGtu0g/)
首先安装semanage:yum -y install policycoreutils-python

检查进程的安全上下文
ps -Z

检查文件与目录的安全上下文
ls -Z

恢复目录文件的上下文访问权限
restorecon -R -v 目录
设置文件的上下文
chcon -t 文件上下文权限 文件
比如/var/www/html 有个文件test.txt不想让httpd访问
就这样设置:
chcon -t user_home_t test.txt
如果是改目录就是
chcon -R -t user_home_t 目录

将某个文件的权限复制到另一个文件上
chcon –reference=源文件 目标文件

(12)linux日志

【1】who:默认输出包括用户名,终端类型,登陆日期及远程主机。使用该命令,系统管理员可以查看当前的系统存在哪些不法用户,从而对其进行审计和处理
【2】users:用单独的一行打印出当前登陆的用户,每个显示的用户名对应一个登陆会话。如果一个用户有不止一个登陆会话,那他的用户名将显示相同的次数。
【3】last:显示以前登陆过的用户。系统管理员可以周期的对这些用户的登陆情况进行审计和考核,从而发现问题。还可以用    last 用户    来查看指定用户历史登陆信息
日志位置在:/var/log/wtmp
【4】lastlog:可以使用该指令检查某特定用户上次登陆的时间,并格式化输出上次登陆的日志/var/log/lastlog的内容。它根据uid排序显示登陆名,端口号和上次登陆的时间,如一个用户从未登陆过,lastlog显示【**从未登陆过**】
【5】apache的访问日志在:/var/log/apache2/access.log
错误日志在:/var/log/apache2/error.log
(12)iptables  配置:
【1】禁止ping(自己能ping出去,别人ping不到你)
iptables -A INPUT -p icmp --icmp-type 8 -s 0/0 -j DROP

【2】避免被扫描(检测到扫描之后会禁止这个ip的访问)
将下面的文本保存成scan.sh,然后sudo bash scan.sh


#script is for stoping Portscan and smurf attack### first flush all the iptables Rulesiptables -F# INPUT iptables Rules# Accept loopback inputiptables -A INPUT -i lo -p all -j ACCEPT# allow 3 way handshakeiptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT### DROPspoofing packetsiptables -A INPUT -s 10.0.0.0/8 -j DROP iptables -A INPUT -s 169.254.0.0/16 -j DROPiptables -A INPUT -s 172.16.0.0/12 -j DROPiptables -A INPUT -s 127.0.0.0/8 -j DROPiptables -A INPUT -s 192.168.0.0/24 -j DROPiptables -A INPUT -s 224.0.0.0/4 -j DROPiptables -A INPUT -d 224.0.0.0/4 -j DROPiptables -A INPUT -s 240.0.0.0/5 -j DROPiptables -A INPUT -d 240.0.0.0/5 -j DROPiptables -A INPUT -s 0.0.0.0/8 -j DROPiptables -A INPUT -d 0.0.0.0/8 -j DROPiptables -A INPUT -d 239.255.255.0/24 -j DROPiptables -A INPUT -d 255.255.255.255 -j DROP#for SMURF attack protectioniptables -A INPUT -p icmp --icmp-type 8 -s 0/0 -j DROP#iptables -A INPUT -p icmp -m icmp -m limit --limit 1/second -j ACCEPT# Droping all invalid packetsiptables -A INPUT -m state --state INVALID -j DROPiptables -A FORWARD -m state --state INVALID -j DROPiptables -A OUTPUT -m state --state INVALID -j DROP# flooding of RST packets, smurf attack Rejectioniptables -A INPUT -p tcp -m tcp --tcp-flags RST RST -m limit --limit 2/second --limit-burst 2 -j ACCEPT# Protecting portscans# Attacking IP will be locked for 24 hours (3600 x 24 = 86400 Seconds)iptables -A INPUT -m recent --name portscan --rcheck --seconds 86400 -j DROPiptables -A FORWARD -m recent --name portscan --rcheck --seconds 86400 -j DROP# Remove attacking IP after 24 hoursiptables -A INPUT -m recent --name portscan --removeiptables -A FORWARD -m recent --name portscan --remove# These rules add scanners to the portscan list, and log the attempt.iptables -A INPUT -p tcp -m tcp --dport 139 -m recent --name portscan --set -j LOG --log-prefix "portscan:"iptables -A INPUT -p tcp -m tcp --dport 139 -m recent --name portscan --set -j DROPiptables -A FORWARD -p tcp -m tcp --dport 139 -m recent --name portscan --set -j LOG --log-prefix "portscan:"iptables -A FORWARD -p tcp -m tcp --dport 139 -m recent --name portscan --set -j DROP# Allow the following ports through from outsideiptables -A INPUT -p tcp -m tcp --dport 25 -j ACCEPTiptables -A INPUT -p tcp -m tcp --dport 80 -j ACCEPTiptables -A INPUT -p tcp -m tcp --dport 443 -j ACCEPTiptables -A INPUT -p tcp -m tcp --dport 22 -j ACCEPT# Allow ping means ICMP port is open (If you do not want ping replace ACCEPT with REJECT)iptables -A INPUT -p icmp -m icmp --icmp-type 8 -j ACCEPT# Lastly reject All INPUT trafficiptables -A INPUT -j REJECT################# Below are for OUTPUT iptables rules ############################################### Allow loopback OUTPUT iptables -A OUTPUT -o lo -j ACCEPTiptables -A OUTPUT -m state --state ESTABLISHED,RELATED -j ACCEPT# Allow the following ports through from outside # SMTP = 25# DNS =53# HTTP = 80# HTTPS = 443# SSH = 22### You can also add or remove port no. as per your requirementiptables -A OUTPUT -p tcp -m tcp --dport 25 -j ACCEPTiptables -A OUTPUT -p udp -m udp --dport 53 -j ACCEPTiptables -A OUTPUT -p tcp -m tcp --dport 80 -j ACCEPTiptables -A OUTPUT -p tcp -m tcp --dport 443 -j ACCEPTiptables -A OUTPUT -p tcp -m tcp --dport 22 -j ACCEPT# Allow pingsiptables -A OUTPUT -p icmp -m icmp --icmp-type 8 -j ACCEPT# Lastly Reject all Output trafficiptables -A OUTPUT -j REJECT## Reject Forwarding  trafficiptables -A FORWARD -j REJECT[niexinming@MiWiFi-R3-srv ~]$ cat scan.sh #script is for stoping Portscan and smurf attack### first flush all the iptables Rulesiptables -F# INPUT iptables Rules# Accept loopback inputiptables -A INPUT -i lo -p all -j ACCEPT# allow 3 way handshakeiptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT### DROPspoofing packetsiptables -A INPUT -s 10.0.0.0/8 -j DROP iptables -A INPUT -s 169.254.0.0/16 -j DROPiptables -A INPUT -s 172.16.0.0/12 -j DROPiptables -A INPUT -s 127.0.0.0/8 -j DROPiptables -A INPUT -s 192.168.0.0/24 -j DROPiptables -A INPUT -s 224.0.0.0/4 -j DROPiptables -A INPUT -d 224.0.0.0/4 -j DROPiptables -A INPUT -s 240.0.0.0/5 -j DROPiptables -A INPUT -d 240.0.0.0/5 -j DROPiptables -A INPUT -s 0.0.0.0/8 -j DROPiptables -A INPUT -d 0.0.0.0/8 -j DROPiptables -A INPUT -d 239.255.255.0/24 -j DROPiptables -A INPUT -d 255.255.255.255 -j DROP#for SMURF attack protectioniptables -A INPUT -p icmp --icmp-type 8 -s 0/0 -j DROP#iptables -A INPUT -p icmp -m icmp -m limit --limit 1/second -j ACCEPT# Droping all invalid packetsiptables -A INPUT -m state --state INVALID -j DROPiptables -A FORWARD -m state --state INVALID -j DROPiptables -A OUTPUT -m state --state INVALID -j DROP# flooding of RST packets, smurf attack Rejectioniptables -A INPUT -p tcp -m tcp --tcp-flags RST RST -m limit --limit 2/second --limit-burst 2 -j ACCEPT# Protecting portscans# Attacking IP will be locked for 24 hours (3600 x 24 = 86400 Seconds)iptables -A INPUT -m recent --name portscan --rcheck --seconds 86400 -j DROPiptables -A FORWARD -m recent --name portscan --rcheck --seconds 86400 -j DROP# Remove attacking IP after 24 hoursiptables -A INPUT -m recent --name portscan --removeiptables -A FORWARD -m recent --name portscan --remove# These rules add scanners to the portscan list, and log the attempt.iptables -A INPUT -p tcp -m tcp --dport 139 -m recent --name portscan --set -j LOG --log-prefix "portscan:"iptables -A INPUT -p tcp -m tcp --dport 139 -m recent --name portscan --set -j DROPiptables -A FORWARD -p tcp -m tcp --dport 139 -m recent --name portscan --set -j LOG --log-prefix "portscan:"iptables -A FORWARD -p tcp -m tcp --dport 139 -m recent --name portscan --set -j DROP# Allow the following ports through from outsideiptables -A INPUT -p tcp -m tcp --dport 25 -j ACCEPTiptables -A INPUT -p tcp -m tcp --dport 80 -j ACCEPTiptables -A INPUT -p tcp -m tcp --dport 443 -j ACCEPTiptables -A INPUT -p tcp -m tcp --dport 22 -j ACCEPT# Allow ping means ICMP port is open (If you do not want ping replace ACCEPT with REJECT)iptables -A INPUT -p icmp -m icmp --icmp-type 8 -j ACCEPT# Lastly reject All INPUT trafficiptables -A INPUT -j REJECT################# Below are for OUTPUT iptables rules ############################################### Allow loopback OUTPUT iptables -A OUTPUT -o lo -j ACCEPTiptables -A OUTPUT -m state --state ESTABLISHED,RELATED -j ACCEPT# Allow the following ports through from outside # SMTP = 25# DNS =53# HTTP = 80# HTTPS = 443# SSH = 22### You can also add or remove port no. as per your requirementiptables -A OUTPUT -p tcp -m tcp --dport 25 -j ACCEPTiptables -A OUTPUT -p udp -m udp --dport 53 -j ACCEPTiptables -A OUTPUT -p tcp -m tcp --dport 80 -j ACCEPTiptables -A OUTPUT -p tcp -m tcp --dport 443 -j ACCEPTiptables -A OUTPUT -p tcp -m tcp --dport 22 -j ACCEPT# Allow pingsiptables -A OUTPUT -p icmp -m icmp --icmp-type 8 -j ACCEPT# Lastly Reject all Output trafficiptables -A OUTPUT -j REJECT## Reject Forwarding  trafficiptables -A FORWARD -j REJECT

 


[3]只允许某个ip段的主机登陆:
iptables -I INPUT -s 192.168.1/24 -p tcp --dport 22 -j ACCEPT
iptables -A INPUT -p tcp --dport 22 -j DROP

0 0