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
pub static PREDMTRXCOLS: usize = 26;
pub static PREDMTRXROWS: usize = 500;
pub mod input_tensors {
use std::collections::VecDeque;
use std::error::Error;
use std::fmt::{Debug, Display, Formatter};
use std::ops::{Index, IndexMut};
use crate::extensions::ExtensionCategory;
use crate::extensions::ExtensionCategory::{Email, Event, PasswordVault};
use crate::process::ProcessRecord;
type Matrix<T> = VecDeque<Vec<T>>;
#[derive(Debug)]
pub struct PredictionRow {
pub ops_read: u64,
pub ops_setinfo: u64,
pub ops_written: u64,
pub ops_open: u64,
pub bytes_read: u64,
pub bytes_written: u64,
pub entropy_read: f32,
pub entropy_written: f32,
pub files_opened: usize,
pub files_deleted: usize,
pub files_read: usize,
pub files_renamed: usize,
pub files_written: usize,
pub extensions_read: usize,
pub extensions_written: usize,
pub extensions_written_doc: usize,
pub extensions_written_archives: usize,
pub extensions_written_db: usize,
pub extensions_written_code: usize,
pub extensions_written_exe: usize,
pub dirs_with_files_created: usize,
pub dirs_with_files_updated: usize,
pub pids: usize,
pub exe_exists: bool,
pub clusters: usize,
pub clusters_max_size: usize,
pub alters_email_file: bool,
pub password_vault_read_count: usize,
pub alters_event_log_file: bool,
pub alters_ssh_file: bool,
pub on_shared_drive_read_count: u32,
pub on_shared_drive_write_count: u32,
pub on_removable_drive_read_count: u32,
pub on_removable_drive_write_count: u32,
}
impl PredictionRow {
pub fn from(proc: &ProcessRecord) -> PredictionRow {
PredictionRow {
bytes_read: proc.bytes_read,
bytes_written: proc.bytes_written,
ops_read: proc.ops_read,
ops_setinfo: proc.ops_setinfo,
ops_written: proc.ops_written,
ops_open: proc.ops_open,
entropy_read: Self::order_magnitude(proc.entropy_read) as f32,
entropy_written: Self::order_magnitude(proc.entropy_written) as f32,
extensions_read: proc.extensions_read.count_all(),
extensions_written: proc.extensions_written.count_all(),
files_opened: proc.files_opened.len(),
files_deleted: proc.files_deleted.len(),
files_read: proc.files_read.len(),
files_renamed: proc.files_renamed.len(),
files_written: proc.files_written.len(),
pids: proc.pids.len(),
extensions_written_doc: proc
.extensions_written
.count_category(ExtensionCategory::Docs),
extensions_written_archives: proc
.extensions_written
.count_category(ExtensionCategory::Archives),
extensions_written_db: proc
.extensions_written
.count_category(ExtensionCategory::Database),
extensions_written_code: proc
.extensions_written
.count_category(ExtensionCategory::Code),
extensions_written_exe: proc
.extensions_written
.count_category(ExtensionCategory::Exe),
dirs_with_files_created: proc.dirs_with_files_created.len(),
dirs_with_files_updated: proc.dirs_with_files_updated.len(),
exe_exists: proc.exe_exists,
clusters: proc.clusters,
clusters_max_size: proc.clusters_max_size,
alters_email_file: proc.extensions_read.count_category(Email) > 0
|| proc.extensions_written.count_category(Email) > 0,
password_vault_read_count: proc.extensions_read.count_category(PasswordVault),
alters_event_log_file: proc.extensions_read.count_category(Event) > 0
|| proc.extensions_written.count_category(Event) > 0,
alters_ssh_file: proc.fpaths_updated.contains(".ssh"),
on_shared_drive_read_count: proc.on_shared_drive_read_count,
on_shared_drive_write_count: proc.on_shared_drive_write_count,
on_removable_drive_read_count: proc.on_removable_drive_read_count,
on_removable_drive_write_count: proc.on_removable_drive_write_count,
}
}
pub fn to_vec_f32(&self) -> Vec<f32> {
let res: Vec<f32> = vec![
self.ops_read as f32,
self.ops_setinfo as f32,
self.ops_written as f32,
self.ops_open as f32,
self.bytes_read as f32,
self.bytes_written as f32,
self.entropy_read as f32,
self.entropy_written as f32,
self.files_opened as f32,
self.files_deleted as f32,
self.files_read as f32,
self.files_renamed as f32,
self.files_written as f32,
self.extensions_read as f32,
self.extensions_written as f32,
self.extensions_written_doc as f32,
self.extensions_written_archives as f32,
self.extensions_written_db as f32,
self.extensions_written_code as f32,
self.extensions_written_exe as f32,
self.dirs_with_files_created as f32,
self.dirs_with_files_updated as f32,
self.pids as f32,
self.exe_exists as u8 as f32,
self.clusters as f32,
self.clusters_max_size as f32,
];
res
}
#[inline]
fn order_magnitude(a: f64) -> u32 {
if a <= 0f64 {
0
} else {
a.log10() as u32
}
}
}
pub type VecvecCappedF32 = VecvecCapped<f32>;
#[derive(Debug, Eq, PartialEq, Clone)]
pub struct VecvecCapped<T> {
pub capacity_cols: usize,
pub capacity_rows: usize,
elems: Matrix<T>,
}
impl<T: Copy + Clone + Debug> VecvecCapped<T> {
pub fn new(capacity_cols: usize, capacity_rows: usize) -> VecvecCapped<T> {
VecvecCapped {
capacity_cols,
capacity_rows,
elems: VecDeque::new(),
}
}
pub fn rows_len(&self) -> usize {
self.elems.len()
}
pub fn push_row(&mut self, row: Vec<T>) -> Result<(), VecvecCappedError> {
if row.len() != self.capacity_cols {
Err(VecvecCappedError::InvalidRowSize)
} else {
if self.elems.len() == self.capacity_rows {
self.elems.pop_front();
}
self.elems.push_back(row);
Ok(())
}
}
pub fn to_vec(&self) -> Vec<T> {
let mut res = Vec::new();
for v in self.elems.iter() {
let mut vc = v.clone();
res.append(&mut vc);
}
res
}
}
impl<T> Index<usize> for VecvecCapped<T> {
type Output = Vec<T>;
fn index(&self, index: usize) -> &Self::Output {
&self.elems[index]
}
}
impl<T> IndexMut<usize> for VecvecCapped<T> {
fn index_mut(&mut self, index: usize) -> &mut Self::Output {
&mut self.elems[index]
}
}
#[derive(Debug)]
pub enum VecvecCappedError {
InvalidRowSize,
}
impl Display for VecvecCappedError {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match *self {
VecvecCappedError::InvalidRowSize => write!(f, "Invalid row size"),
}
}
}
impl Error for VecvecCappedError {}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn add_invalid_size_row_should_fail() {
let mut mtrx = VecvecCapped::new(2, 3);
let v = vec![1, 2, 3, 4];
assert!(mtrx.push_row(v).is_err());
}
#[test]
fn add_too_many_rows_should_pop() {
let mut mtrx = VecvecCapped::new(3, 2);
let mut ctrl = VecvecCapped::new(3, 2);
let v1 = vec![1, 2, 3];
let v2 = vec![3, 4, 5];
let v3 = vec![6, 7, 8];
mtrx.push_row(v1.clone()).unwrap();
mtrx.push_row(v2.clone()).unwrap();
mtrx.push_row(v3.clone()).unwrap();
ctrl.push_row(v2.clone()).unwrap();
ctrl.push_row(v3.clone()).unwrap();
assert_eq!(mtrx, ctrl);
}
#[test]
fn test_square_bracket_op() {
let mut mtrx = VecvecCapped::new(3, 2);
let v1 = vec![1, 2, 3];
let v2 = vec![3, 4, 5];
mtrx.push_row(v1).unwrap();
mtrx.push_row(v2).unwrap();
assert_eq!(mtrx[1][2], 5);
}
}
}