mercredi 25 février 2015

php- is my code secured enough to protect from remote command execution when using shell_exec()?



I am using shell_exec function on the following variables to execute commands on the shell:



  1. fname (characters only)

  2. fpack(characters only)

  3. email (a valid email address)


My code is:



<?php

require_once 'connectionToDB.php';

$fname = mysqli_real_escape_string($dbc, filter_var(escapeshellarg($_POST['fname']), FILTER_SANITIZE_STRING));
$fpack = mysqli_real_escape_string($dbc, filter_var(escapeshellarg($_POST['fpack']), FILTER_SANITIZE_STRING));
$email = mysqli_real_escape_string($dbc, filter_var(escapeshellarg($_POST['email']), FILTER_SANITIZE_EMAIL));

/** Verify name of applicaion **/
if(!ctype($fname)) {
$op = json_encode(array('type' => 'error', 'msg' => 'Application name must be in english alphabetical letters only'));
die($op);
}
if(strlen($fname)>20) {
$op = json_encode(array('type' => 'error', 'msg' => 'Application name must be less than 20 characters'));
die($op);
}

/** Verify name of package **/
if(!ctype($fpack)) {
$op = json_encode(array('type' => 'error', 'msg' => 'Package name must be in english alphabetical letters only'));
die($op);
}
if(strlen($fpack)>20) {
$op = json_encode(array('type' => 'error', 'msg' => 'Package name must be less than 20 characters'));
die($op);
}

/** Verify user's email **/
if (strlen($email)>50) {
$op = json_encode(array('type' => 'error', 'msg' => 'Email must be of less than 50 characters'));
die($op);
}

if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$op = json_encode(array('type' => 'error', 'msg' => 'Please provide a valid email address.'));
die($op);
}


As I said I am using shell_exec on these variables, I am scared of remote command execution. Is my code safe enough to prevent RCE?





Aucun commentaire:

Enregistrer un commentaire