> For the complete documentation index, see [llms.txt](https://docs.talsec.app/appsec-articles/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.talsec.app/appsec-articles/articles/how-to-hack-and-protect-flutter-apps-steal-firebase-auth-token-and-attack-the-api.-pt.-3-3.md).

# How to Hack & Protect Flutter Apps — Steal Firebase Auth token and attack the API. (Pt. 3/3)

*JWT Token, MiTM Attacks and API Protection. Keep them properly protected and don’t put your enterprise at risk.*

*Part 1 (*[*link*](/appsec-articles/articles/how-to-hack-and-protect-flutter-apps-simple-and-actionable-guide-pt.-1-3.md)*) ↓*

* Disassemble app.
* Extract its secrets.

*Part 2 (*[*link*](/appsec-articles/articles/how-to-hack-and-protect-flutter-apps-owasp-mas-and-rasp.-pt.-2-3.md)*) ↓*

* Make a fake clone.
* Check every transmitted JSON.
* Inject code.

*Part 3 (this article) ↓*

* **Steal authentication tokens.**
* **and attack the API.**

<figure><img src="https://1548930415-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNjTFXsqCLQ3RU2oA2uHC%2Fuploads%2FmV8DsubZrpIG6pTmtZio%2Fimage.png?alt=media&amp;token=abfc502a-9e11-46d0-bbb5-e1b451445017" alt=""><figcaption><p>How to Hack and How to Protect Flutter Apps</p></figcaption></figure>

### First things first. JSON web tokens. <a href="#a25d" id="a25d"></a>

A token is a piece of data that represents a specific identity or authorization. There are different types of tokens, including authentication tokens, which are used to authenticate a user’s identity, and bearer tokens, which grant access to a protected resource.

JWT, or JSON web tokens, are types of tokens used for authenticating and authorizing users. These tokens are encoded and signed, allowing the server to verify their authenticity. However, JWT tokens can be vulnerable to certain attacks, such as weak token signing and token tampering.

Weak tokens are tokens that have been compromised or are otherwise insecure. This can include tokens with weak or easily guessable signing keys, or tokens that have been stolen or otherwise obtained by an attacker. These weak tokens can be used to gain unauthorized access to a system or a protected resource.

The JWT standard allows the non-use of an algorithm (”alg”:”none”) to sign the token. This is of course a very bad practice. A user could change his rights on the fly (from “role”: “user” to “role”: “admin”), without any control by the server. In the same way, if the configuration carried out at the server level accepts different algorithms and the “none” variable, which consists of not having cryptographic functions, it will be possible for an attacker to bypass the integrity verification function to access the data of other users or even of the admin. This can be mitigated by choosing a reliable algorithm (SHA256) and force server to always check algorithm, and therefore reject the “none” variable.

<figure><img src="https://1548930415-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNjTFXsqCLQ3RU2oA2uHC%2Fuploads%2FemqLD6yXbeIRJcJwg0P8%2Fimage.png?alt=media&amp;token=4db3bac3-f1d1-4bbd-96ee-605a222c5e08" alt=""><figcaption><p>Example of JWT (source: https://jwt.io/introduction)</p></figcaption></figure>

### Firebase Authentication <a href="#id-0835" id="id-0835"></a>

Firebase Authentication is Flutter Apps’ most favorable authentication service. It allows developers to easily integrate user authentication into their applications using a variety of authentication methods, such as email and password, phone number, or social media accounts like Google, Facebook, and Twitter.

<figure><img src="https://1548930415-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNjTFXsqCLQ3RU2oA2uHC%2Fuploads%2FvPNcmU2Mk1L5b8hICUCE%2Fimage.png?alt=media&amp;token=14480dfb-2f7a-465b-972c-1d07fb44f324" alt=""><figcaption><p>Firebase Authentication on pub.dev</p></figcaption></figure>

When a user signs up for an account using Firebase Authentication, the service generates an authentication token that is associated with the user’s account. This token is then stored on the user’s device and is used to verify their identity whenever they access the application.

When the user attempts to log in to the application, the authentication token is sent to the Firebase Authentication server, where it is verified and then passed back to the application. If the token is valid, the user is granted access to the application.

<figure><img src="https://1548930415-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNjTFXsqCLQ3RU2oA2uHC%2Fuploads%2Fl3OHXwuk0beoD4kSCpCU%2Fimage.png?alt=media&amp;token=f582260e-1d05-4b2b-8f3e-b11aacdc18b2" alt=""><figcaption></figcaption></figure>

## How to steal Firebase authentication token <a href="#id-27c6" id="id-27c6"></a>

Now, if you’ve read the previous section carefully, you already know what we are after:

> This token is then stored on the user’s device…

I want to emphasize that storing this token is a completely appropriate solution assuming the device’s sandbox and other security measures are intact.

### Step 1: Demo app <a href="#id-8214" id="id-8214"></a>

This hello-world Flutter app is connected to Firebase Authentication. A few widgets, some copy-pasting from the Firebase tutorial, and it was up and running:

<figure><img src="https://1548930415-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNjTFXsqCLQ3RU2oA2uHC%2Fuploads%2FBQO66TYcoFZeQ18GtTbQ%2Fimage.png?alt=media&amp;token=511b7964-53d1-4225-94a8-19060a4ccb8d" alt=""><figcaption><p>Login screen with credentials</p></figcaption></figure>

Once I registered my new account, I checked the account exists as well in Authentication Dashboard:

<figure><img src="https://1548930415-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNjTFXsqCLQ3RU2oA2uHC%2Fuploads%2Fp9ooDPawTdfHiXYdP30D%2Fimage.png?alt=media&amp;token=173f8efb-2cc9-4902-92db-69696bd0522c" alt=""><figcaption></figcaption></figure>

Everything works well, I am signed in

<figure><img src="https://1548930415-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNjTFXsqCLQ3RU2oA2uHC%2Fuploads%2FEpuKpm24gM5oxUzcVfVQ%2Fimage.png?alt=media&amp;token=db91e61e-f5be-4968-b9bb-5c4657a121ee" alt=""><figcaption></figcaption></figure>

### Step 2: Extraction of JWT <a href="#id-8937" id="id-8937"></a>

Sorry, I won’t show you any zero-day vulnerability. I assume there is a vulnerable system at play or a malicious 3rd party library (yes, 3rd party dependencies can interfere with app’s data and [more](https://nakedsecurity.sophos.com/2017/08/15/how-shared-android-libraries-could-be-weaponized-for-data-theft/)), which will steal data **from within your application**.

<figure><img src="https://1548930415-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNjTFXsqCLQ3RU2oA2uHC%2Fuploads%2FqHUsDEvMFkDysvrjc5t7%2Fimage.png?alt=media&amp;token=94822f3c-745e-4b49-be7c-9e6d80068090" alt=""><figcaption></figcaption></figure>

Locate the app’s data directory `/shared_prefs`, which contains a file named `com.google.firebase.auth(…)` .

<figure><img src="https://1548930415-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNjTFXsqCLQ3RU2oA2uHC%2Fuploads%2FHVuIQWsp18fK8LEZKIsI%2Fimage.png?alt=media&amp;token=72dfb527-5eba-43e4-b359-7be99493981d" alt=""><figcaption></figcaption></figure>

The file has the following content:

<figure><img src="https://1548930415-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNjTFXsqCLQ3RU2oA2uHC%2Fuploads%2FkwTUuclAbXy9Da768LZr%2Fimage.png?alt=media&amp;token=4bb8abad-96eb-471a-9a07-3d681919d501" alt=""><figcaption></figcaption></figure>

I moved the content to my computer for easier manipulation. The highlighted text is Firebase Authentication JWT:

<figure><img src="https://1548930415-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNjTFXsqCLQ3RU2oA2uHC%2Fuploads%2FOvboSnS8n8E8pj8lBs3q%2Fimage.png?alt=media&amp;token=b05d6053-b331-4439-8c85-5f3fff739e6d" alt=""><figcaption></figcaption></figure>

TIP: You can use fancy JWT viewer — jwt.io:

<figure><img src="https://1548930415-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNjTFXsqCLQ3RU2oA2uHC%2Fuploads%2FTK5P0VJxnNQAPg4v6eVI%2Fimage.png?alt=media&amp;token=b4fbd1d9-9682-4da7-bbdc-534b37645a97" alt=""><figcaption></figcaption></figure>

And here is the data:

<figure><img src="https://1548930415-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNjTFXsqCLQ3RU2oA2uHC%2Fuploads%2FxwJx2GDeSxts4Dqac7tn%2Fimage.png?alt=media&amp;token=05527d95-34bb-4477-a02f-908e93f3bb7e" alt=""><figcaption></figcaption></figure>

An attacker could misuse this token for an impersonation attack. But to be able to make such an attack, it’s necessary to know the API. Let’s explore the API of the Flutter app.

## Attack the API <a href="#b6b3" id="b6b3"></a>

### How to do API attacks in a nutshell <a href="#c3cd" id="c3cd"></a>

An API attack is a type of cyberattack in which an attacker exploits vulnerabilities in an API in order to gain unauthorized access to sensitive data or systems.

Here are some examples of how a reverse engineer might carry out an API attack:

1. Sniffing: The attacker captures network traffic containing API requests and responses in order to extract sensitive information such as passwords or access tokens. This can be done using tools like Wireshark or Burp Suite.
2. Injection: The attacker modifies the API request in order to inject malicious code into the system. This can be done using techniques like SQL injection or cross-site scripting (XSS).
3. Tampering: The attacker modifies the API request in order to alter the data being transmitted, such as changing the value of a transaction or account balance.
4. Replay: The attacker captures a valid API request and then replays it multiple times in order to cause a denial of service (DoS) attack or gain unauthorized access to the system.

These are just a few examples of API attacks that a reverse engineer might carry out.

To attack the API of a mobile app, a reverse engineer would need to first identify the API and its associated vulnerabilities. This can typically be done by using a tool to intercept network traffic from the mobile app and analyzing the requests and responses to and from the API.

Once the API has been identified, the reverse engineer can attempt to exploit any vulnerabilities that are found. For example, if the API is not properly validated or sanitized, the reverse engineer could try injecting malicious code into the request in order to gain unauthorized access to the system or steal sensitive data.

Other tactics that might be used in an API attack on a mobile app include tampering with requests in order to alter data, capturing and replaying requests in order to cause a denial of service (DoS) attack, and sniffing network traffic to extract sensitive information.

### Discover API architecture from Flutter app <a href="#id-8039" id="id-8039"></a>

* **strings** — You can use `strings` command available in many Linux distros to gather string resources from any Flutter app:

`$ strings my-flutter-app.apk | grep http` to gather URLs

* **GraphQL architecture** — You can find GraphQL queries in the app’s binary

```graphql
query {
  user(id: "123") {
    name
    email
    posts {
      title
      content
    }
  }
}
```

* **REST architecture** — You can use `strings` or Burp Proxy (MITM) again. Common APIs can be found [here](https://github.com/danielmiessler/SecLists/blob/master/Discovery/Web-Content/api/api-endpoints.txt). The web service may also have it’s OpenAPI (Swagger) documentation available.

```
/api/v1/account/user/verify
/api/v1/analytics/events
/api/v1/articles.json
/api/v1/asset/asset
/api/v1/asset/assets
/api/v1/auth
```

I decided to end this article here as there is a plethora of API hacking tutorials: <https://duckduckgo.com/?q=api+hacking+burp+postman>

## Actionable steps for app owners <a href="#d704" id="d704"></a>

1. Check of your Firebase is properly configured: <https://github.com/MuhammadKhizerJaved/Insecure-Firebase-Exploit>
2. Glance over the **Resources** section of this article and check out one of the provided cheatsheets (the most relevant to your app)
3. IMPORTANT: Majority of network attacks (impersonation, malicious scripts, botnets, JWT stealing, etc.) can be defeated by Talsec’s unique technology [AppiCrypt](https://www.talsec.app/appicrypt). Check it out!

Thank you for reading the third part of this guide. I hope the information I’ve provided has been useful and informative! Thank you again for joining me on this journey.

*written by Tomáš Soukal, Security Consultant at Talsec*

***

## Resources <a href="#ba8c" id="ba8c"></a>

It would be impossible to provide tutorial for every possible mobile app + web service architecture. Hence, I compiled useful list of OWASP and other resources about web application security. Use the list below to find quickly technologies and guides related to your use-case.

### Latest ASVS <a href="#id-1e7d" id="id-1e7d"></a>

{% embed url="<https://github.com/OWASP/ASVS/releases/latest?source=post_page-----8693c2361468-------------------------------->" %}

### Pinning <a href="#id-571a" id="id-571a"></a>

{% embed url="<https://cheatsheetseries.owasp.org/cheatsheets/Pinning_Cheat_Sheet.html?source=post_page-----8693c2361468-------------------------------->" %}

### Injection <a href="#id-3db2" id="id-3db2"></a>

{% embed url="<https://cheatsheetseries.owasp.org/cheatsheets/Injection_Prevention_Cheat_Sheet.html?source=post_page-----8693c2361468-------------------------------->" %}

### Transaction authorization <a href="#ecdd" id="ecdd"></a>

{% embed url="<https://cheatsheetseries.owasp.org/cheatsheets/Transaction_Authorization_Cheat_Sheet.html?source=post_page-----8693c2361468-------------------------------->" %}

### Credential Stuffing Prevention <a href="#id-5d33" id="id-5d33"></a>

{% embed url="<https://cheatsheetseries.owasp.org/cheatsheets/Credential_Stuffing_Prevention_Cheat_Sheet.html?source=post_page-----8693c2361468-------------------------------->" %}

### REST Security Cheat Sheet

{% embed url="<https://cheatsheetseries.owasp.org/cheatsheets/REST_Security_Cheat_Sheet.html?source=post_page-----8693c2361468--------------------------------#access-control>" %}

### Authentication Cheat Sheet

{% embed url="<https://cheatsheetseries.owasp.org/cheatsheets/Authentication_Cheat_Sheet.html?source=post_page-----8693c2361468-------------------------------->" %}

### Forgot Password Cheat Sheet

{% embed url="<https://cheatsheetseries.owasp.org/cheatsheets/Forgot_Password_Cheat_Sheet.html?source=post_page-----8693c2361468-------------------------------->" %}

### Session Management Cheat Sheet

{% embed url="<https://cheatsheetseries.owasp.org/cheatsheets/Session_Management_Cheat_Sheet.html?source=post_page-----8693c2361468-------------------------------->" %}

### Weak Token

{% embed url="<https://cheatsheetseries.owasp.org/cheatsheets/JSON_Web_Token_for_Java_Cheat_Sheet.html?source=post_page-----8693c2361468-------------------------------->" %}

### SSRF

{% embed url="<https://cheatsheetseries.owasp.org/cheatsheets/Server_Side_Request_Forgery_Prevention_Cheat_Sheet.html?source=post_page-----8693c2361468-------------------------------->" %}

### File Upload Security <a href="#id-221a" id="id-221a"></a>

{% embed url="<https://cheatsheetseries.owasp.org/cheatsheets/File_Upload_Cheat_Sheet.html?source=post_page-----8693c2361468-------------------------------->" %}

### Input Validation Cheat Sheet <a href="#b939" id="b939"></a>

{% embed url="<https://cheatsheetseries.owasp.org/cheatsheets/Input_Validation_Cheat_Sheet.html?source=post_page-----8693c2361468-------------------------------->" %}

### **GraphQL Cheat Sheet**

{% embed url="<https://cheatsheetseries.owasp.org/cheatsheets/GraphQL_Cheat_Sheet.html?source=post_page-----8693c2361468-------------------------------->" %}

### Query Parametrization

{% embed url="<https://cheatsheetseries.owasp.org/cheatsheets/Query_Parameterization_Cheat_Sheet.html?source=post_page-----8693c2361468-------------------------------->" %}

## AWS Security

{% embed url="<https://aws.amazon.com/blogs/security/defense-in-depth-open-firewalls-reverse-proxies-ssrf-vulnerabilities-ec2-instance-metadata-service/?source=post_page-----8693c2361468-------------------------------->" %}

### AWS Misconfigurations

{% embed url="<https://www.crowdstrike.com/cybersecurity-101/cloud-security/common-aws-misconfigurations/?source=post_page-----8693c2361468-------------------------------->" %}

{% embed url="<https://snyk.io/blog/top-5-aws-misconfigurations/?source=post_page-----8693c2361468-------------------------------->" %}

Firebase security checklist

{% embed url="<https://firebase.google.com/support/guides/security-checklist>" %}

Misconfiguration of Firebase — world readable <https://xyz.firebaseio.com/.json>

Exploit tool for this vulnerability:

{% embed url="<https://github.com/MuhammadKhizerJaved/Insecure-Firebase-Exploit?source=post_page-----8693c2361468-------------------------------->" %}

*A simple Python Exploit to Write Data to Insecure/vulnerable firebase databases! Commonly found inside Mobile Apps. If the owner of the app have set the security rules as true for both “read” & “write” an attacker can probably dump database and write his own data to firebase db.*\
[<br>](https://www.crowdstrike.com/cybersecurity-101/cloud-security/common-aws-misconfigurations/?source=post_page-----8693c2361468--------------------------------)[<br>](https://aws.amazon.com/blogs/security/defense-in-depth-open-firewalls-reverse-proxies-ssrf-vulnerabilities-ec2-instance-metadata-service/?source=post_page-----8693c2361468--------------------------------)Control Access with Custom Claims and Security Rules | Firebase Authentication

{% embed url="<https://firebase.google.com/docs/auth/admin/custom-claims?source=post_page-----8693c2361468-------------------------------->" %}

Verify ID Tokens | Firebase Authentication

{% embed url="<https://firebase.google.com/docs/auth/admin/verify-id-tokens?source=post_page-----8693c2361468-------------------------------->" %}


---

# 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, and the optional `goal` query parameter:

```
GET https://docs.talsec.app/appsec-articles/articles/how-to-hack-and-protect-flutter-apps-steal-firebase-auth-token-and-attack-the-api.-pt.-3-3.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
