Skip to content

SC-MLIDS: A Hybrid Machine Learning Intrusion Detection System Framework with Integrated Server and Client Models for Wireless Sensor Networks

License

Notifications You must be signed in to change notification settings

Hongwei-Z/SC-MLIDS

Repository files navigation

SC-MLIDS: A Hybrid Machine Learning Intrusion Detection Scheme with Integrated Server and Client Models for Wireless Sensor Networks

SC-MLIDS (Server-Client Machine Learning Intrusion Detection Scheme)

Author: Hongwei Zhang

Dalhousie University

File Description:

  1. Server and client files:
    1. helper.py: Process the dataset, output model metrics, and generate encryption keys.
    2. server.py: Receive data from clients, decrypt, decompress, save, and train network traffic model.
    3. client1.py: Train local sensor model, send data to server, encrypt, compress.
    4. client2.py: Same as above.
    5. client3.py: Same as above.
  2. Model aggregated prediction files:
    1. aggregated_predict.py: Includes two aggregated model prediction methods, and one model testing method.
    2. Demo_Aggregated_Prediction_1.ipynb: A demonstration of the methods in the above file.
    3. Demo_Aggregated_Prediction_2.ipynb: Demonstration of aggregated prediction methods using different classifiers.
  3. Other files:
    1. Preprocessing.ipynb: A demonstration of the dataset preprocessing process.
    2. merged_data.csv: The new dataset generated by the above file.
    3. requirements.txt: pip install -r requirements.txt

Code Execution Instruction:

  1. Start the server and clients, train the models, transfer the models, and save the models. Open two terminals, one for the server and another for the clients.

    1. In the first terminal, load the helper and start the server:

      python helper.py
      python server.py
    2. In the second terminal, run each of the three clients:

      # Note: Run the next client only after seeing the previous client's task completed in the server terminal.
      python client1.py
      python client2.py
      python client3.py
  2. Test the models using the model aggregated prediction methods in file aggregated_predict.py. Run file Demo_Aggregated_Prediction_1.ipynb and Demo_Aggregated_Prediction_2.ipynb. This step should be integrated into the server in the WSN simulation, it is separated here for ease of demonstration.

