Skip to content

Commit 708fd1f

Browse files
committed
feat: Mole uses Yara 4.x
2 parents 21ee6d1 + 1c342d5 commit 708fd1f

File tree

12 files changed

+232
-64
lines changed

12 files changed

+232
-64
lines changed

CHANGELOG.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,5 @@
1919
### Features
2020
- Added mole logger in engine package (0fc40f9)
2121

22-
[Unreleased]: https://github.com/mole-ids/mole/compare/v0.1.2...HEAD
23-
[v0.1.2]: https://github.com/mole-ids/mole/compare/v0.1.1...v0.1.2
22+
[Unreleased]: https://github.com/mole-ids/mole/compare/v0.1.1...HEAD
2423
[v0.1.1]: https://github.com/mole-ids/mole/compare/v0.1.0...v0.1.1

docs/content/getting-started/install-mole.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ box. However, the Mole IDS team is working to port Mole to the major platforms.
99

1010
!!! note "Mole IDS Dependencies & Requirements"
1111
Mole IDS is build upon two libraries and they have to be installed on the
12-
system you want to run Mole IDS.
12+
system you want to run Mole IDS.
1313

1414
* [Yara](https://virustotal.github.io/yara/)
1515
* [PF_RING](https://www.ntop.org/products/packet-capture/pf_ring/)
@@ -85,13 +85,12 @@ echo pf_ring | sudo tee -a /etc/modules
8585

8686
### Install Yara
8787

88-
At the moment Mole IDS uses Yara version 3.11.0. We know there is a newer version
89-
of Yara and we will added asoon as possible.
88+
Mole IDS uses the latest Yara version avaliable at the moment, which is Yara v4.0.2.
9089

9190
```shell
92-
wget https://github.com/VirusTotal/yara/archive/v3.11.0.tar.gz -O yara.tgz
91+
wget https://github.com/VirusTotal/yara/archive/4.0.2.tar.gz -O yara.tgz
9392
tar xvfz yara.tgz
94-
cd yara-3.11.0
93+
cd yara-4.0.2
9594
./bootstrap.sh
9695
./configure --enable-magic
9796
make

docs/content/writing-rules.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Writting rules
22

33
Mole IDS rule system is built on top of yara. You can find information about
4-
[writting basic yara rules](https://yara.readthedocs.io/en/v3.11.0/writingrules.html)
4+
[writting basic yara rules](https://yara.readthedocs.io/en/stable/writingrules.html)
55

66
## Syntax
77

go.mod

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,14 @@ module github.com/mole-ids/mole
33
go 1.14
44

55
require (
6-
github.com/google/gopacket v1.1.17
7-
github.com/hillu/go-yara v1.2.2
8-
github.com/k0kubun/pp v3.0.1+incompatible
9-
github.com/mattn/go-colorable v0.1.6 // indirect
6+
github.com/google/gopacket v1.1.18
7+
github.com/hillu/go-yara/v4 v4.0.2
108
github.com/oklog/ulid v1.3.1
119
github.com/pkg/errors v0.9.1
1210
github.com/spf13/cobra v1.0.0
1311
github.com/spf13/pflag v1.0.5 // indirect
14-
github.com/spf13/viper v1.6.3
15-
go.uber.org/zap v1.14.1
16-
golang.org/x/sys v0.0.0-20200420163511-1957bb5e6d1f // indirect
17-
golang.org/x/tools v0.0.0-20200425043458-8463f397d07c // indirect
12+
github.com/spf13/viper v1.7.0
13+
go.uber.org/zap v1.15.0
14+
golang.org/x/sys v0.0.0-20200728102440-3e129f6d46b1 // indirect
15+
golang.org/x/tools v0.0.0-20200731060945-b5fad4ed8dd6 // indirect
1816
)

go.sum

Lines changed: 160 additions & 40 deletions
Large diffs are not rendered by default.

internal/tree/tree.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
package tree
1515

1616
import (
17-
"github.com/hillu/go-yara"
17+
"github.com/hillu/go-yara/v4"
1818
"github.com/pkg/errors"
1919

2020
"github.com/mole-ids/mole/internal/nodes"

internal/types/types.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
package types
1515

1616
import (
17-
"github.com/hillu/go-yara"
17+
"github.com/hillu/go-yara/v4"
1818
"github.com/mole-ids/mole/internal/nodes"
1919
)
2020

pkg/engine/engine.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818

1919
"github.com/google/gopacket"
2020
"github.com/google/gopacket/layers"
21+
"github.com/hillu/go-yara/v4"
2122
"github.com/pkg/errors"
2223
"go.uber.org/zap"
2324

@@ -172,7 +173,10 @@ func (motor *Engine) checkAndFire(pe *PacketExtractor) {
172173

173174
for _, matchID := range matches {
174175
if scanner, found := motor.RuleMap[matchID]; found {
175-
matches, err := scanner.ScanMem(pe.GetPacketPayload())
176+
var matches yara.MatchRules
177+
scanner = scanner.SetCallback(&matches)
178+
179+
err := scanner.ScanMem(pe.GetPacketPayload())
176180
if err != nil {
177181
logger.Log.Errorf(ScannerScanMemFaildMsg, err.Error())
178182
return
@@ -185,7 +189,12 @@ func (motor *Engine) checkAndFire(pe *PacketExtractor) {
185189
event.Timestamp = &models.MoleTime{
186190
Time: metadata.Timestamp,
187191
}
188-
event.EventType = match.Meta["type"].(string)
192+
typ, ok := extractMeta(match.Metas, "type").(string)
193+
if !ok {
194+
event.EventType = "unkown"
195+
} else {
196+
event.EventType = typ
197+
}
189198
event.InIface = pe.GetIfaceName()
190199
event.Proto = meta[nodes.Proto.String()].GetValue()
191200
event.SrcIP = meta[nodes.SrcNet.String()].GetValue()
@@ -196,7 +205,7 @@ func (motor *Engine) checkAndFire(pe *PacketExtractor) {
196205
event.Alert = models.AlertEvent{
197206
Name: match.Rule,
198207
Tags: match.Tags,
199-
Meta: match.Meta,
208+
Meta: toMoleMetaMap(match.Metas),
200209
}
201210

202211
var matchArr models.MatchArray
@@ -215,3 +224,7 @@ func (motor *Engine) checkAndFire(pe *PacketExtractor) {
215224
}
216225
}
217226
}
227+
228+
func (motor *Engine) ruleMatching(m []yara.MatchRule, err error) {
229+
230+
}

pkg/engine/helper.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@
1313
// limitations under the License.
1414
package engine
1515

16+
import (
17+
"github.com/hillu/go-yara/v4"
18+
"github.com/mole-ids/mole/pkg/logger/models"
19+
)
20+
1621
// inProtos checks `pkgProto` exists in `protos`
1722
func inProtos(proto string, protos []string) bool {
1823
for _, p := range protos {
@@ -22,3 +27,22 @@ func inProtos(proto string, protos []string) bool {
2227
}
2328
return false
2429
}
30+
31+
func extractMeta(metas []yara.Meta, key string) interface{} {
32+
for _, meta := range metas {
33+
if meta.Identifier == key {
34+
return meta.Value
35+
}
36+
}
37+
return nil
38+
}
39+
40+
func toMoleMetaMap(metas []yara.Meta) models.MetaMap {
41+
var obj models.MetaMap
42+
obj = make(models.MetaMap)
43+
44+
for _, meta := range metas {
45+
obj[meta.Identifier] = meta.Value
46+
}
47+
return obj
48+
}

pkg/rules/helpers.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import (
1919
"regexp"
2020
"strings"
2121

22-
"github.com/hillu/go-yara"
22+
"github.com/hillu/go-yara/v4"
2323
"github.com/mole-ids/mole/internal/nodes"
2424
"github.com/mole-ids/mole/internal/types"
2525
"github.com/mole-ids/mole/internal/utils"
@@ -29,7 +29,7 @@ import (
2929
// GetRuleMetaInfo returns the rule metadata
3030
func GetRuleMetaInfo(rule yara.Rule) (metarule types.MetaRule, err error) {
3131
metarule = make(types.MetaRule)
32-
for _, meta := range rule.MetaList() {
32+
for _, meta := range rule.Metas() {
3333
if utils.InStrings(meta.Identifier, nodes.Keywords) {
3434
// This will never generate an error becauses meta.Identifieris double
3535
// checked in the previous conditional

pkg/rules/helpers_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import (
2121
"strings"
2222
"testing"
2323

24-
"github.com/hillu/go-yara"
24+
"github.com/hillu/go-yara/v4"
2525
"github.com/mole-ids/mole/internal/nodes"
2626
"github.com/mole-ids/mole/internal/types"
2727
"github.com/spf13/viper"

test_rules/t1.yar

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
rule Test1 : Foo Bar {
2+
meta:
3+
author = "Mole-IDS"
4+
type = "alert"
5+
uuid = "<not used>"
6+
proto = "tcp"
7+
src = "any"
8+
sport = "80"
9+
dst = "any"
10+
dport = "any"
11+
strings:
12+
$method = "GET"
13+
condition:
14+
$method at 0
15+
}

0 commit comments

Comments
 (0)