Thursday 19 February 2015

Adding a malicious node in NS2 in AODV Protocol

Adding a malicious node is ns2 using aodv protocol. The node which is declared as malicious will simply drop the router packet (DROP_RTR_ROUTE_LOOP).

Two files have to be modified.
1. aodv.h
2. aodv.cc

aodv.h file changes

Declare a boolean variable malicious as shown below in the protected scope in the class AODV

bool malicious;

aodv.cc file changes

1. Initialize the malicious varible with a value "false". Declare it inside the constructor as shown below
AODV::AODV(nsaddr_t id):Agent(PT_AODV)...
{
.......
malicious = false;
}

2. Add the following statement to the aodv.cc file in the "if(argc==2)" statment.

if(strcmp(argv[1], "malicious") == 0) {
    malicious = true;
   return TCL_OK;
}


3. Implement the behavior of the malicious node by setting the following code in the rt_resolve(Packet *p) function. The malicious node will simply drop the packet as indicated below.

if(malicious==true)
{
drop(p,DROP_RTR_ROUTE_LOOP);
}

Once done, recompile ns2 as given below
Open Terminal -> Go to ~ns-2.35/ directory and type the command make to compile
$ cd /home/pradeep/ns-allinone-2.35/ns-2.35/
$ make clean
$ make                                              # it will take time to compile
$ sudo make install

Once the compilation is done, Check the malicious behavior using the Tcl Script by setting any one node as malicious node. The command to set the malicious node is

$ns at 0.0 "[$n(1) set ragent_] malicious" 

The variable referred for node2 is n1 (set n(1) [$ns node])

you  can disable the packet dropping by adding # before above line

#$ns at 0.0 "[$n(1) set ragent_] malicious"


aodv.tcl file









#======================================================================
# Define options
#======================================================================
 set val(chan)         Channel/WirelessChannel  ;# channel type
 set val(prop)         Propagation/TwoRayGround ;# radio-propagation model
 set val(ant)          Antenna/OmniAntenna      ;# Antenna type
 set val(ll)           LL                       ;# Link layer type
 set val(ifq)          Queue/DropTail/PriQueue  ;# Interface queue type
 set val(ifqlen)       50                       ;# max packet in ifq
 set val(netif)        Phy/WirelessPhy          ;# network interface type
 set val(mac)          Mac/802_11               ;# MAC type
 set val(nn)           6                        ;# number of mobilenodes
 set val(rp)           AODV                     ;# routing protocol
 set val(x)            800
 set val(y)            800



set ns [new Simulator]
#ns-random 0

set f [open out.tr w]
$ns trace-all $f
set namtrace [open out.nam w]
$ns namtrace-all-wireless $namtrace $val(x) $val(y)
set topo [new Topography]
$topo load_flatgrid 800 800

create-god $val(nn)

set chan_1 [new $val(chan)]
set chan_2 [new $val(chan)]
set chan_3 [new $val(chan)]
set chan_4 [new $val(chan)]
set chan_5 [new $val(chan)]
set chan_6 [new $val(chan)]

# CONFIGURE AND CREATE NODES

$ns node-config  -adhocRouting $val(rp) \
          -llType $val(ll) \
                 -macType $val(mac) \
                 -ifqType $val(ifq) \
                 -ifqLen $val(ifqlen) \
                 -antType $val(ant) \
                 -propType $val(prop) \
                 -phyType $val(netif) \
                 #-channelType $val(chan) \
                 -topoInstance $topo \
                 -agentTrace ON \
                 -routerTrace ON \
                 -macTrace ON \
                 -movementTrace OFF \
                 -channel $chan_1

proc finish {} {
    global ns namtrace
    $ns flush-trace
        close $namtrace 
        exec nam -r 5m out.nam &
    exit 0
}

# define color index
$ns color 0 blue
$ns color 1 red
$ns color 2 chocolate
$ns color 3 red
$ns color 4 brown
$ns color 5 tan
$ns color 6 gold
$ns color 7 black
                      
