[go: up one dir, main page]

Skip to content

Commit

Permalink
Add go report card and fix minor code issues
Browse files Browse the repository at this point in the history
Signed-off-by: Nikos Filippakis <aesmade@gmail.com>
  • Loading branch information
nikofil committed Aug 8, 2017
1 parent c79d617 commit 6034afc
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 28 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[![Build Status](https://travis-ci.org/mushorg/go-dpi.svg?branch=master)](https://travis-ci.org/mushorg/go-dpi)
[![Coverage Status](https://coveralls.io/repos/github/mushorg/go-dpi/badge.svg?branch=master)](https://coveralls.io/github/mushorg/go-dpi?branch=master)
[![](https://godoc.org/github.com/mushorg/go-dpi?status.svg)](https://godoc.org/github.com/mushorg/go-dpi)
[![Go Report Card](https://goreportcard.com/badge/github.com/mushorg/go-dpi)](https://goreportcard.com/report/github.com/mushorg/go-dpi)

# go-dpi

Expand Down
4 changes: 2 additions & 2 deletions modules/classifiers/classifiers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func TestClassifyFlow(t *testing.T) {
t.Error("ClassifyFlowAll didn't return one result")
}
if results[0] != result {
t.Errorf("ClassifyFlowAll returned a differnt result from Classify: %v", results[0])
t.Errorf("ClassifyFlowAll returned a different result from Classify: %v", results[0])
}
}

Expand Down Expand Up @@ -197,7 +197,7 @@ func TestConfigureModule(t *testing.T) {
func TestInitDestroy(t *testing.T) {
module := NewClassifierModule()
if err := module.Initialize(); err != nil {
t.Errorf("Initalize returned error: %v", err)
t.Errorf("Initialize returned error: %v", err)
}
if err := module.Destroy(); err != nil {
t.Errorf("Destroy returned error: %v", err)
Expand Down
56 changes: 31 additions & 25 deletions modules/classifiers/netbios.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,35 @@ import (
// NetBIOSClassifier struct
type NetBIOSClassifier struct{}

func checkTCPNetBIOS(payload []byte, packetsRest []*gopacket.Packet) bool {
if len(payload) < 8 {
return false
}
nbLen := int(binary.BigEndian.Uint16(payload[2:4]))
// check for session request
isSessRequest := payload[0] == 0x81 && payload[1] == 0
// check for space padding
names := bytes.Split(payload[4:], []byte{0})
namesHavePadding := len(names) == 3 && names[0][0] == ' ' && names[1][0] == ' '
return nbLen+4 == len(payload) && isSessRequest && namesHavePadding
}

func checkUDPNetBIOSWrapper(isFirstPktBroadcast *bool) func([]byte, []*gopacket.Packet) bool {
return func(payload []byte, packetsRest []*gopacket.Packet) bool {
// try to detect a name query
if len(payload) != 50 {
return false
}
// we only detect queries with one question
hasOneQuestion := bytes.Compare(payload[4:12], []byte{0, 1, 0, 0, 0, 0, 0, 0}) == 0
// check if the question is a broadcast packet
isBcastNQ := *isFirstPktBroadcast && payload[2] == 1 && payload[3] == 0x10
// check if the question is a stat query packet
isStatNQ := !(*isFirstPktBroadcast) && payload[2] == 0 && payload[3] == 0
return hasOneQuestion && (isBcastNQ || isStatNQ)
}
}

// HeuristicClassify for NetBIOSClassifier
func (classifier NetBIOSClassifier) HeuristicClassify(flow *types.Flow) bool {
var isFirstPktBroadcast bool
Expand All @@ -21,32 +50,9 @@ func (classifier NetBIOSClassifier) HeuristicClassify(flow *types.Flow) bool {
}
}
isNetbiosTCP := checkFirstPayload(flow.Packets, layers.LayerTypeTCP,
func(payload []byte, packetsRest []*gopacket.Packet) bool {
if len(payload) < 8 {
return false
}
nbLen := int(binary.BigEndian.Uint16(payload[2:4]))
// check for session request
isSessRequest := payload[0] == 0x81 && payload[1] == 0
// check for space padding
names := bytes.Split(payload[4:], []byte{0})
namesHavePadding := len(names) == 3 && names[0][0] == ' ' && names[1][0] == ' '
return nbLen+4 == len(payload) && isSessRequest && namesHavePadding
})
checkTCPNetBIOS)
isNetbiosUDP := checkFirstPayload(flow.Packets, layers.LayerTypeUDP,
func(payload []byte, packetsRest []*gopacket.Packet) bool {
// try to detect a name query
if len(payload) != 50 {
return false
}
// we only detect queries with one question
hasOneQuestion := bytes.Compare(payload[4:12], []byte{0, 1, 0, 0, 0, 0, 0, 0}) == 0
// check if the question is a broadcast packet
isBcastNQ := isFirstPktBroadcast && payload[2] == 1 && payload[3] == 0x10
// check if the question is a stat query packet
isStatNQ := !isFirstPktBroadcast && payload[2] == 0 && payload[3] == 0
return hasOneQuestion && (isBcastNQ || isStatNQ)
})
checkUDPNetBIOSWrapper(&isFirstPktBroadcast))
return isNetbiosTCP || isNetbiosUDP
}

Expand Down
2 changes: 1 addition & 1 deletion modules/wrappers/wrappers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func TestClassifyFlowInitialized(t *testing.T) {
t.Error("ClassifyFlowAll didn't return one result")
}
if results[0] != result {
t.Errorf("ClassifyFlowAll returned a differnt result from Classify: %v", results[0])
t.Errorf("ClassifyFlowAll returned a different result from Classify: %v", results[0])
}
module.Destroy()
if !initialized.destroyCalled {
Expand Down

0 comments on commit 6034afc

Please sign in to comment.