winrm-pentest 0.1.0

Async WinRM 2.0 (WS-Management) client for offensive security — NTLM/Kerberos/CredSSP auth, remote PowerShell, encoded-command exec, SMB-free file upload. Cross-platform. Pre-alpha
Documentation
//! PowerShell helpers: `-EncodedCommand` framing (mandatory UTF-16LE base64)
//! and an opt-in deflate-then-b64 payload that expands via a
//! `[System.IO.Compression]` one-liner.
//!
//! Evil-WinRM uses the plain UTF-16LE variant. Deflated payloads are useful
//! when the caller has multi-KB scripts and wants to keep the SOAP envelope
//! small — WSMan caps `MaxEnvelopeSize` at 512 KB by default.

use base64::{engine::general_purpose::STANDARD as B64, Engine};
use flate2::write::DeflateEncoder;
use flate2::Compression;
use std::io::Write;

/// UTF-16LE, then base64 — the wire format `powershell.exe -EncodedCommand`
/// accepts. Return value drops straight into `run_command("powershell", &["-NoProfile","-EncodedCommand", &out])`.
pub fn encode_ps_command(script: &str) -> String {
    let utf16: Vec<u8> = script.encode_utf16().flat_map(u16::to_le_bytes).collect();
    B64.encode(&utf16)
}

/// Deflate `script` (UTF-8), base64-wrap it, and emit a self-inflating
/// PowerShell one-liner. The receiver runs:
///   `iex ([Text.Encoding]::UTF8.GetString([IO.Compression.DeflateStream]::new(
///        [IO.MemoryStream]::new([Convert]::FromBase64String("...")),
///        [IO.Compression.CompressionMode]::Decompress
///    ).GetBuffer()))`
///
/// The returned string is the one-liner itself, ready to feed to
/// [`encode_ps_command`].
pub fn encode_ps_deflated(script: &str) -> String {
    let mut enc = DeflateEncoder::new(Vec::new(), Compression::default());
    enc.write_all(script.as_bytes()).expect("in-memory write");
    let deflated = enc.finish().expect("in-memory finish");
    let b64 = B64.encode(&deflated);
    format!(
        "$b=[Convert]::FromBase64String('{b64}');\
         $s=New-Object IO.MemoryStream(,$b);\
         $d=New-Object IO.Compression.DeflateStream($s,[IO.Compression.CompressionMode]::Decompress);\
         $r=New-Object IO.StreamReader($d);\
         iex $r.ReadToEnd()"
    )
}

/// Result surface for `invoke_ps`.
#[derive(Debug, Clone)]
pub struct ExecResult {
    pub stdout: String,
    pub stderr: String,
    pub exit_code: i32,
}