加固
- CentOS 7防火墙换用Friewalld
SSH配置
安装ssh
#安装
yum install ssh
#启动
service sshd start
#设置开机启动
chkconfig ssd on
#重启
service sshd restart
#重启网络
service network restart
#centos7
systemctl restart sshd.service
配置
配置文件目录: /etc/ssh/sshd_config
,参考
Port 22022 #对外端口,改了端口后别忘记了防火墙的出口端口设置
Protocol 2 #协议
RSAAuthentication yes #rsa登录
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys
PermitRootLogin yes #允许root用户以任何认证方式登录 without-password no
SyslogFacility AUTHPRIV
PasswordAuthentication no #禁止密码登录
只允许特定用户登录
在sshd_config中设置AllowUsers
AllowUsers a b c
重启sshd服务,则只有a/b/c3个用户可以登陆
密钥生成
#生成
ssh-keygen -t rsa
#添加
cat .ssh/id_rsa.pub >> .ssh/authorized_keys
#检查
cat .ssh/authorized_keys
常见免密码登录失败分析
配置问题
- 检查配置文件
/etc/ssh/sshd_config
是否开启了AuthorizedKeysFile
选项 - 检查
AuthorizedKeysFile
选项指定的文件是否存在并内容正常
目录权限问题
- ~权限设置为700
- ~/.ssh权限设置为700
~/.ssh/authorized_keys的权限设置为
600
sudo chmod 700 ~ sudo chmod 700 ~/.ssh sudo chmod 600 ~/.ssh/authorized_keys
配置ssd防止暴力破解
# KeyError: 'FAILED_ENTRY_REGEX10' 20161206居然有问题要还原代码编译 git reset --hard 65e1bc1 #依赖 pip install ipaddr #安装 python steup.py install #编辑配置文件 cp denyhosts.conf /etc #配置一下 /etc/denyhosts.conf #启动服务 cp daemon-control-dist /etc/init.d/daemon-control chown root /etc/init.d/daemon-control chmod 700 /etc/init.d/daemon-control #编辑daemon-control DENYHOSTS_BIN = "/usr/bin/denyhosts.py" #加入开机启动 chkconfig --level 3 daemon-control on /etc/init.d/daemon-control start #日志tail -50 /var/log/denyhosts
#https://github.com/denyhosts/denyhosts/blob/master/denyhosts.conf
#linux版本相关 SECURE_LOG HOSTS_DENY LOCK_FILE
SECURE_LOG = /var/log/secure #ssh 日志文件,它是根据这个文件来判断的。
HOSTS_DENY = /etc/hosts.deny #控制用户登陆的文件,根据UNIX版本不同配置不同
PURGE_DENY = 15m #过多久后清除已经禁止的,默认为空,即马上清除掉,这个根据自己的需要设置。
BLOCK_SERVICE = sshd #被加入hosts.deny后阻止的服务:阻止SSHD服务
#允许无效用户失败的次数
DENY_THRESHOLD_INVALID = 1
#允许普通用户登陆失败的次数
DENY_THRESHOLD_VALID = 10
#允许root登陆失败的次数
DENY_THRESHOLD_ROOT = 5
#对WORK_DIR/restricted-usernames配置的用户次数判定
DENY_THRESHOLD_RESTRICTED = 1
WORK_DIR = /usr/share/denyhosts/data #工作目录
SUSPICIOUS_LOGIN_REPORT_ALLOWED_HOSTS=YES #allowed-hosts中的用户有可疑登录行为如果此配置为YES则会报告
HOSTNAME_LOOKUP=NO #是否做域名反解
LOCK_FILE = /var/lock/subsys/denyhosts
############ THESE SETTINGS ARE OPTIONAL ############
ADMIN_EMAIL = wujiyu115@gmail.com #管理员邮件地址
SMTP_HOST = localhost
SMTP_PORT = 25
SMTP_FROM = DenyHosts <nobody@localhost>
SMTP_SUBJECT = Redocn-DenyHosts Report
AGE_RESET_VALID=5d
AGE_RESET_ROOT=25d
AGE_RESET_RESTRICTED=25d
AGE_RESET_INVALID=10d
######### THESE SETTINGS ARE SPECIFIC TO DAEMON MODE ##########
DAEMON_LOG = /var/log/denyhosts #日志文件
DAEMON_SLEEP = 30s
DAEMON_PURGE = 1h
Nginx配置
常用命令
#启动
sudo nginx
#关闭
sudo nginx -s stop
#检测配置是否正常:
nginx –t
#重新启动
nginx -s reload
配置
nginx反向代理长链接
upstream chat_cluster{
##多server负载,根据ip_hash作分流也可以用weight权重分流
server 127.0.0.1:10000;
server 127.0.0.1:10001;
ip_hash;
keepalive 1024;
}
server
{
listen 80;
server_name chat.rootk.com;
location / {
proxy_pass http://chat_cluster;
proxy_http_version 1.1;
# very important, nginx will waitting for the response from tornado
# if the time have passed more than 7200, nginx send http 504 to client
proxy_read_timeout 7200;
proxy_set_header Connection "";
proxy_set_header Host $host;
proxy_set_header X-Real-Ip $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
lnmp集成环镜
##默认网站(虚拟主机)##
/usr/local/nginx/conf/vhost
#LNMP默认网站配置文件:
/usr/local/nginx/conf/nginx.conf
#LNMPA默认网站配置文件:
/usr/local/nginx/conf/nginx.conf 和 /usr/local/apache/conf/extra/httpd-vhosts.conf
#LAMP默认网站配置文件:
/usr/local/apache/conf/extra/httpd-vhosts.conf
#LAMP默认网站
/home/wwwroot/default
#做反代,nginx里面的server_name和tomcat那里的Host保持一致
iptables
#!!!!iptables ACCEPT要放在DROP前才生效 /etc/sysconfig/iptables
#查看iptables的数据
iptables -L -n
#保存iptables的数据
service iptables save
#把某端口的权限开放给某个IP
iptables -I INPUT -s 113.68.65.85 -p tcp --dport 3306 -j ACCEPT
iptables -A INPUT -p tcp --dport 3306 -j DROP
service iptables save
#把某端口权限打开 dport指本地,sport指外部
iptables -A INPUT -p tcp --dport 8888 -j ACCEPT
iptables -A OUTPUT -p tcp --sport 8888 -j ACCEPT
#限制只能某个ip访问端口
iptables -I INPUT -s 10.21.213.74 -p tcp --dport 8888 -j ACCEPT
iptables -A INPUT -p tcp --dport 8888 -j DROP
#插入到指定位置 -i
iptables -I INPUT 2 -s 14.23.124.130 -p tcp --dport 8888 -j ACCEPT
#添加允许之后要DROP掉其他端口的input
iptables -P INPUT DROP
#查看INPUT的linnumber
iptables -L INPUT –line-numbers
#删除一条
iptables -D INPUT 3
#保存
service iptables save
#查看iptables状态
service iptables status
#iptables服务重启
service iptables restart
mysql配置
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
pid-file=/var/run/mysqld/mysqld.pid
log-bin=mysql-bin
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
忘记密码:
service mysqld stop
mysqld_safe --user=root --skip-grant-tables
mysql -u root
use mysql
update user set password=password("new_pass") where user="root";
flush privileges;
为root添加远程连接的能力:
GRANT ALL PRIVILEGES ON *.* TO root@"%" IDENTIFIED BY "root";
use mysql;
update user set Password = password('xxx') where User='root';
flush privileges;
#修改 Mysql 配置文件 sudo vi /etc/mysql/my.cnf
#注释#bind-address = 127.0.0.1
重启sudo /etc/init.d/mysql restart
创建用户
CREATE USER 'herogame'@'localhost' IDENTIFIED BY 'xxx'
加到开机列表:
chkconfig -add mysqld
一条命令解决mysql_config not found:
#遇到过几次pip安装mysql-python的时候出现如题的问题,在这里记录一下解决方法。
#找不到mysql_config一般是由于通过lnmp.org或者其他方式安装mysql以后mysql_config是在/usr/local/mysql/bin/里面,这里面的文件不是在任意位置都可以访问的
#解决方法:
ln -s /usr/local/mysql/bin/mysql_config /usr/local/bin/mysql_config
#将mysql_config链接到/usr/local/bin目录下
jdk安装
http://blog.csdn.net/sonnet123/article/details/9290561
Tomcat加固
Tomcat帐号安全
- 将
$CATALINA_HOME\conf\tomcat-users.xml
中文件的所有用户都注释掉 - 删除除了需要部署上去的应用,其余位于
$CATALINA_HOME\webapps
文件夹中的应用 - 禁止Tomcat显示目录列表,确保
$CATALINA_HOME\conf\web.xml
中listings
的值为false
- 配置shutdown端口:
<Serverport="未被占用的端口" shutdown="较为复杂的字符串">
隐藏Tomcat版本
catalina.jar\org\apache\catalina\util\ServerInfo.properties
server.info=需要展现的信息如SmartCity server.number=SmartCity server.built=20140101
开机管理
sudo apt-get install sysv-rc-conf
#直接加入启动程序,例如把 /etc/init.d/nginx 加入到系统自动 启动列表中:
sudo sysv-rc-conf nginx on //开启
sudo sysv-rc-conf nginx off //关闭
入侵日志分析
#查看登录成功日志过滤自己的ip ,-v 是not
last |grep -v 113.68.65.85
#查看登录日志,是否有暴力破解
tail -500 /var/log/secure |grep "Failed password"
#history日志,查看是不是自己的操作
history
#查看所有用户
cat /etc/passwd
#查看1天前修改过的常规文件,看别人删除或者修改你的文件没
find . -type f -mtime -1
#查看开启的服务
chkconfig --list | grep '3:on'
#查看进程
ps aux | grep python
#端口情况
sudo lsof -i
sudo netstat -lptu
sudo netstat -tulpn
sudo netstat -ntlp
#查看程序启动时间
ps -eo pid,lstart,etime,cmd |grep skynet