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

How to Implement Network Attacks in ns3

To Implement network attacks in ns-3 by simulating different types of attacks, such as Denial of Service (DoS), Man-in-the-Middle (MITM), and packet sniffing are listed below. Best coding on all domains for network attacks in ns-3 are shared by our team. The steps will guide on how to implement these attacks in ns3:

Step-by-Step Implementation of network Attacks in ns3:

  1. Install ns3

Ensure ns3 is installed on the system.

  1. Define the Network Topology

Define the network topology including:

  • Normal nodes (legitimate users)
  • Attacker nodes
  • Server nodes
  1. Create Network Nodes

Create network nodes using NodeContainer.

NodeContainer normalNodes, attackerNodes, serverNodes;

normalNodes.Create(3);

attackerNodes.Create(1);

serverNodes.Create(1);

4. Set Up Network Devices

Install network devices on the nodes using appropriate network interfaces, such as WiFi for wireless communication.

WifiHelper wifi;

wifi.SetStandard(WIFI_PHY_STANDARD_80211n_5GHZ);

WifiMacHelper mac;

mac.SetType(“ns3::AdhocWifiMac”);

YansWifiPhyHelper phy = YansWifiPhyHelper::Default();

YansWifiChannelHelper channel = YansWifiChannelHelper::Default();

phy.SetChannel(channel.Create());

NetDeviceContainer normalDevices = wifi.Install(phy, mac, normalNodes);

NetDeviceContainer attackerDevices = wifi.Install(phy, mac, attackerNodes);

NetDeviceContainer serverDevices = wifi.Install(phy, mac, serverNodes);

5. Configure Mobility Model

Set up the mobility model for the nodes.

MobilityHelper mobility;

mobility.SetMobilityModel(“ns3::ConstantPositionMobilityModel”);

mobility.Install(normalNodes);

mobility.Install(attackerNodes);

mobility.Install(serverNodes);

6. Implement Network Attacks

Implement different types of network attacks. Below are examples for DoS, MITM, and packet sniffing attacks.

a. DoS Attack Implementation

class DoSAttackApplication : public Application {

public:

    void StartApplication() override {

        sendSocket = Socket::CreateSocket(GetNode(), UdpSocketFactory::GetTypeId());

        sendSocket->Connect(InetSocketAddress(destAddress, destPort));

 

        // Schedule the first packet send

        SendPacket();

    }

 

    void SetRemote(Address address, uint16_t port) {

        destAddress = address;

        destPort = port;

    }

 

    void SendPacket() {

        std::string message = “This is a DoS attack packet”;

        Ptr<Packet> packet = Create<Packet>((uint8_t*) message.c_str(), message.size());

        sendSocket->Send(packet);

 

        // Schedule the next packet send

        Simulator::Schedule(MilliSeconds(10), &DoSAttackApplication::SendPacket, this);

    }

 

private:

    Ptr<Socket> sendSocket;

    Address destAddress;

    uint16_t destPort;

};

b. Man-in-the-Middle Attack Implementation

 

class MITMAttackApplication : public Application {

public:

    void StartApplication() override {

        recvSocket = Socket::CreateSocket(GetNode(), UdpSocketFactory::GetTypeId());

        recvSocket->Bind(InetSocketAddress(Ipv4Address::GetAny(), listenPort));

        recvSocket->SetRecvCallback(MakeCallback(&MITMAttackApplication::HandleRead, this));

 

        sendSocket = Socket::CreateSocket(GetNode(), UdpSocketFactory::GetTypeId());

    }

 

    void SetRemote(Address address, uint16_t port) {

        destAddress = address;

        destPort = port;

    }

 

    void SetListenPort(uint16_t port) {

        listenPort = port;

    }

 

    void HandleRead(Ptr<Socket> socket) {

        Ptr<Packet> packet;

        Address from;

        while ((packet = socket->RecvFrom(from))) {

            // Forward the packet to the destination

            sendSocket->SendTo(packet, 0, InetSocketAddress(destAddress, destPort));

        }

    }

 

private:

    Ptr<Socket> recvSocket;

    Ptr<Socket> sendSocket;

    Address destAddress;

    uint16_t destPort;

    uint16_t listenPort;

};

  1. Packet Sniffing Implementation

class PacketSniffingApplication : public Application {

public:

    void StartApplication() override {

        recvSocket = Socket::CreateSocket(GetNode(), UdpSocketFactory::GetTypeId());

        recvSocket->Bind(InetSocketAddress(Ipv4Address::GetAny(), listenPort));

        recvSocket->SetRecvCallback(MakeCallback(&PacketSniffingApplication::HandleRead, this));

    }

 

    void SetListenPort(uint16_t port) {

        listenPort = port;

    }

 

    void HandleRead(Ptr<Socket> socket) {

        Ptr<Packet> packet;

        Address from;

        while ((packet = socket->RecvFrom(from))) {

            std::cout << “Sniffed packet: ” << packet->ToString() << std::endl;

        }

    }

 

private:

    Ptr<Socket> recvSocket;

    uint16_t listenPort;

};

7. Set Up Applications

Install the applications on the nodes.

ApplicationContainer normalApps, attackerApps, serverApps;

 

// Normal node applications (e.g., sending normal traffic)

for (uint32_t i = 0; i < normalNodes.GetN(); ++i) {

    OnOffHelper onoff(“ns3::UdpSocketFactory”, InetSocketAddress(serverNodes.Get(0)->GetObject<Ipv4>()->GetAddress(1, 0).GetLocal(), 9));

    onoff.SetConstantRate(DataRate(“500kb/s”));

    ApplicationContainer app = onoff.Install(normalNodes.Get(i));

    app.Start(Seconds(1.0));

    app.Stop(Seconds(20.0));

    normalApps.Add(app);

}

 

// Attacker node applications (e.g., DoS attack)

for (uint32_t i = 0; i < attackerNodes.GetN(); ++i) {

    Ptr<DoSAttackApplication> app = CreateObject<DoSAttackApplication>();

    app->SetRemote(serverNodes.Get(0)->GetObject<Ipv4>()->GetAddress(1, 0).GetLocal(), 9);

    attackerNodes.Get(i)->AddApplication(app);

    app->SetStartTime(Seconds(5.0));

    app->SetStopTime(Seconds(20.0));

    attackerApps.Add(app);

}

 

// Server node application (e.g., packet sink)

PacketSinkHelpersink(“ns3::UdpSocketFactory”, InetSocketAddress(Ipv4Address::GetAny(), 9));

serverApps.Add(sink.Install(serverNodes.Get(0)));

serverApps.Start(Seconds(1.0));

serverApps.Stop(Seconds(20.0));

8. Set Up Routing Protocols

Configure routing protocols for the network.

AodvHelper aodv;

InternetStackHelper internet;

internet.SetRoutingHelper(aodv);

internet.Install(normalNodes);

internet.Install(attackerNodes);

internet.Install(serverNodes);

9. Assign IP Addresses

Assign IP addresses to the network devices.

Ipv4AddressHelper address;

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

Ipv4InterfaceContainer normalInterfaces = address.Assign(normalDevices);

Ipv4InterfaceContainer attackerInterfaces = address.Assign(attackerDevices);

Ipv4InterfaceContainer serverInterfaces = address.Assign(serverDevices);

10. Run the Simulation

Configure the simulation runtime and execute it.

Simulator::Stop(Seconds(20.0));

Simulator::Run();

Simulator::Destroy();

Example of a Simple Network Attack Script

#include “ns3/core-module.h”

#include “ns3/network-module.h”

#include “ns3/internet-module.h”

#include “ns3/wifi-module.h”

#include “ns3/mobility-module.h”

#include “ns3/applications-module.h”

#include “ns3/aodv-module.h”

using namespace ns3;

class DoSAttackApplication : public Application {

public:

    void StartApplication() override {

        sendSocket = Socket::CreateSocket(GetNode(), UdpSocketFactory::GetTypeId());

        sendSocket->Connect(InetSocketAddress(destAddress, destPort));

        // Schedule the first packet send

        SendPacket();

    }

    void SetRemote(Address address, uint16_t port) {

        destAddress = address;

        destPort = port;

    }

    void SendPacket() {

        std::string message = “This is a DoS attack packet”;

        Ptr<Packet> packet = Create<Packet>((uint8_t*) message.c_str(), message.size());

        sendSocket->Send(packet);

 

        // Schedule the next packet send

        Simulator::Schedule(MilliSeconds(10), &DoSAttackApplication::SendPacket, this);

    }

 

private:

    Ptr<Socket> sendSocket;

    Address destAddress;

    uint16_t destPort;

};

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

    NodeContainer normalNodes, attackerNodes, serverNodes;

    normalNodes.Create(3);

    attackerNodes.Create(1);

    serverNodes.Create(1);

    // WiFi setup

    WifiHelper wifi;

    wifi.SetStandard(WIFI_PHY_STANDARD_80211n_5GHZ);

    WifiMacHelper mac;

    mac.SetType(“ns3::AdhocWifiMac”);

    YansWifiPhyHelper phy = YansWifiPhyHelper::Default();

    YansWifiChannelHelper channel = YansWifiChannelHelper::Default();

    phy.SetChannel(channel.Create());

    NetDeviceContainer normalDevices = wifi.Install(phy, mac, normalNodes);

    NetDeviceContainer attackerDevices = wifi.Install(phy, mac, attackerNodes);

    NetDeviceContainer serverDevices = wifi.Install(phy, mac, serverNodes);

    // Mobility setup

    MobilityHelper mobility;

    mobility.SetMobilityModel(“ns3::ConstantPositionMobilityModel”);

    mobility.Install(normalNodes);

    mobility.Install(attackerNodes);

    mobility.Install(serverNodes);

 

    // Internet stack and routing

    AodvHelper aodv;

    InternetStackHelper internet;

    internet.SetRoutingHelper(aodv);

    internet.Install(normalNodes);

    internet.Install(attackerNodes);

    internet.Install(serverNodes);

    Ipv4AddressHelper address;

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

    Ipv4InterfaceContainer normalInterfaces = address.Assign(normalDevices);

    Ipv4InterfaceContainer attackerInterfaces = address.Assign(attackerDevices);

    Ipv4InterfaceContainer serverInterfaces = address.Assign(serverDevices);

    // Install applications

    ApplicationContainer normalApps, attackerApps, serverApps;

    // Normal node applications (e.g., sending normal traffic)

    for (uint32_t i = 0; i < normalNodes.GetN(); ++i) {

        OnOffHelper onoff(“ns3::UdpSocketFactory”, InetSocketAddress(serverNodes.Get(0)->GetObject<Ipv4>()->GetAddress(1, 0).GetLocal(), 9));

        onoff.SetConstantRate(DataRate(“500kb/s”));

        ApplicationContainer app = onoff.Install(normalNodes.Get(i));

        app.Start(Seconds(1.0));

        app.Stop(Seconds(20.0));

        normalApps.Add(app);

    }

    // Attacker node applications (e.g., DoS attack)

    for (uint32_t i = 0; i < attackerNodes.GetN(); ++i) {

        Ptr<DoSAttackApplication> app = CreateObject<DoSAttackApplication>();

        app->SetRemote(serverNodes.Get(0)->GetObject<Ipv4>()->GetAddress(1, 0).GetLocal(), 9);

        attackerNodes.Get(i)->AddApplication(app);

        app->SetStartTime(Seconds(5.0));

        app->SetStopTime(Seconds(20.0));

        attackerApps.Add(app);

    }

    // Server node application (e.g., packet sink)

    PacketSinkHelpersink(“ns3::UdpSocketFactory”, InetSocketAddress(Ipv4Address::GetAny(), 9));

    serverApps.Add(sink.Install(serverNodes.Get(0)));

    serverApps.Start(Seconds(1.0));

    serverApps.Stop(Seconds(20.0));

    Simulator::Stop(Seconds(20.0));

    Simulator::Run();

    Simulator::Destroy();

    return 0;

}

 Successfully, we have implemented the network attacks in ns3 environment using the given procedures by simulating different types of attacks to prevent the network. Related to your thesis we provide system development support reach out for us for novel help.