Skip to content
This repository has been archived by the owner on Feb 13, 2021. It is now read-only.

Commit

Permalink
Merge pull request #40 from dmitris/golint
Browse files Browse the repository at this point in the history
fix some golint issues
  • Loading branch information
dmitris authored Oct 22, 2020
2 parents f93b81d + 52a5fe2 commit ad2188a
Show file tree
Hide file tree
Showing 11 changed files with 71 additions and 19 deletions.
8 changes: 0 additions & 8 deletions .travis.yml

This file was deleted.

6 changes: 4 additions & 2 deletions data/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func (m *MemoryStore) Set(key string, value interface{}) bool {
return true
}

// Get retrieve the value pointed by the key.
// Get retrieves the value pointed by the key.
func (m *MemoryStore) Get(key string) (value interface{}, ok bool) {
value, ok = m.heap[key]
switch value.(type) {
Expand All @@ -49,6 +49,8 @@ func (m *MemoryStore) IncrBy(key string, delta int64) (newVal int64) {

}

// DelPrefix deletes records from the MemoryStore's heap
// when the keys match the given prefix.
func (m *MemoryStore) DelPrefix(prefix string) {
for k := range m.heap {
if strings.HasPrefix(k, prefix) {
Expand All @@ -57,7 +59,7 @@ func (m *MemoryStore) DelPrefix(prefix string) {
}
}

// Dummy method
// Publish is a dummy no-op method.
func (m *MemoryStore) Publish(k string, d interface{}) {

}
Expand Down
4 changes: 2 additions & 2 deletions fuzzer/arachni/arachni.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ import (
"github.com/yahoo/gryffin"
)

// Fuzzer is the handle for the fuzzing methods.
type Fuzzer struct{}

// Fuzz runs an Arachni scan.
func (s *Fuzzer) Fuzz(g *gryffin.Scan) (count int, err error) {

var cookies []string
// for _, c := range g.CookieJar.Cookies(g.Request.URL) {
for _, c := range g.Cookies {
Expand Down Expand Up @@ -63,7 +64,6 @@ func (s *Fuzzer) Fuzz(g *gryffin.Scan) (count int, err error) {
}

func (s *Fuzzer) extract(g *gryffin.Scan, output string) (count int) {

for _, l := range strings.Split(output, "\n") {
l = strings.TrimSpace(l)
switch {
Expand Down
2 changes: 2 additions & 0 deletions fuzzer/dummy/dummy.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ import (
"github.com/yahoo/gryffin"
)

// Fuzzer is the handle for the fuzzing methods.
type Fuzzer struct{}

// Fuzz runs a dummy scan.
func (s *Fuzzer) Fuzz(g *gryffin.Scan) (count int, err error) {

cmd := exec.Command("echo", g.Request.URL.Host)
Expand Down
2 changes: 2 additions & 0 deletions fuzzer/sqlmap/sqlmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ import (
"github.com/yahoo/gryffin"
)

// Fuzzer is the handle for the fuzzing methods.
type Fuzzer struct{}

// Fuzz runs an sqlmap scan.
func (s *Fuzzer) Fuzz(g *gryffin.Scan) (count int, err error) {

var cookies []string
Expand Down
17 changes: 14 additions & 3 deletions global.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,27 @@ package gryffin

import (
"io"
// "io/ioutil"
"sync"
)

var memoryStore *GryffinStore
var logWriter io.Writer
var (
memoryStore *GryffinStore
logWriter io.Writer
memoryStoreMu sync.Mutex
logWriterMu sync.Mutex
)

// SetMemoryStore sets the package internal global variable
// for the memory store.
func SetMemoryStore(m *GryffinStore) {
memoryStoreMu.Lock()
memoryStore = m
memoryStoreMu.Unlock()
}

// SetLogWriter sets the log writer.
func SetLogWriter(w io.Writer) {
logWriterMu.Lock()
logWriter = w
logWriterMu.Unlock()
}
13 changes: 11 additions & 2 deletions gryffin.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ type Renderer interface {
GetLinks() <-chan *Scan
}

// LogMessage contains the data fields to be marshall as a json for forwarding to the log processor.
// LogMessage contains the data fields to be marshalled as JSON for forwarding to the log processor.
type LogMessage struct {
Service string
Msg string
Expand All @@ -84,9 +84,11 @@ type LogMessage struct {
// NewScan creates a scan.
func NewScan(method, url, post string) *Scan {
// ensure we got a memory store..
memoryStoreMu.Lock()
if memoryStore == nil {
memoryStore = NewGryffinStore()
}
memoryStoreMu.Unlock()

id := GenRandomID()

Expand Down Expand Up @@ -352,7 +354,7 @@ func (s *Scan) IsDuplicatedPage() bool {
return true
}

// Scan runs the vulnerability fuzzer, return the issue count
// Fuzz runs the vulnerability fuzzer, return the issue count.
func (s *Scan) Fuzz(fuzzer Fuzzer) (int, error) {
c, err := fuzzer.Fuzz(s)
return c, err
Expand All @@ -379,11 +381,13 @@ func (s *Scan) ShouldCrawl() bool {

// TODO - LogFmt (fmt string)
// TODO - LogI (interface)
// Error logs the error for the given service.
func (s *Scan) Error(service string, err error) {
errmsg := fmt.Sprint(err)
s.Logm(service, errmsg)
}

// Logmf logs the message for the given service.
func (s *Scan) Logmf(service, format string, a ...interface{}) {
s.Logm(service, fmt.Sprintf(format, a...))
}
Expand All @@ -402,15 +406,20 @@ func (s *Scan) Logm(service, msg string) {
s.Log(m)
}

// Logf logs using the given format string.
func (s *Scan) Logf(format string, a ...interface{}) {
str := fmt.Sprintf(format, a...)
s.Log(str)
}

// Log encodes the given argument as JSON and writes it to
// the log writer.
func (s *Scan) Log(v interface{}) {
if logWriter == nil {
return
}
logWriterMu.Lock()
encoder := json.NewEncoder(logWriter)
encoder.Encode(v)
logWriterMu.Unlock()
}
10 changes: 9 additions & 1 deletion html-distance/bktree.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@
package distance

import (
"sync"

"github.com/mfonda/simhash"
)

// Oracle answers the query if a fingerprint has been seen.
type Oracle struct {
fingerprint uint64 // node value.
nodes [65]*Oracle // leaf nodes
mu sync.Mutex
}

// NewOracle return an oracle that could tell if the fingerprint has been seen or not.
Expand All @@ -45,6 +48,8 @@ func (n *Oracle) See(f uint64) *Oracle {
}

// the target node is already set,
n.mu.Lock()
defer n.mu.Unlock()
if c := n.nodes[d]; c != nil {
return c.See(f)
}
Expand All @@ -65,7 +70,10 @@ func (n *Oracle) Seen(f uint64, r uint8) bool {
if k > 64 {
break
}
if c := n.nodes[k]; c != nil {
n.mu.Lock()
c := n.nodes[k]
n.mu.Unlock()
if c != nil {
if c.Seen(f, r) {
return true
}
Expand Down
9 changes: 9 additions & 0 deletions serialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"net/http"
)

// NewScanFromJson creates a Scan from the passed JSON blob.
func NewScanFromJson(b []byte) *Scan {
// ensure we got a memory store..
if memoryStore == nil {
Expand All @@ -21,6 +22,7 @@ func NewScanFromJson(b []byte) *Scan {
return &scan
}

// Json serializes Scan as JSON.
func (s *Scan) Json() []byte {
ss := &SerializableScan{
s,
Expand All @@ -39,16 +41,23 @@ func (s *Scan) Json() []byte {

}

// SerializableScan is a Scan extended with serializable
// request and response fields.
type SerializableScan struct {
*Scan
Request *SerializableRequest
Response *SerializableResponse
}

// SerializableResponse is a Scan extended with serializable
// response field.
type SerializableResponse struct {
*http.Response
Request *SerializableRequest
}

// SerializableRequest is a Scan extended with serializable
// request field.
type SerializableRequest struct {
*http.Request
Cancel string
Expand Down
17 changes: 17 additions & 0 deletions session.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,24 @@ import (
"encoding/json"
"fmt"
"strconv"
"sync"
"time"

distance "github.com/yahoo/gryffin/html-distance"
)

// GryffinStore includes data and handles for Gryffin message processing,
type GryffinStore struct {
Oracles map[string]*distance.Oracle
Hashes map[string]bool
Hits map[string]int
Mu sync.RWMutex
// store data.Store - currently unused, TODO: use or remove
snd chan []byte
rcv chan []byte
}

// PublishMessage is the data in the messages handled by Gryffin.
type PublishMessage struct {
F string // function, i.e. See or Seen
T string // type (kind), i.e. oracle or hash
Expand Down Expand Up @@ -101,12 +105,17 @@ func (s *GryffinStore) Seen(prefix string, kind string, v uint64, r uint8) bool

switch kind {
case "oracle":
s.Mu.RLock()
if oracle, ok := s.Oracles[prefix]; ok {
s.Mu.RUnlock()
return oracle.Seen(v, r)
}
s.Mu.RUnlock()
case "hash":
k := prefix + "/" + strconv.FormatUint(v, 10)
s.Mu.RLock()
_, ok := s.Hashes[k]
s.Mu.RUnlock()
return ok
}
return false
Expand All @@ -115,10 +124,14 @@ func (s *GryffinStore) Seen(prefix string, kind string, v uint64, r uint8) bool
func (s *GryffinStore) oracleSee(prefix string, f uint64, localOnly bool) {
k := prefix
// Local update
s.Mu.RLock()
oracle, ok := s.Oracles[k]
s.Mu.RUnlock()
if !ok {
s.Mu.Lock()
s.Oracles[k] = distance.NewOracle()
oracle = s.Oracles[k]
s.Mu.Unlock()
}
oracle.See(f)

Expand All @@ -134,7 +147,9 @@ func (s *GryffinStore) oracleSee(prefix string, f uint64, localOnly bool) {

func (s *GryffinStore) hashesSee(prefix string, h uint64, localOnly bool) {
k := prefix + "/" + strconv.FormatUint(h, 10)
s.Mu.Lock()
s.Hashes[k] = true
s.Mu.Unlock()
// Remote update
if !localOnly && s.snd != nil {
go func() {
Expand All @@ -148,6 +163,8 @@ func (s *GryffinStore) Hit(prefix string) bool {
// prefix is domain.
ts := time.Now().Truncate(5 * time.Second).Unix()
k := prefix + "/" + strconv.FormatInt(ts, 10)
s.Mu.Lock()
defer s.Mu.Unlock()
if v, ok := s.Hits[k]; ok {
if v >= 5 {
return false
Expand Down
2 changes: 1 addition & 1 deletion session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func TestNewGryffinStore(t *testing.T) {

go func() {
store1.See("foo", "oracle", uint64(0x1234))
var b []byte = <-store1.GetSndChan()
b := <-store1.GetSndChan()
t.Log("Store1 got ", string(b))
store2.GetRcvChan() <- b

Expand Down

0 comments on commit ad2188a

Please sign in to comment.