How to Implement Network Defences in ns2

To implement network defence in NS2 has numerous steps that has ne generate the network simulation in which defensive mechanisms are implemented to secure the network from attacks and intrusions. This can be containing the firewalls, intrusion detection systems (IDS), encryption, and other security measures. The aim is to mimic on how these defence mechanisms can prevent or mitigate the impacts of numerous network threats.

Step-by-Step Guide to Implementing Network Defense in NS2

  1. Understand Network Defense Components:
  • Firewall: A system that observes and controls incoming and outgoing network traffic based on predetermined security rules.
  • Intrusion Detection System (IDS): A system that classifies the malicious activities and potential intrusions by observing the network traffic.
  • Encryption: Securing the data by converting it into protect the format that cannot be easily implicit by unauthorized users.
  • Threat Detection and Response: Mechanisms to classify and respond to potential security threats in real-time.
  1. Set Up the NS2 Environment:
  • Make sure NS2 is installed on the computer.
  • Understand with writing TCL scripts, as NS2 simulations are controlled through TCL.
  1. Define the Network Topology:
  • Generate nodes that signify network devices that involve normal users, potential attackers, and defense nodes like firewall, IDS.

# Define the simulator

set ns [new Simulator]

# Create a trace file for capturing network traffic

set tracefile [open defense_trace.tr w]

$ns trace-all $tracefile

# Create a NAM file for visualization

set namfile [open defense_analysis.nam w]

$ns namtrace-all-wireless $namfile 10

# Set up the network parameters

set opt(chan)   Channel/WiredChannel         ;# Wired channel for LAN

set opt(prop)   Propagation/ConstantSpeed    ;# Propagation model for wired

set opt(netif)  Phy/WiredPhy                 ;# Network interface type for wired

set opt(mac)    Mac/802_3                    ;# Ethernet MAC type

set opt(ifq)    Queue/DropTail/PriQueue      ;# Interface queue type

set opt(ll)     LL                           ;# Link layer type

set opt(ifqlen) 50                           ;# Queue size

set opt(delay)  10ms                         ;# Link delay for wired network

# Create a topography object

create-god 10

# Configure the nodes (e.g., network devices and defense mechanisms)

$ns node-config -llType $opt(ll) \

-macType $opt(mac) \

-ifqType $opt(ifq) \

-ifqLen $opt(ifqlen) \

-propType $opt(prop) \

-phyType $opt(netif) \

-channelType $opt(chan) \

-topoInstance $topo \

-agentTrace ON \

-routerTrace ON \

-macTrace OFF \

-movementTrace OFF

# Create network nodes

set node1 [$ns node]  ;# Network Node 1 (e.g., server)

set node2 [$ns node]  ;# Network Node 2 (e.g., client)

set node3 [$ns node]  ;# Network Node 3 (e.g., router)

# Create an attacker node

set attacker [$ns node] ;# Attacker Node

# Create a defense node (e.g., firewall, IDS)

set defense_node [$ns node] ;# Defense Node

# Set initial positions for the nodes (optional for wired networks)

$node1 set X_ 100.0

$node1 set Y_ 100.0

$node1 set Z_ 0.0

$node2 set X_ 200.0

$node2 set Y_ 100.0

$node2 set Z_ 0.0

$node3 set X_ 300.0

$node3 set Y_ 100.0

$node3 set Z_ 0.0

$attacker set X_ 400.0

$attacker set Y_ 100.0

$attacker set Z_ 0.0

$defense_node set X_ 500.0

$defense_node set Y_ 100.0

$defense_node set Z_ 0.0

  1. Simulate Normal and Malicious Traffic:
  • Execute both normal communication among the network nodes and malicious activities from the attacker node.

# Create duplex links between nodes with defined bandwidth and delay

$ns duplex-link $node1 $node3 100Mb 10ms DropTail

$ns duplex-link $node2 $node3 100Mb 10ms DropTail

$ns duplex-link $attacker $node3 100Mb 10ms DropTail

$ns duplex-link $defense_node $node3 100Mb 10ms DropTail

# Normal traffic: Node 1 to Node 2 via Node 3

set tcp_node1 [new Agent/TCP]

$ns attach-agent $node1 $tcp_node1

set tcp_node2_sink [new Agent/TCPSink]

$ns attach-agent $node2 $tcp_node2_sink

$ns connect $tcp_node1 $tcp_node2_sink

# Start normal communication

set app_node1 [new Application/FTP]

$app_node1 attach-agent $tcp_node1

$ns at 2.0 “$app_node1 start”

# Malicious traffic: DoS attack from Attacker to Node 1

set udp_attacker [new Agent/UDP]

$ns attach-agent $attacker $udp_attacker

set null_sink [new Agent/Null]

$ns attach-agent $node1 $null_sink

$ns connect $udp_attacker $null_sink

# Start the DoS attack at time 3.0 seconds

set dos_attack [new Application/Traffic/CBR]

$dos_attack set packetSize_ 1024

$dos_attack set interval_ 0.001  ;# High-frequency packets to simulate DoS

$dos_attack attach-agent $udp_attacker

$ns at 3.0 “$dos_attack start”

  1. Implement the Network Defense Mechanism:
  • Execute defense mechanisms such as a firewall to block unwanted traffic, IDS to classify intrusions, and encryption to secure data.

Example 1: Firewall Implementation

# Firewall logic: Block traffic from attacker

proc firewall {src dst} {

if {$src == “attacker”} {

puts “Firewall: Blocking traffic from $src to $dst”

$src reset

} else {

puts “Firewall: Allowing traffic from $src to $dst”

}

}

# Schedule firewall checks on traffic from the attacker

$ns at 3.1 “firewall $attacker $node1”

Example 2: IDS Implementation

# IDS Logic: Detect and respond to potential threats

proc ids_detect {src dst packet_size threshold} {

global ns

if {$packet_size > $threshold} {

puts “IDS Alert: Possible DoS attack from $src to $dst at time [$ns now]”

# Block the attacker if a threat is detected

$ns at [expr $ns now + 0.5] “$src reset”

puts “IDS Action: Traffic from $src blocked”

}

}

# Schedule IDS to monitor traffic

$ns at 3.1 “ids_detect $attacker $node1 1024 800”

Example 3: Encryption Implementation

# Simple encryption function

proc encrypt_data {plaintext key} {

set ciphertext “”

foreach char [split $plaintext “”] {

set encrypted_char [expr {[scan $char %c] + $key}]

append ciphertext [format %c $encrypted_char]

}

return $ciphertext

}

# Simple decryption function

proc decrypt_data {ciphertext key} {

set plaintext “”

foreach char [split $ciphertext “”] {

set decrypted_char [expr {[scan $char %c] – $key}]

append plaintext [format %c $decrypted_char]

}

return $plaintext

}

# Encrypt data before sending

set original_message “HELLO”

set key 3

set encrypted_message [encrypt_data $original_message $key]

puts “Sending encrypted message: $encrypted_message”

# Decrypt data at the receiver end

$ns at 3.5 “set decrypted_message [decrypt_data $encrypted_message $key]”

$ns at 4.0 “puts \”Decrypted message: $decrypted_message\””

  1. Run the Simulation:
  • Describe as they the simulation should terminate and executed it. The finish procedure will close the trace files and launch NAM for visualization.

# Define the finish procedure

proc finish {} {

global ns tracefile namfile

$ns flush-trace

close $tracefile

close $namfile

exec nam defense_analysis.nam &

exit 0

}

# Schedule the finish procedure at 10 seconds

$ns at 10.0 “finish”

# Run the simulation

$ns run

  1. Analyse the Results:
  • Evaluation the defense_trace.tr files to evaluate on how the defense mechanisms responded to the attack. Look for firewall blocks, IDS alerts, and how encrypted data was managed.
  • Use the NAM file (defense_analysis.nam) to visualize the network operations and monitor on how the network defense mechanisms communicated with the network during the attack.
  1. Customize and Extend:
  • Customize the simulation by:
    • Executing the more advanced defense mechanisms, like multi-layered security, adaptive firewall rules, or incorporating the SIEM (Security Information and Event Management) systems.
    • To mimic various kinds of attacks, like phishing, SQL injection, or man-in-the-middle attacks, to validate the robustness of the defense system.
    • To add other security protocols such as VPNs, SSL/TLS, or MAC filtering.

Here, we completely implement the network defence in ns2 tools that setup the simulation and then generate the nodes and then apply the network defense process and evaluated. We also share the more data regarding the network defence. To get tailored services you can contact us for getting novel and original project ideas from our experts.