Penetration Testing Browser-Based Crypto Wallets
An Article by:
Justin Regele
Date:
February 14, 2024
Recently I had the opportunity to give a brief talk at Secureum’s TrustX conference in Istanbul on some attacks I’ve developed while penetration testing various crypto wallets. Given the brevity of the talk, I wanted to take a moment to add some context.
First off, regarding the title, I called the wallets browser-based just because not all of them were browser extensions. A browser-based wallet is simply a wallet built using web tech that uses a browser as the application host. This makes it different from something like an Electron app which, while also based on web tech, runs as a standalone application.
The first challenge anyone with a background in web app pentesting encounters when targeting a browser extension is that our normal toolkit no longer really applies.
In Web2 applications, we’re largely dealing with APIs; in legacy applications, straight HTML forms. The front end is user-facing code making HTTP requests to the back end. Because all these requests travel over the TCP/IP stack, we can set up SOCKS proxies between the browser and our network interface and intercept requests, change them in transit, repeat them, and launch attacks against them. Easy.
With a browser extension, requests no longer hit our network interface. They don’t even leave the browser. The Postmessage API handles communication between the web page and the extension. This is important to understand because our attack surface is now defined by the kinds of messages we can get through Postmessage.
Architecture
In order to properly assess an application, we need to understand how it’s built. In a Web2 application, we have the client (browser) making HTTP requests to the server(s). We say servers plural because microservices may be at play and we might be dealing with several different APIs, which are likely running behind Load Balancers. These forward the requests to an available server running the applications code, which will then communicate with the backend datastore. Each hop will run over TCP/IP, running either HTTPS, websockets, or whatever protocol the backend datastore requires.
A browser extension wallet is different. The client is still a web page, which in Web3 lingo is often called a dApp (decentralized App). The wallet is the browser extension that protects and holds the keys for signing messages and relaying them to the blockchain through an RPC node. Because the blockchain is decentralized, there is no single point of entry, but to get any transaction processed, the TX must get into a Mempool somewhere. So this is what the wallet provides via an RPC provider.
We could also just invoke an RPC provider from within the dApp by passing a provider URL to a Web3.js or Ethers.js object, but this would not provide any sort of bastion around our key material, nor would it be able to work for any website. Browser-based wallets that are Single Page Applications work this way, and the keystore is encrypted in browser storage.
It is also worth mentioning that a lot of Web2 architecture is still used even in Web3 applications, as the wallet will often query services for gas prices, token prices, and other indexer services.
In the simplest of terms, a browser extension is divided into three parts.

