Skip to content

Commit 5b1c348

Browse files
authored
feat: add feature for compiling .proto files with protoc (#188)
By default, .proto files are compiled using the pure-Rust compiler implemented by the protobuf-rust, but the protoc feature allows switching to protoc, the official Protobuf compiler. This required some changes in .proto files, because protoc is stricter than the pure-Rust protobuf compiler.
1 parent a70e7d8 commit 5b1c348

File tree

13 files changed

+175
-154
lines changed

13 files changed

+175
-154
lines changed

.github/workflows/code_health.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ jobs:
1111
- uses: dtolnay/[email protected]
1212
with:
1313
components: clippy
14-
- run: cargo clippy --tests --no-deps --all-features -- --deny clippy::all
14+
- run: cargo clippy --tests --no-deps -- --deny clippy::all
1515

1616
rustfmt:
1717
name: Rustfmt

.github/workflows/tests.yaml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ jobs:
2121
- win-msvc
2222
# - win-gnu
2323
- no-default-features
24+
- protoc
2425
include:
2526
- build: msrv
2627
os: ubuntu-latest
@@ -76,6 +77,13 @@ jobs:
7677
rust_flags: "-Awarnings"
7778
experimental: false
7879

80+
- build: protoc
81+
os: ubuntu-latest
82+
rust: stable
83+
args: "--package yara-x --features=protoc,magic-module"
84+
rust_flags: "-Awarnings"
85+
experimental: false
86+
7987
steps:
8088
- name: Checkout sources
8189
uses: actions/checkout@v4
@@ -95,6 +103,12 @@ jobs:
95103
sudo apt-get update
96104
sudo apt-get install -y libmagic-dev
97105
106+
- name: Install protoc
107+
if: matrix.build == 'protoc'
108+
run: |
109+
sudo apt-get install -y protobuf-compiler
110+
cargo install protobuf-codegen
111+
98112
- name: Install Rust toolchain
99113
uses: dtolnay/rust-toolchain@master
100114
with:

lib/Cargo.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,14 @@ exact-atoms = []
4141
# the fast regexp matching mechanism for testing purposes.
4242
fast-regexp = []
4343

44+
# Whether to use protoc for parsing and compiling .proto files. By default,
45+
# .proto files are parsed and compiled by the pure-Rust compiler implemented
46+
# by the `rust-protobuf` crate. With this feature you can change this behavior
47+
# and use protoc, the official Protocol Buffer compiler. You'll need to have
48+
# protoc installed in your system, together with the protoc-gen-rust plugin.
49+
# Follow the instructions in: https://lib.rs/crates/protobuf-codegen3
50+
protoc = []
51+
4452
# Enables debug logs.
4553
logging = ["dep:log"]
4654

lib/build.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,11 +143,15 @@ fn main() {
143143
let mut proto_compiler = Codegen::new();
144144
let mut proto_parser = Parser::new();
145145

146-
proto_compiler
147-
.pure()
148-
.cargo_out_dir("protos")
149-
.include("src/modules/protos");
146+
if cfg!(feature = "protoc") {
147+
proto_compiler.protoc();
148+
proto_parser.protoc();
149+
} else {
150+
proto_compiler.pure();
151+
proto_parser.pure();
152+
}
150153

154+
proto_compiler.cargo_out_dir("protos").include("src/modules/protos");
151155
proto_parser.include("src/modules/protos");
152156

153157
// All `.proto` files in src/modules/protos must be compiled

lib/src/modules/macho/parser.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1835,7 +1835,7 @@ impl From<&MinVersion> for protos::macho::MinVersion {
18351835
let mut result = protos::macho::MinVersion::new();
18361836

18371837
result.set_device(
1838-
protobuf::EnumOrUnknown::<protos::macho::DEVICE_TYPE>::from_i32(
1838+
protobuf::EnumOrUnknown::<protos::macho::DeviceType>::from_i32(
18391839
mv.device as i32,
18401840
)
18411841
.unwrap(),

lib/src/modules/pe/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,14 @@ fn main(input: &[u8]) -> PE {
4444
#[module_export]
4545
fn is_32bit(ctx: &ScanContext) -> Option<bool> {
4646
let magic = ctx.module_output::<PE>()?.opthdr_magic?;
47-
Some(magic.value() == OptHdrMagic::IMAGE_NT_OPTIONAL_HDR32_MAGIC as i32)
47+
Some(magic.value() == OptionalMagic::IMAGE_NT_OPTIONAL_HDR32_MAGIC as i32)
4848
}
4949

5050
/// Returns true if the file is a 64-bit PE.
5151
#[module_export]
5252
fn is_64bit(ctx: &ScanContext) -> Option<bool> {
5353
let magic = ctx.module_output::<PE>()?.opthdr_magic?;
54-
Some(magic.value() == OptHdrMagic::IMAGE_NT_OPTIONAL_HDR64_MAGIC as i32)
54+
Some(magic.value() == OptionalMagic::IMAGE_NT_OPTIONAL_HDR64_MAGIC as i32)
5555
}
5656

5757
/// Returns true if the file is dynamic link library (DLL)

lib/src/modules/pe/parser.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2213,7 +2213,7 @@ impl From<PE<'_>> for protos::pe::PE {
22132213
result.set_number_of_symbols(pe.pe_hdr.number_of_symbols);
22142214
result.set_size_of_optional_header(pe.pe_hdr.size_of_optional_header.into());
22152215

2216-
result.opthdr_magic = Some(EnumOrUnknown::<protos::pe::OptHdrMagic>::from_i32(pe
2216+
result.opthdr_magic = Some(EnumOrUnknown::<protos::pe::OptionalMagic>::from_i32(pe
22172217
.optional_hdr
22182218
.magic.into()));
22192219

lib/src/modules/protos/macho.proto

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ option (yara.module_options) = {
1212
};
1313

1414
message MinVersion {
15-
optional DEVICE_TYPE device = 1;
15+
optional DeviceType device = 1;
1616
optional string version = 2;
1717
optional string sdk = 3;
1818
}
@@ -113,7 +113,7 @@ message Segment {
113113
optional uint32 maxprot = 8 [(yaml.field).fmt = "x"];
114114
optional uint32 initprot = 9 [(yaml.field).fmt = "x"];
115115
optional uint32 nsects = 10;
116-
optional uint32 flags = 11 [(yaml.field).fmt = "flags:SEGMENT_FLAG"];
116+
optional uint32 flags = 11 [(yaml.field).fmt = "flags:SegmentFlag"];
117117
repeated Section sections = 12;
118118
}
119119

@@ -133,7 +133,7 @@ message File {
133133
optional uint32 filetype = 4;
134134
optional uint32 ncmds = 5;
135135
optional uint32 sizeofcmds = 6;
136-
optional uint32 flags = 7 [(yaml.field).fmt = "flags:FILE_FLAG"];
136+
optional uint32 flags = 7 [(yaml.field).fmt = "flags:FileFlag"];
137137
optional uint32 reserved = 8;
138138
optional uint64 number_of_segments = 9;
139139
optional bytes dynamic_linker = 10;
@@ -194,29 +194,29 @@ message Macho {
194194
repeated File file = 30;
195195
}
196196

197-
enum HEADER {
197+
enum Header {
198198
option (yara.enum_options).inline = true;
199199
MH_MAGIC = 0 [(yara.enum_value).i64 = 0xfeedface];
200200
MH_CIGAM = 1 [(yara.enum_value).i64 = 0xcefaedfe];
201201
MH_MAGIC_64 = 2 [(yara.enum_value).i64 = 0xfeedfacf];
202202
MH_CIGAM_64 = 3 [(yara.enum_value).i64 = 0xcffaedfe];
203203
}
204204

205-
enum FAT_HEADER {
205+
enum FatHeader {
206206
option (yara.enum_options).inline = true;
207207
FAT_MAGIC = 0 [(yara.enum_value).i64 = 0xcafebabe];
208208
FAT_CIGAM = 1 [(yara.enum_value).i64 = 0xbebafeca];
209209
FAT_MAGIC_64 = 2 [(yara.enum_value).i64 = 0xcafebabf];
210210
FAT_CIGAM_64 = 3 [(yara.enum_value).i64 = 0xbfbafeca];
211211
}
212212

213-
enum MASK_64BIT {
213+
enum Mask64Bit {
214214
option (yara.enum_options).inline = true;
215215
CPU_ARCH_ABI64 = 0x01000000;
216216
CPU_SUBTYPE_LIB64 = 0 [(yara.enum_value).i64 = 0x80000000];
217217
}
218218

219-
enum CPU_TYPE {
219+
enum CpuType {
220220
option (yara.enum_options).inline = true;
221221
CPU_TYPE_MC680X0 = 0x00000006;
222222
CPU_TYPE_X86 = 0x00000007;
@@ -231,12 +231,12 @@ enum CPU_TYPE {
231231
CPU_TYPE_POWERPC64 = 0x01000012;
232232
}
233233

234-
enum CPU_I386_TYPE {
234+
enum CpuI386Type {
235235
option (yara.enum_options).inline = true;
236236
CPU_TYPE_I386 = 0x00000007;
237237
}
238238

239-
enum CPU_INTEL_SUBTYPE {
239+
enum CpuIntelSubType {
240240
option (yara.enum_options).inline = true;
241241
CPU_SUBTYPE_INTEL_MODEL_ALL = 0x00000000;
242242
CPU_SUBTYPE_386 = 0x00000003;
@@ -251,17 +251,17 @@ enum CPU_INTEL_SUBTYPE {
251251
CPU_SUBTYPE_XEON_MP = 0x0000001c;
252252
}
253253

254-
enum CPU_I386_SUBTYPE {
254+
enum CpuI386SubType {
255255
option (yara.enum_options).inline = true;
256256
CPU_SUBTYPE_I386_ALL = 0x00000003;
257257
}
258258

259-
enum CPU_X86_SUBTYPE {
259+
enum CpuX86SubType {
260260
option (yara.enum_options).inline = true;
261261
CPU_SUBTYPE_X86_64_ALL = 0x00000003;
262262
}
263263

264-
enum CPU_INTEL_PENTIUM_SUBTYPE {
264+
enum CpuIntelPentiumSubType {
265265
option (yara.enum_options).inline = true;
266266
CPU_SUBTYPE_PENT = 0x00000005;
267267
CPU_SUBTYPE_PENTPRO = 0x00000016;
@@ -275,7 +275,7 @@ enum CPU_INTEL_PENTIUM_SUBTYPE {
275275
CPU_SUBTYPE_PENTIUM_4_M = 0x0000001a;
276276
}
277277

278-
enum CPU_ARM_SUBTYPE {
278+
enum CpuArmSubType {
279279
option (yara.enum_options).inline = true;
280280
CPU_SUBTYPE_ARM_ALL = 0x00000000;
281281
CPU_SUBTYPE_ARM_V4T = 0x00000005;
@@ -291,18 +291,18 @@ enum CPU_ARM_SUBTYPE {
291291
CPU_SUBTYPE_ARM_V7EM = 0x00000010;
292292
}
293293

294-
enum CPU_ARM_64_SUBTYPE {
294+
enum CpuArm64SubType {
295295
option (yara.enum_options).inline = true;
296296
CPU_SUBTYPE_ARM_V5TEJ = 0x00000007;
297297
CPU_SUBTYPE_ARM64_ALL = 0x00000000;
298298
}
299299

300-
enum CPU_SPARC_SUBTYPE {
300+
enum CpuSparcSubType {
301301
option (yara.enum_options).inline = true;
302302
CPU_SUBTYPE_SPARC_ALL = 0x00000000;
303303
}
304304

305-
enum CPU_POWERPC_SUBTYPE {
305+
enum CpuPowerPCSubType {
306306
option (yara.enum_options).inline = true;
307307
CPU_SUBTYPE_POWERPC_ALL = 0x00000000;
308308
CPU_SUBTYPE_POWERPC_601 = 0x00000001;
@@ -319,13 +319,13 @@ enum CPU_POWERPC_SUBTYPE {
319319
CPU_SUBTYPE_POWERPC_970 = 0x00000064;
320320
}
321321

322-
enum CPU_MC_SUBTYPE {
322+
enum CpuMcSubType {
323323
option (yara.enum_options).inline = true;
324324
CPU_SUBTYPE_MC980000_ALL = 0x00000000;
325325
CPU_SUBTYPE_MC98601 = 0x00000001;
326326
}
327327

328-
enum FILE_TYPE {
328+
enum FileType {
329329
option (yara.enum_options).inline = true;
330330
MH_OBJECT = 0x00000001;
331331
MH_EXECUTE = 0x00000002;
@@ -340,7 +340,7 @@ enum FILE_TYPE {
340340
MH_KEXT_BUNDLE = 0x0000000b;
341341
}
342342

343-
enum FILE_FLAG {
343+
enum FileFlag {
344344
option (yara.enum_options).inline = true;
345345
MH_NOUNDEFS = 0x00000001;
346346
MH_INCRLINK = 0x00000002;
@@ -370,21 +370,21 @@ enum FILE_FLAG {
370370
MH_APP_EXTENSION_SAFE = 0x02000000;
371371
}
372372

373-
enum SEGMENT_FLAG {
373+
enum SegmentFlag {
374374
option (yara.enum_options).inline = true;
375375
SG_HIGHVM = 0x00000001;
376376
SG_FVMLIB = 0x00000002;
377377
SG_NORELOC = 0x00000004;
378378
SG_PROTECTED_VERSION_1 = 0x00000008;
379379
}
380380

381-
enum SECTION_FLAG_MASK {
381+
enum SectionFlagMask {
382382
option (yara.enum_options).inline = true;
383383
SECTION_TYPE = 0x000000ff;
384384
SECTION_ATTRIBUTES = 0 [(yara.enum_value).i64 = 0xffffff00];
385385
}
386386

387-
enum SECTION_TYPE {
387+
enum SectionType {
388388
option (yara.enum_options).inline = true;
389389
S_REGULAR = 0x00000000;
390390
S_ZEROFILL = 0x00000001;
@@ -410,7 +410,7 @@ enum SECTION_TYPE {
410410
S_THREAD_LOCAL_INIT_FUNCTION_POINTERS = 0x00000015;
411411
}
412412

413-
enum SECTION_ATTRIBUTES {
413+
enum SectionAttributes {
414414
option (yara.enum_options).inline = true;
415415
S_ATTR_PURE_INSTRUCTIONS = 0 [(yara.enum_value).i64 = 0x80000000];
416416
S_ATTR_NO_TOC = 0x40000000;
@@ -424,7 +424,7 @@ enum SECTION_ATTRIBUTES {
424424
S_ATTR_LOC_RELOC = 0x00000100;
425425
}
426426

427-
enum DEVICE_TYPE {
427+
enum DeviceType {
428428
option (yara.enum_options).inline = true;
429429
MACOSX = 0x00000024;
430430
IPHONEOS = 0x00000025;

0 commit comments

Comments
 (0)