Exec($_GET
github.com
github.com
$cmdname = split(' ', $_GET['command']);
if(!in_array(IDEMPOTENT_COMMANDS, $cmdname))
echo '<h1>Your request is not guaranteed to be idempotent. Please use a POST.</h1>';
else
exec($_GET['command'] ... if(!in_array(IDEMPOTENT_COMMANDS, $cmdname)) {
header("HTTP/1.1 405 Method Not Allowed");
die();
}
Fixed ;).PHP with its argument ordering strikes again :)
if(!IDEMPOTENT_COMMANDS[$cmdname]) { ...
would do, and, assuming dictionary lookups are optimised, is possibly faster. if (! isset(IDEMPOTENT_COMMANDS[$cmdname]))
otherwise you'll get an undefined index notice :) echo is idempotent
As a simple call, yes. But there are many shell tricks (redirection, command substitution, process substitution) that can make an echo call have significant side effects - so if you were daft enough to be considering this you'd need to do much more checking before submitting the provided instruction to your shell, and those checks would need to know which shell you were targeting (in fact you'd probably want to force the issue by exec()ing a specific shell instead of just using the default for the user the code is running as).They don't give untintended full access to the web server.
That said, they are a little insecure.
https://en.wikipedia.org/wiki/Webmin
Maybe not unintended, but definitely full access, and the world is almost certainly full of outdated/non whitelist access/weakly passworded panels.
Also, on a a sufficiently misconfigured server, you could always use \! (mysql's shell_exec, etc.) with phpmyadmin etc. to open a remote shell somewhere, then work from there.
But of course we just laugh about Perl and pat ourself on our backs with our safe new languages because we clearly know much more than those anachronistic neckbeards.
And there is some justification for that: if those "safe new languages" are doing the type checking at compile time, that is better than only finding out you have a safety issue when you fail at run time.
It's not explicit enough and it's easy enough to find legitimate code with accidental untainting of dangerous data.
Ruby requires an explicit untaint call, and IMHO it's the right way to go.
And yes, this can be encoded in the type system and you can also make it so the sanitization is context dependent, i.e. http://www.comp.nus.edu.sg/~prateeks/papers/csas-ccs11.pdf
It's leaky as hell, because all components have to get the marking right. Also, not all Objects have equal trust levels. Objects created due to a HTTP request (GET-Parameters) should certainly be tainted, but how about Object read through IO - do we trust out filesystem? Do we trust the database? Thats more of an architectural decision.
In the end, the problem comes down to this: you have to whitelist the world and everything you miss is an error.
Different languages (SQL, JS, HTML, Shell, Plaintext, etc.) are treated as different types. Language-specific functions only accept arguments of the relevant type (eg. shell_exec takes Shell, db_query takes SQL, etc.). User input is Plaintext (usually; sometimes it might be something more specific like BASE64).
Different languages can't be combined (eg. SQL can't be concatenated with Plaintext), but they can be converted down to Plaintext and Plaintext can be converted to any language via escaping functions. This avoids injection attacks, since the only way to please the type-checker is by escaping properly.
$device = $_GET['device']; $state = $_GET['state'];
exec( "sudo ./send " . $device . " " . $state );
EDIT: it is for home automation, but also appears to be a CS class group project.
[0] http://gcattani.co.vu/2013/03/a-tale-of-a-php-shell/ [1] http://php.net/manual/en/function.escapeshellarg.php
Everything that may come from a user must be filtered, escaped or generally treated as hostile.
As an example on an IRC channel someone once made their chan bot log the channel to the web, all it took was pasting javascript into an IRC window, and typing "LOL look at this! http://stupidbot.com/ircweblog". Channel pwned.
Someone should write a script that automatically raises an issues for each line and each project, it's probably possible, but I'm chronically lazy.
And to be clear, I'm talking about providing at least one legit use for passing user input directly to exec without any kind of filtering...
He deliberately wrote vulnerable code to test his auditing script. There are more repos like this.
Instead of half-assing the problem, please dedicate 10 minute of your life to look in, analyze & report one or two of the problems you find.
Also explain why you think this is an security issue.
You will:
* help someone out by pointing out an issue
* hopefully educate the person how to write better code
* educate yourself in reading and understanding others spaghetti codeIt should be easily doable to write a tool that finds an exec() of a variable that was assigned a $GET etc
- "eval(raw_input())" --> https://github.com/search?q=%22eval%28raw_input%28%29%29%22&...
- "eval(request" --> https://github.com/search?q=%22eval%28request%22&type=Code&r...
- "eval(request.POST" --> https://github.com/search?q=exec%28%24_POST&type=Code&ref=se...
- "eval(request.GET" --> https://github.com/search?q=%22eval%28request.GET%22&type=Co...
<?php
$result=shell_exec("cat ".$_GET['name'].".txt");
echo $result;
?>
How to abuse: $_GET['name'] = "/dev/null; rm -rf /; echo ";Removing all the files from a filesystem is something only a script kiddy would do, and it's probably a "best case scenario" for the owner of the server, because the impact of that is relatively small (just re-install the server and restore the backups). But once the attacker starts injecting mallware, stealing customer information (credit card numbers anyone?) or anything else nasty they can think of that they would benefit from, then you are in a whole lot more trouble...
git add id_rsa
WARNING: You may have just staged a private key.
or better echo '{"password": "mypassword"}' > config.json
git add config.json
WARNING: You may have just staged a password.is more the point you were trying to make, but yes.
https://github.com/search?q=exec+sudo+%24_GET&type=Code&ref=...
Maybe it's still an asinine error somewhat common, but i wouldn't take that search results as proof of how common it is...
https://github.com/search?q=%22exec%28%24_POST%22&type=Code
vs.
[0] https://github.com/riflon/Timantti/blob/2459c44fde2b378f63ac...
https://github.com/search?q=%22exec%28%24_GET%22&type=Code&r...
Otherwise there are way too many false positives. Still, 188 results is pretty awful.
$_GET['a']($_GET['b']);
88,846 PHP
1,420 HTML+ERB
1,177 JavaScript
1,128 HTML
423 Ruby
250 XML
160 Markdown
117 Emacs Lisp
91 INI
65 PerlEdit: ah, most of those are Metasploit scripts.