首页 > 编程语言 >python dig 模拟—— DGA域名判定用

python dig 模拟—— DGA域名判定用

时间:2023-06-01 14:05:18浏览次数:39  
标签:domain DGA resolver python ip dig dns com

 

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

 

标签:domain,DGA,resolver,python,ip,dig,dns,com
From: https://blog.51cto.com/u_11908275/6393458

相关文章

  • 代码重复检查工具——python的使用CPD比较好用,clone digger针对py2
    代码重复检测:cpd--minimum-tokens100--filesg:\source\python\--languagepython>log.txt输出类似:=====================================================================Founda381line(1849tokens)duplicationinthefollowingfiles:Startingatline24of......
  • python通过文件操作字典
    python通过文件操作字典python把字典保存到文件中python从文件中加载字典importjsonmy_dict={'Apple':4,'Banana':2,'Orange':6,'Grapes':11,'area1':[[23,56],[66,12],[68,89],[90,890]]}#保存文件tf=open("myDictionary.js......
  • 6道Python简单的测试题,你知道答案吗?
    学Python光掌握基础理论知识是不够的,我们需要将理论知识转化为实战技能,本篇文章小编为大家整理了6道Python简单的测试题,快来检测一下你的Python基础怎么样!1、以下代码的输出结果为:print(round(-3.6))A.-4B.-4.0C.-3D.-3.02、以下代码的输出结果为......
  • python mock使用
    Overviewmock 是一个用于单元测试的Python库,它使用mock模拟系统中如class,method等部分,并且断言它们是如何被调用的。在编写单元测试时,mock非常适合模拟数据库,web服务器等依赖外部的场景。本文是mock的入门篇,主要介绍mock的基本用法。除了mock外,还有许多其它的moc......
  • python 合并k个有序链表
     fromheapqimportheappush,heappopclassSolution:defmergeKLists(self,lists):q=[]fori,headinenumerate(lists):ifhead:heappush(q,(head.val,i,head))node=dummy=ListNode(0)......
  • python代码规范 自动优化工具Black
    自动优化工具Black在众多代码格式化工具中,Black算是比较新的一个,它***的特点是可配置项比较少,个人认为这对于新手来说是件好事,因为我们不必过多考虑如何设置Black,让Black自己做决定就好。1).安装与使用与pylint类似,直接pipinstallblack即可完成该模块的安装,不过black依赖于Pyth......
  • python--PythonMagick安装过程
    一、发现问题使用:pipinstallpythonmagick安装出错:1.打开网站下载自己python版本对应的pythonmagick版本其中的cp310,cp39之类就是对应Python的版本,不知道的也可以使用pipdebug--verbose查看2.打开CMD窗口,进入到pythonmagick的安装目录3.使用import命令将jpg、png图......
  • python之struct模块处理二进制
    嵌入式开发中,有时需要对二进制流文件进行读写操作,一种方法是将二进制流文件转换为c语言数组形式。这样可以使用python的struct模块,python的struct模块可以方便的进行字节与二进制之间的相互转换。1struct模块常用的几个函数函数说明struct.pack(format,v1,v2,...)......
  • python selenium框架解决ip代理框不能自动化登录,解决pyautogui开不了多线程问题
    有时候我们使用python自动化框架的时候,打开一个网页的时候,它会出现出线这一种登录框,我们f12检查不了,用开发者工具强制检查里面没有任何属性.那这时候我们就可以用到python第三方库:pyautoguiPyAutoGUI:是一个Python库,可用于自动化GUI(图形用户界面)程序的任务。它可以让Pytho......
  • Python批量填补遥感影像的无效值NoData
      本文介绍基于Python中ArcPy模块,对大量栅格遥感影像文件批量进行无效值(NoData值)填充的方法。  在处理栅格图像文件时,我们经常会遇到图像中存在有无效值(即NoData值)的情况。如下图所示,这里有一个矢量面要素图层和该矢量图层范围对应的一景栅格图像;可以看到,由于该栅格图像存在......