|
| 1 | +--- |
| 2 | +title: VPS被爬当肉鸡,谈谈安全管理(CentOS7) |
| 3 | +date: 2017-04-01 12:27:26 |
| 4 | +categories: |
| 5 | +- 环境搭建 |
| 6 | +tags: |
| 7 | +- CentOS |
| 8 | +- VPS |
| 9 | +permalink: VPS-Safety |
| 10 | +--- |
| 11 | + 用了一年多 VSP 进行 SS 科学上网,直到收到官网的发送`Service suspension notification`邮件,才意识到我的 VPS 在安全方面基本处于裸奔状态。登录服务器页面看到一个黄色的警告页面,说我利用 VSP 做代理去做 DDOS 攻击别人,需要暂停我的服务器。 |
| 12 | + |
| 13 | +<!-- more --> |
| 14 | + |
| 15 | + 作为一台有公网 IP 的服务器,每天都会至少被数以万计的爬虫掠过,被无穷的扫描器扫过,被很多的弱口令试探过。作为一台 Shadowsocks 服务器,如果你分享出去,可能被别人利用 BT 下载,入侵跳板,或者直接试探你的服务器。 |
| 16 | + |
| 17 | +## 基础知识 |
| 18 | + |
| 19 | +- 常用指令区域 |
| 20 | + |
| 21 | +```bash |
| 22 | +#更新系统 |
| 23 | +yum update |
| 24 | +#查询用户登录状态 |
| 25 | +who /var/log/wtmp |
| 26 | +#查询用户历史信息 |
| 27 | +su username // 切换用户 |
| 28 | +history // 查看历史操作 |
| 29 | +``` |
| 30 | + |
| 31 | +- 设置防火墙(添加8010端口--公开区域) |
| 32 | + |
| 33 | +```bash |
| 34 | +firewall-cmd --zone=public --permanent --add-port=8010/tcp |
| 35 | +firewall-cmd --reload |
| 36 | +firewall-cmd --zone=public --list-ports #查看是否添加成功 |
| 37 | +# 扩展 |
| 38 | +firewall-cmd --list-all-zones#查看所有区域的配置 |
| 39 | +firewall-cmd --permanent --zone=public --add-service=http |
| 40 | +firewall-cmd --permanent --zone=public --remove-service=http |
| 41 | +firewall-cmd --zone=public --list-service |
| 42 | +systemctl stop firewalld #停止防火墙服务 |
| 43 | +# 检测端口是否开启成功 |
| 44 | +ssh 54.11.21.33 -p 8010 -v # 前面的IP为服务器的IP |
| 45 | +``` |
| 46 | + |
| 47 | +- 安全管理`/etc/ssh/sshd_config` |
| 48 | + |
| 49 | + + 修改端口,不用22端口登录 |
| 50 | + + 禁止root登陆 |
| 51 | + + 禁止密码登陆,使用SSH Key指纹登陆 |
| 52 | + + 限制其他用户登陆 |
| 53 | + + 使用 Chroot SSHD 将 SFTP 用户局限于其自己的主目录 |
| 54 | + + 登陆IP限制 |
| 55 | + + 禁用基于主机的身份验证 |
| 56 | + + 删除不安全文件 |
| 57 | + + 设置自动掉线 |
| 58 | + + 非root启动ShadowSocks |
| 59 | + + 安装Tripwire每日扫描,发送邮件到邮箱 |
| 60 | + + 每日登陆检查 |
| 61 | + |
| 62 | + |
| 63 | +##安装Tripwire每日扫描,发送邮件 |
| 64 | + |
| 65 | +- 安装EPEL |
| 66 | + |
| 67 | +```bash |
| 68 | +yum install epel-release |
| 69 | +``` |
| 70 | + |
| 71 | +- 安装Tripwire |
| 72 | + |
| 73 | +```bash |
| 74 | +yum install tripwire |
| 75 | +``` |
| 76 | + |
| 77 | +- 创建密钥和数据库 |
| 78 | + |
| 79 | +```bash |
| 80 | +cd /etc/tripwire |
| 81 | +# 生成策略文件(原来的策略文件带有好多位置路劲) |
| 82 | +# 检查策略文件未知劲: tripwire --check | grep -w "Filename:" |
| 83 | + git clone .... |
| 84 | + perl generateTwpol.pl twpol.txt > twpol.txt.new |
| 85 | +# |
| 86 | +# 生成秘钥(记住密码,修改加密配置文件需要输入密码) |
| 87 | + tripwire-setup-keyfiles |
| 88 | +# 加密配置文件与策略文件 |
| 89 | + twadmin -m F -c tw.cfg -S site.key twcfg.txt |
| 90 | + twadmin -m P -c tw.cfg -p tw.pol -S site.key twpol.txt.new |
| 91 | +# 初始化操作,生成数据库 |
| 92 | + tripwire --init |
| 93 | +# |
| 94 | +# 查看报告 |
| 95 | +twprint -m r --twrfile /var/lib/tripwire/report/XXX.twr |
| 96 | +``` |
| 97 | + |
| 98 | +- 设置系统时区 |
| 99 | + |
| 100 | +```bash |
| 101 | +date -R #查看当前时区 |
| 102 | +#Wed, 26 Apr 2017 12:18:02 +0800 #最后一个为时区 |
| 103 | +timedatectl set-timezone Asia/Shanghai #设置时区为中国时区 |
| 104 | +``` |
| 105 | + |
| 106 | +- 定时检查并发送邮件 |
| 107 | + |
| 108 | +```bash |
| 109 | +# 如果发送不了,安装sendmail |
| 110 | +yum -y install sendmail |
| 111 | +systemctl start sendmail |
| 112 | +====== 测试邮件是否发送成功 ====== |
| 113 | +echo "测试邮件内容" | mail -s "test" yourEmailName@gmail.com |
| 114 | +# # # # 利用mail命令发送html格式的邮件: |
| 115 | +msg="<b><div style=’color:red’>HTML Message goes here</div></b>" |
| 116 | +title=`echo -e "邮件主题\nContent-Type: text/html;charset=utf-8"` |
| 117 | +echo $msg | mail -s "$title" test@mail.com |
| 118 | +====== 执行发送邮件(注意邮件的垃圾箱)====== |
| 119 | +### 编辑器定时器 |
| 120 | +crontab -e |
| 121 | +======================== |
| 122 | +# 添加如下内容(每天凌晨1:30检测系统,发送报告) |
| 123 | +30 01 * * * /usr/sbin/tripwire --check | mail -s "Tripwire report for `uname -n`" hansoncoder@gmail.com -r report.tripwire |
| 124 | +======================== |
| 125 | +# 重启定时器 |
| 126 | +systemctl reload crond |
| 127 | +systemctl restart crond |
| 128 | +``` |
| 129 | + |
| 130 | +## shadowsocks相关 |
| 131 | +安装shadowsocks已经有很多教程,我这里只记录多用户配置文件,方便后期查阅。 |
| 132 | + |
| 133 | +- 多用户配置`/etc/shadowsocks.json`内容 |
| 134 | + |
| 135 | +```bash |
| 136 | +{ |
| 137 | + "port_password": |
| 138 | + { |
| 139 | + "端口1":"密码1", |
| 140 | + "端口2":"密码2" |
| 141 | + }, |
| 142 | + "_comment": |
| 143 | + { |
| 144 | + "端口1":"TestUser", |
| 145 | + "端口2":"MySS" |
| 146 | + }, |
| 147 | + "timeout":300, |
| 148 | + "method":"aes-256-cfb", |
| 149 | + "fast_open":false |
| 150 | +} |
| 151 | +``` |
| 152 | + |
| 153 | +- 开启自动启动`/etc/systemd/system/shadowsocks.service`内容 |
| 154 | + |
| 155 | +```bash |
| 156 | +[Unit] |
| 157 | +Description=Shadowsocks |
| 158 | +# |
| 159 | +[Service] |
| 160 | +TimeoutStartSec=0 |
| 161 | +User=userName #启动SS的用户,最好是非root用户 |
| 162 | +ExecStart=/usr/bin/ssserver -c /etc/shadowsocks.json |
| 163 | +ExecReload=/bin/kill -HUP $MAINPID |
| 164 | +ExecStop=/usr/bin/ssserver -d stop |
| 165 | +RestartSec=5s #启动失败,5秒自动重启 |
| 166 | +# |
| 167 | +[Install] |
| 168 | +WantedBy=multi-user.target |
| 169 | +``` |
| 170 | + |
| 171 | +- 设置非root启动ss |
| 172 | + |
| 173 | +```bash |
| 174 | +ssserver -c /etc/shadowsocks.json -d start –user userName |
| 175 | +systemctl start shadowsocks #启动shadowsocks |
| 176 | +``` |
| 177 | + |
| 178 | +- 查看ssserver服务进程启动者是否为userName |
| 179 | + |
| 180 | +```bash |
| 181 | +top #查看启动进程(top -u userName) |
| 182 | +#90 userNam+ 20 0 32442 423 4234 S 0.0 2.5 0:08.23 ssserver |
| 183 | +``` |
| 184 | + |
| 185 | +- centOS 7 终端与Vim中文乱码解决 |
| 186 | + |
| 187 | +```bash |
| 188 | +vim /etc/locale.conf |
| 189 | +#将LANG="XXXX" 改为LANG="zh_CN.UTF-8" |
| 190 | +vim /etc/vimrc |
| 191 | +# 在/etc/vimrc添加如下内容 |
| 192 | +#====== 中文乱码 ====== |
| 193 | +set fileencodings=ucs-bom,utf-8,gbk,gb2312,cp936,gb18030,big5,latin-1 |
| 194 | +set encoding=utf-8 |
| 195 | +set termencoding=utf-8 |
| 196 | +set fileencoding=utf-8 |
| 197 | +#====== end ====== |
| 198 | +``` |
| 199 | + |
| 200 | +## 参考文献 |
| 201 | + |
| 202 | +- VPS安全 |
| 203 | + |
| 204 | +> |
| 205 | + + [Shadowsocks安装配置](http://morning.work/page/2015-12/install-shadowsocks-on-centos-7.html) |
| 206 | + + [VPS 安全之 SSH 设置](https://blog.phpgao.com/vps_ssh.html) |
| 207 | + + [服务器被黑之后的心路历程](http://monklof.com/post/10/?from=timeline&isappinstalled=1) |
| 208 | + + [CentOS7 中 firewall 防火墙详解和配置以及切换为 iptables 防火墙](http://www.phpchina.com/portal.php?mod=view&aid=40252) |
| 209 | + + [CentOS7 Firewall 防火墙详解](https://blog.linuxeye.cn/406.html) |
| 210 | + |
| 211 | +- 安装Tripwire |
| 212 | + |
| 213 | +> |
| 214 | + + [熟练使用 Tripwire](==https://www.ibm.com/developerworks/cn/aix/library/au-usingtripwire/==) |
| 215 | + + [CentOS7 安全加固、检测、审计](http://www.ctolib.com/topics-106990.html) |
| 216 | + |
| 217 | +- Linux学习资料 |
| 218 | + |
| 219 | +> |
| 220 | + + [鸟哥的Linux私房菜:基础学习篇 第四版](https://wizardforcel.gitbooks.io/vbird-linux-basic-4e/content/120.html) |
0 commit comments