# Abusing VSCode: From Malicious Extensions to Stolen Credentials (Part 2)

- Type: Blog post
- Author: Fabian Kammel & Kevin Ward
- Published: 26 June 2024
- Tags: Security, Assurance, Software Supply Chain Security
- Source: https://control-plane.io/blog/abusing-vscode-from-malicious-extensions-to-stolen-credentials-part-2

- [High Level Architecture](https://control-plane.io/blog/abusing-vscode-from-malicious-extensions-to-stolen-credentials-part-2#high-level-architecture)
- [VSCode API](https://control-plane.io/blog/abusing-vscode-from-malicious-extensions-to-stolen-credentials-part-2#vscode-api)
- [Electron safeStorage](https://control-plane.io/blog/abusing-vscode-from-malicious-extensions-to-stolen-credentials-part-2#electron-safestorage)
- [Chromium OSCrypt](https://control-plane.io/blog/abusing-vscode-from-malicious-extensions-to-stolen-credentials-part-2#chromium-oscrypt)
- [Analysis](https://control-plane.io/blog/abusing-vscode-from-malicious-extensions-to-stolen-credentials-part-2#analysis)
- [Limited Documentation](https://control-plane.io/blog/abusing-vscode-from-malicious-extensions-to-stolen-credentials-part-2#limited-documentation)
- [No Sandboxing](https://control-plane.io/blog/abusing-vscode-from-malicious-extensions-to-stolen-credentials-part-2#no-sandboxing)
- [Hardcoded Chromium Password](https://control-plane.io/blog/abusing-vscode-from-malicious-extensions-to-stolen-credentials-part-2#hardcoded-chromium-password)

[73% of developers use VSCode](https://survey.stackoverflow.co/2023/#section-most-popular-technologies-integrated-development-environment) to work on hobby projects and enterprise software alike. At the same time, a disproportional amount of independent security research has been performed on VSCode to enable the community to make informed, risk-based decisions when it comes to VSCode adoption and hardening. Both [Check Point](https://blog.checkpoint.com/securing-the-cloud/malicious-vscode-extensions-with-more-than-45k-downloads-steal-pii-and-enable-backdoors/) and [Aqua](https://www.aquasec.com/blog/can-you-trust-your-vscode-extensions/) show how easily malicious extensions steal personal identifiable information (PII) and other sensitive data by impersonating popular extensions in the [VSCode Marketplace](https://marketplace.visualstudio.com/). [Cycode](https://cycode.com/blog/exposing-vscode-secrets/) unveiled that stealing tokens from other extensions, even though protected through VSCode's internal vscode-encrypt rust module, is trivial. Nothing keeps the same extension from sending this critical information off to a nefarious command & control (C2) server either.

Since Cycode published these findings, [VSCode migrated away from their internal rust module](https://github.com/microsoft/vscode-discussions/discussions/748#discussioncomment-6682193) and instead use [Electron](https://www.electronjs.org/)'s [safeStorage API](https://www.electronjs.org/docs/latest/api/safe-storage) to enable their [SecretStorage API](https://code.visualstudio.com/api/references/vscode-api#SecretStorage). In this blog post we dive deep into the implementations of VSCode, Electron & Chromium, to demonstrate that the same limitations as initially laid out by Cycode still exist today: **any malicious extension can steal other extension's credentials**, even though they are managed and namespaced by the VSCode SecretStorage API.

## High Level Architecture

VSCode relies on Electron, a popular open-source framework to create desktop applications using web technologies, which run on any platform. The rendering stack in turn utilizes the Chromium browser engine, which also provides APIs to securely store secrets backed by the operating system. On Linux this functionality is implemented by [libsecret](https://wiki.gnome.org/Projects/Libsecret).

![Diagram 1 from the abusing vscode from malicious extensions to stolen credentials part 2 article](https://cdn.sanity.io/images/smdn7m7k/production/3ec994cdddba2d45776140be88c2af70757a8529-1536x1748.png?w=1600&fm=webp&q=80&auto=format)

Since libsecret is a Linux specific implementation, some details may be different for MacOS ([keychain](https://support.apple.com/guide/keychain-access/what-is-keychain-access-kyca1083/mac)) and Windows ([DPAPI](https://en.wikipedia.org/wiki/Data_Protection_API)), but the same attack vector should work on any OS.

## VSCode API

The VSCode API includes several security-relevant functionalities, most notably the SecretStorage class. It allows extensions to store and retrieve secrets beyond a single session and workspace. Its documentation states that this is a "[storage utility for secrets [and] information that [are] **sensitive**\](https://code.visualstudio.com/api/references/vscode-api#SecretStorage)". It also takes care of persisting the secrets between sessions in a shared SQLite3 database, which also holds the user's configuration and common VSCode settings. It is stored in the user's home directory: ${HOME}/.config/Code/User/globalStorage/state.vscdb.

The database holds a single table, ItemTable, with a very simple layout that features only two columns: key and value.

Secrets always have a key that is prefixed with secret://, followed by a simple JSON object that tracks the ID of the extension for which this secret is stored (this enables namespaced secrets), as well as an arbitrary key which is used by the extension to look up its secrets. A full key value looks like this:

```text
secret://{"extensionId":"my.extension","key":"secret-token"}
```

The value column holds another JSON object that mainly stores the encrypted secret using a byte slice representation:

```text
{"type":"Buffer","data":[118,49,49,79,119,...]}
```

## Electron safeStorage

The [Electron safeStorage API](https://www.electronjs.org/docs/latest/api/safe-storage) is a very thin wrapper, [only ~100 lines of C++](https://github.com/electron/electron/blob/5d120359f6f6adf730455189d099b0a63a0e5cc9/shell/browser/api/electron_api_safe_storage.cc), to call down to the Chromium OSCrypt package. Its API documentation promises to protect "[data stored on disk from being **accessed by other applications** or users with full disk access](https://www.electronjs.org/docs/latest/api/safe-storage)".

## Chromium OSCrypt

This is the library where the actual cryptographic operations are implemented. Let's step through each major operation performed.

### Encryption / Decryption

The [OSCryptImpl::EncryptString](https://source.chromium.org/chromium/chromium/src/+/main:components/os_crypt/sync/os_crypt_linux.cc;l=152;bpv=1;bpt=1) and [OSCryptImpl::DecryptString](https://source.chromium.org/chromium/chromium/src/+/main:components/os_crypt/sync/os_crypt_linux.cc;l=187) is what is called by the Electron safeStorage API.

The core [encryption](https://source.chromium.org/chromium/chromium/src/+/main:components/os_crypt/sync/os_crypt_linux.cc;l=152) mechanism relies on AES-128 in cipher-block-chaining (CBC) mode. The [initialization vector (IV) is hard-coded](https://source.chromium.org/chromium/chromium/src/+/main:components/os_crypt/sync/os_crypt_linux.cc;l=172) to the string of 16 spaces (0x20 hex). No modifications are made to the user-provided plain text.

Once the cipher-text is produced, a prefix is added. The prefix v11 means encrypted with a [randomly generated password](https://control-plane.io/blog/abusing-vscode-from-malicious-extensions-to-stolen-credentials-part-2#key-generation). The value v10 means that [the hard-coded key peanuts](https://source.chromium.org/chromium/chromium/src/+/main:components/os_crypt/sync/os_crypt_linux.cc;l=316) was used to encrypt the string, which is used as a fallback on Linux systems where the keyring or other secure storage is not available.

![Diagram 2 from the abusing vscode from malicious extensions to stolen credentials part 2 article](https://cdn.sanity.io/images/smdn7m7k/production/82facf27a7fb990d5e009f9d06020f5a57fe42e1-2714x874.png?w=1600&fm=webp&q=80&auto=format)

### Key Management

The key is derived from a password, which in turn is stored in the user's local keyring, the [implementation of this is backed by libsecret](https://source.chromium.org/chromium/chromium/src/+/main:components/os_crypt/sync/key_storage_libsecret.cc). The [salt](https://source.chromium.org/chromium/chromium/src/+/main:components/os_crypt/sync/key_storage_libsecret.cc) and [iteration count](https://source.chromium.org/chromium/chromium/src/+/main:components/os_crypt/sync/key_storage_libsecret.cc) are hard-coded.

![Diagram 3 from the abusing vscode from malicious extensions to stolen credentials part 2 article](https://cdn.sanity.io/images/smdn7m7k/production/0991bba17d18cc933d39f9877b95c7bb748cfc79-1904x874.png?w=1600&fm=webp&q=80&auto=format)

### Key Generation

In case there is not yet a password stored in the keyring for this application [a new random password is base64 encoded](https://source.chromium.org/chromium/chromium/src/+/main:components/os_crypt/sync/key_storage_libsecret.cc;l=65) and stored in the keyring.

![Diagram 4 from the abusing vscode from malicious extensions to stolen credentials part 2 article](https://cdn.sanity.io/images/smdn7m7k/production/62131838ab2f6234c3ca3eda8756d64ad814267b-2050x292.png?w=1600&fm=webp&q=80&auto=format)

Even though the password is base64 encoded, it is never decoded and simply fed as-is into the key derivation function! This may or may not have a been a frustrating culprit when developing the PoC 😉

## Analysis

Equipped with this knowledge, we can dive into the problems that arise from the chosen implementations.

### Limited Documentation

The attentive reader may have picked up on the changing promises made by the documentation about the capabilities to store secrets. Let's repeat them side by side:

- [VSCode SecretStorage API](https://code.visualstudio.com/api/references/vscode-api#SecretStorage): "storage utility for secrets, information that is **sensitive**" + [Electron safeStorage API](https://www.electronjs.org/docs/latest/api/safe-storage): "This module protects data stored on disk from being **accessed by other applications** or users with full disk access." + [Chromium OSCrypt API](https://source.chromium.org/chromium/chromium/src/+/main:components/os_crypt/): "This directory contains OSCrypt implementations that support cryptographic primitives that allow **binding data to the OS user**." + Libsecret itself [provides no documentation about it's security assumptions](https://gnome.pages.gitlab.gnome.org/libsecret/), but a [SO post](https://superuser.com/questions/1527816/what-is-libsecret) puts it's quite succinctly: "allows a user to store, e.g., passwords in a way that they are easily accessible **for the logged on user**, but very hard to access by someone else."

Even though there is no evidence to support it, the Electron API claims to keep secrets safe from other applications. Even worse, the VSCode documentation makes no further claims and declares the storage secure for sensitive information.

When API documentation provides too little information about their capabilities and assumptions, developers are put at a disadvantage to make good decisions.

We have started conversations with the [VSCode](https://github.com/microsoft/vscode/issues/213903) and [Electron](https://github.com/electron/electron/issues/42318) projects to improve the documentation and enable developers to make informed decisions.

### No Sandboxing

As there is [no sandboxing implemented in VSCode](https://github.com/microsoft/vscode/issues/52116), any extension has the same permissions as any other user process. Extensions are free to read and write files on disk, make network requests, or call other available APIs and libraries.

Therefore, an evil extension is able to directly call out to libsecret and sqlite3 to read encrypted secrets from other extensions and decrypt them using the OS-managed key.

![Diagram 5 from the abusing vscode from malicious extensions to stolen credentials part 2 article](https://cdn.sanity.io/images/smdn7m7k/production/a011015c6d2c9e9852bca35df97f944f20a5b585-1714x1748.png?w=1600&fm=webp&q=80&auto=format)

[We have released an example extension that does exactly this.](https://github.com/controlplaneio/research-vscode) It has some built-in safeguards, but when you run the "Steal Secrets" function, it will happily decrypt and print all VSCode-managed secrets from other extensions.

As there is no plan to support extension sandboxing, enterprises need to take on the challenge of discovering, classifying and limiting access to malicious extensions before they can harm their business.

### Hardcoded Chromium Password

As Linux is a more fragmented ecosystem than MacOS and Windows, when there is no keyring available, Chromium falls back to **using a static password to encrypt secrets**. This level of security is equal to not using a password at all, as there is no secret knowledge required to decrypt the stored information. Therefore, even other users on a shared system are able to decrypt data if they get access to an encrypted string.

This opens up new avenues for an attacker. Breaking availability, e.g., via denial of service or misconfiguration, is usually an easier target than breaking state-of-the-art encryption.

**This limitation should be communicated more clearly, as the fallback is a silent one.**

## Disclosure

We followed VSCode's [security policy](https://github.com/microsoft/vscode/blob/main/SECURITY.md) and disclosed our findings to Microsoft using the Microsoft Security Response Center (MSRC).

Microsoft reviewed our findings and concluded that "users should only install extensions they trust" [as publicly documented](https://code.visualstudio.com/docs/editor/workspace-trust#_restricted-mode).

We will continue our efforts with the VSCode & Electron teams to enable users and developers to make better security decisions in future.
