当前位置:网站首页>Rasp implementation of PHP
Rasp implementation of PHP
2022-07-03 13:53:00 【yolo_ yyh】
Catalog
Two 、PHP Introduction to expansion
3、 ... and 、PHP Of HOOK Realization
One 、 What is? RASP
RASP The full name is Runtime Application self-protection, That is, the runtime applies self-protection , This is a kind of embedded into the application , Real time detection of requests from outside 、 Input technology .PHP Of RASP It's through PHP The expanded form is embedded in PHP In the interpreter of .
Two 、PHP Introduction to expansion
PHP There are different working modes in different environments , Common are : Single process mode and Apache Multi process or multi thread mode in the environment . But no matter which mode , The following processes need to be executed :

chart 1 Single process expansion execution process
In single process mode, the whole PHP The life cycle of is :
chart 2 Single process lifecycle
Life cycle in multi process mode :

chart 3 Multi process lifecycle
In the case of multiple processes, each process only performs module initialization and module shutdown once , Will continue to perform request initialization - Processing requests - The process of requesting closure . Multithreading mode is similar to , It's just the thread that handles the request .
So we can initialize the module (MINIT) Or request initialization (RINIT) Stage hook, In this way, every time we process the request, our business logic function , You can input in our business logic function 、 Or request monitoring , After the abnormality is judged, the risk can be reported .
3、 ... and 、PHP Of HOOK Realization
Want to know hook The way , I need to look at it first PHP Processing flow of script .
PHP After lexical analysis and linguistic analysis of the script, it will generate OPArray, That is to say OPCode Array of , Every OPCode All represent a different operation , The name is similar to the following :
ZEND_ADD: Perform the arithmetic addition of two operands ;
ZEND_EXIT: sign out PHP perform ;
Zend VM There is a main branch loop in (while(1) Dead cycle ), Only when executed opcode Of handler The return value of 1(ZEND_VM_RETURN()) when , This cycle will end , So the compiler will be for each PHP The script adds one at the end RETURN Of OPCode.
With ZEND_ADD This opcode For example , This structure contains two operands (op1 and op2)、handler( A function pointer )、result( The result of the operation ).Zend VM According to the type of the two operands , Find the corresponding handler, In the source code ZEND_ADD This opcode Of handler The definition is as follows :
ZEND_VM_HANDLER(1, ZEND_ADD, CONST|TMP|VAR|CV, CONST|TMP|VAR|CV)
{
USE_OPLINE
zend_free_op free_op1, free_op2;
SAVE_OPLINE();
fast_add_function(&EX_T(opline->result.var).tmp_var,
GET_OP1_ZVAL_PTR(BP_VAR_R),
GET_OP2_ZVAL_PTR(BP_VAR_R) TSRMLS_CC);
FREE_OP1();
FREE_OP2();
CHECK_EXCEPTION();
ZEND_VM_NEXT_OPCODE();
}The last two parameters of the function represent op1 and op2 Acceptable operand types .
The processing tool will be based on the definition of this function , Yes op1 and op2 Make type combinations , Generate 16 Handle specific types handler function . these handler The function is named as follows :
static int ZEND_FASTCALL ZEND_ADD_SPEC_CONST_CONST_HANDLER(ZEND_OPCODE_HANDLER_ARGS) { /* handler code */ }
static int ZEND_FASTCALL ZEND_ADD_SPEC_CONST_TMP_HANDLER(ZEND_OPCODE_HANDLER_ARGS) { /* handler code */ }
static int ZEND_FASTCALL ZEND_ADD_SPEC_CONST_VAR_HANDLER(ZEND_OPCODE_HANDLER_ARGS) { /* handler code */ }
static int ZEND_FASTCALL ZEND_ADD_SPEC_CONST_CV_HANDLER(ZEND_OPCODE_HANDLER_ARGS) { /* handler code */ }
static int ZEND_FASTCALL ZEND_ADD_SPEC_TMP_CONST_HANDLER(ZEND_OPCODE_HANDLER_ARGS) { /* handler code */ }
static int ZEND_FASTCALL ZEND_ADD_SPEC_TMP_TMP_HANDLER(ZEND_OPCODE_HANDLER_ARGS) { /* handler code */ }
......The law is :static int ZEND_FASTCALL OPCode_SPEC_{OP1-TYPE}_{OP2-TYPE}_HANDLER
So which one is finally executed handler It depends on the type that requires two operands .
So we can replace OPCode Of handler, It happens that there is a corresponding interface in the source code zend_set_user_opcode_handler(zend_uchar opcode, user_opcode_handler_t handler) available .
except OPCode Outside ,PHP There are also many built-in functions , such as sprintf、 system、usort wait , These functions are not OPcode Of , But these functions are stored in the global function table , Can pass CG(function_table) obtain , These functions also have corresponding handler A function pointer , So we can directly back up the original handler After use function->internal_function.handler = new_handler Replace it .
边栏推荐
- 研发团队资源成本优化实践
- Complete deep neural network CNN training with tensorflow to complete picture recognition case 2
- CVPR 2022 | interpretation of 6 excellent papers selected by meituan technical team
- 使用tensorflow进行完整的DNN深度神经网络CNN训练完成图片识别案例
- Screenshot of the operation steps of upload labs level 4-level 9
- SQL Injection (AJAX/JSON/jQuery)
- Windos creates Cordova prompt because running scripts is prohibited on this system
- Box layout of Kivy tutorial BoxLayout arranges sub items in vertical or horizontal boxes (tutorial includes source code)
- JVM family - overview, program counter day1-1
- Ocean CMS vulnerability - search php
猜你喜欢
![Mysql:insert date:SQL 错误 [1292] [22001]: Data truncation: Incorrect date value:](/img/2f/33504391a661ecb63d42d75acf3a37.png)
Mysql:insert date:SQL 错误 [1292] [22001]: Data truncation: Incorrect date value:

