openresty 安装文档
http://openresty.org/en/installation.html
学习:
先找个没有安装过 nginx (或 openresty)的虚拟机,从零开始,源码编译一套最简洁的 nginx (或 openresty),自己指定安装位置,自己设置为 service 源码安装 nginx 并设置为 service
以下只写 openresty
练手
再找一个虚拟机,直接二进制安装( apt install ),他会自动配置好安装路径、执行文件位置、conf 文件、站点目录,并且设置好 service,后续使用都是缺省配置好了!
$ openresty -V ,复制当前 configure arguments ,去掉其中的 --add-module 部分 ,最后加上自己准备增加的模块名称,构建新的 configure arguments
下载最新稳定版源码 ,按照新的 configure arguments 编译、make 、make install
过程中会出现预装依赖、配置位置、服务状态/启动/重启等问题
全部解决并测试成功后,方可在实际环境动手
实战
按照前一步练习中所做的记录,重新走一遍 ,如果已经 apt install 过,先确认一次环境配置是否和练习时一样
以下是增加 naxsi WAF 模块的练手过程记录
官方中文安装文档
http://openresty.org/cn/linux-packages.html#ubuntu
抄录一下官方步骤
sudo systemctl disable nginx
sudo systemctl stop nginx
步骤一:安装导入 GPG 公钥时所需的几个依赖包(整个安装过程完成后可以随时删除它们):
sudo apt-get -y install --no-install-recommends wget gnupg ca-certificates
步骤二:导入我们的 GPG 密钥:
wget -O - https://openresty.org/package/pubkey.gpg | sudo apt-key add -
步骤三:添加我们官方 APT 仓库。
echo “deb http://openresty.org/package/ubuntu $(lsb_release -sc) main”
| sudo tee /etc/apt/sources.list.d/openresty.list
步骤四:更新 APT 索引,并安装
sudo apt-get update
sudo apt-get -y install openresty
wget https://github.com/nbs-system/naxsi/archive/master.zip
mv master.zip naxsi-master.zip
unzip naxsi-master.zip
cd naxsi-master/
$ ls
LICENSE logo.png naxsi_config naxsi_src nxapi README.md t
以下将重新编译 openresty , --add-module=/home/dhbm/naxsi-master/naxsi_src/ 加上 naxsi 模块
$ openresty -V
nginx version: openresty/1.19.9.1
built with OpenSSL 1.1.1k 25 Mar 2021
TLS SNI support enabled
configure arguments: --prefix=/usr/local/openresty/nginx --with-cc-opt='-O2 -DNGX_LUA_ABORT_AT_PANIC -I/usr/local/openresty/zlib/include -I/usr/local/openresty/pcre/include -I/usr/local/openresty/openssl111/include' --add-module=../ngx_devel_kit-0.3.1 --add-module=../echo-nginx-module-0.62 --add-module=../xss-nginx-module-0.06 --add-module=../ngx_coolkit-0.2 --add-module=../set-misc-nginx-module-0.32 --add-module=../form-input-nginx-module-0.12 --add-module=../encrypted-session-nginx-module-0.08 --add-module=../srcache-nginx-module-0.32 --add-module=../ngx_lua-0.10.20 --add-module=../ngx_lua_upstream-0.07 --add-module=../headers-more-nginx-module-0.33 --add-module=../array-var-nginx-module-0.05 --add-module=../memc-nginx-module-0.19 --add-module=../redis2-nginx-module-0.15 --add-module=../redis-nginx-module-0.3.7 --add-module=../ngx_stream_lua-0.0.10 --with-ld-opt='-Wl,-rpath,/usr/local/openresty/luajit/lib -L/usr/local/openresty/zlib/lib -L/usr/local/openresty/pcre/lib -L/usr/local/openresty/openssl111/lib -Wl,-rpath,/usr/local/openresty/zlib/lib:/usr/local/openresty/pcre/lib:/usr/local/openresty/openssl111/lib' --with-pcre-jit --with-stream --with-stream_ssl_module --with-stream_ssl_preread_module --with-http_v2_module --without-mail_pop3_module --without-mail_imap_module --without-mail_smtp_module --with-http_stub_status_module --with-http_realip_module --with-http_addition_module --with-http_auth_request_module --with-http_secure_link_module --with-http_random_index_module --with-http_gzip_static_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-threads --with-stream --with-http_ssl_module
./configure --prefix=/usr/local/openresty --with-cc-opt='-O2 -DNGX_LUA_ABORT_AT_PANIC -I/usr/local/openresty/zlib/include -I/usr/local/openresty/pcre/include -I/usr/local/openresty/openssl111/include' --with-ld-opt='-Wl,-rpath,/usr/local/openresty/luajit/lib -L/usr/local/openresty/zlib/lib -L/usr/local/openresty/pcre/lib -L/usr/local/openresty/openssl111/lib -Wl,-rpath,/usr/local/openresty/zlib/lib:/usr/local/openresty/pcre/lib:/usr/local/openresty/openssl111/lib' --with-pcre-jit --with-stream --with-stream_ssl_module --with-stream_ssl_preread_module --with-http_v2_module --without-mail_pop3_module --without-mail_imap_module --without-mail_smtp_module --with-http_stub_status_module --with-http_realip_module --with-http_addition_module --with-http_auth_request_module --with-http_secure_link_module --with-http_random_index_module --with-http_gzip_static_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-threads --with-stream --with-http_ssl_module --add-module=/home/dhbm/naxsi-master/naxsi_src/
修改说明:
–prefix=/usr/local/openresty ,开始完全抄 prefix=/usr/local/openresty/nginx ,结果发现后续配置文件和执行文件位置总是对不上!所以,需要去掉后面这个 nginx ,否则,最后会多一层 nginx 目录!
去掉所有的 --add-module ,好像这些都会自动 add 上去 (openresty 实际就是在 nginx 上 add 了这些模块)
最后面加上我要的模块 --add-module=/home/dhbm/naxsi-master/naxsi_src/
增加的模块路径最好写完整的绝对路径
apt-get install libpcre3-dev
libssl-dev perl make build-essential curl
$ wget https://openresty.org/download/openresty-1.19.9.1.tar.gz
$ tar -zxvf openresty-1.19.9.1.tar.gz
$ cd openresty-1.19.9.1/
~/openresty-1.19.9.1$ ls
bundle configure COPYRIGHT patches README.markdown README-windows.txt util
执行以上新建的 ./configure
$ make
...
sed -e "s|%%PREFIX%%|/usr/local/openresty/nginx/nginx|" \
-e "s|%%PID_PATH%%|/usr/local/openresty/nginx/nginx/logs/nginx.pid|" \
-e "s|%%CONF_PATH%%|/usr/local/openresty/nginx/nginx/conf/nginx.conf|" \
-e "s|%%ERROR_LOG_PATH%%|/usr/local/openresty/nginx/nginx/logs/error.log|" \
< docs/man/nginx.8 > objs/nginx.8
make[2]: Leaving directory '/home/dhbm/openresty-1.19.9.1/build/nginx-1.19.9'
make[1]: Leaving directory '/home/dhbm/openresty-1.19.9.1/build/nginx-1.19.9'
$ sudo make install
...
make[2]: Leaving directory '/home/dhbm/openresty-1.19.9.1/build/nginx-1.19.9'
make[1]: Leaving directory '/home/dhbm/openresty-1.19.9.1/build/nginx-1.19.9'
mkdir -p /usr/local/openresty/nginx/site/lualib /usr/local/openresty/nginx/site/pod /usr/local/openresty/nginx/site/manifest
ln -sf /usr/local/openresty/nginx/nginx/sbin/nginx /usr/local/openresty/nginx/bin/openresty
可以看到 openresty 实际是 nginx 的软链接
ln -sf /usr/local/openresty/nginx/nginx/sbin/nginx /usr/local/openresty/nginx/bin/openresty
确认一下
$ openresty -V 2>&1 |grep naxsi
$ which openresty
/usr/local/openresty/bin/openresty
$ cd /usr/local/openresty/bin/
$ ll
total 172
drwxr-xr-x 2 root root 4096 Aug 27 17:29 ./
drwxr-xr-x 11 root root 4096 Aug 27 17:29 ../
-rwxr-xr-x 1 root root 19185 Aug 27 17:29 md2pod.pl*
-rwxr-xr-x 1 root root 15994 Aug 27 17:29 nginx-xml2pod*
lrwxrwxrwx 1 root root 37 Aug 27 17:29 openresty -> /usr/local/openresty/nginx/sbin/nginx*
-rwxr-xr-x 1 root root 63510 Aug 27 17:29 opm*
-rwxr-xr-x 1 root root 36623 Aug 27 17:29 resty*
-rwxr-xr-x 1 root root 14957 Aug 27 17:29 restydoc*
-rwxr-xr-x 1 root root 8873 Aug 27 17:29 restydoc-index*
这些文件的创建时间是否时刚才 make install 完成时间?
如果不是,说明没有 install 成功!检查 --prefix=/usr/local/openresty
然后,重新编译,手工替换费劲,易出错!
sudo openresty -t
nginx: the configuration file /usr/local/openresty/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/openresty/nginx/conf/nginx.conf test is successful
$ systemctl list-unit-files
$ systemctl list-unit-files |grep rest
$ sudo service openresty status
$ sudo service openresty reload
$ sudo service openresty start
官网文档 Basic Setup
https://github.com/nbs-system/naxsi/wiki/naxsi-setup
$ cd /usr/local/openresty/nginx/conf
$ sudo cp ~/naxsi-master/naxsi_config/naxsi_core.rules .
$ sudo vim naxsi.rules
#LearningMode; #Enables learning mode
SecRulesEnabled;
#SecRulesDisabled;
DeniedUrl "/RequestDenied";
## check rules
CheckRule "$SQL >= 8" BLOCK;
CheckRule "$RFI >= 8" BLOCK;
CheckRule "$TRAVERSAL >= 4" BLOCK;
CheckRule "$EVADE >= 4" BLOCK;
CheckRule "$XSS >= 8" BLOCK;
$ sudo vim nginx.conf
http {
include mime.types;
# wzh 20210827
# include /usr/local/openresty/nginx/conf/naxsi_core.rules;
include naxsi_core.rules;
...
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
# wzh 20210831
# SecRulesDisabled; #optional, naxsi is disabled by default
SecRulesEnabled; #enable naxsi
# include /usr/local/openresty/nginx/conf/naxsi.rules;
include naxsi.rules;
error_log logs/error.log;
proxy_pass http://127.0.0.1:8080;
root html;
index index.html index.htm;
}
location /RequestDenied {
return 403;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
...
# wzh 20210819
server {
listen 8080;
location / {
default_type text/html;
content_by_lua_block {
ngx.say("<p>20210831 hello, world</p>");
}
root html;
index index.html index.htm;
}
}
修改说明
location /RequestDenied {
return 403;
}
$ sudo openresty -t
$ sudo service openresty reload
正常访问,不带犯规!
$ curl http://192.168.1.203/?name=wzh
<p>20210831 hello, world</p>
带犯规访问
$ curl http://192.168.1.203/?id=100//and//1=1
带犯规访问
$ curl http://127.0.0.1/?id=100//and//1=1
带犯规,绕过 WAF 直接访问 8080 端口网站
$ curl http://127.0.0.1:8080/?name=%28%29
<p>20210831 hello, world</p>
看来每个网站都要设置 WAF ,要么配合网关强制所有网站必须经过 WAF