Created
July 6, 2023 19:29
-
-
Save moreati/281db7b3e01cb63231e710e2178912c3 to your computer and use it in GitHub Desktop.
Wrap an invocation of sudo, saving the input bytes including any passwords
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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) |
Author
moreati
commented
Jul 6, 2023
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment