Chinaunix首页 | 论坛 | 博客
  • 博客访问: 334945
  • 博文数量: 64
  • 博客积分: 2301
  • 博客等级: 大尉
  • 技术积分: 570
  • 用 户 组: 普通用户
  • 注册时间: 2009-03-25 11:48
文章分类

全部博文(64)

文章存档

2013年(5)

2012年(6)

2011年(36)

2010年(7)

2009年(10)

分类: LINUX

2009-07-31 17:50:32

Linux安全配置步骤简述

一、磁盘分区

◆1、如果是新安装系统,对磁盘分区应考虑安全性:

1)根目录(/)、用户目录(/home)、临时目录(/tmp)和/var目录应分开到不同的磁盘分区;

2)以上各目录所在分区的磁盘空间大小应充分考虑,避免因某些原因造成分区空间用完而导致系统崩溃;

◆2、对于/tmp/var目录所在分区,大多数情况下不需要有suid属性的程序,所以应为这些分区添加nosuid属性;

方法一:修改/etc/fstab文件,添加nosuid属性字。例如:

/dev/hda2       /tmp      ext2     exec,dev,nosuid,rw      0 0

方法二:如果对/etc/fstab文件操作不熟,建议通过linuxconf程序来修改。

* 运行linuxconf程序;

* 选择"File systems"下的"Access local drive"

* 选择需要修改属性的磁盘分区;

* 选择"No setuid programs allowed"选项;

* 根据需要选择其它可选项;

* 正常退出。(一般会提示重新mount该分区)

二、安装

1、对于非测试主机,不应安装过多的软件包。这样可以降低因软件包而导致出现安全漏洞的可能性。

2、对于非测试主机,在选择主机启动服务时不选择非必需的服务。例如routedypbind等。

三、安全配置与增强

内核升级。起码要升级至2.2.16以上版本。

GNU libc共享库升级。(警告:如果没有经验,不可轻易尝试。可暂缓。)

关闭危险的网络服务。echochargenshellloginfingerNFSRPC

关闭非必需的网络服务。talkntalkpop-2

常见网络服务安全配置与升级

确保网络服务所使用版本为当前最新和最安全的版本。

取消匿名FTP访问

去除非必需的suid程序

使用tcpwrapper

使用ipchains防火墙

日志系统syslogd

一些细节:

◆1、操作系统内部的log file是检测是否有网络入侵的重要线索,当然这个假定你的logfile不被侵入者所破坏,如果你有台服务器用专线直接连到Internet上,这意味 着你的IP地址是永久固定的地址,你会发现有很多人对你的系统做telnet/ftp登录尝试,试着运行如下语句去检查。

#more /var/log/secure | grep refused

◆2、限制具有SUID权限标志的程序数量,具有该权限标志的程序以root身份运行,是一个潜在的安全漏洞,当然,有些程序是必须要具有该标志的,象passwd程序。

◆3BIOS安全。设置BIOS密码且修改引导次序禁止从软盘启动系统。

◆4、用户口令。用户口令是Linux安全的一个最基本的起点,很多人使用的用户口令就是简单的password,这等于给侵入者敞开了大 门,虽然从理论上说没有不能确解的用户口令,只要有足够的时间和资源可以利用。比较好的用户口令是那些只有他自己能够容易记得并理解的一串字符,并且绝对 不要在任何地方写出来。

◆5/etc/exports 文件。如果你使用NFS网络文件系统服务,那么确保你的/etc/exports具有最严格的存取权限设置,不意味着不要使用任何通配符,不允许root 写权限,mount成只读文件系统。编辑文件/etc/exports并且加入例如:

/dir/to/export host1.mydomain.com(ro,root_squash)

/dir/to/export host2.mydomain.com(ro,root_squash)

其中/dir/to/export 是你想输出的目录,host.mydomain.com是登录这个目录的机器名,

ro意味着mount成只读系统,root_squash禁止root写入该目录。

为了让上面的改变生效,运行

/usr/sbin/exportfs –a

◆6、确信/etc/inetd.conf的所有者是root,且文件权限设置为600

 

编辑/etc/inetd.conf禁止以下服务:

ftp, telnet, shell, login, exec, talk, ntalk, imap, pop-2, pop-3, finger,

auth, etc. 除非你真的想用它。

