| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "github.com/hillu/go-yara/v4" |
| 5 | |
| 6 | "bytes" |
| 7 | "flag" |
| 8 | "fmt" |
| 9 | "log" |
| 10 | "os" |
| 11 | "path/filepath" |
| 12 | "strconv" |
| 13 | "sync" |
| 14 | ) |
| 15 | |
| 16 | func printMatches(item string, m []yara.MatchRule, err error) { |
| 17 | if err != nil { |
| 18 | log.Printf("%s: error: %s", item, err) |
| 19 | return |
| 20 | } |
| 21 | if len(m) == 0 { |
| 22 | log.Printf("%s: no matches", item) |
| 23 | return |
| 24 | } |
| 25 | buf := &bytes.Buffer{} |
| 26 | fmt.Fprintf(buf, "%s: [", item) |
| 27 | for i, match := range m { |
| 28 | if i > 0 { |
| 29 | fmt.Fprint(buf, ", ") |
| 30 | } |
| 31 | fmt.Fprintf(buf, "%s:%s", match.Namespace, match.Rule) |
| 32 | } |
| 33 | fmt.Fprint(buf, "]") |
| 34 | log.Print(buf.String()) |
| 35 | } |
| 36 | |
| 37 | func main() { |
| 38 | var ( |
| 39 | rules rules |
| 40 | vars variables |
| 41 | processScan bool |
| 42 | pids []int |
| 43 | threads int |
| 44 | ) |
| 45 | flag.BoolVar(&processScan, "processes", false, "scan processes instead of files") |
| 46 | flag.Var(&rules, "rule", "add rules in source form: [namespace:]filename") |
| 47 | flag.Var(&vars, "define", "define variable referenced n ruleset") |
| 48 | flag.IntVar(&threads, "threads", 1, "use specified number of threads") |
| 49 | flag.Parse() |
| 50 | |
| 51 | if len(rules) == 0 { |
| 52 | flag.Usage() |
| 53 | log.Fatal("no rules specified") |
| 54 | } |
| 55 | |
| 56 | args := flag.Args() |
| 57 | if len(args) == 0 { |
| 58 | flag.Usage() |
| 59 | log.Fatal("no files or processes specified") |
| 60 | } |
| 61 | |
| 62 | if processScan { |
| 63 | for _, arg := range args { |
| 64 | if pid, err := strconv.Atoi(arg); err != nil { |
| 65 | log.Fatalf("Could not parse %s ad number", arg) |
| 66 | } else { |
| 67 | pids = append(pids, pid) |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | c, err := yara.NewCompiler() |
| 73 | if err != nil { |
| 74 | log.Fatalf("Failed to initialize YARA compiler: %s", err) |
| 75 | } |
| 76 | for id, value := range vars { |
| 77 | if err := c.DefineVariable(id, value); err != nil { |
| 78 | log.Fatal("failed to define variable '%s': %s", id, err) |
| 79 | } |
| 80 | } |
| 81 | for _, rule := range rules { |
| 82 | f, err := os.Open(rule.filename) |
| 83 | if err != nil { |
| 84 | log.Fatalf("Could not open rule file %s: %s", rule.filename, err) |
| 85 | } |
| 86 | err = c.AddFile(f, rule.namespace) |
| 87 | f.Close() |
| 88 | if err != nil { |
| 89 | log.Fatalf("Could not parse rule file %s: %s", rule.filename, err) |
| 90 | } |
| 91 | } |
| 92 | r, err := c.GetRules() |
| 93 | if err != nil { |
| 94 | log.Fatalf("Failed to compile rules: %s", err) |
| 95 | } |
| 96 | |
| 97 | wg := sync.WaitGroup{} |
| 98 | wg.Add(threads) |
| 99 | |
| 100 | if processScan { |
| 101 | c := make(chan int, threads) |
| 102 | for i := 0; i < threads; i++ { |
| 103 | s, _ := yara.NewScanner(r) |
| 104 | go func(c chan int, tid int) { |
| 105 | for pid := range c { |
| 106 | var m yara.MatchRules |
| 107 | log.Printf("<%02d> Scanning process %d...", tid, pid) |
| 108 | err := s.SetCallback(&m).ScanProc(pid) |
| 109 | printMatches(fmt.Sprintf("<pid %d", pid), m, err) |
| 110 | } |
| 111 | wg.Done() |
| 112 | }(c, i) |
| 113 | } |
| 114 | for _, pid := range pids { |
| 115 | c <- pid |
| 116 | } |
| 117 | close(c) |
| 118 | } else { |
| 119 | c := make(chan string, threads) |
| 120 | for i := 0; i < threads; i++ { |
| 121 | s, _ := yara.NewScanner(r) |
| 122 | go func(c chan string, tid int) { |
| 123 | for filename := range c { |
| 124 | var m yara.MatchRules |
| 125 | log.Printf("<%02d> Scanning file %s... ", tid, filename) |
| 126 | err := s.SetCallback(&m).ScanFile(filename) |
| 127 | printMatches(filename, m, err) |
| 128 | } |
| 129 | wg.Done() |
| 130 | }(c, i) |
| 131 | } |
| 132 | for _, path := range args { |
| 133 | if err := filepath.Walk(path, func(path string, info os.FileInfo, err error) error { |
| 134 | if info.Mode().IsRegular() { |
| 135 | c <- path |
| 136 | } else if info.Mode().IsDir() { |
| 137 | return nil |
| 138 | } else { |
| 139 | log.Printf("Sipping %s", path) |
| 140 | } |
| 141 | return nil |
| 142 | }); err != nil { |
| 143 | log.Printf("walk: %s: %s", path, err) |
| 144 | } |
| 145 | } |
| 146 | close(c) |
| 147 | } |
| 148 | wg.Wait() |
| 149 | } |