mininet-sec / mininet-sec

Emulation platform for studying and experimenting cybersecurity tools in programmable networks
GNU General Public License v3.0
2 stars 0 forks source link

Open Terminal on Dash does not work if mininet-sec console is being used #1

Open italovalcy opened 3 weeks ago

italovalcy commented 3 weeks ago

Hi,

If you leave a command running on mininet-sec console, let's say a ping from a host to another, and then try to open the Terminal on the WebUI, you will get the following error:

Welcome to Mininet-sec @ h1

You can copy with ctrl+shift+c or ctrl+shift+x
You can paste with ctrl+shift+v
You can clear with ctrl+l

message handler error
Traceback (most recent call last):
  File "/usr/local/lib/python3.11/dist-packages/engineio/server.py", line 434, in run_handler
    return self.handlers[event](*args)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/dist-packages/socketio/server.py", line 643, in _handle_eio_message
    self._handle_connect(eio_sid, pkt.namespace, pkt.data)
  File "/usr/local/lib/python3.11/dist-packages/socketio/server.py", line 537, in _handle_connect
    success = self._trigger_event(
              ^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/dist-packages/socketio/server.py", line 614, in _trigger_event
    return handler(*args)
           ^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/dist-packages/flask_socketio/__init__.py", line 282, in _handler
    return self._handle_event(handler, message, namespace, sid,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/dist-packages/flask_socketio/__init__.py", line 823, in _handle_event
    ret = handler(auth)
          ^^^^^^^^^^^^^
  File "/src/mnsec/mnsec/api_server.py", line 362, in pty_connect
    homeDir = self.mnsec.setupHostHomeDir(host)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/src/mnsec/mnsec/net.py", line 107, in setupHostHomeDir
    host.cmd(f"mkdir -p {homeDir}")
  File "/usr/lib/python3/dist-packages/mininet/node.py", line 386, in cmd
    self.sendCmd( *args, **kwargs )
  File "/usr/lib/python3/dist-packages/mininet/node.py", line 303, in sendCmd
    assert self.shell and not self.waiting
AssertionError
italovalcy commented 3 weeks ago

Maybe it has to do with some recent changes I made on the xterm function, which now leverages Node.popen instead of calling mnexec directly:

diff --git a/mnsec/api_server.py b/mnsec/api_server.py
index b9c377d..9afdb04 100644
--- a/mnsec/api_server.py
+++ b/mnsec/api_server.py
@@ -360,13 +360,22 @@ class APIServer:
                 # of this subprocess
                 host_pid = self.mnsec[host].pid
                 homeDir = self.mnsec.setupHostHomeDir(host)
-                myenv = {"PS1": f"\\u@{host}:\\W\\$ ", "HOME": homeDir}
+                myenv = dict(os.environ)
+                myenv.update({"PS1": f"\\u@{host}:\\W\\$ ", "HOME": homeDir})
                 # workaround to avoid bash overridding PS1
                 myenv["SUDO_USER"] = "root"
                 myenv["SUDO_PS1"] = "# "
-                subprocess.run(['mnexec', '-a', str(host_pid), "bash"], env=myenv, cwd=homeDir)
+                #subprocess.run(['mnexec', '-a', str(host_pid), "bash"], env=myenv, cwd=homeDir)
+                with self.mnsec[host].popen(
+                    "bash", env=myenv, cwd=homeDir, stdout=None, stderr=None,
+                ) as process:
+                    try:
+                        stdout, stderr = process.communicate()
+                    except:
+                        process.kill()
                 # after finish make sure we cleanup
                 self.xterm_conns.pop(host, None)
             else:
                 # this is the parent process fork.
                 # store child fd and pid