Repository navigation
Add DNS Module - #761
Add DNS Module#761lodos2005 wants to merge 4 commits into
Conversation
|
Thanks for the PR! Could you PR all of the structs to impacket? That is probably the better place instead of that NetExec builds its own lib. Also other people can use them if needed. |
|
fortra/impacket#1994 |
|
@lodos2005 any update on this? Could you also replace ldap3 with impackets LDAP as we now have CRUD methods? |
|
@lodos2005 r u there? |
Add dns Modüle Signed-off-by: Hakan Yavuz <lodos05@gmail.com>
Add DNS module Signed-off-by: Hakan Yavuz <lodos05@gmail.com>
77a39ff to
3317931
Compare
- Use impacket.dnsp.ADIDNSManager for the full record lifecycle instead of a hand-rolled LDAP layer, dropping the ldap3 dependency and all locally defined MS-DNSP structures (requires fortra/impacket#1994) - Reuse the already authenticated connection.ldap_connection so hash and Kerberos logins work like every other module - Replace the built-in help screen with a concise options docstring including usage examples, short aliases are parsed via a single map - Derive the default action before validation so RECORD+DATA adds, RECORD queries and no options lists zones as documented - Add the required CATEGORY attribute Signed-off-by: Hakan Yavuz <lodos05@gmail.com>
|
heyyy @NeffIsBack, lodosisback. deeply sorry for the late response. i refactored code , this now depends on fortra/impacket#1994 and ldap3 is gone (uses impacket's own LDAP CRUD via connection.ldap_connection) and some improvement |
Welcome back :) |
yep exactly |
NeffIsBack
left a comment
There was a problem hiding this comment.
A few thoughts:
- We should just implement the logic manually instead of relying on a helper logic of impacket. The logic there looks fairly simple and then we have direct control over it.
- If you run into issues because
parse_result_attributesreturns a decoded string but you need raw bytes, feel free to add exceptions for that attribute in the parsing function. - Please try to decrease line lengths by keeping e.g. logging calls one-liners. Imo makes it more readable.
- Drop the impacket.dnsp dependency: the MS-DNSP structures and all record handling now live in the module, built on impacket's LDAP CRUD methods over the already authenticated connection - Add a dnsRecord exception to parse_result_attributes so raw record bytes are returned instead of a lossy string decode - Move the option/action constants inside the class, abort from options() by returning False, keep logging calls one-liners and split active/tombstoned records with a plain loop - Validate the DATA IPv4 address before writing records Tested against an AD lab with password, NTLM hash, Kerberos, ccache and certificate authentication. Signed-off-by: Hakan Yavuz <lodos05@gmail.com>
|
Thanks for the review @NeffIsBack — all addressed in f1b3456! The module is now fully self-contained (no impacket.dnsp dependency, so CI import error gone), dnsRecord returns raw bytes via a parse_result_attributes exception, and all style points are applied (class constants, return False aborts, one-liner logging, simple tombstone loop). Tested with password/hash/Kerberos/ccache/certificate auth against an AD lab. Anything else, just ping me! |
NeffIsBack
left a comment
There was a problem hiding this comment.
Code already looking quite good. Shouldn't be much to adjust anymore
| RECORD_TYPE_MAPPING = { | ||
| 0: "ZERO", | ||
| 1: "A", | ||
| 2: "NS", | ||
| 5: "CNAME", | ||
| 6: "SOA", | ||
| 33: "SRV", | ||
| 65281: "WINS", | ||
| } | ||
|
|
||
|
|
||
| class DNS_RECORD(Structure): | ||
| """ | ||
| dnsRecord - used in LDAP | ||
| [MS-DNSP] section 2.3.2.2 | ||
| """ | ||
|
|
||
| structure = ( | ||
| ("DataLength", "<H-Data"), | ||
| ("Type", "<H"), | ||
| ("Version", "B=5"), | ||
| ("Rank", "B"), | ||
| ("Flags", "<H=0"), | ||
| ("Serial", "<L"), | ||
| ("TtlSeconds", ">L"), | ||
| ("Reserved", "<L=0"), | ||
| ("TimeStamp", "<L=0"), | ||
| ("Data", ":"), | ||
| ) | ||
|
|
||
|
|
||
| class DNS_COUNT_NAME(Structure): | ||
| """ | ||
| DNS_COUNT_NAME, used for FQDNs in LDAP communication | ||
| [MS-DNSP] section 2.2.2.2.2 | ||
| """ | ||
|
|
||
| structure = ( | ||
| ("Length", "B-RawName"), | ||
| ("LabelCount", "B"), | ||
| ("RawName", ":"), | ||
| ) | ||
|
|
||
| def toFqdn(self): | ||
| ind = 0 | ||
| labels = [] | ||
| for _i in range(self["LabelCount"]): | ||
| nextlen = struct.unpack("B", self["RawName"][ind:ind + 1])[0] | ||
| labels.append(self["RawName"][ind + 1:ind + 1 + nextlen].decode("utf-8")) | ||
| ind += nextlen + 1 | ||
| # For the final dot | ||
| labels.append("") | ||
| return ".".join(labels) | ||
|
|
||
|
|
||
| class DNS_RPC_RECORD_A(Structure): | ||
| """ | ||
| DNS_RPC_RECORD_A | ||
| [MS-DNSP] section 2.2.2.2.4.1 | ||
| """ | ||
|
|
||
| structure = (("address", ":"),) | ||
|
|
||
| def formatCanonical(self): | ||
| return socket.inet_ntoa(self["address"]) | ||
|
|
||
| def fromCanonical(self, canonical): | ||
| self["address"] = socket.inet_aton(canonical) | ||
|
|
||
|
|
||
| class DNS_RPC_RECORD_NODE_NAME(Structure): | ||
| """ | ||
| DNS_RPC_RECORD_NODE_NAME | ||
| [MS-DNSP] section 2.2.2.2.4.2 | ||
| """ | ||
|
|
||
| structure = (("nameNode", ":", DNS_COUNT_NAME),) | ||
|
|
||
|
|
||
| class DNS_RPC_RECORD_SRV(Structure): | ||
| """ | ||
| DNS_RPC_RECORD_SRV | ||
| [MS-DNSP] section 2.2.2.2.4.18 | ||
| """ | ||
|
|
||
| structure = ( | ||
| ("wPriority", ">H"), | ||
| ("wWeight", ">H"), | ||
| ("wPort", ">H"), | ||
| ("nameTarget", ":", DNS_COUNT_NAME), | ||
| ) | ||
|
|
||
|
|
||
| class DNS_RPC_RECORD_SOA(Structure): | ||
| """ | ||
| DNS_RPC_RECORD_SOA | ||
| [MS-DNSP] section 2.2.2.2.4.3 | ||
| """ | ||
|
|
||
| structure = ( | ||
| ("dwSerialNo", ">L"), | ||
| ("dwRefresh", ">L"), | ||
| ("dwRetry", ">L"), | ||
| ("dwExpire", ">L"), | ||
| ("dwMinimumTtl", ">L"), | ||
| ("namePrimaryServer", ":", DNS_COUNT_NAME), | ||
| ("zoneAdminEmail", ":", DNS_COUNT_NAME), | ||
| ) | ||
|
|
||
|
|
||
| class DNS_RPC_RECORD_TS(Structure): | ||
| """ | ||
| DNS_RPC_RECORD_TS (Tombstone Record) | ||
| [MS-DNSP] section 2.2.2.2.4.23 | ||
| """ | ||
|
|
||
| structure = (("entombedTime", "<Q"),) | ||
|
|
||
| def toDatetime(self): | ||
| microseconds = self["entombedTime"] / 10.0 | ||
| return datetime.datetime(1601, 1, 1) + datetime.timedelta(microseconds=microseconds) |
There was a problem hiding this comment.
Let's import these from impacket tho. No logic, but structures should be defined there. Just assume they exist, I will merge and switch to our fork if needed.
| opsec_safe = True | ||
| multiple_hosts = True |
There was a problem hiding this comment.
Those should be removed, they don't exist anymore
| """ | ||
| options = {self.ALIASES.get(key.upper(), key.upper()): value for key, value in module_options.items()} | ||
|
|
||
| self.action = options.get("ACTION", "").lower() |
There was a problem hiding this comment.
Just add "list" as the fallback so it is the default
| # Derive the default action before validating it | ||
| if not self.action: | ||
| if self.record and self.data: | ||
| self.action = "add" | ||
| elif self.record: | ||
| self.action = "query" | ||
| else: | ||
| self.action = "list" |
There was a problem hiding this comment.
Let's not assume any action if the user doesn't explicitly specify it. Since we have a default that is fine imo
| rootdse = parse_result_attributes(self.ldap.search(searchBase="", searchFilter="(objectClass=*)", attributes=["schemaNamingContext"], scope=ldapasn1.Scope("baseObject"))) | ||
| self.schema_root = rootdse[0]["schemaNamingContext"] if rootdse else "" |
There was a problem hiding this comment.
Let's query that in enum_host_info() since we do the exact same call there.
| self.dns_server = getattr(connection.args, "dns_server", None) or connection.host | ||
| self.dns_timeout = getattr(connection.args, "dns_timeout", None) or 3 |
There was a problem hiding this comment.
You don't need getattr, it is always available since it's not proto dependent.
| return serial + 1 | ||
| return int(datetime.datetime.now().timestamp()) | ||
|
|
||
| def _query_soa_serial(self, zone): |
There was a problem hiding this comment.
Please add some comments, this is hard to understand :D
| if not self.allow_multiple: | ||
| for data in entry["records"]: | ||
| existing = DNS_RECORD(data) | ||
| if existing["Type"] == 1: |
There was a problem hiding this comment.
Please replace the "1" everywhere with some "A record" constant. Imo this would improve readability a lot. Same for other records if you find something else
Description
This pull request introduces a new module, dns, for managing DNS records within an Active Directory-integrated DNS environment directly via LDAP. It provides administrators and pentesters with a powerful command-line tool to perform various DNS operations, from creating and deleting records to querying existing zones, without leaving the NetExec console.
This module is inspired by the functionality of dnstool.py from krbrelayx by @dirkjanm and is designed to be a robust and user-friendly addition to NetExec's capabilities.
Key Features
Help Usage

Query Usage

Forest Query Usage

Add Usage

Forest Add Usage

Add AllowMultiple Usage

Modify Usage

Remove Usage

Ldap Remove Usage

List

List-DN

Type of change
Setup guide for the review
Please provide guidance on what setup is needed to test the introduced changes, such as your locally running machine Python version & OS, as well as the target(s) you tested against, including software versions.
In particular:
Screenshots (if appropriate):
Screenshots are always nice to have and can give a visual representation of the change.
If appropriate include before and after screenshot(s) to show which results are to be expected.
Checklist:
poetry run python -m ruff check . --preview, use--fixto automatically fix what it can)