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

95 lines
3.9 KiB
Go

package shell
import (
"context"
"strings"
"time"
"github.com/sresarehumantoo/wsmancer/internal/output"
)
// remoteCommandScript lists every command the target can run, cmdlets,
// functions, aliases and applications alike, which is exactly the set a real
// PowerShell console completes. Projecting to $_.Name mirrors the two existing
// remote listers rather than relying on how a CommandInfo stringifies.
//
// A broken module makes Get-Command emit error records for that module while
// still returning everything else, so errors are tolerated as long as names came
// back (see remoteCommandNames).
const remoteCommandScript = `Get-Command -ErrorAction SilentlyContinue | ForEach-Object { $_.Name }`
// remoteCommandTimeout bounds the one fetch. It matches the budget RemotePaths
// uses for a directory listing: Tab must never make the prompt hang, so a target
// that cannot answer in time simply yields no cmdlet candidates.
const remoteCommandTimeout = 5 * time.Second
// remoteCommandRetry is how long to wait before trying again after a failed
// fetch. Latching the failure for the whole session would let one transport
// hiccup cost the operator cmdlet completion until they reconnect; retrying on
// every Tab would instead pay the timeout over and over. A cooldown does neither.
const remoteCommandRetry = 30 * time.Second
// remoteCommandNames returns the target's command names, fetching them once and
// caching them for the session. ~1900 names on a stock Server 2016 box, one
// ~0.4s round trip, after which filtering is local and instant, which is why
// this is a cache rather than a per-keystroke query.
//
// It never returns an error: a failure means no cmdlet candidates, never a
// disturbed prompt. The shell's own built-ins are added separately by the
// caller, so they keep completing regardless.
func (sh *Shell) remoteCommandNames() []string {
// -N/--no-rpath-completion promises to disable remote completion entirely,
// not merely remote *paths*, so it suppresses these candidates outright,
// even a cache warmed before the check, since the flag is about what the
// operator is willing to offer, not about saving a round trip.
if sh.o == nil || sh.o.NoRPath {
return nil
}
// The cache is just data; only *filling* it needs a session. Checking the
// session first would make a warm cache unusable, which is both wrong and
// untestable without a live target.
if len(sh.remoteCmds) > 0 {
return sh.remoteCmds
}
if sh.s == nil || !sh.commandFetchDue() {
return nil
}
sh.remoteCmdsAt = time.Now()
ctx, cancel := context.WithTimeout(context.Background(), remoteCommandTimeout)
defer cancel()
res, err := sh.s.RunRaw(ctx, remoteCommandScript)
if err != nil || res == nil {
return nil
}
var names []string
for _, v := range res.Output {
for _, ln := range strings.Split(output.Text(v), "\n") {
if s := strings.TrimSpace(ln); s != "" {
names = append(names, s)
}
}
}
// Deliberately not gated on res.Errors: a single unloadable module produces
// error records alongside a perfectly good list, and dropping the list then
// would cost completion on exactly the crusty boxes it helps most.
sh.remoteCmds = names
return names
}
// commandFetchDue reports whether enough time has passed since the last failed
// attempt to try again. Split out from remoteCommandNames so the back-off policy
// is testable on its own, without a session to fetch through.
func (sh *Shell) commandFetchDue() bool {
return sh.remoteCmdsAt.IsZero() || time.Since(sh.remoteCmdsAt) >= remoteCommandRetry
}
// invalidateRemoteCommands drops the cache so the next completion refetches it.
// Called from `menu`, the operator's explicit "re-read what this session can
// run" action, the moment after an Import-Module or a script load when the
// command surface may have grown.
func (sh *Shell) invalidateRemoteCommands() {
sh.remoteCmds = nil
sh.remoteCmdsAt = time.Time{}
}