The Inpage script is the javascript that gets injected into the page itself. When a page is loaded, the extension’s Content Script embeds the Inpage code into the page’s DOM.
For a wallet, this will expose an object to dApps to facilitate communication with the content script. We can call this the wallet’s externally facing API object. Either the Content Script or the Inpage code will also initialize a series of event handlers to listen for messages. The Content Script can then pass these to the Background script, which since Manifest Version 3 (MV3), is a Service Worker. The content script acts as a gateway to the background script, where the secrets should be handled. This communication channel will utilize Browser APIs not available to a regular page, ensuring that pages cannot initiate connections to the background service worker. The security of the wallet, and our ability to launch attacks against it, will depend on how securely separated these layers are.
The boundary between Inpage and Content Script is where we can see how the comms channels for the wallet are constructed. There are various ways to set up comms between these two realms, with postMessage API and Custom Events among the most common. Generally, the wallet’s externally facing API object will dispatch one of these methods from Inpage, which will be heard from the Content Script. This is going to be done under the hood of the object, since the object is designed to present an API for dApp developers to interact with the wallet.
This Inpage object should never be considered a security boundary, as it is easily bypassed. If a sensitive function in the Content Script can be called over RPC using postMessage, but no function exists in the Inpage object, a malicious dApp can still make that RPC call. It is quite trivial to do so. This is essentially what takes place during Attack 3,described below.
There are also the Popup pages and associated Javascript that control the UI of the extension. While these can be vulnerable to Cross-Site Scripting, we will not go into much detail about them here.
Threat Models
For testing, we have taken two different approaches, each with a different threat model:
- Malicious dApp
- Malicious Extension
In both cases, we had to develop the tooling ourselves, but since the browser itself is the host, doing so was just a matter of writing some Javascript.
For the malicious dApp approach, our attack surface is limited to what a single page is capable of doing. Our threat model assumes the dApp is compromised. Either it is malicious in and of itself, or a supply chain attack has infected a module somewhere.
For malicious extensions, some of the limitations of the Same Origin Policy no longer apply. Because extensions inject the Inpage code into the page, that code then becomes a part of the Origin of the dApp, and will have full access to the dApp’s data. What the malicious extension cannot access or interact with, however, are the origins of the extension itself, such as the Content Script or Background Service Workers. This is why attacks 4 and 5 — both of which use the malicious extension attack vector — attack the dApp itself.
What is Our Goal?
In pentesting a web application, our end goal is essentially server compromise. We want to get Remote Code Execution to grab the secrets, dump the database, and so on. If we can dump the database through a SQL injection vulnerability, even better. Other risky vulnerabilities such as Cross Site Scripting or Cross-Site Request Forgery can also be leveraged to try to dump the secrets — i.e. the database passwords, the database itself, cloud services API access keys, encryption keys, and so on.
In a wallet, our goal would be to gain access to funds. The most direct way to do this would be to steal the key material. But if we can get access to funds by leveraging the wallet without accessing the key material, the end result would be the same.
HD Wallets and Seed Phrases
A quick word on HD wallets and seed phrases. A lot of people understand in a general sense public key infrastructure, where there is a public/private key pair. For those unfamiliar with how this applies to blockchain, the public key would be the address of the wallet, and the private key is used to cryptographically sign transactions. The private key is a large number, which to be readable by humans is represented in hexadecimals. This is not useful for memorization purposes. Seed phrases, also known as mnemonics, are phrases of 12 or 24 words that can be used to derive a private key.
In a Hierarchically Derivative wallet, a seed phrase can be used to derive multiple private keys for different tokens, so a single seed phrase can be used on multiple chains. So while getting access to a particular private key would allow an attacker to take control of that wallet’s access to that specific token, it MIGHT not give access to other tokens.
This is why our attacks focus on getting the seed phrase of the wallet, rather than the private key.
Pentesting: The Attacks
Attack 1. Seed Phrase Dragnet
A really common UX pattern amongst wallets is the Copy To Clipboard pattern. This is because backing up a seed phrase is paramount if a user wants to maintain access to their funds in the event their wallet is deleted or becomes corrupted. The problem with this very common practice is that it introduces a step that exposes key material before it can be saved to disk.
Note that saving the seed phrase to disk is dangerous only under the threat model where the machine itself has been compromised. And this threat model is usually disregarded as out of scope during assessments because once the host is compromised, keeping a wallet secure becomes nearly impossible.
Copy to Clipboard is dangerous because the clipboard by its very nature needs to be accessible to all applications running on the user’s computer, including other browser tabs. So if we assume the presence of malicious javascript at scale (say an infected node dependency) that monitors the clipboard for 12- or 24-word strings, we have potentially exposed sensitive material.
There are caveats to this attack, though. New browser protections are in place that require the user to grant domain access to the clipboard. We have also found that the browser tab must be in focus for the clipboard harvesting to execute. So for this attack to truly work, several preconditions need to be met.
- An attack page is loaded in a browser tab that the victim has permitted to access the clipboard, either as a result of social engineering or legitimate use. In the case of legitimate use, we are probably dealing with a supply chain attack to run the malicious listener code.
- The user has created a new wallet, or backed up the seed phrase from an existing wallet.
- While the seed phrase is still stored in the clipboard, the attack page has come into focus again, harvesting the clipboard data.
The number of required preconditions significantly reduces the likelihood of such an attack succeeding, but again we need to think of this as a dragnet method. If such an attack were launched at scale, eventually a wallet would get successfully breached, and there might not be any record it had been compromised. An attacker would have equal control over the wallet as the victim for the entire lifespan of the wallet. The point at which the attacker drains the wallet could lie far in the future once the victim has significant resources on-chain.
Attack 2. Clickjacking
This attack is an optimized version of the first, but it also relies on the wallet not having any framing protections. What this means is that pages of the wallet can be loaded inside an <iframe> tag. Clickjacking (also known as UI Redressing) attacks are standard Web2 security problem developed in response to increased security around Cross-Site Request Forgery (CSRF) attacks.
The idea behind such attacks is that if a CSRF token needs to be loaded from the server in a hidden form field, why not just leverage that actual form but make it invisible? The attack works because an <iframe> can have an Opacity set to 0, making it completely invisible,while its Z-index can still be on top of whatever the victim will see.
If a wallet is equipped with a convenient “Copy to Clipboard” button and has no framing protections, the attack is simple. A malicious web page loads in the hidden iframe of the wallet right over the button the user will be required to push. The victim is then tricked into clicking this hidden “Copy to Clipboard” button, exposing the seed phrase in the clipboard while the attacker monitors it.
Clickjacking attacks can be prevented in regular HTTP applications with the X-FRAME-OPTIONS header, or by using a frame-ancestors directive in theContent-Security-Policy. If no CSP is provided in MV3, a default CSP is implemented by MV3 that does not include frame-ancestors, so it still must be added.
Attack 3. Asking Nicely
When an extension injects its inpage API object into a dApp, that object is used by dApp developers as a convenience. Undocumented — and unintended — functionality can still exist on the other side of the RPC channel.
Let’s say a Wallet’s API object has just two functions:
- connect()
- sentTransaction()
We have the following code in the Content Script that listens for messages.

Because the Content Script indexes the Wallet object to make the function call, any method that exists inside Wallet is accessible, even if it is not available in the external API object.
So if our Wallet object looks like this:

We could create a postMessage request and invoke handleMessage. If this handleMessage function exposes further functionality, such as functions that return the seed phrase, it becomes possible to extract the seed phrase with only a specially crafted JSON payload.
Attack 4. Transaction Piggybacking
Under a different threat model, where a malicious browser extension is installed, the attacks no longer come from the dApp but attack the dApp itself. Malicious extensions could either be deliberately masquerading as legitimate, or they could have been compromised in some way.
Here, the same origin policy doesn’t protect the dApp, although the extension cannot access the wallet’s internals. A wallet will need to install some sort of listener in the dApp in order to process the postMessage requests, so during initialization, it will create event listeners that will listen for particular messages.
A malicious extension can install the same listener with different functions. And because it would have full access to the page, it would also have full access to the RPC interface. In this circumstance, there is nothing that would prevent a malicious extension from simply requesting the transfer of all funds to an attacker-controlled address at any given moment — except for the fact that that would be an incredibly unwise thing to do. The wallet’s UI will take control of the flow as soon as the RPC message is handled, and if a user had not initiated the request, sudden popups requesting approval of the funds transfer would alert them that something was wrong.
However, because a malicious extension can listen for legitimate requests, it can piggyback on them and follow up the request immediately with a subsequent request. Because the malicious extension has heard the initial request, it can even copy all the data from the request into the new request, replacing only the receiver’s address.
For wallets designed to handle only a single transaction at a time, the malicious request might overwrite the original, legitimate one. And if the user does not check that the address is actually the intended recipient, approving the piggybacked request would result in the irretrievable loss of funds.
Even if a wallet doesn’t overwrite the original request, but instead stacks them in an array, piggybacking could also still pose a risk for users. Say a user approves the original transaction and a second appears immediately afterward. This might lead them to believe that the first transaction had failed and prompt them to approve the second transaction. Because transactions take time to mine, the attacker could also leverage Front Running by including higher gas fees in the malicious transaction, in hopes that the malicious second transaction is mined before the first, which would beat any insufficient funds errors that might occur running a transaction twice.
This threat model poses some difficult issues. The adversary here has full access to the dApp, so even attempting to authenticate requests through cryptographic signatures is useless, since the attacker will be able to access the same signing keys. This problem sits at the intersection of the limited technological capabilities browsers currently have, the intended functionality of browser extensions, and the goals of Web3.
It might be tempting to disregard this threat model as out of scope for a security audit, as is often the case with compromised host threat models. But a compromised host is a different scenario than a compromised browser extension. Until Secure EcmaScript (SES) compartmentalization becomes standard practice, the threat of supply chain attacks on ANY browser extension means that a lot of unsuspecting users could remain targets to this sort of attack.
Attack 5. Enabling Insecure Development Mode Features
All modern web development will produce different builds of an application, and any released application will be a production build. But while a project is under development, developers often have different needs than eventual end users. They need to test the application while it’s under construction, which can involve debugger features that could pose a security risk in a Production build.
Popular build tools like Webpack use environment variables to determine at build time what sort of build will be produced. A development build might have source maps to assist developers in debugging the application, whereas a production build would only produce minified — and possibly obfuscated — Javascript code.
Vulnerabilities can arise if specific components in the application are susceptible to change behavior based on the build environment. The threat model for this type of attack requires a) that the wallet is a single page application rather than an extension; and b) that the attacker is leveraging a malicious browser extension. Because the malicious extension can interact with the DOM of the wallet, it is possible in certain scenarios that development mode features can be enabled to launch an attack.
In the case of Angular.js applications, __ngContext__ gives read/write access to Angular variables on a component in the DOM. Malicious extensions can inject code that will fetch the root node, walk the DOM to the target component, and interact with it. If the enabled functionality allows the attacker to launch further attacks, the wallet itself could become compromised.
These then are a few of the attacks I have developed while penetration testing various browser-based crypto wallets, along with my thoughts on mitigation. The list will almost certainly grow and evolve along with the risk landscape. The nature of the work we do is that we must constantly stay on top of the evolution of the technology in order to stay ahead of the bad actors. That vigilance is Defense’s promise to you.