|
4 | 4 | "encoding/json" |
5 | 5 | "fmt" |
6 | 6 | "net/http" |
| 7 | + "strings" |
7 | 8 |
|
8 | 9 | "github.com/utmstack/soc-ai/configurations" |
9 | 10 | "github.com/utmstack/soc-ai/schema" |
@@ -65,3 +66,141 @@ func ChangeAlertStatus(id string, status int, observations string) error { |
65 | 66 |
|
66 | 67 | return nil |
67 | 68 | } |
| 69 | + |
| 70 | +type AlertCorrelation struct { |
| 71 | + CurrentAlert schema.Alert |
| 72 | + RelatedAlerts []schema.Alert |
| 73 | + Classifications []string |
| 74 | +} |
| 75 | + |
| 76 | +func GetRelatedAlerts() ([]schema.GPTAlertResponse, error) { |
| 77 | + result, err := ElasticSearch(configurations.ALERT_INDEX_PATTERN, "*", "*") |
| 78 | + if err != nil { |
| 79 | + return nil, fmt.Errorf("error getting historical alerts: %v", err) |
| 80 | + } |
| 81 | + |
| 82 | + var alerts []schema.GPTAlertResponse |
| 83 | + err = json.Unmarshal(result, &alerts) |
| 84 | + if err != nil { |
| 85 | + return nil, fmt.Errorf("error unmarshalling alerts: %v", err) |
| 86 | + } |
| 87 | + |
| 88 | + return alerts, nil |
| 89 | +} |
| 90 | + |
| 91 | +func FindRelatedAlerts(currentAlert schema.Alert) (*AlertCorrelation, error) { |
| 92 | + correlation := &AlertCorrelation{ |
| 93 | + CurrentAlert: currentAlert, |
| 94 | + RelatedAlerts: []schema.Alert{}, |
| 95 | + Classifications: []string{}, |
| 96 | + } |
| 97 | + |
| 98 | + historicalResponses, err := GetRelatedAlerts() |
| 99 | + if err != nil { |
| 100 | + return nil, err |
| 101 | + } |
| 102 | + |
| 103 | + var alertIDs []string |
| 104 | + for _, resp := range historicalResponses { |
| 105 | + alertIDs = append(alertIDs, resp.ActivityID) |
| 106 | + } |
| 107 | + |
| 108 | + for _, id := range alertIDs { |
| 109 | + alert, err := GetAlertsInfo(id) |
| 110 | + if err != nil { |
| 111 | + continue |
| 112 | + } |
| 113 | + |
| 114 | + if isAlertRelated(currentAlert, alert) { |
| 115 | + correlation.RelatedAlerts = append(correlation.RelatedAlerts, alert) |
| 116 | + |
| 117 | + for _, resp := range historicalResponses { |
| 118 | + if resp.ActivityID == alert.ID { |
| 119 | + correlation.Classifications = append(correlation.Classifications, resp.Classification) |
| 120 | + break |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + return correlation, nil |
| 127 | +} |
| 128 | + |
| 129 | +func isAlertRelated(current, historical schema.Alert) bool { |
| 130 | + if current.Destination.IP != "" && current.Destination.IP == historical.Destination.IP { |
| 131 | + return true |
| 132 | + } |
| 133 | + if current.Destination.Port != 0 && current.Destination.Port == historical.Destination.Port { |
| 134 | + return true |
| 135 | + } |
| 136 | + if current.Destination.Host != "" && current.Destination.Host == historical.Destination.Host { |
| 137 | + return true |
| 138 | + } |
| 139 | + if current.Destination.User != "" && current.Destination.User == historical.Destination.User { |
| 140 | + return true |
| 141 | + } |
| 142 | + |
| 143 | + if current.Source.IP != "" && current.Source.IP == historical.Source.IP { |
| 144 | + return true |
| 145 | + } |
| 146 | + if current.Source.Port != 0 && current.Source.Port == historical.Source.Port { |
| 147 | + return true |
| 148 | + } |
| 149 | + if current.Source.Host != "" && current.Source.Host == historical.Source.Host { |
| 150 | + return true |
| 151 | + } |
| 152 | + if current.Source.User != "" && current.Source.User == historical.Source.User { |
| 153 | + return true |
| 154 | + } |
| 155 | + |
| 156 | + return false |
| 157 | +} |
| 158 | + |
| 159 | +func BuildCorrelationContext(correlation *AlertCorrelation) string { |
| 160 | + var context strings.Builder |
| 161 | + |
| 162 | + context.WriteString("\nHistorical Context:\n") |
| 163 | + context.WriteString(fmt.Sprintf("Found %d related alerts with similar characteristics:\n", len(correlation.RelatedAlerts))) |
| 164 | + |
| 165 | + for i, alert := range correlation.RelatedAlerts { |
| 166 | + context.WriteString(fmt.Sprintf("\nRelated Alert %d:\n", i+1)) |
| 167 | + context.WriteString(fmt.Sprintf("- Name: %s\n", alert.Name)) |
| 168 | + context.WriteString(fmt.Sprintf("- Severity: %s\n", alert.SeverityLabel)) |
| 169 | + context.WriteString(fmt.Sprintf("- Category: %s\n", alert.Category)) |
| 170 | + context.WriteString(fmt.Sprintf("- Classification: %s\n", correlation.Classifications[i])) |
| 171 | + context.WriteString(fmt.Sprintf("- Time: %s\n", alert.Timestamp)) |
| 172 | + |
| 173 | + if alert.Source.IP != "" { |
| 174 | + context.WriteString(fmt.Sprintf("- Source IP: %s\n", alert.Source.IP)) |
| 175 | + } |
| 176 | + if alert.Destination.IP != "" { |
| 177 | + context.WriteString(fmt.Sprintf("- Destination IP: %s\n", alert.Destination.IP)) |
| 178 | + } |
| 179 | + if alert.Source.Host != "" { |
| 180 | + context.WriteString(fmt.Sprintf("- Source Host: %s\n", alert.Source.Host)) |
| 181 | + } |
| 182 | + if alert.Destination.Host != "" { |
| 183 | + context.WriteString(fmt.Sprintf("- Destination Host: %s\n", alert.Destination.Host)) |
| 184 | + } |
| 185 | + if alert.Source.User != "" { |
| 186 | + context.WriteString(fmt.Sprintf("- Source User: %s\n", alert.Source.User)) |
| 187 | + } |
| 188 | + if alert.Destination.User != "" { |
| 189 | + context.WriteString(fmt.Sprintf("- Destination User: %s\n", alert.Destination.User)) |
| 190 | + } |
| 191 | + if alert.Source.Port != 0 { |
| 192 | + context.WriteString(fmt.Sprintf("- Source Port: %d\n", alert.Source.Port)) |
| 193 | + } |
| 194 | + if alert.Destination.Port != 0 { |
| 195 | + context.WriteString(fmt.Sprintf("- Destination Port: %d\n", alert.Destination.Port)) |
| 196 | + } |
| 197 | + if alert.Protocol != "" { |
| 198 | + context.WriteString(fmt.Sprintf("- Protocol: %s\n", alert.Protocol)) |
| 199 | + } |
| 200 | + if alert.Severity != 0 { |
| 201 | + context.WriteString(fmt.Sprintf("- Severity: %d\n", alert.Severity)) |
| 202 | + } |
| 203 | + } |
| 204 | + |
| 205 | + return context.String() |
| 206 | +} |
0 commit comments