aboutsummaryrefslogtreecommitdiff
path: root/cmd/wmi.go
blob: 59096ec3415ce8d910e93866f9b3914186a32b2c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package cmd

import (
  "encoding/json"
  "fmt"
  "github.com/FalconOpsLLC/goexec/internal/exec"
  wmiexec "github.com/FalconOpsLLC/goexec/internal/exec/wmi"
  "github.com/spf13/cobra"
)

func wmiCmdInit() {
  registerRpcFlags(wmiCmd)
  wmiCallCmdInit()
  wmiCmd.AddCommand(wmiCallCmd)
  wmiProcessCmdInit()
  wmiCmd.AddCommand(wmiProcessCmd)
}

func wmiCallCmdInit() {
  wmiCallCmd.Flags().StringVarP(&dceConfig.Resource, "namespace", "n", "//./root/cimv2", "WMI namespace")
  wmiCallCmd.Flags().StringVarP(&wmi.Class, "class", "C", "", `WMI class to instantiate (i.e. "Win32_Process")`)
  wmiCallCmd.Flags().StringVarP(&wmi.Method, "method", "m", "", `WMI Method to call (i.e. "Create")`)
  wmiCallCmd.Flags().StringVarP(&wmi.Args, "args", "A", "{}", `WMI Method argument(s) in JSON dictionary format (i.e. {"CommandLine":"calc.exe"})`)
  if err := wmiCallCmd.MarkFlagRequired("method"); err != nil {
    panic(err)
  }
}

func wmiProcessCmdInit() {
  wmiProcessCmd.Flags().StringVarP(&command, "command", "c", "", "Process command line")
  wmiProcessCmd.Flags().StringVarP(&workingDirectory, "directory", "d", `C:\`, "Working directory")
  if err := wmiProcessCmd.MarkFlagRequired("command"); err != nil {
    panic(err)
  }
}

var (
  wmi struct {
    Class  string
    Method string
    Args   string
  }
  wmiMethodArgsMap map[string]any

  wmiCmd = &cobra.Command{
    Use:   "wmi",
    Short: "Establish execution via WMI",
    Args:  cobra.NoArgs,
  }
  wmiCallCmd = &cobra.Command{
    Use:   "call",
    Short: "Execute specified WMI method",
    Long: `Description:
  The call method creates an instance of the specified WMI class (-c),
  then calls the provided method (-m) with the provided arguments (-A).

References:
  https://learn.microsoft.com/en-us/windows/win32/wmisdk/wmi-classes
  `,
    Args: needs(needsTarget("cifs"), needsRpcTarget("cifs"), func(cmd *cobra.Command, args []string) (err error) {
      if err = json.Unmarshal([]byte(wmi.Args), &wmiMethodArgsMap); err != nil {
        err = fmt.Errorf("parse JSON arguments: %w", err)
      }
      return
    }),
    Run: func(cmd *cobra.Command, args []string) {
      executor := wmiexec.Module{}
      cleanCfg := &exec.CleanupConfig{} // TODO
      connCfg := &exec.ConnectionConfig{
        ConnectionMethod:       exec.ConnectionMethodDCE,
        ConnectionMethodConfig: dceConfig,
      }

      execCfg := &exec.ExecutionConfig{
        ExecutableName:  executable,
        ExecutableArgs:  executableArgs,
        ExecutionMethod: wmiexec.MethodCall,
        ExecutionMethodConfig: wmiexec.MethodCallConfig{
          Class:     wmi.Class,
          Method:    wmi.Method,
          Arguments: wmiMethodArgsMap,
        },
      }

      ctx = log.With().
        Str("module", "wmi").
        Str("method", "proc").
        Logger().WithContext(ctx)

      if err := executor.Connect(ctx, creds, target, connCfg); err != nil {
        log.Fatal().Err(err).Msg("Connection failed")
      }
      defer func() {
        if err := executor.Cleanup(ctx, cleanCfg); err != nil {
          log.Error().Err(err).Msg("Cleanup failed")
        }
      }()
      if err := executor.Exec(ctx, execCfg); err != nil {
        log.Error().Err(err).Msg("Execution failed")
      }
    },
  }

  wmiProcessCmd = &cobra.Command{
    Use:   "proc",
    Short: "Start a Windows process",
    Long: `Description:
  The proc method creates an instance of the Win32_Process WMI class, then
  calls the Win32_Process.Create method with the provided command (-c),
  and optional working directory (-d).

References:
  https://learn.microsoft.com/en-us/windows/win32/cimwin32prov/create-method-in-class-win32-process
`,
    Args: needs(needsTarget("cifs"), needsRpcTarget("cifs")),
    Run: func(cmd *cobra.Command, args []string) {

      executor := wmiexec.Module{}
      cleanCfg := &exec.CleanupConfig{} // TODO
      connCfg := &exec.ConnectionConfig{
        ConnectionMethod:       exec.ConnectionMethodDCE,
        ConnectionMethodConfig: dceConfig,
      }
      execCfg := &exec.ExecutionConfig{
        ExecutableName:  executable,
        ExecutableArgs:  executableArgs,
        ExecutionMethod: wmiexec.MethodProcess,

        ExecutionMethodConfig: wmiexec.MethodProcessConfig{
          Command:          command,
          WorkingDirectory: workingDirectory,
        },
      }

      ctx = log.With().
        Str("module", "wmi").
        Str("method", "proc").
        Logger().WithContext(ctx)

      if err := executor.Connect(ctx, creds, target, connCfg); err != nil {
        log.Fatal().Err(err).Msg("Connection failed")
      }
      defer func() {
        if err := executor.Cleanup(ctx, cleanCfg); err != nil {
          log.Error().Err(err).Msg("Cleanup failed")
        }
      }()
      if err := executor.Exec(ctx, execCfg); err != nil {
        log.Error().Err(err).Msg("Execution failed")
      }
    },
  }
)