set n(0) [$ns node]
$ns at 0.0 "$n(0) color blue"
$n(0) color "0"
$n(0) shape "circle"
set n(1) [$ns node]
$ns at 0.0 "$n(1) color red"
$n(1) color "blue"
$n(1) shape "circle"
set n(2) [$ns node]
$n(2) color "tan"
$n(2) shape "circle"
set n(3) [$ns node]
$n(3) color "red"
$n(3) shape "circle"
set n(4) [$ns node]
$n(4) color "tan"
$n(4) shape "circle"
set n(5) [$ns node]
$ns at 0.0 "$n(5) color blue"
$n(5) color "red"
$n(5) shape "circle"


for {set i 0} {$i < $val(nn)} {incr i} {
    $ns initial_node_pos $n($i) 30+i*100
}
#$ns at 0.0 "[$n(1) set ragent_] malicious"  
        
$ns at 0.0 "$n(0) setdest 100.0 100.0 3000.0"
$ns at 0.0 "$n(1) setdest 200.0 200.0 3000.0"
$ns at 0.0 "$n(2) setdest 300.0 200.0 3000.0"
$ns at 0.0 "$n(3) setdest 400.0 300.0 3000.0"
$ns at 0.0 "$n(4) setdest 500.0 300.0 3000.0"
$ns at 0.0 "$n(5) setdest 600.0 400.0 3000.0"

# CONFIGURE AND SET UP A FLOW

set sink0 [new Agent/LossMonitor]
set sink1 [new Agent/LossMonitor]
set sink2 [new Agent/LossMonitor]
set sink3 [new Agent/LossMonitor]
set sink4 [new Agent/LossMonitor]
set sink5 [new Agent/LossMonitor]
$ns attach-agent $n(0) $sink0
$ns attach-agent $n(1) $sink1
$ns attach-agent $n(2) $sink2
$ns attach-agent $n(3) $sink3
$ns attach-agent $n(4) $sink4
$ns attach-agent $n(5) $sink5

#$ns attach-agent $sink2 $sink3
set tcp0 [new Agent/TCP]
$ns attach-agent $n(0) $tcp0
set tcp1 [new Agent/TCP]
$ns attach-agent $n(1) $tcp1
set tcp2 [new Agent/TCP]
$ns attach-agent $n(2) $tcp2
set tcp3 [new Agent/TCP]
$ns attach-agent $n(3) $tcp3
set tcp4 [new Agent/TCP]
$ns attach-agent $n(4) $tcp4
set tcp5 [new Agent/TCP]
$ns attach-agent $n(5) $tcp5


proc attach-CBR-traffic { node sink size interval } {
   #Get an instance of the simulator
   set ns [Simulator instance]
   #Create a CBR  agent and attach it to the node
   set cbr [new Agent/CBR]
   $ns attach-agent $node $cbr
   $cbr set packetSize_ $size
   $cbr set interval_ $interval

   #Attach CBR source to sink;
   $ns connect $cbr $sink
   return $cbr
  }

set cbr0 [attach-CBR-traffic $n(0) $sink5 1000 .030]
$ns at 0.5 "$cbr0 start"
$ns at 5.5 "finish"
puts "Start of simulation.."
$ns run

For video tutorial
https://www.youtube.com/watch?v=ca-yFicgDZs

thank you!

