-
Notifications
You must be signed in to change notification settings - Fork 0
/
connection.go
331 lines (287 loc) · 9.95 KB
/
connection.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
package oracle
import (
"net"
"strconv"
"github.com/dmin12/zgrab_tls1.3"
)
// HandshakeLog gives the results of the initial connection handshake in a form
// suitable for zgrab2 output.
type HandshakeLog struct {
// AcceptVersion is the protocol version value from the Accept packet.
AcceptVersion uint16 `json:"accept_version"`
// GlobalServiceOptions is the set of GlobalServiceOptions flags that the
// server returns in the Accept packet.
GlobalServiceOptions map[string]bool `json:"global_service_options,omitempty"`
// ConnectFlags0 is the first set of ConnectFlags values that the server
// returns in the Accept packet for the first.
ConnectFlags0 map[string]bool `json:"connect_flags0,omitempty"`
// ConnectFlags1 is the second set of ConnectFlags values that the server
// returns in the Accept packet for the first.
ConnectFlags1 map[string]bool `json:"connect_flags1,omitempty"`
// DidResend is true if the server sent a Resend packet in response to the
// client's first Connect packet.
DidResend bool `json:"did_resend"`
// RedirectTargetRaw is the connect descriptor returned by the server in the
// Redirect packet, if one is sent. Otherwise it is empty/omitted.
RedirectTargetRaw string `json:"redirect_target_raw,omitempty"`
// RedirectTarget is the parsed connect descriptor returned by the server in
// the Redirect packet, if one is sent. Otherwise it is empty/omitted.
RedirectTarget Descriptor `json:"redirect_target,omitempty"`
// RefuseErrorRaw is the Data from the Refuse packet returned by the server;
// it is empty if the server does not return a Refuse packet.
RefuseErrorRaw string `json:"refuse_error_raw,omitempty"`
// RefuseError is the parsed descriptor returned by the server in the Refuse
// packet; it is empty if the server does not return a Refuse packet.
RefuseError Descriptor `json:"refuse_error,omitempty"`
// RefuseReasonApp is the "AppReason" returned by the server in a Refused
// response packet.
RefuseReasonApp string `json:"refuse_reason_app,omitempty"`
// RefuseReasonSys is the "SysReason" returned by the server in a Refused
// response packet.
RefuseReasonSys string `json:"refuse_reason_sys,omitempty"`
// RefuseVersion is the parsed DESCRIPTION.VSNNUM field from the RefuseError
// string returned by the server in the Refuse packet, in dotted-decimal
// format.
RefuseVersion string `json:"refuse_version,omitempty"`
// DidResend is set to true if the server sent a Resend packet after the
// first Connect packet.
// NSNVersion is the ReleaseVersion string (in dotted decimal format) in the
// root of the Native Service Negotiation packet.
NSNVersion string `json:"nsn_version,omitempty"`
// NSNServiceVersions is a map from the Native Service Negotiation service
// name to the ReleaseVersion in that service packet.
NSNServiceVersions map[string]string `json:"nsn_service_versions,omitempty"`
}
// Connection holds the state for a scan connection to the Oracle server.
type Connection struct {
conn net.Conn
target *zgrab2.ScanTarget
scanner *Scanner
resent bool
redirect string
tnsDriver *TNSDriver
}
// send ensures everything gets written
func (conn *Connection) send(data []byte) error {
rest := data
n := 0
for n < len(rest) {
n, err := conn.conn.Write(rest)
if err != nil {
return err
}
rest = rest[n:]
}
return nil
}
// readPacket tries to read/parse a packet from the connection.
func (conn *Connection) readPacket() (*TNSPacket, error) {
return conn.tnsDriver.ReadTNSPacket(conn.conn)
}
// SendPacket sends the given packet body to the server (prefixing the
// appropriate header -- with flags == 0), and read / parse the response.
// Automatically handles Resend responses; the caller is responsible for
// handling other exceptional cases.
func (conn *Connection) SendPacket(packet TNSPacketBody) (TNSPacketBody, error) {
toSend, err := conn.tnsDriver.EncodePacket(&TNSPacket{Body: packet})
if err != nil {
return nil, err
}
if err := conn.send(toSend); err != nil {
return nil, err
}
response, err := conn.readPacket()
if err != nil {
return nil, err
}
if response.Body.GetType() == PacketTypeResend {
conn.resent = true
// Only re-send once.
if err = conn.send(toSend); err != nil {
return nil, err
}
response, err = conn.readPacket()
if err != nil {
return nil, err
}
}
return response.Body, nil
}
// Handle numeric args in any radix.
func u16Flag(v string) uint16 {
ret, err := strconv.ParseUint(v, 0, 16)
if err != nil {
panic(err)
}
return uint16(ret)
}
// Connect to the server and do a handshake with the given config.
func (conn *Connection) Connect(connectDescriptor string) (*HandshakeLog, error) {
result := HandshakeLog{}
extraData := []byte{}
if len(connectDescriptor)+len(extraData)+0x3A > 0x7fff {
return nil, ErrInvalidInput
}
// TODO: Variable fields in the connect descriptor (e.g. host?)
connectPacket := &TNSConnect{
Version: conn.scanner.config.Version,
MinVersion: conn.scanner.config.MinVersion,
GlobalServiceOptions: ServiceOptions(u16Flag(conn.scanner.config.GlobalServiceOptions)),
SDU: u16Flag(conn.scanner.config.SDU),
TDU: u16Flag(conn.scanner.config.TDU),
ProtocolCharacteristics: NTProtocolCharacteristics(u16Flag(conn.scanner.config.ProtocolCharacterisics)),
MaxBeforeAck: 0,
ByteOrder: defaultByteOrder,
DataLength: uint16(len(connectDescriptor)),
DataOffset: uint16(0x003A + len(extraData)),
MaxResponseSize: 0x00000800,
ConnectFlags0: ConnectFlags(u16Flag(conn.scanner.config.ConnectFlags) & 0xff),
ConnectFlags1: ConnectFlags(u16Flag(conn.scanner.config.ConnectFlags) >> 8),
CrossFacility0: 0,
CrossFacility1: 0,
ConnectionID0: [8]byte{0, 0, 0, 0, 0, 0, 0, 0},
ConnectionID1: [8]byte{0, 0, 0, 0, 0, 0, 0, 0},
Unknown3A: extraData,
ConnectDescriptor: connectDescriptor,
}
response, err := conn.SendPacket(connectPacket)
if err != nil {
return nil, err
}
if conn.resent {
result.DidResend = true
}
var accept *TNSAccept
switch resp := response.(type) {
case *TNSAccept:
accept = resp
case *TNSRedirect:
result.RedirectTargetRaw = string(resp.Data)
if parsed, err := DecodeDescriptor(result.RedirectTargetRaw); err != nil {
result.RedirectTarget = parsed
}
// TODO: Follow redirects?
return &result, nil
case *TNSRefuse:
result.RefuseErrorRaw = string(resp.Data)
result.RefuseReasonApp = resp.AppReason.String()
result.RefuseReasonSys = resp.SysReason.String()
if desc, err := DecodeDescriptor(result.RefuseErrorRaw); err == nil {
result.RefuseError = desc
if versions := desc.GetValues("DESCRIPTION.VSNNUM"); len(versions) > 0 {
// If there are multiple VSNNUMs, we only care about the first.
decVersion := versions[0]
if intVersion, err := strconv.ParseUint(decVersion, 10, 32); err == nil {
result.RefuseVersion = ReleaseVersion(intVersion).String()
}
}
}
return &result, nil
default:
return &result, ErrUnexpectedResponse
}
// TODO: Unclear what all of these values these do. Defaults taken from the
// values sent by the Oracle SQLPlus 11.2 client.
result.AcceptVersion = accept.Version
result.GlobalServiceOptions = accept.GlobalServiceOptions.Set()
result.ConnectFlags0 = accept.ConnectFlags0.Set()
result.ConnectFlags1 = accept.ConnectFlags1.Set()
// uint32 PID + uint32 ??
// In real clients, seems to be a small u32 followed by some kind of u32
// counter/timestamp.
supervisorBytes0 := []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
supervisorBytes1 := []byte{
0xde, 0xad, 0xbe, 0xef,
0x00, 0x03, // Array type?
0x00, 0x00, 0x00, 0x04, // Array length?
0x00, 0x04, // Drivers?
0x00, 0x01,
0x00, 0x01,
0x00, 0x02,
}
authUB2 := uint16(0xe0e1)
authStatus := uint16(0xfcff)
authUB1 := uint8(1)
// Windows NTS authentication
authString := "NTS"
// Supported encryption algorithms?
encryptionBytes := []byte{0x00, 0x11, 0x06, 0x10, 0x0c, 0x0f, 0x0a, 0x0b, 0x08, 0x02, 0x01, 0x03}
// Drivers?
dataIntegrityBytes := []byte{0x00, 0x03, 0x01}
encoded, err := (&TNSDataNSN{
ID: DataIDNSN,
Version: encodeReleaseVersion(conn.scanner.config.ReleaseVersion),
Options: NSNOptions(0),
Services: []NSNService{
NSNService{
Type: NSNServiceSupervisor,
Values: []NSNValue{
*NSNValueVersion(conn.scanner.config.ReleaseVersion),
*NSNValueBytes(supervisorBytes0),
*NSNValueBytes(supervisorBytes1),
},
Marker: 0,
},
NSNService{
Type: NSNServiceAuthentication,
Values: []NSNValue{
*NSNValueVersion(conn.scanner.config.ReleaseVersion),
*NSNValueUB2(authUB2),
*NSNValueStatus(authStatus),
*NSNValueUB1(authUB1),
*NSNValueString(authString),
},
Marker: 0,
},
NSNService{
Type: NSNServiceEncryption,
Values: []NSNValue{
*NSNValueVersion(conn.scanner.config.ReleaseVersion),
*NSNValueBytes(encryptionBytes),
},
Marker: 0,
},
NSNService{
Type: NSNServiceDataIntegrity,
Values: []NSNValue{
*NSNValueVersion(conn.scanner.config.ReleaseVersion),
*NSNValueBytes(dataIntegrityBytes),
},
},
},
}).Encode()
if err != nil {
return &result, err
}
nsnRequest := &TNSData{
DataFlags: 0,
Data: encoded,
}
response, err = conn.SendPacket(nsnRequest)
if err != nil {
return &result, err
}
wrappedNSNResponse, ok := response.(*TNSData)
if !ok {
return &result, ErrUnexpectedResponse
}
if wrappedNSNResponse.GetID() != DataIDNSN {
return &result, ErrUnexpectedResponse
}
nsnResponse, err := DecodeTNSDataNSN(wrappedNSNResponse.Data)
if err != nil {
return &result, err
}
result.NSNServiceVersions = make(map[string]string)
for _, svc := range nsnResponse.Services {
if !svc.Type.IsUnknown() {
for _, sub := range svc.Values {
if sub.Type == NSNValueTypeVersion {
result.NSNServiceVersions[svc.Type.String()] = sub.String()
break
}
}
}
}
return &result, nil
}