Search This Blog

[ns-3] Blackhole Attack Simulation in ns-3

Labels: , , , , , , , ,


The patch provided in this post enables a ns-3 user to simulate "Blackhole Attack in ns-3" for Mobile Ad hoc Network (MANET) Simulations. Blackhole attack has been implemented using Ad hoc On demand Distance Vector (AODV) routing protocol.

This patch is available for:
1. ns-allinone-3.25    -  Blackhole-ns-3.25.patch
2. ns-allinone-3.24.1 -  Blackhole-ns-3.24.1.patch
2. ns-allinone-3.24    -  Blackhole-ns-3.24.patch
3. ns-allinone-3.23    -  Blackhole-ns-3.23.patch
4. ns-allinone-3.22    -  Blackhole-ns-3.22.patch
5. ns-allinone-3.21    -  Blackhole-ns-3.21.patch
6. ns-allinone-3.20    -  Blackhole-ns-3.20.patch

Patch for ns-3.20 Contributed by: Shalini Satre, Wireless Information Networking Group (WiNG), NITK Surathkal.

Material provided with this post:
1. A patch to simulate Blackhole attack in ns-3 and steps to apply that patch.
2. Sample C++ Program to ensure that the patch is applied correctly!

Follow the steps given below to apply Blackhole patch to ns-3.25:

Note: (i) Steps remain same for applying the patch to other versions of ns-3, except that you should use ns-3.xx instead of ns-3.25 wherever applicable. (ii) This blog post is updated whenever a new version of ns-3 is released.

If ns-3 is "not installed" in your machine:

1. Download ns-allinone-3.25.tar.bz2

2. Download Blackhole-ns-3.25.patch

3. Unzip ns-allinone-3.25.tar.bz2. You will get a folder named ns-allinone-3.25

4. Paste the downloaded Blackhole-ns3 patch in the above mentioned folder.

5. Give the following command:

patch -p1 < Blackhole-ns-3.25.patch

6. Go in ns-allinone-3.25 via terminal and give the following command to install ns-3.25

./ --enable-examples --enable-tests

You are done with it!

If ns-3 is "already installed" on your machine:

1. Paste the downloaded Blackhole-ns-3.25 patch in ns-allinone-3.25 directory.

2. Give the following command:

patch -p1 < Blackhole-ns-3.25.patch

3. Go in ns-allinone-3.25/ns-3.25 directory and give the following commands:


You are done with it!

Sample C++ Program to verify the working of Blackhole Patch:

Download the zipped folder from the link given below and extract the files from it:

Put both the extracted files in the following folder:


How to use these files for verification?

I. Go to ns-allinone-3.25/ns-3.25 via terminal and give the following command:

./waf --run scratch/blackhole

You will get an output as shown below:

[Click on the Image to enlarge]


1. Thanks to Shalini Satre for developing this patch and providing a sample C++ Program to verify its working.

2. Thanks to ns-3 mailing list on Google Groups for their wonderful support and encouragement!

Hope it helps.

Mohit P. Tahiliani

Comments (42)

To all, i want "sinkhole attack ns-2 code for mobile adhoc network using AODV protocol". i hope that u will help me and send me a code.

@Vivek - To the best of my knowledge, there is no such code available as of now. So you may have to write the code yourself.

Hope it helps.

Mohit P. Tahiliani

i used Telnet application with tcp in NS2.35 . And i found generated trace
file is empty.
howz it possible??
even in Nam window no data flows.
i tried a lot but could not find the solution.
i tried simple script.
Please have a look once and help me if u can.


set ns [new Simulator]

set mynam [open telnet.nam w]
$ns namtrace-all $mynam

set mytrace [open w]
$ns trace-all $mytrace

set winfile [open telnet.xg w]

