Skip to content

Instantly share code, notes, and snippets.

@stewartpark
Last active June 12, 2018 19:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stewartpark/db0f8d33eeedd259d0e4d385e577d9be to your computer and use it in GitHub Desktop.
Save stewartpark/db0f8d33eeedd259d0e4d385e577d9be to your computer and use it in GitHub Desktop.
Play with a running ruby process safely
#!/usr/bin/env bash
#
# ./reval "pid" "ruby code"
rm /tmp/ruby.out 2> /dev/null
expr=$(echo $2 | base64)
(
echo "set breakpoint pending on";
echo "tb rb_funcallv"
echo "commands"
echo "call (void*)rb_eval_string_protect(\"require'base64';a=eval(Base64.decode64('$expr'));File.write('/tmp/ruby.out',a.to_s)\", (void*)0)"
echo "d"
echo "end"
echo "c"
) | gdb -p $1 &> /dev/null
if [ -f /tmp/ruby.out ]; then
cat /tmp/ruby.out
else
echo "Execution failed"
fi
root@staging-01:/tmp/test# vi reval
root@staging-01:/tmp/test# ./reval 349 "Octopus::Proxy.nil?"
false
root@staging-01:/tmp/test# cat reval
#!/usr/bin/env bash
#
# ./reval "pid" "ruby code"
rm /tmp/ruby.out 2> /dev/null
expr=$(echo $2 | base64)
(
echo "set breakpoint pending on";
echo "tb rb_funcallv"
echo "commands"
echo "call (void*)rb_eval_string_protect(\"require'base64';a=eval(Base64.decode64('$expr'));File.write('/tmp/ruby.out',a.to_s)\", (void*)0)"
echo "d"
echo "end"
echo "c"
) | gdb -p $1 &> /dev/null
if [ -f /tmp/ruby.out ]; then
cat /tmp/ruby.out
echo ""
else
echo "Execution failed"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment