uptimejp / sql_firewall

SQL Firewall Extension for PostgreSQL
http://pgsnaga.blogspot.jp/2015/08/postgresql-sql-firewall.html
Other
175 stars 31 forks source link

sql_firewall

Overview

sql_firewall is a PostgreSQL extension which is intended to protect database from SQL injections or unexpected queries.

sql_firewall module learns queries which can be executed, and prevents/warns on executing queries which are not found in the learned firewall rule.

How it works

sql_firewall can take one of four modes specified in sql_firewall.firewall parameter: "learning", "enforcing", "permissive" and "disabled".

In the "learning" mode, sql_firewall collects pairs of "userid" and "queryid" associated with the executed queries. "queryid" is calculated based on a parse tree, similar to pg_stat_statements.

In the "enforcing" mode, sql_firewall checks whether queries are in the list of collected pairs of "userid" and "queryid", the firewall rules. When a query not in the firewall rules comes in, sql_firewall produces an error with the message to prevent execution.

In the "permissive" mode, sql_firewall checks queries as well, but allows to execute even not in the firewall rules. And produces warnings if the queries are not in the rules.

Compatibility

sql_firewall supports PostgreSQL 9.4.x. Other major versions would be supported in the future release.

Exported rule files would not be compatible between different PostgreSQL major versions, because queryid is calculated from the internal data structure (the Query structure) which is different in different major versions.

Installation

sql_firewall can be built as a PostgreSQL extension.

$ export PATH=$PGHOME/bin:$PATH
$ export USE_PGXS=1
$ make
$ sudo make install

Configuration

 $ vi $PGDATA/postgresql.conf
 <add>
 shared_preload_libraries = 'sql_firewall'
 sql_firewall.firewall = 'learning'
 </add>
 $ pg_ctl -D $PGDATA restart
 $ psql mydb
 mydb=# create extension sql_firewall;

sql_firewall would check all queries incoming to not only the specific database where the module is installed, but all the databases in the entire PostgreSQL cluster.

Even though, the views and functions in the module would be available only on the installed database.

GUC Parameters

Functions

Views

Examples

Authors

Satoshi Nagayasu snaga@uptime.jp