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
mod predictor {
    use crate::config::Config;
    use crate::predictions::prediction::input_tensors::PredictionRow;
    use crate::predictions::prediction::input_tensors::VecvecCappedF32;
    use crate::predictions::prediction::{PREDMTRXCOLS, PREDMTRXROWS};
    use crate::predictions::prediction_malware::TfLiteMalware;
    use crate::predictions::prediction_static::TfLiteStatic;
    use crate::process::ProcessRecord;

    pub trait PredictorHandler {
        fn predict(&mut self, precord: &ProcessRecord) -> Option<f32>;
    }

    pub struct PredictorHandlerBehavioural<'a> {
        config: &'a Config,
        pub timesteps: VecvecCappedF32,
        predictions_count: usize,
        tflite_malware: TfLiteMalware,
    }

    impl PredictorHandler for PredictorHandlerBehavioural<'_> {
        fn predict(&mut self, precord: &ProcessRecord) -> Option<f32> {
            let timestep = PredictionRow::from(precord);
            self.timesteps.push_row(timestep.to_vec_f32()).unwrap();
            if self.timesteps.rows_len() > 0 {
                if self.is_prediction_required(precord) {
                    let prediction = self.tflite_malware.make_prediction(&self.timesteps);
                    return Some(prediction);
                }
            }
            None
        }
    }

    impl PredictorHandlerBehavioural<'_> {
        pub fn new(config: &Config) -> PredictorHandlerBehavioural {
            PredictorHandlerBehavioural {
                config,
                timesteps: VecvecCappedF32::new(PREDMTRXCOLS, PREDMTRXROWS),
                predictions_count: 0,
                tflite_malware: TfLiteMalware::new(),
            }
        }
        fn is_prediction_required(&self, precord: &ProcessRecord) -> bool {
            if precord.bytes_written < 2_000_000
                || precord.files_opened.len() < 70
                || precord.files_written.len() < 40
            {
                false
            } else {
                match self.predictions_count {
                    0..=3 => precord.driver_msg_count % self.config.threshold_drivermsgs == 0,
                    4..=10 => {
                        precord.driver_msg_count % (self.config.threshold_drivermsgs * 50) == 0
                    }
                    11..=50 => {
                        precord.driver_msg_count % (self.config.threshold_drivermsgs * 150) == 0
                    }
                    n if n > 100000 => false,
                    _ => precord.driver_msg_count % (self.config.threshold_drivermsgs * 300) == 0,
                }
            }
        }
    }

    pub struct PredictorHandlerStatic {
        predictor_static: TfLiteStatic,
        prediction: Option<f32>,
        is_prediction_calculated: bool,
    }

    impl PredictorHandler for PredictorHandlerStatic {
        fn predict(&mut self, precord: &ProcessRecord) -> Option<f32> {
            if !self.is_prediction_calculated {
                self.prediction = self.predictor_static.make_prediction(&precord.exepath);
                self.is_prediction_calculated = true;
            }
            self.prediction
        }
    }

    impl PredictorHandlerStatic {
        pub fn new() -> PredictorHandlerStatic {
            PredictorHandlerStatic {
                predictor_static: TfLiteStatic::new(),
                prediction: None,
                is_prediction_calculated: false
            }
        }
    }

    pub struct PredictorMalware<'a> {
        pub predictor_behavioural: PredictorHandlerBehavioural<'a>,
        pub predictor_static: PredictorHandlerStatic,
    }

    impl PredictorHandler for PredictorMalware<'_> {
        fn predict(&mut self, precord: &ProcessRecord) -> Option<f32> {
            let opt_pred_b = self.predictor_behavioural.predict(precord);
            let opt_pred_s = self.predictor_static.predict(precord);

            match (opt_pred_s, opt_pred_b) {
                (Some(pred_s), Some(pred_b)) => Some(self.ponderate_prediction(precord, pred_s, pred_b)),
                (Some(pred_s), None) => Some(pred_s),
                (None, Some(pred_b)) => Some(pred_b),
                _ => None
            }
        }
    }

    impl PredictorMalware<'_> {
        pub fn new<'a>(config: &'a Config) -> PredictorMalware<'a> {
            PredictorMalware {
                predictor_behavioural: PredictorHandlerBehavioural::new(config),
                predictor_static: PredictorHandlerStatic::new(),
            }
        }

        fn ponderate_prediction(&self, precord: &ProcessRecord, pred_s: f32, pred_b: f32) -> f32 {
            let ponderation = match precord.driver_msg_count {
                0..=20 => 0.0,
                21..=50 => 0.5,
                _ => 0.8
            };
            (1.0-ponderation) * pred_s + ponderation * pred_b
        }
    }
}

pub mod process_record_handling {
    use std::os::raw::c_ulonglong;
    use std::sync::mpsc::Sender;

    use bindings::Windows::Win32::System::Diagnostics::Debug::{
        DebugActiveProcess
    };

    use crate::actions_on_kill::ActionsOnKill;
    use crate::config::{Config, KillPolicy, Param};
    use crate::csvwriter::CsvWriter;
    use crate::predictions::prediction::input_tensors::PredictionRow;
    use crate::process::{ProcessRecord, ProcessState};
    use crate::worker::predictor::{
        PredictorHandler, PredictorMalware,
    };

    pub trait ProcessRecordIOHandler {
        fn handle_io(&mut self, process_record: &mut ProcessRecord);
    }

    pub struct ProcessRecordHandlerLive<'a> {
        config: &'a Config,
        tx_kill: Sender<c_ulonglong>,
        predictor_malware: PredictorMalware<'a>,
    }

    impl ProcessRecordIOHandler for ProcessRecordHandlerLive<'_> {
        fn handle_io(&mut self, precord: &mut ProcessRecord) {
            if let Some(prediction_behavioural) = self
                .predictor_malware
                .predict(precord)
            {
                println!("{} - {}", precord.appname, prediction_behavioural);
                if prediction_behavioural > self.config.threshold_prediction
                    || precord.appname.contains("TEST-OLRANSOM")
                {
                    println!("Ransomware Suspected!!!");
                    eprintln!("precord.gid = {:?}", precord.gid);
                    println!("{}", precord.appname);
                    println!("with {} certainty", prediction_behavioural);
                    println!(
                        "\nSee {}\\threats for details.",
                        self.config[Param::DebugPath]
                    );
                    println!(
                        "\nPlease update {}\\exclusions.txt if it's a false positive",
                        self.config[Param::ConfigPath]
                    );

                    match self.config.get_kill_policy() {
                        KillPolicy::Suspend => {
                            if precord.process_state != ProcessState::Suspended {
                                try_suspend(precord);
                            }
                        }
                        KillPolicy::Kill => {
                            self.tx_kill.send(precord.gid).unwrap();
                        }
                        KillPolicy::DoNothing => {}
                    }
                    ActionsOnKill::new().run_actions(
                        &self.config,
                        precord,
                        &self.predictor_malware.predictor_behavioural.timesteps,
                        prediction_behavioural,
                    );
                }
            }
        }
    }

    impl<'a> ProcessRecordHandlerLive<'a> {
        pub fn new(
            config: &'a Config,
            tx_kill: Sender<c_ulonglong>,
        ) -> ProcessRecordHandlerLive<'a> {
            ProcessRecordHandlerLive {
                config,
                tx_kill,
                predictor_malware: PredictorMalware::new(config),
            }
        }
    }

    pub struct ProcessRecordHandlerReplay {
        // predictor_behavioural: PredictorHandlerBehavioural<'a>,
        csvwriter: CsvWriter,
        timesteps_stride: usize,
    }

    impl ProcessRecordIOHandler for ProcessRecordHandlerReplay {
        fn handle_io(&mut self, precord: &mut ProcessRecord) {
            let timestep = PredictionRow::from(precord);
            if precord.driver_msg_count % self.timesteps_stride == 0 {
                self.csvwriter
                    .write_debug_csv_files(&precord.appname, precord.gid, &timestep)
                    .expect("Cannot write csv learn file");
            }
            // if let Some(prediction) = self.predictor_behavioural.predict(precord) {
            // if prediction > self.config.threshold_prediction {
            //     println!("Record {}: {}", precord.appname, prediction);
            //     println!("########");
            // }
            // }
        }
    }

    impl ProcessRecordHandlerReplay {
        pub fn new(config: &Config) -> ProcessRecordHandlerReplay {
            ProcessRecordHandlerReplay {
                // predictor_behavioural: PredictorHandlerBehavioural::new(config),
                csvwriter: CsvWriter::from(config),
                timesteps_stride: config.timesteps_stride,
            }
        }
    }

    fn try_suspend(proc: &mut ProcessRecord) {
        proc.process_state = ProcessState::Suspended;
        for pid in &proc.pids {
            unsafe {
                DebugActiveProcess(*pid as u32);
            }
        }
    }
}

mod process_records {
    use std::collections::HashMap;
    use std::os::raw::c_ulonglong;

    use crate::process::ProcessRecord;

    pub struct ProcessRecords {
        pub process_records: HashMap<c_ulonglong, ProcessRecord>,
    }

    impl ProcessRecords {
        pub fn new() -> ProcessRecords {
            ProcessRecords {
                process_records: HashMap::new(),
            }
        }

        pub fn get_precord_by_gid(&self, gid: c_ulonglong) -> Option<&ProcessRecord> {
            self.process_records.get(&gid)
        }

        pub fn get_precord_mut_by_gid(&mut self, gid: c_ulonglong) -> Option<&mut ProcessRecord> {
            self.process_records.get_mut(&gid)
        }

        pub fn insert_precord(&mut self, gid: c_ulonglong, precord: ProcessRecord) {
            self.process_records.insert(gid, precord);
        }
    }
}

pub mod worker {
    use std::path::{Path, PathBuf};

    use bindings::Windows::Win32::Foundation::{CloseHandle, HINSTANCE, PSTR};
    use bindings::Windows::Win32::System::Diagnostics::Debug::GetLastError;
    use bindings::Windows::Win32::System::ProcessStatus::K32GetModuleFileNameExA;
    use bindings::Windows::Win32::System::Threading::{
        OpenProcess, PROCESS_QUERY_INFORMATION, PROCESS_VM_READ,
    };
    use windows::Handle;

    use crate::{IOMessage};
    use crate::config::{Config, Param};
    use crate::csvwriter::CsvWriter;
    use crate::process::ProcessRecord;
    use crate::whitelist::WhiteList;
    use crate::worker::process_record_handling::{
        ProcessRecordHandlerReplay, ProcessRecordIOHandler,
    };
    use crate::worker::process_records::ProcessRecords;

    pub trait IOMsgPostProcessor {
        fn postprocess(&mut self, iomsg: &mut IOMessage, precord: &ProcessRecord);
    }

    pub struct IOMsgPostProcessorWriter {
        csv_writer: CsvWriter,
    }

    impl IOMsgPostProcessor for IOMsgPostProcessorWriter {
        fn postprocess(&mut self, iomsg: &mut IOMessage, precord: &ProcessRecord) {
            iomsg.runtime_features.exepath = precord.exepath.clone();
            iomsg.runtime_features.exe_still_exists = true;
            let buf = rmp_serde::to_vec(&iomsg).unwrap();
            self.csv_writer
                .write_irp_csv_files(&buf)
                .expect("Cannot write irp file");
        }
    }

    impl IOMsgPostProcessorWriter {
        pub fn from(config: &Config) -> IOMsgPostProcessorWriter {
            let filename =
                &Path::new(&config[Param::DebugPath]).join(Path::new("drivermessages.txt"));
            IOMsgPostProcessorWriter {
                csv_writer: CsvWriter::from_path(filename),
            }
        }
    }

    pub struct Worker<'a> {
        whitelist: Option<&'a WhiteList>,
        process_records: ProcessRecords,
        process_record_handler: Option<Box<dyn ProcessRecordIOHandler + 'a>>,
        iomsg_postprocessors: Vec<Box<dyn IOMsgPostProcessor>>,
    }

    impl<'a> Worker<'a> {
        pub fn new() -> Worker<'a> {
            Worker {
                whitelist: None,
                process_records: ProcessRecords::new(),
                process_record_handler: None,
                iomsg_postprocessors: vec![],
            }
        }

        pub fn whitelist(mut self, whitelist: &'a WhiteList) -> Worker<'a> {
            self.whitelist = Some(&whitelist);
            self
        }

        pub fn process_record_handler(
            mut self,
            phandler: Box<dyn ProcessRecordIOHandler + 'a>,
        ) -> Worker<'a> {
            self.process_record_handler = Some(phandler);
            self
        }

        pub fn register_iomsg_postprocessor(
            mut self,
            postprecessor: Box<dyn IOMsgPostProcessor>,
        ) -> Worker<'a> {
            self.iomsg_postprocessors.push(postprecessor);
            self
        }

        pub fn build(self) -> Worker<'a> {
            self
        }

        pub fn new_replay(config: &'a Config, whitelist: &'a WhiteList) -> Worker<'a> {
            Worker {
                whitelist: Some(whitelist),
                process_records: ProcessRecords::new(),
                process_record_handler: Some(Box::new(ProcessRecordHandlerReplay::new(config))),
                iomsg_postprocessors: vec![],
            }
        }

        pub fn process_io(&mut self, iomsg: &mut IOMessage) {
            self.register_precord(iomsg);
            if let Some(precord) = self.process_records.get_precord_mut_by_gid(iomsg.gid) {
                precord.add_irp_record(iomsg);
                if let Some(process_record_handler) = &mut self.process_record_handler {
                    process_record_handler.handle_io(precord);
                }
                for postprocessor in &mut self.iomsg_postprocessors {
                    postprocessor.postprocess(iomsg, precord);
                }
            }
        }

        fn register_precord(&mut self, iomsg: &mut IOMessage) {
            match self.process_records.get_precord_by_gid(iomsg.gid) {
                None => {
                    if let Some(exepath) = self.exepath_from_pid(iomsg) {
                        let appname = self
                            .appname_from_exepath(&exepath)
                            .unwrap_or(String::from("DEFAULT"));
                        if !self.is_app_whitelisted(&appname) {
                            if !exepath
                                .parent()
                                .unwrap_or(Path::new("/"))
                                .starts_with(r"C:\Windows\System32")
                            {
                                let precord = ProcessRecord::from(
                                    iomsg,
                                    appname,
                                    exepath.clone(),
                                );
                                self.process_records.insert_precord(iomsg.gid, precord);
                            }
                        }
                    }
                }
                Some(_) => {}
            }
        }

        fn is_app_whitelisted(&self, appname: &str) -> bool {
            match self.whitelist {
                None => false,
                Some(wl) => wl.is_app_whitelisted(appname),
            }
        }

        fn exepath_from_pid(&self, iomsg: &IOMessage) -> Option<PathBuf> {
            let pid = iomsg.pid.clone() as u32;
            unsafe {
                let handle = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, false, pid);
                if handle.is_invalid() || handle.0 == 0 {
                    //println!("ERROR: Invalid Handle: {} - {}", drivermsg.pid, GetLastError().0);
                } else {
                    let mut buffer: Vec<u8> = Vec::new();
                    buffer.resize(1024, 0);
                    let res = K32GetModuleFileNameExA(
                        handle,
                        HINSTANCE(0),
                        PSTR(buffer.as_mut_ptr()),
                        1024,
                    );
                    if res == 0 {
                        let _errorcode = GetLastError().0;
                    } else {
                        let pathbuf = PathBuf::from(
                            String::from_utf8_unchecked(buffer).trim_matches(char::from(0)),
                        );
                        return Some(pathbuf);
                    }
                    CloseHandle(handle);
                }
            }
            None
        }

        fn appname_from_exepath(&self, exepath: &PathBuf) -> Option<String> {
            if let Some(filename) = exepath.file_name() {
                Some(filename.to_string_lossy().to_string())
            } else {
                None
            }
        }
    }
}