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
|
package dcomexec
import (
"context"
"errors"
"fmt"
"github.com/FalconOpsLLC/goexec/internal/client/dce"
"github.com/FalconOpsLLC/goexec/internal/exec"
"github.com/RedTeamPentesting/adauth"
guuid "github.com/google/uuid"
"github.com/oiweiwei/go-msrpc/dcerpc"
"github.com/oiweiwei/go-msrpc/midl/uuid"
"github.com/oiweiwei/go-msrpc/msrpc/dcom"
"github.com/oiweiwei/go-msrpc/msrpc/dcom/iremotescmactivator/v0"
"github.com/oiweiwei/go-msrpc/msrpc/dcom/oaut"
"github.com/oiweiwei/go-msrpc/msrpc/dcom/oaut/idispatch/v0"
"github.com/oiweiwei/go-msrpc/msrpc/dtyp"
"github.com/rs/zerolog"
)
const (
DefaultDcomEndpoint = "ncacn_ip_tcp:[135]"
)
var (
MmcUuid = uuid.MustParse("49B2791A-B1AE-4C90-9B8E-E860BA07F889")
ShellWindowsUuid = uuid.MustParse("9BA05972-F6A8-11CF-A442-00A0C90A8F39")
RandCid = dcom.CID(*dtyp.GUIDFromUUID(uuid.MustParse(guuid.NewString())))
IDispatchIID = &dcom.IID{
Data1: 0x20400,
Data2: 0x0,
Data3: 0x0,
Data4: []byte{0xc0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x46},
}
ComVersion = &dcom.COMVersion{
MajorVersion: 5,
MinorVersion: 7,
}
MmcClsid = dcom.ClassID(*dtyp.GUIDFromUUID(MmcUuid))
ORPCThis = &dcom.ORPCThis{
Version: ComVersion,
CID: &RandCid,
}
)
func (mod *Module) Connect(ctx context.Context, creds *adauth.Credential, target *adauth.Target, ccfg *exec.ConnectionConfig) (err error) {
log := zerolog.Ctx(ctx).With().
Str("method", ccfg.ConnectionMethod).
Str("func", "Exec").Logger()
if ccfg.ConnectionMethod == exec.ConnectionMethodDCE {
if cfg, ok := ccfg.ConnectionMethodConfig.(dce.ConnectionMethodDCEConfig); !ok {
return fmt.Errorf("invalid configuration for DCE connection method")
} else {
opts := []dcerpc.Option{dcerpc.WithSign(), dcerpc.WithSecurityLevel(0)}
// Fetch target hostname
if mod.hostname, err = target.Hostname(ctx); err != nil {
log.Debug().Err(err).Msg("Failed to get target hostname")
opts = append(opts, dcerpc.WithTargetName(mod.hostname))
}
// Create DCE connection
if mod.dce, err = cfg.GetDce(ctx, creds, target, DefaultDcomEndpoint, "", opts...); err != nil {
log.Error().Err(err).Msg("Failed to initialize DCE dialer")
return fmt.Errorf("create DCE dialer: %w", err)
}
inst := &dcom.InstantiationInfoData{
ClassID: &MmcClsid,
IID: []*dcom.IID{IDispatchIID},
ClientCOMVersion: ComVersion,
}
scm := &dcom.SCMRequestInfoData{
RemoteRequest: &dcom.CustomRemoteRequestSCMInfo{
RequestedProtocolSequences: []uint16{7},
},
}
loc := &dcom.LocationInfoData{}
ac := &dcom.ActivationContextInfoData{}
ap := &dcom.ActivationProperties{
DestinationContext: 2,
Properties: []dcom.ActivationProperty{inst, ac, loc, scm},
}
apin, err := ap.ActivationPropertiesIn()
if err != nil {
return err
}
act, err := iremotescmactivator.NewRemoteSCMActivatorClient(ctx, mod.dce)
if err != nil {
return err
}
cr, err := act.RemoteCreateInstance(ctx, &iremotescmactivator.RemoteCreateInstanceRequest{
ORPCThis: &dcom.ORPCThis{
Version: ComVersion,
Flags: 1,
CID: &RandCid,
},
ActPropertiesIn: apin,
})
if err != nil {
return err
}
log.Info().Msg("RemoteCreateInstance succeeded")
apout := &dcom.ActivationProperties{}
if err = apout.Parse(cr.ActPropertiesOut); err != nil {
return err
}
si := apout.SCMReplyInfoData()
pi := apout.PropertiesOutInfo()
if si == nil {
return fmt.Errorf("remote create instance response: SCMReplyInfoData is nil")
}
if pi == nil {
return fmt.Errorf("remote create instance response: PropertiesOutInfo is nil")
}
oIPID := pi.InterfaceData[0].IPID()
opts = append(opts, si.RemoteReply.OXIDBindings.EndpointsByProtocol("ncacn_ip_tcp")...)
mod.dce, err = cfg.GetDce(ctx, creds, target, DefaultDcomEndpoint, "", opts...)
if err != nil {
return err
}
log.Info().Msg("created new DCERPC dialer")
mod.dc, err = idispatch.NewDispatchClient(ctx, mod.dce, dcom.WithIPID(oIPID))
if err != nil {
return err
}
log.Info().Msg("created IDispatch client")
}
}
return
}
func (mod *Module) Exec(ctx context.Context, ecfg *exec.ExecutionConfig) (err error) {
log := zerolog.Ctx(ctx).With().
Str("method", ecfg.ExecutionMethod).
Str("func", "Exec").Logger()
if ecfg.ExecutionMethod == MethodMmc {
if cfg, ok := ecfg.ExecutionMethodConfig.(MethodMmcConfig); !ok {
return errors.New("invalid configuration")
} else {
// https://learn.microsoft.com/en-us/previous-versions/windows/desktop/mmc/view-executeshellcommand
method := "Document.ActiveView.ExecuteShellCommand"
log = log.With().Str("classMethod", method).Logger()
log.Info().
Str("executable", ecfg.ExecutableName).
Str("arguments", ecfg.ExecutableArgs).Msg("Attempting execution")
command := &oaut.Variant{
Size: 5,
VT: 8,
VarUnion: &oaut.Variant_VarUnion{Value: &oaut.Variant_VarUnion_BSTR{BSTR: &oaut.String{Data: ecfg.ExecutableName}}},
}
directory := &oaut.Variant{
Size: 5,
VT: 8,
VarUnion: &oaut.Variant_VarUnion{Value: &oaut.Variant_VarUnion_BSTR{BSTR: &oaut.String{Data: cfg.WorkingDirectory}}},
}
parameters := &oaut.Variant{
Size: 5,
VT: 8,
VarUnion: &oaut.Variant_VarUnion{Value: &oaut.Variant_VarUnion_BSTR{BSTR: &oaut.String{Data: ecfg.ExecutableArgs}}},
}
windowState := &oaut.Variant{
Size: 5,
VT: 8,
VarUnion: &oaut.Variant_VarUnion{Value: &oaut.Variant_VarUnion_BSTR{BSTR: &oaut.String{Data: cfg.WindowState}}},
}
// Arguments must be passed in reverse order
if _, err := callMethod(ctx, mod.dc, method, windowState, parameters, directory, command); err != nil {
log.Error().Err(err).Msg("Failed to call method")
return fmt.Errorf("call %q: %w", method, err)
}
log.Info().Msg("Method call successful")
}
}
return nil
}
|