|
| 1 | +// Package grpc helps exploit modules talk to plaintext (h2c) or TLS |
| 2 | +// gRPC services. Two layers: |
| 3 | +// |
| 4 | +// - Invoke is the fast path for unary RPCs: one request, one response, |
| 5 | +// no grpc-go imports on the caller side. |
| 6 | +// |
| 7 | +// body := grpc.EncodeBytesField(1, payload) |
| 8 | +// out, ok := grpc.Invoke(host, port, "/svc/Method", body, 5, conf.SSL) |
| 9 | +// |
| 10 | +// - Dial returns a *grpc.ClientConn pre-configured with the raw codec |
| 11 | +// and transport. Use it when Invoke is not enough: server- / |
| 12 | +// client- / bidi-streaming RPCs, gRPC reflection, or reusing one |
| 13 | +// connection across many calls. The caller drives the standard |
| 14 | +// grpc-go ClientStream API and is responsible for closing the conn. |
| 15 | +// |
| 16 | +// EncodeBytesField / EncodeStringField wrap a value as a proto3 |
| 17 | +// length-delimited field so callers can build proto messages by hand on |
| 18 | +// the simple cases. For complex schemas, callers bring their own |
| 19 | +// protoc-generated marshallers and pass the resulting bytes through. |
| 20 | +package grpc |
| 21 | + |
| 22 | +import ( |
| 23 | + "context" |
| 24 | + "crypto/tls" |
| 25 | + "errors" |
| 26 | + "fmt" |
| 27 | + "net" |
| 28 | + "strconv" |
| 29 | + "time" |
| 30 | + |
| 31 | + "github.com/vulncheck-oss/go-exploit/output" |
| 32 | + "google.golang.org/grpc" |
| 33 | + "google.golang.org/grpc/credentials" |
| 34 | + "google.golang.org/grpc/credentials/insecure" |
| 35 | + "google.golang.org/grpc/encoding" |
| 36 | +) |
| 37 | + |
| 38 | +const rawCodecName = "vulncheck-raw-bytes" |
| 39 | + |
| 40 | +// rawCodec passes pre-encoded protobuf bytes through grpc-go untouched |
| 41 | +// so callers can hand-roll proto3 wire format without protoc. |
| 42 | +type rawCodec struct{} |
| 43 | + |
| 44 | +var errCodecType = errors.New("grpc rawCodec: unexpected value type") |
| 45 | + |
| 46 | +func (rawCodec) Marshal(v any) ([]byte, error) { |
| 47 | + b, ok := v.([]byte) |
| 48 | + if !ok { |
| 49 | + return nil, fmt.Errorf("%w: %T (want []byte)", errCodecType, v) |
| 50 | + } |
| 51 | + |
| 52 | + return b, nil |
| 53 | +} |
| 54 | + |
| 55 | +func (rawCodec) Unmarshal(data []byte, v any) error { |
| 56 | + out, ok := v.(*[]byte) |
| 57 | + if !ok { |
| 58 | + return fmt.Errorf("%w: %T (want *[]byte)", errCodecType, v) |
| 59 | + } |
| 60 | + *out = data |
| 61 | + |
| 62 | + return nil |
| 63 | +} |
| 64 | + |
| 65 | +func (rawCodec) Name() string { return rawCodecName } |
| 66 | + |
| 67 | +// grpc-go's codec registry is process-wide, so registration must be |
| 68 | +// package-level. |
| 69 | +// |
| 70 | +//nolint:gochecknoinits |
| 71 | +func init() { |
| 72 | + encoding.RegisterCodec(rawCodec{}) |
| 73 | +} |
| 74 | + |
| 75 | +// Dial opens a gRPC client connection with the package's raw codec and |
| 76 | +// the chosen transport. ssl picks h2c (false) or TLS (true); under TLS |
| 77 | +// the client skips certificate verification (exploit targets routinely |
| 78 | +// use self-signed certs). The caller is responsible for Close. Returns |
| 79 | +// ok=false on any error from grpc.NewClient; the underlying error is |
| 80 | +// logged at framework-debug level. |
| 81 | +func Dial(host string, port int, ssl bool) (*grpc.ClientConn, bool) { |
| 82 | + addr := net.JoinHostPort(host, strconv.Itoa(port)) |
| 83 | + conn, err := grpc.NewClient(addr, |
| 84 | + grpc.WithTransportCredentials(transportCreds(ssl)), |
| 85 | + grpc.WithDefaultCallOptions(grpc.ForceCodec(rawCodec{})), |
| 86 | + ) |
| 87 | + if err != nil { |
| 88 | + output.PrintfFrameworkDebug("grpc.NewClient(%s): %s", addr, err) |
| 89 | + |
| 90 | + return nil, false |
| 91 | + } |
| 92 | + |
| 93 | + return conn, true |
| 94 | +} |
| 95 | + |
| 96 | +// Invoke runs a unary RPC with timeoutSec as the dial-and-invoke |
| 97 | +// budget. Returns the response body or ok=false on any failure; the |
| 98 | +// underlying error is logged at framework-debug level with the method, |
| 99 | +// host and port so failed RPCs are attributable in a multi-target run. |
| 100 | +func Invoke(host string, port int, method string, in []byte, timeoutSec int, ssl bool) ([]byte, bool) { |
| 101 | + conn, ok := Dial(host, port, ssl) |
| 102 | + if !ok { |
| 103 | + return nil, false |
| 104 | + } |
| 105 | + defer conn.Close() |
| 106 | + |
| 107 | + ctx, cancel := context.WithTimeout(context.Background(), |
| 108 | + time.Duration(timeoutSec)*time.Second) |
| 109 | + defer cancel() |
| 110 | + |
| 111 | + var out []byte |
| 112 | + if err := conn.Invoke(ctx, method, in, &out); err != nil { |
| 113 | + output.PrintfFrameworkDebug("grpc Invoke %s on %s:%d: %s", method, host, port, err) |
| 114 | + |
| 115 | + return nil, false |
| 116 | + } |
| 117 | + |
| 118 | + return out, true |
| 119 | +} |
| 120 | + |
| 121 | +// transportCreds picks the credential option for the requested |
| 122 | +// transport. The TLS config mirrors protocol/httphelper.go: certificate |
| 123 | +// verification is intentionally disabled and the minimum version is |
| 124 | +// permissive. We have no control over the SSL versions supported on the |
| 125 | +// remote target. Be permissive for more targets. |
| 126 | +func transportCreds(ssl bool) credentials.TransportCredentials { |
| 127 | + if ssl { |
| 128 | + return credentials.NewTLS(&tls.Config{ |
| 129 | + InsecureSkipVerify: true, //nolint:gosec |
| 130 | + MinVersion: tls.VersionSSL30, //nolint:staticcheck |
| 131 | + }) |
| 132 | + } |
| 133 | + |
| 134 | + return insecure.NewCredentials() |
| 135 | +} |
| 136 | + |
| 137 | +// EncodeBytesField wraps value as a proto3 length-delimited field |
| 138 | +// (wire type 2). Lets modules skip protoc on messages with one or two |
| 139 | +// fields. |
| 140 | +func EncodeBytesField(fieldNumber uint32, value []byte) []byte { |
| 141 | + out := appendVarint(nil, uint64(fieldNumber<<3|2)) |
| 142 | + out = appendVarint(out, uint64(len(value))) |
| 143 | + |
| 144 | + return append(out, value...) |
| 145 | +} |
| 146 | + |
| 147 | +// EncodeStringField wraps value as a proto3 string field (same wire |
| 148 | +// shape as bytes). |
| 149 | +func EncodeStringField(fieldNumber uint32, value string) []byte { |
| 150 | + return EncodeBytesField(fieldNumber, []byte(value)) |
| 151 | +} |
| 152 | + |
| 153 | +func appendVarint(b []byte, v uint64) []byte { |
| 154 | + for v >= 0x80 { |
| 155 | + b = append(b, byte(v|0x80)) |
| 156 | + v >>= 7 |
| 157 | + } |
| 158 | + |
| 159 | + return append(b, byte(v)) |
| 160 | +} |
0 commit comments