test.go 994 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package main
  2. import (
  3. yara "github.com/hillu/go-yara"
  4. "io/ioutil"
  5. "os"
  6. "fmt"
  7. )
  8. func main() {
  9. //rule := "rule test : tag1 { meta: author = \"Matt Blewitt\" strings: $a = \"abc\" fullword condition: $a }"
  10. rule := `rule test
  11. {
  12. meta:
  13. date = "2021-07-26"
  14. description = "this is a test"
  15. tag = "test"
  16. strings:
  17. $dev = "xiaomi" nocase
  18. condition:
  19. $dev
  20. }`
  21. compiler, err := yara.NewCompiler()
  22. if compiler == nil || err != nil {
  23. return
  24. }
  25. if err = compiler.AddString(rule, ""); err != nil {
  26. return
  27. }
  28. rules, err := compiler.GetRules()
  29. if err != nil {
  30. return
  31. }
  32. s, err := yara.NewScanner(rules)
  33. if err != nil {
  34. return
  35. }
  36. testFile, _ := ioutil.TempFile("", "TestFile")
  37. defer os.Remove(testFile.Name())
  38. testFile.Write([]byte("xiaomi10"))
  39. testFile.Close()
  40. var matchRules yara.MatchRules
  41. if err := s.SetCallback(&matchRules).ScanFile(testFile.Name()); err != nil {
  42. return
  43. } else if len(matchRules) != 1 {
  44. return
  45. }
  46. fmt.Printf("Matches: %+v", matchRules)
  47. }