-
Notifications
You must be signed in to change notification settings - Fork 20
updated dnscommon for NS, PTR, MX records and enhancement for 'AAAA' and 'TXT' records #166
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 5 commits
35cc77c
ba00027
7f73846
71d1fd1
34aac48
c2421fd
49e888f
9ea31f2
4a965e0
14f78f0
d4c2ef7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -109,6 +109,7 @@ _type_lookup_table = { | |
| 14 : 'MINFO', # Mailbox Information | ||
| 15 : 'MX', # Mail Exchange | ||
| 16 : 'TXT', # Text Strings | ||
| 28 : 'AAAA', # IPv6 address | ||
| 252 : 'AXFR', # Request for transfer of zone | ||
| 253 : 'MAILB', # Request for mailbox-related RRs | ||
| 254 : 'MAILA', # Request for mail agent RRs (Obsolete) | ||
|
|
@@ -130,6 +131,7 @@ _type_lookup_table = { | |
| 'MINFO' : 14, | ||
| 'MX' : 15, | ||
| 'TXT' : 16, | ||
| 'AAAA' : 28, | ||
| 'AXFR' : 252, | ||
| 'MAILB' : 253, | ||
| 'MAILA' : 254, | ||
|
|
@@ -494,17 +496,16 @@ def _read_single_answer(answer_index, dns_query_data): | |
| """ | ||
| # Parse answer address. | ||
| read_index, answer_name = _parse_address(answer_index, dns_query_data) | ||
|
|
||
| # Read the type. | ||
| answer_type = _type_lookup_table[ | ||
| _charpair_to_int16(dns_query_data[read_index:read_index + 2])] | ||
| read_index += 2 | ||
|
|
||
| read_index += 2 | ||
| # Read the class. | ||
| answer_class = _class_lookup_table[ | ||
| _charpair_to_int16(dns_query_data[read_index:read_index + 2])] | ||
| read_index += 2 | ||
|
|
||
| # Some math magic with the TTL. | ||
| time_to_live = _charpair_to_int16(dns_query_data[read_index:read_index + 2]) * 2 ** 16 | ||
| read_index += 2 | ||
|
|
@@ -519,9 +520,25 @@ def _read_single_answer(answer_index, dns_query_data): | |
|
|
||
| # There are a lot of different types of queries. We can parse all of these ones | ||
| # in the same way, though. | ||
| simple_answers = ['A', 'NS', 'CNAME', 'MD', 'MB', 'MF', 'MG', 'MR', 'MX', 'PTR'] | ||
| simple_answers = ['A', 'CNAME', 'MD', 'MB', 'MF', 'MG', 'MR'] | ||
| if answer_type in simple_answers: | ||
| read_index, resource_data['address'] = _parse_answer_address(read_index, dns_query_data) | ||
| elif answer_type == 'NS': | ||
| read_index, resource_data['nameserver'] = _parse_address(read_index,dns_query_data) | ||
| elif answer_type == 'PTR': | ||
| read_index, resource_data['domain'] = _parse_address(read_index, dns_query_data) | ||
| elif answer_type == 'MX': | ||
| read_index+=2 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How come the index must be modified for
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As we can see there is some values like 10,20,30... before the MX records, we need to increase index to get to correct data from raw packet. |
||
| read_index, resource_data['records'] = _parse_address(read_index, dns_query_data) | ||
| elif answer_type == 'AAAA': | ||
| read_index, resource_data['address'] = _parse_aaaa_address(read_index, dns_query_data) | ||
| elif answer_type == 'TXT': | ||
| txt_length = len(dns_query_data)-read_index | ||
| resource_text = [] | ||
| for i in range(0,txt_length): | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Coding Style?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, why does the loop exist?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As 'TXT' query contains text information like: We need controlled loop to convert raw packet to txt format and save it into answer section. |
||
| resource_text.append(dns_query_data[read_index]) | ||
| read_index+=1 | ||
| resource_data['text'] = ''.join(resource_text) | ||
| elif answer_type == 'SOA': | ||
| read_index, mname = _parse_address(read_index, dns_query_data) | ||
| resource_data['mname'] = mname | ||
|
|
@@ -552,7 +569,6 @@ def _read_single_answer(answer_index, dns_query_data): | |
| resource_data['retry'] = retry | ||
| resource_data['expire'] = expire | ||
| resource_data['minimum'] = minimum | ||
|
|
||
| answer_dict = { | ||
| 'name' : answer_name, | ||
| 'type' : answer_type, | ||
|
|
@@ -564,7 +580,60 @@ def _read_single_answer(answer_index, dns_query_data): | |
| return read_index, answer_dict | ||
|
|
||
|
|
||
| def _parse_aaaa_address(address_index, dns_query_data): | ||
| """ | ||
| <Purpose> | ||
| This method parses a IPv6 directly out of an answer | ||
| section. This method is needed because answer IPs are not delimited, | ||
| since their labels are not of variable length. | ||
|
|
||
| <Arguments> | ||
| address_index | ||
| The integer index at which the address starts. | ||
| dns_query_data | ||
| The string containing raw query data | ||
|
|
||
| <Exceptions> | ||
| If the address is improperly formatted, this will produce an integer | ||
| parsing failure. | ||
|
|
||
| <Side Effects> | ||
| None | ||
|
|
||
| <Returns> | ||
| A tuple containing the new read index and IPv6 Address. | ||
| """ | ||
| #converts packet data to hex | ||
| add = '' | ||
| address = '' | ||
| new_list = [] | ||
| for i in range(0,16): | ||
| add+=str(hex(ord(dns_query_data[address_index]))) | ||
| add+=":" | ||
| address_index += 1 | ||
| add = add[:-1] | ||
|
|
||
| #converts hex data to readable IPv6 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not use plain Python string formatting? |
||
| add_list = add.split(":") | ||
| for i in add_list: | ||
| i = i[2:] | ||
| if len(i) == 2: | ||
| new_list.append(i) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please fix indent level. |
||
| elif len(i)==1: | ||
| i = '0'+i | ||
| new_list.append(i) | ||
|
|
||
| for i in range(0,16,2): | ||
| try: | ||
| address+= new_list[i]+new_list[i+1] | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wrong indent level. See Coding Style Guidelines. |
||
| address+=':' | ||
| except IndexError: | ||
| pass | ||
| address=address[:-1] | ||
| #address = address.replace('0000','') | ||
| #address = address.replace('::',':') | ||
|
|
||
| return address_index, address | ||
|
|
||
| def _parse_answer_address(address_index, dns_query_data): | ||
| """ | ||
|
|
@@ -600,7 +669,7 @@ def _parse_answer_address(address_index, dns_query_data): | |
| address_index += 1 | ||
| address += str(ord(dns_query_data[address_index])) | ||
| address_index += 1 | ||
|
|
||
| return address_index, address | ||
|
|
||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please combine all the answer types that are treated the same way under a single
elifbranch.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should not combine all because when you query for MX or reverse or AAAA etc then you have additional section containing extra data. We don't need that but if we don't separate then packet dictionary will contain mixed details. So by separating, packet dictionary will contain all information with their respective query types.
for eg.
or