$30 off During Our Annual Pro Sale. View Details »

Autenticação de Dois Fatores

Autenticação de Dois Fatores

Como implementar autenticação de dois fatores em sua aplicação

Ricardo Coelho

September 18, 2017
Tweet

More Decks by Ricardo Coelho

Other Decks in Programming

Transcript

  1. Autenticação
    de Dois Fatores
    Ricardo Coelho
    @ramcoelho

    View Slide

  2. View Slide

  3. Autenticar?
    Identificar
    uma pessoa
    de maneira
    assertiva
    utilizando
    uma métrica
    mensurável
    ou comparável

    View Slide

  4. Fatores?
    Cada um dos
    diferentes
    mecanismos
    de identificação
    (métricas)
    * * *
    ?

    View Slide

  5. Fatores Comuns
    Senha
    Biometria
    (Impressão Digital)
    PIN
    Token / SMS / E-mail
    Certificado Digital

    View Slide

  6. Bancos
    Senha + Token

    View Slide

  7. Bancos
    Senha + Token

    View Slide

  8. e-Tokens

    View Slide

  9. e-Tokens

    View Slide

  10. “To cloud or not to cloud”
    –Authamlet

    View Slide

  11. Exemplo
    Inserção Manual
    ou
    Leitura de QR Code

    View Slide

  12. Exemplo
    Inserção Manual
    ou
    Leitura de QR Code

    View Slide

  13. Exemplo
    Inserção Manual
    ou
    Leitura de QR Code

    View Slide

  14. One Time Password - OTP
    Senhas descartáveis
    Exposição apenas do código derivado
    Hash de um Segredo + Semente Variável
    Conversões e Transformações

    View Slide

  15. OTP
    HOTP TOTP
    HMAC-BASED
    OTP
    TIME-BASED
    OTP
    Hashed Message
    Authentication Code
    Also HMAC
    But time, not counter

    View Slide

  16. HOTP
    $secret = ‘4CAF7G34BBGS4CTF’;
    $counter++;
    $hash = hash(‘sha1’, $secret . $counter);
    $code = toDigits($hash, 6);

    View Slide

  17. TOTP
    $secret = ‘4CAF7G34BBGS4CTF’;
    $time = (int)(time() / 30);
    $hash = hash(‘sha1’, $secret . $time);
    $code = toDigits($hash, 6);

    View Slide

  18. HOTP vs. TOTP
    HOTP não exige sincronização de relógios
    TOTP trabalha melhor com janelas
    estreitas (mais segurança)
    HOTP permite validação bi-direcional
    TOTP não perde sincronização por mal uso

    View Slide

  19. Formato e-Token

    View Slide

  20. OTPAuth

    View Slide

  21. OTPAuth
    otpauth://totp/usuario%40servidor.com
    ?secret=4CAF7G34BBGS4CTF
    &period=30&digits=6
    &algorithm=SHA1&issuer=MeuApp

    View Slide

  22. BASE64
    46 61 74 6f 72
    01000110 01100001 01110100 01101111 01110010
    010001 100110 000101 110100 011011 110111 0010
    RmF0b3I=
    Fator

    View Slide

  23. BASE64
    0 - A
    1 - B
    2 - C
    3 - D
    4 - E
    5 - F
    6 - G
    7 - H
    8 - I
    9 - J
    10 - K
    11 - L
    12 - M
    13 - N
    14 - O
    15 - P
    16 - Q
    17 - R
    18 - S
    19 - T
    20 - U
    21 - V
    22 - W
    23 - X
    24 - Y
    25 - Z
    26 - a
    27 - b
    28 - c
    29 - d
    30 - e
    31 - f
    32 - g
    33 - h
    34 - i
    35 - j
    36 - k
    37 - l
    38 - m
    39 - n
    40 - o
    41 - p
    42 - q
    43 - r
    44 - s
    45 - t
    46 - u
    47 - v
    48 - w
    49 - x
    50 - y
    51 - z
    52 - 0
    53 - 1
    54 - 2
    55 - 3
    56 - 4
    57 - 5
    58 - 6
    59 - 7
    60 - 8
    61 - 9
    62 - +
    63 - /

    View Slide

  24. BASE32
    Fator
    46 61 74 6f 72
    01000110 01100001 01110100 01101111 01110010
    01000 11001 10000 10111 01000 11011 11011 10010
    IZQXI33S

    View Slide

  25. BASE32
    0 - A
    1 - B
    2 - C
    3 - D
    4 - E
    5 - F
    6 - G
    7 - H
    16 - Q
    17 - R
    18 - S
    19 - T
    20 - U
    21 - V
    22 - W
    23 - X
    24 - Y
    25 - Z
    26 - 2
    27 - 3
    28 - 4
    29 - 5
    30 - 6
    31 - 7
    8 - I
    9 - J
    10 - K
    11 - L
    12 - M
    13 - N
    14 - O
    15 - P

    View Slide

  26. Implementação
    $ composer require spomky-labs/otphp
    chdir(__DIR__);
    require ‘vendor/autoload.php’;
    use OTPHP\TOTP;
    $otp = TOTP::create();
    echo ‘OTP: ’ . $otp->now();

    View Slide

  27. Implementação
    $ composer require spomky-labs/otphp
    (…)
    use OTPHP\HOTP;
    $counter = 15;
    $otp = HOTP::create();
    echo ‘OTP: ’ . $otp->at($counter);

    View Slide

  28. Implementação
    (…)
    use OTPHP\TOTP;
    $otp = TOTP::create(‘4CAF7G34BBGS4CTF’);
    $otp->verify($input);
    $ composer require spomky-labs/otphp

    View Slide

  29. OTPAuth
    otpauth://totp/MeuApp:[email protected]
    ?issuer=MeuApp&secret=4CAF7G34BBGS4CTF
    &period=30&digits=6&algorithm=sha1
    (…)
    $totp = TOTP::create(
    ‘4CAF7G34BBGS4CTF’, 30, ‘SHA1’, 6
    );
    $totp->setLabel(‘[email protected]’);
    $totp->setIssuer(‘MeuApp’);
    echo $totp->getProvisioningUri() . “\n”;
    <— Implícito

    View Slide

  30. Implementação
    $ composer require bacon/bacon-qr-code

    View Slide

  31. Implementação
    (…)
    $otp = TOTP::create(‘4CAF7G34BBGS4CTF’);
    $otp->setLabel(‘[email protected]’);
    $otp->setIssuer(‘MeuApp’);
    $renderer = new \BaconQrCode\Renderer\ImageRenderer(
    new \BaconQrCode\Renderer\RendererStyle\RendererStyle(256),
    new \BaconQrCode\Renderer\Image\ImagickImageBackEnd()
    );
    $writer = new \BaconQrCode\Writer($renderer);
    $writer->writeFile(
    $otp->getProvisioningUri(),
    ‘qrcode.png’
    );
    otpauth://totp/
    MeuApp:[email protected]
    ?issuer=MeuApp
    &secret=4CAF7G34BBGS4CTF

    View Slide

  32. Implementação
    Jamais ative a autenticação em dois
    fatores sem exigir a confirmação do OTP
    pelo usuário
    Jamais desative também
    Implemente um canal alternativo para
    entrega do OTP (SMS, e-mail)

    View Slide

  33. OTP vs. Passwords
    OTP é limitado em termos de segurança
    Senhas têm tamanho variado
    Senhas podem nunca expirar
    Ambos são baseados em segredo

    View Slide

  34. TOTP + Password

    View Slide

  35. Segurança
    Janelas TOTP devem ter no máximo 30s
    Implemente notificação de login simultâneo
    Criptografe o segredo, guarde apenas hash
    das senhas
    OTP não protege aplicação contra
    vazamentos catastróficos (banco e fonte)

    View Slide

  36. Referências
    https:/
    /tools.ietf.org/html/rfc2289 (OTPAuth)
    https:/
    /tools.ietf.org/html/rfc4226 (HOTP)
    https:/
    /tools.ietf.org/html/rfc6238 (TOTP)
    https:/
    /tools.ietf.org/html/rfc2104 (HMAC)
    https:/
    /tools.ietf.org/html/rfc4648 (Base32)

    View Slide