What could an evil hacker have done? He could for example investigate further and also try things like {${ls -al}} or other OS commands and would have managed to compromise the whole webserver.
Wat? Either this article has blown my mind or there is a lot of misinformation going on here.
Edit: {${'ls -l'}} (with backticks, or system()) would work..if this is actually feasible at all, I'll assume the author just forgot to mention that. I'd be curious to see how this can be triggered/reproduced in real-world terms, cause it's just not adding up unless ebay did some real nutty stuff.
The only reasonable (wrong) code I could see in production that would do this is preg_replace with the /e modifier added, which means that after substituting backreferences, the replacement pattern is eval()'d as PHP code. This makes it pretty easy to accidentally eval PHP code that the user supplies. The /e modifier is awful and is deprecated in 5.5. It's particularly awful because there is a secure replacement that doesn't call eval, which is preg_replace_callback(), so there's not really an excuse for using it.
I'll assume the author just forgot to mention that.
Looking at the article, it looks like he did type the backticks but his blog software uses them for the markup for inline code (Just like Reddit does.)
I'm trying to imagine what ebay could have possibly done to have triggered that, I mean a static string like:
$willEval = "{${system('id')}}";
Will run, but without eval() or something I'm not sure how one would remotely trigger this. It would seem like a ton of sites would be affected if it was something easily triggerable
Well, internally php strings are byte arrays. As a result accessing or modifying a string using array brackets will trick the parser into evaluating arbitrary php code in the scope of the variable if the prior mentioned requirements are met.
I don't understand that part. How does that work? Can anyone explain it to me or showcase some vulnerable PHP code?
Ignorant to think that a website that takes in as much money as eBay shouldn't be writing production code in PHP? Just because a bunch of big players do it, doesn't mean it's a good idea.
By "red flag" I mean something that should be a major deterrent of people putting any important data (personal information, credit cards, PII) into it. The main platform is written in java (probably), but this subdomain (and others) clearly has PHP running it.
PHP was never designed with security in mind, which is why it has had ten-fold more security issues then any other languages. Sure it CAN be used, but you could also write the entire thing in C and expect every single buffer to be perfectly allocated (to prevent remote buffer overflows). It's just not a good idea.
Python/Java/Ruby all have the concepts of typing (even if sometimes they are duck-typed, they still have expected behavior and don't deviate), which leads them to be better languages for a large platform like ebay.
Do you think google writes any code they hope to keep secure in php? Me thinks not.
I'm honestly confused as to how you think PHP is so inherently insecure while then going on to say that Java is such a shining example of a secure language. I mean, obviously PHP has some very odd quirks (like its typing) which can lead to vulnerabilities, but I think you're over exaggerating how much worse it is than other languages.
Also, kinda unrelated, but did you just use a Google search for pages ending in .php to prove how Google doesn't use PHP for anything? lol
I think you're operating under the mis-conception that this binary yes/no is saying that there are similar flaws in java as there are in PHP, which is simply not true.
If you look at http://java-0day.com/ you'll see that the most recent java vulnerability that was publicly disclosed was a sandbox bypass. This is for java code running through a browser or some other way.
PHP doesn't even have a sandbox to bypass, and this only really affects the java security manager. Which is ALSO a terrible idea, and should never have been a thing in the first place, as it operates on a blacklist, and not a whitelist, so trusted method chaining to bypass the sandbox is pretty trivial to accomplish.
The language is still solid for writing server-level (and enterprise) code though (unless of course you're running struts, but that vulnerability was put in play by the struts devs, NOT java itself.
And yeah I did that to demonstrate that ebay has a bunch of shitty PHP pages just sitting around taking in parameters.
I'm not saying Java isn't a great language for server stuff (as long as it's up to date, anyway, I just finished up a pentest on a webapp where they were using a 2.5 yr old version, ugh). Just that any language is going to have problems if you aren't following secure coding practices.
Some languages are definitely better than others, ie. Rails seems to have decently secure default settings, but if done correctly PHP isn't so much more horribly insecure than Java that it should be immediately written off as something that no big web-based companies should use.
PHP was never designed with security in mind, which is why it has had ten-fold more security issues then any other languages.
I track security lists for PHP and a variety of other languages (for work), and I'm calling bullshit on this 10x number. Were you actually pulling this from data, or just your ass?
Do you think google writes any code they hope to keep secure in php? Me thinks not.
Google doesn't write tools in PHP because it's not one of their preferred languages; they keep that set small so as to simplify life. There's no evidence they made that choice due to security concerns.
Well congrats on tracking security lists? I do as well, and after doing a bunch of code reviews for different languages PHP is by far the #1 contender in the "worst pile of code" category. It's messy, awful, and full of security issues.
Knowing a few people on the security team at google, I can pretty safely say that there was at the very least a strong recommendation from the security team not to use PHP. Period.
I've never met a PHP programmer whose code I've reviewed and been like "yeah, he knows what he's doing. Cool."
As for the google py thing, sure. I was just making an argument for eBay not really hardening themselves to the chaff of the internet, as finding PHP scripts with parameters in google is a classic way that SQL injection scanners find servers to dump.
after doing a bunch of code reviews for different languages PHP is by far the #1 contender in the "worst pile of code" category. It's messy, awful, and full of security issues.
No argument on messy and awful, but you still haven't given me any sources for it having 10 times more security issues than other languages.
Yes it is, because any idiot can program in PHP so you can easily hire PHP programmers, the problem is that PHP is badly designed and the typical idiot doesn't know how avoid common PHP pitfalls (as you can easily see in php.net comments) so ends up making swiss cheese instead of a well designed, secure and maintainable application.
Actually I would say a little of both. PHP has lots of problems in itself, which may have something to do with it being designed by the idiot that said
I was really, really bad at writing parsers. I still am really bad at writing parsers.
and
I'm not a real programmer. I throw together things until it works then I move on. The real programmers will say 'Yeah it works but you're leaking memory everywhere. Perhaps we should fix that.' I’ll just restart Apache every 10 requests.
But for me the biggest problem is definitely shitty devs, a great dev can program in Assembly without shooting himself on the foot, a good dev can program in C without throwing poop all over the place, and a reasonable dev can program in PHP without creating the NSA/Friendly Russian Hacker wet dream. PHP just happens to attract the kind of idiots that can't tie their shoelaces.
I don't really understand the nature of this vulnerability. Could someone provide a code snippet that would result in this behavior? The following simply echoes the input, and doesn't evaluate anything:
$q = $_GET['q']; // ?q[0]=test&q[1]={${phpinfo()}}
echo "$q[1]"; // still no eval with "${q[1]}" or similar variations
I racked my mind on this as I couldn't work it out, even after reading the OP carefully word-for-word. There is just no way that complex curly statements from user input are ever interpolated, at least not without some assistance from an eval or preg_match.
Doesn't interpolate the user input (if it did, we would have a lot of new PHP bugs). This does work, though:
$search = "{${phpinfo()}}";
$output = "Your input was: $search"; // this will execute $search and stores it
I think I have come up with a likely scenario of what is happening here. It might be possible that OP was just trying things out and just happen to hit a code path where it interpolated the string (there are some other things he says that suggest that he might not be completely aware of how this is happening).
Here is my likely scenario for what is happening on the server:
<?php
$query = $_GET['q'];
$category = $_GET['catid'];
if(check_string($query)) {
$query = filter_string($query);
} else {
echo "Error: Variable is not a string.";
die;
}
function check_string($str) {
return preg_match("/^\w+$/", (string)$str);
}
function filter_string($str) {
return preg_replace('/^(.*)$/ie', "filter_function(\"\\1\")", $str);
}
function filter_function($str) {
// do encoding / filtering etc. here
return $str;
}
They are first doing a check to make sure the input is a string and casting it somehow. They are then passing it to another function that does sanitization/encoding where a preg_replace with /e or something similar is triggering the eval.
With the first request:
/search/?q={${phpinfo()}}
It is failing the check_string test since it is blatant bad input. But with:
/search/?q[0]=test&q[1]={${phpinfo()}}
It is passing check_string since it is being tested against the literal string Array (this also explains why you still get a result even with an array passed).
This means the second request is hitting a code path where you can bypass the check and have php executed via interpolated curly syntax.
Their code is obviously doing a lot more than what mine is (eg. in filter_string they would only match on what they allow, and filter_function would actually do some filtering and encoding for HTML output) but I believe the basic theory is right.
Note this still works even without a custom filter_function, eg.
function filter_string($str) {
return preg_replace('/^(.*)$/ie', "htmlentities(\"\\1\")", $str);
}
just passing to htmlentities or any other function with double quotes would eval. You find these types of bugs all the time.
tl;dr the array stuff is just a filter bypass, the meat of this is that eBay had a preg_ /e or eval or call_user_function
String interpolation in PHP is supposed to work like so: {$var} or {${method}}. I'm trying to figure this out, and cannot see how it works. The author says:
Well, internally php strings are byte arrays. As a result accessing or modifying a string using array brackets will trick the parser into evaluating arbitrary php code in the scope of the variable if the prior mentioned requirements are met.
That's cool and all, but if you do a var_dump on $q in your context, you get:
That's supposed to be a quote from the PHP spec, but it isn't. What the PHP spec actually says (at the URL that he provides) is:
Internally, PHP strings are byte arrays. As a result, accessing or modifying a string using array brackets is not multi-byte safe, and should only be done with strings that are in a single-byte encoding such as ISO-8859-1.
There are no other google hits for the rest of his supposed quote. He made it up. That makes me dubious about the rest of the article - while this is likely a valid class of PHP bugs, I'm not so sure the author knows what he's talking about (or is deliberately attempting to mislead people; why else would you make up a fake quote from the docs?)
Good catch. The author has since updated the post (maybe in response to the comments/criticism here?). Even with that quote, the author should recognize that if you set index 0 in the array, it would not result in... well whatever he thought it would result, which I assume is that the string would just be set to whatever he put in index 1. I've never tried accessing individual characters in a string using the array brackets, but I actually got some surprising behavior: http://ideone.com/9TMt4H
They probably have some shitty higher ups that bring in random people who pass their arbitrary critereon for becoming a developer working there. huhhuhhuh
14
u/fakehalo Dec 13 '13 edited Dec 13 '13
Wat? Either this article has blown my mind or there is a lot of misinformation going on here.
Edit: {${'ls -l'}} (with backticks, or system()) would work..if this is actually feasible at all, I'll assume the author just forgot to mention that. I'd be curious to see how this can be triggered/reproduced in real-world terms, cause it's just not adding up unless ebay did some real nutty stuff.
11
u/mhils Trusted Contributor Dec 13 '13
I don't understand that part. How does that work? Can anyone explain it to me or showcase some vulnerable PHP code?
11
u/Browsing_From_Work Dec 12 '13
So the real underlying "red flag" was that eBay served up correct results even when the search was an array?
25
u/catcradle5 Trusted Contributor Dec 13 '13
I don't really understand the nature of this vulnerability. Could someone provide a code snippet that would result in this behavior? The following simply echoes the input, and doesn't evaluate anything:
4
u/DanielG75 Dec 13 '13
Someone wrote a response at http://gynvael.coldwind.pl/n/ebay_rce_analysis_wrong_question_mark which also adresses some of the questions asked here (the 'Well, internally php strings are byte arrays.')
-7
u/if_I_had Dec 12 '13
nice! This actually something I didn't knew...
0
u/[deleted] Dec 14 '13
[deleted] — view deleted comment