Files
sresarehumantoo 2f1dbda6e1 wsmancer: initial public release (0.1.0)
An interactive WinRM shell for offensive Windows work, speaking the
PowerShell Remoting Protocol end to end. Command results come back as typed
objects (structured JSON on demand), and it authenticates against stock,
locked-down listeners: NTLM and Kerberos with WinRM message sealing that is
integrity-checked in both directions, pass-the-hash, and certificate auth.

Highlights:
  - -o json structured output; -x one-shot and -x - batch/session mode over
    one connection, built for automation as well as a prompt
  - context-aware Tab completion (local/remote paths, the target's own cmdlet
    names, and parameters/values via the target's TabExpansion2), ghost text
  - a session that recovers a dropped connection, and detects a shell the
    target has forgotten
  - the full evil-winrm post-ex set: -s scripts + menu, services,
    invoke-binary, donut-loader, bypass-4msi
  - SOCKS5 pivoting, TLS verification (--ca-cert / --accept-fingerprint /
    --insecure), client certs / mutual TLS

Every claim is live-verified against a real WinRM target; see
docs/verification.md. A Go port of Hackplayers/evil-winrm (LGPL-3.0), with all
20 of its flags and every post-ex command; PSRP transport builds on a patched
smnsjas/go-psrp (MIT), vendored under go-psrp-fork/.
2026-08-12 20:39:57 -04:00

110 lines
3.7 KiB
Go

package cli
import (
"os"
"path/filepath"
"strings"
"testing"
)
// certPair writes two files so the -c/-k existence checks pass; their contents
// never matter to option parsing.
func certPair(t *testing.T) (string, string) {
t.Helper()
dir := t.TempDir()
pub, priv := filepath.Join(dir, "client.crt"), filepath.Join(dir, "client.key")
for _, p := range []string{pub, priv} {
if err := os.WriteFile(p, []byte("x"), 0o600); err != nil {
t.Fatalf("write %s: %v", p, err)
}
}
return pub, priv
}
func parse(t *testing.T, args ...string) (*Options, error) {
t.Helper()
return Parse(args, os.Stderr)
}
// A certificate on its own is the credential. This is the mode Ruby evil-winrm
// means by -c/-k, and the reason -u is not required with it: the target maps the
// certificate to an account, so the client never names one.
func TestCertificateAloneSelectsCertificateAuth(t *testing.T) {
pub, priv := certPair(t)
o, err := parse(t, "-i", "host", "-S", "-c", pub, "-k", priv)
if err != nil {
t.Fatalf("parse: %v", err)
}
if o.AuthMode() != AuthCertificate {
t.Errorf("AuthMode = %v, want AuthCertificate", o.AuthMode())
}
// The password prompt must not fire here: there is no password to ask for,
// and asking would stall a scripted cert login at a prompt.
if o.NeedsPassword() {
t.Error("a certificate login should never be prompted for a password")
}
}
// The pre-existing behavior, a client certificate presented *alongside* NTLM
// or Kerberos, is a different and stronger thing than upstream's cert auth, and
// carrying credentials must keep selecting it.
func TestCertificateWithCredentialsKeepsTheCredentialMode(t *testing.T) {
pub, priv := certPair(t)
cases := map[string]struct {
args []string
want AuthMode
}{
"with a password": {[]string{"-u", "u", "-p", "pw"}, AuthPassword},
"with a hash": {[]string{"-u", "u", "-H", "aabb"}, AuthHash},
"with a ticket": {[]string{"-K", "/dev/null"}, AuthKerberos},
}
for name, tc := range cases {
args := append([]string{"-i", "host", "-S", "-c", pub, "-k", priv}, tc.args...)
o, err := parse(t, args...)
if err != nil {
t.Fatalf("%s: parse: %v", name, err)
}
if got := o.AuthMode(); got != tc.want {
t.Errorf("%s: AuthMode = %v, want %v", name, got, tc.want)
}
}
}
// $WSMANCER_PASSWORD is read before the mode is decided, so an exported
// password would otherwise turn a cert login into an NTLM one without a word.
func TestCertificateAuthIsNotDerailedByThePasswordEnvVar(t *testing.T) {
pub, priv := certPair(t)
t.Setenv("WSMANCER_PASSWORD", "leftover")
o, err := parse(t, "-i", "host", "-S", "-c", pub, "-k", priv, "-u", "u")
if err != nil {
t.Fatalf("parse: %v", err)
}
if o.AuthMode() != AuthPassword {
t.Errorf("AuthMode = %v, want AuthPassword: an exported password is still a password",
o.AuthMode())
}
}
// -u is required for the NTLM modes and not for the two that never name a user.
func TestUserRequirementFollowsTheMode(t *testing.T) {
pub, priv := certPair(t)
if _, err := parse(t, "-i", "host", "-S", "-c", pub, "-k", priv); err != nil {
t.Errorf("certificate auth should not require -u: %v", err)
}
_, err := parse(t, "-i", "host", "-p", "pw")
if err == nil || !strings.Contains(err.Error(), "-u/--user is required") {
t.Errorf("a password login without -u should be refused, got: %v", err)
}
}
// A certificate is only ever presented in a TLS handshake, so without -S there
// is no handshake to present it in.
func TestCertificateAuthRequiresTLS(t *testing.T) {
pub, priv := certPair(t)
_, err := parse(t, "-i", "host", "-c", pub, "-k", priv)
if err == nil || !strings.Contains(err.Error(), "-S/--ssl") {
t.Errorf("cert auth without -S should be refused, got: %v", err)
}
}