[development][security][modsecurity][nginx] nginx / modsecurity development things
接续前节:[security][modsecurity][nginx] nginx 与 modsecurity
nginx开发手册:https://nginx.org/en/docs/dev/development_guide.html#introduction
modsecurity开发手册:https://modsecurity.org/developers.html
nginx 与 modsecurity的接口在这里:https://nginx.org/en/docs/dev/development_guide.html#Modules
1. 可以确定的事情:
1. modsecurity 是 nginx 的一个mod。
2. nginx加载一个mod的方法:
1. 在运行 configure 脚本时,使用参数:
using --add-module=/path/to/module for static compilation
and --add-dynamic-module=/path/to/module for dynamic compilation.
2. 模块需要在这个路径下准备一个配置文件: config
3. 以及模块源码文件。
4. 2,3中的内容,在目录 github/ModSecurity/nginx/modsecurity/ 下。
modsecurity,好像是一个http类型的mod,读了Modules章节之后还要读HTTP章节。
https://nginx.org/en/docs/dev/development_guide.html#http
没太细啃,大概过了一下,总之就是没怎么看懂。。。
3. 如何写一个module:
参考:https://nginx.org/en/docs/dev/development_guide.html#http
3.1 关键的结构体:
struct ngx_module_s {
ngx_uint_t ctx_index;
ngx_uint_t index;
char *name;
ngx_uint_t spare0;
ngx_uint_t spare1;
ngx_uint_t version;
const char *signature;
void *ctx;
ngx_command_t *commands;
ngx_uint_t type;
ngx_int_t (*init_master)(ngx_log_t *log);
ngx_int_t (*init_module)(ngx_cycle_t *cycle);
ngx_int_t (*init_process)(ngx_cycle_t *cycle);
ngx_int_t (*init_thread)(ngx_cycle_t *cycle);
void (*exit_thread)(ngx_cycle_t *cycle);
void (*exit_process)(ngx_cycle_t *cycle);
void (*exit_master)(ngx_cycle_t *cycle);
uintptr_t spare_hook0;
uintptr_t spare_hook1;
uintptr_t spare_hook2;
uintptr_t spare_hook3;
uintptr_t spare_hook4;
uintptr_t spare_hook5;
uintptr_t spare_hook6;
uintptr_t spare_hook7;
};
ngx_module_s
modsecurity 中的实现:
ngx_module_t ngx_http_modsecurity = {
NGX_MODULE_V1,
&ngx_http_modsecurity_ctx, /* module context */
ngx_http_modsecurity_commands, /* module directives */
NGX_HTTP_MODULE, /* module type */
NULL, /* init master */
NULL, /* init module */
ngx_http_modsecurity_init_process, /* init process */
NULL, /* init thread */
NULL, /* exit thread */
ngx_http_modsecurity_terminate, /* exit process */
ngx_http_modsecurity_terminate, /* exit master */
NGX_MODULE_V1_PADDING
};
ngx_http_modsecurity
其中,成员commands,赋值为 NGX_HTTP_MODULE
结构体 ngx_http_modsecurity 是一切的开始。
3.2 关键的函数:
ngx_http_modsecurity_config()// 初始化配置文件的入口。
ngx_http_modsecurity_preconfiguration ()
L----> modsecInit ()
ngx_http_modsecurity_init ()
L: phases[NGX_HTTP_PREACCESS_PHASE].handlers = ngx_http_modsecurity_handler
参考:phases章节
Each HTTP request passes through a list of HTTP phases. Each phase is specialized in a particular type of processing.
Most phases allow installing handlers. The phase handlers are called successively once the request reaches the phase.
Many standard nginx modules install their phase handlers as a way to get called at a specific request processing stage.
在不同的阶段注册不同的回调函数, modsecurity 在 NGX_HTTP_PREACCESS_PHASE 阶段注册了函数 ngx_http_modsecurity_handler。
ngx_http_modsecurity_handler()
从注释看来,这里就是入口函数了:
/*
** [ENTRY POINT] does : this function called by nginx from the request handler
*/
3.3 关键函数
modsecurity_process_phase()
在apache2模块中,此函数基本上可以理解为是入口,与Eagle_eye中的使用,保持一致。
3.4 关键函数
好多函数的实现,采用次方式:
int modsecProcessRequestHeaders(request_rec *r) {
return hookfn_post_read_request(r);
}
这是一个宏,参考standalone/hooks.c
可以发现 函数hookfn_post_read_request 等于 函数ap_hook_post_read_request()中第一个参数对其进行的赋值。
格式为: hookfn%(NAME) ==> ap_hook%(NAME)
/home/tong/Src/thirdparty/github/ModSecurity [git::stable/cur *] [tong@T7] [:]
> grep -r ap_hook_post_read_request ./
./apache2/mod_security2.c: ap_hook_post_read_request(hook_request_early,
也就是说 hookfn_post_read_request 等价于 apache2/mod_security2.c:hook_request_early
从 nginx/modsecurity/config 的内容,也可以看见include了apache2的代码。
至此,nginx模块的入口,也统一到了函数:
modsecurity_process_phase()
3.5 API
standalone/api.h
apache2/mod_security2.c
[development][security][modsecurity][nginx] nginx / modsecurity development things的更多相关文章
- [security][modsecurity][nginx] nginx 与 modsecurity
参考文档: https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#installation-for-nginx nginx不支 ...
- ModSecurity for Nginx
Announcing the availability of ModSecurity extension for Nginx ModSecurity for Nginx ModSecurity for ...
- nginx配合modsecurity实现WAF功能
一.准备工作 系统:centos 7.2 64位.nginx1.10.2, modsecurity2.9.1 owasp3.0 1.nginx:http://nginx.org/download/ng ...
- Centos7.4 modsecurity with nginx 安装
1.准备: 系统环境:Centos7.4 软件及版本: nginx:OpenResty1.13.6.1 ModSecurity:ModSecurity v3.0.0rc1 (Linux) modsec ...
- ubuntu 重启 nginx 失败,* Restarting nginx nginx ...fail!
ubuntu 重启 nginx 失败,* Restarting nginx nginx ...fail! 执行 nginx 重启服务时,提示失败如下: $ sudo service ngi ...
- 敏捷软件工程(agile software development) VS传统软件工程(traditional software development)
敏捷软件工程(agile software development) VS传统软件工程(traditional software development) Agile principle ...
- nginx: [error] open() "/var/run/nginx/nginx.pid" failed (2: No such file or directory)
在重启nginx服务的时候,出现了这个错误. [root@izuf68g6a94fj32w0afx00z etc]# nginx -c /var/run/nginx/nginx.pid nginx: ...
- nginx入门与实战 安装 启动 配置nginx Nginx状态信息(status)配置 正向代理 反向代理 nginx语法之location详解
nginx入门与实战 网站服务 想必我们大多数人都是通过访问网站而开始接触互联网的吧.我们平时访问的网站服务 就是 Web 网络服务,一般是指允许用户通过浏览器访问到互联网中各种资源的服务. Web ...
- nginx: [error] invalid PID number "" in "/var/run/nginx/nginx.pid"
一.出现这个的情况 解决方法一: 1.添加正在运行pid号到/var/run/nginx/nginx.pid就可以解决问题了(这个情况是在重启的情况下发现的) 2.如果是重启机器之后,系统有时会删掉/ ...
随机推荐
- Socket网络编程--epoll小结
以前使用的用于I/O多路复用为了方便就使用select函数,但select这个函数是有缺陷的.因为它所支持的并发连接数是有限的(一般小于1024),因为用户处理的数组是使用硬编码的.这个最大值为FD_ ...
- [Big Data - ELK] ELK(ElasticSearch, Logstash, Kibana)搭建实时日志分析平台
ELK平台介绍 在搜索ELK资料的时候,发现这篇文章比较好,于是摘抄一小段: 以下内容来自: http://baidu.blog.51cto.com/71938/1676798 日志主要包括系统日志. ...
- Swift 4迁移总结:喜忧参半,新的起点
Swift 4迁移总结:喜忧参半,新的起点 每日一篇优秀博文 这次Swift 3 到 4 的迁移代码要改动的地方比较少,花了一个下午的时间就完成了迁移.Swift 把原来 4.0 的目标从 ABI 稳 ...
- RHEL/CentOS 一些不错的第三方软件包仓库
被Debian/Ubuntu惯坏了之后,再来用 RHEL/CentOS 实在是有点不习惯,软件都老且不说,关键是你还别嫌老(5年一个大版本的节奏真受不了啊,RHEL 6上面还是python 2.6), ...
- 推荐几个Windows工具软件: Stickies - 桌面贴
主页: http://www.zhornsoftware.co.uk/stickies/index.html Stickies work like Post-it notes for your PC. ...
- HDOJ 1393 Weird Clock(明确题意就简单了)
Problem Description A weird clock marked from 0 to 59 has only a minute hand. It won't move until a ...
- Ubuntu14.04下安装MATLAB后,通过命令行打开其图形界面
安装的是Matlab R2017a,使用的是默认安装目录,安装在目录/usr/local/MATLAB/R2017a/bin中.那么安装完成之后系统不会给Matlab添加系统路径,只有把终端切换到安装 ...
- 在MyEclipse中将Java Project转换成Web Project
在MyEclipse中将Java Project转换成Web Project 此添加方法是针对MyEclipse中添加的: 编辑工程的.project文件: 添加 <nature>com. ...
- 仿迅雷播放器教程 -- C++ 100款开源界面库 (10)
(声明:Alberl以后说到开源库,一般都是指著名的.或者不著名但维护至少3年以上的.那些把代码一扔就没下文的,Alberl不称之为开源库,只称为开源代码.这里并不是贬低,像Alberl前面那个系 ...
- 路由查询命令tracert
from:https://jingyan.baidu.com/article/9c69d48f4df25713c8024e66.html 在命令行中输入“tracert ”并在后面加入一个IP地址,可 ...