HHVM 函数动态劫持技术探究
int main(int argc, char** argv) {
? ????????? ....
? ?? return HPHP::execute_program(args.size(), &args[0]);
}
int execute_program(int argc, char **argv) {
? ?? ....
????? ret_code =execute_program_impl(argc, argv);
??? }
? ????????? }

#include"hphp/runtime/base/array-data.h"
#include"hphp/runtime/base/array-init.h"
#include"hphp/runtime/ext/extension.h"
#include"hphp/runtime/ext/std/ext_std_function.h"
namespace HPHP {
static int64_t HHVM_FUNCTION(example_hook){
?//Array arr = HHVM_FN(get_defined_functions)();
?return 1;
}
?
static class ExampleExtension : publicExtension {
?public:
?ExampleExtension() : Extension("hook") {}
?virtual void moduleInit() {
???HHVM_FE(example_hook);
???loadSystemlib();
? }
} s_hook_extension;
?
HHVM_GET_MODULE(hook)?
virtual void moduleLoad(constIniSetting::Map& /*ini*/, Hdf /*hdf*/) {
?virtual void moduleInit() {}
?virtual void moduleShutdown() {}
?virtual void threadInit() {}
?virtual void threadShutdown() {}
?virtual void requestInit() {}
?virtual void requestShutdown() {}
实现一个简单扩展,只需要使用HHVM_FUNCTION关键词定义一个函数:
return_valueHHVM_FUNCTION(func_name,params)
其中return_value为返回值,func_name为函数名,params为不固定参数,然后在moduleInit或者moduleLoad函数中写入:
定义函数与php类似,其中函数返回值和参数类型需要与HHVM_FUNCTION中定义C++函数相对应,具体对应关系为:
void moduleLoad(const IniSetting::Map&ini, Hdf hdf) {
?std::set<std::string> extFiles;
? …..
? //Load up any dynamic extensions from extension_dir
?std::string extDir = RuntimeOption::ExtensionDir;
?
? for(std::string extFile : extFiles) {
???moduleLoad(extFile);
? }
? …..
? for(auto& ext : s_ordered) {
???ext->moduleLoad(ini, hdf);
? }
}


void reNameFunc(const String& old_name,const String& new_name)
? {
???????VMRegAnchor _;
???????auto const old = old_name.get();
???????auto const oldNe =const_cast<NamedEntity*>(NamedEntity::get(old));
???????Func* func_2 = Unit::lookupFunc(oldNe);
???????auto const fnew = new_name.get();
???????auto const newNe =const_cast<NamedEntity*>(NamedEntity::get(fnew));
???????oldNe->setCachedFunc(nullptr);
???????newNe->m_cachedFunc.bind(rds::Mode::Normal);
???????newNe->setCachedFunc(func_2);
? }




目前由于php7的推出,hhvm已经在性能等多方面呈现出劣势,而facebook也将hhvm全面转向hack,因此未来使用hhvm的可能会越来越少,笔者在此的研究更多的是分享一种hook的思路。
百度安全应急响应中心
百度安全应急响应中心,简称BSRC,是百度致力于维护互联网健康生态环境,保障百度产品和业务线的信息安全,促进安全专家的合作与交流,而建立的漏洞收集以及应急响应平台。地址:https://bsrc.baidu.com


长按关注
关注公众号:拾黑(shiheibook)了解更多
[广告]赞助链接:
四季很好,只要有你,文娱排行榜:https://www.yaopaiming.com/
让资讯触达的更精准有趣:https://www.0xu.cn/
关注网络尖刀微信公众号随时掌握互联网精彩
- 1 习近平将发表二〇二六年新年贺词 7904141
- 2 2026年国补政策来了 7808738
- 3 东部战区:开火!开火!全部命中! 7712893
- 4 2026年这些民生政策将惠及百姓 7616985
- 5 小学食堂米线过期2.5小时被罚5万 7519709
- 6 解放军喊话驱离台军 原声曝光 7428214
- 7 为博流量直播踩烈士陵墓?绝不姑息 7327605
- 8 每月最高800元!多地发放养老消费券 7238391
- 9 数字人民币升级 1月1日起将计付利息 7141831
- 10 2026年1月1日起 一批新规将施行 7040675








百度安全应急响应中心
