Skip to content
This repository was archived by the owner on Dec 6, 2023. It is now read-only.

Commit 25686f4

Browse files
author
mpgn
authored
Merge pull request #509 from @p0dalirius
Added LDAP module to list AD sites and subnets
2 parents f2ce260 + 8c9a3d3 commit 25686f4

File tree

1 file changed

+103
-0
lines changed

1 file changed

+103
-0
lines changed

cme/modules/subnets.py

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
from impacket.ldap import ldapasn1 as ldapasn1_impacket
2+
3+
def searchResEntry_to_dict(results):
4+
data = {}
5+
for attr in results['attributes']:
6+
key = str(attr['type'])
7+
value = str(attr['vals'][0])
8+
data[key] = value
9+
return data
10+
11+
class CMEModule:
12+
'''
13+
Retrieves the different Sites and Subnets of an Active Directory
14+
15+
Authors:
16+
Podalirius: @podalirius_
17+
'''
18+
19+
def options(self, context, module_options):
20+
"""
21+
showservers Toggle printing of servers (default: true)
22+
"""
23+
24+
self.showservers = True
25+
26+
if module_options and 'SHOWSERVERS' in module_options:
27+
if module_options['SHOWSERVERS'].lower() == "true" or module_options['SHOWSERVERS'] == "1":
28+
self.showservers = True
29+
elif module_options['SHOWSERVERS'].lower() == "false" or module_options['SHOWSERVERS'] == "0":
30+
self.showservers = False
31+
else:
32+
print("Could not parse showservers option.")
33+
34+
name = 'subnets'
35+
description = 'Retrieves the different Sites and Subnets of an Active Directory'
36+
supported_protocols = ['ldap']
37+
opsec_safe = True
38+
multiple_hosts = False
39+
40+
def on_login(self, context, connection):
41+
dn = ','.join(["DC=%s" % part for part in context.domain.split('.')])
42+
43+
context.log.info('Getting the Sites and Subnets from domain')
44+
45+
list_sites = connection.ldapConnection.search(
46+
searchBase="CN=Configuration,%s" % dn,
47+
searchFilter='(objectClass=site)',
48+
attributes=['distinguishedName', 'name', 'description'],
49+
sizeLimit=999
50+
)
51+
for site in list_sites:
52+
if isinstance(site, ldapasn1_impacket.SearchResultEntry) is not True:
53+
continue
54+
site = searchResEntry_to_dict(site)
55+
site_dn = site['distinguishedName']
56+
site_name = site['name']
57+
site_description = ""
58+
if "description" in site.keys():
59+
site_description = site['description']
60+
# Getting subnets of this site
61+
list_subnets = connection.ldapConnection.search(
62+
searchBase="CN=Sites,CN=Configuration,%s" % dn,
63+
searchFilter='(siteObject=%s)' % site_dn,
64+
attributes=['distinguishedName', 'name'],
65+
sizeLimit=999
66+
)
67+
if len([subnet for subnet in list_subnets if isinstance(subnet, ldapasn1_impacket.SearchResultEntry)]) == 0:
68+
context.log.highlight("Site \"%s\"" % site_name)
69+
else:
70+
for subnet in list_subnets:
71+
if isinstance(subnet, ldapasn1_impacket.SearchResultEntry) is not True:
72+
continue
73+
subnet = searchResEntry_to_dict(subnet)
74+
subnet_dn = subnet['distinguishedName']
75+
subnet_name = subnet['name']
76+
77+
if self.showservers:
78+
# Getting machines in these subnets
79+
list_servers = connection.ldapConnection.search(
80+
searchBase=site_dn,
81+
searchFilter='(objectClass=server)',
82+
attributes=['cn'],
83+
sizeLimit=999
84+
)
85+
if len([server for server in list_servers if isinstance(server, ldapasn1_impacket.SearchResultEntry)]) == 0:
86+
if len(site_description) != 0:
87+
context.log.highlight("Site \"%s\" (Subnet:%s) (description:\"%s\")" % (site_name, subnet_name, site_description))
88+
else:
89+
context.log.highlight("Site \"%s\" (Subnet:%s)" % (site_name, subnet_name))
90+
else:
91+
for server in list_servers:
92+
if isinstance(server, ldapasn1_impacket.SearchResultEntry) is not True:
93+
continue
94+
server = searchResEntry_to_dict(server)['cn']
95+
if len(site_description) != 0:
96+
context.log.highlight("Site \"%s\" (Subnet:%s) (description:\"%s\") (Server:%s)" % (site_name, subnet_name, site_description, server))
97+
else:
98+
context.log.highlight("Site \"%s\" (Subnet:%s) (Server:%s)" % (site_name, subnet_name, server))
99+
else:
100+
if len(site_description) != 0:
101+
context.log.highlight("Site \"%s\" (Subnet:%s) (description:\"%s\")" % (site_name, subnet_name, site_description))
102+
else:
103+
context.log.highlight("Site \"%s\" (Subnet:%s)" % (site_name, subnet_name))

0 commit comments

Comments
 (0)