Skip to content
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

Bugfix: yara_lint() should remove invalid imports from yara rules. #3987

Merged
merged 1 commit into from
Jan 1, 2025
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
3 changes: 0 additions & 3 deletions api/authenticators/oidc.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
api_utils "www.velocidex.com/golang/velociraptor/api/utils"
config_proto "www.velocidex.com/golang/velociraptor/config/proto"
"www.velocidex.com/golang/velociraptor/logging"
"www.velocidex.com/golang/velociraptor/utils"
)

type OIDCConnector interface {
Expand Down Expand Up @@ -143,8 +142,6 @@ func (self *OidcAuthenticator) oauthOidcLogin(
}
oidcOauthConfig.Endpoint = provider.Endpoint()

utils.Debug(oidcOauthConfig)

// Create oauthState cookie
oauthState, err := r.Cookie("oauthstate")
if err != nil {
Expand Down
9 changes: 7 additions & 2 deletions api/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -424,8 +424,13 @@ func getRows(
return nil, nil, nil, err
}

rs_reader, err := result_sets.NewResultSetReader(
file_store_factory, log_path)
options, err := tables.GetTableOptions(request)
if err != nil {
return nil, nil, nil, err
}

rs_reader, err := result_sets.NewResultSetReaderWithOptions(
ctx, config_obj, file_store_factory, log_path, options)
if err != nil {
return nil, nil, nil, err
}
Expand Down
19 changes: 19 additions & 0 deletions artifacts/testdata/server/testcases/yara_lint.in.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Parameters:
YaraRules: |
import "pe"

/* We dont support import "hash" so this import should be removed. */
import "hash"

rule Hive {
condition: pe.imphash() == "f34d5f2d4577ed6d9ceec516c1f5a744"
}

rule BadRule {
condition: hash.sha256(0, filesize) == "ab05"
}

Queries:
- LET Cleaned <= yara_lint(clean=TRUE, rules=YaraRules)
- SELECT Cleaned FROM scope()
- SELECT * FROM yara(rules=Cleaned, accessor="data", files="Hello")
5 changes: 5 additions & 0 deletions artifacts/testdata/server/testcases/yara_lint.out.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
LET Cleaned <= yara_lint(clean=TRUE, rules=YaraRules)[]SELECT Cleaned FROM scope()[
{
"Cleaned": "import \"pe\"\n\nrule Hive {\n condition:\n pe.imphash() == \"f34d5f2d4577ed6d9ceec516c1f5a744\"\n}\n"
}
]SELECT * FROM yara(rules=Cleaned, accessor="data", files="Hello")[]
15 changes: 14 additions & 1 deletion gui/velociraptor/src/components/core/paged-table.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -672,7 +672,10 @@ class VeloPagedTable extends Component {
if(_.isEmpty(transform) && !_.isEmpty(this.props.transform)) {
Object.assign(transform, this.props.transform);
}
return transform;
}

getTransformedRenderer = (transform)=>{
if(_.isEmpty(transform)) {
return <></>;
}
Expand Down Expand Up @@ -856,6 +859,16 @@ class VeloPagedTable extends Component {
downloads.columns = active_columns;
}

if(transformed.filter_column) {
downloads.filter_column = transformed.filter_column;
downloads.filter_regex = transformed.filter_regex;
}

if (transformed.sort_column) {
downloads.sort_column = transformed.sort_column;
downloads.sort_direction = transformed.sort_direction;
}

let all_compacted = true;
let none_compacted = true;

Expand Down Expand Up @@ -929,7 +942,7 @@ class VeloPagedTable extends Component {

</ButtonGroup>
<ButtonGroup className="float-right">
{ transformed }
{ this.getTransformedRenderer(transformed) }
</ButtonGroup>
{ this.props.toolbar || <></> }
</Navbar>
Expand Down
9 changes: 9 additions & 0 deletions magefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,15 @@ func Arm() error {
}.Run()
}

func Mips() error {
return Builder{
extra_tags: " release yara ",
goos: "linux",
disable_cgo: true,
arch: "mips",
}.Run()
}

// Builds a Development binary. This does not embed things like GUI
// resources to allow them to be loaded from the local directory.
func Dev() error {
Expand Down
2 changes: 1 addition & 1 deletion vql/common/yara.go
Original file line number Diff line number Diff line change
Expand Up @@ -711,7 +711,7 @@ func RuleGenerator(scope vfilter.Scope, rule string) string {
method += " " + kw

default:
scope.Log("Unknown shorthand directive %v", kw)
scope.Log("yara: Warning unknown shorthand directive %v - treating as Yara Rule", kw)
return rule
}
}
Expand Down
36 changes: 11 additions & 25 deletions vql/common/yara_rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,13 @@ var (
includedFunctions = map[string][]string{
"pe": []string{
"calculate_checksum",
"imphash",
"section_index",
"section_index",
"exports",
"exports",
"exports",
"exports_index",
"exports_index",
"exports_index",
"imports",
"imports",
"imports",
"imports",
"imports",
"imports",
"imports",
"imports",
"import_rva",
"import_rva",
"delayed_import_rva",
"delayed_import_rva",
"locale",
"language",
"is_dll",
Expand All @@ -52,27 +39,18 @@ var (
"math": {
"in_range",
"deviation",
"deviation",
"mean",
"mean",
"serial_correlation",
"serial_correlation",
"monte_carlo_pi",
"monte_carlo_pi",
"entropy",
"entropy",
"min",
"max",
"to_number",
"abs",
"count",
"count",
"percentage",
"percentage",
"mode",
"mode",
"to_string",
"to_string",
},
"elf": {
"telfhash",
Expand Down Expand Up @@ -528,7 +506,15 @@ func (self *RuleLinter) Lint() (*RuleLinter, []error) {
}
}

result.ruleset.Imports = self.ruleset.Imports
result.ruleset.Imports = nil

// Only include valid imports
for _, imp := range self.ruleset.Imports {
_, pres := includedFunctions[imp]
if pres {
result.ruleset.Imports = append(result.ruleset.Imports, imp)
}
}
return result, errors
}

Expand Down Expand Up @@ -618,7 +604,7 @@ func (self *YaraLintFunction) Call(ctx context.Context,
func (self *YaraLintFunction) Info(scope vfilter.Scope, type_map *vfilter.TypeMap) *vfilter.FunctionInfo {
return &vfilter.FunctionInfo{
Name: "yara_lint",
Doc: "Clean a set of yara rules. This removed invalid or unsupported rules.",
Doc: "Clean a set of yara rules. This removes invalid or unsupported rules.",
ArgType: type_map.AddType(scope, &YaraLintFunctionArgs{}),
Metadata: vql.VQLMetadata().Build(),
}
Expand Down
5 changes: 5 additions & 0 deletions vql/linux/ebpf/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/*
This is the ebpf plugin. It uses a modified version of tracee.
*/

package ebpf
3 changes: 2 additions & 1 deletion vql/linux/ebpf/ebpf.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//go:build linux
//go:build linux && (arm64 || amd64)
// +build linux
// +build arm64 amd64

package ebpf

Expand Down
3 changes: 2 additions & 1 deletion vql/linux/ebpf/logger.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//go:build linux
//go:build linux && (arm64 || amd64)
// +build linux
// +build arm64 amd64

package ebpf

Expand Down
3 changes: 2 additions & 1 deletion vql/linux/ebpf/profile.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//go:build linux
//go:build linux && (arm64 || amd64)
// +build linux
// +build arm64 amd64

package ebpf

Expand Down
4 changes: 4 additions & 0 deletions vql/server/flows/uploads.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ func (self UploadsPlugins) Call(
components = upload.Stats.Components
}

if len(components) > 0 {
components[len(components)-1] += upload.Type
}

vfs_path := path_specs.NewUnsafeFilestorePath(components...).
SetType(api.PATH_TYPE_FILESTORE_ANY)

Expand Down
Loading