|
1 | 1 | package api
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "bytes" |
4 | 5 | "encoding/json"
|
5 | 6 | "fmt"
|
| 7 | + "io" |
6 | 8 | "io/ioutil"
|
7 | 9 | "math/rand"
|
8 | 10 | "os"
|
9 | 11 | "path/filepath"
|
| 12 | + "strings" |
10 | 13 | "sync"
|
11 | 14 | "testing"
|
12 | 15 | "time"
|
13 | 16 |
|
14 |
| - "github.com/0xrawsec/golang-evtx/evtx" |
15 | 17 | "github.com/0xrawsec/golang-utils/log"
|
| 18 | + "github.com/0xrawsec/golang-utils/readers" |
16 | 19 | "github.com/0xrawsec/golang-utils/scanner"
|
17 | 20 | "github.com/0xrawsec/golang-utils/sync/semaphore"
|
| 21 | + "github.com/0xrawsec/whids/event" |
| 22 | + "github.com/0xrawsec/whids/utils" |
18 | 23 | )
|
19 | 24 |
|
20 | 25 | var (
|
@@ -50,35 +55,44 @@ var (
|
50 | 55 | },
|
51 | 56 | }
|
52 | 57 |
|
53 |
| - events = []string{ |
54 |
| - // regular log |
55 |
| - `{"Event":{"EventData":{"EventType":"CreateKey","Image":"C:\\Windows\\servicing\\TrustedInstaller.exe","ProcessGuid":"{49F1AF32-38C1-5AC7-0000-00105E5D0B00}","ProcessId":"2544","TargetObject":"HKLM\\SOFTWARE\\Microsoft\\EnterpriseCertificates\\Disallowed","UtcTime":"2018-04-06 20:07:14.423"},"System":{"Channel":"Microsoft-Windows-Sysmon/Operational","Computer":"CALDERA02.caldera.loc","Correlation":{},"EventID":"12","EventRecordID":"886970","Execution":{"ProcessID":"1456","ThreadID":"1712"},"Keywords":"0x8000000000000000","Level":"4","Opcode":"0","Provider":{"Guid":"{5770385F-C22A-43E0-BF4C-06F5698FFBD9}","Name":"Microsoft-Windows-Sysmon"},"Security":{"UserID":"S-1-5-18"},"Task":"12","TimeCreated":{"SystemTime":"2018-04-06T09:07:14.424360200Z"},"Version":"2"}}}`, |
56 |
| - // alert log |
57 |
| - `{"Event":{"EventData":{"CreationUtcTime":"2018-02-26 16:28:13.169","Image":"C:\\Program Files\\cagent\\cagent.exe","ProcessGuid":"{49F1AF32-11B0-5A90-0000-0010594E0100}","ProcessId":"1216","TargetFilename":"C:\\commander.exe","UtcTime":"2018-02-26 16:28:13.169"},"GeneInfo":{"Criticality":10,"Signature":["ExecutableFileCreated","NewExeCreatedInRoot"]},"System":{"Channel":"Microsoft-Windows-Sysmon/Operational","Computer":"CALDERA01.caldera.loc","Correlation":{},"EventID":"11","EventRecordID":"1274413","Execution":{"ProcessID":"1408","ThreadID":"1652"},"Keywords":"0x8000000000000000","Level":"4","Opcode":"0","Provider":{"Guid":"{5770385F-C22A-43E0-BF4C-06F5698FFBD9}","Name":"Microsoft-Windows-Sysmon"},"Security":{"UserID":"S-1-5-18"},"Task":"11","TimeCreated":{"SystemTime":"2018-02-26T16:28:13.185436300Z"},"Version":"2"}}}`, |
58 |
| - `{"Event":{"EventData":{"CommandLine":"\"powershell\" -command -","Company":"Microsoft Corporation","CurrentDirectory":"C:\\Windows\\system32\\","Description":"Windows PowerShell","FileVersion":"6.1.7600.16385 (win7_rtm.090713-1255)","Hashes":"SHA1=5330FEDAD485E0E4C23B2ABE1075A1F984FDE9FC,MD5=852D67A27E454BD389FA7F02A8CBE23F,SHA256=A8FDBA9DF15E41B6F5C69C79F66A26A9D48E174F9E7018A371600B866867DAB8,IMPHASH=F2C0E8A5BD10DBC167455484050CD683","Image":"C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe","IntegrityLevel":"System","LogonGuid":"{49F1AF32-11AE-5A90-0000-0020E7030000}","LogonId":"0x3e7","ParentCommandLine":"C:\\commander.exe -f","ParentImage":"C:\\commander.exe","ParentProcessGuid":"{49F1AF32-359D-5A94-0000-0010A9530C00}","ParentProcessId":"3068","ProcessGuid":"{49F1AF32-35A0-5A94-0000-0010FE5E0C00}","ProcessId":"1244","Product":"Microsoft® Windows® Operating System","TerminalSessionId":"0","User":"NT AUTHORITY\\SYSTEM","UtcTime":"2018-02-26 16:28:16.514"},"GeneInfo":{"Criticality":10,"Signature":["HeurSpawnShell","PowershellStdin"]},"System":{"Channel":"Microsoft-Windows-Sysmon/Operational","Computer":"CALDERA01.caldera.loc","Correlation":{},"EventID":"1","EventRecordID":"1274784","Execution":{"ProcessID":"1408","ThreadID":"1652"},"Keywords":"0x8000000000000000","Level":"4","Opcode":"0","Provider":{"Guid":"{5770385F-C22A-43E0-BF4C-06F5698FFBD9}","Name":"Microsoft-Windows-Sysmon"},"Security":{"UserID":"S-1-5-18"},"Task":"1","TimeCreated":{"SystemTime":"2018-04-06T16:28:16.530122800Z"},"Version":"5"}}}`, |
59 |
| - `{"Event":{"EventData":{"CallTrace":"C:\\Windows\\SYSTEM32\\ntdll.dll+4d61a|C:\\Windows\\system32\\KERNELBASE.dll+19577|UNKNOWN(000000001ABD2A68)","GrantedAccess":"0x143a","SourceImage":"C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe","SourceProcessGUID":"{49F1AF32-3922-5A94-0000-0010E3581900}","SourceProcessId":"1916","SourceThreadId":"2068","TargetImage":"C:\\Windows\\system32\\lsass.exe","TargetProcessGUID":"{49F1AF32-11AD-5A90-0000-00102F6F0000}","TargetProcessId":"472","UtcTime":"2018-02-26 16:43:26.380"},"GeneInfo":{"Criticality":10,"Signature":["HeurMaliciousAccess","MaliciousLsassAccess","SuspWriteAccess","SuspiciousLsassAccess"]},"System":{"Channel":"Microsoft-Windows-Sysmon/Operational","Computer":"CALDERA01.caldera.loc","Correlation":{},"EventID":"10","EventRecordID":"1293693","Execution":{"ProcessID":"1408","ThreadID":"1652"},"Keywords":"0x8000000000000000","Level":"4","Opcode":"0","Provider":{"Guid":"{5770385F-C22A-43E0-BF4C-06F5698FFBD9}","Name":"Microsoft-Windows-Sysmon"},"Security":{"UserID":"S-1-5-18"},"Task":"10","TimeCreated":{"SystemTime":"2018-02-26T16:43:26.447894800Z"},"Version":"3"}}}`, |
60 |
| - } |
| 58 | + eventFile = "./data/events.json" |
| 59 | + events = make([]event.EdrEvent, 0) |
61 | 60 | )
|
62 | 61 |
|
63 |
| -func emitEvents(count int) (ce chan *evtx.GoEvtxMap) { |
64 |
| - timecreatedPath := evtx.Path("/Event/System/TimeCreated/SystemTime") |
65 |
| - ce = make(chan *evtx.GoEvtxMap) |
| 62 | +func init() { |
| 63 | + data, err := ioutil.ReadFile(eventFile) |
| 64 | + if err != nil { |
| 65 | + panic(err) |
| 66 | + } |
| 67 | + for line := range readers.Readlines(bytes.NewBuffer(data)) { |
| 68 | + event := event.EdrEvent{} |
| 69 | + json.Unmarshal(line, &event) |
| 70 | + events = append(events, event) |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +func emitEvents(count int) (ce chan *event.EdrEvent) { |
| 75 | + ce = make(chan *event.EdrEvent) |
66 | 76 | go func() {
|
67 | 77 | defer close(ce)
|
68 | 78 | for i := 0; i < count; i++ {
|
69 |
| - e := new(evtx.GoEvtxMap) |
70 | 79 | i := rand.Int() % len(events)
|
71 |
| - err := json.Unmarshal([]byte(events[i]), e) |
72 |
| - e.Set(&timecreatedPath, time.Now().Format(time.RFC3339Nano)) |
73 |
| - if err != nil { |
74 |
| - log.Errorf("Cannot unmarshall event") |
75 |
| - } |
76 |
| - ce <- e |
| 80 | + e := events[i] |
| 81 | + e.Event.System.TimeCreated.SystemTime = time.Now() |
| 82 | + ce <- &e |
77 | 83 | }
|
78 | 84 | }()
|
79 | 85 | return
|
80 | 86 | }
|
81 | 87 |
|
| 88 | +func readerFromEvents(count int) io.Reader { |
| 89 | + tmp := make([]string, 0, count) |
| 90 | + for event := range emitEvents(count) { |
| 91 | + tmp = append(tmp, string(utils.Json(event))) |
| 92 | + } |
| 93 | + return bytes.NewBufferString(strings.Join(tmp, "\n")) |
| 94 | +} |
| 95 | + |
82 | 96 | func countLinesInGzFile(filepath string) int {
|
83 | 97 | var line int
|
84 | 98 | fd, err := os.Open(filepath)
|
|
0 commit comments