o
    j6h                     @   sZ   d dl Z d dlZd dlmZ G dd dZG dd dZG dd dZG d	d
 d
eZdS )    N)ProtocolPacketc                   @   s2  e Zd ZdZeddZeddZeddZeddZeddZ	eddZ
eddZeddZed	dZeddZed
dZeddZeddZeddZeddZeddZeddZeddZeddZeddZeddZeddZeddZeddZeddZeddZeddZeddZ eddZ!dS )DNSFlagsz&Bitmap with the flags of a dns packet.0000000000000000   100000000000000001000000000000000010000000000000000010000000000001001000000000000000010000000000000000100000000000000001000000000000000010000000000000000010000000000000000100000000000000001000000000000000010000000000000011000000000000000010000000000000101000000000000001100000000000001110000000000000000100000000000010010000000000000101N)"__name__
__module____qualname____doc__intQR_QUERYQR_RESPONSEOP_STANDARD_QUERYOP_INVERSE_QUERYOP_STATUS_QUERY	OP_NOTIFY	OP_UPDATEAA_NOT_AUTH_ANSWERAA_AUTH_ANSWERTC_NOT_TRUNCATEDTC_TRUNCATEDRD_NOT_RECURSIVE_QUERYRD_RECURSIVE_QUERYRA_NOT_AVAILABLERA_AVAILABLEZAUTHENTICATED_DATACHECKING_DISABLEDRCODE_NO_ERRORRCODE_FORMAT_ERRORRCODE_SERVER_FAILURERCODE_NAME_ERRORRCODE_NOT_IMPLEMENTEDRCODE_REFUSEDRCODE_YXDOMAINRCODE_YXRRSETRCODE_NXRRSETRCODE_NOAUTHRCODE_NOTZONE r=   r=   /root/aizidognhua/tmp/workspace/projects/ec89d86c-575f-41c9-af57-ac45cbdbf775/venv/lib/python3.10/site-packages/impacket/dns.pyr   )   s>    



























r   c                   @   s(  e Zd ZdZdZdZdZdZdZdZ	dZ
d	Zd
ZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZ dZ!d Z"d!Z#d"Z$d#Z%d$Z&d%Z'd&Z(d'Z)d(Z*d)Z+d*Z,d+Z-d,Z.d-Z/d.Z0d/Z1d0Z2d1Z3d2Z4d3Z5d4Z6d5Z7d6Z8d7Z9d8Z:d9Z;d:Z<d;Z=d<Z>d=Z?d>Z@d?ZAd@ZBdAZCdBZDdCZEdDZEeFdEdF ZGdGS )HDNSType   r                     	   
                                                                      !   "   #   $   %   &   '   (   )   *   +   ,   -   .   /   0   1   2   3   7   8   9   c   d   e   f   g                        i   i  c                 C   ,   t tj D ]\}}|| kr|  S qd S N)listr?   __dict__itemstypeitemvaluer=   r=   r>   getTypeName   
   zDNSType.getTypeNameN)Hr   r   r   ANSMDMFCNAMESOAMBMGMRNULLWKSPTRHINFOMINFOMXTXTRPAFSDBX25ISDNRTNSAPNSAP_PTRSIGKEYPXGPOSAAAALOCNXTEIDNBNBSTATATMANAPTRKXCERTA6DNAMESINKOPTAPLDSSSHFPIPSECKEYRRSIGNSECDNSKEYDHCIDNSEC3
NSEC3PARAMHIPNINFORKEYSPFUINFOUIDGIDUNSPECTKEYTSIGIXFRAXFRMAILBMAILAALLDNSSECstaticmethodr   r=   r=   r=   r>   r?   S   s    r?   c                   @   s0   e Zd ZdZdZdZdZdZdZe	dd Z
d	S )
DNSClassr   r@   rA   rB   r   r   c                 C   r   r   )r   r   r   r   r   r=   r=   r>   getClassName   r   zDNSClass.getClassNameN)r   r   r   RESERVEDINCHHSNONEANYr   r   r=   r=   r=   r>   r      s    r   c                   @   s  e Zd ZdZdZdZdZdZdZdZ	dZ
dZdZdZdZdZeddZeddZdVddZd	d
 Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd  Zd!d" Z d#d$ Z!d%d& Z"d'd( Z#d)d* Z$d+d, Z%d-d. Z&d/d0 Z'd1d2 Z(d3d4 Z)dWd6d7Z*d8d9 Z+d:d; Z,d<d= Z-d>d? Z.d@dA Z/dBdC Z0dDdE Z1dFdG Z2dHdI Z3dJdK Z4dLdM Z5dNdO Z6dPdQ Z7dRdS Z8dTdU Z9dS )XDNSzThe Message Header is present in all messages. Never empty.
    Contains various flags and values which control the transaction.r   rB   1100000000111111Nc                 C   s4   d| _ d| _t| | j | j |r| | d S d S )NrJ   r   )_DNS__HEADER_BASE_SIZE_DNS__TAIL_SIZEr   __init__load_packet)selfaBufferr=   r=   r>   r      s   zDNS.__init__c                 C      | j dS )Get 16 bit message ID.r   headerget_wordr   r=   r=   r>   get_transaction_id      zDNS.get_transaction_idc                 C      | j d| dS )Set 16 bit message ID.r   Nr   set_wordr   r   r=   r=   r>   set_transaction_id      zDNS.set_transaction_idc                 C   r   )r   r   r   r   r=   r=   r>   get_transaction_id_tcp   r   zDNS.get_transaction_id_tcpc                 C   r   )r   r   Nr   r   r=   r=   r>   set_transaction_id_tcp   r   zDNS.set_transaction_id_tcpc                 C   r   )Get 16 bit flags.r   r   r   r=   r=   r>   	get_flags   r   zDNS.get_flagsc                 C   r   )Set 16 bit flags.r   Nr   r   r=   r=   r>   	set_flags   r   zDNS.set_flagsc                 C   r   )r   rB   r   r   r=   r=   r>   get_flags_tcp   r   zDNS.get_flags_tcpc                 C   r   )r   rB   Nr   r   r=   r=   r>   set_flags_tcp   r   zDNS.set_flags_tcpc                 C   r   )UGet Unsigned 16 bit integer specifying the number of entries in the question section.rB   r   r   r=   r=   r>   get_qdcount   r   zDNS.get_qdcountc                 C   r   )USet Unsigned 16 bit integer specifying the number of entries in the question section.rB   Nr   r   r=   r=   r>   set_qdcount   r   zDNS.set_qdcountc                 C   r   )r   rD   r   r   r=   r=   r>   get_qdcount_tcp   r   zDNS.get_qdcount_tcpc                 C   r   )r   rD   Nr   r   r=   r=   r>   set_qdcount_tcp   r   zDNS.set_qdcount_tcpc                 C   r   )z[Get Unsigned 16 bit integer specifying the number of resource records in the answer sectionrD   r   r   r=   r=   r>   get_ancount   r   zDNS.get_ancountc                 C   r   )z[Set Unsigned 16 bit integer specifying the number of resource records in the answer sectionrD   Nr   r   r=   r=   r>   set_ancount  r   zDNS.set_ancountc                 C   r   )zkGet Unsigned 16 bit integer specifying the number of name server resource records in the authority section.rF   r   r   r=   r=   r>   get_nscount  r   zDNS.get_nscountc                 C   r   )zkSet Unsigned 16 bit integer specifying the number of name server resource records in the authority section.rF   Nr   r   r=   r=   r>   set_nscount	  r   zDNS.set_nscountc                 C   r   )zhGet Unsigned 16 bit integer specifying the number of resource records in the additional records section.rH   r   r   r=   r=   r>   get_arcount  r   zDNS.get_arcountc                 C   r   )zhSet Unsigned 16 bit integer specifying the number of resource records in the additional records section.rH   Nr   r   r=   r=   r>   set_arcount  r   zDNS.set_arcountc                 C      |   d S zGet a list of the DNS Question.r   )_DNS__get_questionsr   r=   r=   r>   get_questions  r   zDNS.get_questionsc           	      C   s   g }d}|   }|  }t|D ]>}| ||\}}|||| j  }|| j7 }|||| j  }|| j7 }td|d }td|d }||||f q||fS )Nr   !H)	r   get_body_as_stringrangeparseCompressedMessage_DNS__TYPE_LEN_DNS__CLASS_LENstructunpackappend	r   auxoffsetqdcountdata_qnameqtypeqclassr=   r=   r>   __get_questions     

