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 建设人民城市 致广大而尽精微 7904121
- 2 香港火灾已造成94人遇难 7809503
- 3 山东威海地震 有人梦中被晃醒 7714156
- 4 一起来看冬日里的中式美学 7616681
- 5 央视曝光小诊所用医保卡换豆油和面 7523467
- 6 手机厂商将110标注为“匪警”引质疑 7428747
- 7 睡觉时猛然一抖是身体在求救吗 7330005
- 8 2人徒步梅里雪山大环线失联超120天 7237325
- 9 王毅:日本现职领导人公然开历史倒车 7137683
- 10 四川官宣:婚假由5日延到20日 7039644

百度安全应急响应中心
