viewstateutil

package
v0.5.7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 16, 2026 License: MIT Imports: 15 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package viewstateutil checks an ASP.NET __VIEWSTATE's MAC against known machineKey validation keys, offline. It is shared by the passive aspnet-viewstate-detect module and `vigolium kit viewstate-crack`, so the two cannot drift.

Covered: the legacy (pre-4.5) MAC, which ASP.NET computes when the application does not run the 4.5 crypto stack (no <httpRuntime targetFramework="4.5"/> and no <machineKey compatibilityMode="Framework45"/>). Per the .NET reference source (System.Web/UI/ObjectStateFormatter.cs Deserialize and GetMacKeyModifier, System.Web/Configuration/MachineKeySection.cs GetDecodedData, HashData, GetHMACSHA1Hash, HashDataUsingKeyedAlgorithm and InitValidationAndEncyptionSizes), the field is

base64( serialized || HMAC-H(validationKey, serialized || modifier) )

where H follows the configured validation: SHA1 (also the validation used when validation is AES or 3DES) is HMAC-SHA1, and HMACSHA256/384/512 are those HMACs. The modifier is the page's client-state identifier as four little-endian bytes; the page publishes it as __VIEWSTATEGENERATOR, the identifier formatted "X8". ASP.NET's HMAC pads the key to the hash block size (64 bytes for SHA-1/256, 128 for SHA-384/512), which is standard HMAC for keys no longer than the block.

Not covered, and never reported as checked:

  • the 4.5+ purpose-based MAC (AspNetCryptoServiceProvider), whose keys are derived per purpose with SP800-108 from the page's directory and type name;
  • validation="MD5", whose digest the reference source delegates to a native helper (UnsafeNativeMethods.GetSHA1Hash with a 16-byte output) that cannot be checked from the managed source;
  • a page that sets ViewStateUserKey, which appends that per-user string to the modifier;
  • an encrypted ViewState (legacy ViewStateEncryptionMode, or validation AES/3DES with encryption on): its bytes are ciphertext, not the serialized stream.

Index

Constants

This section is empty.

Variables

View Source
var LegacyAlgorithms = []Algorithm{
	{Name: "SHA1", /* contains filtered or unexported fields */},
	{Name: "HMACSHA256", /* contains filtered or unexported fields */},
	{Name: "HMACSHA384", /* contains filtered or unexported fields */},
	{Name: "HMACSHA512", /* contains filtered or unexported fields */},
}

LegacyAlgorithms are the keyed validations the legacy MAC uses, in the order they are tried. SHA1 also stands for validation="AES" and "3DES", which validate with HMAC-SHA1.

View Source
var PublishedKeys = sync.OnceValues(func() ([]MachineKey, error) {
	data, err := wordlists.WordlistsFS.ReadFile(publishedKeysList)
	if err != nil {
		return nil, fmt.Errorf("loading %s: %w", publishedKeysList, err)
	}
	keys, err := ParseMachineKeys(data)
	if err != nil {
		return nil, fmt.Errorf("%s: %w", publishedKeysList, err)
	}
	return keys, nil
})

PublishedKeys returns the embedded published-key list (aspnet.machinekeys.list), parsed once. The slice is shared: callers must not modify it, and a caller adding its own keys appends to a copy (slices.Clip).

Functions

func DecodeViewState

func DecodeViewState(value string) ([]byte, error)

DecodeViewState base64-decodes a __VIEWSTATE value as ASP.NET does (standard alphabet, padded). A URL-encoded value must be unescaped first.

func GeneratorModifier

func GeneratorModifier(generator string) ([]byte, error)

GeneratorModifier turns a __VIEWSTATEGENERATOR value (the client-state identifier as eight hex digits) into the four little-endian modifier bytes the MAC covers.

func HasSerializedHeader

func HasSerializedHeader(value string) bool

HasSerializedHeader reports whether a __VIEWSTATE value's first base64 quantum decodes to the ObjectStateFormatter header (0xFF 0x01), without decoding the whole value. It is a necessary condition for IsSerialized on DecodeViewState's result, so a false answer skips the full decode of an encrypted ViewState.

func IsSerialized

func IsSerialized(decoded []byte) bool

IsSerialized reports whether decoded starts with the ObjectStateFormatter header (0xFF, then format version 0x01): the bytes are the plain serialized stream, not ciphertext.

func LegacyMAC

func LegacyMAC(alg Algorithm, key, serialized, modifier []byte) []byte

LegacyMAC computes the legacy ViewState MAC of serialized under key.

func ParseKeyHex

func ParseKeyHex(s string) ([]byte, error)

ParseKeyHex decodes a validationKey as written in web.config. An "AutoGenerate" value is not a key.

Types

type Algorithm

type Algorithm struct {
	// Name is the machineKey validation value reported for a match.
	Name string
	// contains filtered or unexported fields
}

Algorithm is one legacy validation algorithm: the machineKey validation names that select it and its HMAC.

type MachineKey

type MachineKey struct {
	ValidationKey []byte
	Source        string
}

MachineKey is a published validation key and where it was published.

func ParseMachineKeys

func ParseMachineKeys(data []byte) ([]MachineKey, error)

ParseMachineKeys reads a key list: one key per line as hex, optionally followed by whitespace and the key's source; blank lines and lines starting with '#' are skipped. A line whose key is not hex is an error, so a bad embedded list fails its test instead of silently shrinking.

func (MachineKey) Hex

func (k MachineKey) Hex() string

Hex is the validation key as web.config writes it: uppercase hex.

type Match

type Match struct {
	Key       MachineKey
	Algorithm string
}

Match is a key that reproduces a ViewState's MAC.

func CrackLegacy

func CrackLegacy(decoded, modifier []byte, keys []MachineKey) (Match, bool)

CrackLegacy returns the first key and algorithm whose legacy MAC matches the trailing bytes of decoded. decoded must be a serialized (unencrypted) ViewState; ok is false for anything else or when no key matches. A key longer than the algorithm's block is skipped: ASP.NET's own HMAC set-up does not hash such a key the way standard HMAC does.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL