mirror of
https://github.com/sresarehumantoo/wsmancer.git
synced 2026-11-24 05:49:16 +01:00
2f1dbda6e1
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/.
119 lines
4.4 KiB
Go
119 lines
4.4 KiB
Go
package shell
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"github.com/sresarehumantoo/wsmancer/internal/output"
|
|
)
|
|
|
|
// scriptChunk is the base64 payload appended per command while streaming a
|
|
// payload into a remote variable. It must stay well inside the target's
|
|
// MaxEnvelopeSizekb (500KB default) after PSRP framing; 60 KiB leaves ample
|
|
// headroom.
|
|
const scriptChunk = 60 * 1024
|
|
|
|
// errPayloadCancelled reports a Ctrl-C during the streaming phase. It wraps
|
|
// context.Canceled so that reportErr stays silent: sendB64Var has already told
|
|
// the operator what happened, and every caller re-wraps this with %w.
|
|
var errPayloadCancelled = fmt.Errorf("canceled before the payload was run: %w", context.Canceled)
|
|
|
|
// sendB64Var streams a base64 string into a global variable on the target in
|
|
// envelope-sized chunks, so a payload larger than one PSRP message (a big script
|
|
// or assembly) can be assembled remotely before use. The caller removes the
|
|
// variable afterwards.
|
|
//
|
|
// Ctrl-C aborts the stream between chunks. It deliberately does *not* cancel the
|
|
// in-flight chunk's context, for two reasons. A chunk is one small string append,
|
|
// so waiting for it costs a single round trip, less than the ~0.74s a
|
|
// StopPipelines terminate would cost, and it leaves no abandoned pipeline to
|
|
// terminate, which the one-runspace pool would otherwise queue the caller's
|
|
// cleanup behind. More importantly, canceling mid-Execute is *undetectable
|
|
// here*: the fork returns (result, nil) with "context canceled" as a plain string
|
|
// in Result.Errors, so the err check below would see success and, on the last
|
|
// chunk, hand the caller a silently truncated payload.
|
|
//
|
|
// Aborting is always safe: nothing has been executed at this point, the payload
|
|
// is only accumulating in a variable, so a cancel costs the upload and nothing
|
|
// else. That is exactly what an operator who changes their mind mid-`donut-loader`
|
|
// needs.
|
|
func (sh *Shell) sendB64Var(ctx context.Context, sigs <-chan os.Signal, name, b64 string) error {
|
|
err := streamB64Var(name, b64, sigs, func(script string) error {
|
|
_, runErr := sh.s.RunRaw(ctx, script)
|
|
return runErr
|
|
})
|
|
if errors.Is(err, errPayloadCancelled) && !sh.json {
|
|
fmt.Fprintln(sh.out, sh.c.Yellow("^C (canceled; nothing was run on the target)"))
|
|
}
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return sh.verifyB64Var(ctx, name, len(b64))
|
|
}
|
|
|
|
// streamB64Var is the chunk loop, split out from the session so it can be tested
|
|
// without a live target. run sends one command; sigs, when it fires, aborts the
|
|
// stream.
|
|
//
|
|
// The signal is checked *after* each chunk rather than before, so that a Ctrl-C
|
|
// arriving during the final chunk is still consumed here. Checking only at the
|
|
// top of the loop would leave that signal buffered (the channel has cap 1 and
|
|
// nothing drains it between commands) for the next executeAs to pick up, which
|
|
// would then terminate a command the operator never meant to stop, the original
|
|
// bug this guards against.
|
|
func streamB64Var(name, b64 string, sigs <-chan os.Signal, run func(script string) error) error {
|
|
first := true
|
|
for len(b64) > 0 {
|
|
n := scriptChunk
|
|
if n > len(b64) {
|
|
n = len(b64)
|
|
}
|
|
part := b64[:n]
|
|
b64 = b64[n:]
|
|
op := "+="
|
|
if first {
|
|
op = "="
|
|
first = false
|
|
}
|
|
// The base64 alphabet contains no single quote, so quoting is safe.
|
|
if err := run(fmt.Sprintf("$global:%s %s '%s'", name, op, part)); err != nil {
|
|
return err
|
|
}
|
|
select {
|
|
case <-sigs:
|
|
return errPayloadCancelled
|
|
default:
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// verifyB64Var checks that the remote variable holds every character that was
|
|
// sent. A dropped or short chunk otherwise surfaces only much later, as a
|
|
// base64-decode failure for a script, or, for donut-loader, not at all: a
|
|
// truncated shellcode blob is injected and executed just the same. One round trip
|
|
// buys the same guarantee the file-transfer path gets from its SHA-256 check.
|
|
func (sh *Shell) verifyB64Var(ctx context.Context, name string, want int) error {
|
|
res, err := sh.s.RunRaw(ctx, fmt.Sprintf(`'{0}' -f $global:%s.Length`, name))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
for _, v := range res.Output {
|
|
got, convErr := strconv.Atoi(strings.TrimSpace(output.Text(v)))
|
|
if convErr != nil {
|
|
continue
|
|
}
|
|
if got != want {
|
|
return fmt.Errorf("payload truncated in transit: the target holds %d of %d bytes", got, want)
|
|
}
|
|
return nil
|
|
}
|
|
// An unparseable answer is not treated as failure: better to let the payload
|
|
// run than to block on a quirk of the target's formatting.
|
|
return nil
|
|
}
|