forked from Velocidex/velociraptor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupload.go
248 lines (213 loc) · 6.53 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
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
package api
import (
"crypto/sha256"
"encoding/hex"
"fmt"
"io"
"net/http"
"path"
"www.velocidex.com/golang/velociraptor/acls"
"www.velocidex.com/golang/velociraptor/api/authenticators"
api_proto "www.velocidex.com/golang/velociraptor/api/proto"
artifacts_proto "www.velocidex.com/golang/velociraptor/artifacts/proto"
"www.velocidex.com/golang/velociraptor/json"
"www.velocidex.com/golang/velociraptor/logging"
"www.velocidex.com/golang/velociraptor/paths"
"www.velocidex.com/golang/velociraptor/services"
)
func toolUploadHandler() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
org_id := authenticators.GetOrgIdFromRequest(r)
org_manager, err := services.GetOrgManager()
if err != nil {
returnError(w, http.StatusUnauthorized, err.Error())
return
}
org_config_obj, err := org_manager.GetOrgConfig(org_id)
if err != nil {
returnError(w, http.StatusUnauthorized, err.Error())
return
}
// Check for acls
userinfo := GetUserInfo(r.Context(), org_config_obj)
permissions := acls.ARTIFACT_WRITER
perm, err := services.CheckAccess(org_config_obj, userinfo.Name, permissions)
if !perm || err != nil {
returnError(w, http.StatusUnauthorized,
"User is not allowed to upload tools.")
return
}
// Parse our multipart form, 10 << 20 specifies a maximum
// upload of 10 MB files.
err = r.ParseMultipartForm(10 << 25)
if err != nil {
returnError(w, http.StatusBadRequest, "Unsupported params")
return
}
defer r.MultipartForm.RemoveAll()
tool := &artifacts_proto.Tool{}
params, pres := r.Form["_params_"]
if !pres || len(params) != 1 {
returnError(w, http.StatusBadRequest, "Unsupported params")
return
}
err = json.Unmarshal([]byte(params[0]), tool)
if err != nil {
returnError(w, http.StatusBadRequest, "Unsupported params")
return
}
// FormFile returns the first file for the given key `myFile`
// it also returns the FileHeader so we can get the Filename,
// the Header and the size of the file
file, handler, err := r.FormFile("file")
if err != nil {
returnError(w, 403, fmt.Sprintf("Unsupported params: %v", err))
return
}
defer file.Close()
tool.Filename = path.Base(handler.Filename)
tool.ServeLocally = true
path_manager := paths.NewInventoryPathManager(org_config_obj, tool)
pathspec, file_store_factory, err := path_manager.Path()
if err != nil {
returnError(w, 404, err.Error())
}
writer, err := file_store_factory.WriteFile(pathspec)
if err != nil {
returnError(w, http.StatusInternalServerError,
fmt.Sprintf("Error: %v", err))
return
}
defer writer.Close()
err = writer.Truncate()
if err != nil {
returnError(w, http.StatusInternalServerError,
fmt.Sprintf("Error: %v", err))
return
}
sha_sum := sha256.New()
_, err = io.Copy(writer, io.TeeReader(file, sha_sum))
if err != nil {
returnError(w, http.StatusInternalServerError,
fmt.Sprintf("Error: %v", err))
return
}
tool.Hash = hex.EncodeToString(sha_sum.Sum(nil))
inventory, err := services.GetInventory(org_config_obj)
if err != nil {
returnError(w, http.StatusInternalServerError,
fmt.Sprintf("Error: %v", err))
return
}
ctx := r.Context()
err = inventory.AddTool(ctx, org_config_obj, tool,
services.ToolOptions{
AdminOverride: true,
})
if err != nil {
returnError(w, http.StatusInternalServerError,
fmt.Sprintf("Error: %v", err))
return
}
// Now materialize the tool
tool, err = inventory.GetToolInfo(
r.Context(), org_config_obj, tool.Name, tool.Version)
if err != nil {
returnError(w, http.StatusInternalServerError,
fmt.Sprintf("Error: %v", err))
return
}
serialized, _ := json.Marshal(tool)
_, err = w.Write(serialized)
if err != nil {
logger := logging.GetLogger(org_config_obj, &logging.GUIComponent)
logger.Error("toolUploadHandler: %v", err)
}
})
}
func formUploadHandler() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
org_id := authenticators.GetOrgIdFromRequest(r)
org_manager, err := services.GetOrgManager()
if err != nil {
returnError(w, http.StatusUnauthorized, err.Error())
return
}
org_config_obj, err := org_manager.GetOrgConfig(org_id)
if err != nil {
returnError(w, http.StatusUnauthorized, err.Error())
return
}
// Check for acls
userinfo := GetUserInfo(r.Context(), org_config_obj)
permissions := acls.COLLECT_CLIENT
perm, err := services.CheckAccess(org_config_obj, userinfo.Name, permissions)
if !perm || err != nil {
returnError(w, http.StatusUnauthorized,
"User is not allowed to upload files for forms.")
return
}
// Parse our multipart form, 10 << 20 specifies a maximum
// upload of 10 MB files.
err = r.ParseMultipartForm(10 << 20)
if err != nil {
returnError(w, http.StatusBadRequest, "Unsupported params")
return
}
defer r.MultipartForm.RemoveAll()
form_desc := &api_proto.FormUploadMetadata{}
params, pres := r.Form["_params_"]
if !pres || len(params) != 1 {
returnError(w, http.StatusBadRequest, "Unsupported params")
return
}
err = json.Unmarshal([]byte(params[0]), form_desc)
if err != nil {
returnError(w, http.StatusBadRequest, "Unsupported params")
return
}
// FormFile returns the first file for the given key `file`
// it also returns the FileHeader so we can get the Filename,
// the Header and the size of the file
file, handler, err := r.FormFile("file")
if err != nil {
returnError(w, 403, fmt.Sprintf("Unsupported params: %v", err))
return
}
defer file.Close()
form_desc.Filename = path.Base(handler.Filename)
path_manager := paths.NewFormUploadPathManager(
org_config_obj, form_desc.Filename)
pathspec, file_store_factory, err := path_manager.Path()
if err != nil {
returnError(w, 403, fmt.Sprintf("Error: %v", err))
return
}
form_desc.Url = path_manager.URL()
writer, err := file_store_factory.WriteFile(pathspec)
if err != nil {
returnError(w, http.StatusInternalServerError,
fmt.Sprintf("Error: %v", err))
return
}
defer writer.Close()
err = writer.Truncate()
if err != nil {
returnError(w, http.StatusInternalServerError,
fmt.Sprintf("Error: %v", err))
return
}
_, err = io.Copy(writer, file)
if err != nil {
returnError(w, http.StatusInternalServerError,
fmt.Sprintf("Error: %v", err))
return
}
serialized, _ := json.Marshal(form_desc)
_, err = w.Write(serialized)
if err != nil {
logger := logging.GetLogger(org_config_obj, &logging.GUIComponent)
logger.Error("toolUploadHandler: %v", err)
}
})
}