-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
191 lines (170 loc) · 4.46 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
package main
import (
"compress/gzip"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
)
// Result contains results of querying on all events
type Result struct {
// Count int
EventName map[string]int
}
type Record struct {
AwsRegion string `json:"awsRegion"`
ErrorCode string `json:"errorCode"`
EventID string `json:"eventID"`
EventName string `json:"eventName"`
EventSource string `json:"eventSource"`
Resources []struct {
ARN string `json:"ARN"`
AccountID string `json:"accountId"`
Type string `json:"type"`
} `json:"resources"`
UserIdentity struct {
Type string `json:"type"`
UserName string `json:"userName"`
Arn string `json:"arn"`
SessionContext struct {
SessionIssuer struct {
Type string `json:"type"`
Arn string `json:"arn"`
}
}
} `json:"userIdentity"`
}
type Events struct {
Records []Record
}
func listFiles(dir string) []string {
cleanedList := make([]string, 0)
x, err := ioutil.ReadDir(dir)
if err != nil {
panic(err)
}
// Remove all files that ends not to json
for f := range x {
if x[f].IsDir() {
nestedDir := fmt.Sprintf("%v/%v", dir, x[f].Name())
cleanedList = append(cleanedList, listFiles(nestedDir)...)
} else if strings.HasSuffix(x[f].Name(), ".json") || strings.HasSuffix(x[f].Name(), ".json.gz") {
// TODO: Use regex
cleanedList = append(cleanedList, fmt.Sprintf("%v/%v", dir, x[f].Name()))
} else {
println("skipping " + x[f].Name())
}
}
return cleanedList
}
func parseFile(roleName string, eventType string, file string) []Record {
f, err := os.Open(filepath.Clean(file))
var fileContent []byte
if err != nil {
panic(err)
}
defer f.Close()
if strings.HasSuffix(f.Name(), ".gz") {
// Need to unpack it first
reader, err := gzip.NewReader(f)
if err != nil {
panic(err)
}
defer reader.Close()
fileContent, err = ioutil.ReadAll(reader)
if err != nil {
panic(err)
}
} else {
fileContent, err = ioutil.ReadAll(f)
if err != nil {
panic(err)
}
}
// TODO: Remove Events struct
var tmp Events
err = json.Unmarshal(fileContent, &tmp)
if err != nil {
panic(err)
}
var allRecords []Record
var cleanRecords []Record
allRecords = append(allRecords, tmp.Records...)
for i := 0; i < len(allRecords); i++ {
if eventType == "role" {
current := allRecords[i].UserIdentity.SessionContext.SessionIssuer
if current.Type == "Role" && strings.HasSuffix(current.Arn, roleName) {
cleanRecords = append(cleanRecords, allRecords[i])
}
currentSts := allRecords[i].Resources
if len(currentSts) != 0 {
if currentSts[0].Type == "AWS::IAM::Role" && strings.HasSuffix(currentSts[0].ARN, roleName) {
cleanRecords = append(cleanRecords, allRecords[i])
}
}
}
if eventType == "user" {
current := allRecords[i]
if current.UserIdentity.Type == "IAMUser" && strings.HasSuffix(current.UserIdentity.Arn, roleName) {
cleanRecords = append(cleanRecords, allRecords[i])
}
}
}
return cleanRecords
}
func main() {
var records []Record
allRequests := make(map[string]Result)
roleName := flag.String("roleName", "", "Role name for analyze")
userName := flag.String("userName", "", "User name for analyze")
dirName := flag.String("dirName", "", "Path for CloudTrail logs")
concurrency := flag.Int("concurrency", 20, "Number of concurrent threads")
flag.Parse()
if *roleName == "" && *userName == "" {
println("You must provide at least role or user name")
os.Exit(1)
}
if *dirName == "" {
println("You need provide path to logs")
os.Exit(1)
}
files := listFiles(*dirName)
sem := make(chan bool, *concurrency)
// TODO: counter for events is wrong for 10-20 events. Need to fix it
for _, v := range files {
sem <- true
go func() {
defer func() { <-sem }()
if *roleName != "" {
records = append(records, parseFile(*roleName, "role", v)...)
}
if *userName != "" {
records = append(records, parseFile(*userName, "user", v)...)
}
}()
}
for i := 0; i < cap(sem); i++ {
sem <- true
}
// println("Results:" + len(records))
for i := 0; i < len(records); i++ {
evSource := records[i].EventSource
evName := records[i].EventName
v, ok := allRequests[evSource]
if !ok {
v = Result{}
v.EventName = make(map[string]int)
}
v.EventName[evName]++
allRequests[evSource] = v
}
println("Result of scanning:")
for source, event := range allRequests {
for k, v := range event.EventName {
fmt.Printf("%v:%v - %v\n", source, k, v)
}
}
}