/
ntlmssp.go
149 lines (115 loc) · 2.94 KB
/
ntlmssp.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package flamingo
import (
"bytes"
"encoding/binary"
"fmt"
"strings"
)
func ntlmsspExtractFieldsFromBlob(blob []byte) map[string]string {
var err error
res := make(map[string]string)
ntlmsspOffset := bytes.Index(blob, []byte{'N', 'T', 'L', 'M', 'S', 'S', 'P', 0x00, 0x02, 0x00, 0x00, 0x00})
if ntlmsspOffset < 0 {
return res
}
data := blob[ntlmsspOffset:]
// Basic sanity check
if len(data) < (12 + 6 + 12 + 8 + 6 + 8) {
return res
}
idx := 12
targetName, idx, err := ntlmsspExtractOffset(data, idx)
if err != nil {
return res
}
// Negotiate Flags
negotiateFlags := binary.LittleEndian.Uint32(data[idx:])
res["flags"] = fmt.Sprintf("0x%.8x", negotiateFlags)
idx += 4
// NTLM Server Challenge
idx += 8
// Reserved
idx += 8
// Target Info
targetInfo, idx, err := ntlmsspExtractOffset(data, idx)
if err != nil {
return res
}
// Version
versionMajor := uint8(data[idx])
idx++
versionMinor := uint8(data[idx])
idx++
versionBuild := binary.LittleEndian.Uint16(data[idx:])
idx += 2
ntlmRevision := binary.BigEndian.Uint32(data[idx:])
// macOS reverses the endian order of this field for some reason
if ntlmRevision == 251658240 {
ntlmRevision = binary.LittleEndian.Uint32(data[idx:])
}
res["version"] = fmt.Sprintf("%d.%d.%d", versionMajor, versionMinor, versionBuild)
res["revision"] = fmt.Sprintf("%d", ntlmRevision)
res["target_name"] = ntlmsspTrimName(string(targetName))
idx = 0
for {
if idx+4 > len(targetInfo) {
break
}
attrType := binary.LittleEndian.Uint16(targetInfo[idx:])
idx += 2
// End of List
if attrType == 0 {
break
}
attrLen := binary.LittleEndian.Uint16(targetInfo[idx:])
idx += 2
if idx+int(attrLen) > len(targetInfo) {
// Too short
break
}
attrVal := targetInfo[idx : idx+int(attrLen)]
idx += int(attrLen)
switch attrType {
case 1:
res["nb_computer"] = ntlmsspTrimName(string(attrVal))
case 2:
res["nb_domain"] = ntlmsspTrimName(string(attrVal))
case 3:
res["dns_computer"] = ntlmsspTrimName(string(attrVal))
case 4:
res["dns_domain"] = ntlmsspTrimName(string(attrVal))
case 7:
ts := binary.LittleEndian.Uint64(attrVal[:])
res["timestamp"] = fmt.Sprintf("0x%.16x", ts)
}
// End of List
if attrType == 0 {
break
}
}
return res
}
func ntlmsspExtractOffset(blob []byte, idx int) ([]byte, int, error) {
res := []byte{}
if len(blob) < (idx + 6) {
return res, idx, fmt.Errorf("data truncated")
}
len1 := binary.LittleEndian.Uint16(blob[idx:])
idx += 2
// len2 := binary.LittleEndian.Uint16(blob[idx:])
idx += 2
off := binary.LittleEndian.Uint32(blob[idx:])
idx += 4
// Allow zero length values
if len1 == 0 {
return res, idx, nil
}
if len(blob) < int(off+uint32(len1)) {
return res, idx, fmt.Errorf("data value truncated")
}
res = append(res, blob[off:off+uint32(len1)]...)
return res, idx, nil
}
func ntlmsspTrimName(name string) string {
return strings.TrimSpace(strings.Replace(name, "\x00", "", -1))
}