特别是禁止那些r命令.如果你用ssh/scp,那么你也可以禁止掉telnet/ftp

为了使改变生效,运行

#killall -HUP inetd

你也可以运行

#chattr +i /etc/inetd.conf

使该文件具有不可更改属性。

只有root才能解开,用命令

#chattr -i /etc/inetd.conf

 

◆7 TCP_WRAPPERS

默认地,Redhat Linux允许所有的请求,TCP_WRAPPERS增强你的站点的安全性是举手

之劳,你可以放入

“ALL: ALL”/etc/hosts.deny中禁止所有的请求,然后放那些明确允许的请求到

/etc/hosts.allow中,如:

sshd: 192.168.1.10/255.255.255.0 gate.openarch.com

IP地址192.168.1.10和主机名gate.openarch.com,允许通过ssh连接。

配置完了之后,用tcpdchk检查

[root@deep]# tcpdchk

tcpchkTCP_Wrapper配置检查工具,

它检查你的tcp wrapper配置并报告所有发现的潜在/存在的问题。

◆8、别名文件 aliases

编辑别名文件/etc/aliases(也可能是/etc/mail/aliases),移走/注释掉下面的行。

# Basic system aliases -- these MUST be present.

MAILER-DAEMON: postmaster

postmaster: root

# General redirections for pseudo accounts.

bin: root

daemon: root

#games: root ?remove or comment out.

#ingres: root ?remove or comment out.

nobody: root

#system: root ?remove or comment out.

#toor: root ?remove or comment out.

#uucp: root ?remove or comment out.

# Well-known aliases.

#manager: root ?remove or comment out.

#dumper: root ?remove or comment out.

#operator: root ?remove or comment out.

# trap decode to catch security attacks

#decode: root

# Person who should get root's mail

#root: marc

最后更新后不要忘记运行/usr/bin/newaliases,使改变生效。

◆9sysctl的修改

echo "

 

#improve the number of open files

fs.file-max = 60000

 

#disable ip source routing

net.ipv4.conf.all.accept_source_route = 0

net.ipv4.conf.lo.accept_source_route = 0

net.ipv4.conf.eth0.accept_source_route = 0

net.ipv4.conf.default.accept_source_route = 0

 

#enable ignoring broadcasts request

net.ipv4.icmp_echo_ignore_broadcasts = 1

 

#enable ignoring ping request

net.ipv4.icmp_echo_ignore_all = 1

 

 

#disbale icmp redirect acceptance

net.ipv4.conf.all.accept_redirects = 0

net.ipv4.conf.lo.accept_redirects = 0

net.ipv4.conf.lo.eth0.accept_redirects = 0

net.ipv4.conf.lo.default.accept_redirects = 0

 

# enable bad error message protection

net.ipv4.icmp_ignore_bogus_error_response = 1

 

#enable log spoofed packets.source routed packets,redirect packets

net.ipv4.conf.all.log_martians = 1

net.ipv4.conf.lo.log_martians = 1

net.ipv4.conf.eth0.log_martians = 1

net.ipv4.conf.default.log_martians = 1

 

#decrease the time default value for tcp_fin_timeout connection

net.ipv4.tcp_fin_timeout = 30

 

#decrease the time default value for tcp_keeplive_time connection

net.ipv4.tcp_keeplive_time = 1800

 

#turn off the tcp_window_scaling support

net.ipv4.tcp_window_scaling = 0

 

#turn off the tcp_sack support

net.ipv4.tcp_sack = 0

 

#turn off the tcp_timestamps support

net.ipv4.tcp_timestamps = 0

 

" >> /etc/sysctl.conf

◆10、不要显示出操作系统和版本信息。

如果你希望某个人远程登录到你的服务器时不要显示操作系统和版本信息,你能改变

/etc/inetd.conf中的一行象下面这样:

telnet stream tcp nowait root /usr/sbin/tcpd in.telnetd -h

-h标志在最后使得telnet后台不要显示系统信息,而仅仅显示login:

◆11The /etc/host.conf file

编辑host.conf文件(vi /etc/host.conf)且加下面的行:

# Lookup names via DNS first then fall back to /etc/hosts.

order bind,hosts

# We don't have machines with multiple IP addresses on the same card

(like virtual server,IP Aliasing).

multi off

# Check for IP address spoofing.

nospoof on

IP Spoofing: IP-Spoofing is a security exploit that works by tricking

computers in a trust relationship that you are someone that you really aren't.

◆12. The /etc/securetty file and /etc/inittab file

该文件指定了允许root登录的tty设备,/etc/securetty/bin/login程序读取,它的

格式是一行一个被允许的名字列表,如你可以编辑/etc/securetty且注释出下面的行。

tty1

#tty2

#tty3

#tty4

#tty5

#tty6

#tty7

#tty8

-意味着root仅仅被允许在tty1终端登录。

/etc/inittab文件中有如下一段话:

  # Run gettys in standard runlevels

  1:2345:respawn:/sbin/mingetty tty1

  2:2345:respawn:/sbin/mingetty tty2

  #3:2345:respawn:/sbin/mingetty tty3

  #4:2345:respawn:/sbin/mingetty tty4

  #5:2345:respawn:/sbin/mingetty tty5

  #6:2345:respawn:/sbin/mingetty tty6

 

  系统默认的可以使用6个控制台,即Alt+F1,Alt+F2...,这里在3456前面加上“#”,

注释该句话,这样现在只有两个控制台可供使用,最好保留两个。然后重新启动init进程,改动即可生效!

 /sbin/init q  init的重新启动

 

◆13. 特别的帐号

禁止所有默认的被操作系统本身启动的且不需要的帐号,当你第一次装上系统时就应该做此检查,Linux提供了各种帐号,你可能不需要,如果你不需要这个帐号,就移走它,你有的帐号越多,就越容易受到攻击。

为删除你系统上的用户,用下面的命令:

[root@deep]# userdel username

为删除你系统上的组用户帐号,用下面的命令:

[root@deep]# groupdel username

在终端上打入下面的命令删掉下面的用户。

[root@deep]# userdel adm

[root@deep]# userdel lp

[root@deep]# userdel sync

[root@deep]# userdel shutdown

[root@deep]# userdel halt

[root@deep]# userdel mail

如果你不用sendmail服务器,procmail.mailx,就删除这个帐号。

[root@deep]# userdel news

[root@deep]# userdel uucp

[root@deep]# userdel operator

[root@deep]# userdel games

如果你不用X windows 服务器,就删掉这个帐号。

[root@deep]# userdel gopher

[root@deep]# userdel ftp

如果你不允许匿名FTP,就删掉这个用户帐号。

===

打入下面的命令删除组帐号

[root@deep]# groupdel adm

[root@deep]# groupdel lp

[root@deep]# groupdel mail

如不用Sendmail服务器,删除这个组帐号

[root@deep]# groupdel news

[root@deep]# groupdel uucp

[root@deep]# groupdel games

如你不用X Windows,删除这个组帐号

[root@deep]# groupdel dip

[root@deep]# groupdel pppusers

[root@deep]# groupdel popusers

如果你不用POP服务器,删除这个组帐号

[root@deep]# groupdel slipusers

用下面的命令加需要的用户帐号

[root@deep]# useradd username

用下面的命令改变用户口令

[root@deep]# passwd username

chattr命令给下面的文件加上不可更改属性。

[root@deep]# chattr +i /etc/passwd

[root@deep]# chattr +i /etc/shadow

[root@deep]# chattr +i /etc/group

[root@deep]# chattr +i /etc/gshadow

◆14、阻止任何人su作为root.

如果你不想任何人能够su作为root,你能编辑/etc/pam.d/su加下面的行:

auth sufficient /lib/security/pam_rootok.so debug

auth required /lib/security/pam_wheel.so group=isd

意味着仅仅isd组的用户可以su作为root.

然后,如果你希望用户adminsu作为root.就运行下面的命令。

[root@deep]# usermod -G10 admin

 

 

◆15、资源限制

