2 ms·
[18:54:40 ~]$ irb >> [].send('exec', 'date') Tue Nov 16 18:54:48 GMT 2010 You can call Kernel's methods on any object (usually - there are some cases where
by petercooper 16y ago
[18:54:40 ~]$ irb
>> [].send('exec', 'date')
Tue Nov 16 18:54:48 GMT 2010
You can call Kernel's methods on any object (usually - there are some cases where not) through send, e.g. [].send(:puts, 10) .. I was working with Sven Fuchs on some "sandboxing" stuff a couple of years ago and we discovered this little doozy! (Though it's possible to figure out by noting that [].puts tells you private method `puts' called and send gets you around that problem.)
Typically you pass a symbol as the first argument but a string works too which, I believe, would make my attack vector work. Going to try it now.
Update: /send/exec/date works as expected (and stops the server).
- steveklabnik 16y agoWeird... ruby-1.8.7-p302 > [].respond_to? :exec => false Guess I shouldn't trust it. (Also, I originally ran Array.respond_to?...)
- petercooper 16y agoTry: [].respond_to?(:exec, true) # => true respond_to? doesn't include private methods by default but can be made to do so with the second argument. I didn't know this either till I just looked it up. This made sense to me since I'd assume respond_to? would only yield true if it were possible to make the call without error. Yet, this logic does not hold true: class X; def y; respond_to? :exec; end; end X.new.y # => FALSE So it seems it doesn't matter if the private method is callable or not, respond_to? won't give you a true for any private method, accessible in the current scope or not, unless you pass the extra flag. (I'm not really writing all of this to give you an education, more to give me one. I was totally unaware of all this and found it interesting :-))
- steveklabnik 16y agoNo, don't feel bad. That's awesome. I knew that thing about adding true, I just didn't realize that exec was private.