Wormhole is the common attack in ad hoc routing in which one malicious node tunnels the packets from its location to other malicious nodes. If the source node happens to choose this fake route, the attacker has the option of delivering the packets or dropping them. In this tcl code, the wormhole attacker does not obey the communication model. Data Transmission is established between nodes using UDP agent and CBR traffic. An adversary forms tunnel with other adversary using a direct low latency communication link.
for doing this you have to modify the aodv.h and aodv.cc code files in ns2 to create wormhole attack method as the concept mentioned above.
for {set i 0} {$i < $val(nn) } { incr i }
{ set node_($i) [$ns node] }
set udp [new Agent/UDP]
$ns attach-agent $node_(0)
$udp set cbr [new Application/Traffic/CBR]
$cbr set packetSize_ 1024
$cbr set interval_ 0.1
$cbr attach-agent $udp set null [new Agent/Null]
$ns attach-agent $node_(1) $null
$ns connect $udp $null
$ns at 2.0 "$cbr start"
$ns at 10.0 "$cbr stop"
$ns at 2.0 "$ns trace-annotate \"Sender sends the data to the receiver through the selected router which is attacker\""
$ns at 2.1 "$ns trace-annotate \"Attacker 3 and 8 forms wormhole\""
set udp [new Agent/UDP]
$ns attach-agent $node_(1)
$udp set cbr [new Application/Traffic/CBR]
$cbr set packetSize_ 1024
$cbr set interval_ 0.1
$cbr attach-agent $udp set null [new Agent/Null]
$ns attach-agent $node_(3) $null
$ns connect $udp $null
$ns at 3.0 "$cbr start"
$ns at 10.0 "$cbr stop"
$ns at 3.0 "$ns trace-annotate \"Attacker forwards the data to Attacker 8 which does not forward the data to receiver\""
Run this code to implement wormhole attack in wireless sensor networks using ns2
#wormhole-attack.tcl
#=================================== # Simulation parameters setup #=================================== set val(chan) Channel/WirelessChannel ;# channel type set val(prop) Propagation/TwoRayGround ;# radio-propagation model set val(netif) Phy/WirelessPhy ;# network interface type set val(mac) Mac/802_11 ;# MAC type set val(ifq) Queue/DropTail/PriQueue ;# interface queue type set val(ll) LL ;# link layer type set val(ant) Antenna/OmniAntenna ;# antenna model set val(ifqlen) 50 ;# max packet in ifq set val(nn) 7 ;# number of mobilenodes set val(rp) AODV ;# routing protocol set val(x) 800 ;# X dimension of topography set val(y) 541 ;# Y dimension of topography set val(stop) 100.0 ;# time of simulation end #=================================== # Initialization #=================================== #Create a ns simulator set ns [new Simulator] #Setup topography object set topo [new Topography] $topo load_flatgrid $val(x) $val(y) create-god $val(nn) #Open the NS trace file set tracefile [open wormhole-attack.tr w] $ns trace-all $tracefile #Open the NAM trace file set namfile [open wormhole-attack.nam w] $ns namtrace-all $namfile $ns namtrace-all-wireless $namfile $val(x) $val(y) set chan [new $val(chan)];#Create wireless channel #=================================== # Mobile node parameter setup #=================================== $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) \ -channel $chan \ -topoInstance $topo \ -agentTrace ON \ -routerTrace ON \ -macTrace OFF \ -movementTrace ON #=================================== # Nodes Definition #=================================== #Create 7 nodes set n0 [$ns node] $n0 set X_ 99 $n0 set Y_ 299 $n0 set Z_ 0.0 $ns initial_node_pos $n0 20 set n1 [$ns node] $n1 set X_ 299 $n1 set Y_ 297 $n1 set Z_ 0.0 $ns initial_node_pos $n1 20 set n2 [$ns node] $n2 set X_ 499 $n2 set Y_ 298 $n2 set Z_ 0.0 $ns initial_node_pos $n2 20 set n3 [$ns node] $n3 set X_ 700 $n3 set Y_ 299 $n3 set Z_ 0.0 $ns initial_node_pos $n3 20 set n4 [$ns node] $n4 set X_ 199 $n4 set Y_ 350 $n4 set Z_ 0.0 $ns initial_node_pos $n4 20 set n5 [$ns node] $n5 set X_ 599 $n5 set Y_ 350 $n5 set Z_ 0.0 $ns initial_node_pos $n5 20 set n6 [$ns node] $n6 set X_ 600 $n6 set Y_ 200 $n6 set Z_ 0.0 $ns initial_node_pos $n6 20 # Node 5 is given RED Color and a label- indicating it is a Blackhole Attacker $n5 color red $ns at 0.0 "$n5 color red" $ns at 0.0 "$n5 label Attacker" # Node 0 is given GREEN Color and a label - acts as a Source Node $n0 color green $ns at 0.0 "$n0 color green" $ns at 0.0 "$n0 label Source" # Node 3 is given BLUE Color and a label- acts as a Destination Node $n3 color blue $ns at 0.0 "$n3 color blue" $ns at 0.0 "$n3 label Destination" #=================================== # Set node 5 as attacker #=================================== #$ns at 40.0 "[$n2 set ragent_] wormhole-peer" #$ns at 50.0 "[$n2 set ragent_] normal" [$n2 set ll_(0)] wormhole-peer [$n5 set ll_(0)] #=================================== # Agents Definition #=================================== #Setup a UDP connection set udp0 [new Agent/UDP] $ns attach-agent $n0 $udp0 set null1 [new Agent/Null] $ns attach-agent $n3 $null1 $ns connect $udp0 $null1 $udp0 set packetSize_ 1000 #=================================== # Applications Definition #=================================== #Setup a CBR Application over UDP connection set cbr0 [new Application/Traffic/CBR] $cbr0 attach-agent $udp0 $cbr0 set packetSize_ 1000 $cbr0 set rate_ 0.1Mb $cbr0 set random_ null $ns at 1.0 "$cbr0 start" $ns at 100.0 "$cbr0 stop" #=================================== # Termination #=================================== #Define a 'finish' procedure proc finish {} { global ns tracefile namfile $ns flush-trace close $tracefile close $namfile exec nam wormhole-attack.nam & exit 0 } for {set i 0} {$i < $val(nn) } { incr i } { $ns at $val(stop) "\$n$i reset" } $ns at $val(stop) "$ns nam-end-wireless $val(stop)" $ns at $val(stop) "finish" $ns at $val(stop) "puts \"done\" ; $ns halt" $ns run