@@ -3,31 +3,87 @@ package brute
33import (
44 "context"
55 "fmt"
6+ "net"
67 "time"
78
89 "go.mongodb.org/mongo-driver/mongo"
910 "go.mongodb.org/mongo-driver/mongo/options"
10- "go.mongodb.org/mongo-driver/mongo/readpref"
11+
12+ "github.com/x90skysn3k/brutespray/modules"
1113)
1214
13- func BruteMongoDB (host string , port int , user , password string , timeout time.Duration ) (bool , bool ) {
15+ type ContextDialerWrapper struct {
16+ CM * modules.ConnectionManager
17+ }
18+
19+ func (cdw * ContextDialerWrapper ) DialContext (ctx context.Context , network , address string ) (net.Conn , error ) {
20+ if _ , ok := ctx .Deadline (); ok {
21+
22+ return cdw .CM .DialFunc (network , address )
23+ }
24+ return cdw .CM .DialFunc (network , address )
25+ }
26+
27+ func BruteMongoDB (host string , port int , user , password string , timeout time.Duration , socks5 string , netInterface string ) (bool , bool ) {
28+ cm , err := modules .NewConnectionManager (socks5 , timeout , netInterface )
29+ if err != nil {
30+ //fmt.Printf("Failed to create connection manager: %v\n", err)
31+ return false , false
32+ }
33+
1434 ctx , cancel := context .WithTimeout (context .Background (), timeout )
1535 defer cancel ()
1636
17- clientOptions := options .Client ().ApplyURI (fmt .Sprintf ("mongodb://%s:%s@%s:%d" , user , password , host , port ))
37+ dialer := & ContextDialerWrapper {CM : cm }
38+
39+ clientOptions := options .Client ().
40+ ApplyURI (fmt .Sprintf ("mongodb://%s:%s@%s:%d" , user , password , host , port )).
41+ SetDialer (dialer )
1842 client , err := mongo .Connect (ctx , clientOptions )
1943 if err != nil {
44+ //fmt.Printf("Failed to connect: %v\n", err)
2045 return false , false
2146 }
2247 defer func () {
2348 if err := client .Disconnect (ctx ); err != nil {
2449 _ = err
50+ //fmt.Printf("Failed to disconnect: %v\n", err)
2551 }
2652 }()
2753
28- err = client .Ping ( ctx , readpref . Primary () )
54+ err = client .Database ( "admin" ). RunCommand ( ctx , map [ string ] interface {}{ "ping" : 1 }). Err ( )
2955 if err != nil {
56+ if mongo .IsTimeout (err ) {
57+ //fmt.Printf("Connection timeout: %v\n", err)
58+ return false , false
59+ }
60+ if isAuthError (err ) {
61+ //fmt.Printf("Authentication failed: %v\n", err)
62+ return false , true
63+ }
64+ //fmt.Printf("Other error during ping: %v\n", err)
3065 return false , true
3166 }
67+
68+ //fmt.Println("Authentication successful.")
3269 return true , true
3370}
71+
72+ func isAuthError (err error ) bool {
73+ if commandError , ok := err .(mongo.CommandError ); ok {
74+ authErrorCodes := map [int32 ]bool {
75+ 18 : true , // Authentication failed
76+ 13 : true , // Unauthorized
77+ 8000 : true , // SaslAuthenticationFailed
78+ }
79+ return authErrorCodes [commandError .Code ]
80+ }
81+ if writeException , ok := err .(mongo.WriteException ); ok {
82+ for _ , we := range writeException .WriteErrors {
83+ if we .Code == 18 || we .Code == 13 || we .Code == 8000 {
84+ return true
85+ }
86+ }
87+ }
88+ return false
89+ }
0 commit comments