How to Implement Network Security Information in NS2

To implement the Network Security Information in NS2, we have to simulate the security-based situations, attacks or protocols which help to optimize or evaluate the security posture of a network. It has encrypting data, validating users, identifying interference or replicating security attacks includes DoS or man-in-the-middle attacks to analyze the network’s flexibility.

Here’s how you can approach implementing Network Security Information in NS2:

Step-by-Step Guide to Implement Network Security Information in NS2:

  1. Set Up the Network Topology:
  • First, we have to configure a simplified network of nodes. You will later execute security mechanisms like encryption, validation, or intrusion detection on top of this network.

Example OTcl script for basic network setup:

set ns [new Simulator]

set nf [open out.tr w]

$ns trace-all $nf

set namfile [open out.nam w]

$ns namtrace-all $namfile

# Define network nodes

set node0 [$ns node]   ;# Sender

set node1 [$ns node]   ;# Receiver

set node2 [$ns node]   ;# Intermediate Node (or Attacker Node)

# Create duplex links

$ns duplex-link $node0 $node1 1Mb 10ms DropTail

$ns duplex-link $node0 $node2 1Mb 10ms DropTail

$ns duplex-link $node2 $node1 1Mb 10ms DropTail

# Define traffic from node0 to node1

set udp [new Agent/UDP]

set null [new Agent/Null]

$ns attach-agent $node0 $udp

$ns attach-agent $node1 $null

$ns connect $udp $null

set cbr [new Application/Traffic/CBR]

$cbr attach-agent $udp

$cbr set packetSize_ 512

$cbr set rate_ 1Mb

$ns at 1.0 “$cbr start”

$ns at 5.0 “$cbr stop”

# Run simulation

$ns at 6.0 “finish”

proc finish {} {

global ns nf namfile

$ns flush-trace

close $nf

close $namfile

exec nam out.nam &

exit 0

}

$ns run

  1. Implement Data Encryption and Decryption:
  • One basic form of security is encryption. In NS2, you can replicate encryption by altering packet headers or payloads to indicate encrypted data. On the receiver side, you simulate decryption by converting the data back to its original form.

Example OTcl script to simulate encryption:

# Encrypt the message before sending it

proc encrypt_message {msg} {

# Simple encryption logic (e.g., reversing the string)

return [string reverse $msg]

}

# Decrypt the received message

proc decrypt_message {encrypted_msg} {

# Simple decryption logic (reverse the string again)

return [string reverse $encrypted_msg]

}

# Send encrypted message from node0

$ns at 2.0 “$node0 send_encrypted_message $udp”

proc send_encrypted_message {udp} {

set message “Hello, secure world!”

set encrypted_message [encrypt_message $message]

# Simulate sending the encrypted message

$udp send $encrypted_message

}

# Receive the encrypted message and decrypt it at node1

proc receive_message {null} {

set received_message [get_packet_data $null]

set decrypted_message [decrypt_message $received_message]

puts “Decrypted message at node1: $decrypted_message”

}

# Bind receive_message function to node1’s agent

$ns at 2.5 “$null recv $node1”

  • In this sample, a basic encryption scheme (reversing the string) is applied before delivering the data. The receiver decrypts the message by reversing the string back to its actual form.
  1. Simulate Authentication:
  • Authentication can be mimicked by making certain that a node certifies the identity of another node before accepting data. In NS2, you can simulate this by embedding validation tokens or authorizations in the packet headers.

Example OTcl script for authentication:

# Authentication logic (using a simple token)

set auth_token “secure_token”

# Node0 sends data with an authentication token

proc send_authenticated_message {udp} {

set message “Hello, authenticate me!”

set token [authenticate_node “node0”]

set authenticated_message “$message:$token”

$udp send $authenticated_message

}

# Node1 verifies the token before accepting the message

proc verify_authentication {msg} {

set parts [split $msg “:”]

set message [lindex $parts 0]

set token [lindex $parts 1]

if {$token == $auth_token} {

return 1  ;# Authenticated

} else {

return 0  ;# Not authenticated

}

}

# Node1 receives the message and checks authentication

proc receive_authenticated_message {null} {

set received_message [get_packet_data $null]

if {[verify_authentication $received_message]} {

puts “Authentication successful, message received: $received_message”

} else {

puts “Authentication failed, message discarded.”

}

}

# Bind the authenticated receive function to node1

$ns at 3.0 “$null recv_authenticated $node1”

  • Here, the sender attaches a simple authentication token to the message. The receiver verifies the token before processing the message.
  1. Simulate Intrusion Detection System (IDS):
  • An Intrusion Detection System (IDS) can be simulated by observing traffic patterns or packet content to identify mischievous activity like unauthorized access or attacks.

Example of an OTcl script to simulate an IDS:

# Simple IDS that detects unusual traffic patterns (e.g., too many packets in a short time)

set packet_threshold 10

set time_window 1.0   ;# 1 second time window for detection

proc ids_monitor_traffic {node} {

global packet_count

set packet_count 0

$node trace-traffic

}

proc trace-traffic {node} {

# Increment packet count for the node

global packet_count

incr packet_count

# Check if the packet count exceeds the threshold

if {$packet_count > $packet_threshold} {

puts “Intrusion detected at $node: Too many packets in a short time!”

}

# Reset the count after the time window

$ns at [expr [clock seconds] + $time_window] “reset_packet_count”

}

proc reset_packet_count {} {

global packet_count

set packet_count 0

}

# Start the IDS at node0

$ns at 2.0 “ids_monitor_traffic $node0”

  • This script imitates an IDS that identifies traffic anomalies like exceeding a predefined packet rate. If the threshold is exceeded, it flags the event as a capable intrusion.
  1. Simulate Security Attacks (e.g., DoS, MITM, Sniffing):
  • Examine the resilience of the network by simulating different attacks like Denial of Service (DoS), Man-in-the-Middle (MITM), or sniffing attacks.

Denial of Service (DoS) Attack:

# Simulate a DoS attack by sending a flood of traffic from node2 to node0

set udp_attack [new Agent/UDP]

$ns attach-agent $node2 $udp_attack

$ns connect $udp_attack $null

set cbr_attack [new Application/Traffic/CBR]

$cbr_attack attach-agent $udp_attack

$cbr_attack set packetSize_ 512

$cbr_attack set rate_ 5Mb  ;# High rate to simulate a flood

# Start the DoS attack at 3.0 seconds

$ns at 3.0 “$cbr_attack start”

$ns at 5.0 “$cbr_attack stop”

  • This attack floods the network with traffic, mimicking a DoS attack on node0.

Man-in-the-Middle (MITM) Attack:

# Simulate a MITM attack by intercepting and altering traffic at node2

proc intercept_traffic {pkt} {

set hdr [Packet::gethdr $pkt]

set src [$hdr src_]

set dst [$hdr dst_]

if { $src == $node0 && $dst == $node1 } {

# Modify the packet content

set payload [Packet::getdata $pkt]

set new_payload “Modified Data”

Packet::setdata $pkt $new_payload

# Forward the modified packet to node1

$node2 send $pkt $node1

}

}

# Attach the interceptor to node2

$ns at 2.0 “attach_interceptor $node2 intercept_traffic”

  • This imitates a MITM attack where the attacker intercepts and adjusts the packet content before dispatching it.
  1. Run the Simulation and Collect Results:
  • After executing encryption, authentication, IDS, or attacks, execute the simulation and gather the results from the created trace file.

ns your_script.tcl

  1. Analyze the Results:
  • Use the trace file or network records to evaluate the activities of the network under security mechanisms or attacks. You can compute metrics like packet loss, throughput, or latency during attacks or see the success of validation and IDS detections.

Example of using AWK to analyze packet flow:

awk ‘{if ($1 == “r” && $4 == “UDP”) print “Packet received at:”, $3}’ out.tr

  • This script assists observe when and where packets are obtained, helping identify potential attack patterns.
  1. Visualize the Security Events in NAM:
  • NAM (Network Animator) is used to visualize the network activities as well as attacks and how the network acts under security conditions.

nam out.nam

  • NAM will display the flow of packets, and you can observe how traffic is affected by attacks or security measures.

Summary:

To implement Network Security Information in NS2, you:

  1. Configure a network topology with nodes and links.
  2. Apply encryption and authentication to secure data communication.
  3. Simulate Intrusion Detection Systems (IDS) to see doubtful activity or attacks.
  4. Simulate attacks like DoS, MITM, or sniffing to examine network resilience.
  5. Run the simulation and analyze trace files to analyze the performance of security protocols and the network’s weaknesses to attacks.
  6. Visualize the security events in NAM for a better understanding of the network’s activities under security situations.

In this manual, we had clearly concentrated on how to implement the Network Security Information by creating a simplified topology to establish the security mechanisms like encryption and authentication to guard the network from the potential attacks. For further development, we will deliver any details for you.

For any types of implementation you can share with us all the details by mail we will guide you with best outcomes.