A-A+

CentOS服务器安全配置策略和命令行

2016年02月20日 站长资讯 暂无评论

近期服务器频繁有被入侵的,大致分析了一下入侵行为,整理了需要做的安全策略:

管理机端设置:

1.跳板机权限只开放运维人员

a.运维人员密码必须重新设置,密码长度不小于8位

b.密码设置必须要有大小写,数字,特殊字符组合。

c.强制90天更改密码

d.设置密码避免重复使用

e.个人账户设定登录失败次数为6次,一旦超过6次,将会锁定账号。

2.跳板机禁止root登录,每周update

3.禁止在跳板机上保存登录密码,ip列表等信息,将history记录默认设置为500条。

4.修改PPTP vpn服务器账户密码,定期更新补丁。

服务端设置:

a.登录段配置同之前配置。

b.定期修改密码。

c.服务器端每周update补丁。

d.禁用root直接登录,创建运维人员单独账号。必要时使用sudo成为root进行管理

e.服务器禁止ping操作,增加安全性。

d.隐藏系统版本号,防止版本信息泄露

d.优化sysct.conf预防DDoS攻击

e.定时检查主要配置文件,系统主要目录是否有异常,可通过MD5校验如发现有变动重新分发一份。如/etc/passwd /etc/shadow /etc/group /etc/sysctl.conf, /bin /sbin等。

具体细节如下:

  1. #设定用户90天修改密码,提前7天提醒  
  2. UserList=$(ls /home/|awk '{print $NF}'|grep -v lost+found)  
  3. for user in  $UserList  
  4. do  
  5.         chage -M 90 -W 7 $user  
  6. done  
  7. #禁ping  
  8. echo 1 > /proc/sys/net/ipv4/icmp_echo_ignore_all  
  9. #设定用户过期时间90默认密码长度8位  
  10. cp /etc/login.defs /etc/login.defs.bak  
  11. sed -i '/PASS_MIN_LEN/s/[0-9]\{1,6\}/90/' /etc/login.defs  
  12. sed -i '/PASS_MIN_LEN/s/[0-9]\{1,3\}/8/' /etc/login.defs  
  13. #设定用户登录,普通用户登录识别超过6次锁定300s.  
  14. echo "account required pam_tally.so deny=100 no_magic_root reset" >>/etc/pam.d/system-auth  
  15. echo "auth required pam_tally.so onerr=fail deny=6 unlock_time=300>>/etc/pam.d/system-auth   
  16. #隐藏系统版本号  
  17. mv /etc/issue /etc/isseu  
  18. mv /etc/issue.net  /etc/isseu.net  
  19. mv /etc/redhat-release /etc/rehdat-release  
  20. #优化内核参数  
  21. echo '  
  22. # Kernel sysctl configuration file for Red Hat Linux  
  23. #  
  24. # For binary values, 0 is disabled, 1 is enabled.  See sysctl(8) and  
  25. # sysctl.conf(5) for more details.  
  26.     
  27. # Controls IP packet forwarding  
  28. net.ipv4.ip_forward = 0  
  29.     
  30. # Controls source route verification  
  31. net.ipv4.conf.default.rp_filter = 1  
  32.     
  33. # Do not accept source routing  
  34. net.ipv4.conf.default.accept_source_route = 0  
  35.     
  36. # Controls the System Request debugging functionality of the kernel  
  37. kernel.sysrq = 0  
  38.     
  39. # Controls whether core dumps will append the PID to the core filename  
  40. # Useful for debugging multi-threaded applications  
  41. kernel.core_uses_pid = 1  
  42.     
  43. # Controls the use of TCP syncookies   
  44. net.ipv4.tcp_syncookies = 1  
  45.     
  46. # Controls the maximum size of a message, in bytes  
  47. kernel.msgmnb = 65536  
  48.     
  49. # Controls the default maxmimum size of a mesage queue  
  50. kernel.msgmax = 65536  
  51.     
  52. # Controls the maximum shared segment size, in bytes  
  53. kernel.shmmax = 68719476736  
  54.     
  55. # Controls the maximum number of shared memory segments, in pages  
  56. kernel.shmall = 4294967296  
  57. # ------------- Kernel Optimization -------------  
  58. net.ipv4.tcp_max_tw_buckets = 60000  
  59. net.ipv4.tcp_sack = 1  
  60. net.ipv4.tcp_window_scaling = 1  
  61. net.ipv4.tcp_rmem = 4096 87380 4194304  
  62. net.ipv4.tcp_wmem = 4096 16384 4194304  
  63. net.core.wmem_default = 8388608  
  64. net.core.rmem_default = 8388608  
  65. net.core.rmem_max = 16777216  
  66. net.core.wmem_max = 16777216  
  67. net.core.netdev_max_backlog = 262144  
  68. net.core.somaxconn = 262144  
  69. net.ipv4.tcp_max_orphans = 3276800  
  70. net.ipv4.tcp_max_syn_backlog = 262144   
  71. net.ipv4.tcp_timestamps = 0  
  72. net.ipv4.tcp_synack_retries = 1  
  73. net.ipv4.tcp_syn_retries = 1  
  74. net.ipv4.tcp_tw_recycle = 1  
  75. net.ipv4.tcp_tw_reuse = 1  
  76. net.ipv4.tcp_mem = 94500000 915000000 927000000  
  77. net.ipv4.tcp_fin_timeout = 1  
  78. net.ipv4.tcp_keepalive_time = 30  
  79. net.ipv4.ip_local_port_range = 1024 65000  
  80. net.ipv4.ip_conntrack_max = 655360  
  81. net.ipv4.netfilter.ip_conntrack_max =655360  
  82. net.ipv4.netfilter.ip_conntrack_tcp_timeout_established = 180 ' >>/etc/sysctl.conf  
  83. #生效  
  84. sysctl -p  
  85. #记录histtory日志  
  86. echo '  
  87. #history     
  88. export HISTTIMEFORMAT="%F %T `whoami` "  
  89. USER_IP=`who -u am i 2>/dev/null| awk '{print $NF}'|sed -e 's/[()]//g'`  
  90. HISTDIR=/usr/local/bin/.history    
  91. if [ -z $USER_IP ]     
  92. then    
  93. USER_IP=`hostname`     
  94. fi    
  95. if [ ! -d $HISTDIR ]     
  96. then    
  97. mkdir -p $HISTDIR     
  98. chmod 777 $HISTDIR     
  99. fi    
  100. if [ ! -d $HISTDIR/${LOGNAME} ]     
  101. then    
  102. mkdir -p $HISTDIR/${LOGNAME}     
  103. chmod 300 $HISTDIR/${LOGNAME}     
  104. fi    
  105. export HISTSIZE=4000    
  106. DT=`date +%Y%m%d_%H%M%S`     
  107. export HISTFILE="$HISTDIR/${LOGNAME}/${USER_IP}.history.$DT"  
  108. chmod 600 $HISTDIR/${LOGNAME}/*.history* 2>/dev/null '>>/etc/profile  
  109. #安全登录  
  110. /etc/hosts.allow  
  111. #  
  112. # hosts.allow   This file describes the names of the hosts which are  
  113. #               allowed to use the local INET services, as decided  
  114. #               by the '/usr/sbin/tcpd' server.  
  115. #  
  116.     
  117.     
  118. sshd:192.168.1.101  
  119.     
  120. /etc/hosts.deny  
  121. #  
  122. # hosts.deny    This file describes the names of the hosts which are   
  123. #               *not* allowed to use the local INET services, as decided  
  124. #               by the '/usr/sbin/tcpd' server.  
  125. #  
  126. # The portmap line is redundant, but it is left to remind you that  
  127. # the new secure portmap uses hosts.deny and hosts.allow.  In particular  
  128. # you should know that NFS uses portmap!  
  129.     
  130. sshd:all  
  131. #检查防火墙配置(开放指定用户地址登录|服务地址开放用户地址段)  
  132. iptables-save  
  133. #创建普通用户  
  134.   
  135. useradd mdf  
  136. echo -e 'pwd123456'|passwd mdf --stdin  
  137. #禁止root登录权限  
  138.   
  139. sed -i 's/#PermitRootLogin yes/PermitRootLogin no/g' /etc/ssh/sshd_config  
  140. #优化ssh链接慢问题  
  141.   
  142. sed -i 's/GSSAPIAuthentication yes/GSSAPIAuthentication no/' /etc/ssh/sshd_config  
  143. sed -i '/#UseDNS yes/a\UseDNS no' /etc/ssh/sshd_config  
  144. /etc/init.d/sshd restart  
标签:

给我留言