github.com/runreveal/sigmalitesigmalite パッケージは、[Sigma 検出フォーマット][] のパーサーと実行エンジンを提供します。
rule, err := sigmalite.ParseRule([]byte(`
title: My example rule
detection:
keywords:
- foo
- bar
selection:
EventId: 1234
condition: keywords and selection
`))
if err != nil {
return err
}
entry := &sigmalite.LogEntry{
Message: "Hello foo",
Fields: map[string]string{
"EventId": "1234",
},
}
isMatch := rule.Detection.Matches(entry, nil)
go get github.com/runreveal/sigmalite
ルールは YAML 形式で記述され、最低限 title と detection を含める必要があります。
title: My example rule
detection:
keywords:
- foo
- bar
selection:
EventId: 1234
condition: keywords and selection
detection ブロック内の condition フィールドは、同じ detection ブロック内の他のフィールドセレクターを結合する論理式です。この例では、このルールは EventId フィールドが 1234 と完全に一致し、かつ メッセージに "foo" または "bar" を含むログエントリにマッチします。
フィールドは [正規表現][] を使ってマッチさせることもできます。
title: My example rule with a timestamp
detection:
selection:
Timestamp|re: ^2024-06-01T(01|02|03):[0-5][0-9]:[0-5][0-9]$
condition: selection
さらに CIDR も使用できます。
title: My example rule with IP addresses
detection:
local:
DestinationIp|cidr:
- "127.0.0.0/8"
- "10.0.0.0/8"
- "172.16.0.0/12"
- "192.168.0.0/16"
- "169.254.0.0/16"
- "::1/128" # IPv6 loopback
- "fe80::/10" # IPv6 link-local addresses
- "fc00::/7" # IPv6 private addresses
condition: not local
詳細は [公式 Sigma ルールドキュメント][] を参照してください。
このライブラリは以下の [フィールド修飾子][] をサポートしています。
FieldResolver インターフェースは、標準の Sigma 仕様を拡張し、単純なキー/値ペアを超えた複雑なフィールド検索シナリオをサポートします。これにより、以下のようなカスタムフィールド解決ロジックを実装できます。
event.process.user)process.*.user や network[*].ip のようなフィールドパターンをサポートtype FieldResolver interface {
Resolve(fieldName string, entry *LogEntry) []string
}
Resolve メソッドは Sigma ルールからフィールド名を受け取り、マッチするすべての値を文字列スライスとして返します。マッチするものがない場合は nil または空のスライスを返します。
// CustomResolver demonstrates field resolution for structured logs
type CustomResolver struct{}
func (r *CustomResolver) Resolve(fieldName string, entry *sigma.LogEntry) []string {
switch fieldName {
case "process.users":
// Aggregate user fields from multiple sources
var users []string
if user, ok := entry.Fields["Event.Process.User"]; ok {
users = append(users, user)
}
if user, ok := entry.Fields["Event.Login.User"]; ok {
users = append(users, user)
}
if user, ok := entry.Fields["Event.Session.User"]; ok {
users = append(users, user)
}
return users
case "network.internal_ips":
// Extract all IP addresses from network-related fields
var ips []string
for fieldName, value := range entry.Fields {
if strings.Contains(strings.ToLower(fieldName), "ip") {
// Simple IP validation (in real usage, use proper validation)
if strings.Contains(value, ".") {
ips = append(ips, value)
}
}
}
return ips
default:
return nil
}
}
func matches(detection *sigmalite.Detection) bool {
opts := &sigmalite.MatchOptions{
FieldResolver: CustomResolver{},
},
entry := &sigmalite.LogEntry{
Message: string("Message Text"),
Fields: nil, // Using resolver so this can be empty
}
return detection.Matches(entry, opts)
}
フィールドリゾルバはすべての フィールド修飾子 とシームレスに連携し、解決された値に正規表現パターン、大文字小文字を区別しないマッチング、その他の変換を適用できます。