proc finish {} {
global ns mynam mytrace
$ns flush-trace
close $mynam
close $mytrace
#exec nam d1.nam &
exit 0

set n0 [$ns node]
set n1 [$ns node]

$ns duplex-link $n0 $n1 1Mb 10ms DropTail
$ns queue-limit $n0 $n1 4

set tcp0 [new Agent/TCP]

# $tcp0 set ssthresh_ 50

$ns attach-agent $n0 $tcp0

set telnet0 [new Application/Telnet]
$telnet0 attach-agent $tcp0
$telnet0 set interval_ 20

set sink0 [new Agent/TCPSink]

$ns attach-agent $n1 $sink0

$ns connect $tcp0 $sink0

$ns at 0.5 "$telnet0 start"
$ns at 4.5 "$telnet0 stop"

proc plotWindow {tcpSource file} {
global ns
set time 0.01
set now [$ns now]
set cwnd [$tcpSource set cwnd_]
set wnd [$tcpSource set window_]
set ssthresh [$tcpSource set ssthresh_]
puts $file "$now $cwnd \t \t \t \t \t \t $wnd \t \t \t \t \t $ssthresh"
$ns at [expr $now+$time] "plotWindow $tcpSource $file"


$ns at 0.5 "plotWindow $tcp0 $winfile"

$ns at 5.0 finish

$ns run

Subject: problem in building dce quagga

hello sir.....
when i build the dce quagga using the command build

i am getting the following problem...can you help me

Building iputils - Problem
> Subprocess failed with error 2: ['make', 'CFLAGS=-fpic', 'CFLAGS+=-D_GNU_SOURCE', 'CFLAGS+=-g', 'CFLAGS+=-Wstrict-prototypes', 'CFLAGS+=-Wall', 'LDFLAGS=-pie', 'LDFLAGS+=-rdynamic', 'ping', 'ping6']
> Error: Critical dependency, module "iputils" failed

Can I use this model but instead of immobile nodes? I use a random2dwalk mobility model When I tried it it did not work.. ANY help???

@Anjali - I haven't worked on Telnet application in ns-2 and so would not be able to help you much on this!

@Ahsan - I recommend you to write to ?Hajime Tazaki for this error that you are getting. He is the correct person to help you out on that.

@Rania - The patch has nothing to do with the mobility models and there should not be any error due to that. I suggest you to cross check your mobility related code to sort out the errors.

Hope it helps.

Mohit P. Tahiliani

doesn't work with ns-3.20..
it asks input files while applying patch..

@Nagesh - The patch works absolutely fine with ns-3.20.

I believe you gave the patch command from a different directory.

Try once again by giving the command in the correct directory.

Hope it helps.

Mohit P. Tahiliani


I am working on wormhole detection in AOMDV protocol.
For that, I have got code for a tcl file from here:

but I am getting many errors when I use the same code.
Can you help me and suggest some links where I can find wormhole patch for AOMDV protocol. Or the changes required in the above file when I use it in my environment.

I am using NS 2.34

@Ankur - Before running the TCL file, you must try to see if there exists a code in C++ to launch the wormhole attack. If that does not exist, you will certainly get an error while running the TCL file.

I haven't come across any C++ code for wormhole attack simulation with AOMDV.

Hope it helps.

Mohit P. Tahiliani

Hello sir,
this code work perfectly in ns-3.21

i want to simulate grey-hole attack so what changes i must do?

Please suggest me some idea so that i can detect both greyhole and blackhole attack.

Thanks & regards

@Affable - I haven't worked on grey hole attack. But if you are working on AODV - most of the changes will be in the same file where we have made changes for Blackhole attack.

Mohit P. Tahiliani

what about Sybil attack in VANET??? is there any patch for ns3 or ns2 ??

@Jaydip - Not really! some links claim to have the patch but when you finally download - that's not the actual code.

Mohit P. Tahiliani

Hi Sir
I want to implement snoop agent in Base Station for wired wireless scenario . when i do by simply changing LL to LL/LLSnoop got segmentation fault error.
How can I do that? if any code or example then please provide me.

@Satish - Try some examples given in


They have given a sample example for Snoop.

Hope it helps.

Mohit P. Tahiliani

Sir i want to implement the grayhole attack in ns3.Can you please provide me with the code

Sir i have tried implementing black hole code in ns3 it worked fine.I want to implement gray hole attack can you tell me what changes i need to do in black hole file.

Sir, can i knw whether the blackhole attack in manet source code that u have provided will work for vanet??

@Renita - I have not worked on Gray hole attack yet. Once I have a patch for it, I will post it on this blog.

@Pooja - Yes, this patch will work for VANETs also, provided you use AODV as the routing protocol.

Hope it helps.

Mohit P. Tahiliani

Sir thank you for your help! you are saving a lot of academic lives! May I know any other attack modules you have worked on ?

@Anubhav Rawat - We have worked on wormhole attack and its examples. The code for same is available in ns-3-users google group. The patch isn't ready yet.

Hope it helps.

Mohit P. Tahiliani

hello sir..
i need to simulate wormhole attack. I have 2 files named and myapp.h but its showing error
please help me to run wormhole attack in ns3.

Sir i don't find the packet drop when i change the mobility model of the code.I changed it into Random Way Point model and then they are forwarding all the packets.

i need to simulat wormhole attack in ns2 .
please help me by sending .tcl and .patch file
my email

I want to simulate blackhole attack on lte network in ns3.
i have simulated lte network, will the code provided by you for blackhole attack will help me for this. if yes then sir please provide me some guideline.


Plz help!!!! Why your blackhole sample code do not give throughput of every seconds??

Thank you for the great work done in providing us this useful blackhole attack patch sir.
Please, I have a question, why is it that only 4 packets are dropped by the malicious blackhole node out of all the 5 packets sent in the sample code?

I want to simulate mac scheduler (lte module)on ns-3.25, but i m not able to simulate sample codes available in models.How to execute those codes?

Hello Sir!

Can we simulate balckhole patch 3.25 in ns-3.26?

Respected Sir,

when i run blackhole patch 3.25 i found following error pls help me.

neelam@neelam-Lenovo-G550:~/Downloads/ns-allinone-3.25$ patch -p1 < Blackhole-ns-3.25.patch
patching file ns-3.25/src/aodv/model/
Hunk #1 FAILED at 253.
Hunk #2 FAILED at 527.
Hunk #3 FAILED at 1209.
Hunk #4 FAILED at 1231.
Hunk #5 FAILED at 1300.
5 out of 5 hunks FAILED -- saving rejects to file ns-3.25/src/aodv/model/
patching file ns-3.25/src/aodv/model/aodv-routing-protocol.h
Hunk #1 FAILED at 86.
Hunk #2 FAILED at 161.
2 out of 2 hunks FAILED -- saving rejects to file ns-3.25/src/aodv/model/aodv-routing-protocol.h.rej

HI all I am a reaserch scholar and my domain is WSN for the detection and defense of sybil attacks I need the base code for the sybil attack?
Can anyone help me to find it out

Dear sir,
How can i calculate these values for a mesh network to find out blackhole attack.

YansWifiChannelHelper wifiChannel ;
wifiChannel.SetPropagationDelay ("ns3::ConstantSpeedPropagationDelayModel");
wifiChannel.AddPropagationLoss ("ns3::TwoRayGroundPropagationLossModel",
"SystemLoss", DoubleValue(1),
"HeightAboveZ", DoubleValue(1.5));

wifiPhy.Set ("TxPowerStart", DoubleValue(33));
wifiPhy.Set ("TxPowerEnd", DoubleValue(33));
wifiPhy.Set ("TxPowerLevels", UintegerValue(1));
wifiPhy.Set ("TxGain", DoubleValue(0));
wifiPhy.Set ("RxGain", DoubleValue(0));
wifiPhy.Set ("EnergyDetectionThreshold", DoubleValue(-61.8));
wifiPhy.Set ("CcaMode1Threshold", DoubleValue(-64.8));


Hi. Dear Mohit. I am running the ns-3.26 version. I have run the sample in your post. Its giving me one error: " Invalid attribute set (IsMalicious) on ns3::aodv::RoutingProtocol. Can you tell me how to resolve it, please? Thanks in advance. :)

Plz help!!!! i work in my project about routing security to prevent block hole attack using AODV protocol and i use algorithem for checking bolck hole using data routing table but i want code,can help me, code sample for prevented block hole attack.

i'm esti,i'm from indonesia.i using DSR protocol in ns-3.25. but it is so difficult to me for using black hole attack too.can you help me to give me code patch blackhole attack in DSR?

hello sir ,
i want to simulate flooding attack in MANET using AODV protocol in NS-3.19 .
please provide me the patch file for detection of flooding attack.
if not , then what changes that ,i should done in blackhole attack patch file?
please help me sir!!
i shall be very thankful to you.

hello sir. i want to ask is patch 3.25 work for ns3 3.27 version? thanks..

Could you please post the's code? Thanks.

Greetings Sir! I have the following issue:
../scratch/ In function ‘int main(int, char**)’:
../scratch/ error: ‘NqosWifiMacHelper’ was not declared in this scope
NqosWifiMacHelper wifiMac = NqosWifiMacHelper::Default ();
../scratch/ error: ‘wifiMac’ was not declared in this scope
wifiMac.SetType ("ns3::AdhocWifiMac");

How can I vary the data rates of the nodes ?

Sir can you please explain the code