aboutsummaryrefslogtreecommitdiff
path: root/cmd/scmr.go
blob: 9df9ef1b86227f89edfdf9d5c13f35cf5df05eb8 (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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package cmd

import (
  "github.com/FalconOpsLLC/goexec/internal/exec"
  "github.com/FalconOpsLLC/goexec/internal/util"
  "github.com/FalconOpsLLC/goexec/internal/windows"
  "github.com/RedTeamPentesting/adauth"
  "github.com/oiweiwei/go-msrpc/dcerpc"
  "github.com/spf13/cobra"

  scmrexec "github.com/FalconOpsLLC/goexec/internal/exec/scmr"
)

func scmrCmdInit() {
  registerRpcFlags(scmrCmd)
  scmrCreateCmdInit()
  scmrCmd.AddCommand(scmrCreateCmd)
  scmrChangeCmdInit()
  scmrCmd.AddCommand(scmrChangeCmd)
  scmrDeleteCmdInit()
  scmrCmd.AddCommand(scmrDeleteCmd)
}

func scmrCreateCmdInit() {
  scmrCreateCmd.Flags().StringVarP(&scmrDisplayName, "display-name", "n", "", "Display name of service to create")
  scmrCreateCmd.Flags().StringVarP(&scmrServiceName, "service-name", "s", "", "Name of service to create")
  scmrCreateCmd.Flags().BoolVar(&scmrNoDelete, "no-delete", false, "Don't delete service after execution")
  scmrCreateCmd.Flags().BoolVar(&scmrNoStart, "no-start", false, "Don't start service")
  scmrCreateCmd.Flags().StringVarP(&executablePath, "executable-path", "f", "", "Full path to a remote Windows executable file")
  scmrCreateCmd.Flags().StringVarP(&executableArgs, "args", "a", "", "Arguments to pass to the executable")
  if err := scmrCreateCmd.MarkFlagRequired("executable-path"); err != nil {
    panic(err)
  }
}

func scmrChangeCmdInit() {
  scmrChangeCmd.Flags().StringVarP(&scmrDisplayName, "display-name", "n", "", "Display name of service to create")
  scmrChangeCmd.Flags().BoolVar(&scmrNoStart, "no-start", false, "Don't start service")
  scmrChangeCmd.Flags().StringVarP(&scmrServiceName, "service-name", "s", "", "Name of service to modify")
  scmrChangeCmd.Flags().StringVarP(&executablePath, "executable-path", "f", "", "Full path to remote Windows executable")
  scmrChangeCmd.Flags().StringVarP(&executableArgs, "args", "a", "", "Arguments to pass to executable")
  if err := scmrChangeCmd.MarkFlagRequired("service-name"); err != nil {
    panic(err)
  }
}

func scmrDeleteCmdInit() {
  scmrDeleteCmd.Flags().StringArrayVarP(&scmrServiceNames, "service-name", "s", scmrServiceNames, "Name of service(s) to delete")
  if err := scmrDeleteCmd.MarkFlagRequired("service-name"); err != nil {
    panic(err)
  }
}

var (
  // scmr arguments
  scmrServiceName  string
  scmrServiceNames []string
  scmrDisplayName  string
  scmrNoDelete     bool
  scmrNoStart      bool

  creds  *adauth.Credential
  target *adauth.Target

  scmrCmd = &cobra.Command{
    Use:   "scmr",
    Short: "Establish execution via SCMR",
    Args:  cobra.NoArgs,
  }
  scmrCreateCmd = &cobra.Command{
    Use:   "create [target]",
    Short: "Create & run a new Windows service to gain execution",
    Long: `Description:
  The create method calls RCreateServiceW to create a new Windows service with
  the provided executable & arguments as the lpBinaryPathName

References:
  https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-scmr/6a8ca926-9477-4dd4-b766-692fab07227e
`,
    Args: needsRpcTarget("cifs"),
    Run: func(cmd *cobra.Command, args []string) {

      if scmrServiceName == "" {
        log.Warn().Msg("No service name was specified, using random string")
        scmrServiceName = util.RandomString()
      }
      if scmrNoDelete {
        log.Warn().Msg("Service will not be deleted after execution")
      }
      if scmrDisplayName == "" {
        log.Debug().Msg("No display name specified, using service name as display name")
        scmrDisplayName = scmrServiceName
      }

      executor := scmrexec.Module{}
      cleanCfg := &exec.CleanupConfig{
        CleanupMethod:       scmrexec.CleanupMethodDelete,
        CleanupMethodConfig: scmrexec.CleanupMethodDeleteConfig{},
      }
      connCfg := &exec.ConnectionConfig{
        ConnectionMethod:       exec.ConnectionMethodDCE,
        ConnectionMethodConfig: dceConfig,
      }
      execCfg := &exec.ExecutionConfig{
        ExecutablePath:  executablePath,
        ExecutableArgs:  executableArgs,
        ExecutionMethod: scmrexec.MethodCreate,

        ExecutionMethodConfig: scmrexec.MethodCreateConfig{
          NoDelete:    scmrNoDelete,
          ServiceName: util.RandomStringIfBlank(scmrServiceName),
          DisplayName: scmrDisplayName,
          ServiceType: windows.SERVICE_WIN32_OWN_PROCESS,
          StartType:   windows.SERVICE_DEMAND_START,
        },
      }
      ctx = log.With().
        Str("module", "scmr").
        Str("method", "create").
        Logger().WithContext(ctx)

      if err := executor.Connect(ctx, creds, target, connCfg); err != nil {
        log.Fatal().Err(err).Msg("Connection failed")
      }
      if !scmrNoDelete {
        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")
      }
    },
  }
  scmrChangeCmd = &cobra.Command{
    Use:   "change [target]",
    Short: "Change an existing Windows service to gain execution",
    Args:  needsRpcTarget("cifs"),
    Run: func(cmd *cobra.Command, args []string) {

      executor := scmrexec.Module{}
      cleanCfg := &exec.CleanupConfig{
        CleanupMethod:       scmrexec.CleanupMethodRevert,
        CleanupMethodConfig: scmrexec.CleanupMethodRevertConfig{},
      }
      connCfg := &exec.ConnectionConfig{
        ConnectionMethod:       exec.ConnectionMethodDCE,
        ConnectionMethodConfig: dceConfig,
      }
      execCfg := &exec.ExecutionConfig{
        ExecutablePath:  executablePath,
        ExecutableArgs:  executableArgs,
        ExecutionMethod: scmrexec.MethodChange,

        ExecutionMethodConfig: scmrexec.MethodChangeConfig{
          NoStart:     scmrNoStart,
          ServiceName: scmrServiceName,
        },
      }
      ctx = log.With().
        Str("module", "scmr").
        Str("method", "change").
        Logger().WithContext(ctx)

      if err := executor.Connect(ctx, creds, target, connCfg); err != nil {
        log.Fatal().Err(err).Msg("Connection failed")
      }
      if !scmrNoDelete {
        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")
      }
    },
  }
  scmrDeleteCmd = &cobra.Command{
    Use:   "delete [target]",
    Short: "Delete an existing Windows service",
    Long: `Description:
TODO
`,
    Args: needsRpcTarget("cifs"),
    Run: func(cmd *cobra.Command, args []string) {
      dceConfig.DceOptions = append(dceConfig.DceOptions, dcerpc.WithInsecure())

      executor := scmrexec.Module{}
      cleanCfg := &exec.CleanupConfig{
        CleanupMethod:       scmrexec.CleanupMethodDelete,
        CleanupMethodConfig: scmrexec.CleanupMethodDeleteConfig{ServiceNames: scmrServiceNames},
      }
      connCfg := &exec.ConnectionConfig{
        ConnectionMethod:       exec.ConnectionMethodDCE,
        ConnectionMethodConfig: dceConfig,
      }
      ctx = log.With().
        Str("module", "scmr").
        Str("method", "delete").
        Logger().WithContext(ctx)

      if err := executor.Connect(ctx, creds, target, connCfg); err != nil {
        log.Fatal().Err(err).Msg("Connection failed")

      } else if err = executor.Cleanup(ctx, cleanCfg); err != nil {
        log.Fatal().Err(err).Msg("Delete failed")
      }
    },
  }
)