forked from Velocidex/velociraptor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhtml.go
364 lines (308 loc) · 8.62 KB
/
html.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
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
// This implements a template renderer for the GUI environment.
package reporting
import (
"bytes"
"context"
"fmt"
"html"
"log"
"strings"
"text/template"
"github.com/Depado/bfchroma"
"github.com/Masterminds/sprig/v3"
"github.com/Velocidex/ordereddict"
chroma_html "github.com/alecthomas/chroma/formatters/html"
blackfriday "github.com/russross/blackfriday/v2"
actions_proto "www.velocidex.com/golang/velociraptor/actions/proto"
artifacts_proto "www.velocidex.com/golang/velociraptor/artifacts/proto"
config_proto "www.velocidex.com/golang/velociraptor/config/proto"
"www.velocidex.com/golang/velociraptor/json"
"www.velocidex.com/golang/velociraptor/services"
vql_subsystem "www.velocidex.com/golang/velociraptor/vql"
"www.velocidex.com/golang/vfilter"
)
type HTMLTemplateEngine struct {
*BaseTemplateEngine
tmpl *template.Template
ctx context.Context
log_writer *logWriter
Data map[string]*actions_proto.VQLResponse
SanitizeHTML bool
}
func (self *HTMLTemplateEngine) Expand(values ...interface{}) interface{} {
_, argv := parseOptions(values)
// Not enough args.
if len(argv) != 1 {
return ""
}
results := []*ordereddict.Dict{}
switch t := argv[0].(type) {
default:
return t
case chan *ordereddict.Dict:
for item := range t {
results = append(results, item)
}
}
return results
}
func renderObject(value interface{}) string {
serialized := ""
switch t := value.(type) {
case vfilter.Null, *vfilter.Null:
return ""
case string:
return html.EscapeString(t)
case int, int64, int32, int16, int8,
uint64, uint32, uint16, uint8,
float64, float32, bool:
return fmt.Sprintf("%v", value)
default:
serialized = "<pre>" + json.StringIndent(value) + "</pre>"
}
return serialized
}
func (self *HTMLTemplateEngine) Table(values ...interface{}) interface{} {
_, argv := parseOptions(values)
// Not enough args.
if len(argv) != 1 {
return ""
}
switch t := argv[0].(type) {
default:
return t
case []*ordereddict.Dict:
columns := []string{}
result := "<div class=\"table\"><table class=\"table table-striped\"><thead>\n"
for _, item := range t {
if len(columns) == 0 {
columns = item.Keys()
result += " <tr>\n"
for _, name := range columns {
result += " <th>" + html.EscapeString(name) + "</th>\n"
}
result += " </tr>\n"
result += "</thead>\n<tbody>\n"
}
result += " <tr>\n"
for _, name := range columns {
value, _ := item.Get(name)
result += fmt.Sprintf(" <td>%v</td>\n", renderObject(value))
}
result += " </tr>\n"
}
result += "</tbody>\n/table></div>\n"
return result
case chan *ordereddict.Dict:
columns := []string{}
result := "<div class=\"table\"><table class=\"table table-striped\">\n<thead>\n"
for item := range t {
if len(columns) == 0 {
columns = item.Keys()
result += " <tr>\n"
for _, name := range columns {
result += " <th>" + html.EscapeString(name) + "</th>\n"
}
result += " </tr>\n"
result += "</thead>\n<tbody>\n"
}
result += " <tr>\n"
for _, name := range columns {
value, _ := item.Get(name)
result += fmt.Sprintf(" <td>%v</td>\n", renderObject(value))
}
result += " </tr>\n"
}
result += "</tbody>\n</table></div>\n"
return result
}
}
func (self *HTMLTemplateEngine) Noop(values ...interface{}) string {
return ""
}
func (self *HTMLTemplateEngine) RenderRaw(
template_string string, target interface{}) (string, error) {
tmpl, err := self.tmpl.Parse(SanitizeGoTemplates(template_string))
if err != nil {
return "", err
}
buffer := &bytes.Buffer{}
err = tmpl.Execute(buffer, target)
if err != nil {
return "", err
}
return buffer.String(), nil
}
func (self *HTMLTemplateEngine) Execute(report *artifacts_proto.Report) (string, error) {
tmpl, err := self.tmpl.Parse(SanitizeGoTemplates(report.Template))
if err != nil {
return "", err
}
buffer := &bytes.Buffer{}
err = tmpl.Execute(buffer, self.Artifact)
if err != nil {
self.Error("Template Erorr: %v", err)
return "", err
}
// We expect the template to be in markdown format, so now
// generate the HTML
output := blackfriday.Run(
buffer.Bytes(),
blackfriday.WithRenderer(bfchroma.NewRenderer(
bfchroma.ChromaOptions(
chroma_html.ClassPrefix("chroma"),
chroma_html.WithClasses(true),
chroma_html.WithLineNumbers(true)),
bfchroma.Style("github"),
)))
// Add classes to various tags
output_string := strings.ReplaceAll(string(output),
"<table>", "<table class=\"table table-striped\">")
if !self.SanitizeHTML {
return output_string, err
}
// Sanitize the HTML.
return bm_policy.Sanitize(output_string), nil
}
func (self *HTMLTemplateEngine) getMultiLineQuery(query string) (string, error) {
t := self.tmpl.Lookup(query)
if t == nil {
return query, nil
}
buf := &bytes.Buffer{}
err := t.Execute(buf, self.Artifact)
if err != nil {
return "", err
}
// html/template escapes its template but this
// is the wrong thing to do for us because we
// use the template as a work around for
// text/template actions not spanning multiple
// lines.
return html.UnescapeString(buf.String()), nil
}
func (self *HTMLTemplateEngine) Import(artifact, name string) interface{} {
definition, pres := self.BaseTemplateEngine.Repository.Get(
self.config_obj, artifact)
if !pres {
self.Error("Unknown artifact %v", artifact)
return ""
}
for _, report := range definition.Reports {
if report.Name == name {
// We parse the template for new definitions,
// we dont actually care about the output.
_, err := self.tmpl.Parse(SanitizeGoTemplates(report.Template))
if err != nil {
self.Error("Template Erorr: %v", err)
}
}
}
return ""
}
func (self *HTMLTemplateEngine) Markdown(values ...string) interface{} {
result := ""
for _, value := range values {
r, err := self.getMultiLineQuery(value)
if err != nil {
self.Error("Markdown error: %v", err)
continue
}
result += r
}
// We expect the template to be in markdown format, so now
// generate the HTML
output := blackfriday.Run(
[]byte(result),
blackfriday.WithRenderer(bfchroma.NewRenderer(
bfchroma.ChromaOptions(
chroma_html.ClassPrefix("chroma"),
chroma_html.WithClasses(true),
chroma_html.WithLineNumbers(true)),
bfchroma.Style("github"),
)))
// Add classes to various tags
output_string := strings.ReplaceAll(string(output),
"<table>", "<table class=\"table table-striped\">")
if !self.SanitizeHTML {
return output_string
}
// Sanitize the HTML.
return bm_policy.Sanitize(output_string)
}
func (self *HTMLTemplateEngine) Query(queries ...string) interface{} {
output_chan := make(chan *ordereddict.Dict)
go func() {
defer close(output_chan)
for _, multiquery := range queries {
query, err := self.getMultiLineQuery(multiquery)
if err != nil {
self.Error("VQL Error: %v", err)
return
}
multi_vql, err := vfilter.MultiParse(query)
if err != nil {
self.Error("VQL Error: %v", err)
return
}
ctx, cancel := context.WithCancel(self.ctx)
defer cancel()
for _, vql := range multi_vql {
for row := range vql.Eval(ctx, self.Scope) {
select {
case <-ctx.Done():
return
case output_chan <- vfilter.RowToDict(
ctx, self.Scope, row):
}
}
}
}
}()
return output_chan
}
func (self *HTMLTemplateEngine) Error(fmt_str string, argv ...interface{}) string {
self.Scope.Log(fmt_str, argv...)
return ""
}
func (self *HTMLTemplateEngine) Messages() []string {
return self.log_writer.Messages()
}
func NewHTMLTemplateEngine(
config_obj *config_proto.Config,
ctx context.Context,
scope vfilter.Scope,
acl_manager vql_subsystem.ACLManager,
repository services.Repository,
artifact_name string,
sanitize_html bool) (
*HTMLTemplateEngine, error) {
base_engine, err := newBaseTemplateEngine(
config_obj, scope, acl_manager, nil, repository,
artifact_name)
if err != nil {
return nil, err
}
log_writer := &logWriter{}
base_engine.Scope.SetLogger(log.New(log_writer, "", 0))
template_engine := &HTMLTemplateEngine{
BaseTemplateEngine: base_engine,
ctx: ctx,
log_writer: log_writer,
SanitizeHTML: sanitize_html,
}
template_engine.tmpl = template.New("").Funcs(sprig.TxtFuncMap()).Funcs(
template.FuncMap{
"Query": template_engine.Query,
"Markdown": template_engine.Markdown,
"Scope": template_engine.GetScope,
"Table": template_engine.Table,
"LineChart": template_engine.Noop,
"Timeline": template_engine.Noop,
"Get": template_engine.getFunction,
"Expand": template_engine.Expand,
"import": template_engine.Import,
"str": strval,
})
return template_engine, nil
}