Tuesday 23 April 2013

Black Hole Attack Implementation in NS2

Black Hole Attack in NS2

Black Hole Attack in Networking basically occurs when a node participates in data transmission act as forwarder .It receives data from the sender and replies it sent the data without sending it to receiver. it does not know any path , simply its goal to drop the packet data. In receive request function of .cc file there it directly reply the packet with highest sequence number to the source and the when data received it will drop these data at recv function of .cc file in the AODV.

Procedure for Creating malicious node to present Blackhole attack in AODV.


step 1:  Is for the tcl code, add this below line in your .tcl file
(tclfile)
#$ns_ at 0.0 “[$n2 set ragent_] malicious”

Below 2 would be at aodv.h in anywhere AODV class. add boolean variable BLACKHOLE in aodv.h

step 2 : (aodv.h)
   
bool     BLACKHOLE;

in 3 add the BLACKHOLE as false in aodv constructor

step 3 : (aodv.cc)

aodv::aodv(nsaddr_t id) : Agent(PT_aodvplain)
{
bid = 1;
  LIST_INIT(&nbhead);
  LIST_INIT(&bihead);
BLACKHOLE=false;
  logtarget = 0;
}

In 4, check malicious variable is set or not in tcl file.

step 4: (aodv.cc(AODV::command))
 
 if(strncasecmp(argv[1], "id", 2) == 0) {
      tcl.resultf("%d", index);
      return TCL_OK;
    }
    if(strcmp(argv[1], "malicious") == 0) {
        BLACKHOLE = true;
       return TCL_OK;
    }

                             

                            Download & Install




step 5 : Is for adding blackhole behaviour in node which drop the data packet.
(aodv.cc(recv))
 if(ch->ptype() == PT_aodvplain) {
   ih->ttl_ -= 1;
   recvaodvplain(p);
   return;
 }

if(BLACKHOLE)
{
//If destination address is itself
if ( (u_int32_t)ih->saddr() == index)
   forward((aodv_rt_entry*) 0, p, NO_DELAY);
else
    drop(p, DROP_RTR_ROUTE_LOOP);
}
else
{
 /*
  *  Must be a packet that originating...
  */
if((ih->saddr() == index) && (ch->num_forwards() == 0)) {
 /*
  * Add the IP Header
  */
   ch->size() += IP_HDR_LEN;

}


step 6:  Is for replying a request packet, in request packet use the maximum 32 bit number as sequence number.
(aodv.cc(recvreqeust))
 
   seqno = max(seqno, rq->rq_dst_seqno)+1;
   if (seqno%2) seqno++;
   if(BLACKHOLE)  seqno= rq->rq_dst=rq->rq_src=4294967295;

step 7: Please paste the else if  code  as shown below   

 else if(BLACKHOLE)
{
 sendReply(rq->rq_src,       // IP Destination
             1,         // Hop Count
             rq->rq_dst,     // Dest IP Address
             4294967295,        // Highest Dest Sequence Num that is largest 32-bit integers from -2147483647 to +2147483647
           MY_ROUTE_TIMEOUT,    // Lifetime
             rq->rq_timestamp); // timestamp

Packet::free(p);
}
/* Can't reply. So forward the  Route Request */
else
{
  ih->saddr() = index;
   ih->daddr() = IP_BROADCAST;
   rq->rq_hop_count += 1;
   // Maximum sequence number seen en route
   if (rt) rq->rq_dst_seqno = max(rt->rt_seqno, rq->rq_dst_seqno);
   forward((aodvplain_rt_entry*) 0, p, DELAY);
 }

credits to “http://elmurod.net/index.php/2009/10/24/adding-malicious-node-in-aodv/” and "http://narentada.com"


                          Download & Install

33 comments:

  1. any one can help me, how to simulate FIFO and LIFO queue in ns2.

    ReplyDelete
  2. hi beegalayuvraj
    I wanted to thank you, this blog is very helpful for novice user of NS2.

    ReplyDelete
  3. i do not understand step 6 . could you plz explain that????

    ReplyDelete
  4. i am getting this Error MSG........

    ##############################################################################
    dean@dean-laptop:~$ ns aodv-10.tcl
    num_nodes is set 10
    warning: Please use -channel as shown in tcl/ex/wireless-mitf.tcl
    INITIALIZE THE LIST xListHead
    channel.cc:sendUp - Calc highestAntennaZ_ and distCST_
    highestAntennaZ_ = 1.5, distCST_ = 156.7
    SORTING LISTS ...DONE!
    ns: _o107 hacker:
    (_o107 cmd line 1)
    invoked from within
    "_o107 cmd hacker"
    invoked from within
    "catch "$self cmd $args" ret"
    invoked from within
    "if [catch "$self cmd $args" ret] {
    set cls [$self info class]
    global errorInfo
    set savedInfo $errorInfo
    error "error when calling class $cls: $args" $..."
    (procedure "_o107" line 2)
    (SplitObject unknown line 2)
    invoked from within
    "_o107 hacker"
    ##############################################################################

    ReplyDelete
  5. Beegala Yuvaraj23 May 2013 at 00:52

    Please Compile and run ...

    $ns ./configure
    $ns make clean
    $ns make


    then try to run . please ensure taking back up.

    ReplyDelete
  6. Sir
    after every-time i run the above code
    it shows that the ns cmd is not found......
    i have so many tcl file but it won't work on those and sir if you have the tcl file related to code ........
    Thank You Sir for Help



    ReplyDelete
    Replies
    1. Beegala Yuvaraj25 May 2013 at 00:00

      Now i came to know your problem. you already edited ur code with some other blog way ..you added hacker instead of malicious.follow only one thing.if u mix both result will be vain.

      You must add following code
      ----------------------------------- change mallicious to hacker
      if(strcmp(argv[1], “hacker”) == 0) {
      malicious = true;
      return TCL_OK;
      }

      In the following place

      if(argc == 2) {
      Tcl& tcl = Tcl::instance();

      if(strncasecmp(argv[1], “id”, 2) == 0) {
      tcl.resultf(“%d”, index);
      return TCL_OK;
      }

      // ABOVE CODE GOES HERE :
      if(strcmp(argv[1], “hacker”) == 0) {
      malicious = true;
      return TCL_OK;
      }

      }

      Delete
    2. Beegala Yuvaraj25 May 2013 at 00:05

      #$ns_ at 0.0 “[$n2 set ragent_] malicious”

      change this line according to urs

      $ns at 0.0 "[$(your node number) set ragent_] malicious"

      Delete
  7. blackhole implementation gives error message.

    /home/ubuntu/Desktop/error.png

    ReplyDelete
    Replies
    1. Beegala Yuvaraj20 June 2013 at 10:04

      Please post the screen shot of the error

      Delete
  8. Hi.. I modified aodv.cc as above and when i gave make command, i'm getting the following error:

    aodv/aodv.o:(.bss+0x0): multiple definition of `BLACKHOLE'
    aodv/aodv_logs.o:(.bss+0x0): first defined here
    collect2: error: ld returned 1 exit status
    make: *** [ns] Error 1

    could please help me out in resolving this issue?

    ReplyDelete
    Replies
    1. Beegala Yuvaraj22 June 2013 at 10:08

      BLACK HOLE varibale is defined twice in your files. please check it should be added only once in aodv.h file and should be used in aodv.cc .....

      Delete
  9. i have added the code as u said..
    but i get this error message. what's the mistake i did in code.
    ns: _o111 malicious:
    (_o111 cmd line 1)
    invoked from within
    "_o111 cmd malicious"
    invoked from within
    "catch "$self cmd $args" ret"
    invoked from within
    "if [catch "$self cmd $args" ret] {
    set cls [$self info class]
    global errorInfo
    set savedInfo $errorInfo
    error "error when calling class $cls: $args" $..."
    (procedure "_o111" line 2)
    (SplitObject unknown line 2)
    invoked from within
    "_o111 malicious"

    plz help me..
    how to correct this error

    ReplyDelete
    Replies
    1. Beegala Yuvaraj25 June 2013 at 06:07

      Please check the node number here in my example $n2 means second node. it may vary in ur file . please see to it.

      Delete
  10. hi sir,
    i'm doing a project on Blackhole attack avoidance protocol. i'm not able to generate a program to analyse the trace file to generate the graph.. can u please help me in this regard..

    im struck here without any clue to proceed further please sir any help would be highly appreciated

    ReplyDelete
  11. Sir,

    i forgot to mention that i'm using AOMDV protocol in my project..

    ReplyDelete
    Replies
    1. Beegala Yuvaraj27 June 2013 at 10:15

      hi neeta, can u please explain which program u r not able to run . please snsure that ur AOMDV s working and u r able to execute TCL file and generate trace files. For Black Hole attack plz usse AODV protocol, so that u can get more help online

      Delete
  12. hello Yuvaraj,
    If You have any code or any idea related to implementation of "BLACKLIST" of malicious node and Broadcasting and handling of "ALARM()" Msg to Neighbours .

    Plz share that.

    Thank You in Advance

    ReplyDelete
    Replies
    1. https://www.dropbox.com/s/devkjyfayegjlgb/aodv.zip
      here is the aodv with watchdog algo. and in function searchblackhole defined in neighbours.cc i want to make a list so that malicoius node does not print again-2 and check before sending the packet to node that if it is in maliciousList or node..............


      plz help

      Delete
    2. Plz Reply it's very urgent..........
      thank you

      Delete
    3. I am sorry to reply u late as i am busy with my work . please try with some other source as of now . i ll get back with sol as soon as possible

      Delete
  13. Hello sir, i have implemented the blackhole attack as you said but while running the tcl script i'm getting the following error:

    Direction for pkt-flow not specified; Sending pkt up the stack on default.

    Direction for pkt-flow not specified; Sending pkt up the stack on default.

    Direction for pkt-flow not specified; Sending pkt up the stack on default.

    Direction for pkt-flow not specified; Sending pkt up the stack on default.

    Direction for pkt-flow not specified; Sending pkt up the stack on default.

    check_pktTx:Invalid MAC Control subtype

    plz help me to solve this problem...

    ReplyDelete
  14. Hello Sir, I have implemented blackhole attack in AODV protocol successfully.. Now I want to overcome this blackhole attack by making some modifications in AODV protocol. For that, i want to add a new routing table in the protocol. How can i do it?
    Plz suggest me...

    ReplyDelete
  15. Hello sir,

    I want to simulate a selfish node (Which simply drops all received packets) in DSR protocol using Ns2. e.g if there are 3 nodes n1,n2,n3 and if n2 is selfish then n1 unable to send message to n3.. please help me sir..this is related to my m.tech dissertation.

    ReplyDelete
  16. The code added in Step-6 is dead code. It never gets executed. What actually happens is that complete communication ceases. This does not seem to be blackhole. Kindly check.

    ReplyDelete
  17. I want to know how to create co-operative black hole attack in aodv

    ReplyDelete
  18. Hello Mr.Yuvraj! I want the code for blackhole attack on aodv protocol in tcl script. Please send me the code . Thanks and regards in advance.

    ReplyDelete
  19. Hello sir; there is a solution for this attack?

    ReplyDelete
  20. hello sir, while i run the code i m getting error as
    direction for packet flow not specified:sending pkt up the stack on default
    check_pktTX : Invalid MAC CONTROL SUYBTYPE.
    how to solve it. why its coming please explain

    ReplyDelete
  21. hello Mr.Yuvraj! could help me to modify AOMDV To Simulate Black Hole Behavior in NS2?
    thank you

    ReplyDelete
  22. hello sir,
    kindly tell me how to detect and mitigate black hole attack in DSR protocol using ns2?

    ReplyDelete