对你的系统上所有的用户设置资源限制可以防止DoS类型攻击(denial of service attacks

如最大进程数,内存数量等。例如,对所有用户的限制象下面这样:

编辑/etc/security/limits.con加:

* hard core 0

* hard rss 5000

* hard nproc 20

你也必须编辑/etc/pam.d/login文件加/检查这一行的存在。

session required /lib/security/pam_limits.so

上面的命令禁止core files“core 0”,限制进程数为“nproc 50“,且限制内存使用

5M“rss 5000”

◆16The /etc/lilo.conf file

a Add: restricted

加这一行到每一个引导映像下面,就这表明如果你引导时用(linux single),则需要一个password.

b Add: password=some_password

当与restricted联合用,且正常引导时,需要用户输入密码,你也要确保lilo.conf

文件不能被不属于root的用户可读,也免看到密码明文。下面是例子:

编辑/etc/lilo.conf加:

====

boot=/dev/sda

map=/boot/map

install=/boot/boot.b

prompt

timeout=50

Default=linux

restricted ?add this line.

password=some_password ?add this line.

image=/boot/vmlinuz-2.2.12-20

label=linux

initrd=/boot/initrd-2.2.12-10.img

root=/dev/sda6

read-only

[root@deep]# chmod 600 /etc/lilo.conf (不再能被其他用户可读).

[root@deep]# /sbin/lilo -v (更新lilo配置).

[root@deep]# chattr +i /etc/lilo.conf(阻止该文件被修改)

◆17、禁止 Control-Alt-Delete 重启动机器命令

[root@deep]# vi /etc/inittab

ca::ctrlaltdel:/sbin/shutdown -t3 -r now

To

#ca::ctrlaltdel:/sbin/shutdown -t3 -r now

[root@deep]# /sbin/init q

◆18、重新设置/etc/rc.d/init.d/目录下所有文件的许可权限

[root@deep]# chmod -R 700 /etc/rc.d/init.d/*

仅仅root可以读,写,执行上述所有script file.

◆19The /etc/rc.d/rc.local file

默认地,当你loginlinux server时,它告诉你linux版本名,内核版本名和服务器

主机名。它给了你太多的信息,如果你就希望得到提示login: ,编辑

/etc/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

然后,做下面的事情:

[root@deep]# rm -f /etc/issue

[root@deep]# rm -f /etc/issue.net

[root@deep]# touch /etc/issue

[root@deep]# touch /etc/issue.net

 

 

◆20、被root拥有的程序的位。

移走那些被root拥有程序的s位标志,当然有些程序需要这个,用命令‘chmod a-s’完成这个。

注:前面带(*)号的那些程序一般不需要拥有s位标志。

[root@deep]# find / -type f \( -perm -04000 -o -perm -02000 \) \-exec ls –lg {} \;

 

 

你可以用下面的命令查找所有带s位标志的程序:

[root@deep]# find / -type f \( -perm -04000 -o -perm -02000 \) \-exec ls -lg {} \;

>; suid-sgid-results

把结果输出到文件suid-sgid-results中。

为了查找所有可写的文件和目录,用下面的命令:

[root@deep]# find / -type f \( -perm -2 -o -perm -20 \) -exec ls -lg {} \; >; ww-files-results

[root@deep]# find / -type d \( -perm -2 -o -perm -20 \) -exec ls -ldg {} \; >; ww-directories-results

用下面的命令查找没有拥有者的文件:

[root@deep]# find / -nouser -o -nogroup >; unowed-results

用下面的命令查找所有的.rhosts文件:

[root@deep]# find /home -name .rhosts >; rhost-results

 

安全守则

1. 废除系统所有默认的帐号和密码。

2. 在用户合法性得到验证前不要显示公司题头、在线帮助以及其它信息。

3. 废除黑客可以攻击系统的网络服务。

4. 使用68位的字母数字式密码。

5. 限制用户尝试登录到系统的次数。

6. 记录违反安全性的情况并对安全记录进行复查。

7. 对于重要信息,上网传输前要先进行加密。

8. 重视专家提出的建议,安装他们推荐的系统补丁

9. 限制不需密码即可访问的主机文件。

10.修改网络配置文件,以便将来自外部的TCP连接限制到最少数量的端口。不允许诸如tftp,sunrpc,printer,rloginrexec之类的协议。

11.upas代替sendmailsendmail有太多已知漏洞,很难修补完全。

12.去掉对操作并非至关重要又极少使用的程序。

13.使用chmod将所有系统目录变更为711模式。这样,攻击者们将无法看到它们当中有什么东西,而用户仍可执行。

14.只要可能,就将磁盘安装为只读模式。其实,仅有少数目录需读写状态。

15.将系统软件升级为最新版本。老版本可能已被研究并被成功攻击,最新版本一般包括了这些问题的补救。

 

阅读(513) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~