-
Notifications
You must be signed in to change notification settings - Fork 4
/
upload.go
180 lines (155 loc) · 3.36 KB
/
upload.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
package main
import (
"bytes"
"context"
"encoding/json"
"io/ioutil"
"os"
"strings"
"sync"
"time"
"github.com/neo4j/neo4j-go-driver/v4/neo4j"
)
func parseFile(file string) (*bloodHoundRawData, error) {
jsonFile, err := os.Open(file)
if err != nil {
return nil, err
}
defer jsonFile.Close()
fileByte, err := ioutil.ReadAll(jsonFile)
if err != nil {
return nil, err
}
var bloodHoundData bloodHoundRawData
// remove UTF-8 BOM!!
fileByte = bytes.TrimPrefix(fileByte, []byte("\xef\xbb\xbf"))
if err := json.Unmarshal(fileByte, &bloodHoundData); err != nil {
return nil, err
}
return &bloodHoundData, nil
}
func processData(
ctx context.Context,
wc *sync.WaitGroup,
file string,
cypherChan chan<- map[string]*cypher,
deleteJsonFile bool,
) error {
defer wc.Done()
log.Debugf("processing file %s ... ", file)
data, err := parseFile(file)
if err != nil {
return err
}
batch := 10
defer cleanUp(data.Meta.Type, time.Now(), file, deleteJsonFile)
switch strings.ToLower(data.Meta.Type) {
case "computers":
slice := data.Computers
for i := 0; i < len(slice); i += batch {
j := i + batch
if j > len(slice) {
j = len(slice)
}
select {
case <-ctx.Done():
return nil
case cypherChan <- buildComputerCyphers(slice[i:j]):
}
}
case "users":
slice := data.Users
for i := 0; i < len(slice); i += batch {
j := i + batch
if j > len(slice) {
j = len(slice)
}
select {
case <-ctx.Done():
return nil
case cypherChan <- buildUserCyphers(slice[i:j]):
}
}
case "groups":
slice := data.Groups
for i := 0; i < len(slice); i += batch {
j := i + batch
if j > len(slice) {
j = len(slice)
}
select {
case <-ctx.Done():
return nil
case cypherChan <- buildGroupCyphers(slice[i:j]):
}
}
case "ous":
slice := data.OUs
for i := 0; i < len(slice); i += batch {
j := i + batch
if j > len(slice) {
j = len(slice)
}
select {
case <-ctx.Done():
return nil
case cypherChan <- buildOUCyphers(slice[i:j]):
}
}
case "gpos":
slice := data.Gpos
for i := 0; i < len(slice); i += batch {
j := i + batch
if j > len(slice) {
j = len(slice)
}
select {
case <-ctx.Done():
return nil
case cypherChan <- buildGPOCyphers(slice[i:j]):
}
}
case "domains":
slice := data.Domains
for i := 0; i < len(slice); i += batch {
j := i + batch
if j > len(slice) {
j = len(slice)
}
select {
case <-ctx.Done():
return nil
case cypherChan <- buildDomainCyphers(slice[i:j]):
}
}
}
return nil
}
func cleanUp(object string, start time.Time, file string, deleteJsonFile bool) {
log.Infof("finished uploading %s data in %.2f min", object, time.Since(start).Minutes())
if deleteJsonFile {
if err := os.Remove(file); err != nil {
log.Errorf("unable to delete %s err:%s", file, err)
}
}
}
func uploadData(wc *sync.WaitGroup, driver neo4j.Driver, cypherChan <-chan map[string]*cypher) error {
defer wc.Done()
session := driver.NewSession(neo4j.SessionConfig{
AccessMode: neo4j.AccessModeWrite,
})
defer session.Close()
timeout := 1 * time.Minute
for cyphers := range cypherChan {
for _, c := range cyphers {
if len(c.list) == 0 {
continue
}
_, err := session.Run(c.statement, map[string]interface{}{"list": c.list}, neo4j.WithTxTimeout(timeout))
if err != nil {
return err
}
}
}
return nil
}