#!/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])

python dig 模拟—— DGA域名判定用的更多相关文章

  1. 用python实现模拟登录人人网

    用python实现模拟登录人人网 字数4068 阅读1762 评论19 喜欢46 我决定从头说起.懂的人可以快速略过前面理论看最后几张图. web基础知识 从OSI参考模型(从低到高:物理层,数据链路 ...

  2. Python requests模拟登录

    Python requests模拟登录 #!/usr/bin/env python # encoding: UTF-8 import json import requests # 跟urllib,ur ...

  3. python urllib2 模拟网站登陆

    python urllib2 模拟网站登陆 1. 可用浏览器先登陆,然后查看网页源码,分析登录表单 2. 使用python urllib2,cookielib 模拟网页登录 import urllib ...

  4. Python实现模拟登陆

    大家经常会用Python进行数据挖掘的说,但是有些网站是需要登陆才能看到内容的,那怎么用Python实现模拟登陆呢?其实网路上关于这方面的描述很多,不过前些日子遇到了一个需要cookie才能登陆的网站 ...

  5. Python中模拟enum枚举类型的5种方法分享

    这篇文章主要介绍了Python中模拟enum枚举类型的5种方法分享,本文直接给出实现代码,需要的朋友可以参考下   以下几种方法来模拟enum:(感觉方法一简单实用) 复制代码代码如下: # way1 ...

  6. 【小白学爬虫连载(10)】–如何用Python实现模拟登陆网站

    Python如何实现模拟登陆爬取Python实现模拟登陆的方式简单来说有三种:一.采用post请求提交表单的方式实现.二.利用浏览器登陆网站记录登陆成功后的cookies,采用get的请求方式,传入c ...

  7. python 怎么模拟加header(如User-Agent、Content-Type等等)

    # -*- coding: cp936 -*- #python 27 #xiaodeng #python 怎么模拟加header(如User-Agent.Content-Type等等) #办法一: i ...

  8. LSTM网络应用于DGA域名识别--文献翻译--更新中

    原文献名称 Predicting Domain Generation Algorithms with Long Short-Term Memory Networks原文作者 Jonathan Wood ...

  9. python爬虫模拟登陆

    python爬虫模拟登陆 学习了:https://www.cnblogs.com/chenxiaohan/p/7654667.html  用的这个 学习了:https://www.cnblogs.co ...

随机推荐

  1. 大数据学习——shell编程

    03/ shell编程综合练习 自动化软件部署脚本 3.1 需求 1.需求描述 公司内有一个N个节点的集群,需要统一安装一些软件(jdk) 需要开发一个脚本,实现对集群中的N台节点批量自动下载.安装j ...

  2. sqlserver复制表数据到另一个表

    SQL Server中,如果目标表存在: insert into 目标表 select * from 原表; SQL Server中,,如果目标表不存在: select * into 目标表 from ...

  3. 潜伏者(codevs 1171)

    题目描述 Description [问题描述]R 国和S 国正陷入战火之中,双方都互派间谍,潜入对方内部,伺机行动.历尽艰险后,潜伏于 S 国的R 国间谍小C 终于摸清了S 国军用密码的编码规则:1. ...

  4. 用DW制作简单的浮动广告

    原文发布时间为:2008-11-08 -- 来源于本人的百度文章 [由搬家工具导入] 浮动广告可以用层和时间轴结合做出,先选择你的dreamweaver“窗口”,然后从“窗口”菜单中选择“时间轴”,时 ...

  5. linux命令1——基础

    Rm 删除命令 Rm [选项][文件] 删除一个文件或者目录 选项:r 递归的删除文件夹及其子文件,f 忽略不存在的文件(不提示) (2)rm删除目录下所有文件,但不删除目录 >>rm - ...

  6. mysql 常用管理命令

    常见的管理mysql命令 (1)用于选择在MySQL工作区指定的数据库(选择数据库): USE Databasename; (2)列出了MySQL数据库管理系统中的所有可访问的数据库: SHOW DA ...

  7. FLEX中restrict限定TextInput输入

    restrict限制的意思 1. 限制某个字符的输入,用符号 ^ 跟上要限制的字符,可跟多个字符  <!-- 限制字符"~"的输入 --> <mx:TextInp ...

  8. Swift 了解

    本篇仅于个人小记,记录个人不熟悉的知识点儿.如若要了解更全,请前往如下网址:http://www.runoob.com/swift/swift-arrays.html 1.Swift 标记 分号:Sw ...

  9. 使用CEF类库处理HTTP请求

    当我们基于CEF开发应用时,可能会有URL请求处理的需求,比如HTTP下载或上传,此时可以利用CEF提供的类库来完成,而不必自己实现或引入其它第三方的类库. 在CEF里为URL Request设计了两 ...

  10. AE的Annotation学习摘记

    http://xg-357.blog.163.com/blog/static/36263124201151763512894/ IFeatureWorkspaceAnno pFWSAnno = (IF ...