Created
August 16, 2021 11:33
-
-
Save wlkr/3e73c58392ccd9f4bbda5b88782eb60d to your computer and use it in GitHub Desktop.
AODV blackhole attack implementation for ns-3.34, based upon https://mohittahiliani.blogspot.com/2014/12/ns-3-blackhole-attack-simulation-in-ns-3.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff -Naur ns-allinone-3.34/ns-3.34/src_orig/aodv/model/aodv-routing-protocol.cc ns-allinone-3.34/ns-3.34/src/aodv/model/aodv-routing-protocol.cc | |
--- ns-allinone-3.34/ns-3.34/src_orig/aodv/model/aodv-routing-protocol.cc 2021-07-14 18:31:06.000000000 +0100 | |
+++ ns-allinone-3.34/ns-3.34/src/aodv/model/aodv-routing-protocol.cc 2021-08-16 12:25:40.915620956 +0100 | |
@@ -296,6 +296,12 @@ | |
StringValue ("ns3::UniformRandomVariable"), | |
MakePointerAccessor (&RoutingProtocol::m_uniformRandomVariable), | |
MakePointerChecker<UniformRandomVariable> ()) | |
+ .AddAttribute ("IsMalicious", "Is the node malicious", | |
+ BooleanValue (false), | |
+ MakeBooleanAccessor (&RoutingProtocol::SetMaliciousEnable, | |
+ &RoutingProtocol::GetMaliciousEnable), | |
+ MakeBooleanChecker ()) | |
+ | |
; | |
return tid; | |
} | |
@@ -597,6 +603,14 @@ | |
Ipv4Address origin = header.GetSource (); | |
m_routingTable.Purge (); | |
RoutingTableEntry toDst; | |
+ /* Code added by Shalini Satre, Wireless Information Networking Group (WiNG), NITK Surathkal for simulating Blackhole Attack */ | |
+ /* Check if the node is suppose to behave maliciously */ | |
+ if(IsMalicious) | |
+ {//When malicious node receives packet it drops the packet. | |
+ std :: cout <<"Launching Blackhole Attack! Packet dropped . . . \n"; | |
+ return false; | |
+ } | |
+ /* Code for Blackhole attack simulation ends here */ | |
if (m_routingTable.LookupRoute (dst, toDst)) | |
{ | |
if (toDst.GetFlag () == VALID) | |
@@ -1339,13 +1353,16 @@ | |
SendReply (rreqHeader, toOrigin); | |
return; | |
} | |
+ | |
/* | |
* (ii) or it has an active route to the destination, the destination sequence number in the node's existing route table entry for the destination | |
* is valid and greater than or equal to the Destination Sequence Number of the RREQ, and the "destination only" flag is NOT set. | |
*/ | |
+ | |
RoutingTableEntry toDst; | |
Ipv4Address dst = rreqHeader.GetDst (); | |
- if (m_routingTable.LookupRoute (dst, toDst)) | |
+ | |
+ if (IsMalicious || m_routingTable.LookupRoute (dst, toDst)) | |
{ | |
/* | |
* Drop RREQ, This node RREP will make a loop. | |
@@ -1361,12 +1378,27 @@ | |
* However, the forwarding node MUST NOT modify its maintained value for the destination sequence number, even if the value | |
* received in the incoming RREQ is larger than the value currently maintained by the forwarding node. | |
*/ | |
- if ((rreqHeader.GetUnknownSeqno () || (int32_t (toDst.GetSeqNo ()) - int32_t (rreqHeader.GetDstSeqno ()) >= 0)) | |
- && toDst.GetValidSeqNo () ) | |
+ if (IsMalicious || ((rreqHeader.GetUnknownSeqno () || (int32_t (toDst.GetSeqNo ()) - int32_t (rreqHeader.GetDstSeqno ()) >= 0)) | |
+ && toDst.GetValidSeqNo ()) ) | |
{ | |
- if (!rreqHeader.GetDestinationOnly () && toDst.GetFlag () == VALID) | |
+ if (IsMalicious || (!rreqHeader.GetDestinationOnly () && toDst.GetFlag () == VALID)) | |
{ | |
m_routingTable.LookupRoute (origin, toOrigin); | |
+ /* Code added by Shalini Satre, Wireless Information Networking Group (WiNG), NITK Surathkal for simulating Blackhole Attack | |
+ * If node is malicious, it creates false routing table entry having sequence number much higher than | |
+ * that in RREQ message and hop count as 1. | |
+ * Malicious node itself sends the RREP message, | |
+ * so that the route will be established through malicious node. | |
+ */ | |
+ if(IsMalicious) | |
+ { | |
+ Ptr<NetDevice> dev = m_ipv4->GetNetDevice (m_ipv4->GetInterfaceForAddress (receiver)); | |
+ RoutingTableEntry falseToDst(dev,dst,true,rreqHeader.GetDstSeqno()+100,m_ipv4->GetAddress (m_ipv4->GetInterfaceForAddress (receiver),0),1,dst,m_activeRouteTimeout); | |
+ | |
+ SendReplyByIntermediateNode (falseToDst, toOrigin, rreqHeader.GetGratuitousRrep ()); | |
+ return; | |
+ } | |
+ /* Code for Blackhole Attack Simulation ends here */ | |
SendReplyByIntermediateNode (toDst, toOrigin, rreqHeader.GetGratuitousRrep ()); | |
return; | |
} | |
@@ -1446,7 +1478,15 @@ | |
/* If the node we received a RREQ for is a neighbor we are | |
* probably facing a unidirectional link... Better request a RREP-ack | |
*/ | |
- if (toDst.GetHop () == 1) | |
+ | |
+ ///Attract node to set up path through malicious node | |
+ | |
+ if(IsMalicious) //Shalini Satre | |
+ { | |
+ rrepHeader.SetHopCount(1); | |
+ } | |
+ | |
+ if (toDst.GetHop () == 1 ) | |
{ | |
rrepHeader.SetAckRequired (true); | |
RoutingTableEntry toNextHop; | |
diff -Naur ns-allinone-3.34/ns-3.34/src_orig/aodv/model/aodv-routing-protocol.h ns-allinone-3.34/ns-3.34/src/aodv/model/aodv-routing-protocol.h | |
--- ns-allinone-3.34/ns-3.34/src_orig/aodv/model/aodv-routing-protocol.h 2021-07-14 18:31:06.000000000 +0100 | |
+++ ns-allinone-3.34/ns-3.34/src/aodv/model/aodv-routing-protocol.h 2021-08-16 12:21:46.265589000 +0100 | |
@@ -170,6 +170,8 @@ | |
{ | |
return m_enableBroadcast; | |
} | |
+ void SetMaliciousEnable (bool f) { IsMalicious = f; } // Method declared for Blackhole Attack Simulation - Shalini Satre | |
+ bool GetMaliciousEnable () const { return IsMalicious; } // Method declared for Blackhole Attack Simulation - Shalini Satre | |
/** | |
* Assign a fixed random variable stream number to the random variables | |
@@ -258,6 +260,8 @@ | |
uint16_t m_rreqCount; | |
/// Number of RERRs used for RERR rate control | |
uint16_t m_rerrCount; | |
+ /// Set node as malicious. Dropping every packet received. | |
+ bool IsMalicious; | |
private: | |
/// Start protocol operation |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment