Upload
er-galvao-abbott
View
496
Download
0
Embed Size (px)
Citation preview
Segurança
em 2016
Segurança PHP em 2016 www.galvao.eti.br
CC Attribution-ShareAlike 3.0 Unported License by Er Galvão Abbott - 5/4/16 - 1 / 37
Presidente da ABRAPHP – Associação Brasileira de Profissionais PHPDiretor da PHP Conference BrasilContribui para a tradução da documentação oficial Atua como Zend Framework Evangelist para o ZTeam, da Zend.Professor Especialista de Pós-Graduação UNOESC (SC) eFaculdade Alfa (PR)
20+ anos desenvolvendo sistemas e aplicações com interface web 15+ destes com PHP 7+ com Zend Framework
Palestrante em eventos nacionais e internacionaisInstrutor de cursos presenciais e a distânciaFundador e líder do GU PHPBRFundador* e membro do GU PHPRS
Site: http://www.galvao.eti.br/ http://people.php.net/galvaoTwitter: @galvaoSlides e Documentos: http://slideshare.net/ergalvaoGithub: http://github.com/galvaoPosts: https://medium.com/@galvao
Quem?!
CC Attribution-ShareAlike 3.0 Unported License by Er Galvão Abbott - 5/4/16 - 2 / 37
www.galvao.eti.brSegurança PHP em 2016
Sumário
CC Attribution-ShareAlike 3.0 Unported License by Er Galvão Abbott - 5/4/16 - 3 / 37
www.galvao.eti.br
O status da Segurança PHP em 2016● Hashing● PRNG● Criptografia● Banco de Dados● Tipagem
Segurança PHP em 2016
Sumário
CC Attribution-ShareAlike 3.0 Unported License by Er Galvão Abbott - 5/4/16 - 4 / 37
www.galvao.eti.brSegurança PHP em 2016
Tudo se resume a evolução
Hashing (Senhas)
CC Attribution-ShareAlike 3.0 Unported License by Er Galvão Abbott - 5/4/16 - 5 / 37
www.galvao.eti.brSegurança PHP em 2016
md5('my_password');sha1('my_password');crypt('my_password', 'my_salt');
Hashing (Senhas)
CC Attribution-ShareAlike 3.0 Unported License by Er Galvão Abbott - 5/4/16 - 6 / 37
www.galvao.eti.br
md5('my_password');sha1('my_password');crypt('my_password', 'my_salt');
Segurança PHP em 2016
Hashing (Senhas)
CC Attribution-ShareAlike 3.0 Unported License by Er Galvão Abbott - 5/4/16 - 7 / 37
www.galvao.eti.br
md5('my_password');sha1('my_password');crypt('my_password', 'my_salt');
Segurança PHP em 2016
Hashing (Senhas)
CC Attribution-ShareAlike 3.0 Unported License by Er Galvão Abbott - 5/4/16 - 8 / 37
www.galvao.eti.br
<?php$hash = password_hash('my_password',
PASSWORD_BCRYPT, ['cost' => 10, 'salt' => 'this_is_my_salt_is_my_salt']
);
Segurança PHP em 2016
Hashing (Senhas)
CC Attribution-ShareAlike 3.0 Unported License by Er Galvão Abbott - 5/4/16 - 9 / 37
www.galvao.eti.br
<?php$hash = password_hash('my_password',
PASSWORD_BCRYPT, ['cost' => 10, 'salt' => 'this_is_my_salt_is_my_salt']
);
if (password_verify('my_password', $hash)) {// YAY!
}
Segurança PHP em 2016
Hashing (Senhas)
CC Attribution-ShareAlike 3.0 Unported License by Er Galvão Abbott - 5/4/16 - 10 / 37
www.galvao.eti.br
<?php$pass = 'my_password';$options = ['cost' => 10];
$hash = password_hash($pass, PASSWORD_DEFAULT, $options
);
if (password_verify($pass, $hash)) {if (password_needs_rehash($hash,
PASSWORD_DEFAULT, $options)) {$newHash = password_hash($pass,
PASSWORD_DEFAULT, $options);
}}
Segurança PHP em 2016
Hashing (Senhas)
CC Attribution-ShareAlike 3.0 Unported License by Er Galvão Abbott - 5/4/16 - 11 / 37
www.galvao.eti.br
<?php$pass = 'my_password';$options = ['cost' => 10];
$hash = password_hash($pass, PASSWORD_DEFAULT, $options
);
if (password_verify($pass, $hash)) {if (password_needs_rehash($hash,
PASSWORD_DEFAULT, $options)) {$newHash = password_hash($pass,
PASSWORD_DEFAULT, $options);
}}
Segurança PHP em 2016
Hashing (Senhas)
CC Attribution-ShareAlike 3.0 Unported License by Er Galvão Abbott - 5/4/16 - 12 / 37
www.galvao.eti.br
<?php$pass = 'my_password';$options = ['cost' => 10];
$hash = password_hash($pass, PASSWORD_DEFAULT, $options
);
if (password_verify($pass, $hash)) {if (password_needs_rehash($hash,
PASSWORD_DEFAULT, $options)) {$newHash = password_hash($pass,
PASSWORD_DEFAULT, $options);
}}
Segurança PHP em 2016
PRNG
CC Attribution-ShareAlike 3.0 Unported License by Er Galvão Abbott - 5/4/16 - 13 / 37
www.galvao.eti.br
rand($min, $max);mt_rand($min, $max);
// Implementação qualquer de randomização // de strings, tipo essa
Segurança PHP em 2016
PRNG
CC Attribution-ShareAlike 3.0 Unported License by Er Galvão Abbott - 5/4/16 - 14 / 37
www.galvao.eti.br
rand($min, $max);mt_rand($min, $max);
// Implementação qualquer de randomização // de strings, tipo essa
Segurança PHP em 2016
PRNG
CC Attribution-ShareAlike 3.0 Unported License by Er Galvão Abbott - 5/4/16 - 15 / 37
www.galvao.eti.br
rand($min, $max);mt_rand($min, $max);
// Implementação qualquer de randomização // de strings, tipo essa
Segurança PHP em 2016
PRNG
CC Attribution-ShareAlike 3.0 Unported License by Er Galvão Abbott - 5/4/16 - 16 / 37
www.galvao.eti.brSegurança PHP em 2016
<?php$randomInt = random_int(0, 10);
PRNG
CC Attribution-ShareAlike 3.0 Unported License by Er Galvão Abbott - 5/4/16 - 17 / 37
www.galvao.eti.brSegurança PHP em 2016
<?php$randomInt = random_int(0, 10);$randomStr = random_bytes(22);
PRNG
CC Attribution-ShareAlike 3.0 Unported License by Er Galvão Abbott - 5/4/16 - 18 / 37
www.galvao.eti.brSegurança PHP em 2016
<?php$randomInt = random_int(0, 10);$randomStr = random_bytes(22);echo $randomInt . PHP_EOL;echo base64_encode($randomStr);
PRNG
CC Attribution-ShareAlike 3.0 Unported License by Er Galvão Abbott - 5/4/16 - 19 / 37
www.galvao.eti.brSegurança PHP em 2016
<?phptry {
$randomInt = random_int(0, 10);echo $randomInt . PHP_EOL;
} catch (Exception $e) {die('Lib de randomização não encontrada: ' .
$e->getMessage());}
try {$randomStr = random_bytes(22);echo base64_encode($randomStr);
} catch (Exception $e) {die('Lib de randomização não encontrada: ' .
$e->getMessage());}
PRNG
CC Attribution-ShareAlike 3.0 Unported License by Er Galvão Abbott - 5/4/16 - 20 / 37
www.galvao.eti.brSegurança PHP em 2016
<?phptry {
$randomInt = random_int(0, 10);echo $randomInt . PHP_EOL;
} catch (Exception $e) {die('Lib de randomização não encontrada: ' .
$e->getMessage());}
try {$randomStr = random_bytes(22);echo base64_encode($randomStr);
} catch (Exception $e) {die('Lib de randomização não encontrada: ' .
$e->getMessage());}
CC Attribution-ShareAlike 3.0 Unported License by Er Galvão Abbott - 5/4/16 - 21 / 37
www.galvao.eti.br
mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $str, MCRYPT_MODE_CBC, $iv
);
mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, $crypt, MCRYPT_MODE_CBC, $iv
);
Segurança PHP em 2016
Criptografia
CC Attribution-ShareAlike 3.0 Unported License by Er Galvão Abbott - 5/4/16 - 22 / 37
www.galvao.eti.br
mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $str, MCRYPT_MODE_CBC, $iv
);
mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, $crypt, MCRYPT_MODE_CBC, $iv
);
Segurança PHP em 2016
Criptografia
CC Attribution-ShareAlike 3.0 Unported License by Er Galvão Abbott - 5/4/16 - 23 / 37
www.galvao.eti.br
$key = \Sodium\randombytes_buf(\Sodium\CRYPTO_SECRETBOX_KEYBYTES);
$nonce = \Sodium\randombytes_buf(\Sodium\CRYPTO_SECRETBOX_NONCEBYTES);$ciphertext = \Sodium\crypto_secretbox('test', $nonce, $key);
Segurança PHP em 2016
Criptografia
$plaintext = \Sodium\crypto_secretbox_open($ciphertext, $nonce, $key);
CC Attribution-ShareAlike 3.0 Unported License by Er Galvão Abbott - 5/4/16 - 24 / 37
www.galvao.eti.br
$crypt = openssl_encrypt('teste', 'blowfish', 'foo', 0, '11122233');
Segurança PHP em 2016
Criptografia
$plaintext = openssl_decrypt($crypt, 'blowfish', 'foo', 0, '11122233');
CC Attribution-ShareAlike 3.0 Unported License by Er Galvão Abbott - 5/4/16 - 25 / 37
www.galvao.eti.brSegurança PHP em 2016
Banco de Dados
mysql_*();Queries com valores diretos;
Entre outras coisas...
CC Attribution-ShareAlike 3.0 Unported License by Er Galvão Abbott - 5/4/16 - 26 / 37
www.galvao.eti.brSegurança PHP em 2016
Banco de Dados
mysql_*();Queries com valores diretos;
Entre outras coisas...
CC Attribution-ShareAlike 3.0 Unported License by Er Galvão Abbott - 5/4/16 - 27 / 37
www.galvao.eti.brSegurança PHP em 2016
Banco de Dados$dsn = 'mysql:dbname=nome_do_banco;host=127.0.0.1';$user = 'usuario_do_banco';$password = 'senha_do_banco';$value = 'ABC'
try { $dbh = new PDO($dsn, $user, $password);} catch (PDOException $e) { echo 'Conexão falhou: ' . $e->getMessage();}
$sql = 'SELECT * FROM produto WHERE nome=:clause';
$sth = $dbh->prepare($sql);$sth->bindParam(':clause', $value, PDO::PARAM_STR);
$sth->execute();$red = $sth->fetchAll();
var_dump($red[0]['nome']);
$dbh = NULL;
CC Attribution-ShareAlike 3.0 Unported License by Er Galvão Abbott - 5/4/16 - 28 / 37
www.galvao.eti.brSegurança PHP em 2016
Banco de Dados$dsn = 'mysql:dbname=nome_do_banco;host=127.0.0.1';$user = 'usuario_do_banco';$password = 'senha_do_banco';$value = 'ABC'
try { $dbh = new PDO($dsn, $user, $password);} catch (PDOException $e) { echo 'Conexão falhou: ' . $e->getMessage();}
$sql = 'SELECT * FROM produto WHERE nome=:clause';
$sth = $dbh->prepare($sql);$sth->bindParam(':clause', $value, PDO::PARAM_STR);
$sth->execute();$red = $sth->fetchAll();
var_dump($red[0]['nome']);
$dbh = NULL;
Tipagem
CC Attribution-ShareAlike 3.0 Unported License by Er Galvão Abbott - 5/4/16 - 29 / 37
www.galvao.eti.br
<?phpdeclare(strict_types=1);
function foo(int $x, int $y){ return $x + $y;}
echo foo('1', 2);
Segurança PHP em 2016
Tipagem
CC Attribution-ShareAlike 3.0 Unported License by Er Galvão Abbott - 5/4/16 - 30 / 37
www.galvao.eti.br
<?phpdeclare(strict_types=1);
function foo(int $x, int $y){ return $x + $y;}
echo foo('1', 2);
Segurança PHP em 2016
Fatal error: Uncaught TypeError: Argument 1 passed to foo() must be of the type integer, string given, called in /home/vagrant/php7tests/t1.php on line 9 and defined in /home/vagrant/php7tests/t1.php:4
Stack trace:...
CC Attribution-ShareAlike 3.0 Unported License by Er Galvão Abbott - 5/4/16 - 31 / 37
www.galvao.eti.brSegurança PHP em 2016
Constantes
Nem tudo muda…… ou o que eu quero dizer com “outras coisas”
CC Attribution-ShareAlike 3.0 Unported License by Er Galvão Abbott - 5/4/16 - 32 / 37
www.galvao.eti.brSegurança PHP em 2016
Constantes
Imagemby nixCraft
CC Attribution-ShareAlike 3.0 Unported License by Er Galvão Abbott - 5/4/16 - 33 / 37
www.galvao.eti.brSegurança PHP em 2016
Constantes
Segurança deixa a aplicação lenta?Segurança é difícil de aprender?Segurança é difícil de implementar?
A resposta continua sendo a mesma:
CC Attribution-ShareAlike 3.0 Unported License by Er Galvão Abbott - 5/4/16 - 34 / 37
www.galvao.eti.brSegurança PHP em 2016
Constantes
Segurança deixa a aplicação lenta?Segurança é difícil de aprender?Segurança é difícil de implementar?
A resposta continua sendo a mesma:
NÃO IMPORTA!
CC Attribution-ShareAlike 3.0 Unported License by Er Galvão Abbott - 5/4/16 - 35 / 37
www.galvao.eti.brSegurança PHP em 2016
Constantes
Notegraphy,Galvão
INFORME-SE!
CC Attribution-ShareAlike 3.0 Unported License by Er Galvão Abbott - 5/4/16 - 36 / 37
www.galvao.eti.brSegurança PHP em 2016
Acompanhe as mudanças da linguagemhttps://wiki.php.net/rfc
Usando libsodium com PHPhttps://paragonie.com/book/pecl-libsodium
Pesquise e ESTUDE!https://www.owasp.org/
RTFM!!!http://php.net/manual/en
Muito obrigado!
CC Attribution-ShareAlike 3.0 Unported License by Er Galvão Abbott - 5/4/16 - 37 / 37
www.galvao.eti.br
? Dúvidas?↓ Críticas?↑ Elogios?!
Segurança PHP em 2016