Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remote Code Execution vulnerability #420

Closed
vvanmol opened this issue Sep 18, 2020 · 7 comments · Fixed by #426
Closed

Remote Code Execution vulnerability #420

vvanmol opened this issue Sep 18, 2020 · 7 comments · Fixed by #426

Comments

@vvanmol
Copy link

vvanmol commented Sep 18, 2020

Hello,

I haven't seen any open issue about this so I'm creating this ticket, but Snyk reported a vulnerability about this package.
Please find more details about it here: https://snyk.io/vuln/SNYK-JS-LOCUTUS-575119

@kvz
Copy link
Collaborator

kvz commented Sep 18, 2020

Is this the issue that needs fixing? #395

@vvanmol
Copy link
Author

vvanmol commented Sep 18, 2020

Seems to be related to the same function, but not the same issue. Here it's about sanitizing two single quotes characters as explained in this Blog post: https://reallinkers.github.io/CVE-2020-13619/

The following code seems to exploit the vulnerability (taken from the node sample in the blog post above):

var phpExec = require('locutus/php/exec');
var child = require('child_process');

var directory = "/home'; whoami;''"
var command = "ls -la " + phpExec.escapeshellarg(directory);
console.log(command);

child.exec(command, function (error,stdout,stderr) {
	console.log(stdout);
	console.log(stderr);
});

@divinity76
Copy link
Contributor

divinity76 commented Sep 22, 2020

here is how i would implement it personally,

function escapeshellarg(arg){
  if(arg.indexOf("\x00") !== -1) {
    throw new Error('escapeshellarg(): Argument #1 ($arg) must not contain any null bytes');
  }
  return "'"+arg.replace(/\'/g,'\'\\\'\'')+"'";
}

for one, it's much closer to how PHP's escapeshellarg() works, and second, it's not vulnerable to the above shell execution exploit, correctly generating the command

$ ls -la '/home'\''; whoami;'\'''\'''
ls: cannot access '/home'\''; whoami;'\'''\''': No such file or directory

$ ls -la ' starts the quote, /home' ends the quote, \' adds an escaped quote, ' starts the quote, ; whoami;' ends the quote, \' adds an escaped quote, ' starts the quote, ' ends the quote, \' adds an escaped quote, and ' starts the last quote required to make the always-appended last ' correctly end the quote :)

(it's technically possible to create a smaller command with the same data, \'\' and ''\'''\''' compiles to the exact same string, but given the security-sensitive aspect if we get the optimization wrong, and the fact that even the php core developers didn't try to optimize this part, we probably shouldn't try to do that either.)

divinity76 added a commit to divinity76/locutus that referenced this issue Sep 22, 2020
divinity76 added a commit to divinity76/locutus that referenced this issue Sep 22, 2020
@divinity76 divinity76 mentioned this issue Sep 22, 2020
2 tasks
@divinity76
Copy link
Contributor

proposed a fix in #426

@divinity76
Copy link
Contributor

divinity76 commented Oct 2, 2020

uhm.. guys? can i get some feedback? (the PR has been completely silent for 10 days now~)

@vvanmol
Copy link
Author

vvanmol commented Oct 2, 2020

Hi @divinity76

Your solution seems to be good, and you confirmed it fixes the vulnerability. Not sure I can do anything from my side, but waiting for maintainers...

@kvz kvz closed this as completed in #426 Oct 2, 2020
kvz pushed a commit that referenced this issue Oct 2, 2020
* patch CVE-2020-13619

fixes #420

* formatting

missing a space if nothing else

* add bufix sample & credits

* update example 2

* forgot escapeshellarg() in example2
@kvz
Copy link
Collaborator

kvz commented Oct 2, 2020

Thank you! Merged and released as locutus@2.0.13

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants