Security is one of the most important elements of the properly designed database environment. There are numerous attack vectors used with SQL injection being probably the most popular one. You can design layers of defence in the application code but what can you do on the database layer? Today we would like to show you how easily you can implement SQL firewall on top of MySQL using ProxySQL. In the second part of this blog we will explain how you can create a whitelist of queries that are allowed to access the database.
Define where to deploy it, you can either pick existing host in the cluster or just write down any IP or hostname. Set credentials for administrative and monitoring users.
Then you can create a new user in the database to be used with ProxySQL or you can import one of the existing ones. You also need to define the database nodes you want to include in the ProxySQL. Answer if you use implicit transactions or not and you are all set to deploy ProxySQL. In a couple of minutes a ProxySQL with configuration prepared based on your input is ready to use.
Given our issue is security, we want to be able to tell ProxySQL how to handle inappropriate queries. Let’s take a look at the query rules, the core mechanism that governs how ProxySQL handles the traffic that passes through it. The list of query rules may look like this:
They are being applied from the lowest ID onwards.
Let’s try to create a query rule which will allow only SELECT queries for a particular user:
We are adding a query rule at the beginning of the rules list. We are going to match anything that is not SELECTs (please note Negate Match Pattern is enabled). The query rule will be used only when the username is ‘devuser’. If all the conditions are matched, the user will see the error as in the “Error Msg” field.
[email protected]:~# mysql -u devuser -h 10.0.0.144 -P6033 -ppass mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or g. Your MySQL connection id is 3024 Server version: 5.5.30 (ProxySQL) Copyright (c) 2009-2019 Percona LLC and/or its affiliates Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or 'h' for help. Type 'c' to clear the current input statement. mysql> create schema myschema; ERROR 1148 (42000): The query is not allowed mysql> SELECT 1; +---+ | 1 | +---+ | 1 | +---+ 1 row in set (0.01 sec) mysql> SELECT * FROM sbtest.sbtest1 LIMIT 1G *************************** 1. row *************************** id: 1 k: 503019 c: 18034632456-32298647298-82351096178-60420120042-90070228681-93395382793-96740777141-18710455882-88896678134-41810932745 pad: 43683718329-48150560094-43449649167-51455516141-06448225399 1 row in set (0.00 sec)
Another example, this time we will try to prevent accidents related to the Bobby Tables situation.
With this query rule in place, your ‘students’ table won’t be dropped by Bobby:
mysql> use school; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> INSERT INTO students VALUES (1, 'Robert');DROP TABLE students;-- Query OK, 1 row affected (0.01 sec) ERROR 1148 (42000): Only superuser can execute DROP TABLE;
As you can see, Bobby was not able to remove our ‘students’ table. He was only nicely inserted into the table.