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

96 lines
3.4 KiB
Go

package shell
import (
"slices"
"testing"
"github.com/sresarehumantoo/wsmancer/internal/cli"
)
func TestParsePSCompletion(t *testing.T) {
idx, cands := parsePSCompletion([]string{"I|12", "C|-Name", "C|-Id", "C|-InputObject"})
if idx != 12 {
t.Errorf("index = %d, want 12", idx)
}
if want := []string{"-Name", "-Id", "-InputObject"}; !slices.Equal(cands, want) {
t.Errorf("cands = %v, want %v", cands, want)
}
}
// Without a replacement index there is no safe place to splice the candidate, so
// the whole answer must be discarded rather than guessed at.
func TestParsePSCompletionRequiresAnIndex(t *testing.T) {
if idx, cands := parsePSCompletion([]string{"C|-Name"}); idx != 0 || cands != nil {
t.Errorf("got (%d, %v), want (0, nil) when no index was reported", idx, cands)
}
// Index 0 is legitimate: completing the very start of the line.
if idx, cands := parsePSCompletion([]string{"I|0", "C|Get-Process"}); idx != 0 || len(cands) != 1 {
t.Errorf("got (%d, %v), want index 0 with one candidate", idx, cands)
}
}
func TestParsePSCompletionIgnoresNoise(t *testing.T) {
idx, cands := parsePSCompletion([]string{"warning text with no pipe", "I|3", "C|-Name", ""})
if idx != 3 || !slices.Equal(cands, []string{"-Name"}) {
t.Errorf("got (%d, %v), want (3, [-Name])", idx, cands)
}
}
// PowerShell hands back text ready to paste, so a value with a space arrives
// already single-quoted. Normalising to the raw value is what lets it flow
// through the same quoting pipeline as every other candidate instead of being
// wrapped a second time.
func TestUnquotePS(t *testing.T) {
cases := map[string]string{
`'Windows Update'`: "Windows Update",
`'C:\Program Files'`: `C:\Program Files`,
`conhost`: "conhost",
`-Name`: "-Name",
`'it''s'`: "it's",
`"quoted"`: "quoted",
`'`: `'`,
``: ``,
`'unterminated`: `'unterminated`,
`C:\NoSpaces\file.txt`: `C:\NoSpaces\file.txt`,
}
for in, want := range cases {
if got := unquotePS(in); got != want {
t.Errorf("unquotePS(%q) = %q, want %q", in, got, want)
}
}
}
// The cursor and the replacement index cross the wire as character counts, but
// the shell splices by byte offset. They differ the moment a line holds a
// non-ASCII rune, and getting it wrong splices mid-rune.
func TestByteOffsetForRune(t *testing.T) {
// 'ü' is rune 5 and occupies bytes 5-6, so every rune after it sits one byte
// further along than its index: rune 6 ('c') is byte 7, and the end of the
// 14-rune string is byte 15.
const s = "Ge-Prücess -Na"
cases := map[int]int{0: 0, 1: 1, 5: 5, 6: 7, 7: 8, 14: 15}
for runeIdx, want := range cases {
if got := byteOffsetForRune(s, runeIdx); got != want {
t.Errorf("byteOffsetForRune(%q, %d) = %d, want %d", s, runeIdx, got, want)
}
}
if got := byteOffsetForRune("abc", 3); got != 3 {
t.Errorf("end-of-string index = %d, want 3", got)
}
for _, bad := range []int{-1, 4, 99} {
if got := byteOffsetForRune("abc", bad); got != -1 {
t.Errorf("byteOffsetForRune(abc, %d) = %d, want -1", bad, got)
}
}
}
// -N disables remote completion entirely, this site included.
func TestPSCompleteRespectsNoRPath(t *testing.T) {
sh := cachedShell(optsNoRPath())
if head, cands := sh.psComplete("Get-Process -", 13); head != 0 || cands != nil {
t.Errorf("got (%d, %v) with -N, want (0, nil)", head, cands)
}
}
func optsNoRPath() *cli.Options { return &cli.Options{NoRPath: true} }