jsluice

repository·main·Indexed 23 days ago

https://github.com/bishopfox/jsluice

A command-line tool for static analysis of JavaScript files to extract high-value information such as URLs, paths, secrets, and syntax trees. It features five modes: 'urls' for extracting endpoints, 'secrets' for finding API keys and sensitive data (with built-in support for AWS, GCP, GitHub, and Firebase), 'tree' for printing syntax trees, 'query' for running Tree-sitter queries, and 'format' for beautifying source code. It supports local files, remote HTTP/HTTPS files, and stdin, outputting results in JSONL format.

Tokens
8.9K
Snippets
19
Records
55
Agent score
84%

What's inside jsluice

  1. Overview of jsluice commands

    main

    The jsluice repository provides two primary command-line tools:

    1. jsluice: The main command-line tool for the jsluice package.
    2. jsurls-sinks: A development tool designed to identify locations within JavaScript source code that potentially contain URLs or file paths.
  2. Use jsurls-sinks to find URL usage patterns in JavaScript

    main
    The jsurls-sinks tool is a development utility designed to identify locations within JavaScript files where URLs are being utilized. This is primarily used to discover new patterns or code structures that could be integrated into the main jsurls matcher set for improved URL extraction.
  3. How UserPattern matching logic works

    main

    A UserPattern determines its matching strategy based on the fields it populates:

    1. Object Matching: If the Object field is populated, it uses objectMatcher(). This requires all patterns defined in the Object slice to match within a single JSON object.
    2. Pair Matching: If Key is provided (but Object is empty), it uses pairMatcher(). This matches against key/value pairs within an object and includes the parent object as Context in the resulting Secret.
    3. String Matching: If only Value is provided (or neither Key nor Object), it uses stringMatcher(). This matches against raw string literals. If the string is part of a pair, the parent object is included as Context.
  4. Extract raw and decoded strings from a Node

    main

    When working with JavaScript string nodes, use these methods to handle quotes and escapes:

    • RawString() string: Returns the JavaScript representation with surrounding quotes removed, but escape sequences are left undecoded (e.g., "\n" remains "\n").
    • DecodedString() string: Returns a fully decoded version of the string (e.g., "\n" becomes an actual newline character).
    • IsStringy() bool: Returns true if the node is a string type or an expression that starts with a string delimiter (`
  5. jsluice CLI Modes

    main

    The jsluice tool operates in one of five distinct modes:

    • urls: Extracts URLs and paths from assignments, function calls (like fetch, window.open, $.ajax), XMLHttpRequest usage, and string literals.
    • secrets: Finds API keys, passwords, and other sensitive data (includes built-in extractors for AWS, GCP, GitHub, and Firebase).
    • tree: Prints a textual representation of the JavaScript syntax tree.
    • query: Runs Tree-sitter queries against the JavaScript files.
    • format: Formats (beautifies) JavaScript source code using jsbeautifier-go.
  6. Extract secrets with 'secrets' mode

    main

    Use the secrets mode to find sensitive data like API keys. It includes built-in support for AWS, GCP, GitHub, and Firebase.

    Custom Secret Matchers: You can provide a JSON file containing custom patterns using the -p/--patterns flag. Each pattern object in the JSON array can include:

    • name: The name used in the output.
    • severity: One of info, low, medium, or high.
    • value: A Go-syntax regular expression to match against string values.
    • key: A Go-syntax regular expression to match against key names.
    • object: An array of patterns to match against keys and values within an entire object.

    Example Pattern File (patterns.json):

    [
      {
        "name": "base64",
        "value": "(eyJ|YTo|Tzo|PD[89]|rO0)[%a-zA-Z0-9+/]+={0,2}",
        "severity": "low"
      }
    ]

    Example Usage:

    jsluice secrets -p patterns.json simple-b64.js | jq
  7. Extract URLs with 'urls' mode

    main

    Use the urls mode to find URLs and paths. jsluice analyzes how values are used (e.g., in fetch calls) rather than just looking at string patterns. By default, complex expressions are replaced with EXPR, but you can customize this with the -P/--placeholder flag.

    Key Options for urls mode:

    • -I, --ignore-strings: Ignore matches that are just simple string literals.
    • -S, --include-source: Include the original source code snippet in the output via a source field.
    • -R, --resolve-paths <url>: Resolve relative paths using the provided base URL.

    Example:

    jsluice urls location.js -I -R https://example.com/~tom/photos/2003/ | jq
  8. Run Tree-sitter queries with 'query' mode

    main

    The query mode allows you to run Tree-sitter queries against JavaScript files. You must specify a query using the -q/--query flag and identify the capture using the @ syntax.

    Key Options for query mode:

    • -q, --query <query>: The Tree-sitter query string (e.g., '(string) @str').
    • -r, --raw-output: If set, jsluice will not attempt to JSON-encode the query output (useful for getting raw strings).

    Example:

    jsluice query -q '(string) @str' config.js
  9. Use the jsluice CLI

    main

    The jsluice command-line tool extracts URLs, paths, secrets, and other interesting bits from JavaScript files using static analysis. It supports local files, remote files via HTTP/HTTPS, and files provided via stdin.

    Basic Syntax:

    jsluice <mode> [options] [file...]

    Input via stdin:

    find . -name '*.js' | jsluice <mode> [options]

    Output Format: Output is provided in JSONL format. It is recommended to pipe the output to jq for human-readable formatting and filtering.