linux - Pass hexdata to an externam program from PHP -


here issue: wish pass hexadecimal data external app php:

exec('echo "'.$message.'" | /usr/bin/gateway'); 

and $message comes user input:

test'"/'\'/"\""//

addslashes(), stripslashes() doesn't solve troubles. while using:

$message = stripslashes($_post['message']); 

it console log can see:

sh: 1: syntax error: unterminated quoted string 

or empty value

so started thinking, converting input hexadecimal values , passing them help:

exec('echo -e '.$message.' | /usr/bin/gateway'); 

but got troubles passing hexadecimal data echo.

so question how that? need pass same string, user writes, gateway service. - convert user input hex form, , put in echo -e (how?) - or somehow fight quotations , slashes

in general case, cannot pass user input through shell because of shell's escaping rules. should doing getting handle on newly spawned process's standard input stream , feed directly.

you can use popen this:

$handle = popen('/usr/bin/gateway', 'w'); fwrite($handle, $_post['message']); pclose($handle); 

if need more example shows , popen not provide enough functionality, proc_open more involved , powerful alternative.

warning: feeding unfiltered user input external process leaves vulnerable exploits related how process interprets , acts on input. if application accessible non-trusted users, consider whitelisting input!


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -