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
|
package cmd
import (
"context"
"github.com/FalconOpsLLC/goexec/internal/util"
"github.com/FalconOpsLLC/goexec/pkg/goexec"
tschexec "github.com/FalconOpsLLC/goexec/pkg/goexec/tsch"
"github.com/oiweiwei/go-msrpc/ssp/gssapi"
"github.com/spf13/cobra"
"os"
"time"
)
func tschCmdInit() {
registerRpcFlags(tschCmd)
tschDemandCmdInit()
tschCmd.AddCommand(tschDemandCmd)
tschCreateCmdInit()
tschCmd.AddCommand(tschCreateCmd)
}
func argsTschTaskIdentifiers(name, path string) error {
switch {
case path != "":
return tschexec.ValidateTaskPath(path)
case name != "":
return tschexec.ValidateTaskName(name)
default:
}
return nil
}
func argsTschDemand(_ *cobra.Command, _ []string) error {
return argsTschTaskIdentifiers(tschDemand.TaskName, tschDemand.TaskPath)
}
func argsTschCreate(_ *cobra.Command, _ []string) error {
return argsTschTaskIdentifiers(tschCreate.TaskName, tschCreate.TaskPath)
}
func tschDemandCmdInit() {
tschDemandCmd.Flags().StringVarP(&tschDemand.TaskName, "name", "t", "", "Name of task to register")
tschDemandCmd.Flags().StringVarP(&tschDemand.TaskPath, "path", "P", "", "Path of task to register")
tschDemandCmd.Flags().Uint32Var(&tschDemand.SessionId, "session", 0, "Hijack existing session given the session ID")
tschDemandCmd.Flags().BoolVar(&tschDemand.NoDelete, "no-delete", false, "Don't delete task after execution")
tschDemandCmd.Flags().StringVar(&tschDemand.UserSid, "sid", "S-1-5-18", "User SID to impersonate")
registerProcessExecutionArgs(tschDemandCmd)
registerExecutionOutputArgs(tschDemandCmd)
tschDemandCmd.MarkFlagsMutuallyExclusive("name", "path")
}
func tschCreateCmdInit() {
tschCreateCmd.Flags().StringVarP(&tschCreate.TaskName, "name", "t", "", "Name of task to register")
tschCreateCmd.Flags().StringVarP(&tschCreate.TaskPath, "path", "P", "", "Path of task to register")
tschCreateCmd.Flags().DurationVar(&tschCreate.StopDelay, "delay-stop", 5*time.Second, "Delay between task execution and termination. This will not stop the process spawned by the task")
tschCreateCmd.Flags().DurationVar(&tschCreate.StartDelay, "start-delay", 5*time.Second, "Delay between task registration and execution")
tschCreateCmd.Flags().DurationVar(&tschCreate.DeleteDelay, "delete-delay", 0*time.Second, "Delay between task termination and deletion")
tschCreateCmd.Flags().BoolVar(&tschCreate.NoDelete, "no-delete", false, "Don't delete task after execution")
tschCreateCmd.Flags().BoolVar(&tschCreate.CallDelete, "call-delete", false, "Directly call SchRpcDelete to delete task")
tschCreateCmd.Flags().StringVar(&tschCreate.UserSid, "sid", "S-1-5-18", "User SID to impersonate")
registerProcessExecutionArgs(tschCreateCmd)
tschCreateCmd.MarkFlagsMutuallyExclusive("name", "path")
}
var (
tschDemand tschexec.TschDemand
tschCreate tschexec.TschCreate
tschCmd = &cobra.Command{
Use: "tsch",
Short: "Establish execution via Windows Task Scheduler (MS-TSCH)",
Args: cobra.NoArgs,
}
tschDemandCmd = &cobra.Command{
Use: "demand [target]",
Short: "Register a remote scheduled task and demand immediate start",
Long: `Description:
Similar to the create method, the demand method will call SchRpcRegisterTask,
But rather than setting a defined time when the task will start, it will
additionally call SchRpcRun to forcefully start the task.
References:
SchRpcRegisterTask - https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-tsch/849c131a-64e4-46ef-b015-9d4c599c5167
SchRpcRun - https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-tsch/77f2250d-500a-40ee-be18-c82f7079c4f0
`,
Args: args(
argsRpcClient("cifs"),
argsSmbClient(),
argsTschDemand,
),
Run: func(cmd *cobra.Command, args []string) {
var err error
tschDemand.Client = &rpcClient
tschDemand.IO = exec
if tschDemand.TaskName == "" && tschDemand.TaskPath == "" {
tschDemand.TaskPath = `\` + util.RandomString()
}
ctx := log.WithContext(gssapi.NewSecurityContext(context.TODO()))
if outputPath == "-" {
exec.Output.Writer = os.Stdout
} else if outputPath != "" {
if exec.Output.Writer, err = os.OpenFile(outputPath, os.O_WRONLY|os.O_CREATE, 0644); err != nil {
log.Fatal().Err(err).Msg("Failed to open output file")
}
defer exec.Output.Writer.Close()
}
if err = goexec.ExecuteCleanMethod(ctx, &tschDemand, &exec); err != nil {
log.Fatal().Err(err).Msg("Operation failed")
}
},
}
tschCreateCmd = &cobra.Command{
Use: "create [target]",
Short: "Create a remote scheduled task with an automatic start time",
Long: `Description:
The create method calls SchRpcRegisterTask to register a scheduled task
with an automatic start time.This method avoids directly calling SchRpcRun,
and can even avoid calling SchRpcDelete by populating the DeleteExpiredTaskAfter
Setting.
References:
SchRpcRegisterTask - https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-tsch/849c131a-64e4-46ef-b015-9d4c599c5167
SchRpcRun - https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-tsch/77f2250d-500a-40ee-be18-c82f7079c4f0
SchRpcDelete - https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-tsch/360bb9b1-dd2a-4b36-83ee-21f12cb97cff
DeleteExpiredTaskAfter - https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-tsch/6bfde6fe-440e-4ddd-b4d6-c8fc0bc06fae
`,
Args: args(
argsRpcClient("cifs"),
argsSmbClient(),
argsTschCreate,
),
Run: func(cmd *cobra.Command, args []string) {
var err error
tschCreate.Tsch.Client = &rpcClient
tschCreate.IO = exec
if tschCreate.TaskName == "" && tschDemand.TaskPath == "" {
tschCreate.TaskPath = `\` + util.RandomString()
}
ctx := log.WithContext(gssapi.NewSecurityContext(context.TODO()))
if outputPath == "-" {
exec.Output.Writer = os.Stdout
} else if outputPath != "" {
if exec.Output.Writer, err = os.OpenFile(outputPath, os.O_WRONLY|os.O_CREATE, 0644); err != nil {
log.Fatal().Err(err).Msg("Failed to open output file")
}
defer exec.Output.Writer.Close()
}
if err = goexec.ExecuteCleanMethod(ctx, &tschCreate, &exec); err != nil {
log.Fatal().Err(err).Msg("Operation failed")
}
if outputPath != "" {
if err = tschCreate.IO.GetOutput(ctx); err != nil {
log.Error().Err(err).Msg("Failed to get process execution output")
returnCode = 4
}
}
},
}
)
|