smb-server 0.12.1

SMB2/3 file-sharing server library with pluggable storage backends.
Documentation
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
//! Client-side NTLM, SPNEGO and SMB2 pieces for the authentication tests.

use hmac::Mac as _;
use md4::Digest as _;
use rc4::{KeyInit as _, StreamCipher as _};
use smb_server::ntstatus::STATUS_MORE_PROCESSING_REQUIRED;
use smb_server::wire::header::{Command, SMB2_FLAGS_SIGNED, Smb2Header};
use smb_server::wire::messages::{FileId, IoctlRequest, NegotiateRequest, SessionSetupResponse};
use tokio::io::AsyncWriteExt as _;
use tokio::net::TcpStream;

use super::{
    NTLMSSP_SIGNATURE, OID_NTLMSSP, OID_SPNEGO, build_header, framed, parse_response_header,
    read_frame, session_setup_body, smb2_signature, try_read_frame, utf16le,
};

pub const OID_KRB5: &[u8] = &[0x2a, 0x86, 0x48, 0x86, 0xf7, 0x12, 0x01, 0x02, 0x02];

pub const NTLM_UNICODE: u32 = 0x0000_0001;
pub const NTLM_REQUEST_TARGET: u32 = 0x0000_0004;
pub const NTLM_SIGN: u32 = 0x0000_0010;
pub const NTLM_NTLM: u32 = 0x0000_0200;
pub const NTLM_ALWAYS_SIGN: u32 = 0x0000_8000;
pub const NTLM_ESS: u32 = 0x0008_0000;
pub const NTLM_TARGET_INFO: u32 = 0x0080_0000;
pub const NTLM_VERSION: u32 = 0x0200_0000;
pub const NTLM_128: u32 = 0x2000_0000;
pub const NTLM_KEY_EXCH: u32 = 0x4000_0000;
pub const NTLM_56: u32 = 0x8000_0000;

/// NTLMv2 client flags with signing, key exchange, 128-bit keys and TargetInfo.
pub const CLIENT_FLAGS: u32 = NTLM_UNICODE
    | NTLM_REQUEST_TARGET
    | NTLM_SIGN
    | NTLM_NTLM
    | NTLM_ALWAYS_SIGN
    | NTLM_ESS
    | NTLM_TARGET_INFO
    | NTLM_VERSION
    | NTLM_128
    | NTLM_KEY_EXCH
    | NTLM_56;

// --- DER --------------------------------------------------------------------

pub fn tlv(tag: u8, content: &[u8]) -> Vec<u8> {
    let mut out = vec![tag];
    let n = content.len();
    if n < 0x80 {
        out.push(n as u8);
    } else {
        let bytes: Vec<u8> = n
            .to_be_bytes()
            .into_iter()
            .skip_while(|&b| b == 0)
            .collect();
        out.push(0x80 | bytes.len() as u8);
        out.extend_from_slice(&bytes);
    }
    out.extend_from_slice(content);
    out
}

/// Splits one TLV off `buf`: (tag, content, rest).
pub fn split_tlv(buf: &[u8]) -> (u8, &[u8], &[u8]) {
    let tag = buf[0];
    let (len, at) = if buf[1] < 0x80 {
        (buf[1] as usize, 2)
    } else {
        let nb = (buf[1] & 0x7f) as usize;
        let len = buf[2..2 + nb]
            .iter()
            .fold(0usize, |v, &b| (v << 8) | b as usize);
        (len, 2 + nb)
    };
    (tag, &buf[at..at + len], &buf[at + len..])
}

/// DER MechTypeList for `mechs`.
pub fn mech_type_list(mechs: &[&[u8]]) -> Vec<u8> {
    let oids: Vec<u8> = mechs.iter().flat_map(|m| tlv(0x06, m)).collect();
    tlv(0x30, &oids)
}

/// GSS-API NegTokenInit with the given MechTypeList and optional mechToken.
pub fn spnego_init(mech_list: &[u8], token: Option<&[u8]>) -> Vec<u8> {
    let mut seq = tlv(0xa0, mech_list);
    if let Some(t) = token {
        seq.extend(tlv(0xa2, &tlv(0x04, t)));
    }
    let inner = [tlv(0x06, OID_SPNEGO), tlv(0xa0, &tlv(0x30, &seq))].concat();
    tlv(0x60, &inner)
}

/// NegTokenResp with an optional responseToken and mechListMIC.
pub fn spnego_resp(token: Option<&[u8]>, mic: Option<&[u8]>) -> Vec<u8> {
    let mut seq = Vec::new();
    if let Some(t) = token {
        seq.extend(tlv(0xa2, &tlv(0x04, t)));
    }
    if let Some(m) = mic {
        seq.extend(tlv(0xa3, &tlv(0x04, m)));
    }
    tlv(0xa1, &tlv(0x30, &seq))
}

/// Fields of a server NegTokenResp.
#[derive(Debug, Default)]
pub struct NegResp {
    pub state: Option<u8>,
    pub mech: Option<Vec<u8>>,
    pub token: Option<Vec<u8>>,
    pub mic: Option<Vec<u8>>,
}

pub fn parse_neg_resp(buf: &[u8]) -> NegResp {
    let (tag, inner, _) = split_tlv(buf);
    assert_eq!(tag, 0xa1, "NegTokenResp");
    let (_, mut seq, _) = split_tlv(inner);
    let mut out = NegResp::default();
    while !seq.is_empty() {
        let (tag, content, rest) = split_tlv(seq);
        let (_, value, _) = split_tlv(content);
        match tag {
            0xa0 => out.state = Some(value[0]),
            0xa1 => out.mech = Some(value.to_vec()),
            0xa2 => out.token = Some(value.to_vec()),
            0xa3 => out.mic = Some(value.to_vec()),
            _ => {}
        }
        seq = rest;
    }
    out
}

// --- NTLM -------------------------------------------------------------------

fn hmac_md5(key: &[u8], parts: &[&[u8]]) -> [u8; 16] {
    let mut mac = hmac::Hmac::<md5::Md5>::new_from_slice(key).expect("hmac key");
    for part in parts {
        mac.update(part);
    }
    mac.finalize().into_bytes().into()
}

fn md5_of(parts: &[&[u8]]) -> [u8; 16] {
    let mut h = md5::Md5::new();
    for part in parts {
        h.update(part);
    }
    h.finalize().into()
}

fn rc4(key: &[u8], data: &mut [u8]) {
    rc4::Rc4::new_from_slice(key)
        .expect("rc4 key")
        .apply_keystream(data);
}

pub fn ntlm_negotiate(flags: u32) -> Vec<u8> {
    let mut msg = NTLMSSP_SIGNATURE.to_vec();
    msg.extend_from_slice(&1u32.to_le_bytes());
    msg.extend_from_slice(&flags.to_le_bytes());
    msg.extend_from_slice(&[0u8; 16]);
    msg.extend_from_slice(&[6, 1, 0, 0x1D, 0, 0, 0, 0x0F]);
    msg
}

/// Server challenge and TargetInfo of a CHALLENGE_MESSAGE.
pub fn parse_challenge(msg: &[u8]) -> ([u8; 8], Vec<u8>) {
    assert_eq!(&msg[..8], NTLMSSP_SIGNATURE);
    let challenge: [u8; 8] = msg[24..32].try_into().unwrap();
    let len = u16::from_le_bytes([msg[40], msg[41]]) as usize;
    let off = u32::from_le_bytes(msg[44..48].try_into().unwrap()) as usize;
    (challenge, msg[off..off + len].to_vec())
}

/// Options for [`ntlm_authenticate`].
#[derive(Clone, Copy)]
pub struct AuthOptions {
    pub flags: u32,
    /// Announce a MIC in MsvAvFlags and fill it in.
    pub mic: bool,
}

impl Default for AuthOptions {
    fn default() -> Self {
        Self {
            flags: CLIENT_FLAGS,
            mic: true,
        }
    }
}

/// NTLMv2 AUTHENTICATE_MESSAGE answering `challenge_msg`, and the
/// ExportedSessionKey.
pub fn ntlm_authenticate(
    user: &str,
    password: &str,
    negotiate_msg: &[u8],
    challenge_msg: &[u8],
    opts: AuthOptions,
) -> (Vec<u8>, [u8; 16]) {
    let (server_challenge, target_info) = parse_challenge(challenge_msg);
    let nt_hash: [u8; 16] = md4::Md4::digest(utf16le(password)).into();
    let response_key = hmac_md5(&nt_hash, &[&utf16le(&user.to_uppercase())]);

    // TargetInfo with its EOL removed, MsvAvFlags added, and a new EOL.
    let mut av = target_info[..target_info.len() - 4].to_vec();
    if opts.mic {
        av.extend_from_slice(&6u16.to_le_bytes());
        av.extend_from_slice(&4u16.to_le_bytes());
        av.extend_from_slice(&2u32.to_le_bytes());
    }
    av.extend_from_slice(&[0, 0, 0, 0]);
    let mut blob = vec![1, 1, 0, 0, 0, 0, 0, 0];
    blob.extend_from_slice(&[0; 8]);
    blob.extend_from_slice(&[0xAA; 8]);
    blob.extend_from_slice(&[0; 4]);
    blob.extend_from_slice(&av);
    blob.extend_from_slice(&[0; 4]);

    let proof = hmac_md5(&response_key, &[&server_challenge, &blob]);
    let kxkey = hmac_md5(&response_key, &[&proof]);
    let nt_response = [&proof[..], &blob].concat();
    let (exported, encrypted) = if opts.flags & NTLM_KEY_EXCH != 0 {
        let random = [0x42u8; 16];
        let mut enc = random;
        rc4(&kxkey, &mut enc);
        (random, enc.to_vec())
    } else {
        (kxkey, Vec::new())
    };

    let user = utf16le(user);
    let lm = [0u8; 24];
    let fields: [&[u8]; 6] = [&lm, &nt_response, &[], &user, &[], &encrypted];
    let header_len = if opts.mic { 88u32 } else { 72 };
    let mut msg = NTLMSSP_SIGNATURE.to_vec();
    msg.extend_from_slice(&3u32.to_le_bytes());
    let mut payload = Vec::new();
    for field in fields {
        let offset = header_len + payload.len() as u32;
        msg.extend_from_slice(&(field.len() as u16).to_le_bytes());
        msg.extend_from_slice(&(field.len() as u16).to_le_bytes());
        msg.extend_from_slice(&offset.to_le_bytes());
        payload.extend_from_slice(field);
    }
    msg.extend_from_slice(&opts.flags.to_le_bytes());
    msg.extend_from_slice(&[6, 1, 0, 0x1D, 0, 0, 0, 0x0F]);
    if opts.mic {
        msg.extend_from_slice(&[0u8; 16]);
    }
    msg.extend_from_slice(&payload);
    if opts.mic {
        let mic = hmac_md5(&exported, &[negotiate_msg, challenge_msg, &msg]);
        msg[72..88].copy_from_slice(&mic);
    }
    (msg, exported)
}

/// NTLMSSP_MESSAGE_SIGNATURE at sequence number 0, with extended session
/// security, 128-bit keys and key exchange.
pub fn ntlm_mac(exported: &[u8; 16], client_to_server: bool, message: &[u8]) -> [u8; 16] {
    let dir = if client_to_server {
        "client-to-server"
    } else {
        "server-to-client"
    };
    let sign_key = md5_of(&[
        exported,
        format!("session key to {dir} signing key magic constant\0").as_bytes(),
    ]);
    let seal_key = md5_of(&[
        exported,
        format!("session key to {dir} sealing key magic constant\0").as_bytes(),
    ]);
    let digest = hmac_md5(&sign_key, &[&0u32.to_le_bytes(), message]);
    let mut checksum = [0u8; 8];
    checksum.copy_from_slice(&digest[..8]);
    rc4(&seal_key, &mut checksum);
    let mut out = [0u8; 16];
    out[0] = 1;
    out[4..12].copy_from_slice(&checksum);
    out
}

// --- SMB2 -------------------------------------------------------------------

/// Sends `body` as `command` and returns the response frame.
pub async fn request(
    s: &mut TcpStream,
    command: Command,
    message_id: u64,
    session_id: u64,
    tree_id: u32,
    body: &[u8],
) -> Vec<u8> {
    let hdr = build_header(command, message_id, session_id, tree_id);
    send(s, hdr, body, None).await
}

/// Sends `hdr` and `body`, signed with an SMB 2.x `key` when one is given,
/// and returns the response frame.
pub async fn send(
    s: &mut TcpStream,
    mut hdr: Smb2Header,
    body: &[u8],
    key: Option<&[u8; 16]>,
) -> Vec<u8> {
    if key.is_some() {
        hdr.flags |= SMB2_FLAGS_SIGNED;
    }
    let mut msg = Vec::new();
    hdr.write(&mut msg).expect("header");
    msg.extend_from_slice(body);
    if let Some(key) = key {
        let sig = smb2_signature(&msg, key);
        msg[48..64].copy_from_slice(&sig);
    }
    s.write_all(&framed(&msg)).await.expect("write");
    read_frame(s).await
}

