13

I've been following walkthroughs for various exploit exercises for linux applications. I understand the content of these well enough, however some refer to using cat to "keep the pipe open", e.g. here.

For example:

(python2 exploit.py ; cat) | ./programToExploit

What does "keeping the pipe open" actually mean in practice, and how is the pipe being kept open as with the command above?

1
  • I've always wondered this, too.
    – wogsland
    Mar 1, 2017 at 19:06

1 Answer 1

15

See the difference between

echo 'ls' | sh -s

and

( echo 'ls'; cat ) | sh -s

or,

{ echo 'ls'; cat; } | sh -s

Here, sh -s will start a shell session that reads commands to execute from standard input (-s).

In the first example, the shell will execute ls and exit (since there's no more input).

In the second and third examples, the shell will execute ls and wait until there's no more input. The input stream to sh -s was first generated by the echo command, but then cat took over. The cat process will read from the parent shell session and pass commands through the pipe to sh -s. If you type ls or any other commands (there will be no prompt to type it at), the shell will execute those commands.

This will continue until you terminate the cat process by pressing Ctrl+D on an empty line to signal the end of input.

I believe that is what you mean by "keeping the pipe open".

7
  • Yes, I tried it and it worked as the author(s) described, I mostly just wanted to understand why it worked and why the subshell was necessary. Your answer covers this, so thanks. Mar 1, 2017 at 18:40
  • 1
    This is a cool little trick!
    – wogsland
    Mar 1, 2017 at 19:07
  • this works but not via cron, im wondering how i can get around that Apr 21, 2023 at 4:48
  • @HaydenThring If you can show what you mean by it not working in cron, and also show what you want to do, you could open a brand new question and refer to this question in it.
    – Kusalananda
    Apr 21, 2023 at 5:58
  • It launching vlc in console rc mode, and piping command too it when starts, without cat a EOF or something is sent and the console interprets that as quit, and closes the app. with cat works good, but not through cron, im guessing when cron closes after launching its sending a similar command ? its fine though im going to try a different approach with screen or supervisor Apr 21, 2023 at 20:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .