当前位置:网站首页>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 .
边栏推荐
- [技術發展-24]:現有物聯網通信技術特點
- KEIL5出现中文字体乱码的解决方法
- SQL Injection (GET/Select)
- Sequence table (implemented in C language)
- Multi person collaborative data annotation based on Baidu brain easydata from scratch
- Flutter dynamic | fair 2.5.0 new version features
- Ocean CMS vulnerability - search php
- Qt学习18 登录对话框实例分析
- PhpMyAdmin stage file contains analysis traceability
- Resource Cost Optimization Practice of R & D team
猜你喜欢

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

Mastering the cypress command line options is the basis for truly mastering cypress

Go language web development series 28: solve cross domain access of CORS with gin contrib / CORS

The latest BSC can pay dividends. Any B usdt Shib eth dividend destruction marketing can

Libuv Library - Design Overview (Chinese version)

Multi person collaborative data annotation based on Baidu brain easydata from scratch

Common network state detection and analysis tools

The solution of Chinese font garbled code in keil5

Go language unit test 4: go language uses gomonkey to test functions or methods

Halcon combined with C # to detect surface defects -- Halcon routine autobahn
随机推荐
项目协作的进度如何推进| 社区征文
The shadow of the object at the edge of the untiy world flickers, and the shadow of the object near the far point is normal
Go language web development series 26: Gin framework: demonstrates the execution sequence of code when there are multiple middleware
Field problems in MySQL
Use vscode to view hex or UTF-8 codes
Go 1.16.4: purpose of go mod tidy
Windos creates Cordova prompt because running scripts is prohibited on this system
The latest BSC can pay dividends. Any B usdt Shib eth dividend destruction marketing can
Box layout of Kivy tutorial BoxLayout arranges sub items in vertical or horizontal boxes (tutorial includes source code)
8 Queen question
Go language unit test 4: go language uses gomonkey to test functions or methods
SQL Injection (POST/Search)
Unity Render Streaming通过Js与Unity自定义通讯
Stack application (balancer)
Setting up remote links to MySQL on Linux
SQL Injection (GET/Search)
SQL Injection (GET/Select)
Depth and breadth first traversal of tree (regardless of binary tree)
Conversion function and explicit
怎样删除对象的某个属性或⽅法