-
Notifications
You must be signed in to change notification settings - Fork 2
/
wmiexec.go
263 lines (207 loc) · 7.19 KB
/
wmiexec.go
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
//go:build ignore
// wmiexec.go is a sample for executing arbitrary WMI class method on the remote machine, examples:
// (1) enumerate sub keys on the remote machine, where 2147483650 is HKEY_LOCAL_MACHINE
// (see https://learn.microsoft.com/en-us/previous-versions/windows/desktop/regprov/enumkey-method-in-class-stdregprov)
//
// go run wmiexec.go \
// --debug \
// --class StdRegProv \
// --method EnumKey \
// --args '{"hDefKey": 2147483650, "sSubKeyName": "SYSTEM\\CurrentControlSet\\Services"}'
//
// (2) cerate a process on the remote machine:
//
// go run wmiexec.go --class Win32Process --method Create --args '{"CommandLine": "calc.exe", "CurrentDirectory": "C:\\"}'
package main
import (
"context"
"encoding/json"
"flag"
"fmt"
"io"
"net"
"os"
"github.com/oiweiwei/go-msrpc/dcerpc"
"github.com/rs/zerolog"
"github.com/oiweiwei/go-msrpc/msrpc/dcom"
"github.com/oiweiwei/go-msrpc/msrpc/dcom/iactivation/v0"
"github.com/oiweiwei/go-msrpc/msrpc/dcom/iobjectexporter/v0"
"github.com/oiweiwei/go-msrpc/msrpc/dcom/wmio/query"
"github.com/oiweiwei/go-msrpc/msrpc/dcom/wmio"
"github.com/oiweiwei/go-msrpc/msrpc/dcom/wmi"
"github.com/oiweiwei/go-msrpc/msrpc/dcom/wmi/iwbemlevel1login/v0"
"github.com/oiweiwei/go-msrpc/msrpc/dcom/wmi/iwbemservices/v0"
"github.com/oiweiwei/go-msrpc/ssp"
"github.com/oiweiwei/go-msrpc/ssp/credential"
"github.com/oiweiwei/go-msrpc/ssp/gssapi"
"github.com/oiweiwei/go-msrpc/msrpc/erref/hresult"
_ "github.com/oiweiwei/go-msrpc/msrpc/erref/win32"
_ "github.com/oiweiwei/go-msrpc/msrpc/erref/wmi"
)
var j = func(data any) string { b, _ := json.MarshalIndent(data, "", " "); return string(b) }
var (
class string
method string
arg string
resource string
debug bool
args wmio.Values
)
func init() {
flag.StringVar(&class, "class", "Win32_Process", "class name")
flag.StringVar(&method, "method", "Create", "method name")
flag.StringVar(&arg, "args", `{"CommandLine":"calc.exe","CurrentDirectory":"C:\\"}`, "method args")
flag.StringVar(&resource, "resource", "//./root/cimv2", "resource name, ie root/default, or root/cimv2")
flag.BoolVar(&debug, "debug", false, "debug")
flag.Parse()
if err := json.Unmarshal([]byte(arg), &args); err != nil {
fmt.Fprintln(os.Stderr, "parse_args", err)
os.Exit(1)
}
}
func init() {
// add credentials.
gssapi.AddCredential(credential.NewFromPassword(os.Getenv("USERNAME"), os.Getenv("PASSWORD")))
// add mechanism.
gssapi.AddMechanism(ssp.SPNEGO)
gssapi.AddMechanism(ssp.KRB5)
gssapi.AddMechanism(ssp.NTLM)
}
func main() {
log := zerolog.New(os.Stderr)
if !debug {
log = zerolog.New(io.Discard)
}
log.Info().Str("class", class).Str("method", method).Str("args", fmt.Sprintf("%+v", args)).Msg("execute")
ctx := gssapi.NewSecurityContext(context.Background())
// https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-dcom/2be2642e-67a1-4690-883b-642b505ddb1d
// ObjectExporter uses well-known endpoint 135.
cc, err := dcerpc.Dial(ctx, net.JoinHostPort(os.Getenv("SERVER"), "135"))
if err != nil {
fmt.Fprintln(os.Stderr, "dial_well_known_endpoint", err)
return
}
defer cc.Close(ctx)
// new object exporter client.
cli, err := iobjectexporter.NewObjectExporterClient(ctx, cc, dcerpc.WithSign(), dcerpc.WithTargetName(os.Getenv("TARGET")))
if err != nil {
fmt.Fprintln(os.Stderr, "new_object_exporter", err)
return
}
// server-alive to determine the bindings.
srv, err := cli.ServerAlive2(ctx, &iobjectexporter.ServerAlive2Request{})
if err != nil {
fmt.Fprintln(os.Stderr, "server_alive2", err)
return
}
if debug {
fmt.Println("-----------------------")
fmt.Println("OBJECT RESOLVER STRING BINDINGS")
fmt.Println("-----------------------")
fmt.Println(j(srv.ObjectResolverBindings.GetStringBindings()))
fmt.Println("-----------------------")
fmt.Println("OBJECT RESOLVER SECURITY BINDINGS")
fmt.Println("-----------------------")
fmt.Println(j(srv.ObjectResolverBindings.GetSecurityBindings()))
}
// dial activation client.
// cc, err = dcerpc.Dial(ctx, net.JoinHostPort(os.Getenv("SERVER"), "135"))
// if err != nil {
// fmt.Fprintln(os.Stderr, "dial_well_known_endpoint_2", err)
// return
// }
// new activation-client.
iact, err := iactivation.NewActivationClient(ctx, cc, dcerpc.WithSign(), dcerpc.WithTargetName(os.Getenv("TARGET")))
if err != nil {
fmt.Fprintln(os.Stderr, "new_activation_client", err)
return
}
// activate the WMI interface.
act, err := iact.RemoteActivation(ctx, &iactivation.RemoteActivationRequest{
ORPCThis: &dcom.ORPCThis{Version: srv.COMVersion},
ClassID: wmi.Level1LoginClassID.GUID(),
IIDs: []*dcom.IID{iwbemlevel1login.Level1LoginIID},
// for TCP/IP it must be []uint16{7} / for named pipes: []uint16{15}.
RequestedProtocolSequences: []uint16{7, 15},
})
if err != nil {
fmt.Fprintln(os.Stderr, "remote_activation", err)
return
}
if act.HResult != 0 {
fmt.Fprintln(os.Stderr, hresult.FromCode(uint32(act.HResult)))
return
}
if debug {
fmt.Println("-----------------------")
fmt.Println("REMOTE ACTIVATION")
fmt.Println("-----------------------")
fmt.Println(j(act))
for i, ifd := range act.InterfaceData {
fmt.Println("-----------------------")
fmt.Println(i, "REMOTE ACTIVATION INTERFACE")
fmt.Println("-----------------------")
fmt.Println(j(ifd.GetObjectReference()))
}
}
std := act.InterfaceData[0].GetStandardObjectReference().Std
// dial WMI using OXID bindings. (use ncacn_tcp_ip).
wcc, err := dcerpc.Dial(ctx, os.Getenv("SERVER"), act.OXIDBindings.EndpointsByProtocol("ncacn_ip_tcp")...)
if err != nil {
fmt.Fprintln(os.Stderr, "dial_wmi_endpoint", err)
return
}
defer wcc.Close(ctx)
// establish context that will be shared between NewLevel1LoginClient and
// NewServicesClient.
ctx = gssapi.NewSecurityContext(ctx)
l1login, err := iwbemlevel1login.NewLevel1LoginClient(ctx, wcc,
dcom.WithIPID(std.IPID),
dcerpc.WithSign(),
dcerpc.WithTargetName(os.Getenv("TARGET")), dcerpc.WithLogger(log))
if err != nil {
fmt.Fprintln(os.Stderr, err)
return
}
pos, err := l1login.EstablishPosition(ctx, &iwbemlevel1login.EstablishPositionRequest{
This: &dcom.ORPCThis{Version: srv.COMVersion},
})
if err != nil {
fmt.Fprintln(os.Stderr, err)
return
}
if debug {
fmt.Println(j(pos))
}
// login to WMI.
login, err := l1login.NTLMLogin(ctx, &iwbemlevel1login.NTLMLoginRequest{
This: &dcom.ORPCThis{Version: srv.COMVersion},
NetworkResource: resource,
})
if err != nil {
fmt.Fprintln(os.Stderr, err)
return
}
if debug {
fmt.Println(j(login))
}
ns := login.Namespace
// start services client.
svcs, err := iwbemservices.NewServicesClient(ctx, wcc, dcom.WithIPID(ns.InterfacePointer().IPID()))
if err != nil {
fmt.Fprintln(os.Stderr, err)
return
}
builder := query.NewBuilder(ctx, svcs, srv.COMVersion)
if debug {
obj, _ := builder.Spawn(class).Method(method).Values(args, wmio.JSONValueToType).Object()
fmt.Println(j(obj))
}
// use simple query builder to execute the Create static method of the Win32_Process.
out, err := builder.Spawn(class).Method(method).Values(args, wmio.JSONValueToType).Exec().Object()
if err != nil {
fmt.Fprintln(os.Stderr, "svcs_exec_method", err)
return
}
fmt.Println(j(out.Values()))
}