Codebase list python-ldapdomaindump / 2c4e2f4
New upstream release. Kali Janitor 1 year, 6 months ago
8 changed file(s) with 316 addition(s) and 129 deletion(s). Raw diff Collapse all Expand all
00 The MIT License (MIT)
11
2 Copyright (c) 2016 Dirk-jan
2 Copyright (c) 2020 Dirk-jan Mollema
33
44 Permission is hereby granted, free of charge, to any person obtaining a copy
55 of this software and associated documentation files (the "Software"), to deal
00 include ldapdomaindump/style.css
1 include LICENSE Readme.md
0 # LDAPDomainDump
1 Active Directory information dumper via LDAP
2
3 ## Introduction
4 In an Active Directory domain, a lot of interesting information can be retrieved via LDAP by any authenticated user (or machine).
5 This makes LDAP an interesting protocol for gathering information in the recon phase of a pentest of an internal network.
6 A problem is that data from LDAP often is not available in an easy to read format.
7
8 ldapdomaindump is a tool which aims to solve this problem, by collecting and parsing information available via LDAP and outputting it in a human readable HTML format, as well as machine readable json and csv/tsv/greppable files.
9
10 The tool was designed with the following goals in mind:
11 - Easy overview of all users/groups/computers/policies in the domain
12 - Authentication both via username and password, as with NTLM hashes (requires ldap3 >=1.3.1)
13 - Possibility to run the tool with an existing authenticated connection to an LDAP service, allowing for integration with relaying tools such as impackets ntlmrelayx
14
15 The tool outputs several files containing an overview of objects in the domain:
16 - *domain_groups*: List of groups in the domain
17 - *domain_users*: List of users in the domain
18 - *domain_computers*: List of computer accounts in the domain
19 - *domain_policy*: Domain policy such as password requirements and lockout policy
20 - *domain_trusts*: Incoming and outgoing domain trusts, and their properties
21
22 As well as two grouped files:
23 - *domain_users_by_group*: Domain users per group they are member of
24 - *domain_computers_by_os*: Domain computers sorted by Operating System
25
26 ## Dependencies and installation
27 Requires [ldap3](https://github.com/cannatag/ldap3) > 2.0 and [dnspython](https://github.com/rthalley/dnspython)
28
29 Both can be installed with `pip install ldap3 dnspython`
30
31 The ldapdomaindump package can be installed with `python setup.py install` from the git source, or for the latest release with `pip install ldapdomaindump`.
32
33 ## Usage
34 There are 3 ways to use the tool:
35 - With just the source, run `python ldapdomaindump.py`
36 - After installing, by running `python -m ldapdomaindump`
37 - After installing, by running `ldapdomaindump`
38
39 Help can be obtained with the -h switch:
40 ```
41 usage: ldapdomaindump.py [-h] [-u USERNAME] [-p PASSWORD] [-at {NTLM,SIMPLE}]
42 [-o DIRECTORY] [--no-html] [--no-json] [--no-grep]
43 [--grouped-json] [-d DELIMITER] [-r] [-n DNS_SERVER]
44 [-m]
45 HOSTNAME
46
47 Domain information dumper via LDAP. Dumps users/computers/groups and
48 OS/membership information to HTML/JSON/greppable output.
49
50 Required options:
51 HOSTNAME Hostname/ip or ldap://host:port connection string to
52 connect to (use ldaps:// to use SSL)
53
54 Main options:
55 -h, --help show this help message and exit
56 -u USERNAME, --user USERNAME
57 DOMAIN\username for authentication, leave empty for
58 anonymous authentication
59 -p PASSWORD, --password PASSWORD
60 Password or LM:NTLM hash, will prompt if not specified
61 -at {NTLM,SIMPLE}, --authtype {NTLM,SIMPLE}
62 Authentication type (NTLM or SIMPLE, default: NTLM)
63
64 Output options:
65 -o DIRECTORY, --outdir DIRECTORY
66 Directory in which the dump will be saved (default:
67 current)
68 --no-html Disable HTML output
69 --no-json Disable JSON output
70 --no-grep Disable Greppable output
71 --grouped-json Also write json files for grouped files (default:
72 disabled)
73 -d DELIMITER, --delimiter DELIMITER
74 Field delimiter for greppable output (default: tab)
75
76 Misc options:
77 -r, --resolve Resolve computer hostnames (might take a while and
78 cause high traffic on large networks)
79 -n DNS_SERVER, --dns-server DNS_SERVER
80 Use custom DNS resolver instead of system DNS (try a
81 domain controller IP)
82 -m, --minimal Only query minimal set of attributes to limit memmory
83 usage
84 ```
85
86 ## Options
87 ### Authentication
88 Most AD servers support NTLM authentication. In the rare case that it does not, use --authtype SIMPLE.
89
90 ### Output formats
91 By default the tool outputs all files in HTML, JSON and tab delimited output (greppable). There are also two grouped files (users_by_group and computers_by_os) for convenience. These do not have a greppable output. JSON output for grouped files is disabled by default since it creates very large files without any data that isn't present in the other files already.
92
93 ### DNS resolving
94 An important option is the *-r* option, which decides if a computers DNSHostName attribute should be resolved to an IPv4 address.
95 While this can be very useful, the DNSHostName attribute is not automatically updated. When the AD Domain uses subdomains for computer hostnames, the DNSHostName will often be incorrect and will not resolve. Also keep in mind that resolving every hostname in the domain might cause a high load on the domain controller.
96
97 ### Minimizing network and memory usage
98 By default ldapdomaindump will try to dump every single attribute it can read to disk in the .json files. In large networks, this uses a lot of memory (since group relationships are currently calculated in memory before being written to disk). To dump only the minimal required attributes (the ones shown by default in the .html and .grep files), use the `--minimal` switch.
99
100 ## Visualizing groups with BloodHound
101 LDAPDomainDump includes a utility that can be used to convert ldapdomaindumps `.json` files to CSV files suitable for BloodHound. The utility is called `ldd2bloodhound` and is added to your path upon installation. Alternatively you can run it with `python -m ldapdomaindump.convert` or with `python ldapdomaindump/convert.py` if you are running it from the source.
102 The conversion tool will take the users/groups/computers/trusts `.json` file and convert those to `group_membership.csv` and `trust.csv` which you can add to BloodHound.
103
104 ## License
105 MIT
0 # LDAPDomainDump
1 Active Directory information dumper via LDAP
2
3 ## Introduction
4 In an Active Directory domain, a lot of interesting information can be retrieved via LDAP by any authenticated user (or machine).
5 This makes LDAP an interesting protocol for gathering information in the recon phase of a pentest of an internal network.
6 A problem is that data from LDAP often is not available in an easy to read format.
7
8 ldapdomaindump is a tool which aims to solve this problem, by collecting and parsing information available via LDAP and outputting it in a human readable HTML format, as well as machine readable json and csv/tsv/greppable files.
9
10 The tool was designed with the following goals in mind:
11 - Easy overview of all users/groups/computers/policies in the domain
12 - Authentication both via username and password, as with NTLM hashes (requires ldap3 >=1.3.1)
13 - Possibility to run the tool with an existing authenticated connection to an LDAP service, allowing for integration with relaying tools such as impackets ntlmrelayx
14
15 The tool outputs several files containing an overview of objects in the domain:
16 - *domain_groups*: List of groups in the domain
17 - *domain_users*: List of users in the domain
18 - *domain_computers*: List of computer accounts in the domain
19 - *domain_policy*: Domain policy such as password requirements and lockout policy
20 - *domain_trusts*: Incoming and outgoing domain trusts, and their properties
21
22 As well as two grouped files:
23 - *domain_users_by_group*: Domain users per group they are member of
24 - *domain_computers_by_os*: Domain computers sorted by Operating System
25
26 ## Dependencies and installation
27 Requires [ldap3](https://github.com/cannatag/ldap3) > 2.0 and [dnspython](https://github.com/rthalley/dnspython)
28
29 Both can be installed with `pip install ldap3 dnspython`
30
31 The ldapdomaindump package can be installed with `python setup.py install` from the git source, or for the latest release with `pip install ldapdomaindump`.
32
33 ## Usage
34 There are 3 ways to use the tool:
35 - With just the source, run `python ldapdomaindump.py`
36 - After installing, by running `python -m ldapdomaindump`
37 - After installing, by running `ldapdomaindump`
38
39 Help can be obtained with the -h switch:
40 ```
41 usage: ldapdomaindump.py [-h] [-u USERNAME] [-p PASSWORD] [-at {NTLM,SIMPLE}]
42 [-o DIRECTORY] [--no-html] [--no-json] [--no-grep]
43 [--grouped-json] [-d DELIMITER] [-r] [-n DNS_SERVER]
44 [-m]
45 HOSTNAME
46
47 Domain information dumper via LDAP. Dumps users/computers/groups and
48 OS/membership information to HTML/JSON/greppable output.
49
50 Required options:
51 HOSTNAME Hostname/ip or ldap://host:port connection string to
52 connect to (use ldaps:// to use SSL)
53
54 Main options:
55 -h, --help show this help message and exit
56 -u USERNAME, --user USERNAME
57 DOMAIN\username for authentication, leave empty for
58 anonymous authentication
59 -p PASSWORD, --password PASSWORD
60 Password or LM:NTLM hash, will prompt if not specified
61 -at {NTLM,SIMPLE}, --authtype {NTLM,SIMPLE}
62 Authentication type (NTLM or SIMPLE, default: NTLM)
63
64 Output options:
65 -o DIRECTORY, --outdir DIRECTORY
66 Directory in which the dump will be saved (default:
67 current)
68 --no-html Disable HTML output
69 --no-json Disable JSON output
70 --no-grep Disable Greppable output
71 --grouped-json Also write json files for grouped files (default:
72 disabled)
73 -d DELIMITER, --delimiter DELIMITER
74 Field delimiter for greppable output (default: tab)
75
76 Misc options:
77 -r, --resolve Resolve computer hostnames (might take a while and
78 cause high traffic on large networks)
79 -n DNS_SERVER, --dns-server DNS_SERVER
80 Use custom DNS resolver instead of system DNS (try a
81 domain controller IP)
82 -m, --minimal Only query minimal set of attributes to limit memmory
83 usage
84 ```
85
86 ## Options
87 ### Authentication
88 Most AD servers support NTLM authentication. In the rare case that it does not, use --authtype SIMPLE.
89
90 ### Output formats
91 By default the tool outputs all files in HTML, JSON and tab delimited output (greppable). There are also two grouped files (users_by_group and computers_by_os) for convenience. These do not have a greppable output. JSON output for grouped files is disabled by default since it creates very large files without any data that isn't present in the other files already.
92
93 ### DNS resolving
94 An important option is the *-r* option, which decides if a computers DNSHostName attribute should be resolved to an IPv4 address.
95 While this can be very useful, the DNSHostName attribute is not automatically updated. When the AD Domain uses subdomains for computer hostnames, the DNSHostName will often be incorrect and will not resolve. Also keep in mind that resolving every hostname in the domain might cause a high load on the domain controller.
96
97 ### Minimizing network and memory usage
98 By default ldapdomaindump will try to dump every single attribute it can read to disk in the .json files. In large networks, this uses a lot of memory (since group relationships are currently calculated in memory before being written to disk). To dump only the minimal required attributes (the ones shown by default in the .html and .grep files), use the `--minimal` switch.
99
100 ## Visualizing groups with BloodHound
101 LDAPDomainDump includes a utility that can be used to convert ldapdomaindumps `.json` files to CSV files suitable for BloodHound. The utility is called `ldd2bloodhound` and is added to your path upon installation. Alternatively you can run it with `python -m ldapdomaindump.convert` or with `python ldapdomaindump/convert.py` if you are running it from the source.
102 The conversion tool will take the users/groups/computers/trusts `.json` file and convert those to `group_membership.csv` and `trust.csv` which you can add to BloodHound. *Note that these files are only compatible with **BloodHound 1.x** which is quite old. There are no plans to support the latest version as the [BloodHound.py project](https://github.com/fox-it/BloodHound.py) was made for this. With the DCOnly collection method this tool will also only talk to LDAP and collect more information than ldapdomaindump would*.
103
104 ## Visualizing dump with a pretty output like enum4linux
105 LDAPDomainDump includes a utility that can be used to output ldapdomaindumps `.json` files to an enum4linux like output. The utility is called `ldd2pretty` and is added to your path upon installation. Alternatively you can run it with `python -m ldapdomaindump.pretty` or with `python ldapdomaindump/pretty.py` if you are running it from the source.
106
107 ## License
108 MIT
0 #!/usr/bin/env python
1 from ldapdomaindump import pretty
2 pretty.main()
0 python-ldapdomaindump (0.9.3-0kali1) UNRELEASED; urgency=low
1
2 * New upstream release.
3
4 -- Kali Janitor <[email protected]> Mon, 26 Sep 2022 16:34:22 -0000
5
06 python-ldapdomaindump (0.9.1-0kali2) kali-dev; urgency=medium
17
28 * Remove Python 2 package
2323 from __future__ import unicode_literals
2424 import sys, os, re, codecs, json, argparse, getpass, base64
2525 # import class and constants
26 from datetime import datetime
26 from datetime import datetime, timedelta
2727 try:
2828 from urllib.parse import quote_plus
2929 except ImportError:
111111 'lockoutThreshold':'Lockout Threshold',
112112 'maxPwdAge':'Max password age',
113113 'minPwdAge':'Min password age',
114 'minPwdLength':'Min password length'}
114 'minPwdLength':'Min password length',
115 'pwdHistoryLength':'Password history length',
116 'pwdProperties':'Password properties',
117 'ms-DS-MachineAccountQuota':'Machine Account Quota',
118 'flatName':'NETBIOS Domain name'}
115119
116120 MINIMAL_COMPUTERATTRIBUTES = ['cn', 'sAMAccountName', 'dNSHostName', 'operatingSystem', 'operatingSystemServicePack', 'operatingSystemVersion', 'lastLogon', 'userAccountControl', 'whenCreated', 'objectSid', 'description', 'objectClass']
117121 MINIMAL_USERATTRIBUTES = ['cn', 'name', 'sAMAccountName', 'memberOf', 'primaryGroupId', 'whenCreated', 'whenChanged', 'lastLogon', 'userAccountControl', 'pwdLastSet', 'objectSid', 'description', 'objectClass']
377381 except (LDAPAttributeError, LDAPCursorError):
378382 ugroups = []
379383 #Add the user default group
380 ugroups.append(self.getGroupCnFromDn(self.groups_dnmap[user.primaryGroupId.value]))
384 try:
385 ugroups.append(self.getGroupCnFromDn(self.groups_dnmap[user.primaryGroupId.value]))
386 # Sometimes we can't query this group or it doesn't exist
387 except KeyError:
388 pass
381389 for group in ugroups:
382390 try:
383391 groupsdict[group].append(user)
430438 #In grouped view, don't include the memberOf property to reduce output size
431439 self.userattributes_grouped = ['cn', 'name', 'sAMAccountName', 'whenCreated', 'whenChanged', 'lastLogon', 'userAccountControl', 'pwdLastSet', 'objectSid', 'description']
432440 self.groupattributes = ['cn', 'sAMAccountName', 'memberOf', 'description', 'whenCreated', 'whenChanged', 'objectSid']
433 self.policyattributes = ['cn', 'lockOutObservationWindow', 'lockoutDuration', 'lockoutThreshold', 'maxPwdAge', 'minPwdAge', 'minPwdLength', 'pwdHistoryLength', 'pwdProperties']
441 self.policyattributes = ['distinguishedName', 'lockOutObservationWindow', 'lockoutDuration', 'lockoutThreshold', 'maxPwdAge', 'minPwdAge', 'minPwdLength', 'pwdHistoryLength', 'pwdProperties', 'ms-DS-MachineAccountQuota']
434442 self.trustattributes = ['cn', 'flatName', 'securityIdentifier', 'trustAttributes', 'trustDirection', 'trustType']
435443
436444 #Escape HTML special chars
445453
446454 #Convert password max age (in 100 nanoseconds), to days
447455 def nsToDays(self, length):
448 return abs(length) * .0000001 / 86400
456 # ldap3 >= 2.6 returns timedelta
457 if isinstance(length, timedelta):
458 return length.total_seconds() / 86400
459 else:
460 return abs(length) * .0000001 / 86400
449461
450462 def nsToMinutes(self, length):
451 return abs(length) * .0000001 / 60
463 # ldap3 >= 2.6 returns timedelta
464 if isinstance(length, timedelta):
465 return length.total_seconds() / 60
466 else:
467 return abs(length) * .0000001 / 60
452468
453469 #Parse bitwise flags into a list
454470 def parseFlags(self, attr, flags_def):
455471 outflags = []
456 if attr is None:
472 if attr is None or attr.value is None:
457473 return outflags
458474 for flag, val in iteritems(flags_def):
459475 if attr.value & val:
593609 return self.formatGroupsHtml(att.values)
594610 #Primary group
595611 if aname == 'primarygroupid':
596 return self.formatGroupsHtml([self.dd.groups_dnmap[att.value]])
612 try:
613 return self.formatGroupsHtml([self.dd.groups_dnmap[att.value]])
614 except KeyError:
615 return 'NOT FOUND!'
597616 #Pwd flags
598617 if aname == 'pwdproperties':
599618 return ', '.join(self.parseFlags(att, pwd_flags))
676695 if aname == 'member' or aname == 'memberof' and type(att.values) is list:
677696 return self.formatGroupsGrep(att.values)
678697 if aname == 'primarygroupid':
679 return self.formatGroupsGrep([self.dd.groups_dnmap[att.value]])
698 try:
699 return self.formatGroupsGrep([self.dd.groups_dnmap[att.value]])
700 except KeyError:
701 return 'NOT FOUND!'
680702 #Domain trust flags
681703 if aname == 'trustattributes':
682704 return ', '.join(self.parseFlags(att, trust_flags))
0 from __future__ import print_function
1 from builtins import str
2 import argparse
3 import json
4 import os.path
5 import re
6 from time import strftime, gmtime
7
8 class PrettyOuput(object):
9
10 def d2b(self, a):
11 tbin = []
12 while a:
13 tbin.append(a % 2)
14 a //= 2
15
16 t2bin = tbin[::-1]
17 if len(t2bin) != 8:
18 for x in range(6 - len(t2bin)):
19 t2bin.insert(0, 0)
20 return ''.join([str(g) for g in t2bin])
21
22 def convert(self, time):
23 if isinstance(time, str):
24 return time
25 if time == 0:
26 return "None"
27 if time == -9223372036854775808:
28 return "Not Set"
29 sec = abs(time) // 10000000
30 days = sec // 86400
31 sec -= 86400*days
32 hrs = sec // 3600
33 sec -= 3600*hrs
34 mins = sec // 60
35 sec -= 60*mins
36 result = ""
37 if days > 1:
38 result += "{0} days ".format(days)
39 elif days == 1:
40 result += "{0} day ".format(days)
41 if hrs > 1:
42 result += "{0} hours ".format(hrs)
43 elif hrs == 1:
44 result += "{0} hour ".format(hrs)
45 if mins > 1:
46 result += "{0} minutes ".format(mins)
47 elif mins == 1:
48 result += "{0} minute ".format(mins)
49 return result
50
51 def password_complexity(self, data):
52
53 print('''
54 +-----------------------------------------+
55 | Password Policy Information |
56 +-----------------------------------------+
57 ''')
58
59 print("[+] Password Info for Domain:", data[0]['attributes']['dc'][0].upper())
60 print("\t[+] Minimum password length: ", data[0]['attributes']['instanceType'][0])
61 print("\t[+] Password history length:", data[0]['attributes']['pwdHistoryLength'][0])
62
63 password_properties = self.d2b(data[0]['attributes']['pwdProperties'][0])
64 print("\t[+] Password Complexity Flags:", password_properties)
65 print("")
66 print("\t\t[+] Domain Refuse Password Change:", password_properties[0])
67 print("\t\t[+] Domain Password Store Cleartext:", password_properties[1])
68 print("\t\t[+] Domain Password Lockout Admins:", password_properties[2])
69 print("\t\t[+] Domain Password No Clear Change:", password_properties[3])
70 print("\t\t[+] Domain Password No Anon Change:", password_properties[4])
71 print("\t\t[+] Domain Password Complex:", password_properties[5])
72 print("")
73 print("\t[+] Maximum password age:", self.convert(data[0]['attributes']['maxPwdAge'][0]))
74 print("\t[+] Minimum password age:", self.convert(data[0]['attributes']['minPwdAge'][0]))
75 print("\t[+] Reset Account Lockout Counter:", self.convert(data[0]['attributes']['lockoutDuration'][0]))
76 print("\t[+] Account Lockout Threshold:", data[0]['attributes']['lockoutThreshold'][0])
77 print("\t[+] Forced Log off Time:", self.convert(data[0]['attributes']['forceLogoff'][0]))
78
79 def domain_info(self, data):
80 print('''
81 +--------------------------------------+
82 | Getting Domain Sid For |
83 +--------------------------------------+
84 ''')
85 print('[+] Domain Name:', data[0]['attributes']['dc'][0])
86 print('Domain Sid:', data[0]['attributes']['objectSid'][0])
87 print('')
88 return data[0]['attributes']['dc'][0]
89
90 def user_info(self, users, dc):
91 print('''
92 +------------------------+
93 | Users Infos |
94 +------------------------+
95 ''')
96 for user in users:
97 desc = user['attributes'].get('description')[0] if user['attributes'].get('description') else "(null)"
98 print("Account: " + dc + "\\" + user['attributes']['sAMAccountName'][0] + "\tName: " + user['attributes']['name'][0] + "\tDesc: " + desc)
99
100 print("")
101 for user in users:
102 print("user:[" + user['attributes']['sAMAccountName'][0] + "]")
103 print("")
104
105 def groups_info(self, groups, dc):
106 print('''
107 +------------------------+import os.path
108 | Groups Infos |
109 +------------------------+
110 ''')
111 for group in groups:
112 print("group:[" + group['attributes']['name'][0] + "]")
113
114 for group in groups:
115 if group['attributes'].get('member'):
116 users = re.findall(r"^CN=([\w\s\-\_\{\}\.\$\#]+)", '\n'.join(group['attributes']['member']), re.M)
117 if users:
118 print("\n[+] Getting domain group memberships:")
119 for user in users:
120 if user == "S-1-5-11":
121 user = "NT AUTHORITY\\Authenticated Users"
122 elif user == "S-1-5-4":
123 user = "NT AUTHORITY\\INTERACTIVE"
124 elif user == "S-1-5-17":
125 user = "NT AUTHORITY\\IUSR"
126 print("Group '" + group['attributes']['name'][0] + "' has member: " + dc + "\\" + user)
127
128 def main():
129 parser = argparse.ArgumentParser(description='LDAPDomainDump to pretty output like enum4linux.')
130
131 #Main parameters
132 parser.add_argument("-d", "--directory", help="The ldapdomaindump directory where the json files are saved. Required files: domain_users.json, domain_groups.json and domain_policy.json")
133 args = parser.parse_args()
134
135 if args.directory:
136 with open(args.directory + '/domain_policy.json') as f:
137 domain = json.load(f)
138 with open(args.directory + '/domain_users.json') as f:
139 users = json.load(f)
140 with open(args.directory + '/domain_groups.json') as f:
141 groups = json.load(f)
142 pretty = PrettyOuput()
143 dc = pretty.domain_info(domain)
144 pretty.password_complexity(domain)
145 pretty.user_info(users, dc.upper())
146 pretty.groups_info(groups, dc.upper())
147 else:
148 print("Missing parameter --directory /output/")
149
150 if __name__ == "__main__":
151 main()
0 from setuptools import setup
1 setup(name='ldapdomaindump',
2 version='0.9.1',
3 description='Active Directory information dumper via LDAP',
4 author='Dirk-jan Mollema',
5 author_email='[email protected]',
6 url='https://github.com/dirkjanm/ldapdomaindump/',
7 packages=['ldapdomaindump'],
8 install_requires=['dnspython', 'ldap3==2.5.1', 'future'],
9 package_data={'ldapdomaindump': ['style.css']},
10 include_package_data=True,
11 scripts=['bin/ldapdomaindump', 'bin/ldd2bloodhound']
12 )
0 from setuptools import setup
1 setup(name='ldapdomaindump',
2 version='0.9.3',
3 description='Active Directory information dumper via LDAP',
4 author='Dirk-jan Mollema',
5 author_email='[email protected]',
6 url='https://github.com/dirkjanm/ldapdomaindump/',
7 packages=['ldapdomaindump'],
8 install_requires=['dnspython', 'ldap3>=2.5,!=2.5.2,!=2.5.0,!=2.6', 'future'],
9 package_data={'ldapdomaindump': ['style.css']},
10 include_package_data=True,
11 scripts=['bin/ldapdomaindump', 'bin/ldd2bloodhound', 'bin/ldd2pretty']
12 )