Skip to content

Instantly share code, notes, and snippets.

@moreati
Created July 6, 2023 19:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save moreati/281db7b3e01cb63231e710e2178912c3 to your computer and use it in GitHub Desktop.
Save moreati/281db7b3e01cb63231e710e2178912c3 to your computer and use it in GitHub Desktop.
Wrap an invocation of sudo, saving the input bytes including any passwords
#!/usr/bin/env python
"""
Wrap an invocation of sudo, saving the input bytes including any passwords.
Write a repr of the input bytes to stdout, following any output from the
sudo command invocation.
"""
import io
import os
import pty
import sys
stdin_stash = io.BytesIO()
def stdin_read(fd):
data = os.read(fd, 1024)
stdin_stash.write(data)
return data
exit_status = pty.spawn(
['sudo', '--reset-timestamp', *sys.argv[1:]],
stdin_read=stdin_read,
)
sys.stdout.write(f'{stdin_stash.getvalue()!r}\n')
sys.stdout.flush()
sys.exit(exit_status)
@moreati
Copy link
Author

moreati commented Jul 6, 2023

$ ./sudowrapper.py echo foo
[sudo] password for alex: 
foo
b'1234 the same combination as my luggage\r'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment