资源描述
linux系统安全详细设置
一、BIOS安全(硬件上的安全)
1.最基本最简单的安全配置,保障计算机硬件配置等不被别人更改。给BIOS设置密码,防止改变启动顺序从软盘或光盘启动。防止特殊的启动盘启动用户的系统,进入rescue或其他模式,改变或删除当前配置等。
2.禁止使用ctrl+alt+delete重起机器
编辑/etc/inittab文件,注释掉下面一行.
ca::ctrlaltdel:/sbin/shutdown -t3 -r now
改成:(使用#)
# ca::ctrlaltdel:/sbin/shutdown -t3 -r now
二、帐号安全
口令是系统的第一道防线,目前大多数攻击都是从截获口令或猜测口令等口令攻击开始的。/etc 目录下主要存放系统的配置文件,应对这个目录下的几个文件进行修改。1./etc/login.defs文件是login程序的配置文件,口令的长度和口令的有效期等可以在这里设置。
[root@ ~]# vi /etc/login.defs
...
PASS_MAX_DAYS 9999 密码被用最多天数
PASS_MIN_DAYS 0 密码被用最少天数
PASS_MIN_LEN 5 系统默认密码长度5,我们可以该成8或更多.
PASS_WARN_AGE 7 密码有效期警告,超过7天将提示用户更换新的密码.
...
2./etc/profile文件是环境变量设置文件。在此文件设置环境变量将对所有用户生效。我们要在此文件设置自动注销帐户的时间及命令的历史记录数。
[root@tp ~]# vi /etc/profile
...
HOSTNAME=`/bin/hostname`
HISTSIZE=1000 这里1000代表用户操作命令的历史记录,应尽量小一些,设置成0也可以。tmout=600 添加此行,如果系统用户在600秒(10分钟)内不做任何操作,将自动注销这个用户。
3./etc/passwd文件存放系统用户名,用户标识(UID),组标识(GID)等的地方。我们要在这里找到并清除没有设置口令的用户,同时还要清除一些特别帐号(因为可能会存在潜在的危险)。
[root@tp ~]# vi /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
...
wh::500:501::/home/wh:/bin/bash
仔细观察上面的一行(wh用户),在第二项,两个冒号中间什么都没有,而上面的的用户(如root用户)都是x,这表明此用户没有密码,要么添加密码,要么删掉。
4.特别帐号的处理
如果不启动用sendmail,删除如下用户
[root@tp wh]# userdel adm
[root@tp wh]# userdel lp
[root@tp wh]# userdel sync
[root@tp wh]# userdel shudown
[root@tp wh]# userdel halt
[root@tp wh]# userdel mail
如果不用X windows服务器.可有删除
[root@tp wh]# userdel news
[root@tp wh]# userdel uucp
[root@tp wh]# userdel operator
[root@tp wh]# userdel games
如果不允许匿名FTP帐号登陆,可删除
[root@tp wh]# userdel gopher
[root@tp wh]# userdel ftp
三、重要文件的安全设置.
首先要了解两个命令
1.chmod:改变文件的属主
2.chattr:改变文件属性
我们要做的是将与安全相关的重要文件的所有者改成root,并给相应的权限,还有就是改变文件的属性让它禁止被修改。以下是与安全相关的重要文件:(其实,只要是不想让其他用户更改的文件都可以这么做)
1./etc/passwd,passwd-,passwd.OLD,group,group- 用户,组的ID等信息文件.
2./etc/shadow,shadow-,gshadow,gshadow- 用户,组密码加密文件.
3./etc/xinetd.conf 网络守护进程主配置文件
4./etc/inittab 系统在启动是会读取这个文件里的内容.
5./etc/services 防止未经许可的删除或添加服务
6./etc/rc.d/rc.sysinit 系统启动是需要读取的文件,
7./etc/rc.d/init.d/*
以一个文件为例,其它都一样
[root@tp etc]# chattr +i passwd
当chattr +i时就是禁止对文件进行修改,当我们要添加用户时,就会有麻烦,因为passwd文件禁止修改写入.所以我们还要改掉它的属性.chattr –i。修改文件的所有者的命令是chown。
四、防止攻击的安全设置
1.限制用户滥用系统资源,主要包括资源最大进程数,内存使用量等.这样可以防止DOS类型攻击,需要编辑文件
[root@tp /]# vi /etc/security/limits.conf
...
(这三行是添加的)
* hard core 0 禁止创建core文件
* hard rss 5000 其他用户(除root)最多使用5M内存
* hard nproc 20 最多进程数限制在20
注:*表示所有登陆到linux的用户.
# End of file
[root@tp /]# vi /etc/pam.d/login
...
在文件末尾加入下面一行
session required /lib/security/pam_limits.so
2.限制控制台的访问
[root@tp /]# vi /etc/securetty
...
注释掉除tty1以外的控制台终端,这时root仅可在tty1终端登录。
tty1
# tty2
# tty3
# tty4
# tty5
# tty6
3.禁止外来ping请求.
[root@tp /]# vi /etc/rc.d/rc.local
...
在最后加入一行
echo 1 > /proc/sys/net/ipv4/icmp_echo_ignore_all
4.防止IP地址欺骗
[root@tp /]# vi /etc/host.conf
加入如下几行
order bind,hosts
multi off
nospoof on
5.禁止su命令进入root
[root@tp pam.d]# vi /etc/pam.d/su
...
在下面加入如下两行
auth sufficient /lib/security/pam_rootok.so debug
auth required /lib/security/pam_wheel.so group=xxx
这表示只有xxx组的用户可以su成root.
6.使用TCP_WRAPPER
在默认情况下linux系统允许所有请求,可用TCP_WRAPPER增强安全性,在/etc/hosts.deny写入"ALL:ALL"禁止所有请求
[root@tp etc]# vi /etc/hosts.deny
#
# hosts.deny This file describes the names of the hosts which are
# *not* allowed to use the local INET services, as decided
# by the '/usr/sbin/tcpd' server.
#
# The portmap line is redundant, but it is left to remind you that
# the new secure portmap uses hosts.deny and hosts.allow. In particular
# you should know that NFS uses portmap!
"ALL:ALL"
把允许访问的客户或服务添加到/etc/hosts.allow,冒号左边为服务,冒号右边为授权的机器。
[root@tp etc]# vi /etc/hosts.allow
#
# hosts.allow This file describes the names of the hosts which are
# allowed to use the local INET services, as decided
# by the '/usr/sbin/tcpd' server.
#
vsftp:211.101.46.253
注:仅允许IP地址为211.101.46.253的机器访问FTP服务器。
7.删减登录信息
[root@tp ~]# rm -f /etc/issue
[root@tp ~]# rm -f /etc/
[root@tp ~]# touch /etc/issue
[root@tp ~]# touch /etc/
五、确保开启服务的安全性
首先看一下自己系统开启了多少服务。
[root@tp ~]# ps -eaf | wc -l
进程个数
我们可以通过查看当前运行的进程来了解一下系统打开了哪些服务:
[root@tp ~]# ps -aux
Warning: bad syntax, perhaps a bogus '-'? See /usr/share/doc/procps-3.2.3/FAQ
:以上为系统提示,可忽略
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.2 2592 560 ? S 21:02 0:00 init[3]
root 2 0.0 0.0 0 0 ? SN 21:02 0:00 [ksoftirqd/0]
root 3 0.0 0.0 0 0 ? S< 21:02 0:00 [events/0]
root 4 0.0 0.0 0 0 ? S< 21:02 0:00 [khelper]
root 5 0.0 0.0 0 0 ? S< 21:02 0:00 [kacpid]
root 20 0.0 0.0 0 0 ? S< 21:02 0:00 [kblockd/0]
root 30 0.0 0.0 0 0 ? S 21:02 0:00 [pdflush]
root 31 0.0 0.0 0 0 ? S 21:02 0:00 [pdflush]
root 33 0.0 0.0 0 0 ? S< 21:02 0:00 [aio/0]
root 21 0.0 0.0 0 0 ? S 21:02 0:00 [khubd]
root 32 0.0 0.0 0 0 ? S 21:02 0:00 [kswapd0]
root 107 0.0 0.0 0 0 ? S 21:02 0:00 [kseriod]
root 181 0.0 0.0 0 0 ? S< 21:03 0:00 [kmirrord]
root 182 0.0 0.0 0 0 ? S< 21:03 0:00 [kmir_mon]
root 190 0.0 0.0 0 0 ? S 21:03 0:00 [kjournald]
root 1085 0.0 0.1 2604 444 ? S<s 21:03 0:00 udevd
root 1611 0.0 0.0 0 0 ? S< 21:03 0:00 [kauditd]
root 1745 0.0 0.0 0 0 ? S< 21:03 0:00 [kmpathd/0]
root 1769 0.0 0.0 0 0 ? S 21:03 0:00 [kjournald]
root 2250 0.0 0.2 2668 632 ? Ss 21:03 0:00 syslogd -m 0
root 2254 0.0 0.1 3352 472 ? Ss 21:03 0:00 klogd -x
rpc 2274 0.0 0.2 2220 572 ? Ss 21:03 0:00 portmap
rpcuser 2294 0.0 0.2 2108 756 ? Ss 21:03 0:00 rpc.statd
root 2322 0.0 0.3 5344 992 ? Ss 21:03 0:00 rpc.idmapd
root 2399 0.0 0.3 2612 816 ? S 21:03 0:00 /usr/sbin/smartd
root 2409 0.0 0.2 3176 540 ? Ss 21:03 0:00 /usr/sbin/acpid
root 2440 0.0 1.4 11192 3680 ? Ss 21:03 0:00 cupsd
root 2497 0.0 0.6 5044 1712 ? Ss 21:03 0:00 /usr/sbin/sshd
root 2526 0.0 0.3 2760 876 ? Ss 21:03 0:00 xinetd -stayalive -pidfile /var/run/xinetd.pid
root 2536 0.0 0.2 1788 528 ? Ss 21:03 0:00 gpm -m /dev/input/mice -t imps2
htt 2565 0.0 0.1 1960 316 ? Ss 21:03 0:00 /usr/sbin/htt -retryonerror 0
htt 2566 0.0 1.1 8256 3024 ? S 21:03 0:00 htt_server -nodaemon
canna 2578 0.0 6.8 19932 17628 ? Ss 21:03 0:00 /usr/sbin/cannaserver -syslog -u canna
root 2590 0.0 0.4 7428 1204 ? Ss 21:03 0:00 crond
xfs 2628 0.0 1.3 5692 3332 ? Ss 21:03 0:00 xfs -droppriv -daemon
root 2638 0.0 0.2 2092 640 ? SNs 21:03 0:00 anacron -s
root 2647 0.0 0.2 3712 740 ? Ss 21:03 0:00 /usr/sbin/atd
dbus 2657 0.0 0.5 13296 1324 ? Ssl 21:03 0:00 dbus-daemon-1 --system
root 2668 0.0 0.4 3156 1040 ? Ss 21:03 0:00 cups-config-daemon
root 2679 0.0 1.7 6540 4424 ? Ss 21:03 0:00 hald
root 2688 0.0 0.5 2916 1288 ? Ss 21:03 0:00 login -- root
root 2689 0.0 0.1 1528 404 tty2 Ss+ 21:03 0:00 /sbin/mingetty tty2
root 2690 0.0 0.1 2048 404 tty3 Ss+ 21:03 0:00 /sbin/mingetty tty3
root 2691 0.0 0.1 3488 404 tty4 Ss+ 21:03 0:00 /sbin/mingetty tty4
root 2692 0.0 0.1 2368 404 tty5 Ss+ 21:03 0:00 /sbin/mingetty tty5
root 2693 0.0 0.1 3296 404 tty6 Ss+ 21:03 0:00 /sbin/mingetty tty6
root 3136 0.0 0.5 5920 1396 tty1 Ss+ 21:05 0:00 -bash
root 3574 0.0 0.8 8400 2276 ? Ss 21:05 0:00 sshd: root@pts/0
root 3576 0.0 0.5 6896 1388 pts/0 Ss 21:05 0:00 -bash
root 3608 0.0 0.4 6584 1216 pts/0 S+ 21:05 0:00 ntsysv
root 4019 0.0 0.8 8408 2276 ? Rs 21:09 0:00 sshd: root@pts/1
root 4021 0.0 0.5 6912 1388 pts/1 Ss 21:09 0:00 -bash
root 4084 0.0 0.2 2852 748 pts/1 R+ 21:17 0:00 ps -aux
以上进程或服务都是开机自动加载的。还可以用其它命令来看一下:
[root@tp ~]# ntsysv (tab键切换,退出)
…………
显示结果中那些前面有*号的就是开机自动启动的服务,和windows系统中一样,没用的服务可以关掉。运行的服务越少,系统就越安全。
查看哪些服务正在运行的命令还有:
[root@tp ~]# netstat -an
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 0.0.0.0:32768 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:113 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN
tcp 0 0 :::80 :::* LISTEN
tcp 0 0 :::22 :::* LISTEN
tcp 0 0 :::443 :::* LISTEN
tcp 0 880 ::ffff:192.168.0.1:22 ::ffff:192.168.0.5:2683 ESTABLISHED
udp 0 0 0.0.0.0:32768 0.0.0.0:*
udp 0 0 0.0.0.0:111 0.0.0.0:*
udp 0 0 0.0.0.0:631 0.0.0.0:*
udp 0 0 0.0.0.0:764 0.0.0.0:*
带有LISTEN的代表正在开启的端口,开启的服务。
以下linux系统的启动过程做一个简单介绍。首先进入下面目录,
[root@tp ~]# cd /etc/rc.d
[root@tp rc.d]# ls
init.d rc rc0.d rc1.d rc2.d rc3.d rc4.d rc5.d rc6.d rc.local rc.sysinit
如果你的系统是图形化界面启动的话,则运行级别是5,自启动服务配置文件目录是rc5.d。多用户模式非图形界面启动,运行级别是3,配置文件目录是rc3.d。是什么模式就进入相应目录,例如:
[root@tp rc.d]# cd rc3.d
[root@tp rc3.d]# ls
K01yum K16rarpd K35cyrus-imapd K50snmptrapd K84bgpd K96ipmi S13irqbalance S55sshd K02NetworkManager K20bootparamd K35dhcpd K50tux K84ospf6d K99readahead S13portmap
S56rawdevices K03rhnsd K20netdump-server K35smb K50vsftpd K84ospfd K99readahead_early
S14nfslock S56xinetd K05atd K20nfs K35vncserver K54dovecot K84ripd S00microcode_ctl
S15mdmonitor S85gpm K05innd K20rstatd K35winbind K61ldap K84ripngd S01sysstat
S18rpcidmapd S87iiim K05saslauthd K20rusersd K36dhcp6s K65kadmin K85mdmpd S05kudzu
S19rpcgssd S90canna K10dc_server K20rwhod K36lisa K65kprop K85zebra S06cpuspeed S25netfs
S90crond K10psacct K24irdaK36mysqld K65krb524 K87auditd S08arptables_jf S26apmd
S95anacron K10radiusd K25squid K36postgresql K65krb5kdc K87multipathd S08ip6tables
S26lm_sensors S97messagebus K10xfs K28amd K45arpwatch K73ypbind K88opensm S08iptables S28autofs S98cups-config-daemon K12dc_client K30sendmail K45named K74nscd K89iscsi S09isdn S40smartd S98haldaemon K12FreeWnn K30spamassassin K46radvd K74ntpd K89netplugd S09pcmcia S44acpid S99local K12mailman K34dhcrelay K50netdump K74ypserv K90bluetooth
S10network S54hpoj K15httpd K34yppasswdd K50snmpd K74ypxfrd K94diskdumpS12syslog S55cups
linux在开机时会读取/etc/rc.d/rc*.d(*是运行级别)
终止K开头的服务,开启S开头的服务。通过ntsysv命令所做的更改都会在这里体现出来。重点是要决定开启和关闭哪些服务。
下面列出每项服务的具体功能,可以根据自己的需要来决定。
amd:自动安装NFS(网络文件系统)守侯进程
apmd:高级电源管理
arpwatch:记录日志并构建一个在LAN接口上看到的以太网地址和IP地址对数据库
atd 运行用户用At命令调度的任务。也在系统负荷比较低时 运行批处理任务。
autofs:自动安装管理进程automount,与NFS相关,依赖于NIS
bootparamd:引导参数服务器,为LAN上的无盘工作站提供引导所需的相关信息
crond:Linux下的计划任务
dhcpd:启动一个DHCP(动态IP地址分配)服务器
gated:网关路由守候进程,使用动态的OSPF路由选择协议
gpm gpm为文本模式下的Linux程序如mc(Midnight Commander)提供了
鼠标的支持。它也支持控制台鼠标的拷贝,粘贴操作以及弹出式菜单。
httpd:WEB服务器
inetd:支持多种网络服务的核心守候程序
innd:Usenet新闻服务器
keytable 该程序的功能是转载您在/etc/sysconfig/keyboards里说明的键盘
映射表,该表可以通过kbdconfig工具进行选 择。您应该使该程序
处于激活状态。
ldap LDAP代表Lightweight Directory Access Protocol, 实现了目录
访问协议的行业标准。
linuxconf:允许使用本地WEB服务器作为用户接口来配置机器
lpd:打印服务器
mars-nwe:mars-nwe文件和用于Novell的打印服务器
mcserv Midnight Commander服务进程允许远程机器上的用户通过Midnight
commander文件管理器操作本机文件。服务进程用PAM来验证用户,
需要给出“用户名/口令”以通过验证
named:DNS服务器
netfs:安装NFS、Samba和NetWare网络文件系统
network:激活已配置网络接口的脚本程序
nfs:打开NFS服务
nscd:nscd(Name Switch Cache daemon)服务器,用于NIS的一个支持服务,它高速缓存用户口令和组成成员关系
pcmcia pcmcia主要用于支持笔记本电脑
portmap:RPC portmap管理器,与inetd类似,它管理基于RPC服务的连接
postgresql:一种SQL数据库服务器
random 保存和恢复系统的高质量随机数生成器,这些随机数是系统一些随
机行为提供的
routed:路由守候进程,使用动态RIP路由选择协议
rstatd:一个为LAN上的其它机器收集和提供系统信息的守候程序
ruserd:远程用户定位服务,这是一个基于RPC的服务,它提供关于当前记录到LAN上一个机器日志中的用户信息
rwalld:激活rpc.rwall服务进程,这是一项基于RPC的服务,允许用户给每个注册到LAN机器上的其他终端写消息
rwhod:激活rwhod服务进程,它支持LAN的rwho和ruptime服务
sendmail:邮件服务器sendmail
smb:Samba文件共享/打印服务
snmpd:本地简单网络管理候进程
squid:激活代理服务器squid
syslog:一个让系统引导时起动syslog和klogd系统日志守候进程的脚本
webmin webmin是基于web的集系统管理与网络管理于一身的强大管理工具。
利用webmin的强大功能,用户可以通过web浏览器来方便地设置自己的服务器、dns、samba、nfs、本地/远程文件系统以及许多其他的系统配置。
xfs:X Window字型服务器,为本地和远程X服务器提供字型集
xntpd:网络时间服务器
ypbind:为NIS(网络信息系统)客户机激活ypbind服务进程
yppasswdd:NIS口令服务器
ypserv:NIS主服务器
gpm:鼠标管理服务
identd:AUTH服务,在提供用户信息方面与finger类似
六、安全日志
这里只介绍如何通过日志来查看那些可疑的用户登陆过机器,并不详细介绍日志其它方面的知识。以下是三个重要的日志文件:
/var/log/wtmp 记录每个用户登陆和退出时间的永久记录。
/var/run/utmp 记录当前登陆到系统的每个用户信息。
/var/log/lastlog 每个用户最后一次登陆的信息(最新的信息)
wtmp和utmp都是二进制文件,它们要用命令来查看内容。
1.命令who可查看utmp文件当前的每个用户的信息,它默认输出包括用户名,终端类型,登陆时间及远程登录主机IP。
如下:
[root@tp log]# who
root pts/0 May 4 22:10 (192.168.0.5)
如果指明了文件,则显示自wtmp文件被创建以来所有登陆的用户信息。
[root@tp log]# who /var/log/wtmp
root tty1 May 4 20:44
root pts/0 May 4 20:52 (211.101.46.195)
root tty1 May 4 21:05
root pts/0 May 4 21:05 (211.101.46.195)
root pts/1 May 4 21:09 (192.168.0.5)
root pts/0 May 4 21:38 (192.168.0.5)
root pts/0 May 4 22:10 (192.168.0.5)
2.命令w,查看utmp文件并显示当前系统中每个用户和它所运行的进程信息.
如:
[root@tp log]# w
23:00:48 up 54 min, 1 user, load average: 0.00, 0.00, 0.00
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root pts/0 192.168.0.5 22:10 0.00s 0.03s 0.00s w
3.users,显示当前当前登陆的用户数量.如,
[root@tp log]# users
root root
这表明两个root用户在同时登陆这台机器.
4.last命令,用来显示wtmp文件第一次创建以来所有登陆过的用户.
如:
[root@tp log]# last
root pts/1 192.168.0.5 Fri May 4 23:01 - 23:02 (00:00)
root pts/0 192.168.0.5 Fri May 4 22:10 still logged in
reboot system boot 2.6.9-34.EL Fri May 4 22:07 (00:59)
root pts/0 192.168.0.5 Fri May 4 21:38 - down (00:27)
reboot system boot 2.6.9-34.EL Fri May 4 21:36 (00:29)
root pts/1 192.168.0.5 Fri May 4 21:09 - down (00:25)
root pts/0 211.101.46.195 Fri May 4 21:05 - down (00:29)
root tty1
展开阅读全文