/
ssh.go
181 lines (159 loc) · 4.11 KB
/
ssh.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
package optssh
import (
"bytes"
"fmt"
"strings"
"sync"
"github.com/5amu/goad/internal/printer"
"github.com/5amu/goad/internal/utils"
"golang.org/x/crypto/ssh"
)
type Options struct {
Targets struct {
TARGETS []string `description:"Provide target IP/FQDN/FILE"`
} `positional-args:"yes"`
Connection struct {
Username string `short:"u" description:"Provide username (or FILE)"`
Password string `short:"p" description:"Provide password (or FILE)"`
PrivKey string `short:"k" long:"private-key" description:"Provide a path to a ssh private key without password"`
Port int `long:"port" default:"22" description:"Port to contact"`
} `group:"Connection Options" description:"Connection Options"`
Mode struct {
Exec string `long:"exec" description:"Execute command on target host"`
Shell bool `long:"shell" description:"Spawn a shell"`
} `group:"Execution Mode"`
targets []string
target2Banner map[string]string
printMutex sync.Mutex
credentials []utils.Credential
cmd string
}
func gatherSSHBanner2Map(mutex *sync.Mutex, targets []string, port int) map[string]string {
res := make(map[string]string)
var mapMutex sync.Mutex
mutex.Lock()
defer mutex.Unlock()
var wg sync.WaitGroup
for _, t := range targets {
wg.Add(1)
go func(p string) {
s, err := GrabBanner(p, port)
if err == nil {
prt := printer.NewPrinter("SSH", p, ParseBanner(s), port)
mapMutex.Lock()
res[p] = ParseBanner(s)
mapMutex.Unlock()
prt.PrintInfo(s)
}
wg.Done()
}(t)
}
wg.Wait()
return res
}
func (o *Options) Run() {
o.targets = utils.ExtractTargets(o.Targets.TARGETS)
o.credentials = utils.NewCredentialsClusterBomb(
utils.ExtractLinesFromFileOrString(o.Connection.Username),
utils.ExtractLinesFromFileOrString(o.Connection.Password),
)
o.target2Banner = gatherSSHBanner2Map(&o.printMutex, o.targets, o.Connection.Port)
var f func(string)
if o.Mode.Exec != "" {
o.cmd = o.Mode.Exec
f = o.exec
} else if o.Mode.Shell {
f = o.shell
} else {
return
}
var wg sync.WaitGroup
for target := range o.target2Banner {
wg.Add(1)
go func(t string) {
f(t)
wg.Done()
}(target)
}
wg.Wait()
}
func (o *Options) authenticate(target string) (*ssh.Client, error) {
prt := printer.NewPrinter("SSH", target, o.target2Banner[target], o.Connection.Port)
defer prt.PrintStored(&o.printMutex)
var c *ssh.Client
for _, cred := range o.credentials {
var err error
if o.Connection.PrivKey != "" {
c, err = ConnectWithKey(
cred.Username,
o.Connection.PrivKey,
target,
o.Connection.Port,
)
} else {
c, err = ConnectWithPassword(
o.Connection.Username,
o.Connection.Password,
target,
o.Connection.Port,
)
}
if err != nil {
prt.StoreFailure(cred.String())
continue
}
prt.StoreSuccess(cred.String())
return c, nil
}
return nil, fmt.Errorf("no valid credential provided")
}
func (o *Options) exec(target string) {
prt := printer.NewPrinter("SSH", target, o.target2Banner[target], o.Connection.Port)
defer prt.PrintStored(&o.printMutex)
c, err := o.authenticate(target)
if err != nil {
return
}
var stdoutBuff, stderrBuff bytes.Buffer
if err := Run(c, o.cmd, &stdoutBuff, &stderrBuff); err != nil {
prt.StoreFailure(err.Error())
}
out := stdoutBuff.String() + stderrBuff.String()
splitted := strings.Split(out, "\n")
for _, s := range splitted[:len(splitted)-1] {
prt.Store(s)
}
}
func (o *Options) shell(target string) {
prt := printer.NewPrinter("SSH", target, o.target2Banner[target], o.Connection.Port)
defer prt.PrintStored(&o.printMutex)
if len(o.credentials) != 1 {
prt.StoreFailure("provide 1 set of credentials")
return
}
var err error
var c *ssh.Client
if o.Connection.Password != "" {
c, err = ConnectWithPassword(
o.Connection.Username,
o.Connection.Password,
target,
o.Connection.Port,
)
} else if o.Connection.PrivKey != "" {
c, err = ConnectWithKey(
o.Connection.Username,
o.Connection.PrivKey,
target,
o.Connection.Port,
)
}
if err != nil {
prt.StoreFailure(err.Error())
return
}
if err := Shell(c); err != nil {
prt.StoreFailure(err.Error())
return
}
}