63 comments:

  1. i use the above code but is not working 4 me.pls help asap.this is so urgent
    i get the following error
    num_nodes is set 6
    INITIALIZE THE LIST xListHead
    ns: _o44 malicious:
    (_o44 cmd line 1)
    invoked from within
    "_o44 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 "_o44" line 2)
    (SplitObject unknown line 2)
    invoked from within
    "_o44 malicious"

    ReplyDelete
    Replies
    1. i have also facing these type of error... if anyone have solution to these type of error plz help us..,,,,plz share ur code for this specific email id- shahir.mukhtar@gmail.com

      Delete
    2. Refer this
      https://www.youtube.com/watch?v=ca-yFicgDZs

      Delete
    3. This comment has been removed by the author.

      Delete
    4. can you please solve my problem to access remotely my pc ....if you have time to do that...it humble request to you,,,...because i watched you video but can"t remove it.....

      Delete
    5. #suraj patil sir will u guide me how to use the abov tcl file?? i jst copied it in notepad and save it with aodv.tcl name in ns2 folder ...aodv subfolder.. but its not working

      Delete
    6. Sir, even im getting the same error and have no clue to deal with this

      Delete
    7. i'm also getting the same error..
      if u find solution kindly let me know.
      vishesh2969@gmail.com

      num_nodes is set 6
      INITIALIZE THE LIST xListHead
      using backward compatible Agent/CBR; use Application/Traffic/CBR instead
      Start of simulation..
      ns: _o44 malicious:
      (_o44 cmd line 1)
      invoked from within
      "_o44 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 "_o44" line 2)
      (SplitObject unknown line 2)
      invoked from within
      "_o44 malicious"

      Delete
  2. the above code has been tested by me and its working. Just follow the steps exactly given above. I have provided a video tutorial also for that. I hope itll help you.

    ReplyDelete
    Replies
    1. will the code also work for ns-2.34??

      Delete
    2. Sir, can you provide the aodv.cc and aodv.h files .my mail id is sreelakshmitrs@gmail.com

      Delete
  3. hey,can you please help me in adding malicious node in PUMA multicast routing protocol.i'll be really thankful to you.

    ReplyDelete
  4. How about a nice day ..

    I could tell if it's possible to clone the SAODV protocol in ns2, it's for my thesis I would appreciate if you can help me

    greetings from Ecuador

    ReplyDelete
    Replies
    1. Their is a another post in my blog related to protocol cloning. Refer that blog.

      Delete
  5. Your blog is really helps for my search and amazingly it was on my searching criteria.. Thanks a lot.

    ceiling mount antenna & das antenna manufacturer

    ReplyDelete
  6. Hello suraj,
    i am M.E. student and need to implement node authentication of Wireless sensor using ECDSA algorithm... i ahve done coding for key generation, exchange signature generation andd verification using ECC ,, can you please help me how to use same c++ code for ns2? and also about tcl scripts .. writing and calling etc

    Awaiting for earliest reply!!!

    Thanks

    Varsha

    ReplyDelete
    Replies
    1. Refer my video tutorials. It'll help you.

      Delete
    2. hi where is avodv.h and avodv.cc files u posted only tcl file

      Delete
  7. hello suraj,
    i am working aodv in adhoc adding malicious node in aodv.cc and aodv.h file chages after make the command is error occur its no target specified is comming what is that error sir pls help me

    ReplyDelete
  8. This comment has been removed by the author.

    ReplyDelete
  9. Here you used aodv protocol.
    On which layer you implemented that, how should I know that?
    Help please?

    ReplyDelete
  10. hello suraj sir i am dharman studying in M.e -cse my project is detection and prevention of gray hole attack in manet using aodv routing protocols...i can add malicious node in aodv.cc aodv.h file after im try to do make cmnd but no use ..and i try to make camnd use i get make****.no target to specified make..
    make clean
    make
    make depend
    make install
    above mention the line give my terminal i get only for error...
    ./configure i will run means its working ...what is the that problem sir ...after that i give ns % get percetage symbols sir its sucessfully ns2 is installed my machine....
    its very urget for my project pls help me i am struck this step

    ReplyDelete
  11. sir i m Dharman v please provide this error solution of this problem ...

    ReplyDelete
  12. sir i m dharman sir above the code is executed but not do packet dropping sir...

    ReplyDelete
  13. dharman@dharman-HP-15-Notebook-PC:~/ns-allinone-2.35/ns-2.35/aodv$ ns grayattack.tcl
    num_nodes is set 6
    INITIALIZE THE LIST xListHead
    using backward compatible Agent/CBR; use Application/Traffic/CBR instead
    Start of simulation..
    ns: malicious": invalid command name "malicious""
    while executing
    "malicious""
    what is that error sir pls help me and gcc -O2 -pipe -Wl,--export-dynamic tclAppInit.o -L/home/dharman/ns-allinone-2.35/tcl8.5.10/unix -ltcl8.5 -ldl -lieee -lm \
    -Wl,-rpath,/home/dharman/ns-allinone-2.35/lib -o tclsh
    tcl8.5.10 make succeeded.
    Installing libtcl8.5.a to /home/dharman/ns-allinone-2.35/lib/
    Installing tclsh as /home/dharman/ns-allinone-2.35/bin/tclsh8.5
    Installing tclConfig.sh to /home/dharman/ns-allinone-2.35/lib/
    Installing libtclstub8.5.a to /home/dharman/ns-allinone-2.35/lib/
    Installing message catalogs
    Creating msgs
    error deleting "/home/dharman/ns-allinone-2.35/lib/tcl8.5/msgs/eu.msg": not owner
    while executing
    "file delete -force -- $d2"
    (procedure "copyDir" line 5)
    invoked from within
    "copyDir [file normalize [lindex $argv 0]] [file normalize [lindex $argv 1]]"
    (file "/home/dharman/ns-allinone-2.35/tcl8.5.10/unix/../tools/installData.tcl" line 50)
    make: *** [install-msgs] Error 1
    tcl8.5.10 installation failed.
    Tcl is not part of the ns project. Please see www.Scriptics.com
    to see if they have a fix for your platform.
    pls help me what this error sir

    ReplyDelete
    Replies
    1. Refer video tutorial. The link is given at the end.

      Delete
  14. above mention error is ./install i will give i m getting this error sir please provide above mention the problem....

    ReplyDelete
  15. I am trying to modify AODV routing protocol using NS 2.35. I have made some changes to the files aodv.cc and aodv.h. Now, to apply these changes I have run a **make** command inside ns-allinone-2.35/ns-2.35 folder and getting the following error message:

    In file included from aodv/aodv_logs.cc:31:0:
    ./aodv/aodv.h:53:18: fatal error: list.h: No such file or directory
    #include
    ^
    compilation terminated.
    make: *** [aodv/aodv_logs.o] Error 1

    How will I solve this?

    ReplyDelete
    Replies
    1. I changed the type of cache of DSR from mobicache to linkcache and then compiled ns2.35 . it gives me the following error:
      dsr/linkcache .cc: fatal error: list.h: no such file or directory.
      Please if you find a solution send it to me : elexten1@yahoo.com
      suraj have you got any suggestions to solve this error?
      please help me . Thanks in advance

      Delete
  16. hi suraj sir i am tired to change that error please sir what is that error
    compilation terminated ..
    make:****no target specified make...

    ReplyDelete
  17. hi suraj I want to change the malicious node.how can I do.can you help me?
    example

    $ns at 0.5 "$cbr0 start"
    $ns at 3.0 "$cbr0 stop"
    $ns at 3.5 "$cbr0 start"
    $ns at 0.0 "[$n(1) set ragent_] malicious2"
    $ns at 0.0 "[$n(3) set ragent_] malicious"
    $ns at 0.0 "$n(3) color gold"
    $ns at 5.5 "finish"

    ReplyDelete
  18. Sir, I have run this code with no error but i am not getting the packets dropped or i couldn't visualize any change in the event field in trace file. Please help me with this 

    ReplyDelete
  19. I have changed both files..add malicious code in tcl file.still not getting proper result i.e node is not ableto droping the packets

    ReplyDelete
  20. i check whether out .tr trace file is generated comes on drooped packet .but i could not visible to packet drop.i check whether awk is packet drooped result is =0 what is my problem .

    ReplyDelete
  21. sir its coming to error in ns error in free() invalid pointer :0*0000000029c508 *** aborted (core dumbed)what is that problem

    ReplyDelete
  22. Greeting Suraj, thanks for your blog Sir. Pls I urgently need ReverseAODV tcl file and Jellyfish code for my project. I will be glad if you can kindly assist to get these. Thanks in advance.

    ReplyDelete

  23. if(strcmp(argv[1], "malicious""?") == 0) {
    malicious = true;
    return TCL_OK;
    }


    One more parameter to be added at ?

    ReplyDelete
  24. hey there!Do you know how to add a Sybil node in NS2 in AODV protocol?I have search for it but I can't find it.Hope that you can help me.

    ReplyDelete
  25. This comment has been removed by the author.

    ReplyDelete
  26. sir, please help me to add a selfish node in adhoc network.is that the same way as malicious node implemented??

    ReplyDelete
  27. Sir please how we can write the same code but with matlab ,i need to applied a sybil attack based on RSSI with matlab ,please i need your help

    ReplyDelete
  28. Can you please explain the out.tr file. Please explain that what each field represents.

    ReplyDelete
  29. Hello sir
    Does this work on NS 2.34 also???

    ReplyDelete
  30. Good evening sir,
    Could you give me the solution to the below mentioned problem asap.
    num_nodes is set 6
    Wrong node routing agent!

    ReplyDelete
  31. hey suraj
    when i am running this or any code of ns2, I am not getting animations in nam file
    In nam file only i see nodes labeled as 1,2,3.. but no circles around nodes or packet sending can be seen
    Can you help me?

    ReplyDelete
  32. hi sir,
    please i need to simulate a sybil and blackhole attacks in wsn ,can you help me

    ReplyDelete
  33. where i place the AODV.tcl file?????

    ReplyDelete
  34. Sir, I am still facing the error of having malicious node. Followed the tutorial video still facing this error. If you can help me I would be grateful

    ReplyDelete
  35. how to generate the xgraph for above tcl file?

    ReplyDelete
  36. This comment has been removed by the author.

    ReplyDelete
  37. This comment has been removed by the author.

    ReplyDelete
  38. hello Suraj can you tel me what the problem with code?
    i apply all the steps that you done but this the error find:

    aldja@aldja:~$ ns aodv.tcl
    num_nodes is set 6
    INITIALIZE THE LIST xListHead
    using backward compatible Agent/CBR; use Application/Traffic/CBR instead
    Start of simulation..
    ns: _o44 malicious :
    (_o44 cmd line 1)
    invoked from within
    "_o44 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 "_o44" line 2)
    (SplitObject unknown line 2)
    invoked from within
    "_o44 malicious "

    ReplyDelete
    Replies
    1. i'm having the same error..can u plzz help me..
      vishesh2969@gmail.com

      Delete
  39. Good Job sir. Your post is super helpful for everyone. I am stuck in simulating wormhole attack in NS2. I don't know how to create a tunnel. Can you help me with that? It will be life saving for me. But still even this is very helpful.
    "A glimmer of light through the gloom"

    ReplyDelete
  40. can anyone help me fixing this error
    trace/cmu-trace.cc:54:49: fatal error: taodv/taodv_packet.h: No such file or directory
    compilation terminated.
    Makefile:93: recipe for target 'trace/cmu-trace.o' failed
    make: *** [trace/cmu-trace.o] Error 1

    ReplyDelete
  41. please send me the aodv.h and aodv.cc files on educationpurpose10@gmail.com

    ReplyDelete
  42. do u have the old awk script ?

    ReplyDelete
  43. I am getting this problem. I have tried many times but still getting so please help me.

    ReplyDelete
  44. num_nodes is set 6
    INITIALIZE THE LIST xListHead
    using backward compatible Agent/CBR; use Application/Traffic/CBR instead
    Start of simulation..
    ns: _o44 blackhole:
    (_o44 cmd line 1)
    invoked from within
    "_o44 cmd blackhole"
    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 "_o44" line 2)
    (SplitObject unknown line 2)
    invoked from within
    "_o44 blackhole"

    ReplyDelete
  45. I am getting this problem. I have tried many times but still getting so please help me. My email is jeelani.0018@gmail.com
    num_nodes is set 6
    INITIALIZE THE LIST xListHead
    using backward compatible Agent/CBR; use Application/Traffic/CBR instead
    Start of simulation..
    ns: _o44 blackhole:
    (_o44 cmd line 1)
    invoked from within
    "_o44 cmd blackhole"
    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 "_o44" line 2)
    (SplitObject unknown line 2)
    invoked from within
    "_o44 blackhole"

    ReplyDelete