pub fn status(frame: &[u8]) -> u32 {
    parse_response_header(frame).0.channel_sequence_status
}

pub fn response_session_id(frame: &[u8]) -> u64 {
    parse_response_header(frame).0.session_id
}

pub const CTX_PREAUTH: u16 = 0x0001;
pub const CTX_ENCRYPTION: u16 = 0x0002;
pub const CTX_SIGNING: u16 = 0x0008;

/// SMB2_PREAUTH_INTEGRITY_CAPABILITIES data with the given hash ids.
pub fn preauth_context(hashes: &[u16]) -> (u16, Vec<u8>) {
    let mut data = (hashes.len() as u16).to_le_bytes().to_vec();
    data.extend_from_slice(&32u16.to_le_bytes());
    for h in hashes {
        data.extend_from_slice(&h.to_le_bytes());
    }
    data.extend_from_slice(&[0x11; 32]);
    (CTX_PREAUTH, data)
}

/// Context data made of a u16 count and u16 ids, as in
/// SMB2_SIGNING_CAPABILITIES and SMB2_ENCRYPTION_CAPABILITIES.
pub fn id_list_context(context_type: u16, ids: &[u16]) -> (u16, Vec<u8>) {
    let mut data = (ids.len() as u16).to_le_bytes().to_vec();
    for id in ids {
        data.extend_from_slice(&id.to_le_bytes());
    }
    (context_type, data)
}

/// SMB 3.1.1 NEGOTIATE request body with `contexts`.
pub fn negotiate_311_body(dialects: &[u16], contexts: &[(u16, Vec<u8>)]) -> Vec<u8> {
    let fixed_end = 64 + 36 + 2 * dialects.len();
    let offset = (fixed_end + 7) & !7;
    let req = NegotiateRequest {
        structure_size: 36,
        dialect_count: dialects.len() as u16,
        security_mode: 0x0001,
        reserved: 0,
        capabilities: 0,
        client_guid: [0xCD; 16],
        negotiate_context_offset_or_client_start_time: offset as u64
            | ((contexts.len() as u64) << 32),
        dialects: dialects.to_vec(),
    };
    let mut body = Vec::new();
    req.write_to(&mut body).expect("negotiate");
    for (i, (context_type, data)) in contexts.iter().enumerate() {
        let target = if i == 0 {
            offset - 64
        } else {
            (body.len() + 7) & !7
        };
        body.resize(target, 0);
        body.extend_from_slice(&context_type.to_le_bytes());
        body.extend_from_slice(&(data.len() as u16).to_le_bytes());
        body.extend_from_slice(&[0; 4]);
        body.extend_from_slice(data);
    }
    body
}

/// (type, data) of each negotiate context in a NEGOTIATE response frame.
pub fn response_contexts(frame: &[u8]) -> Vec<(u16, Vec<u8>)> {
    let count = u16::from_le_bytes([frame[64 + 6], frame[64 + 7]]) as usize;
    let mut at = u32::from_le_bytes(frame[64 + 60..64 + 64].try_into().unwrap()) as usize;
    let mut out = Vec::new();
    for _ in 0..count {
        at = (at + 7) & !7;
        let context_type = u16::from_le_bytes([frame[at], frame[at + 1]]);
        let len = u16::from_le_bytes([frame[at + 2], frame[at + 3]]) as usize;
        out.push((context_type, frame[at + 8..at + 8 + len].to_vec()));
        at += 8 + len;
    }
    out
}

/// Security buffer of a SESSION_SETUP response.
pub fn session_setup_token(frame: &[u8]) -> Vec<u8> {
    let (_, body) = parse_response_header(frame);
    SessionSetupResponse::parse(body)
        .expect("session setup response")
        .security_buffer
}

/// A finished NTLM login.
pub struct Login {
    pub status: u32,
    pub session_id: u64,
    pub key: [u8; 16],
    /// The final response frame.
    pub frame: Vec<u8>,
}

/// Two-round SPNEGO NTLM login as `user`, sending a mechListMIC. Uses
/// message ids `mid` and `mid + 1`.
pub async fn spnego_login(
    s: &mut TcpStream,
    mid: u64,
    session_id: u64,
    user: &str,
    password: &str,
) -> Login {
    login_with(s, mid, session_id, user, password, LoginOptions::default()).await
}

/// Knobs for [`login_with`].
#[derive(Clone, Copy)]
pub struct LoginOptions {
    pub raw: bool,
    pub security_mode: u8,
    pub flags: u8,
    pub previous_session_id: u64,
    pub auth: AuthOptions,
}

impl Default for LoginOptions {
    fn default() -> Self {
        Self {
            raw: false,
            security_mode: 0x01,
            flags: 0,
            previous_session_id: 0,
            auth: AuthOptions::default(),
        }
    }
}

pub async fn login_with(
    s: &mut TcpStream,
    mid: u64,
    session_id: u64,
    user: &str,
    password: &str,
    opts: LoginOptions,
) -> Login {
    let negotiate = ntlm_negotiate(opts.auth.flags);
    let mech_list = mech_type_list(&[OID_NTLMSSP]);
    let first = if opts.raw {
        negotiate.clone()
    } else {
        spnego_init(&mech_list, Some(&negotiate))
    };
    let body = session_setup_body(&first, opts.flags, opts.security_mode, 0);
    let frame = request(s, Command::SessionSetup, mid, session_id, 0, &body).await;
    let st = status(&frame);
    if st != STATUS_MORE_PROCESSING_REQUIRED {
        return Login {
            status: st,
            session_id: response_session_id(&frame),
            key: [0; 16],
            frame,
        };
    }
    let sid = response_session_id(&frame);
    let token = session_setup_token(&frame);
    let challenge = if opts.raw {
        token
    } else {
        parse_neg_resp(&token).token.expect("challenge")
    };
    let (auth, key) = ntlm_authenticate(user, password, &negotiate, &challenge, opts.auth);
    let second = if opts.raw {
        auth
    } else {
        let mic = ntlm_mac(&key, true, &mech_list);
        spnego_resp(Some(&auth), Some(&mic))
    };
    let body = session_setup_body(
        &second,
        opts.flags,
        opts.security_mode,
        opts.previous_session_id,
    );
    let frame = request(s, Command::SessionSetup, mid + 1, sid, 0, &body).await;
    Login {
        status: status(&frame),
        session_id: sid,
        key,
        frame,
    }
}

/// FileId of a successful CREATE response frame.
pub fn created_file_id(frame: &[u8]) -> FileId {
    assert_eq!(status(frame), 0, "CREATE failed");
    smb_server::wire::messages::CreateResponse::parse(&frame[64..])
        .expect("create response")
        .file_id
}

/// IOCTL request body.
pub fn ioctl_body(
    ctl_code: u32,
    file_id: FileId,
    input: &[u8],
    max_output: u32,
    flags: u32,
) -> Vec<u8> {
    let mut body = Vec::new();
    IoctlRequest {
        structure_size: 57,
        reserved: 0,
        ctl_code,
        file_id,
        input_offset: if input.is_empty() { 0 } else { 0x78 },
        input_count: input.len() as u32,
        max_input_response: 0,
        output_offset: 0,
        output_count: 0,
        max_output_response: max_output,
        flags,
        reserved2: 0,
        input: input.to_vec(),
    }
    .write_to(&mut body)
    .expect("ioctl");
    body
}

/// VALIDATE_NEGOTIATE_INFO request input.
pub fn validate_negotiate_input(
    capabilities: u32,
    guid: [u8; 16],
    security_mode: u16,
    dialects: &[u16],
) -> Vec<u8> {
    let mut out = capabilities.to_le_bytes().to_vec();
    out.extend_from_slice(&guid);
    out.extend_from_slice(&security_mode.to_le_bytes());
    out.extend_from_slice(&(dialects.len() as u16).to_le_bytes());
    for d in dialects {
        out.extend_from_slice(&d.to_le_bytes());
    }
    out
}

/// Reads a frame, or returns `None` once the server closes the connection.
pub async fn read_or_closed(s: &mut TcpStream) -> Option<Vec<u8>> {
    match tokio::time::timeout(std::time::Duration::from_secs(5), try_read_frame(s)).await {
        Ok(frame) => frame.ok(),
        Err(_) => panic!("server neither replied nor closed the connection"),
    }
}