Last active
November 3, 2021 06:24
-
-
Save kherge/092e4c28187d4cf5fd1f02d8b3b3d311 to your computer and use it in GitHub Desktop.
Using temporary pipes to run shell code from binary executables.
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
#!/bin/sh | |
### | |
# Uses the 3 pipe to run commands. | |
# | |
# In Rust: https://stackoverflow.com/a/54858159 | |
## | |
inner() | |
{ | |
echo 'echo "Hello, before!"' >&3 | |
# If we don't sleep, printf may run before shell code above. | |
sleep 0.1 | |
printf 'Name? ' | |
read -r NAME | |
echo 'echo "Hello, '"$NAME"'!"' >&3 | |
echo break >&3 | |
} | |
# Create the name for the pipe. | |
PIPE="$(mktemp -u)" | |
# Create the pipe. | |
mkfifo "$PIPE" | |
# Alias it to "3". | |
exec 3<>"$PIPE" | |
# Remove the actual file. | |
rm "$PIPE" | |
# Run a command that uses the pipe. | |
(inner 0<&0 &) | |
# Evaluate output from 3. | |
while read -r THIRD; do | |
eval $THIRD | |
done <&3 | |
# Wait for background task. | |
wait | |
# Close the pipe. | |
exec 3>&- |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment