/
shared.go
189 lines (159 loc) · 5.42 KB
/
shared.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
package aws
import (
"fmt"
"log"
"os"
"runtime"
"strings"
"github.com/BishopFox/cloudfox/internal"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/sts"
"github.com/fatih/color"
)
var cyan = color.New(color.FgCyan).SprintFunc()
var red = color.New(color.FgRed).SprintFunc()
var yellow = color.New(color.FgRed).SprintFunc()
var blue = color.New(color.FgBlue).SprintFunc()
var magenta = color.New(color.FgMagenta).SprintFunc()
var green = color.New(color.FgGreen).SprintFunc()
var AWSRegions = []string{"us-east-1", "us-east-2", "us-west-1", "us-west-2", "af-south-1", "ap-east-1", "ap-south-1", "ap-northeast-3", "ap-northeast-2", "ap-southeast-1", "ap-southeast-2", "ap-northeast-1", "ca-central-1", "eu-central-1", "eu-west-1", "eu-west-2", "eu-south-1", "eu-west-3", "eu-north-1", "me-south-1", "sa-east-1"}
var sharedLogger = internal.TxtLogger()
func GetIamSimResult(SkipAdminCheck bool, roleArnPtr *string, iamSimulatorMod IamSimulatorModule, localAdminMap map[string]bool) (string, string) {
var adminRole, canRolePrivEsc string
canRolePrivEsc = "Skipping, no pmapper data"
if !SkipAdminCheck {
var isRoleAdminBool bool
// If we've seen the function before, skip the isRoleAdmin function and just pull the value from the localAdminMap
if aws.ToString(roleArnPtr) == "" {
return "", ""
}
roleArn := aws.ToString(roleArnPtr)
if val, ok := localAdminMap[roleArn]; ok {
if val {
// we've seen it before and it's an admin
adminRole = "YES"
} else {
// we've seen it before and it's NOT an admin
adminRole = "No"
}
} else {
isRoleAdminBool = isRoleAdmin(iamSimulatorMod, roleArnPtr)
if isRoleAdminBool {
adminRole = "YES"
localAdminMap[roleArn] = true
} else {
adminRole = "No"
localAdminMap[roleArn] = false
}
}
if isRoleAdminBool {
adminRole = "YES"
} else {
adminRole = "No"
}
} else {
adminRole = "Skipped"
canRolePrivEsc = "Skipped"
}
return adminRole, canRolePrivEsc
}
func isRoleAdmin(iamSimMod IamSimulatorModule, principal *string) bool {
adminCheckResult := iamSimMod.isPrincipalAnAdmin(principal)
if adminCheckResult {
return true
} else {
return false
}
}
func InitPmapperGraph(Caller sts.GetCallerIdentityOutput, AWSProfile string, Goroutines int, PmapperDataBasePath string) (PmapperModule, error) {
pmapperMod := PmapperModule{
Caller: Caller,
AWSProfile: AWSProfile,
Goroutines: Goroutines,
PmapperDataBasePath: PmapperDataBasePath,
}
err := pmapperMod.initPmapperGraph()
if err != nil {
return pmapperMod, err
}
return pmapperMod, nil
}
func generatePmapperDataBasePaths(accountId *string) (string, string) {
var edgesPath, nodesPath string
if runtime.GOOS == "darwin" {
dir, err := os.UserHomeDir()
if err != nil {
log.Fatal("Could not get homedir")
}
edgesPath = fmt.Sprintf(dir + "/Library/Application Support/com.nccgroup.principalmapper/" + aws.ToString(accountId) + "/graph/edges.json")
nodesPath = fmt.Sprintf(dir + "/Library/Application Support/com.nccgroup.principalmapper/" + aws.ToString(accountId) + "/graph/nodes.json")
} else if runtime.GOOS == "linux" || runtime.GOOS == "freebsd" || runtime.GOOS == "openbsd" {
xdg, ok := os.LookupEnv("XDG_DATA_HOME")
if ok {
edgesPath = fmt.Sprintf(xdg + aws.ToString(accountId) + "/graph/edges.json")
nodesPath = fmt.Sprintf(xdg + aws.ToString(accountId) + "/graph/nodes.json")
} else {
dir, err := os.UserHomeDir()
if err == nil {
edgesPath = fmt.Sprintf(dir + "/.local/share/principalmapper/" + aws.ToString(accountId) + "/graph/edges.json")
nodesPath = fmt.Sprintf(dir + "/.local/share/principalmapper/" + aws.ToString(accountId) + "/graph/nodes.json")
} else {
log.Fatal("Could not homedir.")
}
}
} else if runtime.GOOS == "windows" {
dir, err := os.UserConfigDir()
if err != nil {
log.Fatal("Could not get userconfigdir")
}
edgesPath = fmt.Sprintf(dir + "\\principalmapper" + aws.ToString(accountId) + "\\graph\\edges.json")
nodesPath = fmt.Sprintf(dir + "\\principalmapper" + aws.ToString(accountId) + "\\graph\\nodes.json")
}
return edgesPath, nodesPath
}
func pmapperIsRoleAdmin(pmapperMod PmapperModule, principal *string) bool {
return pmapperMod.DoesPrincipalHaveAdmin(aws.ToString(principal))
}
func pmapperHasPathToAdmin(pmapperMod PmapperModule, principal *string) bool {
return pmapperMod.DoesPrincipalHavePathToAdmin(aws.ToString(principal))
}
func GetPmapperResults(SkipAdminCheck bool, pmapperMod PmapperModule, roleArn *string) (string, string) {
var adminRole, canRolePrivEsc string
var isRoleAdminBool bool
var canPrivEscBool bool
if !SkipAdminCheck {
if aws.ToString(roleArn) == "" {
return "", ""
}
isRoleAdminBool = pmapperIsRoleAdmin(pmapperMod, roleArn)
canPrivEscBool = pmapperHasPathToAdmin(pmapperMod, roleArn)
if canPrivEscBool {
canRolePrivEsc = "YES"
} else {
canRolePrivEsc = "No"
}
if isRoleAdminBool {
adminRole = "YES"
} else {
adminRole = "No"
}
} else {
adminRole = "Skipped"
canRolePrivEsc = "Skipped"
}
return adminRole, canRolePrivEsc
}
// take an arn and return the resource name
func GetResourceNameFromArn(arn string) string {
parts := strings.Split(arn, "/")
resourceName := parts[len(parts)-1]
return resourceName
}
func removeStringFromSlice(slice []string, element string) []string {
for i, v := range slice {
if v == element {
return append(slice[:i], slice[i+1:]...)
}
}
return slice
}