当前位置:网站首页>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 .
边栏推荐
- HALCON联合C#检测表面缺陷——HALCON例程autobahn
- 怎样删除对象的某个属性或⽅法
- 如何使用lxml判断网站公告是否更新
- Depth and breadth first traversal of tree (regardless of binary tree)
- [how to solve FAT32 when the computer is inserted into the U disk or the memory card display cannot be formatted]
- 解决MySql 1045 Access denied for user ‘root‘@‘localhost‘ (using password: YES)
- Qt学习20 Qt 中的标准对话框(中)
- 从零开始的基于百度大脑EasyData的多人协同数据标注
- Can newly graduated European college students get an offer from a major Internet company in the United States?
- 静态链表(数组的下标代替指针)
猜你喜欢

3D视觉——2.人体姿态估计(Pose Estimation)入门——OpenPose含安装、编译、使用(单帧、实时视频)

SQL Injection (POST/Select)

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

Screenshot of the operation steps of upload labs level 4-level 9

MySQL 数据处理值增删改

Resource Cost Optimization Practice of R & D team

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

SQL Injection (GET/Select)

Several common optimization methods matlab principle and depth analysis

Go language web development series 27: Gin framework: using gin swagger to implement interface documents
随机推荐
Go 1.16.4: manage third-party libraries with Mod
There is nothing new under the sun. Can the meta universe go higher?
Leetcode-1175. Prime Arrangements
Realize the recognition and training of CNN images, and process the cifar10 data set and other methods through the tensorflow framework
Flutter动态化 | Fair 2.5.0 新版本特性
Kivy tutorial how to automatically load kV files
Logback log sorting
Flutter动态化 | Fair 2.5.0 新版本特性
Go language unit test 4: go language uses gomonkey to test functions or methods
Students who do not understand the code can also send their own token, which is easy to learn BSC
Thrift threadmanager and three monitors
研发团队资源成本优化实践
Go language web development series 28: solve cross domain access of CORS with gin contrib / CORS
[how to solve FAT32 when the computer is inserted into the U disk or the memory card display cannot be formatted]
IBEM 数学公式检测数据集
如何使用lxml判断网站公告是否更新
Multi person collaborative data annotation based on Baidu brain easydata from scratch
Use vscode to view hex or UTF-8 codes
User and group command exercises
Go language unit test 5: go language uses go sqlmock and Gorm to do database query mock