天天看点

在NS2 AODV协议中添加blackhole attacker(黑洞攻击) [转载]

原文地址:http://blog.csdn.net/qinleopard/article/details/6426379

---------------------------------------------

在NS2-3.34中添加黑洞攻击的过程还是比较简单的,具体过程大致如下描述:

1. 首先我们在aodv/aodv.h中的AODV类中添加一个标志该Agent(该节点是blackhole的标志)

[cpp]  view plain copy

  1. class AODV:  public Agent {  
  2. int blackhole;  //是否是攻击节点  
  3. }  

2.修改aodv/aodv.cc以实现blackhole 攻击

首先是在command 中定义相应的TCL “blackhole”

[cpp]  view plain copy

  1. if (strcmp(argv[1], "blackhole") == 0) {  
  2.                         printf("exsits blackhole/n");  
  3.             blackhole = 1;  
  4.             return TCL_OK;  
  5.         }  

接下来根据AODV协议和blackhole attack 的特点,我们实现blackhole attack的攻击过程(具体的攻击手段就是

在接收到某个节点发来的路由请求后,黑洞攻击节点不是查看路由表是否由到达目的节点的路由,从而转发或回复一个RREP。

取而代之的是,在它接收到一个RREQ后,立即回复一个RREP路由回复包,说他有到达目的节点的最优路径。而且当黑洞攻击节点

接收到数据包时,全部丢掉,从而形成一个像黑洞一样的攻击,数据包只进不出。具体详细关于黑洞攻击请google了解):

修改aodv.cc 中的recvRequest(Packet *p)函数:

[cpp]  view plain copy

  1. void  
  2. AODV::recvRequest(Packet *p) {  
  3.     if(debug>1) printf("recvRequest/n");  
  4.     struct hdr_ip *ih = HDR_IP(p);  
  5.     struct hdr_aodv_request *rq = HDR_AODV_REQUEST(p);  
  6.     aodv_rt_entry *rt;  
  7.     if (rq->rq_src == index) {  
  8. #ifdef DEBUG  
  9.         //fprintf(stderr, "%s: got my own REQUEST/n", __FUNCTION__);  
  10. #endif // DEBUG  
  11.         Packet::free(p); //如果是自己发出来的就直接丢弃  
  12.         return;  
  13.     }  
  14.     if (id_lookup(rq->rq_src, rq->rq_bcast_id)) {  
  15. #ifdef DEBUG  
  16.         //fprintf(stderr, "%s: discarding request/n", __FUNCTION__);  
  17. #endif // DEBUG  
  18.       // printf("我之前已经接收到了这个分组!and my id is %d /n", index);  
  19.         Packet::free(p); //这个分组已经收到过  
  20.         return;  
  21.     }  
  22.     id_insert(rq->rq_src, rq->rq_bcast_id);  
  23.     aodv_rt_entry *rt0; // rt0 is the reverse route  
  24.     rt0 = rtable.rt_lookup(rq->rq_src);  
  25.     if (rt0 == 0) {   
  26.         // create an entry for the reverse route.  
  27.         rt0 = rtable.rt_add(rq->rq_src);  
  28.     }  
  29.     rt0->rt_expire = max(rt0->rt_expire, (CURRENT_TIME + REV_ROUTE_LIFE));  
  30.     if ( (rq->rq_src_seqno > rt0->rt_seqno ) ||  
  31.             ((rq->rq_src_seqno == rt0->rt_seqno) &&  
  32.              (rq->rq_hop_count < rt0->rt_hops)) ) {  
  33.         // If we have a fresher seq no. or lesser #hops for the  
  34.         // same seq no., update the rt entry. Else don't bother.  
  35.         rt_update(rt0, rq->rq_src_seqno, rq->rq_hop_count, ih->saddr(),  
  36.                 max(rt0->rt_expire, (CURRENT_TIME + REV_ROUTE_LIFE)) );  
  37.         if (rt0->rt_req_timeout > 0.0) {  
  38.             // Reset the soft state and  
  39.             // Set expiry time to CURRENT_TIME + ACTIVE_ROUTE_TIMEOUT  
  40.             // This is because route is used in the forward direction,  
  41.             // but only sources get benefited by this change  
  42.             rt0->rt_req_cnt = 0;  
  43.             rt0->rt_req_timeout = 0.0;  
  44.             rt0->rt_req_last_ttl = rq->rq_hop_count;  
  45.             rt0->rt_expire = CURRENT_TIME + ACTIVE_ROUTE_TIMEOUT;  
  46.         }  
  47.         assert (rt0->rt_flags == RTF_UP);  
  48.         Packet *buffered_pkt;  
  49.         while ((buffered_pkt = rqueue.deque(rt0->rt_dst))) {  
  50.             if (rt0 && (rt0->rt_flags == RTF_UP)) {  
  51.                 assert(rt0->rt_hops != INFINITY2);  
  52.                 forward(rt0, buffered_pkt, NO_DELAY);  
  53.             }  
  54.         }  
  55.     }  
  56.     // End for putting reverse route in rt table  
  57.     rt = rtable.rt_lookup(rq->rq_dst);  
  58. // First check if I am the destination ..  
  59.     if (rq->rq_dst == index) {  
  60. #ifdef DEBUG  
  61.         fprintf(stderr, "%d - %s: destination sending reply/n",  
  62.                 index, __FUNCTION__);  
  63. #endif // DEBUG  
  64.         //printf("I am the desitination and my ip address is %d/n", index);  
  65.         // Just to be safe, I use the max. Somebody may have  
  66.         // incremented the dst seqno.  
  67.         seqno = max(seqno, rq->rq_dst_seqno) + 1;  
  68.         if (seqno % 2) seqno++;  
  69.         sendReply(rq->rq_src,           // IP Destination  
  70.                 1,                    // Hop Count  
  71.                 index,                // Dest IP Address  
  72.                 seqno,                // Dest Sequence Num  
  73.                         rq->rq_src,  
  74.                 MY_ROUTE_TIMEOUT,     // Lifetime  
  75.                 rq->rq_timestamp);    // timestamp  
  76.         Packet::free(p);  
  77.     }  
  78. // I am not the destination, but I may have a fresh enough route.  
  79. //Start balckhole Code  
  80. //a Blackhole attacker always say that have the route to be a sink.  
  81.     else if ((rt && blackhole == 1)) {  
  82.         assert(rq->rq_dst == rt->rt_dst);  
  83.                 //printf("I am the blackhole node and blackhole = %d/n", index);  
  84.         sendReply(rq->rq_src,  
  85.                     1,  
  86.             //rt->rt_hops, //Blackhole gravity.  
  87.                 rq->rq_dst,  
  88.             //rt->rt_seqno + 10, //Blackhole gravity.  
  89.                         4294967295,  
  90.                         rq->rq_src,  
  91.                 (u_int32_t) (rt->rt_expire - CURRENT_TIME),  
  92.                 rq->rq_timestamp);  
  93.         rt->pc_insert(rt0->rt_nexthop); // nexthop to RREQ source  
  94.         rt0->pc_insert(rt->rt_nexthop); // nexthop to RREQ destination  
  95.         //printf("node ip = %d,rt->rt_hops = %d, rt->rt_seqno = %d/n",index,rt->rt_hops, rt->rt_seqno);  
  96. #ifdef RREQ_GRAT_RREP  
  97.         sendReply(rq->rq_dst,  
  98.                 rq->rq_hop_count,  
  99.                 rq->rq_src,  
  100.                 rq->rq_src_seqno,  
  101.             rq->rq_src,  
  102.                 (u_int32_t) (rt->rt_expire - CURRENT_TIME),  
  103.                 rq->rq_timestamp);  
  104.         printf("ifndef RREQ_GRAT_RREP....../n");  
  105. #endif  
  106.         Packet::free(p);  
  107. }  
  108. //End balckhole Code   
  109.     } else if ((rt && (rt->rt_hops != INFINITY2) &&  
  110.             (rt->rt_seqno >= rq->rq_dst_seqno) )) {  
  111.         //printf("I am not the desitination, but is may have a fresh enough route/n");  
  112.         //assert (rt->rt_flags == RTF_UP);  
  113.         assert(rq->rq_dst == rt->rt_dst);  
  114.         //assert ((rt->rt_seqno%2) == 0);    // is the seqno even?  
  115.         sendReply(rq->rq_src,  
  116.                 rt->rt_hops + 1,  
  117.                 rq->rq_dst,  
  118.                 rt->rt_seqno,   
  119.             rq->rq_src,  
  120.                 (u_int32_t) (rt->rt_expire - CURRENT_TIME),  
  121.                 //             rt->rt_expire - CURRENT_TIME,  
  122.                 rq->rq_timestamp);  
  123.         // Insert nexthops to RREQ source and RREQ destination in the  
  124.         // precursor lists of destination and source respectively  
  125.         rt->pc_insert(rt0->rt_nexthop); // nexthop to RREQ source  
  126.         rt0->pc_insert(rt->rt_nexthop); // nexthop to RREQ destination  
  127. #ifdef RREQ_GRAT_RREP  
  128.         sendReply(rq->rq_dst,  
  129.                 rq->rq_hop_count,  
  130.                 rq->rq_src,  
  131.                 rq->rq_src_seqno,  
  132.             rq->rq_src,  
  133.                 (u_int32_t) (rt->rt_expire - CURRENT_TIME),  
  134.                 //             rt->rt_expire - CURRENT_TIME,  
  135.                 rq->rq_timestamp);  
  136. printf("I am not the desitination, but is may have a fresh enough route , RREQ_GRAT_RREP/n");  
  137. #endif  
  138. // TODO: send grat RREP to dst if G flag set in RREQ using rq->rq_src_seqno, rq->rq_hop_counT  
  139. // DONE: Included gratuitous replies to be sent as per IETF aodv draft specification. As of now, G flag has not been dynamically used and is always set or reset in aodv-packet.h --- Anant Utgikar, 09/16/02.  
  140.         Packet::free(p);  
  141.     }  
  142.     else {  
  143.         //Start Blackhole Code  
  144.         if(blackhole == 1) //是黑洞攻击节点  
  145.         {  
  146.              //printf("%d can't replay, but %d is a attacker, so i will replay the RREP packets/n", index, index);  
  147.             sendReply(rq->rq_src,        // IP Destination  
  148.                  1,         // Hop Count  
  149.                  rq->rq_dst,     // Dest IP Address  
  150.                  4294967295, // Highest Dest Sequence Num that is largest 32-bit integers from -2147483647 to +2147483647                 
  151.                 rq->rq_src,                       
  152.                        //rt->rt_seqno + 10,  
  153.                 MY_ROUTE_TIMEOUT,   // Lifetime  
  154.                 rq->rq_timestamp); // timestamp  
  155.                     Packet::free(p);  
  156.         }//End Blackhole Code  
  157.                  else  
  158.                  {      
  159.               ih->saddr() = index;  
  160.           ih->daddr() = IP_BROADCAST;  
  161.           rq->rq_hop_count += 1;  
  162.          // Maximum sequence number seen en route  
  163.           if (rt) rq->rq_dst_seqno = max(rt->rt_seqno, rq->rq_dst_seqno);  
  164.           forward((aodv_rt_entry*) 0, p, DELA   

这里面的sendReply我是修改过了的,就是添加一个字段。这个和实现黑洞攻击没有关系。可以忽略!

然后实现所有的黑洞攻击节点在接收到数据包时(自己不是目的节点),把所有接收到的数据包直接丢掉

在AODV::rt_resolve(Packet *p) 函数中添加:

[cpp]  view plain copy

  1. //if (rt->rt_flags == RTF_UP){  
  2.         if ((rt->rt_flags == RTF_UP) &&  
  3.             //Start Watchdog Code  
  4.             (blackhole != 1)) {  
  5.             //End Watchdog Code  
  6.             assert(rt->rt_hops != INFINITY2);  
  7.             forward(rt, p, NO_DELAY);  
  8.                        // printf("%f forward this packet by not need the REQUEST, dst = %d/n and next_hop = %d/n", Scheduler::instance().clock(),ih->daddr(), rt->rt_nexthop);  
  9.         }  
  10.         //Start Watchdog Code  
  11.         else if(blackhole == 1 && ih->daddr() != index){ //黑洞节点并且我不是目的节点  
  12.             if(blackholed == 0 && packets_dropped >= PACKETS_TO_CONSIDER_AN_ATTACK){  
  13.                 blackholed=1;  
  14.                 printAttackMessage(p);                
  15.             }  
  16.             packets_dropped++;  
  17.             drop(p, DROP_IFQ_FILTER);  
  18.             return;  
  19.         }  
  20.         //End Watchdog Code  

3.最后我们还需要在tcl/lib/ns-mobilenode.tcl中添加

#Blackhole
Node/MobileNode instproc set_Blackhole {} {
    $self instvar ragent_
        puts "Installing new blackhole...................."
        return $ragent_
}
           

4.这样我们可以直接在tcl脚本中使用黑洞攻击节点了:

set n3 [$ns node]
$n3 set X_ 372
$n3 set Y_ 400
$n3 set Z_ 0.0
$ns initial_node_pos $n3 20
$ns at 0.01 "[$n3 set ragent_] blackhole"
$ns at 0.01 "$n3 label /"blackhole node/" "
           

这里就是把节点3定义为一个黑洞攻击节点。

注:在协议中添加攻击行为还可以使用另外一种方法进行添加,就是像创建一种新的协议过程一样,比如说创建一种叫做blackholeAODV

整个协议的创建过程跟很多树上描述的创建一个新的MyPing协议过程一样。这里面的blackholeAODV协议创建过程按照AODV协议创建过程,然后修改AODV协议,使得这个协议能够实现黑洞攻击。最后在tcl脚本中使用是,和使用AODV协议配置一样。比如说你协议的定义的TCL为blackholeAODV,则可以这样子调用:

[c-sharp]  view plain copy

  1. set val(brp)    blackholeAODV              ;# blackhole attack in AODV  
  2. $ns node-config -adhocRouting $val(brp)  
  3. set n3 [$ns node]  
  4. $n3 set X_ 372  
  5. $n3 set Y_ 400  
  6. $n3 set Z_ 0.0  
  7. $ns initial_node_pos $n3 20  

这样同样可以实现上面的黑洞攻击的功能。