modsecurity源码分析

一。so文件导出结构在Mod_security2.c中的最后AP_MODULE_DECLARE_DATA security2_module全局变量.
该变量定义了hook的各个函数位置和处理的指令表。




二。指令处理。
1.SecRuleEngine On:由函数cmd_rule_engine处理,在Apache2_config.c中。
该函数主要是设置directory_config->is_enabled为enable状态.
2.SecDefaultAction:由函数cmd_default_action处理.
该函数先调用msre_actionset_create创建msre_actionset动作集,指定初值。调用msre_parse_actions解析动作指令,赋值到引擎中。
然后判断动作完整性,是否有拦截动作,动作阶段,id号,消息,严重级别等。
3.SecRule:由函数cmd_rule处理,直接调用add_rule添加规则。
该函数先调用msre_ruleset_create创建规则集,然后调用msre_rule_create创建规则。在msre_rule_create中,
调用msre_parse_targets解析规则目标,然后msre_engine_op_resolve解析操作符,然后解析参数,调用msre_actionset_create创建响应的动作集,
调用msre_rule_generate_unparsed添加规则。






三。hook函数处理。
1.注册可选函数modsec_register_tfn,modsec_register_operator,modsec_register_variable,modsec_register_reqbody_processor。
2.注册hook_pre_config,hook_post_config,hook_child_init,处理连接的hook_connection_early,读请求的hook_request_early,
fixups阶段的hook_request_late,hook_error_log,hook_log_transaction等hook函数。
3.注册过滤器hook_insert_filter,hook_insert_error_filter,content_set类型的input_filter,output_filter.
4.hook_pre_config函数:调用modsecurity_create创建mod_security引擎。modsecurity_create函数a.分配msc_engine引擎结构,
b.调用msre_engine_create创建空引擎。
c.调研msre_engine_register_default_variables函数注册变量。
内部调用apr_table_setn函数向engine->variables表中注册PHASE_REQUEST_HEADERS阶段的ARGS,ARGS_COMBINED_SIZE,ARGS_GET,ARGS_GET_NAMES,FILES,FILES_COMBINED_SIZE,GEO等变量。
d.调用msre_engine_register_default_operators向engine->operators表中注册noMatch,ipmatch等操作符。
e.调用msre_engine_register_default_tfns向engine->tfns注册默认的转换函数,比如base64Decode,base64Encode,hexDecode。
f.调用函数msre_engine_register_default_actions向engine->actions注册动作,比如deny,status,block等动作。
在hook_pre_config中继续创建log日志额处理函数modsec_var_log_handler.
5.hook_post_config函数:调用modsecurity_init函数初始化审计日志,geo的一些锁结构。
6.hook_child_init函数:调用modsecurity_child_init函数实现对每一个进程的数据的初始化。
7.hook_connection_early连接处理函数:主要是限制忙碌状态下的最多连接数。
8.hook_request_early函数:调用create_tx_context创建事务上下文,创建指定的用户的目录配置,设置请求的一些初始化。
调用modsecurity_tx_init,设置content-type,cookie,匹配变量,初始化集合,匹配规则集等。
9.hook_request_late函数:设置目录配置。调用modsecurity_process_phase处理request_headers,然后调用perform_interception拦截。调用
read_request_body读取请求体,调用modsecurity_process_phase处理请求体,然后调用perform_interception拦截。
modsecurity_process_phase函数,如果是处理请求头,调用modsecurity_process_phase_request_headers,如果是处理请求体,
调用modsecurity_process_phase_request_body函数,两个阶段都调用msre_ruleset_process_phase->msre_ruleset_process_phase_函数,该函数遍历规则集,
对每条规则调用msre_rule_process函数。msre_rule_process_normal:解析参数,目标,调用execute_operator,调用msre_rule->op_metadata->execute匹配。
perform_interception拦截函数:主要做一些日志记录。
10.
 

        --成文于 2015-5-6

ModSecurity Handbook is the definitive guide to ModSecurity, a popular open source web application firewall. Written by Ivan Ristic, who designed and wrote much of ModSecurity, this book will teach you everything you need to know to monitor the activity on your web sites and protect them from attack. Situated between your web sites and the world, web application firewalls provide an additional security layer, monitoring everything that comes in and everything that goes out. They enable you to perform many advanced activities, such as real-time application security monitoring, access control, virtual patching, HTTP traffic logging, continuous passive security assessment, and web application hardening. They can be very effective in preventing application security attacks, such as cross-site scripting, SQL injection, remote file inclusion, and others. Considering that most web sites today suffer from one problem or another, ModSecurity Handbook will help anyone who has a web site to run. The topics covered include: Installation and configuration of ModSecurity Logging of complete HTTP traffic Rule writing IP address, session, and user tracking Session management hardening Whitelisting, blacklisting, and IP reputation management Advanced blocking strategies Integration with other Apache modules Working with rule sets Virtual patching Performance considerations Content injection XML inspection Writing rules in Lua Extending ModSecurity in C The book is suitable for all reader levels: it contains step-by-step installation and configuration instructions for those just starting out, as well as detailed explanations of the internals and discussion of advanced techniques for seasoned users. A comprehensive reference manual is included in the second part of the book. Digital version of ModSecurity Handbook (PDF or EPUB) can be obtained directly from the author, at feistyduck.com.
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值