Skip to content

Commit

Permalink
Fix for php8
Browse files Browse the repository at this point in the history
  • Loading branch information
zyazhb committed Nov 20, 2024
1 parent 3aa5ee3 commit fb8e67e
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 50 deletions.
3 changes: 1 addition & 2 deletions agent/php8/hook/checker/check_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,12 @@ namespace openrasp

void builtin_alarm_info(openrasp::JsonReader &base_json)
{
TSRMLS_FETCH();
LOG_G(alarm_logger).log(LEVEL_INFO, base_json);
}

void builtin_policy_info(openrasp::JsonReader &base_json, ulong hash)
{
TSRMLS_FETCH();
LOG_G(alarm_logger).log(LEVEL_INFO, base_json);
bool skip = false;
if (hash > 0 && slm != nullptr)
{
Expand Down
2 changes: 1 addition & 1 deletion agent/php8/hook/openrasp_command.cc
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ void pre_global_pcntl_exec_COMMAND(OPENRASP_INTERNAL_FUNCTION_PARAMETERS)
{
zend_string *delim = zend_string_init(" ", 1, 0);
zval rst;
php_implode(delim, args, &rst);
php_implode(delim, Z_ARRVAL_P(args), &rst);
zend_string_release(delim);
if (Z_TYPE(rst) == IS_STRING && Z_TYPE_P(command) == IS_STRING)
{
Expand Down
3 changes: 1 addition & 2 deletions agent/php8/hook/openrasp_echo.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,14 @@ extern "C"
#include "Zend/zend_execute.h"
}

static zend_free_op should_free;

int echo_print_handler(zend_execute_data *execute_data)
{
const zend_op *opline = EX(opline);
#if (PHP_MAJOR_VERSION == 7 && PHP_MINOR_VERSION < 3)
zval *inc_filename = zend_get_zval_ptr(opline->op1_type, &opline->op1, execute_data, &should_free, BP_VAR_IS);
#else
zval *inc_filename = zend_get_zval_ptr(opline, opline->op1_type, &opline->op1, execute_data, &should_free, BP_VAR_IS);
zval *inc_filename = zend_get_zval_ptr(opline, opline->op1_type, &opline->op1, execute_data);
#endif
if (inc_filename != nullptr &&
!openrasp_check_type_ignored(XSS_ECHO) &&
Expand Down
3 changes: 1 addition & 2 deletions agent/php8/hook/openrasp_include.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ extern "C"
#include "Zend/zend_execute.h"
}

static zend_free_op should_free;
void include_handler(zval *op1, zend_execute_data *execute_data);
void eval_handler(zval *op1, zend_execute_data *execute_data);
int include_or_eval_handler(zend_execute_data *execute_data);
Expand All @@ -42,7 +41,7 @@ int include_or_eval_handler(zend_execute_data *execute_data)
#if (PHP_MAJOR_VERSION == 7 && PHP_MINOR_VERSION < 3)
inc_filename = zend_get_zval_ptr(opline->op1_type, &opline->op1, execute_data, &should_free, BP_VAR_IS);
#else
inc_filename = zend_get_zval_ptr(opline, opline->op1_type, &opline->op1, execute_data, &should_free, BP_VAR_IS);
inc_filename = zend_get_zval_ptr(opline, opline->op1_type, &opline->op1, execute_data);
#endif
if (inc_filename != nullptr)
{
Expand Down
8 changes: 4 additions & 4 deletions agent/php8/hook/openrasp_pdo.cc
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ void pdo_error_info_intercept(const openrasp::data::V8Material &v8_material, con
static void pdo_exception_intercept(const openrasp::data::V8Material &v8_material, const std::string &driver_name, zval *object)
{
zval rv;
zval *error_info = zend_read_property(php_pdo_get_exception(), object, "errorInfo", sizeof("errorInfo") - 1, 1, &rv);
zval *error_info = zend_read_property(php_pdo_get_exception(), Z_OBJ_P(object), "errorInfo", sizeof("errorInfo") - 1, 1, &rv);
if (nullptr != error_info && Z_TYPE_P(error_info) == IS_ARRAY)
{
error_info_check(v8_material, driver_name, error_info);
Expand All @@ -350,13 +350,13 @@ static void pdo_exception_intercept(const openrasp::data::V8Material &v8_materia
if (driver_name == "mysql" ||
driver_name == "sqlite")
{
zval *code = zend_read_property(php_pdo_get_exception(), object, "code", sizeof("code") - 1, 1, &rv);
zval *code = zend_read_property(php_pdo_get_exception(), Z_OBJ_P(object), "code", sizeof("code") - 1, 1, &rv);
if (nullptr == code)
{
return;
}
std::string error_msg;
zval *message = zend_read_property(php_pdo_get_exception(), object, "message", sizeof("message") - 1, 1, &rv);
zval *message = zend_read_property(php_pdo_get_exception(), Z_OBJ_P(object), "message", sizeof("message") - 1, 1, &rv);
if (Z_TYPE_P(message) == IS_STRING)
{
error_msg = std::string(Z_STRVAL_P(message));
Expand All @@ -376,7 +376,7 @@ static void pdo_exception_intercept(const openrasp::data::V8Material &v8_materia
}
else if (driver_name == "pgsql")
{
zval *message = zend_read_property(php_pdo_get_exception(), object, "message", sizeof("message") - 1, 1, &rv);
zval *message = zend_read_property(php_pdo_get_exception(), Z_OBJ_P(object), "message", sizeof("message") - 1, 1, &rv);
if (nullptr != message && Z_TYPE_P(message) == IS_STRING)
{
std::string error_msg = std::string(Z_STRVAL_P(message), Z_STRLEN_P(message));
Expand Down
1 change: 0 additions & 1 deletion agent/php8/openrasp_error.cc
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ void openrasp_error(int type, openrasp_error_code code, const char *format, ...)
#endif
json_reader.write_string({"message"}, message);
std::string error_content = json_reader.dump();
TSRMLS_FETCH();
LOG_G(rasp_logger).log((severity_level)type, error_content.c_str(), error_content.length(), true, false);
}
else
Expand Down
69 changes: 34 additions & 35 deletions agent/php8/openrasp_hook.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,13 @@ extern "C"

namespace PriorityType
{
enum HookPriority
{
pZero = 0,
pFirst = 1,
pNormal = 2,
pTotal = 3
};
enum HookPriority
{
pZero = 0,
pFirst = 1,
pNormal = 2,
pTotal = 3
};
}
static const int MYSQLI_STORE_RESULT = 0;
static const int MYSQLI_USE_RESULT = 1;
Expand Down Expand Up @@ -152,37 +152,36 @@ typedef void (*php_function)(INTERNAL_FUNCTION_PARAMETERS);
* 在函数体的适当位置添加 origin_function(INTERNAL_FUNCTION_PARAM_PASSTHRU); 可继续执行原始函数
* 执行原始函数前,可调用 zend_get_parameters 等函数获取参数信息
* 执行原始函数后,可查看 return_value 变量获取返回信息
*
*
* @param name 函数完整名称
* @param scope 函数所属 class,全局函数的 scope 为 global
*/
#define DEFINE_HOOK_HANDLER_EX(name, scope, type) \
void scope##_##name##_##type##_handler() \
{ \
HashTable *ht = nullptr; \
zend_function *function; \
if (strcmp("global", ZEND_TOSTR(scope)) == 0) \
{ \
ht = CG(function_table); \
} \
else \
{ \
zend_class_entry *clazz; \
std::string scope_str(ZEND_TOSTR(scope)); \
openrasp::string_replace(scope_str, ZEND_TOSTR(BACKSLASH_IN_CLASS), "\\"); \
if ((clazz = static_cast<zend_class_entry *>( \
zend_hash_str_find_ptr(CG(class_table), scope_str.c_str(), scope_str.length()))) != NULL) \
{ \
ht = &(clazz->function_table); \
} \
} \
if (ht && \
(function = static_cast<zend_function *>(zend_hash_str_find_ptr(ht, ZEND_STRL(ZEND_TOSTR(name))))) != NULL && \
function->internal_function.handler != zif_display_disabled_function) \
{ \
origin_##scope##_##name##_##type = function->internal_function.handler; \
function->internal_function.handler = hook_##scope##_##name##_##type; \
} \
#define DEFINE_HOOK_HANDLER_EX(name, scope, type) \
void scope##_##name##_##type##_handler() \
{ \
HashTable *ht = nullptr; \
zend_function *function; \
if (strcmp("global", ZEND_TOSTR(scope)) == 0) \
{ \
ht = CG(function_table); \
} \
else \
{ \
zend_class_entry *clazz; \
std::string scope_str(ZEND_TOSTR(scope)); \
openrasp::string_replace(scope_str, ZEND_TOSTR(BACKSLASH_IN_CLASS), "\\"); \
if ((clazz = static_cast<zend_class_entry *>( \
zend_hash_str_find_ptr(CG(class_table), scope_str.c_str(), scope_str.length()))) != NULL) \
{ \
ht = &(clazz->function_table); \
} \
} \
if (ht && \
(function = static_cast<zend_function *>(zend_hash_str_find_ptr(ht, ZEND_STRL(ZEND_TOSTR(name))))) != NULL) \
{ \
origin_##scope##_##name##_##type = function->internal_function.handler; \
function->internal_function.handler = hook_##scope##_##name##_##type; \
} \
}

#define OPENRASP_HOOK_FUNCTION_PRIORITY_EX(name, scope, type, priority) \
Expand Down
4 changes: 2 additions & 2 deletions agent/php8/openrasp_log.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ extern "C"
#include "ext/standard/url.h"
#include "ext/standard/php_string.h"
#include "ext/standard/php_array.h"
#include "ext/standard/microtime.h"
// #include "ext/standard/microtime.h"
#include "ext/date/php_date.h"
#include "zend_smart_str.h"
#include "ext/json/php_json.h"
Expand Down Expand Up @@ -394,7 +394,7 @@ bool RaspLoggerEntry::openrasp_log_stream_available(log_appender appender_int)
efree(file_path);
break;
}
stream = php_stream_open_wrapper(file_path, "a+", REPORT_ERRORS | IGNORE_URL_WIN, nullptr);
stream = php_stream_open_wrapper(file_path, "a+", REPORT_ERRORS | IGNORE_URL, nullptr);
if (stream)
{
if (need_create_file && FAILURE == VCWD_CHMOD(file_path, RASP_LOG_FILE_MODE))
Expand Down
2 changes: 1 addition & 1 deletion build-php8.sh
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ output_base="$script_base/rasp-php-$(date +%Y-%m-%d)"
output_ext="$output_base/php${php_zts}/${php_os}-php${php_version}-${php_arch}"

# 编译
cd agent/php7
cd agent/php8
phpize --clean
phpize

Expand Down

0 comments on commit fb8e67e

Please sign in to comment.