Sei qui: Home>Blog>BlogPost>PostgreSQL_SCRAM-SHA-256_authentication (11 Apr 2025, GuidoBrugnara)Modifica Allega

PostgreSQL SCRAM-SHA-256 authentication

05 Dec 2024 - 14:43:36 by Guido Brugnara in Postgresql

Dalla versione 10 PostgreSQL supporta RFC 7677.

Si tratta di uno schema di di trasmissione della password che impedisce lo sniffing delle stesse su connessioni non affidabili e supporta la memorizzazione in una forma crittografica hash, assai più sicura del metodo dasato su MD5.

Segue un esempio di codice in Perl utilizzabile da riga di comando, fornento la password mediante PIPE:
~$ echo 'my password' | ./scram_sha_256.pl
SCRAM-SHA-256$4096:NWxiS29VNlUwMkVaYTlqYw==$bvMFuhI6qZJLywX5SxB1JrdbdAiJQ+elsLDa+E7FFOQ=:BJJ1E8GqCtQMvyPRvYi83nlL6oqqNYlPnYUslMDwbYo=
~$
È così possibile cambiare la password nel database senza scambiare la password (in chiaro) nel codice SQL scambiato tra cliente e server:

ALTER USER "my user" ENCRYPTED PASSWORD 'SCRAM-SHA-256$4096:NWxiS29VNlUwMkVaYTlqYw==$bvMFuhI6qZJLywX5SxB1JrdbdAiJQ+elsLDa+E7FFOQ=:BJJ1E8GqCtQMvyPRvYi83nlL6oqqNYlPnYUslMDwbYo=';


Codice Perl scram_sha_256.pl :
#!/usr/bin/perl
# Copyright: 2024 - Guido Brugnara <gdo@leader.it>
# Licence: This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
use Crypt::Salt;
use Crypt::KeyDerivation qw(pbkdf2);
use Crypt::Mac::HMAC qw(hmac hmac_b64);
use Crypt::Digest::SHA256 qw(sha256_b64);
use MIME::Base64;
my $salt = Crypt::Salt::salt(16);
my $iterations = 4096;
my $password = <STDIN>;
chomp $password;
my $digest_key = pbkdf2($password, $salt, $iterations, 'SHA256', 32);
my $client_key = hmac('SHA256', $digest_key ,'Client Key');
my $b64_client_key = sha256_b64($client_key);
my $b64_server_key = hmac_b64('SHA256', $digest_key, 'Server Key');
my $b64_salt = encode_base64($salt, '');
print "SCRAM-SHA-256\$$iterations:$b64_salt\$$b64_client_key:$b64_server_key\n";


Codice Perl che utilizza il modulo Crypt::PostgreSQL:
#!/usr/bin/perl
use Crypt::PostgreSQL;
print Crypt::PostgreSQL::encrypt_scram('my password')."\n";

Comments (modifica)

 

Versione pagina: r3 - 11 Apr 2025, GuidoBrugnara
Questo sito utilizza FoswikiCopyright (©) Leader.IT - Italy P.I. IT01434390223 Informativa privacy & uso dei cookies