python dig 模拟—— DGA域名判定用
#!/usr/bin/env python import dns.resolver, sys def get_domain_ip(domain):
"""Get the DNS record, if any, for the given domain."""
dns_records = list()
try:
# get the dns resolutions for this domain
dns_results = dns.resolver.query(domain)
dns_records = [ip.address for ip in dns_results]
except dns.resolver.NXDOMAIN as e:
print "the domain does not exist so dns resolutions remain empty. domain:", domain
except dns.resolver.NoAnswer as e:
print "the resolver is not answering so dns resolutions remain empty, domain:", domain
return dns_records hostname = sys.argv[1]
print "Recursive name lookup (simulates dig)..." n=hostname
try:
while True:
for rdata in dns.resolver.query(n, 'CNAME') :
print n, "cname is", rdata
n=rdata.target
except:
print get_domain_ip(n)
例如:
python dig_ip.py 8264.com
Recursive name lookup (simulates dig)...
8264.com cname is qaz2d84guo7uz5q2.gfnormal01at.com.
[u'121.29.18.91'] =>IP地址
python dig_ip.py www.baidu.com
Recursive name lookup (simulates dig)...
www.baidu.com cname is www.a.shifen.com.
www.a.shifen.com. cname is www.wshifen.com.
[u'103.235.46.39', u'103.235.46.40'] =>IP地址
来一个无查询结果的 DGA域名:
python dig_ip.py s09xo3-l5domek9ck5ct3go4m.com
Recursive name lookup (simulates dig)...
the domain does not exist so dns resolutions remain empty. domain: s09xo3-l5domek9ck5ct3go4m.com
[]
其中,dns.resolver.NoAnswer会在查询类别错误时候跑出此异常,例如:
python dig_ip.py www.baidu.com
Recursive name lookup (simulates dig)...
www.baidu.com cname is www.a.shifen.com.
www.a.shifen.com. cname is www.wshifen.com.
The DNS response does not contain an answer to the question: www.wshifen.com. IN CNAME
最后重构下代码:
#!/usr/bin/env python import dns.resolver, sys def get_domain_ip(domain):
"""Get the DNS record, if any, for the given domain."""
dns_records = list()
try:
# get the dns resolutions for this domain
dns_results = dns.resolver.query(domain)
dns_records = [ip.address for ip in dns_results]
except dns.resolver.NXDOMAIN as e:
print "the domain does not exist so dns resolutions remain empty. domain:", domain
except dns.resolver.NoAnswer as e:
print "the resolver is not answering so dns resolutions remain empty, domain:", domain
return dns_records def dig_ip(n):
try:
while True:
for rdata in dns.resolver.query(n, 'CNAME') :
print n, "cname is", rdata
n=rdata.target
except Exception as e:
print e
return get_domain_ip(n) if __name__ == "__main__":
print "Recursive name lookup (simulates dig)..."
print dig_ip(sys.argv[1])
最新文章
- CSS3新特性,绘制常见图形
- [Erlang 0108] Elixir 入门
- Linux中cp和scp命令的使用方法
- git详细教程
- jquery(1.3.2)<;--json-->;spring(3.0)
- 3.4.2内核下的I2C驱动
- loadmore &; scroll
- DTO学习系列之AutoMapper(五)----当EntityFramework爱上AutoMapper
- volatile解析(转)
- 服务端性能测试 TPS
- Supercomputer 解题报告
- Java自动内存管理机制学习(二):垃圾回收器与内存分配策略
- Spring Boot 与 OAuth2 官方最详细教程
- BZOJ2084 [Poi2010]Antisymmetry Manachar
- iOS开发-UIView扩展CGRect
- 2018GIAC全球互联网架构大会上海站最新日程抢先看!
- python 正则基本方法
- 还在手动给css加前缀?no!几种自动处理css前缀的方法简介
- Swift网络封装库Moya中文手册之Targets
- TortoiseGit 使用 HTTP 方式每次 PUSH 无需输入密码的方法
热门文章
- 大数据学习——hive的sql练习
- hrbust-1909理工门外的树,不用线段数,贪心思路~~
- 【最小费用最大流】N. April Fools&#39; Problem (medium)
- RedisDesktopManager 踩坑之旅
- T2627 村村通 codevs
- tomcat配置访问项目时不需要加项目名称
- linux 中断机制浅析
- Oracle冷备和热备脚本
- [转]图解eclipse 查看原始类出现The jar file rt.jar has no source attachment
- HDU 5288(OO’s Sequence-区间互质情况统计)