Ns3 Projects for B.E/B.Tech M.E/M.Tech PhD Scholars.  Phone-Number:9790238391   E-mail: ns3simulation@gmail.com

How to Implement masquerade attack in ns3

To implement masquerade attack in ns3, we have to simulate a scenario where an attacker disguising themselves as a legitimate user or device to gain unauthorized access to network resources. By having an attacker node send packets with spoofed source addresses to deceive other nodes in the network, we can simulate this scenario. Also, ns3simulation.com  provide more related simulation on masquerade attack.

Here are the steps to implement masquerade attack in ns3.

Steps for implementation

  1. Set up your ns3 :
  • Make sure that ns3 is installed in the computer. If not, install it.
  1. Create a new ns3 script :
  • In the scratch directory of ns3, create a new script.
  1. Include necessary libraries :
  • In your script, include the necessary libraries.
  1. Define network topology :
  • For your network topology, create legitimate nodes and one attacker node.
  1. Implement the Masquerade attack logic :
  • To the victim node, send packets with spoofed source addresses.
  1. Enable packet capturing :
  • Enable pcap tracing to capture packets for analysis with Wireshark.
  1. Run the Simulation :
  • Define the simulation parameters and run it.

Example for implementing Masquerade attack

Here is the example for the implementation of Masquerade :

#include “ns3/core-module.h”

#include “ns3/network-module.h”

#include “ns3/internet-module.h”

#include “ns3/point-to-point-module.h”

#include “ns3/applications-module.h”

#include “ns3/ipv4-header.h”

#include “ns3/udp-header.h”

using namespace ns3;

NS_LOG_COMPONENT_DEFINE (“MasqueradeAttack”);

void SendSpoofedPackets (Ptr<Node> attackerNode, Ipv4Address spoofedSourceAddress, Ipv4Address victimAddress, uint16_t port, uint32_t packetSize, uint32_t numPackets, Time interval)

{

Ptr<Socket> socket = Socket::CreateSocket (attackerNode, TypeId::LookupByName (“ns3::UdpSocketFactory”));

InetSocketAddress remote = InetSocketAddress (victimAddress, port);

socket->Connect (remote);

for (uint32_t i = 0; i < numPackets; ++i)

{

Ptr<Packet> packet = Create<Packet> (packetSize);

Ipv4Header ipv4Header;

ipv4Header.SetDestination (victimAddress);

ipv4Header.SetSource (spoofedSourceAddress);

ipv4Header.SetProtocol (17); // UDP protocol number

UdpHeader udpHeader;

udpHeader.SetSourcePort (12345);

udpHeader.SetDestinationPort (port);

udpHeader.SetPayloadSize (packetSize – ipv4Header.GetSerializedSize () – udpHeader.GetSerializedSize ());

packet->AddHeader (udpHeader);

packet->AddHeader (ipv4Header);

socket->Send (packet);

Simulator::Schedule (interval, &SendSpoofedPackets, attackerNode, spoofedSourceAddress, victimAddress, port, packetSize, numPackets, interval);

}

}

int main (int argc, char *argv[])

{

bool verbose = true;

uint32_t nNodes = 2; // Number of legitimate nodes

uint16_t port = 8080; // Target port for the attack

uint32_t packetSize = 1024; // Size of the packet

uint32_t numPackets = 10; // Number of packets to send

Time interval = Seconds (1.0); // Interval between packets

CommandLine cmd;

cmd.AddValue (“nNodes”, “Number of legitimate nodes”, nNodes);

cmd.AddValue (“port”, “Target port for the attack”, port);

cmd.AddValue (“packetSize”, “Size of the packets”, packetSize);

cmd.AddValue (“numPackets”, “Number of packets”, numPackets);

cmd.AddValue (“interval”, “Interval between packets”, interval);

cmd.Parse (argc, argv);

if (verbose)

{

LogComponentEnable (“MasqueradeAttack”, LOG_LEVEL_INFO);

}

NodeContainer nodes;

nodes.Create (nNodes + 1); // +1 for the attacker node

Ptr<Node> attacker = nodes.Get (nNodes);

PointToPointHelper pointToPoint;

pointToPoint.SetDeviceAttribute (“DataRate”, StringValue (“5Mbps”));

pointToPoint.SetChannelAttribute (“Delay”, StringValue (“2ms”));

NetDeviceContainer devices;

for (uint32_t i = 0; i < nodes.GetN () – 1; ++i)

{

devices.Add (pointToPoint.Install (nodes.Get (i), nodes.Get (i + 1)));

}

InternetStackHelper stack;

stack.Install (nodes);

Ipv4AddressHelper address;

address.SetBase (“10.1.1.0”, “255.255.255.0”);

Ipv4InterfaceContainer interfaces = address.Assign (devices);

// Install applications on legitimate nodes

UdpEchoServerHelper echoServer (port);

ApplicationContainer serverApps = echoServer.Install (nodes.Get (nNodes – 1));

serverApps.Start (Seconds (1.0));

serverApps.Stop (Seconds (10.0));

UdpEchoClientHelper echoClient (interfaces.GetAddress (nNodes – 1), port);

echoClient.SetAttribute (“MaxPackets”, UintegerValue (100));

echoClient.SetAttribute (“Interval”, TimeValue (Seconds (0.1)));

echoClient.SetAttribute (“PacketSize”, UintegerValue (packetSize));

ApplicationContainer clientApps = echoClient.Install (nodes.Get (0));

clientApps.Start (Seconds (2.0));

clientApps.Stop (Seconds (10.0));

// Schedule masquerade attack with spoofed source address

Ipv

Explanation

  1. Nodes and links :

Nodes are created for legitimate communication and for one attacker. Point-to-point links between nodes are configured.

  1. Applications :

On one of the legitimate node, a UDP echo server is installed. and On another legitimate node, a UDP echo server is installed to generate traffic.

  1. Masquerade attack logic :

To create and send packets with spoofed source addresses from the attacker node to the victim node,  SendSpoofedPackets is implemented. To start at a specific time in the simulation, we scheduled the masquerade attack.

  1. Packet Capture :

To capture the traffic for analysis with Wireshark, pcap tracing on all nodes is enabled.

  1. Running the Simulation :

The simulation runs with attacker node sending packets with a spoofed source address to the victim node, and the traffic is captured in pcap files.Overall, we had successfully learned on implementing masquerade attack in ns3 by simulating a network where an attacker disguises themselves as a legitimate user or device to gain unauthorized access to network resources.

If you face any difficulties on masquerade attack in ns3 simulation then feel free to address us the issues you are facing.