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

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

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

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

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

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

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

[cpp] view plaincopy
  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 plaincopy
  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. /*
  8. * Drop if:
  9. *      - I'm the source
  10. *      - I recently heard this request.
  11. */
  12. if (rq->rq_src == index) {
  13. #ifdef DEBUG
  14. //fprintf(stderr, "%s: got my own REQUEST/n", __FUNCTION__);
  15. #endif // DEBUG
  16. Packet::free(p); //如果是自己发出来的就直接丢弃
  17. return;
  18. }
  19. if (id_lookup(rq->rq_src, rq->rq_bcast_id)) {
  20. #ifdef DEBUG
  21. //fprintf(stderr, "%s: discarding request/n", __FUNCTION__);
  22. #endif // DEBUG
  23. // printf("我之前已经接收到了这个分组!and my id is %d /n", index);
  24. Packet::free(p); //这个分组已经收到过
  25. return;
  26. }
  27. /*
  28. * Cache the broadcast ID
  29. */
  30. id_insert(rq->rq_src, rq->rq_bcast_id);
  31. /*
  32. * We are either going to forward the REQUEST or generate a
  33. * REPLY. Before we do anything, we make sure that the REVERSE
  34. * route is in the route table.
  35. */
  36. aodv_rt_entry *rt0; // rt0 is the reverse route
  37. rt0 = rtable.rt_lookup(rq->rq_src);
  38. if (rt0 == 0) { /* if not in the route table */
  39. // create an entry for the reverse route.
  40. rt0 = rtable.rt_add(rq->rq_src);
  41. }
  42. rt0->rt_expire = max(rt0->rt_expire, (CURRENT_TIME + REV_ROUTE_LIFE));
  43. if ( (rq->rq_src_seqno > rt0->rt_seqno ) ||
  44. ((rq->rq_src_seqno == rt0->rt_seqno) &&
  45. (rq->rq_hop_count < rt0->rt_hops)) ) {
  46. // If we have a fresher seq no. or lesser #hops for the
  47. // same seq no., update the rt entry. Else don't bother.
  48. rt_update(rt0, rq->rq_src_seqno, rq->rq_hop_count, ih->saddr(),
  49. max(rt0->rt_expire, (CURRENT_TIME + REV_ROUTE_LIFE)) );
  50. if (rt0->rt_req_timeout > 0.0) {
  51. // Reset the soft state and
  52. // Set expiry time to CURRENT_TIME + ACTIVE_ROUTE_TIMEOUT
  53. // This is because route is used in the forward direction,
  54. // but only sources get benefited by this change
  55. rt0->rt_req_cnt = 0;
  56. rt0->rt_req_timeout = 0.0;
  57. rt0->rt_req_last_ttl = rq->rq_hop_count;
  58. rt0->rt_expire = CURRENT_TIME + ACTIVE_ROUTE_TIMEOUT;
  59. }
  60. /* Find out whether any buffered packet can benefit from the
  61. * reverse route.
  62. * May need some change in the following code - Mahesh 09/11/99
  63. */
  64. assert (rt0->rt_flags == RTF_UP);
  65. Packet *buffered_pkt;
  66. while ((buffered_pkt = rqueue.deque(rt0->rt_dst))) {
  67. if (rt0 && (rt0->rt_flags == RTF_UP)) {
  68. assert(rt0->rt_hops != INFINITY2);
  69. forward(rt0, buffered_pkt, NO_DELAY);
  70. }
  71. }
  72. }
  73. // End for putting reverse route in rt table
  74. /*
  75. * We have taken care of the reverse route stuff.
  76. * Now see whether we can send a route reply.
  77. */
  78. rt = rtable.rt_lookup(rq->rq_dst);
  79. // First check if I am the destination ..
  80. if (rq->rq_dst == index) {
  81. #ifdef DEBUG
  82. fprintf(stderr, "%d - %s: destination sending reply/n",
  83. index, __FUNCTION__);
  84. #endif // DEBUG
  85. //printf("I am the desitination and my ip address is %d/n", index);
  86. // Just to be safe, I use the max. Somebody may have
  87. // incremented the dst seqno.
  88. seqno = max(seqno, rq->rq_dst_seqno) + 1;
  89. if (seqno % 2) seqno++;
  90. sendReply(rq->rq_src,           // IP Destination
  91. 1,                    // Hop Count
  92. index,                // Dest IP Address
  93. seqno,                // Dest Sequence Num
  94. rq->rq_src,
  95. MY_ROUTE_TIMEOUT,     // Lifetime
  96. rq->rq_timestamp);    // timestamp
  97. Packet::free(p);
  98. }
  99. // I am not the destination, but I may have a fresh enough route.
  100. //Start balckhole Code
  101. //a Blackhole attacker always say that have the route to be a sink.
  102. else if ((rt && blackhole == 1)) {
  103. assert(rq->rq_dst == rt->rt_dst);
  104. //printf("I am the blackhole node and blackhole = %d/n", index);
  105. sendReply(rq->rq_src,
  106. 1,
  107. //rt->rt_hops, //Blackhole gravity.
  108. rq->rq_dst,
  109. //rt->rt_seqno + 10, //Blackhole gravity.
  110. 4294967295,
  111. rq->rq_src,
  112. (u_int32_t) (rt->rt_expire - CURRENT_TIME),
  113. rq->rq_timestamp);
  114. rt->pc_insert(rt0->rt_nexthop); // nexthop to RREQ source
  115. rt0->pc_insert(rt->rt_nexthop); // nexthop to RREQ destination
  116. //printf("node ip = %d,rt->rt_hops = %d, rt->rt_seqno = %d/n",index,rt->rt_hops, rt->rt_seqno);
  117. #ifdef RREQ_GRAT_RREP
  118. sendReply(rq->rq_dst,
  119. rq->rq_hop_count,
  120. rq->rq_src,
  121. rq->rq_src_seqno,
  122. rq->rq_src,
  123. (u_int32_t) (rt->rt_expire - CURRENT_TIME),
  124. rq->rq_timestamp);
  125. printf("ifndef RREQ_GRAT_RREP....../n");
  126. #endif
  127. Packet::free(p);
  128. }
  129. //End balckhole Code
  130. /*Sniffer IDS 假设中间节点是不能回复RREP的,但是黑洞攻击节点却必须可疑回复,其实这种做法明显是存在很大的缺陷的。
  131. 这样即使可以有效隔离黑洞攻击节点,但是这只能说是路由协议的一种特殊条件的下的检测方法。
  132. 后面我希望在此基础上做进一步的改进,可以把这种攻击手段扩大化,也就是更加普遍话。适应各种路由情况,而不仅限于某种特殊情况
  133. */
  134. } else if ((rt && (rt->rt_hops != INFINITY2) &&
  135. (rt->rt_seqno >= rq->rq_dst_seqno) )) {
  136. //printf("I am not the desitination, but is may have a fresh enough route/n");
  137. //assert (rt->rt_flags == RTF_UP);
  138. assert(rq->rq_dst == rt->rt_dst);
  139. //assert ((rt->rt_seqno%2) == 0);    // is the seqno even?
  140. sendReply(rq->rq_src,
  141. rt->rt_hops + 1,
  142. rq->rq_dst,
  143. rt->rt_seqno,
  144. rq->rq_src,
  145. (u_int32_t) (rt->rt_expire - CURRENT_TIME),
  146. //             rt->rt_expire - CURRENT_TIME,
  147. rq->rq_timestamp);
  148. // Insert nexthops to RREQ source and RREQ destination in the
  149. // precursor lists of destination and source respectively
  150. rt->pc_insert(rt0->rt_nexthop); // nexthop to RREQ source
  151. rt0->pc_insert(rt->rt_nexthop); // nexthop to RREQ destination
  152. #ifdef RREQ_GRAT_RREP
  153. sendReply(rq->rq_dst,
  154. rq->rq_hop_count,
  155. rq->rq_src,
  156. rq->rq_src_seqno,
  157. rq->rq_src,
  158. (u_int32_t) (rt->rt_expire - CURRENT_TIME),
  159. //             rt->rt_expire - CURRENT_TIME,
  160. rq->rq_timestamp);
  161. printf("I am not the desitination, but is may have a fresh enough route , RREQ_GRAT_RREP/n");
  162. #endif
  163. // TODO: send grat RREP to dst if G flag set in RREQ using rq->rq_src_seqno, rq->rq_hop_counT
  164. // 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.
  165. Packet::free(p);
  166. }
  167. /*
  168. * Can't reply. So forward the  Route Request
  169. */
  170. else {
  171. //Start Blackhole Code
  172. if(blackhole == 1) //是黑洞攻击节点
  173. {
  174. //printf("%d can't replay, but %d is a attacker, so i will replay the RREP packets/n", index, index);
  175. sendReply(rq->rq_src,        // IP Destination
  176. 1,         // Hop Count
  177. rq->rq_dst,     // Dest IP Address
  178. 4294967295, // Highest Dest Sequence Num that is largest 32-bit integers from -2147483647 to +2147483647
  179. rq->rq_src,
  180. //rt->rt_seqno + 10,
  181. MY_ROUTE_TIMEOUT,   // Lifetime
  182. rq->rq_timestamp); // timestamp
  183. Packet::free(p);
  184. }//End Blackhole Code
  185. else
  186. {
  187. ih->saddr() = index;
  188. ih->daddr() = IP_BROADCAST;
  189. rq->rq_hop_count += 1;
  190. // Maximum sequence number seen en route
  191. if (rt) rq->rq_dst_seqno = max(rt->rt_seqno, rq->rq_dst_seqno);
  192. forward((aodv_rt_entry*) 0, p, DELA

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

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

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

[cpp] view plaincopy
  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 plaincopy
  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

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

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

  1. 在NS2 AODV协议中添加blackhole attacker(黑洞攻击)

    在NS2-3.34中添加黑洞攻击的过程还是比较简单的,具体过程大致如下描述: 1. 首先我们在aodv/aodv.h中的AODV类中添加一个标志该Agent(该节点是blackhole的标志) cla ...

  2. ios 协议中添加属性---分解ZFPlayer

    场景分析(不限于此项目与此应用场景): 底层:播放器正在播放并展示当前播放进度03:23 上层:控制&更新播放进度,拖动进度条,结束拖动,需要知道进度更新到具体时间,用于执行seekToTim ...

  3. aodv协议C语言代码,AODV协议的运行方式

    这也是一个网管必须具备的知识.那么在前面的文章中,我们已经对AODV协议的基础内容作了介绍,在这里我们主要讲解一下它的运行方式. (a)AODV路由发现 AODV路由协议是一种典型的按需驱动路由协议, ...

  4. AODV协议的运行方式

    (a)AODV路由发现 AODV路由协议是一种典型的按需驱动路由协议,该算法可被称为纯粹的需求路由获取系统,那些不在活跃路径上的节点不会维持任何相关路由信息,也不会参与任何周期路由表的交换.此外,节点 ...

  5. Ad hoc网络路由协议概述4——按需路由协议(2)AODV协议 (Ad-hoc on-demand distance vector algorithm protocol)

    目录 1 一点前言 2 路由发现 2.1 相关概念 2.2 AODV的路由发现过程 2.3 与DSDV协议的对比 3 路由表管理及维护 3.1 更新路由表的策略 4 AODV协议的特点 4.1 优点 ...

  6. iOS中在NavigationController的toolbar中添加Segment控件

    2019独角兽企业重金招聘Python工程师标准>>> 昨天本来想在Navigation的Toolbar里面添加一个Segment控件,没想到一添加就跑到上面的视图上去,愁死了.后来 ...

  7. 向NS2中添加协议PING

    在NS2中增加自己的协议模块一般分一下几个步骤: (1)       添加协议类 (2)       定义协议分组头结构 (3)       编译代码 其实在ns3.35版本中已经有ping协议,此步 ...

  8. 在NS2(2.35版本)中添加 Ping协议

    在NS2的2.29版本中已存在ping协议,我的NS2版本为2.35,这里将现存的ping协议改为meng协议. 原本的ping在文件夹apps下 自己新建协议时,应该建立新的目录meng sudo ...

  9. 向NS2中添加协议PING[转载]

    原文地址:http://www.cnblogs.com/xyl-share-happy/archive/2012/03/20/2407732.html ------------------------ ...

  10. NS2 学习笔记—— AODV协议

    在NS2中,AODV路由协议主要包括以下几个组件: 1.协议实体 2.路由表 3.定时器 (1)广播定时器 (2)周期Hello报文广播定时器 (3)用于邻居管理的定时器 (4)用于路由缓存的定时器 ...

最新文章

  1. CentOS6.8 编译安装LNMP
  2. 机器学习算法与技术简介
  3. .net EF监控 MiniProfiler
  4. MSSQL如何在没有主键的表中删除重复数据
  5. 测试比java_Java11比Java8快多少,不是测试人员一眼也能看懂
  6. 网络信息安全实验 — 网络攻击技术实验(Kali系统,John、lc7、arpspoof、ettercap、SQL注入...)
  7. 技术圈的女性工程师都去哪呢?
  8. Codeforces Round #179 (Div. 2): D. Greg and Graph(Floyd)
  9. 大学计算机基础上机考试试卷,大学计算机基础上机考试试卷(答案).doc
  10. iphone投屏老是显示无法连接服务器,iphone怎么投屏到电视 升级iOS11后为什么投屏会失败...
  11. origin数据平滑_Origin平滑曲线的使用方法
  12. 计算机主机显卡装在哪,电脑显卡在主板上的哪个位置?怎么查看显卡的信息和更新驱动程序?...
  13. 如何用C语言编辑一个万年历,如何用C语言编写一个万年历系统?
  14. 林辉高考机器人_“成都造”高考机器人 高考数学成绩稳定在136分
  15. 数据结构与算法——深入理解红-黑树!
  16. 【火爆全网的抖音无人直播项目:挤地铁源代码】
  17. 『淘宝十年技术路』读后想法
  18. Python 读取excel指定的列
  19. 物联网深度融入生活场景 爆发条件成熟
  20. Ta来了,Ta来了,Spark基础能力测试题Ta来了!

热门文章

  1. L1W3 用1层隐藏层的神经网络分类二维数据
  2. HTTP中GET,POST和PUT的区别
  3. between oracle的用法,关于 oracle between and的用法! | 学步园
  4. linux cat 压缩文件,Linux cat和zcat命令可能比你意识到的更有用
  5. 【stm32单片机基础】按键状态机实现长按和短按
  6. 算法(c++)——循环比赛日程安排问题
  7. windows linux 共存,Windows与Linux共存
  8. 莫队算法 --算法竞赛专题解析(26)
  9. IBM笔记本电池保养细则
  10. 服务器备案问题解决思考?