This repository has been archived by the owner on Jun 26, 2021. It is now read-only.
/
carver.go
138 lines (123 loc) · 3.35 KB
/
carver.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
package filecarver
import (
"encoding/json"
"fmt"
"github.com/oktasecuritylabs/sgt/handlers/response"
"github.com/oktasecuritylabs/sgt/osquery_types"
"github.com/sirupsen/logrus"
"io/ioutil"
"net/http"
"time"
)
var log = logrus.New()
func init() {
log.Level = logrus.InfoLevel
log.Formatter = &logrus.JSONFormatter{}
}
type CarverDB interface {
CreateCarve(carveMap *osquery_types.Carve) error
AddCarveData(data *osquery_types.CarveData) error
}
func NewSessionID() string {
return RandString(15)
}
func StartCarve(db CarverDB) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
handleRequest := func() (interface{}, error) {
w.Header().Set("Content-Type", "application/json")
body, err := ioutil.ReadAll(r.Body)
defer r.Body.Close()
if err != nil {
return nil, fmt.Errorf("failed to read request body: %s", err)
}
//carveMap := osquery_types.CarveMap{}
carve := osquery_types.Carve{}
err = json.Unmarshal(body, &carve)
if err != nil {
log.Errorf("invalid carve post: \n%+v", body)
return nil, fmt.Errorf("Invalid carve post")
}
carve.SessionID = NewSessionID()
log.Infof("Carve map: %+v", carve)
err = db.CreateCarve(&carve)
if err != nil {
log.Errorf("failed to create carve: %s", err.Error())
return nil, fmt.Errorf("Failed to create carve")
}
//create first entry in DB carve table
type statusSuccess struct {
Success bool `json:"success"`
SessionID string `json:"session_id"`
}
ok := statusSuccess{
Success: true,
SessionID: carve.SessionID,
}
return ok, nil
}
ok, err := handleRequest()
if err != nil {
response.WriteError(w, err.Error())
} else {
response.WriteCustomJSON(w, ok)
}
})
}
func ContinueCarve(db CarverDB) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
handleRequest := func() (interface{}, error) {
w.Header().Set("Content-Type", "application/json")
body, err := ioutil.ReadAll(r.Body)
defer r.Body.Close()
if err != nil {
log.Errorf("Invalid continue data: %s", err.Error())
return nil, fmt.Errorf("Invalid continue data")
}
carveData := osquery_types.CarveData{}
err = json.Unmarshal(body, &carveData)
if err != nil {
log.Errorf("Invalid continue data: %s", err.Error())
return nil, fmt.Errorf("Invalid continue data")
}
log.Infof("block ID: %s", carveData.BlockID)
carveData.SessionBlockID = carveData.SetSBID()
carveData.TimeToLive = time.Now().UTC().Add(time.Hour * 4).Unix()
err = db.AddCarveData(&carveData)
if err != nil {
log.Errorf("AddCarveData: %s", (err.Error()))
return nil, fmt.Errorf("Failed to create carve")
}
//create first entry in DB carve table
type statusSuccess struct {
Success bool `json:"success"`
}
ok := statusSuccess{
Success: true,
}
return ok, nil
}
status, err := handleRequest()
if err != nil {
log.Error(err)
} else {
response.WriteCustomJSON(w, status)
}
})
}
func DummyHandler(db CarverDB) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
handleRequest := func() error {
body, err := ioutil.ReadAll(r.Body)
defer r.Body.Close()
if err != nil {
return err
}
fmt.Println(string(body))
return nil
}
err := handleRequest()
if err != nil {
log.Error(err)
}
})
}