Linux 系统安全设置

来源:互联网 发布:显微图像软件 编辑:程序博客网 时间:2024/06/06 09:41
Linux 系统安全设置
一、安装
1 、 建议把"/var"和"/tmp"放在不同的分区;
2 、 另外最好把"/var"和"/usr"放在不同的分区。
3 、 建议所有web 服务器把日志文件(/var/log )单独放入一个分区中,如果有硬盘的话,
最好单独一块硬盘
4 、 不必安装的软件包:
Applications/Archiving: dump
Applications/File: git
Applications/Internet: finger, ftp, fwhois, ncftp, rsh, rsync, talk, telnet
Applications/Publishing: ghostscript, ghostscript-fonts, mpage, rhs-printfilters
Applications/System: arpwatch, bind-utils, knfsd-clients, procinfo, rdate, rdist, screen,
ucd-snmp-utils
Documentation/indexhtml
System Environment/Base: chkfontpath, yp-tools
System Environment/Daemons: XFree86-xfs, lpr, pidentd, portmap, routed, rusers, rwho,
tftp, ucd-snmp, ypbind
System Environment/Libraries: XFree86-libs, libpng
User Interface/X: XFree86-75dpi-fonts, urw-fonts
5 、 安装后必须卸载的软件包:
pump apmd isapnptools redhat-logos mt-st kernel-pcmcia-cs setserial
redhat-release eject linuxconf kudzu gd bc getty_ps raidtools pciutils mailcap setconsole
gnupg
6 、 安装后必须安装的软件包(有了这些软件包我们才可以编译程序):
autoconf-2.13-5.noarch.rpm m4-1.4-12.i386.rpm automake-1.4-5.noarch.rpm
dev86-0.14.9-1.i386.rpm bison-1.28-1.i386.rpm byacc-1.9-11.i386.rpm
cdecl-2.5-9.i386.rpm cpp-1.1.2-24.i386.rpm cproto-4.6-2.i386.rpm
ctags-3.2-1.i386.rpm egcs-1.1.2-24.i386.rpm ElectricFence-2.1-1.i386.rpm
flex-2.5.4a-7.i386.rpm gdb-4.18-4.i386.rpm kernel-headers-2.2.12-20.i386.rpm
glibc-devel-2.1.2-11.i386.rpm make-3.77-6.i386.rpm patch-2.5-9.i386.rpm
7 、
二、系统中设定
1 、 截短"~/.bash_history"文件
编辑/etc/profile 的下面两项可以做到这一点:
HISTFILESIZE=20
HISTSIZE=20
2 、 防止任何人都可以su 为root (为管理开设个人账号,在开通su 权限)
a 、 编辑su 文件(vi /etc/pam.d/su )在文件的头部加入下面两行:
auth sufficient /lib/security/pam_rootok.so debug
auth required /lib/security/pam_wheel.so group=wheel
b 、 然后把您想要执行su 成为root 的用户放入wheel 组
[root@sound]# usermod -G wheel newuser
3 、 对系统资源编辑limits.conf 文件(vi /etc/security/limits.conf ),加入或改变下面这些行:
* hard core 0
* hard rss 5000
* hard nproc 20
这些行的的意思是:"core 0"表示禁止创建core 文件;"nproc 20"把最多进程数限制
到20 ;"rss 5000"表示除了root 之外,其他用户都最多只能用5M 内存。上面这些
都只对登录到系统中的用户有效。通过上面这些限制,就能更好地控制系统中的用
户对进程、core 文件和内存的使用情况。星号"*"表示的是所有登录到系统中的用
户。
然后必须编辑"/etc/pam.d/login"文件,在文件末尾加入下面这一行:
session required /lib/security/pam_limits.so
4 、 提高系统防止SYN 攻击的能力
[root@sound]# echo 1 > /proc/sys/net/ipv4/tcp_syncookies
把这个命令加入"/etc/rc.d/rc.local"文件中
5 、 去掉用户提示信息
编辑"/ect/rc.d/rc.local"文件,在下面这些行的前面加上"#":
# This will overwrite /etc/issue at every boot. So, make any changes you
# want to make to /etc/issue here or you will lose them when you reboot.
#echo "" > /etc/issue
#echo "$R" >> /etc/issue
#echo "Kernel $(uname -r) on $a $(uname -m)" >> /etc/issue
#
#cp -f /etc/issue /etc/issue.net
#echo >> /etc/issue
然后删除"/etc"目录下的"issue.net"和"issue"文件
6 、 限用户使用SSH 登陆
修改文件/etc/ssh/sshd_config
PermitRootLogin no #是否允许root 使用ssh 登录
AllowGroups ftp #只允许ftp 给内用户可以通过ssh 登录
AllowUsers aaa #只允许aaa 这个用户可以通过ssh 登录
还有就是AllowUsers 可以限定登陆主机和用户
eg.
AllowUsers root@215.5.51.* test@1.2.3.4
[root@localhost /]# service sshd restart
7 、 The /etc/securetty file
该文件指定了允许root 登录的tty 设备,/etc/securetty 被/bin/login 程序读取,它
的格式是一行一个被允许的名字列表,如你可以编辑/etc/securetty 且注释出下面
的行。
tty1
#tty2
#tty3
#tty4
#tty5
#tty6
#tty7
#tty8
-意味着root 仅仅被允许在tty1 终端登录,但对用户su 命令转换到root 不会受影响。
8 、 特别的帐号
禁止所有默认的被操作系统本身启动的且不需要的帐号,当你第一次装上系统时就
应该做此检查,Linux 提供了各种帐号,你可能不需要,如果你不需要这个帐号,
就移走它,你有的帐号越多,就越容易受到攻击。
为删除你系统上的用户,用下面的命令:
[root@deep ]#userdel username
为删除你系统上的组用户帐号,用下面的命令:
[root@deep ]#groupdel username
9 、
三、添加防火墙(iptables )
四、对应用程序和服务进行安全控制
修改php.ini 配置文件
以 php 发行版的php.ini-dist 为蓝本进行修改。
1)Error handling and logging
在 Error handling and logging 部分可以做一些设定。先找到:
display_errors =On
php 缺省是打开错误信息显示的,我们把它改为:
display_errors =Off
关 闭错误显示后,php 函数执行错误的信息将不会再显示给用户,这样能在一定程度
上防止攻击者从错误信息得知脚本的物理位置,以及一些其它有用的信息,起码给攻击者的
黑箱检测造成一定的障碍。这些错误信息可能对我们自己有用,可以让它写到指定文件中去,
那么修改以下:
log_errors =Off
改 为:
log_errors =On
以 及指定文件,找到下面这行:
;error_log =filename
去掉前面的;注释,把f i l e n a m e 改为指定文件,如
/usr/local/apache/logs/php_error.log
error_log =/usr/local/apache/logs/php_error.log
这 样所有的错误都会写到php_error.log 文件里。
2)Safe Mode
php 的safe_mode 功能对很多函数进行了限制或禁用了,能在很大程度解决php 的安
全问题。在
Safe Mode 部分找到:
safe_mode =Off
改 为:
safe_mode =On
这 样就打开了safe_mode 功能。象一些能执行系统命令的函数shell_exec()和``被禁
止,其它的一些执行函数如:exec(),system(),passthru(),popen()将被限制只能执行
safe_mode_exec_dir 指定目录下的程序。如果你实在是要执行一些命令或程序,找到
以下:
safe_mode_exec_dir =
指 定要执行的程序的路径,如:
safe_mode_exec_dir =/usr/local/php/exec
然 后把要用的程序拷到/usr/local/php/exec 目录下,这样,象上面的被限制的函数
还能执行该目录里的程序。
关 于安全模式下受限函数的详细信息请查看php 主站的说明:
http://www.php.net/manual/en/features.safe-mode.php
3)disable_functions
如 果你对一些函数的危害性不太清楚,而且也没有使用,索性把这些函数禁止了。找
到下面这行:
disable_functions =
在 ”=“后面加上要禁止的函数,多个函数用”,“隔开。
4)修改httpd.conf
如 果你只允许你的php 脚本程序在web 目录里操作,还可以修改httpd.conf 文件限制
php 的操作路径。比如你的web 目录是/usr/local/apache/htdocs,那么在httpd.conf 里
加上这么几行:
<Directory /usr/local/apache/htdocs>
php_admin_value open_basedir /usr/local/apache/htdocs
</Directory>
这 样,如果脚本要读取/usr/local/apache/htdocs 以外的文件将不会被允许,如果错误
显示打开的话会提示这样的错误:
Warning:open_basedir restriction in effect.File is in wrong directory in
/usr/local/apache/htdocs/open.php on line 4 等等。
5)文件及目录的权限设置
web 目录里除了上传目录,其它的目录和文件的权限一定不能让nobody 用户有写权限。
否则,攻击者可以修改主页文件,所以web 目录的权限一定要设置好。
还 有,php 脚本的属主千万不能是root,因为safe_mode 下读文件的函数被限制成被读
文件的属主必须和当前执行脚本的属主是一样才能被读,否则如果错误显示打开的话会显示
诸如以下的错误:
Warning:SAFE MODE Restriction in effect.The script whose uid is 500 is not
allowed to access /etc/passwd owned by uid 0 in
/usr/local/apache/htdocs/open.php on line 3
这 样我们能防止许多系统文件被读,比如:/etc/passwd 等。
上 传目录和上传脚本的属主也要设成一样,否则会出现错误的,在safe_mode 下这些要
注意。
五、MySql 安全
1 、 给root 用户加上口令
1) 在shell 提示符下用mysqladmin 命令来改root 用户口令
shell>mysqladmin -uroot password test
2)用set password 修改口令:
mysql> set password for root@localhost=password('test');
3)直接修改user 表的root 用户口令
mysql> use mysql;
mysql> update user set password=password('test') where user='root';
mysql> flush privileges;
2 、 删除user 为空的匿名用户
mysql> delete from user where user='';
3 、 授权表使用举例
grant 用于给增加用户和创建权限,revoke 用于删除用户权限。
下面是一些用grant 增加用户和创建权限的例子:
mysql> grant all privileges on *.* to test@localhost identified by 'test' with grant option;
这句增加一个本地具有所有权限的test 用户(超级用户),密码是test 。ON 子句中的*.*意味着"
所有数据库、所有表"。with grant option 表示它具有grant 权限。
mysql> grant select,insert,update,delete,create,drop privileges on test.* to
test1@'192.168.1.0/255.255.255.0' identified by 'test';
这句是增加了一个test1 用户,口令是test ,但是它只能从C 类子网192.168.1 连接,对test 库有
select,insert,update,delete,create,drop 操作权限。
用grant 语句创建权限是不需要再手工刷新授权表的,因为它已经自动刷新了。
给用户创建权限还可以通过直接修改授权表:
mysql> insert into user
values("localhost","test",password("test"),"Y","Y","Y","Y","Y","Y","Y","Y","Y","Y","Y","Y","Y","Y"
);
mysql> flush privileges;
这两句和上面第一句grant 的效果是一样的,也是增加了一个本地的test 超级用户。我们看到用
grant 方便多了,而且还不需flush privileges 。
mysql> insert into user (host,user,password)
values("192.168.1.0/255.255.255.0","test1",PASSWORD("test"));
mysql> insert into db
values("192.168.1.0/255.255.255.0","test","test1","Y","Y","Y","Y","Y","Y","N","N","N","N")
mysql> flush privileges;
这三句和上面第二句grant 的效果也是一样的,也是增加了一个只能从C 类子网192.168.1 连接,
对test 库有select,insert,update,delete,create,drop 操作权限的test1 用户,口令是test 。
要取消一个用户的权限,使用revoke 语句。revoke 的语法非常类似于grant 语句,除了to 用from
取代并且没有identified by 和with grant option 子句,下面是用revoke 删除用户权限的例子:
mysql> revoke all on test.* from test1@'192.168.1.0/255.255.255.0';
这句revoke 就撤消了上面第二句grant 创建的权限,但是test1 用户并没有被删除,必须手工从
user 表删除:
mysql> delete from user where user='test1';
mysql> flush privileges;
这样,test1 用户就彻底删除了。
4 、 增加mysql 连接数和错误数
set-variable = max_connections=5000
set-variable = max_connect_errors=10000
5 、
六、安装软件
1 、 tripwire http://www.tripwiresecurity.com (文件安全检查)
2 、 Chkrootkit (系统安全检查)
3 、 icmp /tcp 日志管理程序
4 、 bastille linux http://bastille-linux.sourceforge.net
5 、 logrotate. /etc/logrotate.conf
6 、 logcheck 日志检查
七、
原创粉丝点击