Skip to content

Commit 1947206

Browse files
committed
change timestamp handling when alertifying
We now want to provide the time of the original event that led to the generated alert as "timestamp_event" in the produced EVE-JSON, while updating the regular "timestamp" field with the the alertification time, to make these two events distinguishable.
1 parent 08a31ae commit 1947206

File tree

2 files changed

+44
-14
lines changed

2 files changed

+44
-14
lines changed

util/alertifier.go

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -112,27 +112,38 @@ func (a *Alertifier) MakeAlert(inputEvent types.Entry, ioc string,
112112
}
113113

114114
// ensure consistent timestamp formatting: try to parse as Suricata timestamp
115+
eventTimestampFormatted := newEntry.Timestamp
115116
inTimestampParsed, err := time.Parse(types.SuricataTimestampFormat, newEntry.Timestamp)
116117
if err != nil {
117118
// otherwise try to parse without zone information
118119
inTimestampParsed, err = time.Parse("2006-01-02T15:04:05.999999", newEntry.Timestamp)
119120
if err == nil {
120-
suriTimestampFormatted := inTimestampParsed.Format(types.SuricataTimestampFormat)
121-
escapedTimestamp, err := EscapeJSON(suriTimestampFormatted)
122-
if err != nil {
123-
return nil, err
124-
}
125-
l, err = jsonparser.Set([]byte(newEntry.JSONLine), escapedTimestamp, "timestamp")
126-
if err != nil {
127-
return nil, err
128-
}
129-
newEntry.Timestamp = suriTimestampFormatted
130-
newEntry.JSONLine = string(l)
121+
eventTimestampFormatted = inTimestampParsed.Format(types.SuricataTimestampFormat)
131122
} else {
132123
log.Warningf("keeping non-offset timestamp '%s', could not be transformed: %s", newEntry.Timestamp, err.Error())
133124
}
134125
}
135-
126+
// Set received original timestamp as "timestamp_event" field
127+
escapedTimestamp, err := EscapeJSON(eventTimestampFormatted)
128+
if err != nil {
129+
return nil, err
130+
}
131+
l, err = jsonparser.Set([]byte(newEntry.JSONLine), escapedTimestamp, "timestamp_event")
132+
if err != nil {
133+
return nil, err
134+
}
135+
// Add current (alerting) timestamp as "timestamp" field
136+
nowTimestampEscaped, err := EscapeJSON(time.Now().UTC().Format(types.SuricataTimestampFormat))
137+
if err != nil {
138+
return nil, err
139+
}
140+
l, err = jsonparser.Set(l, []byte(nowTimestampEscaped), "timestamp")
141+
if err != nil {
142+
return nil, err
143+
}
144+
// update returned entry
145+
newEntry.Timestamp = eventTimestampFormatted
146+
newEntry.JSONLine = string(l)
136147
return &newEntry, nil
137148
}
138149

util/alertifier_test.go

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package util
22

33
// DCSO FEVER
4-
// Copyright (c) 2020, DCSO GmbH
4+
// Copyright (c) 2020, 2021, DCSO GmbH
55

66
import (
77
"encoding/json"
@@ -17,12 +17,13 @@ import (
1717
)
1818

1919
func makeTestHTTPEvent(host string, url string) types.Entry {
20+
testTime, _ := time.Parse("2006-Jan-02", "2013-Feb-03")
2021
e := types.Entry{
2122
SrcIP: fmt.Sprintf("10.0.0.%d", rand.Intn(5)+1),
2223
SrcPort: int64(rand.Intn(60000) + 1025),
2324
DestIP: fmt.Sprintf("10.0.0.%d", rand.Intn(50)),
2425
DestPort: 80,
25-
Timestamp: time.Now().Format(types.SuricataTimestampFormat),
26+
Timestamp: testTime.Format(types.SuricataTimestampFormat),
2627
EventType: "http",
2728
Proto: "TCP",
2829
HTTPHost: host,
@@ -100,6 +101,24 @@ func checkAlertifierAlerts(t *testing.T, a *types.Entry, msg string, ioc string)
100101
if resAlert.ExtraInfo.VastIOC != ioc {
101102
t.Fatalf("wrong ioc ('%s' <-> '%s')", resAlert.ExtraInfo.VastIOC, ioc)
102103
}
104+
eventTimeVal, _, _, err := jsonparser.Get([]byte(a.JSONLine), "timestamp_event")
105+
if err != nil {
106+
t.Fatal(err)
107+
}
108+
if string(eventTimeVal) != "2013-02-03T00:00:00+0000" {
109+
t.Fatalf("wrong event timestamp ('%s' <-> '%s')", string(eventTimeVal), "2013-02-03T00:00:00+0000")
110+
}
111+
alertTimeVal, _, _, err := jsonparser.Get([]byte(a.JSONLine), "timestamp")
112+
if err != nil {
113+
t.Fatal(err)
114+
}
115+
alertTime, err := time.Parse(types.SuricataTimestampFormat, string(alertTimeVal))
116+
if err != nil {
117+
t.Fatal(err)
118+
}
119+
if !alertTime.Add(48 * time.Hour).After(time.Now()) {
120+
t.Fatalf("wrong alert unexpected ('%s' < '%s')", alertTime.Add(48*time.Hour), time.Now())
121+
}
103122
}
104123

105124
func testExtraModifier(inputAlert *types.Entry, ioc string) error {

0 commit comments

Comments
 (0)