yara-x 1.15.0

A pure Rust implementation of YARA.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
use crate::types::{IntegerConstraint, StringConstraint, TypeValue};
use itertools::Itertools;
use std::borrow::Cow;

use serde::{Deserialize, Serialize};
use std::cmp::Ordering;
use std::hash::{Hash, Hasher};
use std::iter::Peekable;
use std::rc::Rc;
use std::str::Chars;

#[derive(Clone, Debug, Eq, PartialEq)]
/// Represents a mangled function name.
///
/// A mangled name is a function name decorated with additional information
/// about the function's arguments and return types.
///
/// Mangled names have the format:
///   
/// `[<type name>::]<func name>@<arguments>@<return type>`
///
/// The prefix `<type name>::` is optional, it is present only if the function
/// is a method of the type identified by `<type name>`. `<arguments>` is a
/// comma-separated list of `<name>:<type>` pairs, where `<name>` is the name
/// of the argument, and `<type>` is a sequence of characters that specify the
/// argument's type. Allowed type characters are:
///
/// ```text
///  i: integer
///  f: float
///  b: bool
///  s: string
///  r: regexp
/// ```
///
/// `<return type>` is a sequence of one or more of the characters above,
/// specifying the type returned by the function (except `r`, because
/// functions can't return regular expressions). For example, a function `add`
/// with two integer arguments `a` and `b` that returns another integer
/// would have the mangled name `add@a:i,b:i@i`. A function `foo` that takes no
/// arguments and returns a tuple of two integers has the mangled name `foo@@ii`.
///
/// Additionally, the return type may be followed by a `u` character if
/// the returned value may be undefined. For example, a function `foo` that
/// receives no argument and returns a string that may be undefined will have
/// a mangled name: `foo@@su`.
///
/// Both `<arguments>` and `<return type>` can be empty if the function
/// doesn't receive arguments or doesn't return a value. Let's see some
/// examples:
///
/// ```text
/// foo()                          ->  foo@@
/// foo(a: i64)                    ->  foo@a:i@
/// foo() -> i32                   ->  foo@@i
/// foo() -> Option<()>            ->  foo@@u
/// foo() -> Option<f32>           ->  foo@@fu
/// foo() -> Option<(f64,f64)>     ->  foo@@ffu
/// ```
///
/// ### Type Constraints
///
/// Types may include constraints that specify additional restrictions on
/// their values. A constraint follows the type character, separated by a
/// colon, and consists of an uppercase letter (representing the constraint
/// type) and possibly additional characters, depending on the constraint.
///
/// Examples:
///
/// ```text
/// foo() -> lowercase string           _> foo@@s:L
/// foo() -> uppercase string           _> foo@@s:U
/// foo() -> string of length 32        _> foo@@s:N32
/// foo() -> 32-byte lowercase string   -> foo@@s:N32:L
/// foo() -> 32-byte uppercase string   -> foo@@s:N32:U
/// foo() -> integer in the range 0-255 -> foo@@i:R0:255
/// ```
///
/// Multiple constraints can be chained by appending them in sequence after
/// the type character.
#[derive(Serialize, Deserialize, Hash)]
pub(crate) struct MangledFnName(String);

impl MangledFnName {
    #[inline]
    pub fn as_str(&self) -> &str {
        self.0.as_str()
    }
}

impl MangledFnName {
    /// Returns the types of arguments and return value for the function.
    pub fn unmangle(&self) -> (Vec<(&str, TypeValue)>, TypeValue) {
        let (_fn_name, arg_names_and_types, ret_type) =
            self.0.split('@').collect_tuple().unwrap_or_else(|| {
                panic!("invalid mangled name: `{}`", self.0)
            });

        let mut args = Vec::new();

        if !arg_names_and_types.is_empty() {
            for arg_str in arg_names_and_types.split(',') {
                let (arg_name, arg_type) =
                    if let Some((n, t)) = arg_str.split_once(':') {
                        (n, t)
                    } else {
                        panic!(
                            "argument name missing in mangled name: `{}`",
                            self.0
                        )
                    };

                let mut chars = arg_type.chars().peekable();
                let type_value =
                    self.next_type(&mut chars).unwrap_or_else(|| {
                        panic!(
                            "invalid argument type in mangled name: `{}`",
                            self.0
                        )
                    });
                args.push((arg_name, type_value));
            }
        }

        let mut chars = ret_type.chars().peekable();
        let ret = self.next_type(&mut chars).unwrap_or_else(|| {
            panic!("expecting return type in mangled name: `{}`", self.0)
        });

        // Return type can't be a regexp.
        assert!(!matches!(ret, TypeValue::Regexp(_)));

        (args, ret)
    }

