-
Notifications
You must be signed in to change notification settings - Fork 75
/
dataextract.go
225 lines (197 loc) · 6.35 KB
/
dataextract.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
/*
Copyright 2019-present, Cruise LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package dataextract
import (
"encoding/json"
"fmt"
"io/ioutil"
"os/exec"
"path"
"regexp"
"strings"
"github.com/BurntSushi/toml"
"github.com/cruise-automation/fwanalyzer/pkg/analyzer"
"github.com/cruise-automation/fwanalyzer/pkg/fsparser"
"github.com/cruise-automation/fwanalyzer/pkg/util"
)
type dataType struct {
File string
Script string
ScriptOptions []string // options for script execution
RegEx string
Json string
Desc string
Name string // the name can be set directly otherwise the key will be used
}
type dataExtractType struct {
config map[string][]dataType
a analyzer.AnalyzerType
}
func New(config string, a analyzer.AnalyzerType) *dataExtractType {
type dataExtractListType struct {
DataExtract map[string]dataType
}
cfg := dataExtractType{a: a, config: make(map[string][]dataType)}
var dec dataExtractListType
_, err := toml.Decode(config, &dec)
if err != nil {
panic("can't read config data: " + err.Error())
}
// convert name based map to filename based map with an array of dataType
for name, item := range dec.DataExtract {
var items []dataType
if _, ok := cfg.config[item.File]; ok {
items = cfg.config[item.File]
}
if item.Name == "" {
// if the key ends with __[0-9] remove the suffix and use it as name
if name[len(name)-1] >= '0' && name[len(name)-1] <= '9' && strings.HasSuffix(name[:len(name)-1], "__") {
item.Name = name[:len(name)-3]
} else {
item.Name = name
}
}
items = append(items, item)
item.File = path.Clean(item.File)
cfg.config[item.File] = items
}
return &cfg
}
func (state *dataExtractType) Start() {}
func (state *dataExtractType) Finalize() string {
return ""
}
func (state *dataExtractType) Name() string {
return "DataExtract"
}
func (state *dataExtractType) CheckFile(fi *fsparser.FileInfo, filepath string) error {
if !fi.IsFile() {
return nil
}
fn := path.Join(filepath, fi.Name)
if _, ok := state.config[fn]; !ok {
return nil
}
items := state.config[fn]
// we record if the specific Name was already added with a non error value
nameFilled := make(map[string]bool)
for _, item := range items {
// Name already set?
if _, ok := nameFilled[item.Name]; ok {
continue
}
if fi.IsLink() {
state.a.AddData(item.Name, fmt.Sprintf("DataExtract ERROR: file is Link (extract data from actual file): %s : %s",
item.Name, item.Desc))
continue
}
if item.RegEx != "" {
reg, err := regexp.Compile(item.RegEx)
if err != nil {
state.a.AddData(item.Name, fmt.Sprintf("DataExtract ERROR: regex compile error: %s : %s %s",
item.RegEx, item.Name, item.Desc))
continue
}
tmpfn, err := state.a.FileGet(fn)
if err != nil {
state.a.AddData(item.Name, fmt.Sprintf("DataExtract ERROR: file read error, file get: %s : %s : %s",
err, item.Name, item.Desc))
continue
}
fdata, err := ioutil.ReadFile(tmpfn)
if err != nil {
state.a.AddData(item.Name, fmt.Sprintf("DataExtract ERROR: file read error, file read: %s : %s : %s",
err, item.Name, item.Desc))
continue
}
_ = state.a.RemoveFile(tmpfn)
res := reg.FindAllStringSubmatch(string(fdata), -1)
if len(res) < 1 {
state.a.AddData(item.Name, fmt.Sprintf("DataExtract ERROR: regex match error, regex: %s : %s : %s",
item.RegEx, item.Name, item.Desc))
} else {
// only one match
if len(res) == 1 && len(res[0]) == 2 {
state.a.AddData(item.Name, res[0][1])
nameFilled[item.Name] = true
} else if len(res) > 1 {
// multiple matches
data := []string{}
for _, i := range res {
if len(i) == 2 {
data = append(data, i[1])
}
}
// convert to JSON array
jdata, _ := json.Marshal(data)
state.a.AddData(item.Name, string(jdata))
nameFilled[item.Name] = true
} else {
state.a.AddData(item.Name, fmt.Sprintf("DataExtract ERROR: regex match error : %s : %s",
item.Name, item.Desc))
}
}
}
if item.Script != "" {
out, err := runScriptOnFile(state.a, item.Script, item.ScriptOptions, fi, fn)
if err != nil {
state.a.AddData(item.Name, fmt.Sprintf("DataExtract ERROR: script error: %s : %s : %s",
err, item.Name, item.Desc))
} else {
state.a.AddData(item.Name, out)
nameFilled[item.Name] = true
}
}
if item.Json != "" {
tmpfn, err := state.a.FileGet(fn)
if err != nil {
state.a.AddData(item.Name, fmt.Sprintf("DataExtract ERROR: file read error, file get: %s : %s : %s",
err, item.Name, item.Desc))
continue
}
fdata, err := ioutil.ReadFile(tmpfn)
if err != nil {
state.a.AddData(item.Name, fmt.Sprintf("DataExtract ERROR: file read error, file read: %s : %s : %s",
err, item.Name, item.Desc))
continue
}
_ = state.a.RemoveFile(tmpfn)
out, err := util.XtractJsonField(fdata, strings.Split(item.Json, "."))
if err != nil {
state.a.AddData(item.Name, fmt.Sprintf("DataExtract ERROR: JSON decode error: %s : %s : %s",
err, item.Name, item.Desc))
continue
}
state.a.AddData(item.Name, out)
nameFilled[item.Name] = true
}
}
return nil
}
// runScriptOnFile runs the provided script with the following parameters:
// <filename> <filename in filesystem> <uid> <gid> <mode> <selinux label - can be empty> -- scriptOptions[0] scriptOptions[1]
func runScriptOnFile(a analyzer.AnalyzerType, script string, scriptOptions []string, fi *fsparser.FileInfo, fpath string) (string, error) {
fname, err := a.FileGet(fpath)
if err != nil {
return "", err
}
options := []string{fname, fpath, fmt.Sprintf("%d", fi.Uid), fmt.Sprintf("%d", fi.Gid),
fmt.Sprintf("%o", fi.Mode), fi.SELinuxLabel}
if len(scriptOptions) > 0 {
options = append(options, "--")
options = append(options, scriptOptions...)
}
out, err := exec.Command(script, options...).Output()
_ = a.RemoveFile(fname)
return string(out), err
}