A evolução da segurança PHP em 2016 se resume a:
- Melhorias nos algoritmos de hashing de senhas;
- Nova função de números aleatórios e geração de bytes aleatórios;
- Suporte a criptografia simétrica com libsodium e OpenSSL;
- Uso obrigatório do PDO para acesso a bancos de dados.
1. 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
2. Presidente da ABRAPHP – Associação Brasileira de Profissionais PHP
Diretor da PHP Conference Brasil
Contribui 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) e
Faculdade 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 internacionais
Instrutor de cursos presenciais e a distância
Fundador e líder do GU PHPBR
Fundador* e membro do GU PHPRS
Site: http://www.galvao.eti.br/
http://people.php.net/galvao
Twitter: @galvao
Slides e Documentos: http://slideshare.net/ergalvao
Github: http://github.com/galvao
Posts: 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
3. 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
4. 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
5. 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');
6. 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
7. 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
8. 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
9. 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
10. 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
11. 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
12. 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
13. 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
14. 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
15. 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
16. 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);
17. 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);
18. 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);
19. 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
<?php
try {
$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());
}
20. 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
<?php
try {
$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());
}
21. 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
22. 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
23. CC Attribution-ShareAlike 3.0 Unported License by Er Galvão Abbott - 5/4/16 - 23 / 37
www.galvao.eti.br
$key = Sodiumrandombytes_buf(SodiumCRYPTO_SECRETBOX_KEYBYTES);
$nonce =
Sodiumrandombytes_buf(SodiumCRYPTO_SECRETBOX_NONCEBYTES);
$ciphertext = Sodiumcrypto_secretbox('test', $nonce, $key);
Segurança PHP em 2016
Criptografia
$plaintext = Sodiumcrypto_secretbox_open($ciphertext, $nonce, $key);
24. 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');
25. 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...
26. 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...
27. 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;
28. 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;
29. Tipagem
CC Attribution-ShareAlike 3.0 Unported License by Er Galvão Abbott - 5/4/16 - 29 / 37
www.galvao.eti.br
<?php
declare(strict_types=1);
function foo(int $x, int $y)
{
return $x + $y;
}
echo foo('1', 2);
Segurança PHP em 2016
30. Tipagem
CC Attribution-ShareAlike 3.0 Unported License by Er Galvão Abbott - 5/4/16 - 30 / 37
www.galvao.eti.br
<?php
declare(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:
...
31. 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”
32. 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
Imagem
by nixCraft
33. 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:
34. 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!
35. 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
36. 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 linguagem
https://wiki.php.net/rfc
Usando libsodium com PHP
https://paragonie.com/book/pecl-libsodium
Pesquise e ESTUDE!
https://www.owasp.org/
RTFM!!!
http://php.net/manual/en
37. 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