    /// Returns true if the function's result may be undefined.
    #[inline]
    pub fn result_may_be_undef(&self) -> bool {
        self.0.ends_with('u')
    }

    /// If this function is a method of some type, returns the type name.
    pub fn method_of(&self) -> Option<&str> {
        self.0.split_once("::").map(|(type_name, _)| type_name)
    }

    fn next_type(&self, chars: &mut Peekable<Chars>) -> Option<TypeValue> {
        match chars.next() {
            Some('u') => Some(TypeValue::Unknown),
            Some('r') => Some(TypeValue::Regexp(None)),
            Some('f') => Some(TypeValue::unknown_float()),
            Some('b') => Some(TypeValue::unknown_bool()),
            Some('i') => {
                let mut constraints = Vec::new();

                while let Some(':') = chars.peek() {
                    chars.next(); // consume the colon (:)
                    match chars.next() {
                        Some('R') => {
                            let min = self.parse_i64(chars);
                            assert_eq!(chars.next(), Some(':'));
                            let max = self.parse_i64(chars);
                            constraints
                                .push(IntegerConstraint::Range(min, max));
                        }
                        None | Some(_) => {
                            panic!("invalid mangled name: `{}`", self.0)
                        }
                    }
                }

                Some(if constraints.is_empty() {
                    TypeValue::unknown_integer()
                } else {
                    TypeValue::unknown_integer_with_constraints(constraints)
                })
            }
            Some('s') => {
                let mut constraints = Vec::new();

                while let Some(':') = chars.peek() {
                    chars.next(); // consume the colon (:)
                    match chars.next() {
                        Some('L') => {
                            constraints.push(StringConstraint::Lowercase);
                        }
                        Some('U') => {
                            constraints.push(StringConstraint::Uppercase);
                        }
                        Some('N') => {
                            let n = self.parse_i64(chars);
                            constraints.push(StringConstraint::ExactLength(
                                n as usize,
                            ));
                        }
                        None | Some(_) => {
                            panic!("invalid mangled name: `{}`", self.0)
                        }
                    }
                }

                Some(if constraints.is_empty() {
                    TypeValue::unknown_string()
                } else {
                    TypeValue::unknown_string_with_constraints(constraints)
                })
            }
            Some(c) => {
                panic!("unknown type `{}` in mangled name: `{}`", c, self.0)
            }
            None => None,
        }
    }

    fn parse_i64(&self, chars: &mut Peekable<Chars>) -> i64 {
        chars
            .by_ref()
            .peeking_take_while(|&c| c.is_ascii_digit() || c == '-')
            .collect::<String>()
            .parse::<i64>()
            .unwrap_or_else(|_| panic!("invalid mangled name: `{}`", self.0))
    }
}

impl<S> From<S> for MangledFnName
where
    S: Into<String>,
{
    fn from(value: S) -> Self {
        Self(value.into())
    }
}

/// Represents a function's signature.
///
/// YARA modules allow function overloading, therefore, functions can have the
/// same name but different arguments.
#[derive(Clone, Serialize, Deserialize, Debug)]
pub(crate) struct FuncSignature {
    pub mangled_name: MangledFnName,
    pub args: Vec<(String, TypeValue)>,
    pub result: TypeValue,
    pub doc: Option<Cow<'static, str>>,
}

impl FuncSignature {
    /// Returns true if the function's result may be undefined.
    #[inline]
    pub fn result_may_be_undef(&self) -> bool {
        self.mangled_name.result_may_be_undef()
    }

    /// If this function is a method of some type, returns the type name.
    #[inline]
    pub fn method_of(&self) -> Option<&str> {
        self.mangled_name.method_of()
    }
}

impl Hash for FuncSignature {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.mangled_name.hash(state);
    }
}

impl Ord for FuncSignature {
    fn cmp(&self, other: &Self) -> Ordering {
        self.mangled_name.as_str().cmp(other.mangled_name.as_str())
    }
}

impl PartialOrd for FuncSignature {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl Eq for FuncSignature {}

impl PartialEq for FuncSignature {
    fn eq(&self, other: &Self) -> bool {
        self.mangled_name == other.mangled_name
    }
}

impl<T: Into<String>> From<T> for FuncSignature {
    /// Creates a [`FuncSignature`] from a string containing a mangled function name.
    fn from(value: T) -> Self {
        let mangled_name = MangledFnName::from(value.into());
        let (args_with_names, result) = mangled_name.unmangle();

        let mut args = Vec::with_capacity(args_with_names.len());
        for (name, ty) in args_with_names {
            args.push((name.to_string(), ty));
        }

        Self { mangled_name, args, result, doc: None }
    }
}

/// A type representing a function.
///
/// Represents both functions and methods. As in any programming language
/// methods are functions associated to a type that receive an instance
/// of that type as their first argument.
#[derive(Clone, Serialize, Deserialize, Debug, Hash, PartialEq, Eq)]
pub(crate) struct Func {
    /// The list of signatures for this function. Functions can be overloaded,
    /// so they may more than one signature.
    signatures: Vec<Rc<FuncSignature>>,
    /// If this function is a method, this field contains the name of the
    /// type. `None` indicates that this is a standard function, not a method.
    method_of: Option<String>,
}

impl<T: Into<String>> From<T> for Func {
    /// Creates a [`Func`] from a string containing a mangled function name.
    fn from(value: T) -> Self {
        let signature = FuncSignature::from(value);
        let method_of = signature.method_of().map(String::from);
        Self { signatures: vec![Rc::new(signature)], method_of }
    }
}

impl Func {
    /// Returns `true` if this function is a method.
    pub fn is_method(&self) -> bool {
        self.method_of.is_some()
    }

    /// Adds a signature to the function.
    ///
    /// If any of the added signatures is a method associated with a specific type,
    /// all other signatures must also be methods for the same type.
    ///
    /// # Panics
    ///
    /// Panics if the function already contains the given signature, or if the added
    /// signature is a method for a different type than the one used in existing
    /// method signatures.
    pub fn add_signature(&mut self, signature: FuncSignature) {
        if let Some(method_of) = &self.method_of {
            assert_eq!(signature.method_of(), Some(method_of.as_str()));
        }

        let signature = Rc::new(signature);
        // Signatures are inserted into self.signatures sorted by
        // mangled named.
        match self.signatures.binary_search(&signature) {
            Ok(_) => {
                panic!(
                    "function `{}` is implemented twice",
                    signature.mangled_name.as_str()
                )
            }
            Err(pos) => self.signatures.insert(pos, signature),
        }
    }

    /// Returns all the signatures for this function.
    #[inline]
    pub fn signatures(&self) -> &[Rc<FuncSignature>] {
        self.signatures.as_slice()
    }

    /// Returns all the signatures for this function, but mutable.
    #[inline]
    pub fn signatures_mut(&mut self) -> &mut [Rc<FuncSignature>] {
        self.signatures.as_mut_slice()
    }
}

#[cfg(test)]
mod test {
    use crate::types::{
        IntegerConstraint, MangledFnName, StringConstraint, TypeValue,
    };
    use pretty_assertions::assert_eq;

    #[test]
    fn mangled_name() {
        assert_eq!(
            MangledFnName::from("foo@@i").unmangle(),
            (vec![], TypeValue::unknown_integer())
        );

        assert_eq!(
            MangledFnName::from("foo@a:i,b:i@i").unmangle(),
            (
                vec![
                    ("a", TypeValue::unknown_integer()),
                    ("b", TypeValue::unknown_integer())
                ],
                TypeValue::unknown_integer()
            )
        );

        assert_eq!(
            MangledFnName::from("foo@a:f,b:f@f").unmangle(),
            (
                vec![
                    ("a", TypeValue::unknown_float()),
                    ("b", TypeValue::unknown_float())
                ],
                TypeValue::unknown_float()
            )
        );

        assert_eq!(
            MangledFnName::from("foo@a:b,b:b@b").unmangle(),
            (
                vec![
                    ("a", TypeValue::unknown_bool()),
                    ("b", TypeValue::unknown_bool())
                ],
                TypeValue::unknown_bool()
            )
        );

        assert_eq!(
            MangledFnName::from("foo@a:s,b:s@s").unmangle(),
            (
                vec![
                    ("a", TypeValue::unknown_string()),
                    ("b", TypeValue::unknown_string())
                ],
                TypeValue::unknown_string()
            )
        );

        assert_eq!(
            MangledFnName::from("foo@a:s,b:s:L@s:L").unmangle(),
            (
                vec![
                    ("a", TypeValue::unknown_string()),
                    (
                        "b",
                        TypeValue::unknown_string_with_constraints(vec![
                            StringConstraint::Lowercase
                        ])
                    )
                ],
                TypeValue::unknown_string_with_constraints(vec![
                    StringConstraint::Lowercase
                ])
            )
        );

        assert_eq!(
            MangledFnName::from("foo@a:s,b:s:U@s:U").unmangle(),
            (
                vec![
                    ("a", TypeValue::unknown_string()),
                    (
                        "b",
                        TypeValue::unknown_string_with_constraints(vec![
                            StringConstraint::Uppercase
                        ])
                    )
                ],
                TypeValue::unknown_string_with_constraints(vec![
                    StringConstraint::Uppercase
                ])
            )
        );

        assert_eq!(
            MangledFnName::from("foo@a:s,b:s:N16@s:N16").unmangle(),
            (
                vec![
                    ("a", TypeValue::unknown_string()),
                    (
                        "b",
                        TypeValue::unknown_string_with_constraints(vec![
                            StringConstraint::ExactLength(16),
                        ])
                    )
                ],
                TypeValue::unknown_string_with_constraints(vec![
                    StringConstraint::ExactLength(16),
                ])
            )
        );

        assert_eq!(
            MangledFnName::from("foo@a:s,b:s:N16:L@s:N16:L").unmangle(),
            (
                vec![
                    ("a", TypeValue::unknown_string()),
                    (
                        "b",
                        TypeValue::unknown_string_with_constraints(vec![
                            StringConstraint::ExactLength(16),
                            StringConstraint::Lowercase
                        ])
                    )
                ],
                TypeValue::unknown_string_with_constraints(vec![
                    StringConstraint::ExactLength(16),
                    StringConstraint::Lowercase
                ])
            )
        );

        assert_eq!(
            MangledFnName::from("foo@@i:R0:10").unmangle(),
            (
                vec![],
                TypeValue::unknown_integer_with_constraints(vec![
                    IntegerConstraint::Range(0, 10),
                ])
            )
        );

        assert_eq!(
            MangledFnName::from("foo@@i:R-100:1000").unmangle(),
            (
                vec![],
                TypeValue::unknown_integer_with_constraints(vec![
                    IntegerConstraint::Range(-100, 1000),
                ])
            )
        );

        assert_eq!(
            MangledFnName::from("Bar::foo@a:i,b:i@iu").method_of(),
            Some("Bar")
        );

        assert_eq!(
            MangledFnName::from("bar.Bar::foo@a:i,b:i@iu").method_of(),
            Some("bar.Bar")
        );

        assert_eq!(MangledFnName::from("foo@a:i,b:i@iu").method_of(), None);

        assert!(!MangledFnName::from("foo@a:i,b:i@i").result_may_be_undef());
        assert!(MangledFnName::from("foo@a:i,b:i@iu").result_may_be_undef());
    }

    #[test]
    #[should_panic]
    fn invalid_mangled_name_1() {
        MangledFnName::from("foo@a:i").unmangle();
    }

    #[test]
    #[should_panic]
    fn invalid_mangled_name_2() {
        MangledFnName::from("foo@@x").unmangle();
    }

    #[test]
    #[should_panic]
    fn invalid_mangled_name_3() {
        MangledFnName::from("foo@a:x@i").unmangle();
    }

    #[test]
    #[should_panic]
    fn missing_argument_name() {
        MangledFnName::from("foo@i@i").unmangle();
    }
}