Created
October 20, 2009 21:45
-
-
Save kentfredric/214645 to your computer and use it in GitHub Desktop.
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/perl | |
use strict; | |
use warnings; | |
use POSIX qw( dup pipe ); | |
use IO::File; | |
use IO::Handle; | |
use Data::Dump qw( dump ); | |
sub openstream { | |
my ( $fhx, @rest ) = @_; | |
# Copy STDOUT and STDERR for resurrection. | |
my $oldstdout = dup( fileno(*STDOUT) ); | |
my $oldstderr = dup( fileno(*STDERR) ); | |
# Generate a pipe to accept data from child process. | |
my ( $response_r, $response_w ) = POSIX::pipe(); | |
# Put Perl handles on the pipe. | |
my $responder = IO::Handle->new->fdopen( $response_r, 'r' ); | |
my $writer = IO::Handle->new->fdopen( $response_w, 'w' ); | |
my $tstdout = IO::Handle->new->fdopen( $oldstdout, 'w' ); | |
$responder->autoflush(1); | |
$writer->autoflush(1); | |
$tstdout->autoflush(1); | |
# Return the handle to the user. | |
$_[0] = $responder; | |
# Reopen STDERR and STDOUT to point to the pipe. | |
open *STDERR, '>>&=', $response_w; | |
open *STDOUT, '>>&=', $response_w; | |
# Run the users app with the new stuff. | |
{ | |
if( not my $pid = fork() ){ | |
select *STDERR; $|++; | |
select *STDOUT; $|++; | |
system @rest; | |
exit; | |
} | |
} | |
open *STDERR, '>>&=', $oldstderr; | |
open *STDOUT, '>>&=', $oldstdout; | |
} | |
openstream my $fh, qw( perl evil.pl ); | |
while ( my $line = <$fh> ) { | |
print dump $line; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment