10000 HTML report generator may produce arbitrary html. by scudette · Pull Request #489 · Velocidex/velociraptor · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

HTML report generator may produce arbitrary html. #489

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion artifacts/definitions/Server/Utils/CreateCollector.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ sources:
)

LET definitions <= SELECT * FROM chain(
a = { SELECT name, parameters, sources, reports
a = { SELECT name, description, parameters, sources, reports
FROM artifact_definitions(names=Artifacts)
WHERE name =~ "^(Custom|Packs)\\." AND
log(message="Adding artifact_definition for " + name) },
Expand Down
3 changes: 2 additions & 1 deletion bin/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ func doHTMLReport() {
for _, artifact_name := range result.Context.Request.Artifacts {
template_engine, err := reporting.NewHTMLTemplateEngine(
config_obj, context.Background(), nil, /* default scope */
vql_subsystem.NullACLManager{}, repository, artifact_name)
vql_subsystem.NullACLManager{}, repository, artifact_name,
false /* sanitize_html */)
kingpin.FatalIfError(err, "Generating report")

template_engine.SetEnv("ClientId", *report_command_flow_client)
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ require (
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e // indirect
golang.org/x/sys v0.0.0-20200625212154-ddb9806d33ae
golang.org/x/text v0.3.3 // indirect
golang.org/x/tools v0.0.0-20200207131002-533eb2654509 // indirect
google.golang.org/api v0.11.0
google.golang.org/appengine v1.6.5 // indirect
Expand All @@ -119,7 +120,7 @@ require (
www.velocidex.com/golang/go-prefetch v0.0.0-20190703150313-0469fa2f85cf
www.velocidex.com/golang/oleparse v0.0.0-20190327031422-34195d413196
www.velocidex.com/golang/regparser v0.0.0-20190625082115-b02dc43c2500
www.velocidex.com/golang/vfilter v0.0.0-20200605101505-2a4beeea86e2
www.velocidex.com/golang/vfilter v0.0.0-20200711150936-76e1310efce7
www.velocidex.com/golang/vtypes v0.0.0-20180924145839-b0d509f8925b
)

Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,8 @@ golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/text v0.3.3 h1:cokOdA+Jmi5PJGXLlLllQSgYigAEfHXJAERHVMaCc2k=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
Expand Down Expand Up @@ -611,5 +613,7 @@ www.velocidex.com/golang/regparser v0.0.0-20190625082115-b02dc43c2500 h1:XqZddiA
www.velocidex.com/golang/regparser v0.0.0-20190625082115-b02dc43c2500/go.mod h1:DVzloLH8L+oF3zma1Jisaat5bGF+4VLggDcYlIp00ns=
www.velocidex.com/golang/vfilter v0.0.0-20200605101505-2a4beeea86e2 h1:iyKy7xjJL+71/kL5R4KrWJE6Keq++eULTTn9pTQc3SM=
www.velocidex.com/golang/vfilter v0.0.0-20200605101505-2a4beeea86e2/go.mod h1:mABF6rGkfq9qwvo2SppBxYonhnd8OPSA6rxzzl75A4Y=
www.velocidex.com/golang/vfilter v0.0.0-20200711150936-76e1310efce7 h1:0p7AX5nmluXkjJ3AEMN8YeqgMkYU/qiVXt325PBFwa4=
www.velocidex.com/golang/vfilter v0.0.0-20200711150936-76e1310efce7/go.mod h1:mABF6rGkfq9qwvo2SppBxYonhnd8OPSA6rxzzl75A4Y=
www.velocidex.com/golang/vtypes v0.0.0-20180924145839-b0d509f8925b h1:z5v5o1dhtzaxvlWm6qSTYZ4OTr56Ol2JpM1Y5Wu9zQE=
www.velocidex.com/golang/vtypes v0.0.0-20180924145839-b0d509f8925b/go.mod h1:tXxIx8UJuI81Hoxcv0DTq2a1Pi1H6l1uCf4dhqUSUkw=
44 changes: 38 additions & 6 deletions reporting/html.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ import (

type HTMLTemplateEngine struct {
*BaseTemplateEngine
tmpl *template.Template
ctx context.Context
log_writer *logWriter
Data map[string]*actions_proto.VQLResponse
tmpl *template.Template
ctx context.Context
log_writer *logWriter
Data map[string]*actions_proto.VQLResponse
SanitizeHTML bool
}

func (self *HTMLTemplateEngine) Expand(values ...interface{}) interface{} {
Expand Down Expand Up @@ -65,10 +66,35 @@ func (self *HTMLTemplateEngine) Table(values ...interface{}) interface{} {
default:
return t

case []*ordereddict.Dict:
columns := []string{}

result := "<table class=\"table table-striped\">\n"

for _, item := range t {
if len(columns) == 0 {
8000 columns = item.Keys()
result += " <tr>\n"
for _, name := range columns {
result += " <th>" + name + "</th>\n"
}
result += " </tr>\n"
}

result += " <tr>\n"
for _, name := range columns {
value, _ := item.Get(name)
result += fmt.Sprintf(" <td>%v</td>\n", value)
}
result += " </tr>\n"
}
result += "</table>\n"
return result

case chan *ordereddict.Dict:
columns := []string{}

result := "<table>\n"
result := "<table class=\"table table-striped\">\n"

for item := range t {
if len(columns) == 0 {
Expand Down Expand Up @@ -141,6 +167,10 @@ func (self *HTMLTemplateEngine) Execute(template_string string) (string, error)
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
}
Expand Down Expand Up @@ -213,7 +243,8 @@ func NewHTMLTemplateEngine(
scope *vfilter.Scope,
acl_manager vql_subsystem.ACLManager,
repository *artifacts.Repository,
artifact_name string) (
artifact_name string,
sanitize_html bool) (
*HTMLTemplateEngine, error) {

base_engine, err := newBaseTemplateEngine(
Expand All @@ -229,6 +260,7 @@ func NewHTMLTemplateEngine(
BaseTemplateEngine: base_engine,
ctx: ctx,
log_writer: log_writer,
SanitizeHTML: sanitize_html,
}
template_engine.tmpl = template.New("").Funcs(sprig.TxtFuncMap()).Funcs(
template.FuncMap{
Expand Down
4 changes: 2 additions & 2 deletions services/launcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func CompileCollectorArgs(
return nil, err
}

ensureToolsDeclared(ctx, config_obj, artifact)
EnsureToolsDeclared(ctx, config_obj, artifact)
}

// Add any artifact dependencies.
Expand Down Expand Up @@ -106,7 +106,7 @@ func getDependentTools(
}

// Make sure we know about tools the artifact itself defines.
func ensureToolsDeclared(
func EnsureToolsDeclared(
ctx context.Context, config_obj *config_proto.Config,
artifact *artifacts_proto.Artifact) error {

Expand Down
2 changes: 1 addition & 1 deletion vql/filesystem/filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ func (self ReadFileFunction) Info(scope *vfilter.Scope, type_map *vfilter.TypeMa
return &vfilter.FunctionInfo{
Name: "read_file",
Doc: "Read a file into a string.",
ArgType: type_map.AddType(scope, &ReadFileArgs{}),
ArgType: type_map.AddType(scope, &ReadFileFunctionArgs{}),
}
}

Expand Down
8 changes: 8 additions & 0 deletions vql/tools/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"www.velocidex.com/golang/velociraptor/config"
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/vfilter"
)
Expand Down Expand Up @@ -150,6 +151,13 @@ func (self CollectPlugin) Call(
continue

}

err = services.EnsureToolsDeclared(ctx, config_obj, artifact)
if err != nil {
scope.Log("collect: ", name)
continue
}

artifact_definitions = append(artifact_definitions, artifact)

request := &actions_proto.VQLCollectorArgs{}
Expand Down
22 changes: 18 additions & 4 deletions vql/tools/reporting.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ import (
"www.velocidex.com/golang/vfilter"
)

type ReportPart struct {
Artifact *artifacts_proto.Artifact
HTML string
}

func getHTMLTemplate(name string, repository *artifacts.Repository) (string, error) {
template_artifact, ok := repository.Get(name)
if !ok || len(template_artifact.Reports) == 0 {
Expand Down Expand Up @@ -60,17 +65,22 @@ func produceReport(
return err
}

content_writer := &bytes.Buffer{}
parts := []*ReportPart{}
main := ""
for _, definition := range definitions {
content_writer := &bytes.Buffer{}
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(), subscope,
vql_subsystem.NullACLManager{}, repository,
definition.Name)
definition.Name, false /* sanitize_html */)
if err != nil {
return err
}
Expand All @@ -87,17 +97,21 @@ func produceReport(

content_writer.Write([]byte(res))
}
parts = append(parts, &ReportPart{
Artifact: definition, HTML: content_writer.String()})
main += content_writer.String()
}

template_engine, err := reporting.NewHTMLTemplateEngine(
config_obj, context.Background(), subscope,
vql_subsystem.NullACLManager{}, repository,
template)
template, false /* sanitize_html */)
if err != nil {
return err
}

template_engine.SetEnv("main", content_writer.String())
template_engine.SetEnv("main", main)
template_engine.SetEnv("parts", parts)

result, err := template_engine.RenderRaw(
html_template_string, template_engine.Env.ToDict())
Expand Down
0