Sécuriser & chiffrer Mariadb - JDLL 2017

53
JDLL – 1 er avril 2017 Sécuriser et Chiffrer MariaDB Christophe Villeneuve @hellosct1 SQL – NoSQL – NewSQL - BigData

Transcript of Sécuriser & chiffrer Mariadb - JDLL 2017

Page 1: Sécuriser & chiffrer Mariadb - JDLL 2017

JDLL – 1er avril 2017

Sécuriser et Chiffrer MariaDB

Christophe Villeneuve@hellosct1

SQL – NoSQL – NewSQL - BigData

Page 2: Sécuriser & chiffrer Mariadb - JDLL 2017

Qui ???

Christophe Villeneuve

mozilla reps - firefox os - B2GOS - ausy - afup – lemug.fr – mysql – mariadb – drupal – demoscene – firefoxos – drupagora – phptour – forumphp – solutionlinux – Libre@toi – eyrolles – editions eni – programmez – linux pratique – webriver – phptv – elephpant - owasp -security

Page 3: Sécuriser & chiffrer Mariadb - JDLL 2017

JDLL – 1er avril 2017

Slide du premier Avril - poissonavril

Otarie

● Mammifère vivant en mer● 120 espèces● Espèce en voie de disparition

● Otarie VS Homme– La Chasse

– Spectacle

– Dresser pour une utilisation militaire

● Nourriture préférée– Harengs, calmars, maquereaux, poissons...

SLIDE

Page 4: Sécuriser & chiffrer Mariadb - JDLL 2017

JDLL – 1er avril 2017

Aujourd'hui

● On a bien rigolé :-)● Quelques rappels MariaDB● SQL + NoSQL = NewSQL● BigData● Sécurité● Chiffrement

Page 5: Sécuriser & chiffrer Mariadb - JDLL 2017

JDLL – 1er avril 2017

Mémoire

Page 6: Sécuriser & chiffrer Mariadb - JDLL 2017

JDLL – 1er avril 2017

✔ Fondé par

✔ Monty Widenius✔ David Axmark

✔ 1983 – 1ère idée

✔ Fondé en 1995

✔ 2007 Préparation pour Nasdaq

✔ 2008 Rachat par Sun

✔ 2009 Sun racheté par Oracle

✔ Promesse de Oracle sur 5 ans (fin 2014)

L'origine Dates importantes

Rappels

Page 7: Sécuriser & chiffrer Mariadb - JDLL 2017

JDLL – 1er avril 2017

✔ Communauté d'amis✔ 100 % Open source et

compatible MySQL✔ Plateforme d'innovation✔ Des commiteurs✔ Fondation (2013)

✔ Dec 2008 Création de Monty Program 

 → Monty Widenius

­­­­­­­­­­­­ MariaDB ­­­­­­­­­­­­­

✔ 5.1 (Fev 2010)✔ 5.5.x (Dec 2016)

✔ 10.0 (Mars 2013)✔ 10.1.x (Mars 2017)✔ 10.2 (1er semestre 2017) 

Galera people

Compression Multimaster Replication

MariaDB : Le projet...

Page 8: Sécuriser & chiffrer Mariadb - JDLL 2017

JDLL – 1er avril 2017

✔ PHP (driver natif) → BSD licence

✔ Python

✔ Perl

✔ Ruby

✔ .NET avec MyODBC

✔ JDBC (basé sur drizzle driver)

✔ C

✔ Oracle connector (licence GPL)

Tous sont LGPL → Aucune licence commerciale

Différents connecteurs avec MariaDB

Page 9: Sécuriser & chiffrer Mariadb - JDLL 2017

JDLL – 1er avril 2017

MariaDB Repositories : installation

> sudo apt-get install software-properties-common

> sudo apt-key adv --recv-keys --keyserver hkp://keyserver.ubuntu.com:80 0xcbcb082a1bb943db

> sudo add-apt-repository 'deb [arch=amd64,i386,ppc64el] http://fr.mirror.babylon.network/mariadb/repo/10.1/ubuntu trusty main'

https://downloads.mariadb.org/mariadb/repositories/#mirror=babylon-fr&distro=Ubuntu&distro_release=trusty--ubuntu_trusty&version=10.1

Page 10: Sécuriser & chiffrer Mariadb - JDLL 2017

JDLL – 1er avril 2017

✔ sudo apt-get install mariadb-server mariadb-client

✔ Vous retrouvez :✔ /etc/mysql/my.cnf✔ > mysql

✔ Au final

Linux

Page 11: Sécuriser & chiffrer Mariadb - JDLL 2017

JDLL – 1er avril 2017

NewSQL

→ Développement → Moteur de stockages

+

Page 12: Sécuriser & chiffrer Mariadb - JDLL 2017

JDLL – 1er avril 2017

Développement

Page 13: Sécuriser & chiffrer Mariadb - JDLL 2017

JDLL – 1er avril 2017

✔ Pont entre les BDD relationnelles et non relationnelles

✔ Toutes les colonnes stockées dans un « blob »

✔ Possibilité de le manipuler

✔ Possibilité de créer des index Virtuels

Colonnes Dynamiques

Page 14: Sécuriser & chiffrer Mariadb - JDLL 2017

JDLL – 1er avril 2017

create table newsql( 

id int 

    auto_increment        primary key,

nom varchar(40),

type enum 

     ('animal',

     'ordinateur'),

prix int,

nosql blob);

COLUMN_CREATE(

column_nr, 

value [as type],

[

column_nr, 

value [as type]

], 

...)

Exemple (colonne dynamique) 1/3

ID nom type Prix blob

Multi produits

Page 15: Sécuriser & chiffrer Mariadb - JDLL 2017

JDLL – 1er avril 2017

Exemple (colonne dynamique) 2/3

>INSERT into newsql values (NULL, 'Atari' ,'ordinateur',500, COLUMN_CREATE('couleur', 'Noir', 'type' , 'STe'));

>INSERT into newsql values (NULL, 'elePHPant','animal',20, COLUMN_CREATE('taille', 'normal', 'description' , 'peluche'));

>UPDATE newsql SET nosql = COLUMN_ADD(nosql, 'couleur' , 'blue') WHERE id=2;

ID Nom type prix couleur type taille description

1 Atari Ordinateur 500 Noir Ste

2 elePHPant animal 20 bleu normal peluche

>SELECT nom, column_list (nosql) FROM newsql;

Multi produits

Page 16: Sécuriser & chiffrer Mariadb - JDLL 2017

JDLL – 1er avril 2017

Exemple (colonne dynamique) 3/3

>SELECT nom, COLUMN_JSON(nosql) FROM newsql;

>SELECT id,type,nom,

COLUMN_GET(nosql, 'couleur' as char) AS couleur,prix FROM newsql;

Multi produits

Page 17: Sécuriser & chiffrer Mariadb - JDLL 2017

JDLL – 1er avril 2017

Moteur de stockagestorage engine

Page 18: Sécuriser & chiffrer Mariadb - JDLL 2017

JDLL – 1er avril 2017

Ouverture vers le NewSQL

● Moteur de stockage : CassandraSE

● Dispo MariaDB 10.0+

● Brique de Apache Cassandra

● Données non relationnelles (NoSQL)

● Assurer l'intégration des données SQL / NoSQL

● Mapping possible

● Manipulations :

static columns, dynamic columns, rowkey ● Installation dans My.cnf

[mysqld]

plugin-load=ha_cassandra.so

Page 19: Sécuriser & chiffrer Mariadb - JDLL 2017

JDLL – 1er avril 2017

CassandraSE : Utilisation (1/2)

> create table t2 (rowkey varchar(36) primary key,

data1 varchar(60),data2 varchar(60)) engine=cassandrakeyspace='mariadbtest'thrift_host='localhost'column_family='cf1';

Clé primaire obligatoire

Colonnes statiques

Sous ensemble des colonnes CF

Page 20: Sécuriser & chiffrer Mariadb - JDLL 2017

JDLL – 1er avril 2017

CassandraSE : Utilisation (2/2)

> insert into t1 values

('rowkey10', 'data1-value', 123456),

('rowkey11', 'data1-value2', 34543);

> select * from t1 where rowkey='rowkey11';

Page 21: Sécuriser & chiffrer Mariadb - JDLL 2017

JDLL – 1er avril 2017

Manipuler les données : Connect

● Dispo MariaDB 10.0+

● ETL : Extract, transform et Load

● Lecture / Ecriture / MAJ

– TXT, DBF, INI, XML

– MS Access, MS Excel, TBL (similaire à MERGE)

– ODBC, MySQL,SQLite, Oracle, DB2, WMI…

– NoSQL : MongoDB● Locale ou distant

● Custom possible

● Accès en parallèle sur multitables

● Installation dans My.cnf

[mysqld]

plugin-load=ha_connect.so

Page 22: Sécuriser & chiffrer Mariadb - JDLL 2017

JDLL – 1er avril 2017

Connect : exemple CSV

● Fichier

– first,last,birthday

– "Christophe","Villeneuve","Jan 1"

– "PHP","ODBC","Nov 22"

> CREATE TABLE users_csv (

first varchar(32) NOT NULL,

last varchar(32) NOT NULL,

birthday varchar(50) NOT NULL

) ENGINE=CONNECT TABLE_TYPE =CSV FILE_NAME ='/var/lib/mysql/users.csv' HEADER=1 SEP_CHAR=',' QUOTED=1;

> SELECT * FROM users_csv;

| first | last | birthday |

+---------------+---------------+-------------+

| Christophe | Villeneuve | Jan 1 |

| PHP | ODBC | Nov 22 |

+---------------+---------------+-------------+

Page 23: Sécuriser & chiffrer Mariadb - JDLL 2017

JDLL – 1er avril 2017

Connect : exemple ODBC

> create Table table2

ENGINE=CONNECT TABLE_TYPE=ODBC

SRCDEF='select key, sum(l_quantity) qt from dbt3.table1 group by key'

CONNECTION='DSN=oracle;UID=login;PWD=password';

> MariaDB [dbt3]> select sum(qt) from (select qt from table2) as result;

→ 31444 Valeur de sum(qt)

Page 24: Sécuriser & chiffrer Mariadb - JDLL 2017

JDLL – 1er avril 2017

Connect : exemple MongoDB (1/2)

● Driver : ha_connect.so

● Méthode ODBC

– > mongoexport

–db test

–collection restaurants

–out

/var/lib/mysql/test/restaurants.json

–jsonArray

https://github.com/NYUITP/sp13_10g

Page 25: Sécuriser & chiffrer Mariadb - JDLL 2017

JDLL – 1er avril 2017

Connect : exemple MongoDB (2/2)

● Avec MariaDB

{“_id” : ObjectId(“580cda5aaf1de8b908ccfe40”),“address” : { “building” : “522”, “coord” : [ -73.95171, 40.767461 ], “street” : “East 74 Street”, “zipcode” : “10021” },“borough” : “Manhattan”,“cuisine” : “American”,“grades” : [ { “date” : ISODate(“2014-09-02T00:00:00Z”), “grade” : “A”, “score” : 12 }, { “date” : ISODate(“2013-12-19T00:00:00Z”), “grade” : “B”, “score” : 16 }, { “date” : ISODate(“2013-05-28T00:00:00Z”), “grade” : “A”, “score” : 9 }, { “date” : ISODate(“2012-12-07T00:00:00Z”), “grade” : “A”, “score” : 13 }, { “date” : ISODate(“2012-03-29T00:00:00Z”), “grade” : “A”, “score” : 11 } ],“name” : “Glorious Food”,“restaurant_id” : “40361521”}

> SELECT proprietors.owner, restaurants.name, grades_grade FROM proprietors, restaurants WHERE proprietors.restaurant_id=restaurants.restaurant_id AND grades_date>‘2015-01-08’ AND grades_grade IN(‘A’,‘B’);

+———————+————————————————+————–+| owner | name | grades_grade |+———————+————————————————+————–+| Treva Tullis | Bagels N Buns | A || Maud Morningstar | La Fusta Restaurant | A || Vanessa Vowell | The Greek Corner | A || Teressa Truex | Mcdonald’S | A || Eli Enright | Neron’S Pizza | A

● Avec MongoDB

Page 26: Sécuriser & chiffrer Mariadb - JDLL 2017

JDLL – 1er avril 2017

Storage Engine : ColumnStore

BigData

Page 27: Sécuriser & chiffrer Mariadb - JDLL 2017

JDLL – 1er avril 2017

MariaDB ColumnStore

● Portage de la Base de données : InfiniDB 4.6.7● Disponible à partir de MariaDB 10.1● Moteur de stockage orienté en colonnes● Architecture (en parallèle)