【电脑插入U盘或者内存卡显示无法格式化FAT32如何解决】

Comprehensive case of MySQL data addition, deletion, modification and query

Using registered classes to realize specific type matching function template
![[bw16 application] instructions for firmware burning of Anxin Ke bw16 module and development board update](/img/b8/31609303fd817c48b6fff7c43f31e5.png)
[bw16 application] instructions for firmware burning of Anxin Ke bw16 module and development board update
![[机缘参悟-37]:人感官系统的结构决定了人类是以自我为中心](/img/06/b71b505c7072d540955fda6da1dc1b.jpg)
[机缘参悟-37]:人感官系统的结构决定了人类是以自我为中心

Unable to stop it, domestic chips have made another breakthrough, and some links have reached 4nm

Common network state detection and analysis tools

Libuv库 - 设计概述(中文版)

Golang — 命令行工具cobra
随机推荐
MySQL 数据增删改查综合案例
Disruptor -- a high concurrency and high performance queue framework for processing tens of millions of levels
Box layout of Kivy tutorial BoxLayout arranges sub items in vertical or horizontal boxes (tutorial includes source code)
MySQL data processing value addition, deletion and modification
Another industry has been broken by Chinese chips. No wonder the leading analog chip companies in the United States have cut prices and sold off
Libuv Library - Design Overview (Chinese version)
Qt学习17 对话框及其类型
Ocean CMS vulnerability - search php
Go language web development series 28: solve cross domain access of CORS with gin contrib / CORS
太阳底下无新事,元宇宙能否更上层楼?
[understanding by chance-37]: the structure of human sensory system determines that human beings are self-centered
Golang — 命令行工具cobra
The reasons why there are so many programming languages in programming internal skills
Asp. Net core1.1 without project JSON, so as to generate cross platform packages
Libuv库 - 设计概述(中文版)
[技术发展-24]:现有物联网通信技术特点
Solve MySQL 1045 access denied for user 'root' @ 'localhost' (using password: yes)
Kivy tutorial how to load kV file design interface by string (tutorial includes source code)
Go language web development series 25: Gin framework: using MD5 to verify the signature for the interface station
掌握Cypress命令行选项,是真正掌握Cypress的基础