|
| 1 | +//go:build yara |
| 2 | +// +build yara |
| 3 | + |
| 4 | +/* |
| 5 | + * Copyright 2021-2022 by Nedim Sabic Sabic |
| 6 | + * https://www.fibratus.io |
| 7 | + * All Rights Reserved. |
| 8 | + * |
| 9 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 10 | + * you may not use this file except in compliance with the License. |
| 11 | + * You may obtain a copy of the License at |
| 12 | + * |
| 13 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 14 | + * |
| 15 | + * Unless required by applicable law or agreed to in writing, software |
| 16 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 17 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 18 | + * See the License for the specific language governing permissions and |
| 19 | + * limitations under the License. |
| 20 | + */ |
| 21 | + |
| 22 | +package functions |
| 23 | + |
| 24 | +import ( |
| 25 | + "fmt" |
| 26 | + "github.com/hillu/go-yara/v4" |
| 27 | + "github.com/rabbitstack/fibratus/pkg/util/multierror" |
| 28 | + log "github.com/sirupsen/logrus" |
| 29 | + "strings" |
| 30 | + "time" |
| 31 | +) |
| 32 | + |
| 33 | +// scanTimeout specifies the timeout interval for the scan operation |
| 34 | +const scanTimeout = time.Second * 10 |
| 35 | + |
| 36 | +// Yara provides signature-based detection in filters and rules. |
| 37 | +// YARA is a tool aimed at (but not limited to) helping malware |
| 38 | +// researchers to identify and classify malware samples. With YARA |
| 39 | +// you can create descriptions of malware families based on textual |
| 40 | +// or binary patterns. Depending on the parameter type supplied to this |
| 41 | +// function, the scan can be performed on the process, filename or a |
| 42 | +// memory block. |
| 43 | +type Yara struct{} |
| 44 | + |
| 45 | +func (f Yara) Call(args []interface{}) (interface{}, bool) { |
| 46 | + if len(args) < 2 { |
| 47 | + return false, false |
| 48 | + } |
| 49 | + var rules string |
| 50 | + var vars map[string]interface{} |
| 51 | + switch r := args[1].(type) { |
| 52 | + case string: |
| 53 | + rules = r |
| 54 | + case []string: |
| 55 | + rules = strings.Join(r, " ") |
| 56 | + } |
| 57 | + if len(args) > 3 { |
| 58 | + vars, _ = args[2].(map[string]interface{}) |
| 59 | + } |
| 60 | + scanner, err := f.newScanner(rules, vars) |
| 61 | + if err != nil { |
| 62 | + log.Warnf("erroneous scanner in YARA function: %v: %s", err, rules) |
| 63 | + return false, true |
| 64 | + } |
| 65 | + defer scanner.Destroy() |
| 66 | + |
| 67 | + var cb yara.MatchRules |
| 68 | + switch n := args[0].(type) { |
| 69 | + case uint32: // pid |
| 70 | + err = scanner.SetCallback(&cb).ScanProc(int(n)) |
| 71 | + case string: // file |
| 72 | + err = scanner.SetCallback(&cb).ScanFile(n) |
| 73 | + case []byte: // mem block |
| 74 | + err = scanner.SetCallback(&cb).ScanMem(n) |
| 75 | + default: // invalid type |
| 76 | + return false, false |
| 77 | + } |
| 78 | + if err != nil { |
| 79 | + log.Warnf("YARA function scan failed: %v", err) |
| 80 | + return false, true |
| 81 | + } |
| 82 | + if len(cb) > 0 { |
| 83 | + log.Debugf("YARA function produced %d match(es)", len(cb)) |
| 84 | + for _, match := range cb { |
| 85 | + log.Debugf("Matched YARA rule: %s", match.Rule) |
| 86 | + } |
| 87 | + } |
| 88 | + return len(cb) > 0, true |
| 89 | +} |
| 90 | + |
| 91 | +func (f Yara) Desc() FunctionDesc { |
| 92 | + desc := FunctionDesc{ |
| 93 | + Name: YaraFn, |
| 94 | + Args: []FunctionArgDesc{ |
| 95 | + {Keyword: "pid|file|bytes", Types: []ArgType{Field, Func, String, Number}, Required: true}, |
| 96 | + {Keyword: "rules", Types: []ArgType{Field, Func, String}, Required: true}, |
| 97 | + {Keyword: "vars", Types: []ArgType{Field, Func, String}}, |
| 98 | + }, |
| 99 | + } |
| 100 | + return desc |
| 101 | +} |
| 102 | + |
| 103 | +func (f Yara) Name() Fn { return YaraFn } |
| 104 | + |
| 105 | +func (f Yara) newScanner(rules string, vars map[string]interface{}) (*yara.Scanner, error) { |
| 106 | + c, err := yara.NewCompiler() |
| 107 | + if err != nil { |
| 108 | + return nil, err |
| 109 | + } |
| 110 | + defer c.Destroy() |
| 111 | + if err := c.AddString(rules, ""); err != nil { |
| 112 | + return nil, err |
| 113 | + } |
| 114 | + for k, v := range vars { |
| 115 | + if err := c.DefineVariable(k, v); err != nil { |
| 116 | + return nil, err |
| 117 | + } |
| 118 | + } |
| 119 | + if len(c.Errors) > 0 { |
| 120 | + return nil, parseCompilerErrors(c.Errors) |
| 121 | + } |
| 122 | + r, err := c.GetRules() |
| 123 | + if err != nil { |
| 124 | + return nil, err |
| 125 | + } |
| 126 | + scanner, err := yara.NewScanner(r) |
| 127 | + if err != nil { |
| 128 | + return nil, err |
| 129 | + } |
| 130 | + scanner.SetFlags(yara.ScanFlagsFastMode) |
| 131 | + scanner.SetTimeout(scanTimeout) |
| 132 | + return scanner, nil |
| 133 | +} |
| 134 | + |
| 135 | +func parseCompilerErrors(errors []yara.CompilerMessage) error { |
| 136 | + errs := make([]error, len(errors)) |
| 137 | + for i, err := range errors { |
| 138 | + errs[i] = fmt.Errorf("%s, line: %d", err.Text, err.Line) |
| 139 | + } |
| 140 | + return multierror.Wrap(errs...) |
| 141 | +} |
0 commit comments