Code Execution Result:

  1. Server running result (first terminal):

    C:\Users>python helper.py
    C:\Users>python server.py
    Server is waiting for connections ...
    --------------------------------------------------------------------------------------------------------------
    Connected to Client 1:
    Address: ('127.0.0.1', 54742)
    
    Client 1 model decryption information:
    Key: b'ZJg3BjY6SI0hL9h8IYYf9GxsUR5aqtd6ntQU5wXpjTs='
    Seed: 0.60792627
    Salt: b'\x03\x02\x8a\xcd\xe5\xd2\xdf\xfa\x00q\xc3\xee~\x85\xef\xdf'
    
    Receiving Client 1 model file: 100%|██████████████████████████████████████| 108M/108M [01:20<00:00, 1.41MB/s]
    Client 1 model file has been received, time spent: 80.2155 seconds
    Client 1 model file size: 107.5145 MB
    Client 1 model file has been decompressed and decrypted, time spent: 1.1003 seconds
    Client 1 model file has been saved to: ./received_models/client_1.joblib
    --------------------------------------------- Client 1 Completed ---------------------------------------------
    Connected to Client 2:
    Address: ('127.0.0.1', 54814)
    
    Client 2 model decryption information:
    Key: b'D39wlizQSuynNVIslW_DzbkqkvO__AjdaQt5x_A9jmQ='
    Seed: 0.60615668
    Salt: b'\xca\xb9\x92\x11>\xe1\x97\xc1\xf6\x13I\x04NQ{"'
    
    Receiving Client 2 model file: 100%|██████████████████████████████████████| 108M/108M [01:04<00:00, 1.74MB/s]
    Client 2 model file has been received, time spent: 64.8709 seconds
    Client 2 model file size: 107.7409 MB
    Client 2 model file has been decompressed and decrypted, time spent: 0.8955 seconds
    Client 2 model file has been saved to: ./received_models/client_2.joblib
    --------------------------------------------- Client 2 Completed ---------------------------------------------
    Connected to Client 3:
    Address: ('127.0.0.1', 54830)
    
    Client 3 model decryption information:
    Key: b'yXP5THp4LPQSTGB_-xy8_x0H8HzaGJoUlzHZ6KfVWoY='
    Seed: 0.60702304
    Salt: b'\xbc\xd9xMwO\x87d\x91\x9e\xb5\xa8\xb9\xa8z\xd6'
    
    Receiving Client 3 model file: 100%|██████████████████████████████████████| 107M/107M [01:16<00:00, 1.48MB/s]
    Client 3 model file has been received, time spent: 76.1133 seconds
    Client 3 model file size: 107.3119 MB
    Client 3 model file has been decompressed and decrypted, time spent: 1.1386 seconds
    Client 3 model file has been saved to: ./received_models/client_3.joblib
    --------------------------------------------- Client 3 Completed ---------------------------------------------
    --------------------------------------- All Clients Have Been Processed --------------------------------------
    Training the global model using network traffic data ...
    Target distribution: {0: 98809, 1: 63943}
    
    Global model training completed in 6.8305 seconds.
    Global model saved to: ./received_models/global_model.joblib
    -------------------------------------------------- All Done --------------------------------------------------
  2. Clients running result (second terminal):

    C:\Users>python client1.py
    Client 1:
    
    Target distribution: {0: 32980, 1: 21270}
    
    Client 1 model training completed in 8.9069 seconds.
    Client 1 model saved to: ./client_models/client_1.joblib
    
    Client 1 model encryption information:
    Key: b'ZJg3BjY6SI0hL9h8IYYf9GxsUR5aqtd6ntQU5wXpjTs='
    Seed: 0.60792627
    Salt: b'\x03\x02\x8a\xcd\xe5\xd2\xdf\xfa\x00q\xc3\xee~\x85\xef\xdf'
    
    Model file size: 106.4457 MB
    Model file encrypted, file size: 141.9277 MB, time spent: 0.7266 seconds
    Model file compressed, file size: 107.5145 MB, time spent: 5.2522 seconds
    
    Client 1 has connected to the server
    Model file has been sent from Client 1, file size: 107.5145 MB, time spent: 0.0100 seconds
    ----------------------------------- Client 1 Completed -----------------------------------
    
    C:\Users>python client2.py
    Client 2:
    
    Target distribution: {0: 32884, 1: 21366}
    
    Client 2 model training completed in 8.3103 seconds.
    Client 2 model saved to: ./client_models/client_2.joblib
    
    Client 2 model encryption information:
    Key: b'D39wlizQSuynNVIslW_DzbkqkvO__AjdaQt5x_A9jmQ='
    Seed: 0.60615668
    Salt: b'\xca\xb9\x92\x11>\xe1\x97\xc1\xf6\x13I\x04NQ{"'
    
    Model file size: 106.6699 MB
    Model file encrypted, file size: 142.2266 MB, time spent: 0.5947 seconds
    Model file compressed, file size: 107.7409 MB, time spent: 3.8984 seconds
    
    Client 2 has connected to the server
    Model file has been sent from Client 2, file size: 107.7409 MB, time spent: 0.0080 seconds
    ----------------------------------- Client 2 Completed -----------------------------------
    
    C:\Users>python client3.py
    Client 3:
    
    Target distribution: {0: 32931, 1: 21319}
    
    Client 3 model training completed in 8.1050 seconds.
    Client 3 model saved to: ./client_models/client_3.joblib
    
    Client 3 model encryption information:
    Key: b'yXP5THp4LPQSTGB_-xy8_x0H8HzaGJoUlzHZ6KfVWoY='
    Seed: 0.60702304
    Salt: b'\xbc\xd9xMwO\x87d\x91\x9e\xb5\xa8\xb9\xa8z\xd6'
    
    Model file size: 106.2449 MB
    Model file encrypted, file size: 141.6600 MB, time spent: 0.5654 seconds
    Model file compressed, file size: 107.3119 MB, time spent: 3.8393 seconds
    
    Client 3 has connected to the server
    Model file has been sent from Client 3, file size: 107.3119 MB, time spent: 0.0080 seconds
    ----------------------------------- Client 3 Completed -----------------------------------
  3. Models aggregated prediction result:

    # See 'Demo_Aggregated_Prediction_1.ipynb' and 'Demo_Aggregated_Prediction_2.ipynb'.

Citations:

  1. The TON_IoT Datasets
  2. scikit-learn: RandomForestClassifier
  3. Real Python: Socket Programming in Python
  4. Stack Overflow: Send big file over socket
  5. Cryptography: Fernet

About

SC-MLIDS: A Hybrid Machine Learning Intrusion Detection System Framework with Integrated Server and Client Models for Wireless Sensor Networks

Topics

Resources

License

Stars

Watchers

Forks