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/.
151 lines
5.4 KiB
Go
151 lines
5.4 KiB
Go
package shell
|
|
|
|
import (
|
|
"bufio"
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
"os/signal"
|
|
"strings"
|
|
"syscall"
|
|
|
|
"github.com/sresarehumantoo/wsmancer/internal/cli"
|
|
"github.com/sresarehumantoo/wsmancer/internal/output"
|
|
"github.com/sresarehumantoo/wsmancer/internal/session"
|
|
"github.com/sresarehumantoo/wsmancer/internal/transcript"
|
|
)
|
|
|
|
// NewBatch builds a Shell for batch mode (`-x -`): commands are read from a
|
|
// stream and run through the same dispatch as the REPL, over one connection.
|
|
//
|
|
// It is the multi-command sibling of NewOnce, and the difference from it is
|
|
// deliberate. once stays false, because batch is a *session*: there is a next
|
|
// command to protect, so a forgotten shell is recovered onto and the run
|
|
// continues, rather than being merely reported and abandoned. Like NewOnce it
|
|
// leaves sh.ed and sh.hist nil, Batch reads the stream itself and a prompt
|
|
// history has no reader, so Batch must reach nothing that touches them.
|
|
func NewBatch(s *session.Session, o *cli.Options, stdout io.Writer) *Shell {
|
|
return newShell(s, o, stdout)
|
|
}
|
|
|
|
// Batch reads commands from r, one per line, and runs each through the shell's
|
|
// own dispatch over a single session, the streaming form of -x. It returns the
|
|
// process exit code: 0 if every command succeeded, 1 if any failed.
|
|
//
|
|
// This is the reason the port claims to beat "nxc-adhoc clunkiness": an agent or
|
|
// a script drives a whole session over one authenticated connection, so the
|
|
// runspace state (working directory, variables, functions, -s scripts,
|
|
// in-memory assemblies) persists across commands, and each command's structured
|
|
// result streams out as it completes, one NDJSON object per line under -o json.
|
|
//
|
|
// It shares dispatch with the REPL and the one-shot, so a built-in behaves
|
|
// identically in all three; the only things batch owns are the aggregate exit
|
|
// code and the stream-reading loop. A blank line is skipped, and `exit`/`quit`
|
|
// ends the batch early (dispatch's stop), matching the interactive shell.
|
|
func (sh *Shell) Batch(ctx context.Context, r io.Reader) int {
|
|
// Ctrl-C terminates the running command's remote pipeline, exactly as in the
|
|
// REPL and the one-shot, the session survives so the next line still runs.
|
|
sigs := make(chan os.Signal, 1)
|
|
signal.Notify(sigs, os.Interrupt, syscall.SIGTERM)
|
|
defer signal.Stop(sigs)
|
|
|
|
sh.startBatchLog()
|
|
defer sh.log.Close()
|
|
|
|
// Same 1 MiB line cap as the piped plainEditor, so a long one-liner (a
|
|
// base64 blob pasted as a command) reads the same way in both paths.
|
|
sc := bufio.NewScanner(r)
|
|
sc.Buffer(make([]byte, 0, 64*1024), 1024*1024)
|
|
|
|
anyFailed := false
|
|
for sc.Scan() {
|
|
raw := strings.TrimSpace(sc.Text())
|
|
if raw == "" {
|
|
continue
|
|
}
|
|
// A line may be a JSON request object {"id":..., "command":"..."} for
|
|
// correlation, or a bare command line. parseRequest tells them apart and
|
|
// hands back the command to run plus the id to echo on every object it
|
|
// produces; reqID is cleared after so a bare line that follows a request
|
|
// carries no id.
|
|
line, id := parseRequest(raw)
|
|
sh.reqID = id
|
|
sh.log.Event("PS %s> %s", sh.cwd, line)
|
|
|
|
// failed describes the last command; reset before each so one failure
|
|
// does not leak its verdict onto the next line's success.
|
|
sh.failed = false
|
|
stop, err := sh.dispatch(ctx, sigs, line)
|
|
if err != nil {
|
|
sh.reportErr(line, err)
|
|
}
|
|
sh.reqID = nil
|
|
if sh.failed {
|
|
anyFailed = true
|
|
}
|
|
if stop {
|
|
break
|
|
}
|
|
}
|
|
if err := sc.Err(); err != nil {
|
|
// A read error on the command stream is not a command failure, but the
|
|
// caller should still hear it and get a non-zero code.
|
|
if sh.json {
|
|
_ = output.RenderEvent(sh.out, "input_error", "reading commands: "+err.Error(), false)
|
|
} else {
|
|
fmt.Fprintln(os.Stderr, sh.c.Red("Error: reading commands: "+err.Error()))
|
|
}
|
|
return 1
|
|
}
|
|
if anyFailed {
|
|
return 1
|
|
}
|
|
return 0
|
|
}
|
|
|
|
// parseRequest interprets one batch input line. It returns the command to run
|
|
// and the correlation id to echo on the response.
|
|
//
|
|
// A line is a structured request only when it is a JSON object with a non-empty
|
|
// string "command", then its "id" (any JSON value) is the correlation token.
|
|
// Anything else is a bare command line run verbatim with no id: a line that does
|
|
// not start with "{", one that is not valid JSON (a PowerShell scriptblock
|
|
// `{ ... }` is not), or a JSON object without a usable command. This keeps
|
|
// bare-line batch working unchanged while letting an agent opt into correlation
|
|
// per line, and the "{" + valid-JSON + string-command test is strict enough that
|
|
// an ordinary command is never mistaken for a request.
|
|
func parseRequest(raw string) (command string, id interface{}) {
|
|
if !strings.HasPrefix(raw, "{") {
|
|
return raw, nil
|
|
}
|
|
var r struct {
|
|
ID interface{} `json:"id"`
|
|
Command string `json:"command"`
|
|
}
|
|
if err := json.Unmarshal([]byte(raw), &r); err != nil || r.Command == "" {
|
|
return raw, nil
|
|
}
|
|
return r.Command, r.ID
|
|
}
|
|
|
|
// startBatchLog opens the -l transcript for a batch session. Like startOnceLog
|
|
// it omits the "session log: <path>" line that belongs on a prompt, not in a
|
|
// stream a consumer is about to parse; the per-command lines are written by the
|
|
// loop.
|
|
func (sh *Shell) startBatchLog() {
|
|
if !sh.o.Log {
|
|
return
|
|
}
|
|
lg, err := transcript.Open(sh.o.IP)
|
|
if err != nil {
|
|
if !sh.json {
|
|
fmt.Fprintln(os.Stderr, sh.c.Yellow("Warning: could not open session log: "+err.Error()))
|
|
}
|
|
return
|
|
}
|
|
sh.log = lg
|
|
sh.log.Event("=== batch session: %s (%s) ===", sh.o.IP, sh.s.AuthMode())
|
|
}
|