– Exécution de requêtes distribuée

– Chargement de données

● Licence GPL

Page 28: Sécuriser & chiffrer Mariadb - JDLL 2017

JDLL – 1er avril 2017

Axée sur la rangée VS Orienté Column

● Axée sur la rangée– Les Lignes stockées séquentiellement dans un fichier

● Orienté colonne– Chaque colonne est stockée dans un fichier séparé

Page 29: Sécuriser & chiffrer Mariadb - JDLL 2017

JDLL – 1er avril 2017

Pourquoi ?

● Traitement des 'petabytes' de données● Utilisation

– Temps de réponse en temps réel ● aux requêtes analytiques

– Traitement par lots → Batch

– Algorithmique

● Sécurité– Bénéficie des mêmes fonctionnalités de sécurité

existante

– Cryptage sur les données en mouvement

Page 30: Sécuriser & chiffrer Mariadb - JDLL 2017

JDLL – 1er avril 2017

Réplication (en temps réel)

● MariaDB MaxScale– Maitre(s) / Eclave(s)

● Réplications les transactions – MariaDB → Hadoop

● Pas d'impact – Sur les performances

– Sur les métadonnées

Page 31: Sécuriser & chiffrer Mariadb - JDLL 2017

JDLL – 1er avril 2017

Architecture

● Moteur utilisateur : précompte les requêtes SQL● Module de performance : moteur de traitement

distribué

(C) MariaDB

Page 32: Sécuriser & chiffrer Mariadb - JDLL 2017

JDLL – 1er avril 2017

Sécuriser

Page 33: Sécuriser & chiffrer Mariadb - JDLL 2017

JDLL – 1er avril 2017

Lors de l'installation

> mysql_secure_installation

Enter current password for root (enter for none): ← Entrée

Set root password? [Y/n] ← y

New password: ← saisir nouveau Mot de passe

Re-enter new password: ← resaisir Mot de passe

Remove anonymous users? [Y/n] ← y

Disallow root login remotely? [Y/n] ← y

Reload privilege tables now? [Y/n] ← y

Page 34: Sécuriser & chiffrer Mariadb - JDLL 2017

JDLL – 1er avril 2017

Les connexions 'SSL / TLS' sécurisés disponibles

● Connexions client / serveur → protocole TLS– Les données peuvent être cryptées pendant le transfert à

l'aide du protocole TLS

● Variable système SSL / TLS● Variable d'état SSL / TLS● Réplication avec connexions sécurisées

– Maître(s) / Esclave(s) avec des données cryptées

Page 35: Sécuriser & chiffrer Mariadb - JDLL 2017

JDLL – 1er avril 2017

Connexion 'SSL / TLS'

● Par défaut – Connexion non chiffré

● Si le serveur prend en charge les connexions sécurisées

→ OUI

● Pour activer TLS

– Démarrer le serveur avec l'option --ssl

> SHOW VARIABLES LIKE 'have_ssl';+---------------+----------+| Variable_name | Value |+---------------+----------+| have_ssl | DISABLED |+---------------+----------+

https://mariadb.com/kb/en/mariadb/secure-connections-overview/

Page 36: Sécuriser & chiffrer Mariadb - JDLL 2017

JDLL – 1er avril 2017

● Cryptage de données● Transfert TLS● Exemple

– have_openssl

– have_ssl

● Interchangeable SSL● SSL

n'est plus considéré comme sécurisé

● Exemple– ssl_ca

– ssl_capath

– ...

Options 'SSL / TLS'

Système/Status de variables Attention

https://mariadb.com/kb/en/mariadb/ssltls-system-variables/

https://mariadb.com/kb/en/mariadb/ssltls-status-variables/

Page 37: Sécuriser & chiffrer Mariadb - JDLL 2017

JDLL – 1er avril 2017

Réplication

https://mariadb.com/kb/en/mariadb/replication-with-secure-connections/

● Maître(s) / Esclave(s)● Répliqués des données cryptées● Activation des connexions sécurisées (obligatoire)● Cluster

AttentionSi un des serveurs n'est pas configuré pour les connexions sécurisés → les connexions ne seront pas cryptées

Page 38: Sécuriser & chiffrer Mariadb - JDLL 2017

