{"record":{"id":"636ce0347a9ad6ae","repo":"sqlmapproject/sqlmap","slug":"malformed-scram-server-first-message-s","errorCode":null,"errorMessage":"malformed SCRAM server-first message (%s)","messagePattern":"malformed SCRAM server-first message \\((.+?)\\)","errorType":"exception","errorClass":"OperationalError","httpStatus":null,"severity":"error","filePath":"extra/dbwire/postgres.py","lineNumber":259,"sourceCode":"        elif code == 5:  # MD5 password\n            salt = payload[4:8]\n            inner = hashlib.md5((password or \"\").encode(\"utf-8\") + (user or \"\").encode(\"utf-8\")).hexdigest()\n            token = b\"md5\" + hashlib.md5(inner.encode(\"ascii\") + salt).hexdigest().encode(\"ascii\")\n            _send(sock, b\"p\", token + b\"\\x00\")\n        elif code == 10:  # SASL (SCRAM-SHA-256)\n            if not hasattr(hashlib, \"pbkdf2_hmac\"):\n                raise NotSupportedError(\"SCRAM-SHA-256 authentication requires Python >= 2.7.8 (hashlib.pbkdf2_hmac)\")\n            client_nonce = base64.b64encode(os.urandom(18)).decode(\"ascii\")\n            cfirst_bare = \"n=,r=%s\" % client_nonce\n            client_first = \"n,,\" + cfirst_bare\n            _send(sock, b\"p\", b\"SCRAM-SHA-256\\x00\" + struct.pack(\"!I\", len(client_first)) + client_first.encode(\"ascii\"))\n        elif code == 11:  # SASLContinue (server-first)\n            try:\n                server_first = payload[4:].decode(\"ascii\")\n                attrs = dict(kv.split(\"=\", 1) for kv in server_first.split(\",\"))\n                snonce, salt, iterations = attrs[\"r\"], base64.b64decode(attrs[\"s\"]), int(attrs[\"i\"])\n            except (KeyError, ValueError, binascii.Error, UnicodeDecodeError) as ex:\n                raise OperationalError(\"malformed SCRAM server-first message (%s)\" % ex)\n            # RFC 5802 5.1: the server nonce MUST start with the client nonce and MUST add material of its\n            # own. Skipping this lets anything that can answer the TCP connection replay a recorded\n            # server-first and drive the exchange - and dbwire has no TLS layer underneath to catch it.\n            if not client_nonce or not snonce.startswith(client_nonce) or len(snonce) <= len(client_nonce):\n                raise OperationalError(\"SCRAM server nonce does not extend the client nonce (rogue server?)\")\n            if iterations < 4096:   # RFC 5802 recommends >= 4096; a tiny count cheapens an offline attack\n                raise OperationalError(\"SCRAM iteration count %d is too low\" % iterations)\n            salted = hashlib.pbkdf2_hmac(\"sha256\", (password or \"\").encode(\"utf-8\"), salt, iterations)\n            client_key = hmac.new(salted, b\"Client Key\", hashlib.sha256).digest()\n            stored_key = hashlib.sha256(client_key).digest()\n            client_final_noproof = \"c=biws,r=%s\" % snonce\n            auth_message = \"%s,%s,%s\" % (cfirst_bare, server_first, client_final_noproof)\n            client_sig = hmac.new(stored_key, auth_message.encode(\"ascii\"), hashlib.sha256).digest()\n            proof = base64.b64encode(_xor(client_key, client_sig)).decode(\"ascii\")\n            _send(sock, b\"p\", (\"%s,p=%s\" % (client_final_noproof, proof)).encode(\"ascii\"))\n        elif code == 12:  # SASLFinal (server-final): verify the server too, or the handshake is one-way\n            # Without this the client proves itself to the server and simply trusts whatever answers back.\n            # ServerSignature = HMAC(ServerKey, AuthMessage) can only be produced by a peer that holds the","sourceCodeStart":241,"sourceCodeEnd":277,"githubUrl":"https://github.com/sqlmapproject/sqlmap/blob/0a35b20e3953d341be6c7ac75ccb0b3362540c8d/extra/dbwire/postgres.py#L241-L277","documentation":"Error \"malformed SCRAM server-first message (%s)\" thrown in sqlmapproject/sqlmap.","triggerScenarios":"Thrown at extra/dbwire/postgres.py:259 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":[],"exampleFix":null,"handlingStrategy":null,"validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"0a35b20e3953d341be6c7ac75ccb0b3362540c8d","analyzedAt":"2026-08-26T23:02:52.002Z","schemaVersion":2},"datasetVersion":"2026-08-27T03:17:27.898Z"}