Documentation
¶
Index ¶
- Variables
- func CalculateEntropy(data []byte) float64
- func DecodeMessages(data []byte) ([][]Field, error)
- func DetectMessageType(fields []Field) string
- func DetectServiceName(srcPort, dstPort int32) string
- func IsPrintable(data []byte) bool
- func IsProtobufData(data []byte) bool
- func ParseMessageTypeMappings(mappings []string)
- func PopulateFields(fields []Field, out map[string]string, order *[]string)
- func ReadVarint(buf *bytes.Reader) (uint64, error)
- func ResolveFields(fields []Field, md protoreflect.MessageDescriptor) map[string]string
- func SetSchemaRegistry(r *SchemaRegistry)
- func SetShowAlternatives(enabled bool)
- type EnumValueInfo
- type Field
- type FieldInfo
- type MessageInfo
- type SchemaRegistry
Constants ¶
This section is empty.
Variables ¶
var Decoder = &decoder.StreamDecoder{ Type: types.Type_NC_Protobuf, Name: "Protobuf", Description: "Generic Protocol Buffer wire format decoder for unknown protobuf traffic", PostInit: func(sd *decoder.StreamDecoder) error { var err error pbLog, _, err = logging.InitZapLogger( decoderconfig.Instance.Out, "protobuf", decoderconfig.Instance.Debug, ) if err != nil { return err } if decoderconfig.Instance.ProtoShowAlternatives { SetShowAlternatives(true) } if len(decoderconfig.Instance.ProtoSearchPaths) > 0 { registry, rErr := NewSchemaRegistry(decoderconfig.Instance.ProtoSearchPaths) if rErr != nil { pbLog.Warn("failed to initialize proto schema registry", zap.Error(rErr)) } else { SetSchemaRegistry(registry) pbLog.Info("proto schema registry initialized", zap.Int("files", registry.FileCount()), zap.Int("messages", registry.MessageCount()), ) } } if len(decoderconfig.Instance.ProtoMessageTypes) > 0 { ParseMessageTypeMappings(decoderconfig.Instance.ProtoMessageTypes) } return nil }, CanDecode: func(client, server []byte) bool { return IsProtobufData(client) || IsProtobufData(server) }, DeInit: func(sd *decoder.StreamDecoder) error { return pbLog.Sync() }, Factory: &protobufReader{}, Typ: core.All, }
Decoder for generic Protocol Buffer wire format detection and analysis.
Functions ¶
func CalculateEntropy ¶
CalculateEntropy computes Shannon entropy of the data in bits.
func DecodeMessages ¶
DecodeMessages attempts to decode one or more protobuf messages from raw bytes.
func DetectMessageType ¶
DetectMessageType classifies a decoded message based on field patterns.
func DetectServiceName ¶
DetectServiceName guesses the service type from port numbers.
func IsPrintable ¶
IsPrintable returns true if all bytes are printable ASCII.
func IsProtobufData ¶
IsProtobufData uses heuristics to detect if data might be protobuf encoded. Checks for valid wire type distribution, varint continuation patterns, and sufficient entropy to distinguish from text protocols.
func ParseMessageTypeMappings ¶ added in v0.9.1
func ParseMessageTypeMappings(mappings []string)
ParseMessageTypeMappings parses "port:MessageType" strings into portMessageTypes.
func PopulateFields ¶
PopulateFields converts ordered decoded fields into the audit record's Fields map (keyed as "type_fieldnum") and FieldOrder slice (preserving wire order). Alternative interpretations are NOT stored here — they go into the dedicated FieldAlternatives map in processData.
func ReadVarint ¶
ReadVarint reads a varint-encoded uint64 from the reader.
func ResolveFields ¶ added in v0.9.1
func ResolveFields(fields []Field, md protoreflect.MessageDescriptor) map[string]string
ResolveFields takes raw wire-format fields and a message descriptor, and returns a map of named fields with their values. Unknown field numbers (not in schema) are kept with their wire-format keys.
func SetSchemaRegistry ¶ added in v0.9.1
func SetSchemaRegistry(r *SchemaRegistry)
SetSchemaRegistry sets the global schema registry in a thread-safe manner.
func SetShowAlternatives ¶ added in v0.9.1
func SetShowAlternatives(enabled bool)
SetShowAlternatives enables or disables multi-interpretation mode.
Types ¶
type EnumValueInfo ¶ added in v0.9.1
EnumValueInfo describes a single enum value.
type Field ¶
type Field struct {
Number uint64 // protobuf field number
Type string // "varint", "fixed64", "string", "bytes", "nested", "fixed32", "packed_varint", "packed_fixed32", "packed_fixed64"
Value string // string representation of the value
Alternatives map[string]string // alternative interpretations keyed by type name (e.g. "sint64", "double", "bool")
}
Field represents a single decoded protobuf field, preserving wire order.
type FieldInfo ¶ added in v0.9.1
type FieldInfo struct {
Name string `json:"name"`
Number int `json:"number"`
Type string `json:"type"`
Label string `json:"label"`
TypeName string `json:"typeName,omitempty"`
EnumValues []EnumValueInfo `json:"enumValues,omitempty"`
}
FieldInfo describes a single field within a message.
type MessageInfo ¶ added in v0.9.1
type MessageInfo struct {
FullName string `json:"fullName"`
Package string `json:"package"`
Name string `json:"name"`
ProtoFile string `json:"protoFile"`
Fields []FieldInfo `json:"fields"`
}
MessageInfo describes a protobuf message type for API responses.
type SchemaRegistry ¶ added in v0.9.1
type SchemaRegistry struct {
// contains filtered or unexported fields
}
SchemaRegistry holds compiled .proto file descriptors and provides message/field lookup by fully qualified name.
func GetSchemaRegistry ¶ added in v0.9.1
func GetSchemaRegistry() *SchemaRegistry
GetSchemaRegistry returns the global schema registry in a thread-safe manner.
func NewSchemaRegistry ¶ added in v0.9.1
func NewSchemaRegistry(searchPaths []string) (*SchemaRegistry, error)
NewSchemaRegistry creates a schema registry by compiling all .proto files found in the given search paths. Import resolution follows the same semantics as protoc: each search path is a root for import resolution.
func (*SchemaRegistry) FileCount ¶ added in v0.9.1
func (r *SchemaRegistry) FileCount() int
FileCount returns the number of compiled .proto files.
func (*SchemaRegistry) ListMessages ¶ added in v0.9.1
func (r *SchemaRegistry) ListMessages() []MessageInfo
ListMessages returns information about all indexed message types.
func (*SchemaRegistry) LookupMessage ¶ added in v0.9.1
func (r *SchemaRegistry) LookupMessage(fullName string) (protoreflect.MessageDescriptor, bool)
LookupMessage returns the message descriptor for a fully qualified name.
func (*SchemaRegistry) MessageCount ¶ added in v0.9.1
func (r *SchemaRegistry) MessageCount() int
MessageCount returns the number of indexed message types.
func (*SchemaRegistry) MessageNames ¶ added in v0.9.1
func (r *SchemaRegistry) MessageNames() []string
MessageNames returns all fully qualified message names.