aboutsummaryrefslogtreecommitdiffstats
path: root/src/main.rs
blob: e23d2242ea8e8f10bd3437ec1b7f73ac5b475d1f (plain)
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
use std::fs::File;
use std::io::Read;
use std::path::Path;
use std::collections::HashMap;

use byteorder::{WriteBytesExt, BigEndian};
use regex::Regex;
use serde::Deserialize;
use log::{debug, warn};

#[derive(Debug)]
struct OperatorTree {
    left: Box<OperatorNode>,
    right: Box<OperatorNode>,
    operator: u8,
}

#[derive(Debug)]
struct OperatorOf {
    n: String,
    pattern: String,
}

#[derive(Debug)]
enum OperatorNode {
    Tree(OperatorTree),
    Of(OperatorOf),
    Identifier(String),
}

#[derive(Deserialize, Debug)]
struct RuleString {
    id: String,
    #[serde(rename = "type")]
    string_type: u8,
    text: String,
    modifiers: HashMap<String, bool>,
}

#[derive(Deserialize, Debug)]
struct RuleEntry {
    identifier: String,
    condition: String,
    strings: Vec<RuleString>,
}

pub struct YaraDatabase {
    entries: Vec<RuleEntry>,
    pub data: Vec<u8>,
}

impl YaraDatabase {
    const STRING_TYPE_STRING: u8 = 0;
    const STRING_TYPE_HEX: u8 = 1;
    const STRING_TYPE_REGEX: u8 = 2;

    const WILDCARD_NIBBLE_LOW: u8 = 0;
    const WILDCARD_NIBBLE_HIGH: u8 = 1;
    const WILDCARD_NIBBLE_BOTH: u8 = 2;

    const CONDITION_OPERATOR_OR: u8 = 0;
    const CONDITION_OPERATOR_AND: u8 = 1;
    const CONDITION_OPERATOR_OF: u8 = 2;
    const CONDITION_OPERATOR_SINGLE: u8 = 3;
    const CONDITION_OPERATOR_TRUE: u8 = 4;

    pub fn new() -> Self {
        YaraDatabase {
            entries: Vec::new(),
            data: Vec::new(),
        }
    }

    pub fn add_file<P: AsRef<Path>>(&mut self, path: P) -> Result<(), Box<dyn std::error::Error>> {
        let mut f = File::open(path)?;
        let mut content = String::new();
        f.read_to_string(&mut content)?;
        let json: serde_json::Value = serde_json::from_str(&content)?;
        if let Some(rules) = json.get("rules") {
            self.entries.extend(serde_json::from_value::<Vec<RuleEntry>>(rules.clone())?);
        }
        Ok(())
    }

    fn build_tree(condition: &str) -> OperatorNode {
        let pattern_or = Regex::new(r"(.*)\s+or\s+(.*)").unwrap();
        let pattern_and = Regex::new(r"(.*)\s+and\s+(.*)").unwrap();
        let pattern_of = Regex::new(r"((\d+)|(all)|(any))\s+of\s+([\w\_\(\)\$\*\,]+)").unwrap();

        debug!("Parsing condition = {}", condition);

        if let Some(caps) = pattern_or.captures(condition) {
            return OperatorNode::Tree(OperatorTree {
                left: Box::new(Self::build_tree(&caps[1])),
                right: Box::new(Self::build_tree(&caps[2])),
                operator: Self::CONDITION_OPERATOR_OR,
            });
        }

        if let Some(caps) = pattern_and.captures(condition) {
            return OperatorNode::Tree(OperatorTree {
                left: Box::new(Self::build_tree(&caps[1])),
                right: Box::new(Self::build_tree(&caps[2])),
                operator: Self::CONDITION_OPERATOR_AND,
            });
        }

        if let Some(caps) = pattern_of.captures(condition) {
            let n = caps[1].to_string();
            let pattern = caps[5].to_string();
            debug!("Leaf: OperatorOf, n = {}, pattern = {}", n, pattern);
            return OperatorNode::Of(OperatorOf { n, pattern });
        }

        debug!("Leaf: remainder = {}", condition);
        OperatorNode::Identifier(condition.trim().to_string())
    }

    fn compile_tree(node: &OperatorNode, strings: &[String]) -> Vec<u8> {
        let mut data = Vec::new();

        match node {
            OperatorNode::Tree(tree) => {
                data.write_u8(tree.operator).unwrap();
                data.extend(Self::compile_tree(&tree.left, strings));
                data.extend(Self::compile_tree(&tree.right, strings));
            }
            OperatorNode::Of(op_of) => {
                debug!("Compiling OperatorOf, n = {}, pattern = {}", op_of.n, op_of.pattern);
                data.write_u8(Self::CONDITION_OPERATOR_OF).unwrap();

                let mut pattern = String::new();
                let mut para = 0;
                for c in op_of.pattern.chars() {
                    match c {
                        '$' => pattern.push_str(r"\$"),
                        '*' => pattern.push_str(".*"),
                        ',' => pattern.push_str(")|("),
                        '(' => { pattern.push('('); para += 1; }
                        ')' => {
                            if para == 0 {
                                warn!("Unmatched parenthesis in pattern {}", op_of.pattern);
                            } else {
                                pattern.push(')');
                                para -= 1;
                            }
                        }
                        ' ' => {}
                        other => pattern.push(other),
                    }
                }
                let re = Regex::new(&format!("^({})$", pattern)).unwrap();
                let of_elements: Vec<u8> = strings.iter().enumerate()
                    .filter(|(_, s)| re.is_match(s))
                    .map(|(i, _)| i as u8)
                    .collect();

                let n = match op_of.n.as_str() {
                    "all" => 0,
                    "any" => 1,
                    _ => op_of.n.parse::<u8>().unwrap_or(0),
                };

                data.write_u8(n).unwrap();
                data.write_u8(of_elements.len() as u8).unwrap();
                for e in of_elements {
                    data.write_u8(e).unwrap();
                }
            }
            OperatorNode::Identifier(id) => {
                debug!("Compiling single identifier {}", id);
                let mut found = false;
                for (i, s) in strings.iter().enumerate() {
                    if s == id {
                        data.write_u8(Self::CONDITION_OPERATOR_SINGLE).unwrap();
                        data.write_u8(i as u8).unwrap();
                        found = true;
                        break;
                    }
                }
                if !found {
                    warn!("Single identifier {} not found, defaulting to true", id);
                    data.write_u8(Self::CONDITION_OPERATOR_TRUE).unwrap();
                }
            }
        }

        data
    }
}