JDLL – 1er avril 2017

Réplication : exemple (1/3)

● Création SSL dans chaque serveursudo mkdir -p /etc/mariadb/ssl

● Gestion des certificats$ sudo openssl genrsa 4096 > ca-key.pem

$ sudo openssl req -new -x509 -nodes -days 3600 -key ca-key.pem -out ca-cert.pem

● Créer un certificat de serveur sur serveur1$ sudo openssl req -newkey rsa:4096 -days 3600 -nodes -keyout server-key.pem -out server-req.pem

$ sudo openssl rsa -in server-key.pem -out server-key.pem

$ sudo openssl x509 -req -in server-req.pem -days 3600 -CA ca-cert.pem -CAkey ca-key.pem -set_serial 01 -out server-cert.pem

Page 39: Sécuriser & chiffrer Mariadb - JDLL 2017

JDLL – 1er avril 2017

Réplication : exemple (2/3)

● Vérification des certificats (serveur1)sudo openssl verify -CAfile ca-cert.pem server-cert.pem client-cert.pem

● Résultatserver-cert.pem: OK

client-cert.pem: OK

● Copier le certificat vers les autres serveurssudo scp /etc/mariadb/ssl/*.pem [email protected]:/etc/mariadb/ssl/

● Installation MariaDBsudo apt-get install mariadb mariadb-server -y

Page 40: Sécuriser & chiffrer Mariadb - JDLL 2017

JDLL – 1er avril 2017

Réplication : exemple (3/3)

● Configuration de my.cnf – pour chaque serveur

# SSLssl-ca=/etc/mariadb/ssl/ca-cert.pem

ADD under [mysqld]:# SSLssl-ca=/etc/mariadb/ssl/ca-cert.pemssl-cert=/etc/mariadb/ssl/server-cert.pemssl-key=/etc/mariadb/ssl/server-key.pem

CHANGE under [mysqld]:server-id = 1log-bin=mysql-binbinlog_format=mixed

Pas de modifications (processus identique) pour :La réplication / MariaDB Galera Cluster

Page 41: Sécuriser & chiffrer Mariadb - JDLL 2017

JDLL – 1er avril 2017

Le chiffrement

Page 42: Sécuriser & chiffrer Mariadb - JDLL 2017

JDLL – 1er avril 2017

Pourquoi chiffrer ?

● Avec MySQL / MariaDB / Percona

● Mariadb → Chiffrer au niveau des tables– Innodb

– XtraDB (Percona)

$ INSERT INTO users (username, password) VALUES ('root', AES_ENCRYPT('somepassword', 'key12346123'));

$ SELECT AES_DECRYPT(password, 'key12346123') FROM users WHERE username = 'admin';

Page 43: Sécuriser & chiffrer Mariadb - JDLL 2017

JDLL – 1er avril 2017

Chiffrement (1/2)

● Disponible pour 10.1+● Contribution Google● Chiffrer

– Pas de modification pour vos applications

– Pas de dégradation des performances globales

– Une meilleure pratique en matière de sécurité de données

● Storage engine supporté– XtraDB

– InnoDB

Page 44: Sécuriser & chiffrer Mariadb - JDLL 2017

JDLL – 1er avril 2017

Chiffrement (2/2)

● Chiffrement : support de clefs (AES Algorithm)

table individuel → PAGE_ENCRYPTION=1

tablespace encryption → Crypte tout ainsi que les logs

● Plugin : file_key_management– Plugin de cryptage

– Lit les clés de cryptage d'un fichier

– Options : ● File_key_management_filename

→ Où se trouve le fichier

● File_key_management_filekey → Clé facultative pour déchiffrer le fichier de clés

● File_key_management_encryption_algorithm → Algorithme de cryptage à utiliser.

https://mariadb.com/kb/en/mariadb/data-at-rest-encryption/

Page 45: Sécuriser & chiffrer Mariadb - JDLL 2017

JDLL – 1er avril 2017

Avantages

● Par rapport à d'autres BDD Open Source– Chiffrement des données au repos

. Chiffrement au niveau des instances et des tables

. Avec support des clés tournantes (contribution Google)

– Validation des mots de passe

– Contrôle d'accès de base de profils (par défaut)

. Renforcé et optimisé

● Type de chiffrement supporté– Clefs de chiffrement identifier (32 bit)

– Advanced Encryption Standard (AES) 128 / 192 / 256 bit

– Exemple :$ openssl enc -aes-256-cbc -md sha1 -k secret -in keys.txt -out keys.enc

Page 46: Sécuriser & chiffrer Mariadb - JDLL 2017

JDLL – 1er avril 2017

config MariaDB : my.cnf

[mysqld]

plugin-load-add=file_key_management.sofile-key-management file-key-management-filename = /home/mdb/keys.enc innodb-encrypt-tables innodb-encrypt-log innodb-encryption-threads=4 aria-encrypt-tables=1 # PAGE row format encrypt-tmp-disk-tables=1 # this is for Aria

Page 47: Sécuriser & chiffrer Mariadb - JDLL 2017

JDLL – 1er avril 2017

Chiffrer une table

CREATE TABLE client ( client_id bigint not null primary key, client_name varchar(80), client_infosensible varchar(20)) ENGINE=InnoDB page_encryption=1 page_encryption_key=1;

Clef 1

Chiffrement 1

Information très sensiblePassword...

● En mode fixe

Page 48: Sécuriser & chiffrer Mariadb - JDLL 2017

JDLL – 1er avril 2017

Chiffrer : mode avancé

● Tablespace encrypton (Google)● Choisir un algorithme de cryptage● Spécifier ce qui doit être chiffrer

– Innodb-encrypt-tables

– Aria

– Aria-encrypt-tables

– Crypter-tmp-disk-tables

– Innodb-encrypt-log

● Prévoir les rotations (impératif)– innodb-encryption-threads = 4

– innodb-encryption-rotate-key-age = 1800

Page 49: Sécuriser & chiffrer Mariadb - JDLL 2017

JDLL – 1er avril 2017

Validation du mot de passe

● Les rappels du mot de passe● MariaDB propose : Validation de mot de passe

– S'assurer que les mots de passe 'utilisateur'

répondent à certaines exigences de sécurité

– Plugin dédiée

– Contrôle par rapport à la configuration ● Fonctions : SET PASSWORD et GRAND

https://mariadb.com/kb/en/mariadb/password-validation/

Page 50: Sécuriser & chiffrer Mariadb - JDLL 2017

JDLL – 1er avril 2017

Exemple (1/2)

SET PASSWORD = PASSWORD('plain-text password');SET PASSWORD FOR `user`@`host` = PASSWORD('plain-text password');SET PASSWORD = OLD_PASSWORD('plain-text password');SET PASSWORD FOR `user`@`host` = OLD_PASSWORD('plain-text password');CREATE USER `user`@`host` IDENTIFIED BY 'plain-text password';GRANT privileges TO `user`@`host` IDENTIFIED BY 'plain-text password';

https://mariadb.com/kb/en/mariadb/password-validation/

SET PASSWORD = 'password hash';SET PASSWORD FOR `user`@`host` = 'password hash';CREATE USER `user`@`host` IDENTIFIED BY PASSWORD 'password hash';CREATE USER `user`@`host` IDENTIFIED VIA mysql_native_password USING 'password hash';CREATE USER `user`@`host` IDENTIFIED VIA mysql_old_password USING 'password hash';GRANT privileges TO `user`@`host` IDENTIFIED BY PASSWORD 'password hash';GRANT privileges TO `user`@`host` IDENTIFIED VIA mysql_native_password USING 'password hash';GRANT privileges TO `user`@`host` IDENTIFIED VIA mysql_old_password USING 'password hash';

● Avant

● Avec clé de hash

Page 51: Sécuriser & chiffrer Mariadb - JDLL 2017

JDLL – 1er avril 2017

Exemple (2/2)

● Vérification

● Erreur de validation

$ UPDATE mysql.user SET password='password hash' WHERE user='user' AND host='host';

$ FLUSH PRIVILEGES;

SUCCES

> grant select on *.* to foobar identified by 'toto';ERROR HY000: Your password does not satisfy the current policy requirements

> show warnings;

Page 52: Sécuriser & chiffrer Mariadb - JDLL 2017

JDLL – 1er avril 2017

En résumé

Page 53: Sécuriser & chiffrer Mariadb - JDLL 2017

JDLL – 1er avril 2017

Merci

QuestionsChristophe Villeneuve@hellosct1