Rootop 服务器运维与web架构

2020-06-19
发表者 Venus
centos下为nginx添加modsecurity安全模块已关闭评论

centos下为nginx添加modsecurity安全模块

官网:https://www.modsecurity.org/download.html

先安装modsecurity,再编译nginx模块
# modsecurity依赖几个包实现某些功能

[root@localhost ~]# yum install -y gcc-c++ flex bison yajl yajl-devel curl-devel curl GeoIP-devel doxygen zlib-devel pcre-devel lmdb-devel libxml2-devel ssdeep-devel lua-devel libtool autoconf automake

# 安装modsecurity

[root@localhost ~]# wget -c https://github.com/SpiderLabs/ModSecurity/releases/download/v3.0.4/modsecurity-v3.0.4.tar.gz
[root@localhost ~]# tar zxvf modsecurity-v3.0.4.tar.gz 
[root@localhost ~]# cd modsecurity-v3.0.4
[root@localhost modsecurity-v3.0.4]# ./configure 
[root@localhost modsecurity-v3.0.4]# make
[root@localhost modsecurity-v3.0.4]# make install
[root@localhost modsecurity-v3.0.4]# cp modsecurity.conf-recommended /usr/local/modsecurity/modsecurity.conf
[root@localhost modsecurity-v3.0.4]# cp unicode.mapping /usr/local/modsecurity/

默认会安装到/usr/local/modsecurity/

# 下载安全规则

[root@localhost ~]# git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
[root@localhost ~]# cd owasp-modsecurity-crs/
[root@localhost owasp-modsecurity-crs]# cp -r rules/ /usr/local/modsecurity/
[root@localhost owasp-modsecurity-crs]# cp crs-setup.conf.example /usr/local/modsecurity/crs-setup.conf

# 下载nginx modsecurity模块

[root@localhost ~]# git clone https://github.com/SpiderLabs/ModSecurity-nginx.git

# 查看nginx原编译参数

[root@localhost nginx-1.17.9]# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.17.9
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --user=nginx --group=nginx --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --add-module=../ModSecurity-nginx/

# 重新编译nginx,添加modsecurity模块

[root@localhost nginx-1.17.9]# ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --add-module=../ModSecurity-nginx/
[root@localhost nginx-1.17.9]# make
[root@localhost nginx-1.17.9]# make install

# 虚拟主机配置

server
{
  server_name admin.local;

  listen 80;

  access_log  /usr/local/nginx/logs/admin_access.log;
  error_log   /usr/local/nginx/logs/admin_error.log;

  modsecurity on;
  modsecurity_rules_file /usr/local/modsecurity/modsecurity.conf;
  
  location /
  {
     proxy_pass http://127.0.0.1:10102;
  }
}

# 配置 /usr/local/modsecurity/modsecurity.conf

# 由 DetectionOnly 改为 On
SecRuleEngine On
# 由 ABIJDEFHZ 改为 ABCDEFHZ
SecAuditLogParts ABCDEFHZ
# 下面3行追加到配置文件
Include /usr/local/modsecurity/crs-setup.conf
Include /usr/local/modsecurity/rules/*.conf
SecAuditLogFormat JSON

保存退出。

SecAuditLogFormat JSON是审计日志改为json格式,便于提取。

审计日志会输出到 /var/log/modsec_audit.log

测试:
可以在url访问中加个参数,如:/login?id=1 and 1=1 ,页面会提示403状态码。
日志中会出现审计日志。

2020-05-18
发表者 Venus
xargs清空文件已关闭评论

xargs清空文件

#目的:
查找指定目录下的日志文件,超过指定大小后就清空

#方法:

find /tmp -size +500M -type f | grep *.log | xargs -I dellist sh -c 'cat /dev/null > dellist'

大部分命令只能在命令行输入参数,不能用管道符传递参数。
xargs命令就是把标准输入做为执行命令的参数。

-I 作用是指定要替换的字符串,也就是把dellist替换为标准输入读取的字符串

2020-05-08
发表者 Venus
mysql导入报@@GLOBAL.GTID_PURGED can only be set when @@GLOBAL.GTID_EXECUTED is empty错误已关闭评论

mysql导入报@@GLOBAL.GTID_PURGED can only be set when @@GLOBAL.GTID_EXECUTED is empty错误

从阿里云mysqldump的sql文件导入本地库报错

[root@localhost ~]# mysql -uroot -p$(cat mysql_password.txt) xxx < /home/backup/xxx/xxx2020-05-07-04-00-01.sql 
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1840 (HY000) at line 24: @@GLOBAL.GTID_PURGED can only be set when @@GLOBAL.GTID_EXECUTED is empty.

原因是阿里云rds自带主从复制,开启了gtid全局事务id用于保证主从复制一致,备份的时候没有忽略gtid,导致导入本地时报错。

方法1、

# 查看当前的gtid
mysql> select @@global.gtid_executed\G;

# 清空本地的gtid
mysql> reset master;
Query OK, 0 rows affected (0.04 sec)

方法2、删掉sql文件中的 SET @@GLOBAL.GTID_PURGED 2行

-- MySQL dump 10.13  Distrib 5.7.17, for linux-glibc2.5 (x86_64)
--
-- Host: rm-xxx.mysql.rds.aliyuncs.com    Database: xxx
-- ------------------------------------------------------
-- Server version       5.7.25-log

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
SET @MYSQLDUMP_TEMP_LOG_BIN = @@SESSION.SQL_LOG_BIN;
SET @@SESSION.SQL_LOG_BIN= 0;

--
-- GTID state at the beginning of the backup 
--

SET @@GLOBAL.GTID_PURGED='6822560b-3659-11e8-afba-7cd30adaf22a:1-33310212,
79abd425-3659-11e8-8eeb-7cd30adaf28e:1-36989543';

方法3、

/usr/local/mysql/bin/mysqldump --set-gtid-purged=OFF
加上--set-gtid-purged=OFF参数再备份,这样就忽略。

2020-05-07
发表者 Venus
filebeat7.6.1修改索引名字后elasticsearch中没有生成新索引已关闭评论

filebeat7.6.1修改索引名字后elasticsearch中没有生成新索引

filebeat7.6.1修改索引名字后,比如下面这样,shop-api xxx

output.elasticsearch:
  hosts: ["http://192.168.0.10:9200"]
  index: "shop-api-%{[agent.version]}-%{+yyyy.MM.dd}"
setup.template.name: "shop-api"
setup.template.pattern: "shop-api-*"

启动后,发现在es中并没有生成新的索引。
filebeat有打印下面的日志信息

2020-05-07T02:40:53.010Z	INFO	[index-management]	idxmgmt/std.go:258	Auto ILM enable success.
2020-05-07T02:40:53.012Z	INFO	[index-management.ilm]	ilm/std.go:139	do not generate ilm policy: exists=true, overwrite=false
2020-05-07T02:40:53.012Z	INFO	[index-management]	idxmgmt/std.go:271	ILM policy successfully loaded.
2020-05-07T02:40:53.012Z	INFO	[index-management]	idxmgmt/std.go:410	Set setup.template.name to '{filebeat-7.6.1 {now/d}-000001}' as ILM is enabled.

提示开启了ILM策略

翻官方文档(https://www.elastic.co/guide/en/beats/filebeat/current/elasticsearch-output.html)后发现:
index配置部分中提示 The index setting is ignored when index lifecycle management is enabled
意思就是index设置的参数在索引生命周期管理(ilm)开启后会忽略。

查看ilm文档 https://www.elastic.co/guide/en/beats/filebeat/current/ilm.html 提示:
Starting with version 7.0, Filebeat uses index lifecycle management by default when it connects to a cluster that supports lifecycle management
从7.0版本开始,当elasticsearch支持生命周期管理时,filebeat默认使用索引生命周期管理,这样就导致自己修改的日志文件名无效了。

关闭ilm功能即可(setup.ilm.enabled: false)。

[root@node1 shop-api]# cat filebeat.yml
filebeat.inputs:
- type: log
  paths:
   - /mnt/logs/*.log
  fields:
   java: true
  fields_under_root: true
  multiline.pattern: '^[0-9]{2}:[0-9]{2}:[0-9]{2}.* \[http-nio'
  multiline.negate: true
  multiline.match: after

setup.ilm.enabled: false
output.elasticsearch:
  hosts: ["http://192.168.0.10:9200"]
  index: "shop-api-%{[agent.version]}-%{+yyyy.MM.dd}"
setup.template.name: "shop-api"
setup.template.pattern: "shop-api-*"

2020-04-30
发表者 Venus
nginx http重定向到https,post数据丢失。已关闭评论

nginx http重定向到https,post数据丢失。

nginx配置http跳转https

if ($scheme = http) {
	return 301 https://$host$request_uri;
}

如果状态码返回301或者302,当post数据到http协议时,重定向后会出现请求方法变为 get,post数据丢失。

解决这个问题就要换返回的状态码。

if ($scheme = http) {
	return 307 https://$host$request_uri;
}

307、308 都可以保持post数据的重定向,包括请求方法也不会变化。
307是临时,308是永久

所以当往一个http地址发送post请求,服务器重定向到https,要配置为307或者308状态码。