-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
311 lines (272 loc) · 10.4 KB
/
main.go
File metadata and controls
311 lines (272 loc) · 10.4 KB
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
package main
import (
"encoding/json"
"flag"
"fmt"
"os"
"reflect"
"strconv"
"strings"
"sync"
"time"
"github.com/neo4j/neo4j-go-driver/v4/neo4j"
)
var config AutoGenerated
var badJumps []TierConnection
var wg sync.WaitGroup
func main() {
uri := flag.String("uri", "bolt://127.0.0.1:7687", "URL of neo4j server Default: bolt://127.0.0.1:7687")
username := flag.String("username", "neo4j", "Username for neo4j server Default: neo4j")
password := flag.String("password", "neo4j", "Password for neo4j server Default: neo4j")
filepath := flag.String("filepath", "./tier0.txt", "Path to file containing list of elements to add tier flag to")
flag.Parse()
if !strings.HasPrefix(*uri, "bolt://") {
*uri = "bolt://" + *uri
}
fmt.Println("Connecting to neo4j server at: ", *uri, " ...")
// Connect to the database
driver, err := neo4j.NewDriver(*uri, neo4j.BasicAuth(*username, *password, ""))
if err != nil {
panic(err)
}
defer driver.Close()
fmt.Println("Reading Config file...")
// Read the config file
configData, err := os.ReadFile("./config.json")
if err != nil {
panic(err)
}
fmt.Println("Parsing Config file...")
// Parse the Config file
err = json.Unmarshal(configData, &config)
if err != nil {
panic(err)
}
fmt.Println("Cleaning current tier0 flag...")
// clean current tier0
err = cleanCurrentTier0(driver)
if err != nil {
panic(err)
}
fmt.Println("Reading tier0 file...")
// Read Tier0 File
dat, err := os.ReadFile(*filepath)
if err != nil {
panic(err)
}
fmt.Println("Adding tier0 flag to elements...")
// Add Tier0 Flag to elements
for _, elementName := range strings.Split(string(dat), "\n") {
go addTierFlagToElement(driver, elementName)
}
wg.Wait()
fmt.Println("Analyzing all jumps...")
// Check all jumps
getAllJumps(driver)
wg.Wait()
fmt.Println("Writing bad jumps to file...")
// Print out bad jumps
t := strconv.FormatInt(int64(time.Now().Unix()), 10)
f, err := os.OpenFile("badConnections"+t+".csv", os.O_WRONLY|os.O_CREATE, 0600)
if err != nil {
panic(err)
}
defer f.Close()
if _, err = f.WriteString("Source, ConnectionType, Target, isACL\n"); err != nil {
panic(err)
}
for _, jump := range badJumps {
writeString := "" + jump.StartEntity.Props["name"].(string) + "," + jump.Relationship.Type + "," + jump.EndEntity.Props["name"].(string) + "," + strconv.FormatBool(jump.Relationship.Props["isacl"].(bool)) + "\n"
if _, err = f.WriteString(writeString); err != nil {
panic(err)
}
}
println("Bad Jumps:", len(badJumps))
}
func cleanCurrentTier0(driver neo4j.Driver) error {
session := driver.NewSession(neo4j.SessionConfig{AccessMode: neo4j.AccessModeWrite})
defer session.Close()
_, err := session.WriteTransaction(func(tx neo4j.Transaction) (interface{}, error) {
result, err := tx.Run("MATCH (n) WHERE n.tier0=true REMOVE n.tier0 RETURN n", map[string]interface{}{})
if err != nil {
return nil, err
}
result.Consume()
return nil, err
})
return err
}
func addTierFlagToElement(driver neo4j.Driver, elementName string) error {
wg.Add(1)
defer wg.Done()
if len(elementName) < 3 {
return nil
}
session := driver.NewSession(neo4j.SessionConfig{AccessMode: neo4j.AccessModeWrite})
defer session.Close()
_, err := session.WriteTransaction(func(tx neo4j.Transaction) (interface{}, error) {
searchString := ""
if strings.Contains(elementName, "@") {
searchString = "MATCH (n) WHERE toLower(n.name) = toLower($elementName) SET n.tier0=true RETURN n"
} else {
searchString = "MATCH (n) WHERE toLower(n.name) CONTAINS toLower($elementName) SET n.tier0=true RETURN n"
}
result, err := tx.Run(searchString, map[string]interface{}{"elementName": elementName})
if err != nil {
return nil, err
}
// how many nodes were affected?
affected, err := result.Consume()
if err != nil {
return nil, err
}
if affected.Counters().PropertiesSet() > 5 {
fmt.Printf("High false positive marked rate for %s with %d nodes affected\n", elementName, affected.Counters().PropertiesSet())
}
return nil, err
})
return err
}
func getAllJumps(driver neo4j.Driver) error {
session := driver.NewSession(neo4j.SessionConfig{AccessMode: neo4j.AccessModeRead})
defer session.Close()
_, err := session.WriteTransaction(func(tx neo4j.Transaction) (interface{}, error) {
result, err := tx.Run("MATCH (m WHERE m.tier0) -[r]- (x WHERE NOT EXISTS(x.tier0) AND EXISTS(x.domain) ) return m,r,x", map[string]interface{}{})
if err != nil {
return nil, err
}
for result.Next() {
record := result.Record()
tier0_device := record.GetByIndex(0).(neo4j.Node)
relationship := record.GetByIndex(1).(neo4j.Relationship)
other_device := record.GetByIndex(2).(neo4j.Node)
if relationship.EndId == other_device.Id {
go anaylseConnection(TierConnection{StartEntity: tier0_device, Relationship: relationship, EndEntity: other_device, IntoT0: false})
} else {
go anaylseConnection(TierConnection{StartEntity: other_device, Relationship: relationship, EndEntity: tier0_device, IntoT0: true})
}
}
return nil, err
})
return err
}
func anaylseConnection(connection TierConnection) {
defer func() {
if err := recover(); err != nil {
fmt.Println("panic occurred:", err)
}
}()
wg.Add(1)
defer wg.Done()
if connection.IntoT0 {
r := reflect.ValueOf(config.IntoT0)
f := reflect.Indirect(r).FieldByName(connection.Relationship.Type).Bool()
if f {
badJumps = append(badJumps, connection)
}
} else {
r := reflect.ValueOf(config.IntoT1)
f := reflect.Indirect(r).FieldByName(connection.Relationship.Type).Bool()
if f {
badJumps = append(badJumps, connection)
}
}
}
type TierConnection struct {
StartEntity neo4j.Node
Relationship neo4j.Relationship
EndEntity neo4j.Node
IntoT0 bool
}
type AutoGenerated struct {
IntoT0 struct {
AdminTo bool `json:"AdminTo"`
MemberOf bool `json:"MemberOf"`
HasSession bool `json:"HasSession"`
ForceChangePassword bool `json:"ForceChangePassword"`
AddMembers bool `json:"AddMembers"`
AddSelf bool `json:"AddSelf"`
CanRDP bool `json:"CanRDP"`
CanPSRemote bool `json:"CanPSRemote"`
ExecuteDCOM bool `json:"ExecuteDCOM"`
SQLAdmin bool `json:"SQLAdmin"`
AllowedToDelegate bool `json:"AllowedToDelegate"`
DCSync bool `json:"DCSync"`
GetChanges bool `json:"GetChanges"`
GetChangesAll bool `json:"GetChangesAll"`
GenericAll bool `json:"GenericAll"`
WriteDacl bool `json:"WriteDacl"`
GenericWrite bool `json:"GenericWrite"`
WriteOwner bool `json:"WriteOwner"`
WriteSPN bool `json:"WriteSPN"`
Owns bool `json:"Owns"`
AddKeyCredentialLink bool `json:"AddKeyCredentialLink"`
ReadLAPSPassword bool `json:"ReadLAPSPassword"`
ReadGMSAPassword bool `json:"ReadGMSAPassword"`
Contains bool `json:"Contains"`
AllExtendedRights bool `json:"AllExtendedRights"`
GPLink bool `json:"GPLink"`
AllowedToAct bool `json:"AllowedToAct"`
AddAllowedToAct bool `json:"AddAllowedToAct"`
TrustedBy bool `json:"TrustedBy"`
SyncLAPSPassword bool `json:"SyncLAPSPassword"`
AZAddMembers bool `json:"AZAddMembers"`
AZAppAdmin bool `json:"AZAppAdmin"`
AZCloudAppAdmin bool `json:"AZCloudAppAdmin"`
AZContains bool `json:"AZContains"`
AZContributor bool `json:"AZContributor"`
AZGetCertificates bool `json:"AZGetCertificates"`
AZGetKeys bool `json:"AZGetKeys"`
AZGetSecrets bool `json:"AZGetSecrets"`
AZGlobalAdmin bool `json:"AZGlobalAdmin"`
AZPrivilegedRoleAdmin bool `json:"AZPrivilegedRoleAdmin"`
AZResetPassword bool `json:"AZResetPassword"`
AZRunsAs bool `json:"AZRunsAs"`
AZUserAccessAdministrator bool `json:"AZUserAccessAdministrator"`
} `json:"IntoT0"`
IntoT1 struct {
AdminTo bool `json:"AdminTo"`
MemberOf bool `json:"MemberOf"`
HasSession bool `json:"HasSession"`
ForceChangePassword bool `json:"ForceChangePassword"`
AddMembers bool `json:"AddMembers"`
AddSelf bool `json:"AddSelf"`
CanRDP bool `json:"CanRDP"`
CanPSRemote bool `json:"CanPSRemote"`
ExecuteDCOM bool `json:"ExecuteDCOM"`
SQLAdmin bool `json:"SQLAdmin"`
AllowedToDelegate bool `json:"AllowedToDelegate"`
DCSync bool `json:"DCSync"`
GetChanges bool `json:"GetChanges"`
GetChangesAll bool `json:"GetChangesAll"`
GenericAll bool `json:"GenericAll"`
WriteDacl bool `json:"WriteDacl"`
GenericWrite bool `json:"GenericWrite"`
WriteOwner bool `json:"WriteOwner"`
WriteSPN bool `json:"WriteSPN"`
Owns bool `json:"Owns"`
AddKeyCredentialLink bool `json:"AddKeyCredentialLink"`
ReadLAPSPassword bool `json:"ReadLAPSPassword"`
ReadGMSAPassword bool `json:"ReadGMSAPassword"`
Contains bool `json:"Contains"`
AllExtendedRights bool `json:"AllExtendedRights"`
GPLink bool `json:"GPLink"`
AllowedToAct bool `json:"AllowedToAct"`
AddAllowedToAct bool `json:"AddAllowedToAct"`
TrustedBy bool `json:"TrustedBy"`
SyncLAPSPassword bool `json:"SyncLAPSPassword"`
AZAddMembers bool `json:"AZAddMembers"`
AZAppAdmin bool `json:"AZAppAdmin"`
AZCloudAppAdmin bool `json:"AZCloudAppAdmin"`
AZContains bool `json:"AZContains"`
AZContributor bool `json:"AZContributor"`
AZGetCertificates bool `json:"AZGetCertificates"`
AZGetKeys bool `json:"AZGetKeys"`
AZGetSecrets bool `json:"AZGetSecrets"`
AZGlobalAdmin bool `json:"AZGlobalAdmin"`
AZPrivilegedRoleAdmin bool `json:"AZPrivilegedRoleAdmin"`
AZResetPassword bool `json:"AZResetPassword"`
AZRunsAs bool `json:"AZRunsAs"`
AZUserAccessAdministrator bool `json:"AZUserAccessAdministrator"`
} `json:"IntoT1"`
}