aboutsummaryrefslogtreecommitdiff
path: root/pkg/exec/tsch/exec.go
blob: 030a016bf12d93f7f4afee2a6ce381e61a78a6d0 (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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
package tschexec

import (
	"context"
	"encoding/xml"
	"errors"
	"fmt"
	"github.com/FalconOpsLLC/goexec/internal/util"
	dce "github.com/FalconOpsLLC/goexec/pkg/client/dcerpc"
	"github.com/FalconOpsLLC/goexec/pkg/exec"
	"github.com/bryanmcnulty/adauth"
	"github.com/oiweiwei/go-msrpc/dcerpc"
	"github.com/oiweiwei/go-msrpc/msrpc/tsch/itaskschedulerservice/v1"
	"github.com/rs/zerolog"
	"regexp"
	"time"
)

const (
	TaskXMLDurationFormat = "2006-01-02T15:04:05.9999999Z"
	TaskXMLHeader         = `<?xml version="1.0" encoding="UTF-16"?>`
)

var (
	TaskPathRegex = regexp.MustCompile(`^\\[^ :/\\][^:/]*$`) // https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-tsch/fa8809c8-4f0f-4c6d-994a-6c10308757c1
	TaskNameRegex = regexp.MustCompile(`^[^ :/\\][^:/\\]*$`)
)

// *very* simple implementation of xs:duration - only accepts +seconds
func xmlDuration(dur time.Duration) string {
	if s := int(dur.Seconds()); s >= 0 {
		return fmt.Sprintf(`PT%dS`, s)
	}
	return `PT0S`
}

// Connect to the target & initialize DCE & TSCH clients
func (mod *Module) Connect(ctx context.Context, creds *adauth.Credential, target *adauth.Target) (err error) {
	if mod.dce == nil {
		mod.dce = dce.NewDCEClient(ctx, false, &dce.SmbConfig{})
		if err = mod.dce.Connect(ctx, creds, target); err != nil {
			return fmt.Errorf("DCE connect: %w", err)
		} else if mod.tsch, err = itaskschedulerservice.NewTaskSchedulerServiceClient(ctx, mod.dce.DCE(), dcerpc.WithSecurityLevel(dcerpc.AuthLevelPktPrivacy)); err != nil {
			return fmt.Errorf("init MS-TSCH client: %w", err)
		}
		mod.log.Info().Msg("DCE connection successful")
	}
	return
}

func (mod *Module) Cleanup(ctx context.Context, creds *adauth.Credential, target *adauth.Target, ccfg *exec.CleanupConfig) (err error) {
	mod.log = zerolog.Ctx(ctx).With().
		Str("module", "tsch").
		Str("method", ccfg.CleanupMethod).Logger()
	mod.creds = creds
	mod.target = target

	if ccfg.CleanupMethod == MethodDelete {
		if cfg, ok := ccfg.CleanupMethodConfig.(MethodDeleteConfig); !ok {
			return errors.New("invalid configuration")
		} else {
			if err = mod.Connect(ctx, creds, target); err != nil {
				return fmt.Errorf("connect: %w", err)
			} else if _, err = mod.tsch.Delete(ctx, &itaskschedulerservice.DeleteRequest{
				Path:  cfg.TaskPath,
				Flags: 0,
			}); err != nil {
				mod.log.Error().Err(err).Str("task", cfg.TaskPath).Msg("Failed to delete task")
				return fmt.Errorf("delete task: %w", err)
			} else {
				mod.log.Info().Str("task", cfg.TaskPath).Msg("Task deleted successfully")
			}
		}
	} else {
		return fmt.Errorf("method not implemented: %s", ccfg.CleanupMethod)
	}
	return
}

func (mod *Module) Exec(ctx context.Context, creds *adauth.Credential, target *adauth.Target, ecfg *exec.ExecutionConfig) (err error) {

	mod.log = zerolog.Ctx(ctx).With().
		Str("module", "tsch").
		Str("method", ecfg.ExecutionMethod).Logger()
	mod.creds = creds
	mod.target = target

	if ecfg.ExecutionMethod == MethodRegister {
		if cfg, ok := ecfg.ExecutionMethodConfig.(MethodRegisterConfig); !ok {
			return errors.New("invalid configuration")

		} else {
			startTime := time.Now().UTC().Add(cfg.StartDelay)
			task := &task{
				TaskVersion:   "1.2",                                                   // static
				TaskNamespace: "http://schemas.microsoft.com/windows/2004/02/mit/task", // static
				TimeTriggers: []taskTimeTrigger{
					{
						StartBoundary: startTime.Format(TaskXMLDurationFormat),
						Enabled:       true,
					},
				},
				Principals: defaultPrincipals,
				Settings:   defaultSettings,
				Actions:    actions{Context: defaultPrincipals.Principals[0].ID, Exec: []actionExec{{Command: ecfg.ExecutableName, Arguments: ecfg.ExecutableArgs}}},
			}
			if !cfg.NoDelete && !cfg.CallDelete {
				if cfg.StopDelay == 0 {
					// EndBoundary cannot be >= StartBoundary
					cfg.StopDelay = 1 * time.Second
				}
				stopTime := startTime.Add(cfg.StopDelay)

				mod.log.Info().Time("when", stopTime).Msg("Task is scheduled to delete")
				task.Settings.DeleteExpiredTaskAfter = xmlDuration(cfg.DeleteDelay)
				task.TimeTriggers[0].EndBoundary = stopTime.Format(TaskXMLDurationFormat)
			}

			if doc, err := xml.Marshal(task); err != nil {
				return fmt.Errorf("marshal task XML: %w", err)

			} else {
				mod.log.Debug().Str("task", string(doc)).Msg("Task XML generated")
				docStr := TaskXMLHeader + string(doc)

				taskPath := cfg.TaskPath
				taskName := cfg.TaskName

				if taskName == "" {
					taskName = util.RandomString()
				}
				if taskPath == "" {
					taskPath = `\` + taskName
				}

				if err = mod.Connect(ctx, creds, target); err != nil {
					return fmt.Errorf("connect: %w", err)
				}
				defer func() {
					if err = mod.dce.Close(ctx); err != nil {
						mod.log.Warn().Err(err).Msg("Failed to dispose dce client")
					} else {
						mod.log.Debug().Msg("Disposed DCE client")
					}
				}()
				var response *itaskschedulerservice.RegisterTaskResponse
				if response, err = mod.tsch.RegisterTask(ctx, &itaskschedulerservice.RegisterTaskRequest{
					Path:       taskPath,
					XML:        docStr,
					Flags:      0, // TODO
					LogonType:  0, // TASK_LOGON_NONE
					CredsCount: 0,
					Creds:      nil,
				}); err != nil {
					return err

				} else {
					mod.log.Info().Str("path", response.ActualPath).Msg("Task registered successfully")

					if !cfg.NoDelete && cfg.CallDelete {
						defer func() {
							if err = mod.Cleanup(ctx, creds, target, &exec.CleanupConfig{
								CleanupMethod:       MethodDelete,
								CleanupMethodConfig: MethodDeleteConfig{TaskPath: taskPath},
							}); err != nil {
								mod.log.Error().Err(err).Msg("Failed to delete task")
							}
						}()
						mod.log.Info().Dur("ms", cfg.StartDelay).Msg("Waiting for task to run")
						select {
						case <-ctx.Done():
							mod.log.Warn().Msg("Cancelling execution")
							return err
						case <-time.After(cfg.StartDelay + (time.Second * 2)): // + two seconds
							// TODO: check if task is running yet; delete if the wait period is over
							break
						}
						return err
					}
				}
			}
		}
	} else if ecfg.ExecutionMethod == MethodDemand {
		if cfg, ok := ecfg.ExecutionMethodConfig.(MethodDemandConfig); !ok {
			return errors.New("invalid configuration")

		} else {
			taskPath := cfg.TaskPath
			taskName := cfg.TaskName

			if taskName == "" {
				mod.log.Debug().Msg("Task name not defined. Using random string")
				taskName = util.RandomString()
			}
			if taskPath == "" {
				taskPath = `\` + taskName
			}
			if !TaskNameRegex.MatchString(taskName) {
				return fmt.Errorf("invalid task name: %s", taskName)
			}
			if !TaskPathRegex.MatchString(taskPath) {
				return fmt.Errorf("invalid task path: %s", taskPath)
			}

			mod.log.Debug().Msg("Using demand method")
			settings := defaultSettings
			settings.AllowStartOnDemand = true
			task := &task{
				TaskVersion:   "1.2",                                                   // static
				TaskNamespace: "http://schemas.microsoft.com/windows/2004/02/mit/task", // static
				Principals:    defaultPrincipals,
				Settings:      defaultSettings,
				Actions: actions{
					Context: defaultPrincipals.Principals[0].ID,
					Exec: []actionExec{
						{
							Command:   ecfg.ExecutableName,
							Arguments: ecfg.ExecutableArgs,
						},
					},
				},
			}
			if doc, err := xml.Marshal(task); err != nil {
				return fmt.Errorf("marshal task: %w", err)
			} else {
				docStr := TaskXMLHeader + string(doc)

				if err = mod.Connect(ctx, creds, target); err != nil {
					return fmt.Errorf("connect: %w", err)
				}
				defer func() {
					if err = mod.dce.Close(ctx); err != nil {
						mod.log.Warn().Err(err).Msg("Failed to dispose dce client")
					} else {
						mod.log.Debug().Msg("Disposed DCE client")
					}
				}()

				var response *itaskschedulerservice.RegisterTaskResponse
				if response, err = mod.tsch.RegisterTask(ctx, &itaskschedulerservice.RegisterTaskRequest{
					Path:       taskPath,
					XML:        docStr,
					Flags:      0, // TODO
					LogonType:  0, // TASK_LOGON_NONE
					CredsCount: 0,
					Creds:      nil,
				}); err != nil {
					return fmt.Errorf("register task: %w", err)

				} else {
					mod.log.Info().Str("task", response.ActualPath).Msg("Task registered successfully")
					if !cfg.NoDelete {
						defer func() {
							if err = mod.Cleanup(ctx, creds, target, &exec.CleanupConfig{
								CleanupMethod:       MethodDelete,
								CleanupMethodConfig: MethodDeleteConfig{TaskPath: taskPath},
							}); err != nil {
								mod.log.Error().Err(err).Msg("Failed to delete task")
							}
						}()
					}
					if _, err = mod.tsch.Run(ctx, &itaskschedulerservice.RunRequest{
						Path:  response.ActualPath,
						Flags: 0, // Maybe we want to use these?
					}); err != nil {
						return err
					} else {
						mod.log.Info().Str("task", response.ActualPath).Msg("Started task")
					}
				}
			}
		}
	} else {
		return fmt.Errorf("method not implemented: %s", ecfg.ExecutionMethod)
	}

	return nil
}