Skip to content

Commit

Permalink
Merge pull request #17 from dowsnature/Beta
Browse files Browse the repository at this point in the history
0.23
  • Loading branch information
downature authored Jan 30, 2017
2 parents 2838974 + 23bab5a commit ec164d1
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 13 deletions.
9 changes: 6 additions & 3 deletions bin/dns.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import threading
import json
import base64
import logging

logging.basicConfig(level=logging.INFO)
dict_data = {}
dict_config = {}
Remote_dns_server='114.114.114.114'
Expand Down Expand Up @@ -63,7 +65,7 @@ def SendDnsData(data,s,addr):
try:
rspdata = sock.recv(4096)
except Exception as e:
print "sock.recv info:",e
logging.warn("Recv:\t%s"%e)
break
s.sendto(rspdata ,addr)
break
Expand All @@ -74,7 +76,7 @@ def main(s):
data, addr = s.recvfrom(2048)
Tthreading(data,s,addr)
except Exception as e:
print "Unknow error :\t",e
logging.warn("Unknow error:\t%s"%e)

if __name__ == '__main__':

Expand All @@ -92,6 +94,7 @@ def main(s):
except Exception as e:
print "\nBinding failed! Please run as administrator,\n\nAnd check the local IP address and port is correct?\n"
print "==========Error message=========="
raise e
logging.critical(e)
sys.exit(-1)
print "Bind successfully! Running ..."
main(s)
30 changes: 21 additions & 9 deletions bin/dnslib/dnsfucation.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# -*- coding:utf-8 -*-

import logging
logging.basicConfig(level=logging.INFO)
def Search_key_ip(string,dict_data):
string = string[:-1]
if string in dict_data.keys():
Expand All @@ -19,13 +20,13 @@ def Hex2Ascii(string):
Int = int(string,16)
return chr(Int)
except Exception as e:
print "Hex2Ascii info:\t",e
logging.warn("Hex2Ascii info:\t%s"%e)

def Hex2Str_dec(string):
try:
return str(int(string,16))
except Exception as e:
print "Hex2Str_dec info:\t",e
logging.warn("Hex2Str_dec info:\t%s"%e)

def HexIP2DecIP(string) :
string = string.replace('\t','').replace('\n','').replace(' ','')
Expand All @@ -36,7 +37,7 @@ def HexIP2DecIP(string) :
else:
return "?.?.?.?"
except Exception as e:
print "HexIP2DecIP info:\t",e
logging("HexIP2DecIP info:\t%s"%e)

def DnshextoDomain(string,start=24,end=26):
'''原始数据:域名指针默认在24-26个字节位置,
Expand All @@ -57,7 +58,7 @@ def DnshextoDomain(string,start=24,end=26):
end +=2
return ''.join(Domain),end
except Exception as e:
print "DnshextoDomain info:\t",e
logging.warn("DnshextoDomain info:\t%s"%e)

def GetDnsDomainIP(data):
'''提取域名和IP
Expand Down Expand Up @@ -92,7 +93,7 @@ def GetDnsDomainIP(data):

return iptext
except Exception as e:
print "GetDnsDomainIP info:\t",e
logging("GetDnsDomainIP info:\t%s"%e)
return []

def analysis(data,dict_data):
Expand All @@ -112,14 +113,24 @@ def analysis(data,dict_data):
data = data.encode('hex').replace(iplist[i],dnsip).decode('hex')
return data

def IP2HEX(ip):
zone = ip.split(".")
HEX = ''
for i in zone:
i = hex(int(i)).replace("0x",'')
if len(i) < 2:
i = '0' + i
HEX += i
return HEX

def analysis2(data,dict_data):
'''构造DNS报文'''
data = data.encode('hex')
domain,end = DnshextoDomain(data)

ip = None
if len(domain) >0:
print "Query:\t",domain
logging.info("Query:\t%s"%domain)
ip = Search_key_ip(domain,dict_data)
if ip :
if data[end+2:end+4] == '1c':
Expand All @@ -129,8 +140,9 @@ def analysis2(data,dict_data):

data = data[0:4] + '81800001000100000000'+data[24:end]+'00010001c00c000100010000003f0004'
#十进制表示的IP变为十六进制表示的IP
dnsip = '{:02X}{:02X}{:02X}{:02X}'.format(*map(int, ip.split('.'))).lower()
print "Revise:\t",domain
#dnsip = '{:02X}{:02X}{:02X}{:02X}'.format(*map(int, ip.split('.'))).lower()
dnsip = IP2HEX(ip)
logging.info("Revise:\t%s"%domain)
data = data + dnsip
return 1,data.decode('hex')
return 0,data.decode('hex')
3 changes: 2 additions & 1 deletion conf/data.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"hosts":{
"racaljk":"https://coding.net/u/scaffrey/p/hosts/git/raw/master/hosts",
"yhosts":"https://raw.githubusercontent.com/vokins/yhosts/master/hosts"
"yhosts":"https://raw.githubusercontent.com/vokins/yhosts/master/hosts",
"adaway":"https://raw.githubusercontent.com/aoccin/adaway/master/hosts"
},
"DNSMasq":"I'm too lazy, you know",
"jsonb64":"https://raw.githubusercontent.com/dowsnature/dowsDNS/master/data/wrcd.base64"
Expand Down

0 comments on commit ec164d1

Please # to comment.