Last active
September 21, 2019 03:46
-
-
Save p120ph37/2bf794a86eeab0445658 to your computer and use it in GitHub Desktop.
Inject an eval into a running Perl process using GDB and temporarily capture STDERR.
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 warnings; | |
use strict; | |
use threads; | |
use File::Temp; | |
use POSIX qw/mkfifo/; | |
my $pid = shift @ARGV; | |
my $eval = shift @ARGV || 'require Carp; local $Carp::CarpLevel = 1; Carp::cluck(\'Currently\');'; | |
my $thread = $ENV{'GDB_THREAD'} || 'all'; | |
my $gdb = $ENV{'GDB'} || '/usr/bin/gdb'; | |
my $f = tmpnam(); | |
$_ = umask(0); mkfifo($f, 0666) or die $!; umask($_); | |
async { | |
open(my $p, '<', $f); | |
unlink $f; | |
print <$p>; | |
}->detach(); | |
$eval = "{open local(*STDERR), '>', '$f'; $eval}"; | |
my $g = open(my $w, '|-', "$gdb -q -n -p $pid >/dev/null 2>&1") or die; | |
syswrite $w, 'thread apply '.$thread.' call Perl_sv_free(Perl_eval_pv(Perl_get_context(),"'.quotemeta($eval)."\",0))\ndetach\nquit\n"; | |
waitpid($g, 0); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage:
./perl_inject.pl 1234 'print STDERR "Current value of \$myglobal is $myglobal\n"'
gdb
to be installed, but does not require perl to have been built with debugging symbols./dev/null
. This is accomplished via a temporary named pipe (fifo).bt
command ingdb
.)