Skip to content

Commit 866cfe6

Browse files
committed
Fixes and optimizations galore
1 parent 31a52f9 commit 866cfe6

24 files changed

+792
-677
lines changed

modules/analyze/webservicefuncs.go

+10-7
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ func analysisfuncs(ws *webservice) {
105105
case "dn", "distinguishedname":
106106
o, found = ws.Objs.Find(activedirectory.DistinguishedName, engine.AttributeValueString(vars["id"]))
107107
case "sid":
108-
sid, err := windowssecurity.SIDFromString(vars["id"])
108+
sid, err := windowssecurity.ParseStringSID(vars["id"])
109109
if err != nil {
110110
w.WriteHeader(400) // bad request
111111
w.Write([]byte(err.Error()))
@@ -147,11 +147,12 @@ func analysisfuncs(ws *webservice) {
147147
Attributes: make(map[string][]string),
148148
}
149149

150-
for attr, values := range o.AttributeValueMap() {
150+
o.AttrIterator(func(attr engine.Attribute, values engine.AttributeValues) bool {
151151
slice := values.StringSlice()
152152
sort.StringSlice(slice).Sort()
153153
od.Attributes[attr.String()] = slice
154-
}
154+
return true
155+
})
155156

156157
if r.FormValue("format") == "json" {
157158
w.WriteHeader(200)
@@ -567,12 +568,13 @@ func analysisfuncs(ws *webservice) {
567568
`, id, node.Label(), node.DN())
568569

569570
if alldetails {
570-
for attribute, values := range node.AttributeValueMap() {
571+
node.AttrIterator(func(attribute engine.Attribute, values engine.AttributeValues) bool {
571572
valuesjoined := strings.Join(values.StringSlice(), ", ")
572573
if util.IsASCII(valuesjoined) {
573574
fmt.Fprintf(w, " %v %v\n", attribute, valuesjoined)
574575
}
575-
}
576+
return true
577+
})
576578
}
577579
fmt.Fprintf(w, " ]\n")
578580
}
@@ -601,7 +603,7 @@ func analysisfuncs(ws *webservice) {
601603
}
602604

603605
if alldetails {
604-
for attribute, values := range object.AttributeValueMap() {
606+
object.AttrIterator(func(attribute engine.Attribute, values engine.AttributeValues) bool {
605607
if values != nil {
606608
valuesjoined := strings.Join(values.StringSlice(), ", ")
607609
if util.IsASCII(valuesjoined) {
@@ -611,7 +613,8 @@ func analysisfuncs(ws *webservice) {
611613
})
612614
}
613615
}
614-
}
616+
return true
617+
})
615618
}
616619
graph.Nodes = append(graph.Nodes, xmlnode)
617620
}

modules/engine/analyzeobjects.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"github.com/lkarlslund/adalanche/modules/ui"
77
)
88

9-
var EdgeMemberOfGroup = NewEdge("MemberOfGroup") // FIXME, this should be generalized to expand-anyway-priority somehoe
9+
var EdgeMemberOfGroup = NewEdge("MemberOfGroup")
1010

1111
var SortBy Attribute = NonExistingAttribute
1212

modules/engine/attributes.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ var (
6363
ObjectSid = NewAttribute("objectSid").Single() // Strange yes, but in the final results there are multiple objects with the same SID
6464
ObjectGUID = NewAttribute("objectGUID").Single().Unique()
6565
NTSecurityDescriptor = NewAttribute("nTSecurityDescriptor").Single()
66-
SchemaIDGUID = NewAttribute("schemaIDGUID") // Dirty, needs proper FIXME for multi domain
66+
SchemaIDGUID = NewAttribute("schemaIDGUID")
6767
RightsGUID = NewAttribute("rightsGUID")
6868
AttributeSecurityGUID = NewAttribute("attributeSecurityGUID")
6969

modules/engine/attributevalue.go

+22-23
Original file line numberDiff line numberDiff line change
@@ -13,46 +13,44 @@ import (
1313
)
1414

1515
func CompareAttributeValues(a, b AttributeValue) bool {
16-
araw := a.Raw()
17-
braw := b.Raw()
18-
switch na := araw.(type) {
19-
case bool:
20-
nb, btype := braw.(bool)
16+
switch na := a.(type) {
17+
case AttributeValueBool:
18+
nb, btype := b.(AttributeValueBool)
2119
if btype {
2220
return na == nb
2321
}
24-
case string:
25-
nb, btype := braw.(string)
22+
case AttributeValueString:
23+
nb, btype := b.(AttributeValueString)
2624
if btype {
27-
return strings.EqualFold(na, nb)
25+
return strings.EqualFold(string(na), string(nb))
2826
}
29-
case int64:
30-
nb, btype := braw.(int64)
27+
case AttributeValueInt:
28+
nb, btype := b.(AttributeValueInt)
3129
if btype {
3230
return na == nb
3331
}
34-
case time.Time:
35-
nb, btype := braw.(time.Time)
32+
case AttributeValueTime:
33+
nb, btype := b.(AttributeValueTime)
3634
if btype {
37-
return na.Equal(nb)
35+
return time.Time(na).Equal(time.Time(nb))
3836
}
39-
case []byte:
40-
nb, btype := braw.([]byte)
37+
case AttributeValueBlob:
38+
nb, btype := b.(AttributeValueBlob)
4139
if btype {
42-
return bytes.Equal(na, nb)
40+
return bytes.Equal([]byte(na), []byte(nb))
4341
}
44-
case windowssecurity.SID:
45-
nb, btype := braw.(windowssecurity.SID)
42+
case AttributeValueSID:
43+
nb, btype := b.(AttributeValueSID)
4644
if btype {
4745
return string(na) == string(nb)
4846
}
49-
case uuid.UUID:
50-
nb, btype := braw.(uuid.UUID)
47+
case AttributeValueGUID:
48+
nb, btype := b.(AttributeValueGUID)
5149
if btype {
5250
return na == nb
5351
}
54-
case *Object:
55-
nb, btype := braw.(*Object)
52+
case AttributeValueObject:
53+
nb, btype := b.(AttributeValueObject)
5654
if btype {
5755
return na == nb // Exact same object pointed to in memory
5856
}
@@ -171,6 +169,7 @@ type AttributeValue interface {
171169
String() string
172170
Raw() interface{}
173171
IsZero() bool
172+
// Compare(other AttributeValue) bool
174173
}
175174

176175
type AttributeValueObject struct {
@@ -189,7 +188,7 @@ func (avo AttributeValueObject) IsZero() bool {
189188
if avo.Object == nil {
190189
return true
191190
}
192-
return len(avo.values) == 0
191+
return avo.values.Len() == 0
193192
}
194193

195194
type AttributeValueString string

modules/engine/attributevaluemap.go

+66-9
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,77 @@
11
package engine
22

3-
type AttributeValueMap map[Attribute]AttributeValues
4-
5-
func NewAttributeValueMap() AttributeValueMap {
6-
return make(AttributeValueMap)
3+
type AttributeValueMap struct {
4+
m map[Attribute]AttributeValues
5+
// firstattribute Attribute
6+
// data []AttributeValues
77
}
88

99
func (avm AttributeValueMap) Get(a Attribute) (av AttributeValues, found bool) {
10-
av, found = avm[a]
10+
if avm.m == nil {
11+
return nil, false
12+
}
13+
av, found = avm.m[a]
1114
return
15+
// if a < avm.firstattribute || int(a-avm.firstattribute) >= len(avm.data) {
16+
// return nil, false
17+
// }
18+
// result := avm.data[a-avm.firstattribute]
19+
// return result, result != nil
20+
}
21+
22+
func (avm *AttributeValueMap) Set(a Attribute, av AttributeValues) {
23+
if avm.m == nil {
24+
avm.m = make(map[Attribute]AttributeValues)
25+
}
26+
avm.m[a] = av
27+
// if len(avm.data) == 0 {
28+
// avm.firstattribute = a
29+
// avm.data = make([]AttributeValues, 1)
30+
// avm.data[0] = av
31+
// } else if a < avm.firstattribute {
32+
// shift := int(avm.firstattribute - a)
33+
// newdata := make([]AttributeValues, len(avm.data)+shift, len(avm.data)+shift)
34+
// copy(newdata[shift:], avm.data)
35+
// avm.data = newdata
36+
// avm.firstattribute = a
37+
// } else if int(a-avm.firstattribute) >= len(avm.data) {
38+
// add := int(a-avm.firstattribute) - len(avm.data) + 1
39+
// newdata := make([]AttributeValues, len(avm.data)+add, len(avm.data)+add)
40+
// copy(newdata, avm.data)
41+
// avm.data = newdata
42+
// }
43+
// avm.data[a-avm.firstattribute] = av
44+
}
45+
46+
func (avm AttributeValueMap) Len() int {
47+
return len(avm.m)
48+
// var count int
49+
// for _, v := range avm.data {
50+
// if v != nil {
51+
// count++
52+
// }
53+
// }
54+
// return count
1255
}
1356

14-
func (avm AttributeValueMap) Set(a Attribute, av AttributeValues) {
15-
avm[a] = av
57+
func (avm *AttributeValueMap) Clear(a Attribute) {
58+
if avm.m != nil {
59+
delete(avm.m, a)
60+
}
61+
// avm.data[a-avm.firstattribute] = nil
1662
}
1763

18-
func (avm AttributeValueMap) Clear(a Attribute) {
19-
delete(avm, a)
64+
func (avm AttributeValueMap) Iterate(f func(attr Attribute, values AttributeValues) bool) {
65+
for attr, values := range avm.m {
66+
if !f(attr, values) {
67+
break
68+
}
69+
}
70+
// for i, values := range avm.data {
71+
// if values != nil {
72+
// if !f(avm.firstattribute+Attribute(i), values) {
73+
// break
74+
// }
75+
// }
76+
// }
2077
}

modules/engine/edge.go

+5-13
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ type EdgeAnalyzer struct {
1515

1616
// Increas this when we run out of space
1717
const PMBSIZE = 2
18-
const MAXPWNMETHODPOSSIBLE = PMBSIZE * 64
18+
const MAXEDGEPOSSIBLE = PMBSIZE * 64
1919

2020
type EdgeBitmap [PMBSIZE]uint64
2121
type Probability int8
@@ -147,7 +147,7 @@ func NewEdge(name string) Edge {
147147
}
148148

149149
newindex := Edge(len(edgeInfos))
150-
if newindex == MAXPWNMETHODPOSSIBLE {
150+
if newindex == MAXEDGEPOSSIBLE {
151151
panic("Too many Edge definitions")
152152
}
153153

@@ -232,30 +232,22 @@ var (
232232

233233
var AllEdgesBitmap EdgeBitmap
234234

235-
var EdgePopularity [MAXPWNMETHODPOSSIBLE]uint64
235+
var EdgePopularity [MAXEDGEPOSSIBLE]uint64
236236

237237
func init() {
238-
for i := Edge(0); i < MAXPWNMETHODPOSSIBLE; i++ {
238+
for i := Edge(0); i < MAXEDGEPOSSIBLE; i++ {
239239
AllEdgesBitmap = AllEdgesBitmap.set(i)
240240
}
241241
}
242242

243-
/*
244-
type PwnMethodsAndProbabilities struct {
245-
EdgeBitmap // Indicates if we have this method registered
246-
probabilitymap EdgeBitmap // Indicates if we have a probability set or should just return 100
247-
probabilities Probabilities
248-
}
249-
*/
250-
251243
type EdgeDirection int
252244

253245
const (
254246
Out EdgeDirection = 0
255247
In EdgeDirection = 1
256248
)
257249

258-
type EdgeConnections map[*Object]EdgeBitmap //sAndProbabilities
250+
type EdgeConnections map[*Object]EdgeBitmap
259251

260252
var globalEdgeConnectionsLock sync.Mutex // Ugly but it will do
261253

0 commit comments

Comments
 (0)