|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/hillu/go-yara/v4" |
| 7 | +) |
| 8 | + |
| 9 | +func TestScanProcessesGrep(t *testing.T) { |
| 10 | + // Setup |
| 11 | + config := Configuration{} |
| 12 | + config.Input.Content.Grep = []string{"bad_string"} |
| 13 | + |
| 14 | + proc := ProcessInformation{ |
| 15 | + PID: 1234, |
| 16 | + ProcessName: "test_process.exe", |
| 17 | + MemoryDump: []byte("This is a memory dump containing a bad_string here."), |
| 18 | + } |
| 19 | + procs := []ProcessInformation{proc} |
| 20 | + |
| 21 | + // Execute |
| 22 | + matches := ScanProcesses(procs, config, nil) |
| 23 | + |
| 24 | + // Verify |
| 25 | + if matches != 1 { |
| 26 | + t.Errorf("Expected 1 match, got %d", matches) |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +func TestScanProcessesNoMatch(t *testing.T) { |
| 31 | + // Setup |
| 32 | + config := Configuration{} |
| 33 | + config.Input.Content.Grep = []string{"missing_string"} |
| 34 | + |
| 35 | + proc := ProcessInformation{ |
| 36 | + PID: 1234, |
| 37 | + ProcessName: "test_process.exe", |
| 38 | + MemoryDump: []byte("This is a memory dump containing a bad_string here."), |
| 39 | + } |
| 40 | + procs := []ProcessInformation{proc} |
| 41 | + |
| 42 | + // Execute |
| 43 | + matches := ScanProcesses(procs, config, nil) |
| 44 | + |
| 45 | + // Verify |
| 46 | + if matches != 0 { |
| 47 | + t.Errorf("Expected 0 matches, got %d", matches) |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +func TestScanProcessesYara(t *testing.T) { |
| 52 | + c, err := yara.NewCompiler() |
| 53 | + if err != nil { |
| 54 | + t.Skip("YARA compiler not available: ", err) |
| 55 | + return |
| 56 | + } |
| 57 | + err = c.AddString(`rule test { strings: $a = "yara_match" condition: $a }`, "test") |
| 58 | + if err != nil { |
| 59 | + t.Fatal("Failed to compile yara rule:", err) |
| 60 | + } |
| 61 | + rules, err := c.GetRules() |
| 62 | + if err != nil { |
| 63 | + t.Fatal("Failed to get rules:", err) |
| 64 | + } |
| 65 | + |
| 66 | + config := Configuration{} |
| 67 | + proc := ProcessInformation{ |
| 68 | + PID: 1234, |
| 69 | + ProcessName: "test_process.exe", |
| 70 | + MemoryDump: []byte("Before yara_match After"), |
| 71 | + } |
| 72 | + |
| 73 | + matches := ScanProcesses([]ProcessInformation{proc}, config, rules) |
| 74 | + if matches != 1 { |
| 75 | + t.Errorf("Expected 1 YARA match, got %d", matches) |
| 76 | + } |
| 77 | +} |
0 commit comments