Configuring SQL/Protect v16
You can configure how SQL/Protect operates.
Meet the following prerequisites before configuring SQL/Protect:
The library file (
sqlprotect.dllon Windows) needed to run SQL/Protect is installed in the
libsubdirectory of your EDB Postgres Advanced Server home directory. For Windows, the EDB Postgres Advanced Server installer does this. For Linux, install the
edb-as<xx>-server-sqlprotectRPM package, where
<xx>is the EDB Postgres Advanced Server version number.
You need the SQL script file
sqlprotect.sqllocated in the
share/contribsubdirectory of your EDB Postgres Advanced Server home directory.
You must configure the database server to use SQL/Protect, and you must configure each database that you want SQL/Protect to monitor:
- You must modify the database server configuration file
postgresql.confby adding and enabling configuration parameters used by SQL/Protect.
- Install database objects used by SQL/Protect in each database that you want SQL/Protect to monitor.
- You must modify the database server configuration file
- Edit the following configuration parameters in the
postgresql.conffile located in the
datasubdirectory of your EDB Postgres Advanced Server home directory:
$libdir/sqlprotectto the list of libraries.
edb_sql_protect.enabled. Controls whether SQL/Protect is actively monitoring protected roles by analyzing SQL statements issued by those roles and reacting according to the setting of
edb_sql_protect.level. When you're ready to begin monitoring with SQL/Protect, set this parameter to
on. The default is
edb_sql_protect.level. Sets the action taken by SQL/Protect when a SQL statement is issued by a protected role. The default behavior is
passive. Initially, set this parameter to
learn. See Setting the protection level for more information.
edb_sql_protect.max_protected_roles. Sets the maximum number of roles to protect. The default is
edb_sql_protect.max_protected_relations. Sets the maximum number of relations to protect per role. The default is
The total number of protected relations for the server is the number of protected relations times the number of protected roles. Every protected relation consumes space in shared memory. The space for the maximum possible protected relations is reserved during database server startup.
edb_sql_protect.max_queries_to_save. Sets the maximum number of offending queries to save in the
edb_sql_protect_queriesview. The default is
5000. If the number of offending queries reaches the limit, additional queries aren't saved in the view but are accessible in the database server log file.
The minimum valid value for this parameter is
100. If you specify a value less than
100, the database server starts using the default setting of
5000. A warning message is recorded in the database server log file.
This example shows the settings of these parameters in the
- After you modify the
postgresql.conffile, restart the database server.
On Linux: Invoke the EDB Postgres Advanced Server service script with the
On a Redhat or CentOS 7.x installation, use the command:
On Windows: Use the Windows Services applet to restart the service named
- For each database that you want to protect from SQL injection attacks, connect to the database as a superuser (either
postgres, depending on your installation options). Then run the script
sqlprotect.sql, located in the
share/contribsubdirectory of your EDB Postgres Advanced Server home directory. The script creates the SQL/Protect database objects in a schema named
This example shows the process to set up protection for a database named
After you create the SQL/Protect database objects in a database, you can select the roles for which to monitor SQL queries for protection and the level of protection to assign to each role.
For each database that you want to protect, you must determine the roles you want to monitor and then add those roles to the protected roles list of that database.
- Connect as a superuser to a database that you want to protect with either
psqlor the Postgres Enterprise Manager client:
- Since the SQL/Protect tables, functions, and views are built under the
sqlprotectschema, use the
SET search_pathcommand to include the
sqlprotectschema in your search path. Doing so eliminates the need to schema-qualify any operation or query involving SQL/Protect database objects:
You must add each role that you want to protect to the protected roles list. This list is maintained in the table
To add a role, use the function
protect_role('rolename'). This example protects a role named
You can list the roles that were added to the protected roles list with the following query:
A view is also provided that gives the same information using the object names instead of the object identification numbers (OIDs):
edb_sql_protect.level configuration parameter sets the protection level, which defines the behavior of SQL/Protect when a protected role issues a SQL statement. The defined behavior applies to all roles in the protected roles lists of all databases configured with SQL/Protect in the database server.
You can set the
edb_sql_protect.level configuration parameter in the
postgresql.conf file to one of the following values to specify learn, passive, or active mode:
learn. Tracks the activities of protected roles and records the relations used by the roles. Use this mode when first configuring SQL/Protect so the expected behaviors of the protected applications are learned.
passive. Issues warnings if protected roles are breaking the defined rules but doesn't stop any SQL statements from executing. This mode is the next step after SQL/Protect learns the expected behavior of the protected roles. It essentially behaves in intrusion detection mode. You can run this mode in production when proper monitoring is in place.
active. Stops all invalid statements for a protected role. This mode behaves as a SQL firewall, preventing dangerous queries from running. This approach is particularly effective against early penetration testing when the attacker is trying to find the vulnerability point and the type of database behind the application. Not only does SQL/Protect close those vulnerability points, it tracks the blocked queries. This tracking can alert administrators before the attacker finds another way to penetrate the system.
The default mode is
If you're using SQL/Protect for the first time, set
After you configure SQL/Protect in a database, add roles to the protected roles list, and set the desired protection level, you can activate SQL/Protect in
active mode. You can then start running your applications.
With a new SQL/Protect installation, the first step is to determine the relations that protected roles are allowed to access during normal operation. Learn mode allows a role to run applications during which time SQL/Protect is recording the relations that are accessed. These are added to the role’s protected relations list stored in table
Monitoring for protection against attack begins when you run SQL/Protect in passive or active mode. In passive and active modes, the role is permitted to access the relations in its protected relations list. These are the specified relations the role can access during typical usage.
However, if a role attempts to access a relation that isn't in its protected relations list, SQL/Protect returns a
ERROR severity-level message. The role’s attempted action on the relation might not be carried out, depending on whether the mode is passive or active.
To activate SQL/Protect in learn mode:
- Set the parameters in the
postgresql.conffile. From the EDB Postgres Advanced Server application menu, select Reload Configuration > Expert Configuration.
For an alternative method of reloading the configuration file, use the
pg_reload_conffunction. Be sure you're connected to a database as a superuser, and execute
Allow the protected roles to run their applications.
For example, the following queries are issued in the
psqlapplication by the protected role
SQL/Protect generates a
NOTICE severity-level message, indicating the relation was added to the role’s protected relations list.
In SQL/Protect learn mode, SQL statements that are cause for suspicion aren't prevented from executing. However, a message is issued to alert the user to potentially dangerous statements:
- As a protected role runs applications, you can query the SQL/Protect tables to see that relations were added to the role’s protected relations list. Connect as a superuser to the database you're monitoring, and set the search path to include the
edb_sql_protect_rel table to see the relations added to the protected relations list:
list_protected_rels view provides more comprehensive information along with the object names instead of the OIDs:
After a role’s applications have accessed all relations they need, you can change the protection level so that SQL/Protect can actively monitor the incoming SQL queries and protect against SQL injection attacks.
Passive mode is a less restrictive protection mode than active.
- To activate SQL/Protect in passive mode, set the following parameters in the
Reload the configuration file as shown in Step 2 of Learn mode.
Now SQL/Protect is in passive mode. For relations that were learned, such as the
emptables of the prior examples, SQL statements are permitted. No special notification to the client by SQL/Protect is required, as shown by the following queries run by user
SQL/Protect doesn't prevent any SQL statement from executing. However, it issues a message of
WARNING severity level for SQL statements executed against relations that weren't learned. It also issues a warning for SQL statements that contain a prohibited signature:
Monitor the statistics for suspicious activity.
By querying the view
edb_sql_protect_stats, you can see the number of times SQL statements executed that referenced relations that weren't in a role’s protected relations list or contained SQL injection attack signatures.
The following is a query on
View information on specific attacks.
By querying the
edb_sql_protect_queriesview, you can see the SQL statements that were executed that referenced relations that weren't in a role’s protected relations list or that contained SQL injection attack signatures.
The following code sample shows a query on
port columns don't return any information if the attack originated on the same host as the database server using the Unix-domain socket (that is,
pg_hba.conf connection type
In active mode, disallowed SQL statements are prevented from executing. Also, the message issued by SQL/Protect has a higher severity level of
ERROR instead of
- To activate SQL/Protect in active mode, set the following parameters in the
- Reload the configuration file as shown in Step 2 of Learn mode.
This example shows SQL statements similar to those given in the examples of Step 2 in Passive mode. These statements are executed by the user
edb_sql_protect.level is set to
The following shows the resulting statistics:
The following is a query on