Library:Protocol Buffers/Embedded descriptors
How to find and decode the FileDescriptorProto that protoc embeds in generated C++, Python and Java code, how the DescriptorTable that points to it is laid out, and the field numbers and enums needed to turn it back into a .proto file.
Recovery recipe
- Locate candidates: search the file for the 6 bytes
.protopreceded by a length byte and a leading0A; or find the stringgoogle/protobuf/descriptor.proto. Every descriptor starts0A <len> <path>.proto(field 1, name), usually followed by12 <len> <package>(field 2). - The blob is a plain protobuf message of type
google.protobuf.FileDescriptorProto; its end is theDescriptorTable.sizefield when you can find the table (below), otherwise parse until a tag is invalid. - Decode with any protobuf library:
FileDescriptorProto.FromString(blob)in Python (pip install protobuf), then print messages, fields, enums and services. Pass several files toFileDescriptorSetand render with protoc plugins or a small script. Dependencies (field 3) name the imported files, which are separate blobs in the same binary. - In a C++ binary, cross-reference from
descriptor_table_<file>to learn the dependent files and message count.
DescriptorTable (C++ runtime, struct google::protobuf::internal::DescriptorTable)
One instance per .proto file, in .data/.rdata with relocations. In protobuf 7.38-dev (protoc 38) the layout, in order (pointer-sized members are 4 or 8 bytes): bool is_initialized; bool is_eager; int size; const char* descriptor; const char* filename; absl::once_flag* once; const DescriptorTable* const* deps; int num_deps; int num_messages; const MigrationSchema* schemas; const MessageGlobalsBase* const* message_globals; const uint32_t* offsets; const EnumDescriptor** file_level_enum_descriptors; const ServiceDescriptor** file_level_service_descriptors; The layout has changed between releases (older versions had default_instances and a register_package-like member), so treat it as version-specific; size followed by descriptor and filename pointers is the stable pattern to search for: a 4-byte size, then a pointer to bytes starting 0x0A, then a pointer to the NUL-terminated .proto path.
FileDescriptorProto field numbers
FileDescriptorProto: name=1, package=2, dependency=3 (repeated string), message_type=4, enum_type=5, service=6, extension=7, options=8, source_code_info=9, public_dependency=10, weak_dependency=11, syntax=12 (string "proto3"; absent for proto2), edition=14, option_dependency=15. DescriptorProto: name=1, field=2, nested_type=3, enum_type=4, extension_range=5, extension=6, options=7, oneof_decl=8, reserved_range=9, reserved_name=10, visibility=11. Nested ExtensionRange: start=1, end=2 (end exclusive), options=3. FieldDescriptorProto: name=1, extendee=2, number=3, label=4, type=5, type_name=6, default_value=7, options=8, oneof_index=9, json_name=10, proto3_optional=17. EnumDescriptorProto: name=1, value=2, options=3. EnumValueDescriptorProto: name=1, number=2, options=3. ServiceDescriptorProto: name=1, method=2, options=3. MethodDescriptorProto: name=1, input_type=2, output_type=3, options=4 (client_streaming=5 and server_streaming=6 are the bool flags).
FieldDescriptorProto.Type and Label
Type: DOUBLE=1, FLOAT=2, INT64=3, UINT64=4, INT32=5, FIXED64=6, FIXED32=7, BOOL=8, STRING=9, GROUP=10, MESSAGE=11, BYTES=12, UINT32=13, ENUM=14, SFIXED32=15, SFIXED64=16, SINT32=17, SINT64=18. Label: OPTIONAL=1, REQUIRED=2, REPEATED=3. type_name is fully qualified with a leading dot (.pkg.Msg). A map<K,V> field is a repeated MESSAGE field whose nested type is named <Field>Entry (key = field 1, value = field 2) and whose MessageOptions has map_entry (field 7, bool) set to true.
Python and Java
- Python
*_pb2.py: the same bytes as an escaped literal inAddSerializedFile(b'...'); extract by importing the module in a sandbox or by parsing the literal; do not import untrusted modules on your normal machine. - Java: the descriptor is split into
descriptorDatastring literal chunks (Latin-1 characters carrying the bytes) in the<File>OuterClass/<File>Protoclass constant pool.
Evidence & references
- Field numbers and enum values:
src/google/protobuf/descriptor.proto(commit 522b8d41d5d5cdc7ef581c913277aa309f9b7c6a) lines 85-130 (FileDescriptorProto), 132-166 (DescriptorProto), 231-330 (FieldDescriptorProto), 341-410 (Enum, Service, Method including client_streaming=5 and server_streaming=6), 648 (map_entry = 7). - DescriptorTable struct:
src/google/protobuf/generated_message_reflection.hlines 283-299.