Linux 安全加固

什么是安全加固、为什么需要进行安全加固、加固后有用吗?不做任何说明。

安全加固列表

  1. BIOS
  2. SSH安全
  3. 账户安全
  4. 禁用telnet
  5. TCPwrappers
  6. root通知
  7. history安全
  8. 禁用所有特殊账户
  9. chmod危险文件
  10. 指定允许root登陆的TTY设备
  11. 检查Rootkit
  12. 安装补丁
  13. 隐藏Apache信息
  14. 隐藏php信息
  15. 关闭不使用的服务
  16. 检测监听中的端口
  17. 关闭打开的端口和服务
  18. 删除不用的rpm包
  19. 禁用危险的php函数
  20. 内核加固(sysctl.conf)
  21. ./tmp /var/tmp,/dev/shm分区安全

加固方法

  1. BIOS
    设置BIOS密码,禁用从CD-ROM和软盘引导,防止一些人未经允许访问你的系统和更改BIOS设置。
  2. SSH安全
    1)禁止root用户登录
    2)更改默认端口
    3)使用秘钥认真登陆
    修改配置文件说明:/etc/ssh/sshd_config
    Port xx
    PermitRootLogin no
    StrictModes no
    RSAAuthentication yes
    PubkeyAuthentication yes
    PermitEmptyPasswords no
    PasswordAuthentication no
    
  3. 账户安全(参考连接:账户安全加固
    1)设置密码策略

    修改 /etc/login.defs 配置文件
     PASS_MAX_DAYS      90                密码最长有效期
     PASS_MIN_DAYS      10                密码修改之间最小的天数
     PASS_MIN_LEN        8                密码长度
     PASS_WARN_AGE       7                口令失效前多少天开始通知用户修改密码
    

    2)对用户密码强度的设定
    ```c
    修改 /etc/pam.d/sysetm-auth(设置新密码不能和旧密码相同,同时新密码至少8位,还要同时包含大字母、小写字母和数字等。)
    password requisite pam_pwquality.so try_first_pass local_users_only retry=3 authtok_type= difok=1 minlen=8 ucredit=-1 lcredit=-1 dcredit=-1

    difok= :此选项用来定义新密码中必须要有几个字符和旧密码不同
    minlen=:此选项用来设置新密码的最小长度
    ucredit= :此选项用来设定新密码中可以包含的大写字母的最大数目。-1 至少一个
    lcredit=:此选项用来设定新密码中可以包含的小写字母的最大数目
    dcredit=:此选项用来设定新密码中可以包含的数字的最大数目

注:这个密码强度的设定只对普通用户有限制作用,root用户无论修改自己的密码还是修改普通用户的时候,不符合强度设置依然可以设置成功

    3)对用户的登录次数进行限制
```c
有一些攻击性的软件是专门采用暴力破解密码的形式反复进行登录尝试,对于这种情况,我们可以调整用户登录次数限制,使其密码输入3次后自动锁定,并且设置锁定时间,在锁定时间内即使密码输入正确也无法登录。

    配置/etc/pam.d/sshd  文件,在 #%PAM-1.0 的下面,加入下面的内容,表示当密码输入错误达到3次,就锁定用户150秒,如果root用户输入密码错误达到3次,锁定300秒。锁定的意思是即使密码正确了也登录不了
    配置/etc/pam.d/sshd : auth required pam_tally2.so deny=3 unlock_time=150 even_deny_root root_unlock_time300
4)设置历史命令保存条数和账户超时时间

设置账户保存历史命令条数,超时时间 。打开 /etc/profile ,修改如下```c
设置账户保存历史命令条数,超时时间。
修改及添加 配置文件 /etc/profile参数:
HISTSIZE(设置历史命令保存条数),TMOUT(设置账户自动注销时间)


4. 禁用telnet
    早期的Linux默认开启telnet服务,telnet,ftp,rlogin都是明文传输的协议是容易被嗅探到的,这就是为什么推荐使用安全的版本(sftp,scp,ssh)的原因如果你必须要使用telnet,那么至少应该隐藏banner信息。
```c
修改配置文件:/etc/xinetd.d/telnet
    设置:disable=yes

5.TCP wrappers
编辑hosts.allow和hosts.deny可以限制或允许访问inet服务。

限制访问inet服务
修改/etc/hosts.allow
建议格式:
    #Approved IP addresses
    ALL:192.168.0.1
    ALL:192.168.5.2
    #CSV uploader machine
    proftpd:10.0.0.5
    #pop3 from antwhere
    ipop3:ALL
修改/etc/hosts.deny
    ALL:ALL EXCEPT localhostENY

6.root通知
当一个具有root权限的用户登录的时候发mail

编辑/root下的.bashrc ,当有root权限的用户登录时发生email通知

    echo ‘ALERT ? Root Shell Access (Server Name) on:’ `date` `who` | mail -s “Alert: Root Access from `who | cut -d”(” -f2 | cut -d”)” -f1`” your@email.com

7.history安全
这是一个避免删除.bash_history或重定向到/dev/null的好主意,因此他不能清除或删除他最后执行的命令。

方法:
    chattr +a .bash_history
    chattr +i .bash_history
获取用户的人会知道他的历史命令锁定并且要同意才可以使用服务

8.禁用所有特殊账户
从系统中删除所有默认用户和组;例如news,lp,sync,shutdown,uucp,games,halt 等

方法:
    删除账户userdel name
    删除组 groupdel name
    锁定特定账户:
    /usr/sbin/usermod -L -s /bin/false user

9.chmod危险文件
限制不具有root权限的用户执行下面这些命令的好主意

方法:
    chmod 700 /bin/ping
    chmod 700 /usr/bin/finger
    chmod 700 /usr/bin/who
    chmod 700 /usr/bin/w
    chmod 700 /usr/bin/locate
    chmod 700 /usr/bin/whereis
    chmod 700 /sbin/ifconfig
    chmod 700 /usr/bin/pico
    chmod 700 /usr/bin/vi
    chmod 700 /usr/bin/which
    chmod 700 /usr/bin/gcc
    chmod 700 /usr/bin/make
    chmod 700 /bin/rpm

10.指定允许root登陆的TTY设备
/etc/securetty文件允许你指定root可以从哪个TTY设备登录

方法:
    vi /etc/securetty
只留2个连接:
    tty1
    tty2

11.检测Rootkit
用chkrootkit或rkhunter,以chkrootkit为例

方法:
    wget ftp://ftp.pangeia.com.br/pub/seg/pac/chkrootkit.tar.gz
    wget ftp://ftp.pangeia.com.br/pub/seg/pac/chkrootkit.md5

1、首先检查md5校验值: md5sum chkrootkit.tar.gz,然后解压安装
    tar -zxvf chkrootkit.tar.gz
    cd chkrootkit
    ./configure
    make sense
2、然后运行./chkrootkit/
3、我们可以将其添加到contrab使其每天自动扫描:
    vi /etc/cron.daily/chkrootkit.sh
    #!/bin/bash
    # 输入chkrootkit的安装目录
4、cd /root/chkrootkit/
    # 输入你想收到检测报告的email
5、 ./chkrootkit | mail -s “Daily chkrootkit from Server Name” your@email.com

12.安装补丁
你要经常检查更新以修复某些缺陷或系统稳定性的改进;否则你存在漏洞的系统将会不时的遭受新的攻击。

方法:
    列出可用更新:up2date -l
    安装未排除的更新:up2date -u
    安装包括排除的更新up2date -uf

13.隐藏Apache信息
隐藏Apache的banner信息使攻击者不知道Apache的版本,从而使他们难以利用漏洞。

方法:
修改/etc/httpd/conf/httpd.conf
改变服务器签名:
    ServerSignature Off
重启Apache /sbin/service httpd restart

14.隐藏php信息
隐藏php的banner信息,原因同上

方法:
修改php.ini
改变
    expose_php=Off
重启Apache

15.关闭不用的服务
把任何未使用的服务关闭,可以在/etc/xinetd.d文件夹里找到。

方法:
    cd /etc/xinetd.d
    grep disable *

这将显示所有服务开启或关闭的状态,然后根据需要来开启或关闭服务

16.检测监听的端口
检测是否有必要开放端口是非常重要的

