Skip to content

Commit

Permalink
add new output format
Browse files Browse the repository at this point in the history
  • Loading branch information
Rvn0xsy committed Sep 22, 2021
1 parent ddee38b commit 89ef695
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 10 deletions.
39 changes: 36 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,23 @@

## 使用示例

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

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

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

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

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

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

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

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

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

![get-computers](./images/img_3.png)

### 获取域内所有用户


```
$ goDomain -username <Username> -password <Password> -base-dn <BaseDN> -host <LDAP-Server> -get-delegation-computers
```

![get-computers](./images/img_4.png)

### 结果输出

- [x] -csv
- [x] -html
- [x] -markdown

**-html**

```bash
$ goDomain -username <Username> -password <Password> -base-dn <BaseDN> -host <LDAP-Server> -get-users -html > /tmp/result.html
```

**-csv**

```bash
$ goDomain -username <Username> -password <Password> -base-dn <BaseDN> -host <LDAP-Server> -get-users -csv > /tmp/result.csv
```

**-markdown**

```bash
$ goDomain -username <Username> -password <Password> -base-dn <BaseDN> -host <LDAP-Server> -get-users -markdown > /tmp/result.md
```
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.16

require (
github.com/go-ldap/ldap v2.5.1+incompatible
github.com/jedib0t/go-pretty/v6 v6.2.4
gopkg.in/asn1-ber.v1 v1.0.0-20181015200546-f715ec2f112d // indirect
gopkg.in/ldap.v2 v2.5.1 // indirect
)
14 changes: 14 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fzipp/gocyclo v0.3.1/go.mod h1:DJHO6AUmbdqj2ET4Z9iArSuwWgYDRryYt2wASxc7x3E=
github.com/go-ldap/ldap v2.5.1+incompatible h1:Opaoft5zMW8IU/VRULB0eGMBQ9P5buRvCW6sFTRmMn8=
github.com/go-ldap/ldap v2.5.1+incompatible/go.mod h1:qfd9rJvER9Q0/D/Sqn1DfHRoBp40uXYvFoEVrNEPqRc=
github.com/jedib0t/go-pretty/v6 v6.2.4 h1:wdaj2KHD2W+mz8JgJ/Q6L/T5dB7kyqEFI16eLq7GEmk=
github.com/jedib0t/go-pretty/v6 v6.2.4/go.mod h1:+nE9fyyHGil+PuISTCrp7avEdo6bqoMwqZnuiK2r2a0=
github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0=
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/pkg/profile v1.2.1/go.mod h1:hJw3o1OdXxsrSjjVksARp5W95eeEaEfptyVZyv6JUPA=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
golang.org/x/sys v0.0.0-20180816055513-1c9583448a9c h1:uHnKXcvx6SNkuwC+nrzxkJ+TpPwZOtumbhWrrOYN5YA=
golang.org/x/sys v0.0.0-20180816055513-1c9583448a9c/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
gopkg.in/asn1-ber.v1 v1.0.0-20181015200546-f715ec2f112d h1:TxyelI5cVkbREznMhfzycHdkp5cLA7DpE+GKjSslYhM=
gopkg.in/asn1-ber.v1 v1.0.0-20181015200546-f715ec2f112d/go.mod h1:cuepJuh7vyXfUyUwEgHQXw849cJrilpS5NeIjOWESAw=
gopkg.in/ldap.v2 v2.5.1 h1:wiu0okdNfjlBzg6UWvd1Hn8Y+Ux17/u/4nlk4CQr6tU=
Expand Down
Binary file added images/img_4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/img_5.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/img_6.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
80 changes: 73 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import (
"flag"
"fmt"
"github.com/go-ldap/ldap"
"github.com/jedib0t/go-pretty/v6/table"
"os"
)

const (
LdapConnectTCP = 1
LdapConnectUDP = 2
FilterTestQuery = "(objectClass=dnsNode)"
FilterUsersQuery = "(objectClass=user)"
FilterComputerQuery = "(objectCategory=computer)"
FilterUnconstrainedDelegationComputerQuery = "(&(samAccountType=805306369)(userAccountControl:1.2.840.113556.1.4.803:=524288)(objectClass=computer))"
FilterDelegationComputerQuery = "(&(samAccountType=805306369)(msds-allowedtodelegateto=*)(objectClass=computer))"
Expand All @@ -24,8 +26,12 @@ type FlagStruct struct{
LDAPPort int
UDPConnect bool
GetComputer bool
GetUsers bool
GetUnconstrainedDelegationComputer bool
GetDelegationComputer bool
OutputCSV bool
OutputHtml bool
OutputMarkdown bool
}


Expand Down Expand Up @@ -72,7 +78,7 @@ func (ldapClient * LdapClient )ConnectLDAP(){

err = ldapClient.ldapCon.Bind(ldapClient.bindUsername, ldapClient.bindPassword)
ldapClient.checkErrorPrintExit(err)
fmt.Println("[*]Connect LDAP Server Success")
// fmt.Println("[*]Connect LDAP Server Success")
}


Expand All @@ -88,7 +94,7 @@ func (ldapClient * LdapClient )Search(query string)(ldapResults * ldap.SearchRe
)
ldapResults, err = ldapClient.ldapCon.Search(searchRequest)
ldapClient.checkErrorPrintExit(err)
fmt.Println(fmt.Sprintf("[*]Query: %s get %d entries ", query , len(ldapResults.Entries)))
// fmt.Println(fmt.Sprintf("[*]Query: %s get %d entries ", query , len(ldapResults.Entries)))
return ldapResults
}

Expand All @@ -115,7 +121,12 @@ func (ldapClient * LdapClient )Close(){


func (ldapClient * LdapClient)GetComputers(ldapResults * ldap.SearchResult) {
for _,value := range ldapResults.Entries {
count := len(ldapResults.Entries)
t := table.NewWriter()
t.SetOutputMirror(os.Stdout)
t.AppendHeader(table.Row{"#", "operatingSystem", "operatingSystemVersion", "dNSHostName", "msDS-AllowedToDelegateTo"})

for index,value := range ldapResults.Entries {
operatingSystem := value.GetAttributeValue("operatingSystem")
operatingSystemVersion := value.GetAttributeValue("operatingSystemVersion")
dNSHostName := value.GetAttributeValue("dNSHostName")
Expand All @@ -124,11 +135,57 @@ func (ldapClient * LdapClient)GetComputers(ldapResults * ldap.SearchResult) {
continue
}

fmt.Println(fmt.Sprintf("[+]HostName: %s OS: %s Version: %s",dNSHostName,operatingSystem,operatingSystemVersion ))
if allowedToDelegate != "" {
fmt.Println("[+]AllowedToDelegate : ", allowedToDelegate)
}
// fmt.Println(fmt.Sprintf("[+]HostName: %s OS: %s Version: %s",dNSHostName,operatingSystem,operatingSystemVersion ))

t.AppendRow([]interface{}{index,operatingSystem,operatingSystemVersion,dNSHostName,allowedToDelegate})
}
t.AppendSeparator()
t.AppendFooter(table.Row{"Total","", count})
t.SetStyle(table.StyleColoredBright)
if flagStruct.OutputCSV{
t.RenderCSV()
return
}

if flagStruct.OutputHtml{
t.RenderHTML()
return
}
if flagStruct.OutputMarkdown{
t.RenderMarkdown()
return
}
t.Render()
}

func (ldapClient * LdapClient)GetUsers(ldapResults * ldap.SearchResult) {
count := len(ldapResults.Entries)
t := table.NewWriter()
t.SetOutputMirror(os.Stdout)
t.AppendHeader(table.Row{"#", "sAMAccountName", "DistinguishedName"})

for index,value := range ldapResults.Entries {
distinguishedName := value.GetAttributeValue("distinguishedName")
sAMAccountName := value.GetAttributeValue("sAMAccountName")
t.AppendRow([]interface{}{index,sAMAccountName,distinguishedName})
}
t.AppendSeparator()
t.AppendFooter(table.Row{"Total","", count})
t.SetStyle(table.StyleColoredBright)
if flagStruct.OutputCSV{
t.RenderCSV()
return
}

if flagStruct.OutputHtml{
t.RenderHTML()
return
}
if flagStruct.OutputMarkdown{
t.RenderMarkdown()
return
}
t.Render()
}

func (ldapClient * LdapClient )GetEntries(ldapResults * ldap.SearchResult, attribute string) {
Expand All @@ -146,8 +203,12 @@ func init() {
flag.StringVar(&flagStruct.LDAPHost,"host","","LDAP Host")
flag.BoolVar(&flagStruct.UDPConnect,"udp",false,"UDP Connect Method (default: tcp)")
flag.BoolVar(&flagStruct.GetComputer,"get-computers",false,"Get All Computers")
flag.BoolVar(&flagStruct.GetUsers,"get-users",false,"Get All Users")
flag.BoolVar(&flagStruct.GetUnconstrainedDelegationComputer,"get-unconstrained-delegation-computers",false,"Get Unconstrained Delegation Computers")
flag.BoolVar(&flagStruct.GetDelegationComputer,"get-delegation-computers",false,"Get Delegation Computers")
flag.BoolVar(&flagStruct.OutputCSV,"csv",false,"Output CSV Format")
flag.BoolVar(&flagStruct.OutputHtml,"html",false,"Output html Format")
flag.BoolVar(&flagStruct.OutputMarkdown,"markdown",false,"Output Markdown Format")
flag.Parse()
if flagStruct.LDAPHost == "" || flagStruct.Username == "" || flagStruct.Password == ""{
flag.Usage()
Expand Down Expand Up @@ -185,4 +246,9 @@ func main() {
Dumper.GetComputers(ldapResult)
}

if flagStruct.GetUsers {
ldapResult := Dumper.Search(FilterUsersQuery)
Dumper.GetUsers(ldapResult)
}

}

0 comments on commit 89ef695

Please sign in to comment.