zDNS.__get_questionsc                 C   r  r  )_DNS__get_questions_tcpr   r=   r=   r>   get_questions_tcp)  r   zDNS.get_questions_tcpc           	      C   s   g }d}|   }|  }t|D ]>}| ||\}}|||| j  }|| j7 }|||| j  }|| j7 }td|d }td|d }||||f q||fS )Nr   r  r   )	r   r	  r
  r  r  r  r  r  r  r  r=   r=   r>   __get_questions_tcp-  r  zDNS.__get_questions_tcpr   c                 C   s   |t |kr
tdtd|||d  d }|d@ dkrKtd|||d  d }|d@ | j }||kr;td	|d7 }| ||d }||fS |dkrW|d7 }|d
fS |d7 }||||  }||7 }| ||\}}|su||fS ||d | fS )z2Parse compressed message defined on rfc1035 4.1.4.z>No more data to parse. Offset is bigger than length of buffer.Br@   r      r  r   i?  z\The infinite loop is in DNS decompression. Encountered pointer points to the current offset.    .)len	Exceptionr  r  r   r  )r   bufr  bytepointernameunnamedr=   r=   r>   r  =  s*   zDNS.parseCompressedMessagec                 C   r  Nr   )_DNS__get_answersr   r=   r=   r>   get_answersZ     zDNS.get_answersc                 C   r  r*  )_DNS__get_authoritativer   r=   r=   r>   get_authoritative]  r-  zDNS.get_authoritativec                 C   r  r*  )_DNS__get_additionalsr   r=   r=   r>   get_additionals`  r-  zDNS.get_additionalsc                 C       |   d }|  }| ||S )Nr@   )r  r   _DNS__process_answer_structure)r   r  ancountr=   r=   r>   __get_answersc  s   zDNS.__get_answersc                 C   r2  )z$Get a list of the DNS Authoritative.r@   )r+  r   r3  )r   r  nscountr=   r=   r>   __get_authoritativeh     zDNS.__get_authoritativec                 C   r2  )z)Get a list of the DNS Additional Records.r@   )r.  r  r3  )r   r  arcountr=   r=   r>   __get_additionalsn  r8  zDNS.__get_additionalsc              
   C   sn  g }|   }t|D ]}| ||\}}|||| j  }td|d }|| j7 }|||| j  }td|d }|| j7 }|||| j  }	td|	d }
|| j7 }|||| j  }td|d }|| j7 }i }|t	j
krt||||  |d< || j7 }n |t	jkr| ||\}}||d< | ||\}}||d< td|||| j  d |d< || j7 }td|||| j  d |d< || j7 }td|||| j  d |d	< || j7 }td|||| j  d |d
< || j7 }td|||| j  d |d< || j7 }n|t	jkr9td|||| j  d |d< | ||\}}||d< no|t	jksK|t	jksK|t	jkr]| ||\}}t|d|d< nK|t	jkr|}td|	dd d }td|	dd d }td|	dd d }||||  |d< ||7 }||||||||f q
||7 }|||||
|f q
||fS )Nr  r   z!L	IPAddress	PrimaryNSAdminMBSerialNumberRefreshIntervalRetryIntervalExpirationLimit
MinimumTTL
PreferenceMailExchangerasciiNamer  r@   r   rB   RDATA)r	  r
  r  r  r  r  r  _DNS__TTL_LEN_DNS__RDLENGTH_LENr?   r   socket	inet_ntoa_DNS__TYPE_A_LENr   _DNS__SERIAL_LEN_DNS__REFRESH_LEN_DNS__RETRY_LEN_DNS__EXPIRATION_LEN_DNS__MINTTL_LENr   _DNS__PREF_LENr   r   r   strdecoder   r  )r   r  numr  r  r  r  r  r  qttl_rawqttl	qrdlengthqrdata	primaryNsadminMbmailExchr(  udp_payload_size	ext_rcodeversionflagsr=   r=   r>   __process_answer_structuret  sl   




"
"
"
"
""
$	zDNS.__process_answer_structurec                 C   s   | j S r   )r   r   r=   r=   r>   get_header_size  s   zDNS.get_header_sizec              
   C   s  d}|   }|  }|  }|  }|  }|  }|d7 }|tj@ r(|d7 }n|d7 }|d||f 7 }|d||f 7 }|d||f 7 }|d||f 7 }|d	||f 7 }|d
||f 7 }|dkr|d7 }|  }|	  |r|
 \}	}
}t|	dt|
|
t||f}|d| 7 }|sn|dkr|d7 }|  }|	  |r|
 \}	}
}}}t|	dt|
|
t|||t|f}|d| 7 }|s|dkr|d7 }|  }|	  |r|
 \}	}
}}}t|	dt|
|
t|||t|f}|d| 7 }|s|dkrd|d7 }|  }|D ]Q}|d }
|
tjkr?|\}	}
}}}}}t|
|
||||t|d f}|d| 7 }q|\}	}
}}}t|	dt|
|
t|||t|f}|d| 7 }q|S )Nr!  zDNS z	RESPONSE
zQUERY
z! - Transaction ID -- [0x%04x] %d
z! - Flags ----------- [0x%04x] %d
z! - QdCount --------- [0x%04x] %d
z! - AnCount --------- [0x%04x] %d
z! - NsCount --------- [0x%04x] %d
z! - ArCount --------- [0x%04x] %d
r   z - Questions:
rE  z8  * Domain: %s - Type: %s [0x%04x] - Class: %s [0x%04x]
z - Answers:
zO  * Domain: %s - Type: %s [0x%04x] - Class: %s [0x%04x] - TTL: %d seconds - %s
z - Authoritative:
z - Additionals:
r@   rG  z  * Name: <Root> - Type: %s [0x%04x] - udp payload size: [%d] - extended RCODE: [0x%02x] - EDNS0 version: [0x%02x] - Z Flags: [0x%02x] - RDATA: [%s]
)r   r   r   r   r   r  r   r!   r  reversepoprS  rT  r?   r   r   r   r,  reprr/  r1  r   )r   residr`  r  r4  r6  r9  	questionsr  r  r  formatanswersrW  rY  authoritativeadditionals
additionalr]  r^  r_  r=   r=   r>   __str__  sr   

$,
,
 ,zDNS.__str__c                 C   s0   |   dkrdS |  d }|  d | }|S Nr   r!  r@   )r   r  r	  )r   questions_offsetraw_datar=   r=   r>   __get_questions_raw  
   zDNS.__get_questions_rawc                 C   <   |   dkrdS |  d }|  d }|  || }|S ro  )r   r  r+  r	  )r   rp  answers_offsetrq  r=   r=   r>   __get_answers_raw     zDNS.__get_answers_rawc                 C   rt  ro  )r   r+  r.  r	  )r   ru  authoritative_offsetrq  r=   r=   r>   __get_authoritative_raw  rw  zDNS.__get_authoritative_rawc                 C   s0   |   dkrdS |  d }|  |d  }|S ro  )r  r.  r	  )r   rx  rq  r=   r=   r>   __get_additionals_raw#  rs  zDNS.__get_additionals_rawc                 C   s\   |   }|  }|  }|  }||7 }|| | | }| | |  d }| | dS )zAdd a raw answerr@   N)_DNS__get_questions_raw_DNS__get_answers_raw_DNS__get_authoritative_raw_DNS__get_additionals_raw	load_bodyr   r   )r   
answer_rawquestions_rawanswers_rawauthoritative_rawadditionals_rawbodycur_answer_countr=   r=   r>   
add_answer*  s   
zDNS.add_answerc                 C   s.   |   }|D ]}|d }|tjkr dS qdS )Nr@   TF)r1  r?   r   )r   rl  r   response_typer=   r=   r>   is_edns0:  s   
zDNS.is_edns0r   )r   ):r   r   r   r   r  r  _DNS__POINTER_LENrH  rI  rL  rM  rN  rO  rP  rQ  rR  r   _DNS__IS_POINTER_DNS__OFFSETMASKr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r  r  r  r  r  r  r,  r/  r1  r+  r.  r0  r3  rb  rn  r{  r|  r}  r~  r  r  r=   r=   r=   r>   r      sn    



RCr   )rJ  r  impacket.ImpactPacketr   r   r?   r   r   r=   r=   r=   r>   <module>   s   "*Q