2022-07-18 23:59:14 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2021-11-24 19:33:14 +00:00
|
|
|
from impacket.ldap import ldapasn1 as ldapasn1_impacket
|
|
|
|
|
2023-05-02 15:17:59 +00:00
|
|
|
|
2021-11-24 19:33:14 +00:00
|
|
|
def searchResEntry_to_dict(results):
|
|
|
|
data = {}
|
2023-05-02 15:17:59 +00:00
|
|
|
for attr in results["attributes"]:
|
|
|
|
key = str(attr["type"])
|
|
|
|
value = str(attr["vals"][0])
|
2021-11-24 19:33:14 +00:00
|
|
|
data[key] = value
|
|
|
|
return data
|
|
|
|
|
2023-05-02 15:17:59 +00:00
|
|
|
|
2023-09-17 20:20:40 +00:00
|
|
|
class NXCModule:
|
2023-04-07 16:40:48 +00:00
|
|
|
"""
|
2023-05-02 15:17:59 +00:00
|
|
|
Retrieves the different Sites and Subnets of an Active Directory
|
2021-11-24 19:33:14 +00:00
|
|
|
|
2023-05-02 15:17:59 +00:00
|
|
|
Authors:
|
|
|
|
Podalirius: @podalirius_
|
2023-04-07 16:40:48 +00:00
|
|
|
"""
|
2021-11-24 19:33:14 +00:00
|
|
|
|
|
|
|
def options(self, context, module_options):
|
|
|
|
"""
|
2023-05-02 15:17:59 +00:00
|
|
|
showservers Toggle printing of servers (default: true)
|
2021-11-24 19:33:14 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
self.showservers = True
|
2023-07-03 14:41:28 +00:00
|
|
|
self.base_dn = None
|
2021-11-24 19:33:14 +00:00
|
|
|
|
2023-05-02 15:17:59 +00:00
|
|
|
if module_options and "SHOWSERVERS" in module_options:
|
2023-05-08 18:39:36 +00:00
|
|
|
if module_options["SHOWSERVERS"].lower() == "true" or module_options["SHOWSERVERS"] == "1":
|
2021-11-24 19:33:14 +00:00
|
|
|
self.showservers = True
|
2023-05-08 18:39:36 +00:00
|
|
|
elif module_options["SHOWSERVERS"].lower() == "false" or module_options["SHOWSERVERS"] == "0":
|
2021-11-24 19:33:14 +00:00
|
|
|
self.showservers = False
|
|
|
|
else:
|
|
|
|
print("Could not parse showservers option.")
|
2023-07-03 14:41:28 +00:00
|
|
|
if module_options and "BASE_DN" in module_options:
|
|
|
|
self.base_dn = module_options["BASE_DN"]
|
2021-11-24 19:33:14 +00:00
|
|
|
|
2023-05-02 15:17:59 +00:00
|
|
|
name = "subnets"
|
|
|
|
description = "Retrieves the different Sites and Subnets of an Active Directory"
|
|
|
|
supported_protocols = ["ldap"]
|
2021-11-24 19:33:14 +00:00
|
|
|
opsec_safe = True
|
|
|
|
multiple_hosts = False
|
|
|
|
|
|
|
|
def on_login(self, context, connection):
|
2023-07-03 14:41:28 +00:00
|
|
|
dn = connection.ldapConnection._baseDN if self.base_dn is None else self.base_dn
|
2021-11-24 19:33:14 +00:00
|
|
|
|
2023-05-02 15:17:59 +00:00
|
|
|
context.log.display("Getting the Sites and Subnets from domain")
|
2021-11-24 19:33:14 +00:00
|
|
|
|
2023-07-03 14:41:28 +00:00
|
|
|
try:
|
|
|
|
list_sites = connection.ldapConnection.search(
|
|
|
|
searchBase="CN=Configuration,%s" % dn,
|
|
|
|
searchFilter="(objectClass=site)",
|
|
|
|
attributes=["distinguishedName", "name", "description"],
|
|
|
|
sizeLimit=999,
|
|
|
|
)
|
|
|
|
except LDAPSearchError as e:
|
|
|
|
context.log.fail(str(e))
|
|
|
|
exit()
|
2021-11-24 19:33:14 +00:00
|
|
|
for site in list_sites:
|
|
|
|
if isinstance(site, ldapasn1_impacket.SearchResultEntry) is not True:
|
|
|
|
continue
|
|
|
|
site = searchResEntry_to_dict(site)
|
2023-05-02 15:17:59 +00:00
|
|
|
site_dn = site["distinguishedName"]
|
|
|
|
site_name = site["name"]
|
2021-11-24 19:33:14 +00:00
|
|
|
site_description = ""
|
|
|
|
if "description" in site.keys():
|
2023-05-02 15:17:59 +00:00
|
|
|
site_description = site["description"]
|
2021-11-24 19:33:14 +00:00
|
|
|
# Getting subnets of this site
|
|
|
|
list_subnets = connection.ldapConnection.search(
|
|
|
|
searchBase="CN=Sites,CN=Configuration,%s" % dn,
|
2023-05-02 15:17:59 +00:00
|
|
|
searchFilter="(siteObject=%s)" % site_dn,
|
|
|
|
attributes=["distinguishedName", "name"],
|
|
|
|
sizeLimit=999,
|
2021-11-24 19:33:14 +00:00
|
|
|
)
|
2023-05-08 18:39:36 +00:00
|
|
|
if len([subnet for subnet in list_subnets if isinstance(subnet, ldapasn1_impacket.SearchResultEntry)]) == 0:
|
2023-05-02 15:17:59 +00:00
|
|
|
context.log.highlight('Site "%s"' % site_name)
|
2021-11-24 19:33:14 +00:00
|
|
|
else:
|
|
|
|
for subnet in list_subnets:
|
2023-05-08 18:39:36 +00:00
|
|
|
if isinstance(subnet, ldapasn1_impacket.SearchResultEntry) is not True:
|
2021-11-24 19:33:14 +00:00
|
|
|
continue
|
|
|
|
subnet = searchResEntry_to_dict(subnet)
|
2023-05-02 15:17:59 +00:00
|
|
|
subnet_dn = subnet["distinguishedName"]
|
|
|
|
subnet_name = subnet["name"]
|
2021-11-24 19:33:14 +00:00
|
|
|
|
|
|
|
if self.showservers:
|
|
|
|
# Getting machines in these subnets
|
|
|
|
list_servers = connection.ldapConnection.search(
|
|
|
|
searchBase=site_dn,
|
2023-05-02 15:17:59 +00:00
|
|
|
searchFilter="(objectClass=server)",
|
|
|
|
attributes=["cn"],
|
|
|
|
sizeLimit=999,
|
2021-11-24 19:33:14 +00:00
|
|
|
)
|
2023-05-08 18:39:36 +00:00
|
|
|
if len([server for server in list_servers if isinstance(server, ldapasn1_impacket.SearchResultEntry)]) == 0:
|
2021-11-24 19:33:14 +00:00
|
|
|
if len(site_description) != 0:
|
2023-05-08 18:39:36 +00:00
|
|
|
context.log.highlight('Site "%s" (Subnet:%s) (description:"%s")' % (site_name, subnet_name, site_description))
|
2021-11-24 19:33:14 +00:00
|
|
|
else:
|
2023-05-08 18:39:36 +00:00
|
|
|
context.log.highlight('Site "%s" (Subnet:%s)' % (site_name, subnet_name))
|
2021-11-24 19:33:14 +00:00
|
|
|
else:
|
|
|
|
for server in list_servers:
|
2023-05-08 18:39:36 +00:00
|
|
|
if isinstance(server, ldapasn1_impacket.SearchResultEntry) is not True:
|
2021-11-24 19:33:14 +00:00
|
|
|
continue
|
2023-05-02 15:17:59 +00:00
|
|
|
server = searchResEntry_to_dict(server)["cn"]
|
2021-11-24 19:33:14 +00:00
|
|
|
if len(site_description) != 0:
|
2023-05-02 15:17:59 +00:00
|
|
|
context.log.highlight(
|
|
|
|
'Site "%s" (Subnet:%s) (description:"%s") (Server:%s)'
|
|
|
|
% (
|
|
|
|
site_name,
|
|
|
|
subnet_name,
|
|
|
|
site_description,
|
|
|
|
server,
|
|
|
|
)
|
|
|
|
)
|
2021-11-24 19:33:14 +00:00
|
|
|
else:
|
2023-05-08 18:39:36 +00:00
|
|
|
context.log.highlight('Site "%s" (Subnet:%s) (Server:%s)' % (site_name, subnet_name, server))
|
2021-11-24 19:33:14 +00:00
|
|
|
else:
|
|
|
|
if len(site_description) != 0:
|
2023-05-08 18:39:36 +00:00
|
|
|
context.log.highlight('Site "%s" (Subnet:%s) (description:"%s")' % (site_name, subnet_name, site_description))
|
2021-11-24 19:33:14 +00:00
|
|
|
else:
|
2023-05-08 18:39:36 +00:00
|
|
|
context.log.highlight('Site "%s" (Subnet:%s)' % (site_name, subnet_name))
|