sábado, 23 de mayo de 2020

Working like WSL2 (without having it)

TL;DR

Here is an approach to work with a non-WSL Linux guest (with VirtualBox or VmWare) and launching native Windows commands (f.e. MS Office Excel)

--

Hi all,

Some people have WSL2. Some people not, like me, and we are in the middle-of-nowhere with WSL1, as we are not able to do real stuff with Docker, Kubernetes, and so on, because WSL1 lacks the needed capabilities that a real Linux kernel has, so the only solution (see rants section below) is to stick with VirtualBox (or VmWare if you prefer) and work isolated from Windows apps.

But here's a way to overcome such limitations.

The idea is to run a small agent in the Windows side (the host) that will be capable of running whatever command you send from the Linux side (the guest).

In order to do that, you'll need:
  • Windows:
    • Java 11 (https://adoptopenjdk.net/?variant=openjdk11&jvmVariant=hotspot)
    • Groovy (https://dl.bintray.com/groovy/maven/apache-groovy-binary-3.0.4.zip)
  • Linux:
    • Netcat

Once you have them installed, you'll have to do:

1. Create the agent in Groovy

Create a file (f.e. c:\GroovySocketServer1070.groovy) with the following content:

new groovy.ui.GroovySocketServer (new GroovyShell(), // false"println line.execute().text", // true1070);

That's all: A poor-man's telnet server written in three lines of Groovy, that will execute whatever command it receives at port 1070!
There's a caveat: If there's an error it will be lost, as STDERR is not captured.

You can run the agent  with a command like (the paths must reflect the exact locations of the Java and Groovy installations) this:

C:\>PATH=C:\p\groovy-3.0.4\bin;C:\p\jdk-11\bin;%PATH% C:\>groovy C:\GroovySocketServer1070.groovy


2. Launch the commands from Linux

From the Linux guest you can launch any Windows program like this:

$ echo "C:\\Windows\\System32\\cmd.exe /C dir" | nc -N 10.0.2.2 1070 $ echo "C:\\Program Files (x86)\\Microsoft Office\\root\\Office16\\EXCEL.EXE" | nc -N realhost 1070 &

Bonus point: The GroovySocketServer allows multiple concurrent executions (so you don't have to wait for the termination of other commands)!

Sweet and easy: You can execute any host program from the guest.

Hope you like it!

Rants