Skip to content

Commit

Permalink
Updating version and adding compatibility fix so we can work with dns…
Browse files Browse the repository at this point in the history
…python < 2.0.0 (#135)

(dnspython 2.x is incompatible with ipwhois)
  • Loading branch information
ianhelle committed Feb 11, 2021
1 parent 7b0b0d5 commit 59d68cd
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 15 deletions.
2 changes: 1 addition & 1 deletion conda/conda-reqs.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ beautifulsoup4>=4.6.3
bokeh>=1.4.0
cryptography>=3.1
deprecated>=1.2.4
dnspython>=2.0.0
dnspython>=1.16.0
folium>=0.9.0
ipython>=7.1.1
ipywidgets>=7.4.2
Expand Down
2 changes: 1 addition & 1 deletion msticpy/_version.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
"""Version file."""
VERSION = "0.9.0.a1"
VERSION = "0.9.0"
35 changes: 26 additions & 9 deletions msticpy/sectools/domain_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"""
from datetime import datetime
from enum import Enum
import json
import ssl
import time
Expand Down Expand Up @@ -116,7 +117,14 @@ def screenshot(url: str, api_key: str = None) -> requests.models.Response:
return image_data


# Backward compat with dnspython 1.x
# If v2.x installed use non-deprecated "resolve" method
# otherwise use "query"
_dns_resolver = Resolver()
if hasattr(_dns_resolver, "resolve"):
_dns_resolve = getattr(_dns_resolver, "resolve")
else:
_dns_resolve = getattr(_dns_resolver, "query")


@export
Expand Down Expand Up @@ -181,7 +189,7 @@ def is_resolvable(url_domain: str) -> bool: # pylint: disable=no-self-use
"""
try:
_dns_resolver.resolve(url_domain, "A")
_dns_resolve(url_domain, "A")
return True
except DNSException:
return False
Expand Down Expand Up @@ -280,7 +288,7 @@ def dns_resolve(url_domain: str, rec_type: str = "A") -> Dict[str, Any]:
"""
domain = parse_url(url_domain).host
try:
return _resolve_resp_to_dict(_dns_resolver.resolve(domain, rdtype=rec_type))
return _resolve_resp_to_dict(_dns_resolve(domain, rdtype=rec_type))
except DNSException as err:
return {
"qname": domain,
Expand All @@ -305,9 +313,7 @@ def ip_rev_resolve(ip_address: str) -> Dict[str, Any]:
"""
try:
return _resolve_resp_to_dict(
_dns_resolver.resolve_address(ip_address, raise_on_no_answer=True)
)
return _resolve_resp_to_dict(_dns_resolve(ip_address, raise_on_no_answer=True))
except DNSException as err:
return {
"qname": ip_address,
Expand All @@ -318,13 +324,24 @@ def ip_rev_resolve(ip_address: str) -> Dict[str, Any]:

def _resolve_resp_to_dict(resolver_resp):
"""Return Dns Python resolver response to dict."""
rdtype = (
resolver_resp.rdtype.name
if isinstance(resolver_resp.rdtype, Enum)
else str(resolver_resp.rdtype)
)
rdclass = (
resolver_resp.rdclass.name
if isinstance(resolver_resp.rdclass, Enum)
else str(resolver_resp.rdclass)
)

return {
"qname": str(resolver_resp.qname),
"rdtype": resolver_resp.rdtype.name,
"rdclass": resolver_resp.rdclass.name,
"rdtype": rdtype,
"rdclass": rdclass,
"response": str(resolver_resp.response),
"nameserver": resolver_resp.nameserver,
"port": resolver_resp.port,
"nameserver": getattr(resolver_resp, "nameserver", None),
"port": getattr(resolver_resp, "port", None),
"canonical_name": str(resolver_resp.canonical_name),
"rrset": [str(res) for res in resolver_resp.rrset],
"expiration": datetime.utcfromtimestamp(resolver_resp.expiration),
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ beautifulsoup4>=4.6.3
bokeh>=1.4.0
cryptography>=3.1
deprecated>=1.2.4
dnspython>=2.0.0
dnspython>=1.16.0
folium>=0.9.0
geoip2>=2.9.0
html5lib
Expand Down
26 changes: 23 additions & 3 deletions tests/test_domain_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
# license information.
# --------------------------------------------------------------------------
"""domain_utilstes extract test class."""
import os

import pytest
import pytest_check as check

from msticpy.sectools import domain_utils

Expand All @@ -30,3 +28,25 @@ def test_validate_domain_fail():
assert not resolvable
assert not blacklisted[0]
assert blacklisted[1] is None


def test_resolver_funcs():
"""Test domain utils functions."""
result = domain_utils.dns_resolve("www.microsoft.com")
check.is_not_none(result["qname"])
check.is_true(result["rrset"])
ip = result["rrset"][0]
result = domain_utils.dns_resolve("www.contoso.garbage")
check.is_not_none(result)
check.is_false(result.get("rrset"))

result = domain_utils.ip_rev_resolve(ip)
check.is_not_none(result)

result = domain_utils.dns_components("www.microsoft.com")
check.equal(result["subdomain"], "www")
check.equal(result["domain"], "microsoft")
check.equal(result["suffix"], "com")
result = domain_utils.url_components("http://www.microsoft.com")
check.equal(result["scheme"], "http")
check.equal(result["host"], "www.microsoft.com")

0 comments on commit 59d68cd

Please sign in to comment.