Permalink
Browse files

Adds support for socket options for DNS connections

  • Loading branch information...
ana-cc authored and irl committed Dec 6, 2018
1 parent bdf79bf commit b6a578ac5c96b9f198378f04c10c742074d5cc34
Showing with 54 additions and 8 deletions.
  1. +9 −0 doc/helpers.rst
  2. +5 −0 doc/helpers/dns.rst
  3. +2 −1 doc/index.rst
  4. +38 −7 pathspider/helpers/dns.py
@@ -0,0 +1,9 @@
Connection Helpers
==================

.. toctree::
:glob:
:titlesonly:

helpers/*

@@ -0,0 +1,5 @@
DNS Helper
==========

.. automodule:: pathspider.helpers.dns
:members:
@@ -37,8 +37,9 @@ Table of Contents
using
plugins
observer
targets.rst
targets
plugindev/index.rst
helpers
internals

.. toctree::
@@ -10,9 +10,15 @@


class PSDNSRecord(DNSRecord):
def spider_send(self, source, job, conn_timeout, tcp=False):
def spider_send(self, source, job, conn_timeout, tcp=False, sockopts=None):
"""
Send packet to nameserver and return response and source port.
:param str source: source address for the connection
:param dict job: dictionary containing the job details
:param bool tcp: perform connection using TCP if *True*, otherwise UDP
:param list(tuple) sockopts: list of socket options to set on the
connection socket as tuples containing the level, option and value
"""
data = self.pack()
if ':' in job['dip']:
@@ -29,6 +35,9 @@ def spider_send(self, source, job, conn_timeout, tcp=False):
else:
sock.bind((source[0], 0))
sock.settimeout(conn_timeout)
sockopts = sockopts or []
for sockopt in sockopts:
sock.setsockopt(*sockopt)
sock.connect((job['dip'], job['dp']))
sock.sendall(data)
sp = sock.getsockname()[1]
@@ -56,6 +65,9 @@ def spider_send(self, source, job, conn_timeout, tcp=False):
sock.bind((source[0], sp))
sp = sock.getsockname()[1]
sock.settimeout(conn_timeout)
sockopts = sockopts or []
for sockopt in sockopts:
sock.setsockopt(*sockopt)
sock.sendto(self.pack(), (job['dip'], job['dp']))
response = None
try:
@@ -71,34 +83,53 @@ def spider_send(self, source, job, conn_timeout, tcp=False):
return (response, sp)


def connect_dns_tcp(source, job, conn_timeout):
def connect_dns_tcp(*args, **kwargs):
"""
This helper function will perform a DNS query over a TCP connection. It
will not perform any special action in the event that this is the
experimental flow, it only performs a DNS query connection.
.. deprecated:: 2.1.0
Use :py:func:`pathspider.helpers.dns.connect_dns` instead.
:param list args: positional arguments for :py:func:`pathspider.helpers.dns.connect_dns`
:param dict kwargs: keyword arguments for :py:func:`pathspider.helpers.dns.connect_dns`
"""

return connect_dns(source, job, conn_timeout, tcp=True)
return connect_dns(*args, tcp=True, **kwargs)

def connect_dns_udp(source, job, conn_timeout):
def connect_dns_udp(*args, **kwargs):
"""
This helper function will perform a DNS query over a TCP connection. It
will not perform any special action in the event that this is the
experimental flow, it only performs a DNS query connection.
.. deprecated:: 2.1.0
Use :py:func:`pathspider.helpers.dns.connect_dns` instead.
:param list args: positional arguments for :py:func:`pathspider.helpers.dns.connect_dns`
:param dict kwargs: keyword arguments for :py:func:`pathspider.helpers.dns.connect_dns`
"""

return connect_dns(source, job, conn_timeout, tcp=False)
return connect_dns(*args, tcp=False, **kwargs)

def connect_dns(source, job, conn_timeout, tcp=False):
def connect_dns(source, job, conn_timeout, tcp=False, sockopts=None):
"""
This helper function will perform a DNS query over a TCP connection. It
will not perform any special action in the event that this is the
experimental flow, it only performs a DNS query connection.
:param str source: source address for the connection
:param dict job: dictionary containing the job details
:param bool tcp: perform connection using TCP if *True*, otherwise UDP
:param list(tuple) sockopts: list of socket options to set on the
connection socket as tuples containing the level, option and value
"""

try:
q = PSDNSRecord(q=DNSQuestion(job['domain'], QTYPE.A))
response, sp = q.spider_send(source, job, conn_timeout, tcp=tcp)
response, sp = q.spider_send(source, job, conn_timeout, tcp=tcp,
sockopts=sockopts)
if response is None:
return {'sp': sp, 'spdr_state': CONN_FAILED}
return {'sp': sp, 'spdr_state': CONN_OK}

0 comments on commit b6a578a

Please sign in to comment.