|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "net/url" |
| 7 | + "strings" |
| 8 | + "time" |
| 9 | + |
| 10 | + ldap "github.com/vjeantet/ldapserver" |
| 11 | + |
| 12 | + "github.com/pterm/pterm" |
| 13 | + log "github.com/sirupsen/logrus" |
| 14 | +) |
| 15 | + |
| 16 | +var LDAPServer *Server |
| 17 | + |
| 18 | +func StartServer(ctx context.Context, serverUrl string) { |
| 19 | + pterm.Info.Println("Server URL: " + serverUrl) |
| 20 | + log.Info("Server URL: " + serverUrl) |
| 21 | + |
| 22 | + listenUrl, err := url.Parse("//" + serverUrl) |
| 23 | + if err != nil { |
| 24 | + pterm.Error.Println("Failed to parse server url") |
| 25 | + log.Fatal("Failed to parse server url") |
| 26 | + } |
| 27 | + // replace ip with 0.0.0.0:port |
| 28 | + listenUrl.Host = "0.0.0.0:" + listenUrl.Port() |
| 29 | + |
| 30 | + pterm.Info.Println("Starting internal LDAP server on", listenUrl.Host) |
| 31 | + log.Info("Starting LDAP server on ", listenUrl.Host) |
| 32 | + LDAPServer = NewServer() |
| 33 | + LDAPServer.sChan = make(chan string, 10000) |
| 34 | + |
| 35 | + go LDAPServer.server.ListenAndServe(listenUrl.Host) |
| 36 | +} |
| 37 | + |
| 38 | +func (s *Server) ReportIP(vulnerableServiceLocation string) { |
| 39 | + vulnUrl, err := url.Parse("//" + vulnerableServiceLocation) |
| 40 | + if err != nil { |
| 41 | + pterm.Error.Println("Failed to parse vulnerable url: " + vulnerableServiceLocation) |
| 42 | + log.Fatal("Failed to parse server url" + vulnerableServiceLocation) |
| 43 | + } |
| 44 | + msg := fmt.Sprintf("SUCCESS: Remote addr: %s", vulnerableServiceLocation) |
| 45 | + log.Info(msg) |
| 46 | + pterm.Success.Println(msg) |
| 47 | + if s != nil && s.sChan != nil { |
| 48 | + resMsg := fmt.Sprintf("vulnerable,%s,%s,", vulnUrl.Hostname(), vulnUrl.Port()) |
| 49 | + updateCsvRecords(resMsg) |
| 50 | + s.sChan <- resMsg |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +type Server struct { |
| 55 | + server *ldap.Server |
| 56 | + sChan chan string |
| 57 | +} |
| 58 | + |
| 59 | +func (s *Server) handleBind(w ldap.ResponseWriter, m *ldap.Message) { |
| 60 | + res := ldap.NewBindResponse(ldap.LDAPResultSuccess) |
| 61 | + w.Write(res) |
| 62 | + return |
| 63 | +} |
| 64 | + |
| 65 | +func (s *Server) handleSearch(w ldap.ResponseWriter, m *ldap.Message) { |
| 66 | + r := m.GetSearchRequest() |
| 67 | + |
| 68 | + pterm.Info.Println("Got LDAP search request: " + r.BaseObject()) |
| 69 | + log.Info("Got LDAP search request: " + r.BaseObject()) |
| 70 | + |
| 71 | + vulnerableLocation := strings.ReplaceAll(string(r.BaseObject()), "_", ":") |
| 72 | + |
| 73 | + res := ldap.NewSearchResultDoneResponse(ldap.LDAPResultSuccess) |
| 74 | + w.Write(res) |
| 75 | + |
| 76 | + s.ReportIP(vulnerableLocation) |
| 77 | + |
| 78 | + return |
| 79 | +} |
| 80 | + |
| 81 | +func NewServer() *Server { |
| 82 | + s := &Server{ |
| 83 | + server: ldap.NewServer(), |
| 84 | + } |
| 85 | + |
| 86 | + ldap.Logger = log.StandardLogger() |
| 87 | + |
| 88 | + routes := ldap.NewRouteMux() |
| 89 | + routes.Bind(s.handleBind) |
| 90 | + routes.Search(s.handleSearch) |
| 91 | + |
| 92 | + s.server.Handle(routes) |
| 93 | + |
| 94 | + return s |
| 95 | +} |
| 96 | + |
| 97 | +func (s *Server) Stop() { |
| 98 | + spinnerSuccess, _ := pterm.DefaultSpinner.Start("Stopping LDAP server") |
| 99 | + time.Sleep(10 * time.Second) |
| 100 | + s.server.Stop() |
| 101 | + spinnerSuccess.Stop() |
| 102 | +} |
0 commit comments