ms-nrpc 0.1.0-dev

MS-NRPC (Netlogon) secure-channel setup + read-only Zerologon (CVE-2020-1472) detection. Defensive-only: destructive exploit/restore paths are deliberately excluded.
Documentation
//! Read-only Zerologon (CVE-2020-1472) probe.
//!
//! With AES-CFB8 and an all-zero IV, encrypting an all-zero plaintext yields
//! all-zero ciphertext with probability ~1/256. `NetrServerAuthenticate3`
//! verifies the client credential by computing exactly that, so sending an
//! all-zero `ClientChallenge` and all-zero `ClientCredential` and retrying
//! makes the KDC accept an *unauthenticated* secure channel roughly once in
//! 256 attempts. If any `NetrServerAuthenticate3` returns `STATUS_SUCCESS`,
//! the DC is vulnerable.
//!
//! This module **only detects**. It never calls `NetrServerPasswordSet2` —
//! the destructive step that zeroes the machine password and breaks the DC.
//! Detection ≠ exploitation.

use dcerpc::epm;
use dcerpc::transport::RpcTcp;
use dcerpc::Result;

use crate::secure_channel::{
    encode_authenticate3, encode_req_challenge, netlogon_syntax, opnum, ret_status, STATUS_SUCCESS,
};

/// Outcome of a Zerologon probe.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Zerologon {
    /// A `NetrServerAuthenticate3` accepted the all-zero credential — the DC
    /// is vulnerable to CVE-2020-1472.
    Vulnerable {
        /// 1-based index of the attempt that succeeded.
        attempts: u32,
    },
    /// Every attempt was rejected (typically `STATUS_ACCESS_DENIED`) — the DC
    /// is patched, or the AES negotiation the check depends on is otherwise
    /// unavailable.
    NotVulnerable {
        /// How many attempts were made before giving up.
        attempts: u32,
    },
}

/// Safe Zerologon detection: bind Netlogon over `ncacn_ip_tcp` and try the
/// all-zero challenge / credential handshake up to `max_attempts` (impacket
/// uses 2000; success is expected within ~256 on a vulnerable DC). Returns as
/// soon as one attempt succeeds. Never resets the machine password.
///
/// `host` is the DC address (name or IP); `netbios` is the DC's NetBIOS name
/// (without the trailing `$`).
pub async fn detect_zerologon(host: &str, netbios: &str, max_attempts: u32) -> Result<Zerologon> {
    let port = epm::resolve_port(host, netlogon_syntax()).await?;
    let mut rpc = RpcTcp::connect(&format!("{host}:{port}")).await?;
    rpc.bind(netlogon_syntax()).await?;

    let zero = [0u8; 8];
    for attempt in 1..=max_attempts {
        // Fresh challenge pair, then try to authenticate with all-zeros.
        let _ = rpc
            .call(opnum::REQ_CHALLENGE, &encode_req_challenge(netbios, &zero))
            .await?;
        let auth = rpc
            .call(opnum::AUTHENTICATE3, &encode_authenticate3(netbios, &zero))
            .await?;
        if ret_status(&auth) == STATUS_SUCCESS {
            return Ok(Zerologon::Vulnerable { attempts: attempt });
        }
    }
    Ok(Zerologon::NotVulnerable {
        attempts: max_attempts,
    })
}