@@ -4,13 +4,15 @@ import (
44 "flag"
55 "fmt"
66 "github.com/go-ldap/ldap"
7+ "github.com/jedib0t/go-pretty/v6/table"
78 "os"
89)
910
1011const (
1112 LdapConnectTCP = 1
1213 LdapConnectUDP = 2
1314 FilterTestQuery = "(objectClass=dnsNode)"
15+ FilterUsersQuery = "(objectClass=user)"
1416 FilterComputerQuery = "(objectCategory=computer)"
1517 FilterUnconstrainedDelegationComputerQuery = "(&(samAccountType=805306369)(userAccountControl:1.2.840.113556.1.4.803:=524288)(objectClass=computer))"
1618 FilterDelegationComputerQuery = "(&(samAccountType=805306369)(msds-allowedtodelegateto=*)(objectClass=computer))"
@@ -24,8 +26,12 @@ type FlagStruct struct{
2426 LDAPPort int
2527 UDPConnect bool
2628 GetComputer bool
29+ GetUsers bool
2730 GetUnconstrainedDelegationComputer bool
2831 GetDelegationComputer bool
32+ OutputCSV bool
33+ OutputHtml bool
34+ OutputMarkdown bool
2935}
3036
3137
@@ -72,7 +78,7 @@ func (ldapClient * LdapClient )ConnectLDAP(){
7278
7379 err = ldapClient .ldapCon .Bind (ldapClient .bindUsername , ldapClient .bindPassword )
7480 ldapClient .checkErrorPrintExit (err )
75- fmt .Println ("[*]Connect LDAP Server Success" )
81+ // fmt.Println("[*]Connect LDAP Server Success")
7682}
7783
7884
@@ -88,7 +94,7 @@ func (ldapClient * LdapClient )Search(query string)(ldapResults * ldap.SearchRe
8894 )
8995 ldapResults , err = ldapClient .ldapCon .Search (searchRequest )
9096 ldapClient .checkErrorPrintExit (err )
91- fmt .Println (fmt .Sprintf ("[*]Query: %s get %d entries " , query , len (ldapResults .Entries )))
97+ // fmt.Println(fmt.Sprintf("[*]Query: %s get %d entries ", query , len(ldapResults.Entries)))
9298 return ldapResults
9399}
94100
@@ -115,7 +121,12 @@ func (ldapClient * LdapClient )Close(){
115121
116122
117123func (ldapClient * LdapClient )GetComputers (ldapResults * ldap.SearchResult ) {
118- for _ ,value := range ldapResults .Entries {
124+ count := len (ldapResults .Entries )
125+ t := table .NewWriter ()
126+ t .SetOutputMirror (os .Stdout )
127+ t .AppendHeader (table.Row {"#" , "operatingSystem" , "operatingSystemVersion" , "dNSHostName" , "msDS-AllowedToDelegateTo" })
128+
129+ for index ,value := range ldapResults .Entries {
119130 operatingSystem := value .GetAttributeValue ("operatingSystem" )
120131 operatingSystemVersion := value .GetAttributeValue ("operatingSystemVersion" )
121132 dNSHostName := value .GetAttributeValue ("dNSHostName" )
@@ -124,11 +135,57 @@ func (ldapClient * LdapClient)GetComputers(ldapResults * ldap.SearchResult) {
124135 continue
125136 }
126137
127- fmt .Println (fmt .Sprintf ("[+]HostName: %s OS: %s Version: %s" ,dNSHostName ,operatingSystem ,operatingSystemVersion ))
128- if allowedToDelegate != "" {
129- fmt .Println ("[+]AllowedToDelegate : " , allowedToDelegate )
130- }
138+ // fmt.Println(fmt.Sprintf("[+]HostName: %s OS: %s Version: %s",dNSHostName,operatingSystem,operatingSystemVersion ))
139+
140+ t .AppendRow ([]interface {}{index ,operatingSystem ,operatingSystemVersion ,dNSHostName ,allowedToDelegate })
141+ }
142+ t .AppendSeparator ()
143+ t .AppendFooter (table.Row {"Total" ,"" , count })
144+ t .SetStyle (table .StyleColoredBright )
145+ if flagStruct .OutputCSV {
146+ t .RenderCSV ()
147+ return
148+ }
149+
150+ if flagStruct .OutputHtml {
151+ t .RenderHTML ()
152+ return
153+ }
154+ if flagStruct .OutputMarkdown {
155+ t .RenderMarkdown ()
156+ return
157+ }
158+ t .Render ()
159+ }
160+
161+ func (ldapClient * LdapClient )GetUsers (ldapResults * ldap.SearchResult ) {
162+ count := len (ldapResults .Entries )
163+ t := table .NewWriter ()
164+ t .SetOutputMirror (os .Stdout )
165+ t .AppendHeader (table.Row {"#" , "sAMAccountName" , "DistinguishedName" })
166+
167+ for index ,value := range ldapResults .Entries {
168+ distinguishedName := value .GetAttributeValue ("distinguishedName" )
169+ sAMAccountName := value .GetAttributeValue ("sAMAccountName" )
170+ t .AppendRow ([]interface {}{index ,sAMAccountName ,distinguishedName })
131171 }
172+ t .AppendSeparator ()
173+ t .AppendFooter (table.Row {"Total" ,"" , count })
174+ t .SetStyle (table .StyleColoredBright )
175+ if flagStruct .OutputCSV {
176+ t .RenderCSV ()
177+ return
178+ }
179+
180+ if flagStruct .OutputHtml {
181+ t .RenderHTML ()
182+ return
183+ }
184+ if flagStruct .OutputMarkdown {
185+ t .RenderMarkdown ()
186+ return
187+ }
188+ t .Render ()
132189}
133190
134191func (ldapClient * LdapClient )GetEntries (ldapResults * ldap.SearchResult , attribute string ) {
@@ -146,8 +203,12 @@ func init() {
146203 flag .StringVar (& flagStruct .LDAPHost ,"host" ,"" ,"LDAP Host" )
147204 flag .BoolVar (& flagStruct .UDPConnect ,"udp" ,false ,"UDP Connect Method (default: tcp)" )
148205 flag .BoolVar (& flagStruct .GetComputer ,"get-computers" ,false ,"Get All Computers" )
206+ flag .BoolVar (& flagStruct .GetUsers ,"get-users" ,false ,"Get All Users" )
149207 flag .BoolVar (& flagStruct .GetUnconstrainedDelegationComputer ,"get-unconstrained-delegation-computers" ,false ,"Get Unconstrained Delegation Computers" )
150208 flag .BoolVar (& flagStruct .GetDelegationComputer ,"get-delegation-computers" ,false ,"Get Delegation Computers" )
209+ flag .BoolVar (& flagStruct .OutputCSV ,"csv" ,false ,"Output CSV Format" )
210+ flag .BoolVar (& flagStruct .OutputHtml ,"html" ,false ,"Output html Format" )
211+ flag .BoolVar (& flagStruct .OutputMarkdown ,"markdown" ,false ,"Output Markdown Format" )
151212 flag .Parse ()
152213 if flagStruct .LDAPHost == "" || flagStruct .Username == "" || flagStruct .Password == "" {
153214 flag .Usage ()
@@ -185,4 +246,9 @@ func main() {
185246 Dumper .GetComputers (ldapResult )
186247 }
187248
249+ if flagStruct .GetUsers {
250+ ldapResult := Dumper .Search (FilterUsersQuery )
251+ Dumper .GetUsers (ldapResult )
252+ }
253+
188254}
0 commit comments