Skip to content

Commit 89ef695

Browse files
committed
add new output format
1 parent ddee38b commit 89ef695

File tree

7 files changed

+124
-10
lines changed

7 files changed

+124
-10
lines changed

README.md

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,23 @@
66

77
## 使用示例
88

9-
![get-computers](./images/img_1.png)
9+
![get-computers](./images/img_6.png)
1010

1111
### 获取域内所有机器DN、操作系统、版本号
1212

1313
```
1414
$ goDomain -username <Username> -password <Password> -base-dn <BaseDN> -host <LDAP-Server> -get-computers
1515
```
1616

17-
![get-computers](./images/img.png)
17+
![get-computers](./images/img_6.png)
1818

1919
### 获取域内所有非约束委派机器DN、操作系统、版本号
2020

2121
```
2222
$ goDomain -username <Username> -password <Password> -base-dn <BaseDN> -host <LDAP-Server> -get-unconstrained-delegation-computers
2323
```
2424

25-
![get-computers](./images/img_2.png)
25+
![get-computers](./images/img_5.png)
2626

2727
### 获取域内所有约束委派机器DN、操作系统、版本号、约束信息
2828

@@ -32,3 +32,36 @@ $ goDomain -username <Username> -password <Password> -base-dn <BaseDN> -host <LD
3232
```
3333

3434
![get-computers](./images/img_3.png)
35+
36+
### 获取域内所有用户
37+
38+
39+
```
40+
$ goDomain -username <Username> -password <Password> -base-dn <BaseDN> -host <LDAP-Server> -get-delegation-computers
41+
```
42+
43+
![get-computers](./images/img_4.png)
44+
45+
### 结果输出
46+
47+
- [x] -csv
48+
- [x] -html
49+
- [x] -markdown
50+
51+
**-html**
52+
53+
```bash
54+
$ goDomain -username <Username> -password <Password> -base-dn <BaseDN> -host <LDAP-Server> -get-users -html > /tmp/result.html
55+
```
56+
57+
**-csv**
58+
59+
```bash
60+
$ goDomain -username <Username> -password <Password> -base-dn <BaseDN> -host <LDAP-Server> -get-users -csv > /tmp/result.csv
61+
```
62+
63+
**-markdown**
64+
65+
```bash
66+
$ goDomain -username <Username> -password <Password> -base-dn <BaseDN> -host <LDAP-Server> -get-users -markdown > /tmp/result.md
67+
```

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ go 1.16
44

55
require (
66
github.com/go-ldap/ldap v2.5.1+incompatible
7+
github.com/jedib0t/go-pretty/v6 v6.2.4
78
gopkg.in/asn1-ber.v1 v1.0.0-20181015200546-f715ec2f112d // indirect
89
gopkg.in/ldap.v2 v2.5.1 // indirect
910
)

go.sum

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,19 @@
1+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
2+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3+
github.com/fzipp/gocyclo v0.3.1/go.mod h1:DJHO6AUmbdqj2ET4Z9iArSuwWgYDRryYt2wASxc7x3E=
14
github.com/go-ldap/ldap v2.5.1+incompatible h1:Opaoft5zMW8IU/VRULB0eGMBQ9P5buRvCW6sFTRmMn8=
25
github.com/go-ldap/ldap v2.5.1+incompatible/go.mod h1:qfd9rJvER9Q0/D/Sqn1DfHRoBp40uXYvFoEVrNEPqRc=
6+
github.com/jedib0t/go-pretty/v6 v6.2.4 h1:wdaj2KHD2W+mz8JgJ/Q6L/T5dB7kyqEFI16eLq7GEmk=
7+
github.com/jedib0t/go-pretty/v6 v6.2.4/go.mod h1:+nE9fyyHGil+PuISTCrp7avEdo6bqoMwqZnuiK2r2a0=
8+
github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0=
9+
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
10+
github.com/pkg/profile v1.2.1/go.mod h1:hJw3o1OdXxsrSjjVksARp5W95eeEaEfptyVZyv6JUPA=
11+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
12+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
13+
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
14+
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
15+
golang.org/x/sys v0.0.0-20180816055513-1c9583448a9c h1:uHnKXcvx6SNkuwC+nrzxkJ+TpPwZOtumbhWrrOYN5YA=
16+
golang.org/x/sys v0.0.0-20180816055513-1c9583448a9c/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
317
gopkg.in/asn1-ber.v1 v1.0.0-20181015200546-f715ec2f112d h1:TxyelI5cVkbREznMhfzycHdkp5cLA7DpE+GKjSslYhM=
418
gopkg.in/asn1-ber.v1 v1.0.0-20181015200546-f715ec2f112d/go.mod h1:cuepJuh7vyXfUyUwEgHQXw849cJrilpS5NeIjOWESAw=
519
gopkg.in/ldap.v2 v2.5.1 h1:wiu0okdNfjlBzg6UWvd1Hn8Y+Ux17/u/4nlk4CQr6tU=

images/img_4.png

463 KB
Loading

images/img_5.png

158 KB
Loading

images/img_6.png

157 KB
Loading

main.go

Lines changed: 73 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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

1011
const (
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

117123
func (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

134191
func (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

Comments
 (0)