forked from Velocidex/velociraptor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreport_archive.go
182 lines (146 loc) · 5.11 KB
/
report_archive.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
// +build server_vql
package main
import (
"bytes"
"context"
"log"
"os"
"github.com/Velocidex/ordereddict"
errors "github.com/pkg/errors"
kingpin "gopkg.in/alecthomas/kingpin.v2"
artifacts_proto "www.velocidex.com/golang/velociraptor/artifacts/proto"
config_proto "www.velocidex.com/golang/velociraptor/config/proto"
"www.velocidex.com/golang/velociraptor/reporting"
"www.velocidex.com/golang/velociraptor/services"
vql_subsystem "www.velocidex.com/golang/velociraptor/vql"
"www.velocidex.com/golang/velociraptor/vql/tools"
)
var (
report_command_archive = report_command.Command("archive", "Generate a report on an existing archive.")
report_command_archive_report = report_command_archive.Flag(
"artifact", "An artifact that contains a report to generate (default Reporting.Default).").
Default("Reporting.Default").String()
report_command_archive_file = report_command_archive.Arg(
"archive", "A path to an archive file.").
Required().String()
report_command_archive_output = report_command_archive.Arg(
"output", "A path to an output file to write on.").
String()
)
func doReportArchive() {
config_obj, err := DefaultConfigLoader.
WithNullLoader().LoadAndValidate()
kingpin.FatalIfError(err, "Unable to load config file")
sm, err := startEssentialServices(config_obj)
kingpin.FatalIfError(err, "Starting services.")
defer sm.Close()
builder := services.ScopeBuilder{
Config: config_obj,
ACLManager: vql_subsystem.NullACLManager{},
Logger: log.New(&LogWriter{config_obj}, " ", 0),
Env: ordereddict.NewDict(),
}
manager, err := services.GetRepositoryManager()
kingpin.FatalIfError(err, "GetRepositoryManager")
scope := manager.BuildScopeFromScratch(builder)
defer scope.Close()
archive, err := reporting.NewArchiveReader(*report_command_archive_file)
kingpin.FatalIfError(err, "Unable to open archive file")
repository, err := getRepository(config_obj)
kingpin.FatalIfError(err, "Unable to load artifacts")
parts := []*ReportPart{}
main := ""
template := *report_command_archive_report
html_template_string, err := getHTMLTemplate(config_obj, template,
repository)
kingpin.FatalIfError(err, "Unable to load report %v", template)
for _, name := range archive.ListArtifacts() {
scope := manager.BuildScopeFromScratch(builder)
defer scope.Close()
// Reports can query the container directly.
scope.AppendPlugins(&tools.ArchiveSourcePlugin{
Archive: archive})
definition, pres := repository.Get(config_obj, name)
if !pres {
scope.Log("Artifact %v not found %v\n", name, err)
continue
}
content_writer := &bytes.Buffer{}
scope.Log("Rendering artifact %v\n", definition.Name)
for _, report := range definition.Reports {
if report.Type != "client" {
continue
}
// Do not sanitize_html since we are writing a
// stand along HTML file - artifacts may
// generate arbitrary HTML.
template_engine, err := reporting.NewHTMLTemplateEngine(
config_obj, context.Background(), scope,
vql_subsystem.NullACLManager{}, repository,
definition.Name, false /* sanitize_html */)
kingpin.FatalIfError(err, "Unable to render")
for _, param := range report.Parameters {
template_engine.SetEnv(param.Name, param.Default)
}
res, err := reporting.GenerateClientReport(
template_engine, "", "", nil)
kingpin.FatalIfError(err, "Unable to render")
content_writer.Write([]byte(res))
}
parts = append(parts, &ReportPart{
Artifact: definition, HTML: content_writer.String()})
main += content_writer.String()
}
// Reports can query the container directly.
scope.AppendPlugins(&tools.ArchiveSourcePlugin{
Archive: archive})
template_engine, err := reporting.NewHTMLTemplateEngine(
config_obj, context.Background(), scope,
vql_subsystem.NullACLManager{}, repository,
template, false /* sanitize_html */)
kingpin.FatalIfError(err, "Unable to render")
template_engine.SetEnv("main", main)
template_engine.SetEnv("parts", parts)
result, err := template_engine.RenderRaw(
html_template_string, template_engine.Env.ToDict())
kingpin.FatalIfError(err, "Unable to render")
writer := os.Stdout
if *report_command_archive_output != "" {
writer, err = os.OpenFile(
*report_command_archive_output,
os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
kingpin.FatalIfError(err, "Unable to open output file")
defer writer.Close()
}
_, err = writer.Write([]byte(result))
kingpin.FatalIfError(err, "Unable to write")
}
type ReportPart struct {
Artifact *artifacts_proto.Artifact
HTML string
}
func getHTMLTemplate(
config_obj *config_proto.Config,
name string, repository services.Repository) (string, error) {
template_artifact, ok := repository.Get(config_obj, name)
if !ok || len(template_artifact.Reports) == 0 {
return "", errors.New("Not found")
}
for _, report := range template_artifact.Reports {
if report.Type == "html" {
return report.Template, nil
}
}
return "", errors.New("Not found")
}
func init() {
command_handlers = append(command_handlers, func(command string) bool {
switch command {
case report_command_archive.FullCommand():
doReportArchive()
default:
return false
}
return true
})
}