|
| 1 | +package com.redcell.gqlanalyzer.checks.impl |
| 2 | + |
| 3 | +import com.redcell.gqlanalyzer.checks.GraphQLCheck |
| 4 | +import com.redcell.gqlanalyzer.checks.Heuristics |
| 5 | +import com.redcell.gqlanalyzer.model.CheckContext |
| 6 | +import com.redcell.gqlanalyzer.model.Confidence |
| 7 | +import com.redcell.gqlanalyzer.model.Finding |
| 8 | +import com.redcell.gqlanalyzer.model.Severity |
| 9 | +import com.redcell.gqlanalyzer.transport.GraphQLHttp |
| 10 | +import com.redcell.gqlanalyzer.transport.QueryBuilder |
| 11 | + |
| 12 | +/** |
| 13 | + * API2:2023 — username/account enumeration. CONFIG-GATED: runs only when the operator |
| 14 | + * supplies a known-valid and known-invalid identifier (that config is the opt-in to the |
| 15 | + * two probes). Sends the same auth operation twice (dummy password → failed logins, |
| 16 | + * non-destructive) and reports a response differential that lets an attacker tell which |
| 17 | + * identifiers exist. |
| 18 | + */ |
| 19 | +class UserEnumerationCheck : GraphQLCheck { |
| 20 | + override val id = "user-enumeration" |
| 21 | + override val owaspId = "API2:2023" |
| 22 | + |
| 23 | + override fun run(ctx: CheckContext): List<Finding> { |
| 24 | + val cfg = ctx.config |
| 25 | + if (!cfg.hasUserEnumConfig) return emptyList() |
| 26 | + val schema = ctx.schema ?: return emptyList() |
| 27 | + |
| 28 | + val field = (schema.queries() + schema.mutations()).firstOrNull { f -> |
| 29 | + Heuristics.isAuthField(f.name) && f.args.any { Heuristics.isIdentifierArg(it.name) } |
| 30 | + } ?: return emptyList() |
| 31 | + val idArg = field.args.first { Heuristics.isIdentifierArg(it.name) } |
| 32 | + |
| 33 | + val validId = cfg.userEnumValid!! |
| 34 | + val invalidId = cfg.userEnumInvalid!! |
| 35 | + val rrValid = GraphQLHttp.postJson(ctx.api, ctx.request, GraphQLHttp.queryEnvelope(QueryBuilder.injectedQuery(schema, field, idArg.name, validId))) |
| 36 | + val rrInvalid = GraphQLHttp.postJson(ctx.api, ctx.request, GraphQLHttp.queryEnvelope(QueryBuilder.injectedQuery(schema, field, idArg.name, invalidId))) |
| 37 | + val bValid = rrValid.response()?.bodyToString().orEmpty() |
| 38 | + val bInvalid = rrInvalid.response()?.bodyToString().orEmpty() |
| 39 | + if (!reveals(bValid, bInvalid, validId, invalidId)) return emptyList() |
| 40 | + |
| 41 | + return listOf( |
| 42 | + Finding( |
| 43 | + name = "GraphQL user enumeration via ${field.name} (distinct responses for valid vs invalid identifier)", |
| 44 | + detail = """ |
| 45 | + The auth operation `${field.name}` responded differently for a known-valid |
| 46 | + identifier than for a known-invalid one (same dummy password), so an attacker can |
| 47 | + determine which accounts exist — enabling targeted credential stuffing, password |
| 48 | + spraying and phishing. |
| 49 | +
|
| 50 | + Probes used failed logins only; no account was accessed. |
| 51 | +
|
| 52 | + CVSS v3.1: AV:N/AC:L/PR:N/UI:N/S:U/C:L/I:N/A:N (5.3, Medium) — unauthenticated |
| 53 | + disclosure of account existence. |
| 54 | + """.trimIndent(), |
| 55 | + severity = Severity.MEDIUM, |
| 56 | + confidence = Confidence.FIRM, |
| 57 | + remediation = """ |
| 58 | + Return a single generic message for all authentication failures (never |
| 59 | + distinguish "no such user" from "wrong password"), keep response timing uniform, |
| 60 | + and apply the same rate limiting to valid and invalid identifiers. |
| 61 | + """.trimIndent(), |
| 62 | + evidence = listOf(rrValid, rrInvalid), |
| 63 | + ), |
| 64 | + ) |
| 65 | + } |
| 66 | + |
| 67 | + companion object { |
| 68 | + /** True when the two responses differ in error templates (identifiers stripped) or data presence. */ |
| 69 | + fun reveals(validBody: String, invalidBody: String, validId: String, invalidId: String): Boolean { |
| 70 | + val tValid = templates(GraphQLHttp.errorMessages(validBody), validId, invalidId) |
| 71 | + val tInvalid = templates(GraphQLHttp.errorMessages(invalidBody), validId, invalidId) |
| 72 | + if (tValid != tInvalid) return true |
| 73 | + return GraphQLHttp.hasData(validBody) != GraphQLHttp.hasData(invalidBody) |
| 74 | + } |
| 75 | + |
| 76 | + /** Normalize messages: drop the identifiers, collapse digits, lowercase — compare the templates. */ |
| 77 | + private fun templates(messages: List<String>, vararg ids: String): Set<String> = |
| 78 | + messages.map { msg -> |
| 79 | + var m = msg.lowercase() |
| 80 | + ids.forEach { m = m.replace(it.lowercase(), "") } |
| 81 | + m.replace(Regex("\\d+"), "#").replace(Regex("\\s+"), " ").trim() |
| 82 | + }.toSet() |
| 83 | + } |
| 84 | +} |
0 commit comments