方法:
    netstat -tulp或
    lsof -i -n | egrep ‘COMMAND|LISTEN|UDP’或
    nmap

17.关闭端口和服务
重点是关闭在系统启动时打开的不需要的端口

方法:
对于正在运行的服务,可以执行chkconfig -list | grep on
禁用服务可以执行chkconfig servicename off
然后停止正在运行的服务:/etc/init.d/service stop

18.删除不用的rpm包
首先应该清楚你的系统的作用,它是web,mail,file服务器或其他,然后觉得哪些包是必要的,之后删除不需要的软件包。

方法:
首先列出安装列表rpm -qa
更详细的信息rpm -qi rpmname
还可以检测删除包可能出现的冲突rpm -e ?test rpmname

19.禁用危险的php函数
禁用php的危险函数防止在网站上执行系统命令

方法:
    whereis php.ini
    vi /usr/local/lib/php.ini
编辑
    disable_functions = “symlink,shell_exec,exec,proc_close,proc_open,popen,
    system,dl,passthru,escapeshellarg, escapeshellcmd”
  1. 安装配置防火墙
    高级策略防火墙(APF)是一种IP表(网络过滤),它是基于当今互联网部署服务器防火墙系统的基本需要和客户部署LINUX安装的唯一需要而设计的。 它是最好的开源防。
    21.内核加固(sysctl.conf)
    sysctl.conf用来加固内核,目的是避免DOS和欺骗攻击
    ```c
    方法:
    到/proc/sys目录或sysctl -a命令了解下当前配置的大概情况
    然后vi /etc/sysctl.conf
    添加如下内容:

    !Kernel sysctl configuration file for Red Hat Linux
    
    ! For binary values, 0 is disabled, 1 is enabled. See sysctl(8) and
    !sysctl.conf(5) for more details.
    ! Controls IP packet forwarding
    net.ipv4.ip_forward = 0
    ! Controls source route verification
    net.ipv4.conf.default.rp_filter = 1
    ! Controls the System Request debugging functionality of the kernel
    kernel.sysrq = 0
    ! Controls whether core dumps will append the PID to the core filename.
    ! Useful for debugging multi-threaded applications.
    kernel.core_uses_pid = 1
    !Prevent SYN attack
    net.ipv4.tcp_syncookies = 1
    net.ipv4.tcp_max_syn_backlog = 2048
    net.ipv4.tcp_synack_retries = 2
    ! Disables packet forwarding
    net.ipv4.ip_forward=0
    ! Disables 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 IP spoofing protection, turn on source route verification
    net.ipv4.conf.all.rp_filter = 1
    net.ipv4.conf.lo.rp_filter = 1
    net.ipv4.conf.eth0.rp_filter = 1
    net.ipv4.conf.default.rp_filter = 1
    ! Disable ICMP Redirect Acceptance
    net.ipv4.conf.all.accept_redirects = 0
    net.ipv4.conf.lo.accept_redirects = 0
    net.ipv4.conf.eth0.accept_redirects = 0
    net.ipv4.conf.default.accept_redirects = 0
    ! 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
    ! Disables 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 IP spoofing protection, turn on source route verification
    net.ipv4.conf.all.rp_filter = 1
    net.ipv4.conf.lo.rp_filter = 1
    net.ipv4.conf.eth0.rp_filter = 1
    14
    net.ipv4.conf.default.rp_filter = 1
    ! Disable ICMP Redirect Acceptance
    net.ipv4.conf.all.accept_redirects = 0
    net.ipv4.conf.lo.accept_redirects = 0
    net.ipv4.conf.eth0.accept_redirects = 0
    net.ipv4.conf.default.accept_redirects = 0
    ! Disables the magic-sysrq key
    kernel.sysrq = 0
    ! Modify system limits for Ensim WEBppliance
    fs.file-max = 65000
    ! Decrease the time default value for tcp_fin_timeout connection
    net.ipv4.tcp_fin_timeout = 15
    ! Decrease the time default value for tcp_keepalive_time connection
    net.ipv4.tcp_keepalive_time = 1800
    ! Turn off the tcp_window_scaling
    net.ipv4.tcp_window_scaling = 0
    ! Turn off the tcp_sack
    net.ipv4.tcp_sack = 0
    ! Turn off the tcp_timestamps
    net.ipv4.tcp_timestamps = 0
    ! Enable TCP SYN Cookie Protection
    net.ipv4.tcp_syncookies = 1
    ! Enable ignoring broadcasts request
    net.ipv4.icmp_echo_ignore_broadcasts = 1
    ! Enable bad error message Protection
    net.ipv4.icmp_ignore_bogus_error_responses = 1
    ! Log Spoofed Packets, Source Routed Packets, Redirect Packets
    net.ipv4.conf.all.log_martians = 1
    ! Set maximum amount of memory allocated to shm to 256MB
    kernel.shmmax = 268435456
    ! Improve file system performance
    vm.bdflush = 100 1200 128 512 15 5000 500 1884 2
    ! Improve virtual memory performance
    vm.buffermem = 90 10 60
    ! Increases the size of the socket queue (effectively, q0).
    net.ipv4.tcp_max_syn_backlog = 1024
    ! Increase the maximum total TCP buffer-space allocatable
    net.ipv4.tcp_mem = 57344 57344 65536
    ! Increase the maximum TCP write-buffer-space allocatable
    net.ipv4.tcp_wmem = 32768 65536 524288
    15
    ! Increase the maximum TCP read-buffer space allocatable
    net.ipv4.tcp_rmem = 98304 196608 1572864
    ! Increase the maximum and default receive socket buffer size
    net.core.rmem_max = 524280
    net.core.rmem_default = 524280
    ! Increase the maximum and default send socket buffer size
    net.core.wmem_max = 524280
    net.core.wmem_default = 524280
    ! Increase the tcp-time-wait buckets pool size
    net.ipv4.tcp_max_tw_buckets = 1440000
    ! Allowed local port range
    net.ipv4.ip_local_port_range = 16384 65536
    ! Increase the maximum memory used to reassemble IP fragments
    net.ipv4.ipfrag_high_thresh = 512000
    net.ipv4.ipfrag_low_thresh = 446464
    ! Increase the maximum amount of option memory buffers
    net.core.optmem_max = 57344
    ! Increase the maximum number of skb-heads to be cached
    net.core.hot_list_length = 1024
    !! DO NOT REMOVE THE FOLLOWING LINE!
    !! nsobuild:20051206

重启后生效
/sbin/sysctl -p
sysctl -w net.ipv4.route.flush=1

22../tmp,/var/tmp,/dev/shm分区的安全
    /tmp,/var/tmp,/dev/shm目录是不安全的,任何用户都可以执行脚本。最好的解决办法是挂载ncexec和nosuid选项的参数
```c
方法:
/tmp目录:
cd /dev
创建 100M (“count”) 的存储文件:
dd if=/dev/zero of=tmpMnt bs=1024 count=100000
设为一个扩展的文件系统:
    /sbin/mke2fs /dev/tmpMnt (“…is not a block special device. continue?”回答yes)
备份现有临时文件:
    cp -R /tmp/ /tmp_backup
用noexec挂载新文件系统:
    mount -o loop,rw,nosuid,noexec /dev/tmpMnt /tmp
    chmod 0777 /tmp
把备份的文件拷贝回去:
    cp -R /tmp_backup/* /tmp/
删除备份:
    rm -rf /tmp_backup
修改/etc/fstab 添加下面的条目使其在引导时仍然有效
    /dev/tmpMnt /tmp ext2 loop,rw,nosuid,noexec 0 0

/var/tmp目录:
    mv /var/tmp /var/tmpbak
    ln -s /tmp /var/tmp
    cp /var/tmpbak/* /tmp/

/dev/shm目录:
编辑/etc/fstab
把 none /dev/shm tmpfs defaults,rw 0 0
改为
    none /dev/shm tmpfs defaults,nosuid,noexec,rw 0 0

免费领取安全学习资料包!

渗透工具

技术文档、书籍

 

面试题

帮助你在面试中脱颖而出

视频

基础到进阶

环境搭建、HTML,PHP,MySQL基础学习,信息收集,SQL注入,XSS,CSRF,暴力破解等等

 

应急响应笔记

学习路线

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值