Skip to content

Commit 0034048

Browse files
committed
Simplify code
1 parent db3e792 commit 0034048

1 file changed

Lines changed: 17 additions & 41 deletions

File tree

nxc/modules/pre2k.py

Lines changed: 17 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
import os
2-
from impacket.ldap import ldap, ldapasn1
32
from impacket.krb5.kerberosv5 import getKerberosTGT
43
from impacket.krb5.ccache import CCache
54
from impacket.krb5.types import Principal
65
from impacket.krb5 import constants
76

7+
from nxc.parsers.ldap_results import parse_result_attributes
8+
from nxc.paths import NXC_PATH
9+
10+
811
class NXCModule:
912
"""
1013
Identify pre-created computer accounts, save the results to a file, and obtain TGTs for each pre-created computer account.
11-
Module by : @shad0wcntr0ller
12-
14+
Module by: @shad0wcntr0ller
1315
"""
1416
name = "pre2k"
1517
description = "Identify pre-created computer accounts, save the results to a file, and obtain TGTs for each"
@@ -22,15 +24,7 @@ def options(self, context, module_options):
2224

2325
def on_login(self, context, connection):
2426
try:
25-
# Initialize connection to LDAP
26-
context.log.info(f"Connecting to LDAP server at ldap://{connection.host}")
27-
28-
if connection.kerberos:
29-
ldap_connection = ldap.LDAPConnection(f"ldap://{connection.host}", connection.baseDN, None)
30-
ldap_connection.kerberosLogin(connection.username, connection.password, connection.domain, lmhash=connection.lmhash, nthash=connection.nthash, aesKey=connection.aesKey, kdcHost=connection.kdcHost)
31-
else:
32-
ldap_connection = ldap.LDAPConnection(f"ldap://{connection.host}", connection.baseDN, None)
33-
ldap_connection.login(connection.username, connection.password, connection.domain, lmhash=connection.lmhash, nthash=connection.nthash)
27+
ldap_connection = connection.ldapConnection
3428

3529
# Define the search filter for pre-created computer accounts
3630
search_filter = "(&(objectClass=computer)(userAccountControl=4128))"
@@ -43,36 +37,19 @@ def on_login(self, context, connection):
4337

4438
try:
4539
# Use paged search to retrieve all computer accounts with specific flags
46-
paged_search_control = ldapasn1.SimplePagedResultsControl(criticality=True, size=1000)
47-
search_results = ldap_connection.search(searchFilter=search_filter, attributes=attributes, searchControls=[paged_search_control])
48-
49-
for item in search_results:
50-
if isinstance(item, ldapasn1.SearchResultEntry):
51-
context.log.debug(f"Raw item: {item.prettyPrint()}")
52-
53-
sam_account_name = None
54-
user_account_control = None
40+
search_results = connection.search(search_filter, attributes)
41+
results = parse_result_attributes(search_results)
42+
context.log.debug(f"Search results: {results}")
5543

56-
for attribute in item["attributes"]:
57-
context.log.debug(f"Attribute: {attribute.prettyPrint()}")
58-
if str(attribute["type"]) == "sAMAccountName":
59-
sam_account_name = str(attribute["vals"][0])
60-
elif str(attribute["type"]) == "userAccountControl":
61-
user_account_control = str(attribute["vals"][0])
62-
63-
context.log.debug(f"Processing computer: {sam_account_name}, UAC: {user_account_control}")
64-
65-
if sam_account_name and user_account_control is not None:
66-
user_account_control = int(user_account_control)
67-
68-
# Check if the account is a pre-created computer account
69-
if user_account_control == 4128: # 4096 | 32
70-
computers.append(sam_account_name)
71-
context.log.debug(f"Added computer: {sam_account_name}")
44+
for computer in results:
45+
context.log.debug(f"Processing computer: {computer['sAMAccountName']}, UAC: {computer['userAccountControl']}")
46+
# Check if the account is a pre-created computer account
47+
if int(computer["userAccountControl"]) == 4128: # 4096 | 32
48+
computers.append(computer["sAMAccountName"])
49+
context.log.debug(f"Added computer: {computer['sAMAccountName']}")
7250

7351
# Save computers to file
74-
base_dir = "/root/.nxc/DiscoveredComputers"
75-
domain_dir = os.path.join(base_dir, connection.domain)
52+
domain_dir = os.path.join(f"{NXC_PATH}/modules/pre2k", connection.domain)
7653
output_file = os.path.join(domain_dir, "precreated_computers.txt")
7754

7855
# Create directories if they do not exist
@@ -91,7 +68,7 @@ def on_login(self, context, connection):
9168
context.log.info("No pre-created computer accounts found.")
9269

9370
# Obtain TGTs and save to ccache
94-
ccache_base_dir = "/root/.nxc/ccache"
71+
ccache_base_dir = f"{NXC_PATH}/modules/pre2k/ccache"
9572
os.makedirs(ccache_base_dir, exist_ok=True)
9673

9774
successful_tgts = 0
@@ -147,4 +124,3 @@ def save_ticket(self, context, username, ticket, sessionKey, ccache_base_dir):
147124
context.log.info(f"Saved ticket in {ccache_filename}")
148125
except Exception as e:
149126
context.log.fail(f"Failed to save ticket for {username}: {e}")
150-

0 commit comments

Comments
 (0)