-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
1190 lines (1007 loc) · 33.5 KB
/
main.go
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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"strings"
"time"
"github.com/charmbracelet/bubbles/list"
"github.com/charmbracelet/bubbles/spinner"
"github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/joho/godotenv"
"golang.org/x/oauth2/google"
)
const (
CHRONICLE_API_BASE_URL = "https://chronicle.googleapis.com"
)
const (
CLAUDE_API_URL = "https://api.anthropic.com/v1/messages"
CLAUDE_PROMPT = `
Analyze the provided JSON output from a security event detection system and create a concise summary with the following structure:
OUTCOME: MALICIOUS or NOT MALICIOUS (select one). Most Security Operation Analysts work hundreds of cases per day, and the majority are false positives. That does not mean everything is not malicious, but keep that in mind when making a decision.
1. Event Overview:
Detection Type
Alert ID
Creation Time
Detection Time
2. Rule Details:
Rule Name
Rule Description
Rule ID
Rule Version
Alert State
Risk Score
3. MITRE ATT&CK Information:
Tactic (TA)
Technique (T1)
MITRE URL
Describe what the identified TA and T1 actually mean. For example, TA0006 is Credential Access and T1558.003 is Steal or Forge Kerberos Tickets: Kerberoasting
4. Event Specifics:
Time Window (Start and End)
Principal Details:
Hostname
IP Address(es)
Username
Principal Process Details:
Command Line
File Path
SHA256 Hash
Target Details:
Hostname
IP Address(es)
Username
Administrative Domain
Application
Target Process Details:
Command Line
File Path
SHA256 Hash
Key Actions or Observations
5. Critical Highlights:
Emphasize any high-severity or unusual aspects and explain why it was marked malicious or not malicious. Make sure to say "I've decided this is (malicious or not malicious) because.. and then the reason.
Depending if it's malicious or not malicious, indicate why something could potentially be the opposite. For example, nltest is normally not malicious, but it can be used to enumerate domain controllers and is often used by threat actors. If you have information, provide feedback on where threat actors may abuse it.
Note any potential false positive indicators
Highlight any security results or additional relevant information
6. Recommended Next Steps:
Suggest immediate actions or further investigation points based on the event details
7. VirusTotal Summary:
Briefly summarize the VirusTotal results, including the number of checks performed, how many were flagged as malicious, and any errors encountered.
Please provide your analysis in a clear, spaced format, adhering to these guidelines:
1. Include only sections and information that are explicitly present in the JSON data.
2. Omit entire sections (including their headers) if no relevant data is found in the JSON.
3. Do not use placeholders like "N/A" or "Not Available". If a field is empty or missing, simply exclude it from your summary.
4. Focus on highlighting the most critical elements for quick review.
5. Ensure that your summary directly reflects the content of the JSON, without adding speculative or filler information.
Remember, it's better to have a shorter, more accurate summary than a longer one with placeholder or speculative information. Your summary should be a direct representation of the data provided in the JSON, omitting any sections or fields for which no data is available.
When analyzing the JSON:
- Only include a section if you find concrete data for it.
- Within each section, only list fields that have actual values in the JSON.
- If an entire section (like Target Details) is empty in the JSON, completely omit that section from your summary.
- Do not create or infer information that isn't explicitly stated in the JSON data.
Your goal is to provide a concise, accurate summary that reflects only the information present in the provided JSON, highlighting the most relevant details for security analysis.
`
)
var debugLogger *log.Logger
var (
maliciousStyle = lipgloss.NewStyle().
Foreground(lipgloss.Color("#FF0000")).
Bold(true)
notMaliciousStyle = lipgloss.NewStyle().
Foreground(lipgloss.Color("#00FF00")).
Bold(true)
panelStyle = lipgloss.NewStyle().
Border(lipgloss.RoundedBorder()).
BorderForeground(lipgloss.Color("#874BFD")).
Padding(1).
MarginRight(1)
)
var (
headerStyle = lipgloss.NewStyle().
Foreground(lipgloss.Color("#FFA500")).
Bold(true).
MarginBottom(1)
subHeaderStyle = lipgloss.NewStyle().
Foreground(lipgloss.Color("#ADD8E6")).
Bold(true)
contentStyle = lipgloss.NewStyle().
MarginLeft(2).
MarginBottom(1)
)
type AISummary struct {
Outcome string
EventOverview string
RuleDetails string
MitreAttackInfo string
EventSpecifics string
CriticalHighlights string
RecommendedNextSteps string
VirusTotalSummary string
}
func wrapText(text string, width int) string {
lines := strings.Split(text, "\n")
var wrappedLines []string
for _, line := range lines {
words := strings.Fields(strings.TrimSpace(line))
if len(words) == 0 {
wrappedLines = append(wrappedLines, "")
continue
}
wrapped := words[0]
spaceLeft := width - len(wrapped)
for _, word := range words[1:] {
if len(word)+1 > spaceLeft {
wrappedLines = append(wrappedLines, wrapped)
wrapped = word
spaceLeft = width - len(word)
} else {
wrapped += " " + word
spaceLeft -= 1 + len(word)
}
}
wrappedLines = append(wrappedLines, wrapped)
}
return strings.Join(wrappedLines, "\n")
}
func (m model) getPanelWidths() (int, int) {
panelWidth := (m.width - 4) / 2 // Subtract 4 for margins
return panelWidth, panelWidth
}
func parseAISummary(summary string) AISummary {
var result AISummary
sections := strings.Split(summary, "\n\n")
for _, section := range sections {
if strings.HasPrefix(section, "OUTCOME:") {
result.Outcome = strings.TrimSpace(strings.TrimPrefix(section, "OUTCOME:"))
} else if strings.HasPrefix(section, "1. Event Overview:") {
result.EventOverview = strings.TrimSpace(strings.TrimPrefix(section, "1. Event Overview:"))
} else if strings.HasPrefix(section, "2. Rule Details:") {
result.RuleDetails = strings.TrimSpace(strings.TrimPrefix(section, "2. Rule Details:"))
} else if strings.HasPrefix(section, "3. MITRE ATT&CK Information:") {
result.MitreAttackInfo = strings.TrimSpace(strings.TrimPrefix(section, "3. MITRE ATT&CK Information:"))
} else if strings.HasPrefix(section, "4. Event Specifics:") {
result.EventSpecifics = strings.TrimSpace(strings.TrimPrefix(section, "4. Event Specifics:"))
} else if strings.HasPrefix(section, "5. Critical Highlights:") {
result.CriticalHighlights = strings.TrimSpace(strings.TrimPrefix(section, "5. Critical Highlights:"))
} else if strings.HasPrefix(section, "6. Recommended Next Steps:") {
result.RecommendedNextSteps = strings.TrimSpace(strings.TrimPrefix(section, "6. Recommended Next Steps:"))
} else if strings.HasPrefix(section, "7. VirusTotal Summary:") {
result.VirusTotalSummary = strings.TrimSpace(strings.TrimPrefix(section, "7. VirusTotal Summary:"))
}
}
return result
}
func renderOutcome(outcome string) string {
if strings.ToUpper(outcome) == "MALICIOUS" {
return maliciousStyle.Render("OUTCOME: MALICIOUS")
}
return notMaliciousStyle.Render("OUTCOME: NOT MALICIOUS")
}
func formatEventOverview(overview string) string {
lines := strings.Split(overview, " - ")
return strings.Join(lines, "\n\n")
}
func formatRuleDetails(details string) string {
lines := strings.Split(details, " - ")
return strings.Join(lines, "\n\n")
}
func formatDetectionDetails(details string) string {
lines := strings.Split(details, "\n")
var formattedLines []string
var currentSection string
isSubSection := false
for _, line := range lines {
line = strings.TrimSpace(line)
if line == "" {
formattedLines = append(formattedLines, "")
continue
}
if strings.HasSuffix(line, ":") {
// This is a main section header
if currentSection != "" {
formattedLines = append(formattedLines, "") // Add extra newline between sections
}
currentSection = line
formattedLines = append(formattedLines, subHeaderStyle.Render(line))
isSubSection = false
} else if strings.Contains(line, ":") {
// This is a key-value pair
parts := strings.SplitN(line, ":", 2)
if len(parts) == 2 {
key := strings.TrimSpace(parts[0])
value := strings.TrimSpace(parts[1])
formattedLines = append(formattedLines, fmt.Sprintf("%s: %s", lipgloss.NewStyle().Foreground(lipgloss.Color("#ADD8E6")).Render(key), value))
} else {
formattedLines = append(formattedLines, line)
}
isSubSection = false
} else {
// This is a continuation of the previous line or a new subsection
if !isSubSection {
formattedLines = append(formattedLines, "")
isSubSection = true
}
formattedLines = append(formattedLines, line)
}
}
return strings.Join(formattedLines, "\n")
}
func formatMitreInfo(info string) string {
lines := strings.Split(info, " - ")
return strings.Join(lines, "\n\n")
}
func formatVirusTotalSummary(summary string) string {
lines := strings.Split(summary, " - ")
return strings.Join(lines, "\n\n")
}
func (m model) renderDetailsPanel(summary AISummary) string {
leftWidth, _ := m.getPanelWidths()
contentWidth := leftWidth - 8 // Adjust for padding and margins
formatSection := func(title string, content string) string {
return fmt.Sprintf("%s\n%s",
subHeaderStyle.Render(title),
contentStyle.Render(wrapText(content, contentWidth)),
)
}
content := lipgloss.JoinVertical(lipgloss.Left,
formatSection("Event Overview:", formatEventOverview(summary.EventOverview)),
formatSection("Rule Details:", formatRuleDetails(summary.RuleDetails)),
formatSection("MITRE ATT&CK Information:", formatMitreInfo(summary.MitreAttackInfo)),
formatSection("VirusTotal Summary:", formatVirusTotalSummary(summary.VirusTotalSummary)),
)
return panelStyle.Width(leftWidth).Render(lipgloss.JoinVertical(lipgloss.Left,
headerStyle.Render("Details"),
content,
))
}
func (m model) renderHighlightsPanel(summary AISummary) string {
_, rightWidth := m.getPanelWidths()
contentWidth := rightWidth - 8 // Adjust for padding and margins
formatSection := func(title string, content string) string {
return fmt.Sprintf("%s\n%s",
subHeaderStyle.Render(title),
contentStyle.Render(wrapText(content, contentWidth)),
)
}
content := lipgloss.JoinVertical(lipgloss.Left,
formatSection("Critical Highlights:", summary.CriticalHighlights),
formatSection("Recommended Next Steps:", summary.RecommendedNextSteps),
)
return panelStyle.Width(rightWidth).Render(lipgloss.JoinVertical(lipgloss.Left,
headerStyle.Render("Highlights & Next Steps"),
content,
))
}
func (m model) renderDetectionDetailsPanel(summary AISummary) string {
fullWidth := m.width - 4 // Adjust for margins
contentWidth := fullWidth - 8 // Adjust for padding and margins
formattedDetails := formatDetectionDetails(summary.EventSpecifics)
wrappedContent := wrapText(formattedDetails, contentWidth)
content := contentStyle.Render(wrappedContent)
return panelStyle.Width(fullWidth).Render(lipgloss.JoinVertical(lipgloss.Left,
headerStyle.Render("Detection Details"),
content,
))
}
func initDebugLogger() {
debugFile, err := os.OpenFile("virustotal_debug.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Printf("Error opening debug log file: %v", err)
return
}
debugLogger = log.New(debugFile, "", log.LstdFlags)
}
func debugLog(format string, v ...interface{}) {
if debugLogger != nil {
debugLogger.Printf(format, v...)
}
}
func queryVirusTotal(hash string, apiKey string) (bool, error) {
debugLog("Querying VirusTotal for hash: %s", hash)
url := fmt.Sprintf("https://www.virustotal.com/api/v3/files/%s", hash)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
debugLog("Error creating request: %v", err)
return false, err
}
req.Header.Add("accept", "application/json")
req.Header.Add("x-apikey", apiKey)
debugLog("Sending request to VirusTotal")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
debugLog("Error sending request: %v", err)
return false, err
}
defer resp.Body.Close()
debugLog("Response status code: %d", resp.StatusCode)
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
debugLog("Error reading response body: %v", err)
return false, err
}
debugLog("Response body: %s", string(body))
var result map[string]interface{}
err = json.Unmarshal(body, &result)
if err != nil {
debugLog("Error unmarshalling JSON: %v", err)
return false, err
}
data, ok := result["data"].(map[string]interface{})
if !ok {
debugLog("Unexpected response format: 'data' field not found or not a map")
return false, fmt.Errorf("unexpected response format")
}
attributes, ok := data["attributes"].(map[string]interface{})
if !ok {
debugLog("Unexpected response format: 'attributes' field not found or not a map")
return false, fmt.Errorf("unexpected response format")
}
lastAnalysisStats, ok := attributes["last_analysis_stats"].(map[string]interface{})
if !ok {
debugLog("Unexpected response format: 'last_analysis_stats' field not found or not a map")
return false, fmt.Errorf("unexpected response format")
}
malicious, ok := lastAnalysisStats["malicious"].(float64)
if !ok {
debugLog("Unexpected response format: 'malicious' field not found or not a float64")
return false, fmt.Errorf("unexpected response format")
}
debugLog("Malicious count: %f", malicious)
return malicious > 0, nil
}
type Rule struct {
Name string `json:"name"`
DisplayName string `json:"displayName"`
Author string `json:"author"`
RuleID string
}
func (r Rule) Title() string { return r.DisplayName }
func (r Rule) Description() string { return "Rule ID: " + r.RuleID }
func (r Rule) FilterValue() string { return r.DisplayName }
type Detection struct {
Type string `json:"type"`
Detection []RuleDetection `json:"detection"`
CreatedTime time.Time `json:"createdTime"`
ID string `json:"id"`
TimeWindow TimeWindow `json:"timeWindow"`
CollectionElements []CollectionElement `json:"collectionElements"`
DetectionTime time.Time `json:"detectionTime"`
}
type RuleDetection struct {
RuleName string `json:"ruleName"`
Description string `json:"description"`
URLBackToProduct string `json:"urlBackToProduct"`
RuleID string `json:"ruleId"`
RuleVersion string `json:"ruleVersion"`
AlertState string `json:"alertState"`
RuleType string `json:"ruleType"`
RuleLabels []Label `json:"ruleLabels"`
RiskScore int `json:"riskScore"`
}
type Label struct {
Key string `json:"key"`
Value string `json:"value"`
}
type TimeWindow struct {
StartTime time.Time `json:"startTime"`
EndTime time.Time `json:"endTime"`
}
type CollectionElement struct {
References []Reference `json:"references"`
Label string `json:"label"`
}
type Reference struct {
Event Event `json:"event"`
}
type Event struct {
Metadata EventMetadata `json:"metadata"`
Additional map[string]interface{} `json:"additional"`
Principal Principal `json:"principal"`
Target Target `json:"target"`
Intermediary []Intermediary `json:"intermediary"`
Observer Observer `json:"observer"`
About []About `json:"about"`
SecurityResult []SecurityResult `json:"securityResult"`
}
type EventMetadata struct {
ProductLogID string `json:"productLogId"`
EventTimestamp time.Time `json:"eventTimestamp"`
EventType string `json:"eventType"`
VendorName string `json:"vendorName"`
ProductName string `json:"productName"`
ProductEventType string `json:"productEventType"`
Description string `json:"description"`
IngestedTimestamp time.Time `json:"ingestedTimestamp"`
ProductDeploymentID string `json:"productDeploymentId"`
ID string `json:"id"`
LogType string `json:"logType"`
BaseLabels map[string]interface{} `json:"baseLabels"`
EnrichmentLabels map[string]interface{} `json:"enrichmentLabels"`
}
type Principal struct {
Hostname string `json:"hostname"`
Process Process `json:"process"`
IP []string `json:"ip"`
Port int `json:"port"`
MAC []string `json:"mac"`
Namespace string `json:"namespace"`
Asset Asset `json:"asset"`
User User `json:"user"`
}
type Process struct {
PID string `json:"pid"`
CommandLine string `json:"commandLine"`
File File `json:"file"`
}
type File struct {
SHA256 string `json:"sha256"`
MD5 string `json:"md5"`
SHA1 string `json:"sha1"`
FullPath string `json:"fullPath"`
}
type Asset struct {
Hostname string `json:"hostname"`
IP []string `json:"ip"`
MAC []string `json:"mac"`
}
type Target struct {
User User `json:"user"`
AdministrativeDomain string `json:"administrativeDomain"`
Application string `json:"application"`
Resource Resource `json:"resource"`
Namespace string `json:"namespace"`
Process Process `json:"process"`
IP []string `json:"ip"`
Hostname string `json:"hostname"`
}
type User struct {
UserID string `json:"userid"`
WindowsSid string `json:"windowsSid"`
}
type Resource struct {
Name string `json:"name"`
ResourceSubtype string `json:"resourceSubtype"`
}
type Intermediary struct {
Hostname string `json:"hostname"`
Namespace string `json:"namespace"`
}
type Observer struct {
Application string `json:"application"`
Namespace string `json:"namespace"`
Labels []Label `json:"labels"`
}
type About struct {
Namespace string `json:"namespace"`
Labels []Label `json:"labels"`
}
type SecurityResult struct {
RuleName string `json:"ruleName"`
Description string `json:"description"`
Severity string `json:"severity"`
}
type model struct {
list list.Model
allRules []Rule
filteredRules []Rule
searchInput textinput.Model
err error
nextPageToken string
loading bool
width int
height int
selectedRule *Rule
detections []Detection
viewState string
httpClient *http.Client
aiSummary string
detectionsList list.Model
selectedDetection *Detection
spinner spinner.Model
waitingForSummary bool
claudeApiKey string
}
type rulesMsg struct {
rules []Rule
nextPageToken string
}
type detectionsMsg struct {
detections []Detection
}
type errMsg error
type aiSummaryMsg string
type detectionItem struct {
detection Detection
}
type tickMsg struct{}
func (i detectionItem) Title() string {
return fmt.Sprintf("Detection ID: %s", i.detection.ID)
}
func (i detectionItem) Description() string {
return fmt.Sprintf("Created: %s, Type: %s", i.detection.CreatedTime.Format(time.RFC3339), i.detection.Type)
}
func (i detectionItem) FilterValue() string {
return i.detection.ID
}
func initialModel(claudeApiKey string) model {
ti := textinput.New()
ti.Placeholder = "Search rules..."
ti.Focus()
s := spinner.New()
s.Spinner = spinner.Dot
s.Style = lipgloss.NewStyle().Foreground(lipgloss.Color("205"))
return model{
list: list.New([]list.Item{}, list.NewDefaultDelegate(), 0, 0),
searchInput: ti,
loading: true,
viewState: "rules",
detectionsList: list.New([]list.Item{}, list.NewDefaultDelegate(), 0, 0),
spinner: s,
waitingForSummary: false,
claudeApiKey: claudeApiKey,
}
}
func (m model) Init() tea.Cmd {
return tea.Batch(
fetchRules(""),
initializeHttpClient(),
)
}
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.Type {
case tea.KeyCtrlC:
return m, tea.Quit
case tea.KeyEsc:
switch m.viewState {
case "rules":
return m, tea.Quit
case "detections":
m.viewState = "rules"
m.detections = nil
m.selectedRule = nil
return m, nil
case "detection_summary":
m.viewState = "detections"
m.selectedDetection = nil
m.aiSummary = ""
m.waitingForSummary = false
return m, nil
}
case tea.KeyTab:
if m.viewState == "rules" {
if m.searchInput.Focused() {
m.searchInput.Blur()
} else {
m.searchInput.Focus()
}
return m, nil
}
case tea.KeyEnter:
switch m.viewState {
case "rules":
selectedItem := m.list.SelectedItem()
if selectedItem != nil {
rule := selectedItem.(Rule)
m.selectedRule = &rule
return m, fetchLastWeekDetectionsCmd(rule.RuleID, m.httpClient)
}
case "detections":
selectedItem := m.detectionsList.SelectedItem()
if selectedItem != nil {
detection := selectedItem.(detectionItem).detection
m.selectedDetection = &detection
m.waitingForSummary = true
m.viewState = "detection_summary"
return m, tea.Batch(
generateAISummaryCmd(detection, m.claudeApiKey),
m.spinner.Tick,
)
}
}
default:
if m.viewState == "rules" {
if m.searchInput.Focused() {
m.searchInput, cmd = m.searchInput.Update(msg)
m.updateFilteredRules()
return m, cmd
} else {
m.list, cmd = m.list.Update(msg)
return m, cmd
}
}
}
case tea.WindowSizeMsg:
m.width = msg.Width
m.height = msg.Height
h, v := docStyle.GetFrameSize()
m.list.SetSize(msg.Width-h, msg.Height-v-3)
m.detectionsList.SetSize(msg.Width-h, msg.Height-v-3)
case rulesMsg:
m.allRules = append(m.allRules, msg.rules...)
m.nextPageToken = msg.nextPageToken
if m.nextPageToken != "" {
return m, fetchRules(m.nextPageToken)
}
m.loading = false
m.updateFilteredRules()
case detectionsMsg:
items := make([]list.Item, len(msg.detections))
for i, d := range msg.detections {
items[i] = detectionItem{d}
}
m.detectionsList.SetItems(items)
m.viewState = "detections"
return m, nil
case aiSummaryMsg:
m.aiSummary = string(msg)
m.waitingForSummary = false
return m, nil
case errMsg:
m.err = msg
m.loading = false
return m, nil
case *http.Client:
m.httpClient = msg
case tickMsg:
var cmd tea.Cmd
m.spinner, cmd = m.spinner.Update(msg)
return m, cmd
}
switch m.viewState {
case "rules":
m.list, cmd = m.list.Update(msg)
case "detections":
m.detectionsList, cmd = m.detectionsList.Update(msg)
}
return m, cmd
}
func (m *model) updateFilteredRules() {
filter := strings.ToLower(m.searchInput.Value())
if filter == "" {
m.filteredRules = m.allRules
} else {
m.filteredRules = []Rule{}
for _, rule := range m.allRules {
if strings.Contains(strings.ToLower(rule.DisplayName), filter) ||
strings.Contains(strings.ToLower(rule.RuleID), filter) {
m.filteredRules = append(m.filteredRules, rule)
}
}
}
items := make([]list.Item, len(m.filteredRules))
for i, rule := range m.filteredRules {
items[i] = rule
}
m.list.SetItems(items)
}
func (m model) View() string {
if m.err != nil {
return fmt.Sprintf("Error: %v", m.err)
}
if m.loading {
return "Loading rules... Please wait."
}
switch m.viewState {
case "rules":
focusText := "Tab to switch focus"
if m.searchInput.Focused() {
focusText = "Search: (Tab to focus list)"
} else {
focusText = "List: (Tab to focus search)"
}
return fmt.Sprintf(
"%s\n%s\n\n%s\n\nTotal rules: %d",
focusText,
m.searchInput.View(),
m.list.View(),
len(m.allRules),
)
case "detections":
return m.renderDetectionsList()
case "detection_summary":
return m.renderDetectionSummary()
}
return ""
}
func (m model) renderDetectionsList() string {
var content strings.Builder
titleStyle := lipgloss.NewStyle().
Foreground(lipgloss.Color("#FAFAFA")).
Background(lipgloss.Color("#7D56F4")).
Padding(0, 1).
MarginBottom(1)
content.WriteString(titleStyle.Render(fmt.Sprintf("Detections for Rule: %s (Last 7 days)", m.selectedRule.DisplayName)))
content.WriteString("\n\n")
content.WriteString(m.detectionsList.View())
content.WriteString("\n\nPress Enter to view detection summary, ESC to go back")
return content.String()
}
func (m model) renderDetectionSummary() string {
var content strings.Builder
titleStyle := lipgloss.NewStyle().
Foreground(lipgloss.Color("#FAFAFA")).
Background(lipgloss.Color("#7D56F4")).
Padding(0, 1).
MarginBottom(1)
content.WriteString(titleStyle.Render(fmt.Sprintf("Summary for Detection: %s", m.selectedDetection.ID)))
content.WriteString("\n\n")
if m.waitingForSummary {
content.WriteString(fmt.Sprintf("%s Generating AI summary...\n", m.spinner.View()))
} else {
summary := parseAISummary(m.aiSummary)
content.WriteString(renderOutcome(summary.Outcome))
content.WriteString("\n\n")
topRow := lipgloss.JoinHorizontal(lipgloss.Top,
m.renderDetailsPanel(summary),
m.renderHighlightsPanel(summary),
)
bottomRow := m.renderDetectionDetailsPanel(summary)
content.WriteString(lipgloss.JoinVertical(lipgloss.Left, topRow, bottomRow))
}
content.WriteString("\n\nPress ESC to go back to detections list")
return content.String()
}
func fetchRules(pageToken string) tea.Cmd {
return func() tea.Msg {
credFile := os.Getenv("CHRONICLE_CRED_FILE")
projectID := os.Getenv("CHRONICLE_PROJECT_ID")
instanceID := os.Getenv("CHRONICLE_INSTANCE_ID")
region := os.Getenv("CHRONICLE_REGION")
if region == "" {
region = "us" // Default to "us" if not specified
}
data, err := ioutil.ReadFile(credFile)
if err != nil {
return errMsg(err)
}
conf, err := google.JWTConfigFromJSON(data, "https://www.googleapis.com/auth/cloud-platform")
if err != nil {
return errMsg(err)
}
client := conf.Client(nil)
baseURL := fmt.Sprintf("https://%s-chronicle.googleapis.com/v1alpha/projects/%s/locations/%s/instances/%s/rules",
region, projectID, region, instanceID)
u, err := url.Parse(baseURL)
if err != nil {
return errMsg(err)
}
q := u.Query()
if pageToken != "" {
q.Set("pageToken", pageToken)
}
u.RawQuery = q.Encode()
resp, err := client.Get(u.String())
if err != nil {
return errMsg(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return errMsg(err)
}
var result struct {
Rules []Rule `json:"rules"`
NextPageToken string `json:"nextPageToken"`
}
if err := json.Unmarshal(body, &result); err != nil {
return errMsg(err)
}
for i := range result.Rules {
parts := strings.Split(result.Rules[i].Name, "/")
result.Rules[i].RuleID = parts[len(parts)-1]
}
return rulesMsg{rules: result.Rules, nextPageToken: result.NextPageToken}
}
}
func fetchDetections(ruleId, alertState, startTime, endTime string, client *http.Client, region, projectID, instanceID string, pageToken string) ([]Detection, error) {
baseURL := fmt.Sprintf("https://%s-chronicle.googleapis.com/v1alpha/projects/%s/locations/%s/instances/%s/legacy:legacySearchDetections",
region, projectID, region, instanceID)
u, err := url.Parse(baseURL)
if err != nil {
return nil, err
}
q := u.Query()
q.Set("ruleId", ruleId)
if alertState != "" {
q.Set("alertState", alertState)
}
if startTime != "" {
q.Set("startTime", startTime)
}
if endTime != "" {
q.Set("endTime", endTime)
}
if pageToken != "" {
q.Set("pageToken", pageToken)
}
u.RawQuery = q.Encode()
resp, err := client.Get(u.String())
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var result struct {
Detections []Detection `json:"detections"`
NextPageToken string `json:"nextPageToken"`
}
if err := json.Unmarshal(body, &result); err != nil {
return nil, err
}
return result.Detections, nil
}
func fetchLastWeekDetectionsCmd(ruleId string, client *http.Client) tea.Cmd {
return func() tea.Msg {
region := os.Getenv("CHRONICLE_REGION")