Skip to content

Commit

Permalink
Got rid of slice range and moved to iterator pattern for Objects, imp…
Browse files Browse the repository at this point in the history
…roved indexes by adding multiindex
  • Loading branch information
lkarlslund committed Oct 27, 2022
1 parent 9c98073 commit 1770d29
Show file tree
Hide file tree
Showing 12 changed files with 538 additions and 424 deletions.
33 changes: 19 additions & 14 deletions modules/analyze/webservicefuncs.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,12 +324,14 @@ func analysisfuncs(ws *webservice) {
// We dont support this yet, so merge all of them
combinedmethods := edges_f.Merge(egdes_m).Merge(edges_l)

for _, source := range includeobjects.Slice() {
for _, target := range excludeobjects.Slice() {
includeobjects.Iterate(func(source *engine.Object) bool {
excludeobjects.Iterate(func(target *engine.Object) bool {
newpg := engine.AnalyzePaths(source, target, ws.Objs, combinedmethods, engine.Probability(minprobability), maxdepth)
pg.Merge(newpg)
}
}
return true
})
return true
})
// pg = engine.AnalyzePaths(includeobjects.First(), excludeobjects.First(), ws.Objs, combinedmethods, engine.Probability(minprobability), 1)
} else {
opts := engine.NewAnalyzeObjectsOptions()
Expand Down Expand Up @@ -657,11 +659,12 @@ func analysisfuncs(ws *webservice) {
return includequery.Evaluate(o)
})

dns := make([]string, len(objects.Slice()))
dns := make([]string, 0, objects.Len())

for i, o := range objects.Slice() {
dns[i] = o.DN()
}
objects.Iterate(func(o *engine.Object) bool {
dns = append(dns, o.DN())
return true
})

err = encoder.Encode(dns)
if err != nil {
Expand Down Expand Up @@ -695,7 +698,7 @@ func analysisfuncs(ws *webservice) {
return includequery.Evaluate(o)
})

err = encoder.Encode(objects.Slice())
err = encoder.Encode(objects.AsSlice())
if err != nil {
w.WriteHeader(400) // bad request
w.Write([]byte(err.Error()))
Expand All @@ -722,7 +725,7 @@ func analysisfuncs(ws *webservice) {
HasLAPS bool `json:"haslaps,omitempty"`
}
var result []info
for _, object := range ws.Objs.Slice() {
ws.Objs.Iterate(func(object *engine.Object) bool {
if object.Type() == engine.ObjectTypeUser &&
object.OneAttrString(engine.MetaWorkstation) != "1" &&
object.OneAttrString(engine.MetaServer) != "1" &&
Expand Down Expand Up @@ -772,7 +775,8 @@ func analysisfuncs(ws *webservice) {

result = append(result, i)
}
}
return true
})

data, err := json.MarshalIndent(result, "", " ")
if err != nil {
Expand Down Expand Up @@ -857,10 +861,11 @@ func analysisfuncs(ws *webservice) {
}

var pwnlinks int
for _, object := range ws.Objs.Slice() {
ws.Objs.Iterate(func(object *engine.Object) bool {
pwnlinks += object.Edges(engine.Out).Len()
}
result.Statistics["Total"] = len(ws.Objs.Slice())
return true
})
result.Statistics["Total"] = ws.Objs.Len()
result.Statistics["PwnConnections"] = pwnlinks

data, _ := json.MarshalIndent(result, "", " ")
Expand Down
7 changes: 4 additions & 3 deletions modules/engine/analyzeobjects.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,12 @@ func AnalyzeObjects(opts AnalyzeObjectsOptions) (pg Graph) {

// Convert to our working map
processinground := 1
for _, object := range opts.IncludeObjects.Slice() {
implicatedobjectsmap[object] = &roundinfo{
opts.IncludeObjects.Iterate(func(o *Object) bool {
implicatedobjectsmap[o] = &roundinfo{
roundadded: processinground,
}
}
return true
})

// Methods and ObjectTypes allowed
detectedges := opts.MethodsF
Expand Down
5 changes: 5 additions & 0 deletions modules/engine/attributes.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,11 @@ var (

type Attribute uint16

type AttributePair struct {
attribute1 Attribute
attribute2 Attribute
}

var attributemutex sync.RWMutex

func NewAttribute(name string) Attribute {
Expand Down
5 changes: 5 additions & 0 deletions modules/engine/attributevalue.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,11 @@ type AttributeValue interface {
IsZero() bool
}

type AttributeValuePair struct {
Value1 AttributeValue
Value2 AttributeValue
}

type AttributeValueObject struct {
*Object
}
Expand Down
99 changes: 99 additions & 0 deletions modules/engine/index.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package engine

import (
"sync"
)

type Index struct {
lookup map[AttributeValue][]*Object
sync.RWMutex
}

func (i *Index) init() {
i.lookup = make(map[AttributeValue][]*Object)
}

func (i *Index) Lookup(key AttributeValue) ([]*Object, bool) {
iv := AttributeValueToIndex(key)
i.RLock()
result, found := i.lookup[iv]
i.RUnlock()
return result, found
}

func (i *Index) Add(key AttributeValue, o *Object, undupe bool) {
iv := AttributeValueToIndex(key)
i.Lock()
if undupe {
existing, _ := i.lookup[iv]
for _, dupe := range existing {
if dupe == o {
i.Unlock()
return
}
}
}

existing, _ := i.lookup[iv]
i.lookup[iv] = append(existing, o)
i.Unlock()
}

func (i *Index) Iterate(each func(key AttributeValue, objects []*Object) bool) {
i.RLock()
for key, value := range i.lookup {
if !each(key, value) {
break
}
}
i.RUnlock()
}

type MultiIndex struct {
lookup map[AttributeValuePair][]*Object
sync.RWMutex
}

func (i *MultiIndex) init() {
i.lookup = make(map[AttributeValuePair][]*Object)
}

func (i *MultiIndex) Lookup(key, key2 AttributeValue) ([]*Object, bool) {
iv := AttributeValueToIndex(key)
iv2 := AttributeValueToIndex(key2)

i.RLock()
result, found := i.lookup[AttributeValuePair{iv, iv2}]
i.RUnlock()
return result, found
}

func (i *MultiIndex) Add(key, key2 AttributeValue, o *Object, undupe bool) {
iv := AttributeValueToIndex(key)
iv2 := AttributeValueToIndex(key2)
avp := AttributeValuePair{iv, iv2}
i.Lock()
if undupe {
existing, _ := i.lookup[avp]
for _, dupe := range existing {
if dupe == o {
i.Unlock()
return
}
}
}

existing, _ := i.lookup[avp]
i.lookup[avp] = append(existing, o)
i.Unlock()
}

func (i *MultiIndex) Iterate(each func(key, key2 AttributeValue, objects []*Object) bool) {
i.RLock()
for pair, value := range i.lookup {
if !each(pair.Value1, pair.Value2, value) {
break
}
}
i.RUnlock()
}
Loading

0 comments on commit 1770d29

Please sign in to comment.