How a-d-team/grafanaexp works
Project Type and Purpose
• This project is a command-line exploitation tool implemented in Go.
• Its purpose is to auto-detect the Grafana vulnerability, retrieve plugins, secret keys, and decrypt the server-side database file to extract data_source information.
• There are two main commands: “exp” (to probe and exploit the vulnerability) and “decode” (to locally decrypt a downloaded database file).
File Structure
• Top-Level Files & Directories:
– “cmd” folder: Contains the main entry point (grafanaExp.go) that initializes CLI commands.
– “internal” folder: Houses modules for handling CLI flags, vulnerability exploitation logic (exp.go), constructing HTTP requests (req.go), and managing global variables (vars.go).
– “pkg/http” folder: Contains a comprehensive collection of HTTP-related tools and implementations. There are subdirectories for CGI, FCGI, cookiejar, HTTP trace, reverse proxy functions, and other HTTP utilities. These form the core of how the tool communicates with remote Grafana instances.
– Build script (build.sh) and dependency files (go.mod, go.sum) indicate that it’s built as a stand-alone Go binary with external dependency management.
Main Components
a. CLI Entry Layer:
• Component: Command-line interface (CLI)
• File: cmd/grafanaExp.go
• Responsibility: Parse command line arguments (exploit modes “exp” and “decode”) and initiate the corresponding workflow.
b. Internal Logic Layer:
• Components:
– Flag Parsing Module (internal/cliFlag.go)
– Exploitation Logic Module (internal/exp.go) – handles vulnerability detection, secret key retrieval, and output preparation.
– Request Builder Module (internal/req.go) and Variables Module (internal/vars.go) – these manage the construction and consistency of HTTP requests/data.
• Responsibility: Process input options, perform vulnerability checks, manage plugin detection, key extraction, and decrypt DB files when needed.
c. HTTP Communication and Utility Layer:
• Component: HTTP Package (pkg/http)
• Responsibilities:
– Managing HTTP communication with target servers.
– Handling specialized HTTP variants (CGI, FCGI, and file transports).
– Implementing client-server interactions along with testing and utility functions (found throughout pkg/http subdirectories such as cookiejar, httputil, and httptrace).
• Multiple sub-packages in pkg/http show that the design leverages the versatility of Go’s net/http functionalities and custom modifications (e.g., bypassing issues in transport implementations).
Data Flow and Interactions
• User Input →
– The user invokes the tool via the CLI with parameters (URL, plugin, DB file, key, etc.).
• CLI Manager (cmd/grafanaExp.go) →
– Routes the command to the correct internal module (either “exp” or “decode” command).
• Internal Processing (internal/) →
– Flag parsing reads and validates user input.
– The exploitation module (exp.go) orchestrates vulnerability detection, calls request-building functions (req.go), and uses global configuration values (vars.go).
• HTTP Communication (pkg/http) →
– Internal modules call upon HTTP utilities for sending crafted HTTP requests to the target Grafana server.
– Various HTTP sub-components (CGI, FCGI, etc.) interact depending on the nature of the vulnerability exploitation.
• Output Generation →
– The exploitation results (list of plugins, secret key, and database connection details) are collected and output to the console.
– In “decode” mode, similar steps are performed with local database file input and decryption logic to produce a clear output.