HIGH dictionary attackfiberhmac signatures

Dictionary Attack in Fiber with Hmac Signatures

Dictionary Attack in Fiber with Hmac Signatures — how this specific combination creates or exposes the vulnerability

A dictionary attack against an API using Hmac Signatures in the Fiber (Go) ecosystem typically targets a design where the server-side uses a static or low-entropy shared secret to sign requests, and the client includes the signature in an HTTP header. If the endpoint does not enforce per-request randomness or replay protection, an attacker can iteratively submit requests with slightly altered parameters (e.g., incremental IDs or usernames) and observe whether the server rejects the signature due to invalid MAC or accepts it when it should not. This behavior can reveal whether a given input is valid without requiring authentication, effectively allowing the attacker to enumerate valid users, resources, or API keys through timing or error-side-channel differences.

Because Hmac Signatures rely on a shared secret and a deterministic signing process (e.g., signing a canonical string that includes method, path, timestamp, and body), a weak secret or predictable data in the signed payload can make brute-force or dictionary attempts feasible. For example, if the signed payload includes a numeric user_id and the server computes HmacSha256(key, "GET:/users/123") without a nonce, an attacker can generate candidate signatures locally and send them to the endpoint. If the server responds differently (e.g., 200 vs 403) based on signature validity, the attacker learns information about the server’s internal handling without needing a valid account.

In a real-world scenario, the vulnerability is not in Hmac Signatures themselves but in how they are applied. Missing protections such as per-request nonces, timestamp windows, and constant-time comparison expose the signing flow to offline guessing. An attacker can collect multiple request/response pairs and attempt to guess the shared secret or valid identifiers. Since the scan category BOLA/IDOR and Authentication are tested in parallel by middleBrick, findings may highlight weak signature construction that permits dictionary-like enumeration patterns. This is especially relevant when endpoints accept client-supplied identifiers and compute Hmac on predictable inputs, effectively turning authentication into a brute-force surface.

Hmac Signatures-Specific Remediation in Fiber — concrete code fixes

To mitigate dictionary attack risks when using Hmac Signatures in Fiber, ensure each signed payload includes a nonce and a short validity window, and enforce constant-time comparison. Below are concrete, working examples in Go using the Fiber framework and the standard library crypto/hmac.

Example 1: Signed request with nonce and timestamp

// server side verification
package main

import (
    "crypto/hmac"
    "crypto/sha256"
    "encoding/hex"
    "net/http"
    "strconv"
    "strings"
    "time"

    "github.com/gofiber/fiber/v2"
)

const (
    sharedSecret = "super-secret-key-32-bytes-long-123456789012"
    timeWindow   = 30 // seconds
)

func verifyHmac(c *fiber.Ctx) error {
    receivedMAC := c.Get("X-Signature")
    timestampStr := c.Get("X-Timestamp")
    nonce := c.Get("X-Nonce")
    method := c.Method()
    path := c.Path()
    body := c.Body()

    ts, err := strconv.ParseInt(timestampStr, 10, 64)
    if err != nil || time.Now().Unix()-ts > timeWindow || time.Now().Unix()-ts < -timeWindow {
        return c.Status(http.StatusForbidden).SendString("invalid timestamp")
    }

    // canonical string; include nonce to prevent replay
    canonical := strings.Join([]string{method, path, string(body), timestampStr, nonce}, "|")
    mac := hmac.New(sha256.New, []byte(sharedSecret))
    mac.Write([]byte(canonical))
    expected := hex.EncodeToString(mac.Sum(nil))

    if !hmac.Equal([]byte(expected), []byte(receivedMAC)) {
        return c.Status(http.StatusForbidden).SendString("invalid signature")
    }

    // proceed with request handling
    return c.SendString("ok")
}

func main() {
    app := fiber.New()
    app.Post("/resource", verifyHmac, func(c *fiber.Ctx) error {
        return c.SendString("data")
    })
    app.Listen(":3000")
}

Example 2: Client generating the Hmac signature

// client side signing
package main

import (
    "crypto/hmac"
    "crypto/sha256"
    "encoding/hex"
    "fmt"
    "net/http"
    "net/url"
    "strconv"
    "strings"
    "time"
)

func signRequest(secret, method, path, body, nonce string) string {
    ts := strconv.FormatInt(time.Now().Unix(), 10)
    canonical := strings.Join([]string{method, path, body, ts, nonce}, "|")
    mac := hmac.New(sha256.New, []byte(secret))
    mac.Write([]byte(canonical))
    return hex.EncodeToString(mac.Sum(nil))
}

func main() {
    secret := "super-secret-key-32-bytes-long-123456789012"
    method := "POST"
    path := "/resource"
    body := `{"username":"test"}`
    nonce := "unique-per-request-abc123"

    sig := signRequest(secret, method, path, body, nonce)
    ts := strconv.FormatInt(time.Now().Unix(), 10)

    req, _ := http.NewRequest(method, "http://localhost:3000"+path, strings.NewReader(body))
    req.Header.Set("X-Signature", sig)
    req.Header.Set("X-Timestamp", ts)
    req.Header.Set("X-Nonce", nonce)

    client := &http.Client{}
    resp, _ := client.Do(req)
    defer resp.Body.Close()
    fmt.Println(resp.StatusCode)
}

These examples ensure that each request is unique, preventing offline dictionary attempts. The server should also enforce rate limiting at the network or application layer to reduce brute-force opportunities. middleBrick can detect missing nonces or timestamp misuse in the scan category BOLA/IDOR and Authentication checks, providing prioritized findings with remediation guidance.

Frequently Asked Questions

Why does including a nonce in Hmac signed requests help prevent dictionary attacks?
A nonce ensures each request is unique, so a captured signature cannot be replayed. For dictionary attacks, this prevents attackers from reusing or offline-guessing valid signatures because every request requires a distinct nonce and the server should reject reused nonces within the timestamp window.
How does middleBrick detect weak Hmac usage that could allow enumeration?
middleBrick runs parallel checks in the Authentication and BOLA/IDOR categories, testing endpoints with varied inputs and inspecting response differences. If signatures do not include nonces or if timing/error patterns reveal validity checks, the scan surfaces findings with severity and remediation guidance.