Saturday, 9 October 2021

Capturing traffic of two hosts in Mininet with Python

I currently have two hosts which run a client and server Python program which send TLS traffic to one another - I have tested this outside of mininet to confirm it works (and it does!).

However, the goal here is to use tcpdump/tshark/wireshark to capture the TLS traffic between these two hosts. I have tried things such as using quietRun or subprocess.Popen to call tcpdump -i any -w capture.pcap however these do not seem to capture the traffic for my hosts, or they stall until I ctrl+c and/or go straight to the CLI(net).

For reference; this is all using mininet CLI - the aim is to do this programmatically

Below is the current code:

#!/usr/bin/python
# -*- coding: utf-8 -*-

# from sys import exit  # pylint: disable=redefined-builtin
import sys
import os
import subprocess
import time
from functools import partial

from mininet.node import Host, UserSwitch, OVSKernelSwitch, Controller, Switch
from mininet.topo import Topo, SingleSwitchTopo
from mininet.util import quietRun, pmonitor
from mininet.log import error, lg, info, setLogLevel
from mininet.net import Mininet
from mininet.cli import CLI
from mininet.link import TCLink


class ExperimentTopology(Topo):

    """Custom mininet topology for robot-controller experiments"""

    def __init__(self):
        """Create custom topology"""

        # Initialize topology

        Topo.__init__(self)

        # Add hosts and switches
        switch = self.addSwitch("s1")
        h1= self.addHost("h1")
        h2= self.addHost("h2")

        # Set link parameters (delay, etc.)
        # bw = Bandwidth in Mbps
        # delay = Link delay (s, ms, us)
        # loss = Percentage packet loss
        # max_queue_size = Maximum queue size
        # use_htb = Use the Hierarchical Token Bucket rate limiter and netem delay/loss emulator?
        # linkopts = dict(bw=10, delay="5ms", loss=10) #max_queue_size=1000

        # Add links
        self.addLink(switch, h1)  # to use params, add ", **linkopts"
        self.addLink(switch, h2)


def main():
    lg.setLogLevel("info")

    # quietRun('tcpdump -i any -w capture.pcap')

    net = Mininet(topo=ExperimentTopology(), waitConnected=True)
    net.start()

    h1= net.get('h1')
    h1p= robot.popen('python3 tls_server.py')

    # time.sleep(10)

    h2 = net.get('h2')
    h2.cmd('python3 tls_client.py'))

    # net.popen('tcpdump -i any -w capture.pcap') # _process = subprocess.Popen(['sudo', 'tcpdump', '-i', 'any', '-w', 'capture.pcap'])

    s1 = net.get('s1')
    s1.cmd(os.system('sudo tshark -w $HOME/captures/capture.pcap'))

    CLI(net)
    h1p.terminate()
    net.stop()
    # _process.terminate()

if __name__ == '__main__':
    main()


from Capturing traffic of two hosts in Mininet with Python

No comments:

Post a Comment