Giter VIP home page Giter VIP logo

wsl-sudo's Introduction

Sudo for WSL

What is this?

This tool allows you to run applications in windows elevated user mode from a non-elevated wsl shell.

It has full terminal support, so you can run interactive applications like vim or a shell through it.

Requirements

wsl-sudo requires Python >= 3.5. It should be preinstalled on any modern linux distribution.

How to setup

Clone this repository or just download wsl-sudo.py.

You can test if the script works by running python3 wsl-sudo.py net.exe sessions and comparing the output with just running net.exe sessions.

For convenience, you might want to add an alias to this script, eg:

alias wudo="python3 /path-to-wsl-sudo/wsl-sudo.py"

Usage examples

$ wudo vim /mnt/c/Windows/System32/Drivers/etc/hosts
$ wudo cp foo.txt /mnt/c/Program Files/
$ wudo cmd  # open elevated standard command prompt
$ wudo bash  # open elevated shell
$ wudo regedit

Note that it will open an UAC prompt every time it is run, so if you want to run multiple commands in succession, you should open an elevated shell (see example above) and run your commands from there

How it works

When run, wsl-sudo uses powershell to run a second process in elevated mode. For security reasons, Windows prevents most kinds of communication between elevated and non-elevated processes. So, the elevated process connects to the non-elevated process via TCP for communication. To prevent other processes from interfering with this connection, it's secured with a random password.

The elevated process will then run the given command and exchange input and output with the original process via the TCP connection. The command is run in a pty, so it acts as if running in an ordinary terminal. Therefore, you can run interactive console-based programs like vim or less.

Related Projects

wsl-sudo's People

Contributors

chronial avatar dtmdl avatar nu774 avatar weinhold avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

wsl-sudo's Issues

WSL2? Permissions? Can't find Powershell? I'm not sure what the problem is!

Hi! This was working brilliantly for me when I was using WSL1, but I'm trying it in WSL2 and I'm having problems. I know there was a revision for WSL2, but I have that revision in repo I cloned.

If I run

python3 wsl-sudo.py net.exe sessions

I get

Traceback (most recent call last):
  File "wsl-sudo.py", line 335, in <module>
    main()
  File "wsl-sudo.py", line 331, in main
    UnprivilegedClient().main(**vars(args))
  File "wsl-sudo.py", line 236, in main
    self.sock, acc = listen_socket.accept()
  File "/usr/lib/python3.8/socket.py", line 292, in accept
    fd, addr = self._accept()
socket.timeout: timed out

If I run

sudo python3 wsl-sudo.py net.exe sessions

I get

[sudo] password for douglaswaugh:
Traceback (most recent call last):
  File "wsl-sudo.py", line 335, in <module>
    main()
  File "wsl-sudo.py", line 331, in main
    UnprivilegedClient().main(**vars(args))
  File "wsl-sudo.py", line 223, in main
    subprocess.check_call(
  File "/usr/lib/python3.8/subprocess.py", line 359, in check_call
    retcode = call(*popenargs, **kwargs)
  File "/usr/lib/python3.8/subprocess.py", line 340, in call
    with Popen(*popenargs, **kwargs) as p:
  File "/usr/lib/python3.8/subprocess.py", line 858, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "/usr/lib/python3.8/subprocess.py", line 1704, in _execute_child
    raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: 'powershell.exe'

But if I invoke powershell in WSL2

powershell.exe

It seems I can access it from Ubuntu, although it's not the cross platform version

Copyright (C) Microsoft Corporation. All rights reserved.

Try the new cross-platform PowerShell https://aka.ms/pscore6

PS Microsoft.PowerShell.Core\FileSystem::\\wsl$\Ubuntu-20.04\home\douglaswaugh\dev>

So it seems I have some sort of permissions issue and some sort of Powershell problem? Any ideas?

Is it supposed to work with WSL2?

I didn't dig deeply into the code, but it looks like it doesn't work,

UAC prompt pops up, but then permissions stay the same. I.e:

➜ python ./wsl-sudo.py net.exe sessions
System error 5 has occurred.

Access is denied.

➜ net.exe sessions
System error 5 has occurred.

Access is denied.

Font change to Raster Fonts if input is piped or redirected

I'm running Ubuntu 22.04 in WSL 1 using Windows 10 21H2 build 19044.1645. If input is normally taken from standard input, like ./wsl-sudo.py ls, then there is no problem. The window continues to use the default Consolas font. But if input is piped, like echo | ./wsl-sudo.py ls, or redirected, like ./wsl-sudo.py ls < wsl-sudo.py or ./wsl-sudo.py ls < /dev/null then the font of the terminal window is changed to "Raster Fonts". The same thing happens when running any program, including plain Windows programs, and attempts to run non-existent programs. The font change is seen before the UAC dialog appears. If I close the WSL window and open it again, the Consolas font is used again.

`socket.timeout: timed out` when running under sudo or otherwise as root?

Is there any way I can use both wsl-sudo.pl and sudo at the same time without having to enter my linux password every time?

As I pointed out in #1, wsl-sudo works fine to edit e.g. the hosts file under WSL2, but I also need my script to have linux sudo priviledges.

If I run

wsl-sudo.py sudo ls -l

sudo asks for a password every time, presumably because wsl-sudo.py creates a new pty for each invocation.

But if I run this (where I preserve the PATH so it can find wsl-sudo.py and misc. windows binaries):

sudo env "PATH=$PATH" wsl-sudo.py ls

I'm presented with the UAC prompt but then get this, which I'd like to avoid:

Traceback (most recent call last):
  File "/home/peter/bin/local/wsl-sudo/wsl-sudo.py", line 332, in <module>
    main()
  File "/home/peter/bin/local/wsl-sudo/wsl-sudo.py", line 328, in main
    UnprivilegedClient().main(**vars(args))
  File "/home/peter/bin/local/wsl-sudo/wsl-sudo.py", line 233, in main
    self.sock, acc = listen_socket.accept()
  File "/usr/lib/python3.8/socket.py", line 292, in accept
    fd, addr = self._accept()
socket.timeout: timed out

This occurs both if I run as above, if I run sudo -i to get an interactive shell, or run wsl -u root to start a brand new WSL session.

STDIN doesn't seem to be working in WSL2

Hi, this is an awesome script, I'm having an issue with using stdin in WSL 2 with the elevated sessions.

The following example will hang, I'm expecting an output of 123 like if I run it in a normal shell echo 123 | cat -.

echo 123 | python3 wsl-sudo.py cat -

This outputs 123 in WSL 1, but then hangs after.

Thanks for taking a look.

socket.timeout: timed out when attempting to run any command with wsl-sudo in WSL2

I'm running WSL2 Ubuntu 20.04 on Windows 11. This is a fresh WSL2 installation with barely nothing else installed.

I did a fresh checkout of the current master, which should have #2 merged in. When I run the script, using any command as argument, I get a timeout with the following stack trace:

python3 wsl-sudo.py net.exe 
Traceback (most recent call last):
  File "wsl-sudo.py", line 335, in <module>
    main()
  File "wsl-sudo.py", line 331, in main
    UnprivilegedClient().main(**vars(args))
  File "wsl-sudo.py", line 236, in main
    self.sock, acc = listen_socket.accept()
  File "/usr/lib/python3.8/socket.py", line 292, in accept
    fd, addr = self._accept()
socket.timeout: timed out

Any clues as what might be causing this?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.