/
logs.go
154 lines (131 loc) · 3.49 KB
/
logs.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
package recon
import (
"context"
"fmt"
"math"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/cloudwatchlogs"
"github.com/hupe1980/awsrecon/pkg/audit/logmessage"
"github.com/hupe1980/awsrecon/pkg/config"
)
var (
DefaultGroupNamePrefix = "/aws/lambda"
DefaultStreamNamePrefix = fmt.Sprintf("%d", time.Now().Year())
)
type Log struct {
AWSService string
Region string
GroupName string
StreamName string
Encrypted bool
Retention string
EventCount int
Hints []string
}
type LogsOptions struct {
GroupNamePrefix string
StreamNamePrefix string
FilterPattern string
StartTime int64
EndTime int64
Verify bool
BeforeHook BeforeHookFunc
AfterRunHook AfterRunHookFunc
}
type LogsRecon struct {
*recon[Log]
cloudwatchlogsClient *cloudwatchlogs.Client
engine *logmessage.Engine
opts LogsOptions
}
func NewLogsRecon(cfg *config.Config, optFns ...func(o *LogsOptions)) *LogsRecon {
opts := LogsOptions{
GroupNamePrefix: DefaultGroupNamePrefix,
StreamNamePrefix: DefaultStreamNamePrefix,
}
for _, fn := range optFns {
fn(&opts)
}
if opts.StartTime == 0 {
opts.StartTime = time.Now().Add(time.Hour * -24).UnixMilli()
}
if opts.EndTime == 0 {
opts.EndTime = math.MaxInt64
}
r := &LogsRecon{
cloudwatchlogsClient: cloudwatchlogs.NewFromConfig(cfg.AWSConfig),
engine: logmessage.NewEngine(opts.Verify),
opts: opts,
}
r.recon = newRecon[Log](func() {
r.runEnumerateServicePerRegion("cloudwatch", cfg.Regions, func(region string) {
r.enumerateLogsPerRegion(region)
})
}, func(o *reconOptions) {
o.BeforeHook = opts.BeforeHook
o.AfterRunHook = opts.AfterRunHook
})
return r
}
func (rec *LogsRecon) enumerateLogsPerRegion(region string) {
p := cloudwatchlogs.NewDescribeLogGroupsPaginator(rec.cloudwatchlogsClient, &cloudwatchlogs.DescribeLogGroupsInput{
LogGroupNamePrefix: aws.String(rec.opts.GroupNamePrefix),
})
for p.HasMorePages() {
page, err := p.NextPage(context.TODO(), func(o *cloudwatchlogs.Options) {
o.Region = region
})
if err != nil {
rec.addError(err)
return
}
for _, g := range page.LogGroups {
input := &cloudwatchlogs.FilterLogEventsInput{
LogGroupName: g.LogGroupName,
StartTime: aws.Int64(rec.opts.StartTime),
EndTime: aws.Int64(rec.opts.EndTime),
}
if rec.opts.StreamNamePrefix != "" {
input.LogStreamNamePrefix = aws.String(rec.opts.StreamNamePrefix)
}
if rec.opts.FilterPattern != "" {
input.FilterPattern = aws.String(rec.opts.FilterPattern)
}
pe := cloudwatchlogs.NewFilterLogEventsPaginator(rec.cloudwatchlogsClient, input)
for pe.HasMorePages() {
eventPage, err := pe.NextPage(context.TODO(), func(o *cloudwatchlogs.Options) {
o.Region = region
})
if err != nil {
rec.addError(err)
return
}
results := rec.engine.Scan(eventPage.Events)
encrypted := false
if g.KmsKeyId != nil {
encrypted = true
}
retention := "Never expire"
if g.RetentionInDays != nil {
retention = fmt.Sprintf("%d", aws.ToInt32(g.RetentionInDays))
}
for k, v := range results {
if v.Count == 0 {
continue
}
rec.addResult(Log{
AWSService: "Cloudwatch",
Region: region,
GroupName: aws.ToString(g.LogGroupName),
Encrypted: encrypted,
Retention: retention,
StreamName: k,
EventCount: v.Count,
Hints: v.Hints,
})
}
}
}
}
}