|
1 | 1 | import random |
2 | | -import os |
3 | 2 | import sys |
4 | 3 | import contextlib |
5 | 4 |
|
6 | 5 | from os.path import isfile |
7 | 6 | from threading import BoundedSemaphore |
8 | 7 | from functools import wraps |
9 | 8 | from time import sleep |
10 | | -from datetime import datetime |
11 | 9 | from ipaddress import ip_address |
12 | 10 | from dns import resolver, rdatatype |
13 | 11 | from socket import AF_UNSPEC, SOCK_DGRAM, IPPROTO_IP, AI_CANONNAME, getaddrinfo |
|
18 | 16 | from nxc.logger import nxc_logger, NXCAdapter |
19 | 17 | from nxc.context import Context |
20 | 18 | from nxc.protocols.ldap.laps import laps_search |
21 | | -from nxc.helpers.pfx import myPKINIT, GETPAC |
| 19 | +from nxc.helpers.pfx import pfx_auth |
22 | 20 |
|
23 | | -from minikerberos.network.clientsocket import KerberosClientSocket |
24 | | -from minikerberos.common.target import KerberosTarget |
25 | | -from minikerberos.common.ccache import CCACHE |
26 | | - |
27 | | -from impacket.krb5.ccache import CCache |
28 | 21 | from impacket.dcerpc.v5 import transport |
29 | 22 |
|
30 | 23 | sem = BoundedSemaphore(1) |
@@ -563,57 +556,7 @@ def login(self): |
563 | 556 | self.logger.fail("You must specify a username when using certificate authentication") |
564 | 557 | return False |
565 | 558 | with sem: |
566 | | - # Static DH params because the ones generated by cryptography are considered unsafe by AD for some weird reason |
567 | | - dhparams = { |
568 | | - "p": int("00ffffffffffffffffc90fdaa22168c234c4c6628b80dc1cd129024e088a67cc74020bbea63b139b22514a08798e3404ddef9519b3cd3a431b302b0a6df25f14374fe1356d6d51c245e485b576625e7ec6f44c42e9a637ed6b0bff5cb6f406b7edee386bfb5a899fa5ae9f24117c4b1fe649286651ece65381ffffffffffffffff", 16), |
569 | | - "g": 2 |
570 | | - } |
571 | | - self.logger.info("Loading certificate and key from file") |
572 | | - |
573 | | - # Load the certificate and key from file |
574 | | - if self.args.pfx_cert or self.args.pfx_base64: |
575 | | - pfx = self.args.pfx_cert if self.args.pfx_cert else self.args.pfx_base64 |
576 | | - ini = myPKINIT.from_pfx(pfx, self.args.pfx_pass, dhparams, bool(self.args.pfx_base64)) |
577 | | - elif self.args.cert_pem and self.args.key_pem: |
578 | | - ini = myPKINIT.from_pem(self.args.cert_pem, self.args.key_pem, dhparams) |
579 | | - else: |
580 | | - self.logger.fail("You must either specify a PFX file + optional password or a combination of Cert PEM file and Private key PEM file") |
581 | | - return None |
582 | | - |
583 | | - username = self.args.username[0] |
584 | | - log_ccache = os.path.expanduser(f"~/.nxc/logs/{self.hostname}_{self.host}_{datetime.now().strftime('%Y-%m-%d_%H%M%S')}-{username}.ccache".replace(":", "-")) |
585 | | - |
586 | | - # Request a TGT with the cert data |
587 | | - req = ini.build_asreq(self.domain, username) |
588 | | - self.logger.info("Requesting TGT") |
589 | | - |
590 | | - sock = KerberosClientSocket(KerberosTarget(self.host)) |
591 | | - try: |
592 | | - res = sock.sendrecv(req) |
593 | | - except Exception as e: |
594 | | - self.logger.fail(str(e)) |
595 | | - return False |
596 | | - |
597 | | - encasrep, session_key, cipher, key = ini.decrypt_asrep(res.native) |
598 | | - ccache_minikerberos = CCACHE() |
599 | | - ccache_minikerberos.add_tgt(res.native, encasrep) |
600 | | - ccache_minikerberos.to_file(log_ccache) |
601 | | - self.logger.info(f"Saved TGT to file { log_ccache }") |
602 | | - self.logger.info(f"Using Kerberos Cache { log_ccache }") |
603 | | - ccache = CCache.loadFile(log_ccache) |
604 | | - principal = f"krbtgt/{self.domain.upper()}@{self.domain.upper()}" |
605 | | - creds = ccache.getCredential(principal) |
606 | | - if creds is not None: |
607 | | - tgt = creds.toTGT() |
608 | | - dumper = GETPAC(username, self.domain, self.host, key, tgt) |
609 | | - nthash = dumper.dump() |
610 | | - if not self.kerberos: |
611 | | - self.hash_login(self.domain, username, nthash) |
612 | | - else: |
613 | | - self.kerberos_login(self.domain, username, "", nthash, "", self.kdcHost, False) |
614 | | - |
615 | | - self.logger.info("Successfully authenticated using Certificate") |
616 | | - return True |
| 559 | + return pfx_auth(self) |
617 | 560 |
|
618 | 561 | if hasattr(self.args, "laps") and self.args.laps: |
619 | 562 | self.logger.debug("Trying to authenticate using LAPS") |
|
0 commit comments