|
| 1 | +package smtp |
| 2 | + |
| 3 | +import ( |
| 4 | + "GoMapEnum/src/utils" |
| 5 | + "fmt" |
| 6 | + "strconv" |
| 7 | + "strings" |
| 8 | + "time" |
| 9 | + |
| 10 | + smtp "github.com/nodauf/net-smtp" |
| 11 | +) |
| 12 | + |
| 13 | +func PrepareSMTPConnections(optionsInterface *interface{}) { |
| 14 | + options := (*optionsInterface).(*Options) |
| 15 | + options.connectionsPool = make(chan *smtp.Client, options.Thread) |
| 16 | + var nbConnectionsRequired int |
| 17 | + nbConnectionsRequired = options.Thread |
| 18 | + if len(options.UsernameList) < options.Thread { |
| 19 | + nbConnectionsRequired = len(options.UsernameList) |
| 20 | + } |
| 21 | + options.Log.Debug("Preparing a pool of " + strconv.Itoa(nbConnectionsRequired) + " connections") |
| 22 | + for i := 1; i <= nbConnectionsRequired; i++ { |
| 23 | + client, err := smtp.Dial(options.Target + ":25") |
| 24 | + if err != nil { |
| 25 | + options.Log.Error("Failed to establish a connection " + err.Error()) |
| 26 | + continue |
| 27 | + } |
| 28 | + err = client.Hello(utils.RandomString(6)) |
| 29 | + if err != nil { |
| 30 | + fmt.Println("hello" + err.Error()) |
| 31 | + } |
| 32 | + err = client.Mail(utils.RandomString(6) + "@" + options.Domain) |
| 33 | + if err != nil { |
| 34 | + fmt.Println("mail" + err.Error()) |
| 35 | + } |
| 36 | + options.connectionsPool <- client |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +func UserEnum(optionsInterface *interface{}, username string) bool { |
| 41 | + options := (*optionsInterface).(*Options) |
| 42 | + valid := false |
| 43 | + smtpConnection := <-options.connectionsPool |
| 44 | + switch strings.ToLower(options.Mode) { |
| 45 | + case "rcpt", "": |
| 46 | + err := smtpConnection.Rcpt(username) |
| 47 | + if err == nil { |
| 48 | + options.Log.Success(username) |
| 49 | + valid = true |
| 50 | + } else { |
| 51 | + options.Log.Debug(username + " => " + err.Error()) |
| 52 | + options.Log.Fail(username) |
| 53 | + } |
| 54 | + case "vrfy": |
| 55 | + err := smtpConnection.Verify(username) |
| 56 | + if err == nil { |
| 57 | + options.Log.Success(username) |
| 58 | + valid = true |
| 59 | + } else { |
| 60 | + options.Log.Debug(username + " => " + err.Error()) |
| 61 | + options.Log.Fail(username) |
| 62 | + } |
| 63 | + case "expn": |
| 64 | + err := smtpConnection.Expand(username) |
| 65 | + if err == nil { |
| 66 | + options.Log.Success(username) |
| 67 | + valid = true |
| 68 | + } else { |
| 69 | + code := strings.Split(err.Error(), " ")[0] |
| 70 | + options.Log.Debug(username + " => " + err.Error()) |
| 71 | + options.Log.Fail(username) |
| 72 | + // If the command is not implemented no need to pursue |
| 73 | + if code == "502" { |
| 74 | + CloseSMTPConnections(optionsInterface) |
| 75 | + options.Log.Fatal("The command is not implemented. No need to pursue using this method.") |
| 76 | + } |
| 77 | + fmt.Println(code) |
| 78 | + } |
| 79 | + default: |
| 80 | + CloseSMTPConnections(optionsInterface) |
| 81 | + options.Log.Fatal("Unrecognised mode: " + options.Mode + ". Only RCPT, VRFY and EXPN are supported.") |
| 82 | + } |
| 83 | + |
| 84 | + options.connectionsPool <- smtpConnection |
| 85 | + return valid |
| 86 | +} |
| 87 | + |
| 88 | +func CloseSMTPConnections(optionsInterface *interface{}) { |
| 89 | + options := (*optionsInterface).(*Options) |
| 90 | + options.Log.Debug("Closing the pool of connections") |
| 91 | + for i := 1; i <= options.Thread; i++ { |
| 92 | + select { |
| 93 | + case smtpConnection := <-options.connectionsPool: |
| 94 | + smtpConnection.Close() |
| 95 | + case <-time.After(1 * time.Second): |
| 96 | + options.Log.Debug("Something went wrong0 A connection seems already closed") |
| 97 | + } |
| 98 | + |
| 99 | + } |
| 100 | + close(options.connectionsPool) |
| 101 | +} |
0 commit comments