DNS隧道通信的检测



DNS 隧道通信


DNS 隧道通信是C&C常用的通信方式,一般常用的编码方式Base64,Binary编码,NetBios编码等,Hex编码等。且请求的Type一般都是txt(为了返回的时候能够加入更多的信息)。payload部分一般是子域名。攻击者自己控一个域的DNS权威应答服务器,然后等待失陷主机请求域名,本地DNS服务器迭代查询转发请求到那台权威DNS,从而实现失陷主机与C&C Server的通信。

DNS 检测方案


+  频率大:一般远超正常的DNS频率;
+ 不重复:一般子域名变化很多,很少重复;但不排除网络不通一直发送前几个指令的请求;
+ 文本类型:一般为了传输更多数据,使用TXT类型;
+ 域名很长,有一段base64、2进制或16进制的编码段的域名;

备注: 可以根据以上做个打分,但是打分机制还没有想好,下面代码中只是一个小例子

+ 规则一:domain长度大于52,每增加一个,增加0.5分;

+ 规则二:出现出去26个字符和点以及-的字符串,则乘以2;

+ 规则三:如果请求type是TXT则严重怀疑,分数乘以2;

代码


https://github.com/cisp/DNSTunnelDetectTools

效果:

#!/usr/bin/env python
# -*- coding:utf-8 -*- """
DNS隧道通信检测工具
作者:陈然
版本:V1.0.3
联系:WeChat-Number -> cr1914518025
""" #脚本信息配置:
_author = "隐私保护"
_nicky = "挖洞的土拨鼠"
_version = "v1.0.3"
_version_string = """\033[0;32m
DNS隧道通信检测工具
作者:陈然
版本:V1.0.3
联系:WeChat-Number -> cr1914518025
操作系统:支持Linux、Unix、MacOS X、Windows
\033[0m""" #引入依赖的库文见、包
import os
import sys
import time
import pcap
import dpkt
import urllib
import logging
import platform
import datetime
from optparse import OptionParser #配置全局设置
reload(sys)
sys.setdefaultencoding("utf-8")
logging.basicConfig(filename="./dnstunneldetect.running.log",level=logging.INFO,filemode='a',format='%(asctime)s-%(levelname)s:%(message)s') #定义全局函数
def dns_request_analyst(string,sport):
"""解DNS请求报文"""
logging.info("分析报文请求")
dnsdata = dpkt.dns.DNS(string)
ret = repr(string)#.replace("\\x03",".").replace("\\x05",".").replace("\\x12",".")
domain = str(ret[41:-21])[3:]#.replace("")
rtype = ret.replace("\\x","")[-9:][0:4]
#print type(domain)
domain = domain.replace("\\x",".")
domainlist = domain.split(".")
domain = domainlist[0]+"."
for dstr in domainlist[1:]:
dstr = dstr[2:]
domain += str(dstr)+"."
domain = domain[0:-1]
score = float(len(domain) - 52.0) * 0.5
if len(domain) <= 30:
score = 0
elif (len(domain) - 52) <= 0:
score = (52 - len(domain)) * 0.2
for item in list(str(domain)):
if item not in list("01234567890-abcdefghijklmnopqrstuvwxyz."):
score *= 2
break
if rtype == '0010':
score *= 2
else:
score = score * 0.4
pid = None
if platform.platform().lower().find("windows") >= 0:
pid = os.popen("netstat -ano | findstr %s"%sport).read().split("\n")[0].split(" ")[-1]
elif platform.platform().lower().find("linux") >= 0:
pid = os.popen("netstat -anop | grep %s | awk '{print $7}'"%sport).read().split("/")[0]
elif platform.platform().lower().find("darwin") >= 0:
pid = os.popen("lsof -nP | grep :%s | awk '{print $2}'"%sport).read().split("\n")
for i in pid:
if i != "['']":
pid = i
break
else:
pass
flag = False
if score > 4
return True,domain,score,pid #定义DNS嗅探解析报文获取类
class Packet_Sniffer_Filter:
"""嗅探并过滤报文"""
def __init__(self,iterfacename):
"""创建报文嗅探器"""
logging.info("创建嗅探器")
self.name = iterfacename#本机的嗅探网卡名称
self.sniffer = pcap.pcap(name=self.name,immediate=True)#设置嗅探器嗅探指定网卡
self.sniffer.setfilter("udp port 53")#初步过滤
def run(self):
logging.info("嗅探器线程开始运行")
for packet_time,packet_data in self.sniffer:
packet = dpkt.ethernet.Ethernet(packet_data)#使用dpkt解pcap格式报文
dip = tuple(map(ord,list(packet.data.dst)))#获取目的IP地址
dip = str(dip).replace(",",".").replace(" ","")[1:-1]
sport = packet.data.data.sport
dport = packet.data.data.dport
if dport != 53:
continue
result_flag,domain,score,processid = dns_request_analyst(packet.data.data.data,sport)#加入待分析队列
if result_flag:
print """\033[0;31m
[*] 疑似DNS隧道通信
[-] 通信域名: %s
[-] 来源端口: %s
[-] 危险评分: %s
[-] 对端地址: %s
[-] 本地进程: %s
\033[0m"""%(domain[3:],sport,score,dip,processid) if __name__ == "__main__":
logging.info("程序启动")
parser = OptionParser()
parser.add_option("-i","--ifname",dest="name",help="Interface Name!")
parser.add_option("-v","--version",dest="version",action="store_true",help="Show Version!")
parser.add_option("-d","--docs",dest="docs",action="store_true",help="Show Documents!")
parser.add_option("-r","--requirments",dest="reqr",action="store_true",help="Show Requriments!")
(options, arges) = parser.parse_args()
if options.version:
print _version_string
exit(0)
if options.docs:
print """\033[0;32m
使用手册--使用于V1.0.1版本
[1] python DNSTunnelDetect.py -i eth1
\033[0"""
exit(0)
if options.reqr:
print """\033[0;32m
[+] sudo pip install pypcap
[+] sudo pip install dpkt
\033[0"""
exit(0)
if options.name in ["",None]:
logging.info("程序缺乏网卡参数,退出运行!")
print "\033[0;31m[-] 请指定网卡\033[0m"
exit(0)
logging.info("程序初始化")
PacketSniffer = Packet_Sniffer_Filter(options.name)
PacketSniffer.run()

DNS隧道通信的检测的更多相关文章

  1. PowerCat DNS 隧道通信

    powercat 也是一套基于 DNS 通信协议的工具.Powercat的dns的通信是基于dnscat设计的(其服务端就是dnscat).在使用dnscat时需要进行下载和编译. dnscat服务端 ...

  2. 一次误报引发的DNS检测方案的思考:DNS隧道检测平民解决方案

    摘自:http://www.freebuf.com/articles/network/149328.html 通过以上分析得出监控需要关注的几个要素:长域名.频率.txt类型.终端是否对解析ip发起访 ...

  3. DNS隧道和工具

    DNS Tunneling及相关实现 转自:http://www.freebuf.com/sectool/112076.html DNS Tunneling,是隐蔽信道的一种,通过将其他协议封装在DN ...

  4. DNS隧道基础

    DNS协议是一种请求/应答协议,也是一种可用于应用层的隧道技术.虽然激增的DNS流量可能会被发现,但基于传统socket隧道已经濒临淘汰鸡TCP.UDP通信大量被防御系统拦截的状况,DNS.ICMP. ...

  5. dns隧道攻击原理及常用工具流量分析

    DNS协议是一种请求应答协议,也是一种可用于应用层的隧道技术.虽然DNS流量的异常变化可能会被发现,但是在基于传统socket隧道已经濒临淘汰,TCP.UDP通信大量被安全设备拦截的大背景下,DNS. ...

  6. DNS隧道实战&&cobaltstrike利用dns隧道

    前言 使用 dns 隧道进行 tcp 通信. 正文 首先配置域名 配置一个 A 记录指向我们的 vps, 然后配置几个 ns 记录,指向刚刚设置的 A 记录 然后在服务端安装 wget https:/ ...

  7. DNS隧道工具dns2tcp

    DNS隧道工具dns2tcp   在很多网络环境中,防火墙会限制出站流量,主机往往只能访问外网主机有限的几个端口,如DNS的53端口.这时,就可以通过DNS请求和响应机制,建立通信隧道.Kali Li ...

  8. DNS隧道

    自己使用的dns隧道通过两种方法,一种是通过dnscat2工具,另一种通过cs上的beacon来进行通信. 第一种方法:dnscat2: 参考文章:https://xz.aliyun.com/t/22 ...

  9. DNS隧道工具:iodine使用

      iodine可以通过一台dns服务器制造一个IPv4数据通道,特别适合在目标主机只能发送dns请求的网络中环境中使用.iodine是基于C语言开发的,分为服务端程序iodined和客户端程序iod ...

随机推荐

  1. kettle7.1无法从Mongo中读取数据

    今天使用kettle读取mongo数据库时,刚开始一直无法读取数据: 在配置项中偶然选择了一个nearest然后成功了,麻蛋. 然后百度查询了下Read Reference是干嘛的,原来是读取源的模式 ...

  2. Android代码内存优化建议-Android官方篇

    转自:http://androidperformance.com/ http://developer.android.com/intl/zh-cn/training/displaying-bitmap ...

  3. 结合使用 Oracle 和 Ruby on Rails 的补充

    本文是对此文的补充: 结合使用 Oracle 和 Ruby on Rails http://www.oracle.com/technetwork/cn/tutorials/rubyrails-0959 ...

  4. android LayoutInflater 笔记

    LayoutInflater类用于查找布局文件并实例化.用于动态将布局加入界面中. 参考链接 http://blog.csdn.net/guolin_blog/article/details/1292 ...

  5. MFC中的UpdateData()

    UpdateData()是MFC的窗口函数,用来刷新数据的,参数只有一个,默认为TRUE 简单的说: UpdateData(TRUE) == 将控件的值赋值给成员变量, UpdateData(FALS ...

  6. 科技发烧友之单反佳能700d中高端

    http://detail.zol.com.cn/series/15/15795_1.html 前三 佳能 尼康 索尼 佳能5d 1.6w 佳能70d 5k 佳能6d 9k 佳能d7100 5k 尼康 ...

  7. ffplay(2.0.1)中的音视频同步

    最近在看ffmpeg相关的一些东西,以及一些播放器相关资料和代码. 然后对于ffmpeg-2.0.1版本下的ffplay进行了大概的代码阅读,其中这里把里面的音视频同步,按个人的理解,暂时在这里作个笔 ...

  8. jQuery的Ajax操作小结——$.ajax和$.getJSON等用法小结

    一.$.ajax用法与举例 jQuery.ajax(url,[settings])     ——返回值:XMLHttpRequest 通过 HTTP 请求加载远程数据,这个是jQuery 的底层 AJ ...

  9. Linux(Ubuntu)下搭建ASP.NET Core环境

    今天来学习一下ASP.NET Core 运行在Ubuntu中.无需安装mono . 环境 Ubuntu 14.04.4 LTS 服务器版 全新安装系统. 下载地址:http://mirrors.neu ...

  10. u3d调用c++ dll的DllNotFoundExceion 问题

    原文地址:http://blog.csdn.net/boren31/article/details/8778504 问题年年有,今年特别多. 开发环境: Windows  XP sp3 Visual  ...