To implement the Artificial Neural Network (ANN) based steganography in ns3, with the help of ANN techniques nodes can embed and extract hidden information inside the data packets by creating a simulated network. We have to external libraries for machine learning and combine them into the simulation because ns3 have any in-built support for neither ANNs nor steganography.
Here, we provide step-by-step guide to help you implement ANN-based steganography in ns3.
Step-by-Step Implementation:
Step 1: Set Up ns3 Environment
- Install ns3: Make certain that ns3 is installed. Follow the installation guide suitable for your operating system.
- Familiarize Yourself with ns3: We can also use the ns3 tutorial for understanding its basic concepts and simulation structure.
Step 2: Set Up Machine Learning Library
- Install a Machine Learning Library: Execute the ANN-based steganography by using TensorFlow or PyTorch libraries. For minimalism, let’s assume we use TensorFlow.
- Install TensorFlow: We can install the TensorFlow using the below instruction. For instance:
pip install tensorflow
Step 3: Define the Network Topology
- Create a Simple Network: Use ns3, to define a basic network that includes creating nodes, setting up channels, and configuring IP addresses. We’ll use a point-to-point topology for simplicity.
#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 <iostream>
#include <sstream>
#include <fstream>
using namespace ns3;
// Function to call a Python script for ANN-based steganography
std::string CallPythonScript(const std::string &input, const std::string &script) {
std::string command = “python3 ” + script + ” \”” + input + “\””;
std::array<char, 128> buffer;
std::string result;
std::shared_ptr<FILE> pipe(popen(command.c_str(), “r”), pclose);
if (!pipe) throw std::runtime_error(“popen() failed!”);
while (fgets(buffer.data(), 128, pipe.get()) != nullptr) {
result += buffer.data();
}
return result;
}
class ANNStegoApp : public Application {
public:
ANNStegoApp() {}
virtual ~ANNStegoApp() {}
void Setup(Address address, uint16_t port) {
m_peerAddress = address;
m_peerPort = port;
}
private:
virtual void StartApplication() {
m_socket = Socket::CreateSocket(GetNode(), TypeId::LookupByName(“ns3::UdpSocketFactory”));
m_socket->Bind();
m_socket->Connect(InetSocketAddress(m_peerAddress, m_peerPort));
// Schedule the first packet transmission
Simulator::Schedule(Seconds(1.0), &ANNStegoApp::SendPacket, this);
// Set up the receive callback
m_socket->SetRecvCallback(MakeCallback(&ANNStegoApp::ReceivePacket, this));
}
virtual void StopApplication() {
if (m_socket) {
m_socket->Close();
m_socket = 0;
}
}
void SendPacket() {
std::string message = “Hello, this is a secure message!”;
std::string hiddenMessage = “Secret”;
std::string input = message + “:” + hiddenMessage;
// Call the Python script to embed the hidden message
std::string stegoMessage = CallPythonScript(input, “embed_stego.py”);
Ptr<Packet> packet = Create<Packet>((uint8_t *)stegoMessage.c_str(), stegoMessage.size());
m_socket->Send(packet);
// Schedule the next packet transmission
Simulator::Schedule(Seconds(1.0), &ANNStegoApp::SendPacket, this);
}
void ReceivePacket(Ptr<Socket> socket) {
Ptr<Packet> packet = socket->Recv();
uint8_t buffer[1024];
packet->CopyData(buffer, packet->GetSize());
std::string stegoMessage((char *)buffer, packet->GetSize());
// Call the Python script to extract the hidden message
std::string extractedMessage = CallPythonScript(stegoMessage, “extract_stego.py”);
std::cout << “Received message with hidden data: ” << extractedMessage << std::endl;
}
Ptr<Socket> m_socket;
Address m_peerAddress;
uint16_t m_peerPort;
};
int main(int argc, char *argv[]) {
NodeContainer nodes;
nodes.Create(2); // Example: 2 nodes (1 client, 1 server)
PointToPointHelper pointToPoint;
pointToPoint.SetDeviceAttribute(“DataRate”, StringValue(“1Gbps”));
pointToPoint.SetChannelAttribute(“Delay”, StringValue(“2ms”));
NetDeviceContainer devices;
devices = pointToPoint.Install(nodes);
InternetStackHelper stack;
stack.Install(nodes);
Ipv4AddressHelper address;
address.SetBase(“10.1.1.0”, “255.255.255.0”);
Ipv4InterfaceContainer interfaces = address.Assign(devices);
uint16_t port = 9;
Ptr<ANNStegoApp> clientApp = CreateObject<ANNStegoApp>();
clientApp->Setup(InetSocketAddress(interfaces.GetAddress(1), port), port);
nodes.Get(0)->AddApplication(clientApp);
clientApp->SetStartTime(Seconds(2.0));
clientApp->SetStopTime(Seconds(10.0));
Ptr<ANNStegoApp> serverApp = CreateObject<ANNStegoApp>();
serverApp->Setup(InetSocketAddress(Ipv4Address::GetAny(), port), port);
nodes.Get(1)->AddApplication(serverApp);
serverApp->SetStartTime(Seconds(1.0));
serverApp->SetStopTime(Seconds(10.0));
Simulator::Run();
Simulator::Destroy();
return 0;
}
Explanation:
- ANN-Based Steganography Python Scripts:
- Using ANN, these python scripts called embed_stego.py and extract_stego.py handles the embedding and extraction of hidden messages.
- Ensure these scripts are in the same directory as the ns3 simulation code.
- ANNStegoApp Class:
- ANNStegoApp class handles the application logic, including sending and receiving steganographic messages.
- Initialize the application using the Setup method with peer address and port.
- StartApplication method sets up the socket connection, schedules the first packet transmission, and sets up the receive callback.
- To embed the hidden message inside the payload , we can use SendPacket method that calls the embed_stego.py Python script.
- To extract the hidden message from the received payload, we can use ReceivePacket method which calls the extract_stego.py Python script.
- Main Function:
- Creates a simple point-to-point network with 2 nodes (client and server).
- Initializes the ANNStegoApp applications on both client and server nodes.
- The client sends steganographic messages to the server, and the server extracts and prints the hidden messages.
ANN-Based Steganography Python Scripts
embed_stego.py
import sys
import tensorflow as tf
from tensorflow.keras.models import load_model
def embed_message(message, hidden_message):
# Load your pre-trained ANN model for embedding
model = load_model(’embed_model.h5′)
# Convert messages to suitable format (e.g., binary, image, etc.)
# This is just an example, adjust according to your model’s requirements
input_data = [ord(c) for c in message]
hidden_data = [ord(c) for c in hidden_message]
# Run the embedding process
stego_data = model.predict([input_data, hidden_data])
# Convert the stego_data back to a string format
stego_message = ”.join(chr(int(c)) for c in stego_data[0])
return stego_message
if __name__ == “__main__”:
input_data = sys.argv[1].split(‘:’)
message = input_data[0]
hidden_message = input_data[1]
stego_message = embed_message(message, hidden_message)
print(stego_message)
extract_stego.py
import sys
import tensorflow as tf
from tensorflow.keras.models import load_model
def extract_message(stego_message):
# Load your pre-trained ANN model for extraction
model = load_model(‘extract_model.h5’)
# Convert stego_message to suitable format (e.g., binary, image, etc.)
# This is just an example, adjust according to your model’s requirements
stego_data = [ord(c) for c in stego_message]
# Run the extraction process
extracted_data = model.predict([stego_data])
# Convert the extracted_data back to a string format
hidden_message = ”.join(chr(int(c)) for c in extracted_data[0])
return hidden_message
if __name__ == “__main__”:
stego_message = sys.argv[1]
hidden_message = extract_message(stego_message)
print(hidden_message)
Compile and Run:
- Compile the Code: Compile the ns3 simulation code using the following command:
g++ -std=c++11 -o ns3-ann-stego main.cc `pkg-config –cflags –libs ns3-dev`
- Run the Simulation: Execute the compiled program:
./ns3-ann-stego
This setup demonstrates a simple implementation of ANN-based steganography in ns3 using external Python scripts for the ANN processing. You can expand it further to include more nodes, complex topologies, and additional cryptographic functionalities as needed. Make sure to have trained ANN models saved as embed_model.h5 and extract_model.h5 in the working directory.
Finally, we entirely provide the information on how to implement the Artificial Neural Network (ANN)-based steganography in the ns3 tool with the help of python scripts. If you need any other implemenaion details relevant to this topic, we can offer them.