Must-Have SQL Security Scripts for ProDBA
Must-Have SQL Security Scripts for ProDBA
Asfaw Gedamu
4/23/2022
Must-Have SQL Security Scripts for Every Pro DBA
Caution: Please use the commands with
care, try them on test environments first.
The first line of code backs up a database to a file called backup.bak. The second line of code
restores a database from a file called backup.bak.
Implementing encryption
CREATE TABLE [table] (
[column] [datatype] ENCRYPTED WITH (ALGORITHM = 'AES_256',
COLUMN_ENCRYPTION_KEY = [key_id]);
);
The first line of code creates a new table with a column that is encrypted using the AES_256
algorithm. The second line of code specifies the key ID that will be used to encrypt the column.
The first line of code creates a trigger named trigger_name that fires after a row is inserted,
updated, or deleted from the table table. The second line of code prints a message to the console
when the trigger fires.
Scanning for security vulnerabilities
To conduct vulnerability scan against a database we have to create two stored procedures
sp_dbcc_check_security and sp_dbcc_check_permissions.
The above stored procedure performs a security scan against the database. The first thing the
stored procedure does is check the permissions on the master database. This is because the
master database contains the system tables, which are the most critical tables in the database. If
the permissions on the master database are not secure, then the entire database is vulnerable.
The next thing the stored procedure does is check the permissions on all other databases. This is
because if the permissions on a database are not secure, then anyone with access to the database
can potentially read, modify, or delete data in the database.
The stored procedure uses the sp_dbcc_check_permissions stored procedure to check the
permissions on the databases. The sp_dbcc_check_permissions stored procedure takes a single
parameter, the name of the database to check. The stored procedure returns a report of any
security vulnerabilities that are found.
The sp_dbcc_check_security stored procedure is a powerful tool that can be used to help you
secure your database. However, it is important to note that the stored procedure does not scan the
entire database for security vulnerabilities. The stored procedure only scans the permissions on
the databases. If you want to scan the entire database for security vulnerabilities, you will need to
use a different tool.
The next thing the stored procedure does is check the permissions on all other schemas. This is
because if the permissions on a schema are not secure, then anyone with access to the schema
can potentially read, modify, or delete data in the schema.
The stored procedure uses the sp_dbcc_check_permissions stored procedure to check the
permissions on the schemas. The sp_dbcc_check_permissions stored procedure takes two
parameters, the name of the database to check and the name of the schema to check. The stored
procedure returns a report of any security vulnerabilities that are found.
The sp_dbcc_check_permissions stored procedure is a powerful tool that can be used to help
you secure your database. However, it is important to note that the stored procedure does not
scan the entire database for security vulnerabilities. The stored procedure only scans the
permissions on the schemas. If you want to scan the entire database for security vulnerabilities,
you will need to use a different tool.
This script runs a security scan against the database. The first line of code, USE master;,
specifies that the script will run in the master database. The second line of code, GO, is a
delimiter that tells SQL Server to execute the previous line of code. The third line of code, EXEC
sp_dbcc_check_security;, executes the sp_dbcc_check_security stored procedure. This
stored procedure performs a security scan against the database and returns a report of any
security vulnerabilities that are found.
The sp_dbcc_check_security stored procedure takes a number of parameters, but the most
important one is the dbname parameter. This parameter specifies the name of the database that
you want to scan. In this case, the dbname parameter is not specified, so the stored procedure will
scan the master database.
The report also includes a link to the Microsoft website where you can learn more about the
vulnerability.
SET PAGESIZE 0
SET FEEDBACK OFF
SET VERIFY OFF
SPOOL temp.sql
SET PAGESIZE 14
SET FEEDBACK ON
SET VERIFY ON
SET PAGESIZE 0
SET FEEDBACK OFF
SET VERIFY OFF
SPOOL temp.sql
SPOOL OFF
-- Comment out following line to prevent immediate run
@temp.sql
SET PAGESIZE 14
SET FEEDBACK ON
SET VERIFY ON
SET PAGESIZE 0
SET FEEDBACK OFF
SET VERIFY OFF
SPOOL temp.sql
SELECT 'GRANT INSERT ON "' || u.table_name || '" TO &1;'
FROM user_tables u
WHERE NOT EXISTS (SELECT '1'
FROM all_tab_privs a
WHERE a.grantee = UPPER('&1')
AND a.privilege = 'INSERT'
AND a.table_name = u.table_name);
SPOOL OFF
-- Comment out following line to prevent immediate run
@temp.sql
SET PAGESIZE 14
SET FEEDBACK ON
SET VERIFY ON
SET PAGESIZE 0
SET FEEDBACK OFF
SET VERIFY OFF
SPOOL temp.sql
SPOOL OFF
-- Comment out following line to prevent immediate run
@temp.sql
SET PAGESIZE 14
SET FEEDBACK ON
SET VERIFY ON
SET PAGESIZE 0
SET FEEDBACK OFF
SET VERIFY OFF
SPOOL temp.sql
SPOOL OFF
SET PAGESIZE 14
SET FEEDBACK ON
SET VERIFY ON
Create package synonyms own by a user
Here, the script generates a SQL script that creates synonyms for all packages, procedures, and
functions owned by the current user for which there are no corresponding synonyms. The script
then executes the generated SQL script.
SET PAGESIZE 0
SET FEEDBACK OFF
SET VERIFY OFF
SPOOL temp.sql
SPOOL OFF
SET PAGESIZE 14
SET FEEDBACK ON
SET VERIFY ON
-- Direct grants
select distinct grantee
from dba_tab_privs
where privilege in ('INSERT', 'UPDATE', 'DELETE')
and owner = upper('&1')
union
-- Grants via a role
select distinct grantee
from dba_role_privs
join dba_users on grantee = username
where granted_role IN (select distinct role
from role_tab_privs
where privilege in ('INSERT', 'UPDATE',
'DELETE')
and owner = upper('&1')
union
select distinct role
from role_sys_privs
where privilege in ('INSERT ANY TABLE',
'UPDATE ANY TABLE', 'DELETE ANY TABLE'))
union
-- Access via ANY sys privileges
select distinct grantee
from dba_sys_privs
join dba_users on grantee = username
where privilege in ('INSERT ANY TABLE', 'UPDATE ANY TABLE',
'DELETE ANY TABLE');
SPOOL temp.sql
SPOOL OFF
SET PAGESIZE 14
SET FEEDBACK ON
SET VERIFY ON
SPOOL temp.sql
SPOOL OFF
SET PAGESIZE 14
SET FEEDBACK ON
SET VERIFY ON
Script: view_synonyms.sql
Use the SQL script below to create synonyms for all views in the schema that do not
already have a synonym. The script generates a SQL script that creates synonyms for all views
owned by the current user for which there are no corresponding synonyms. The script then
executes the generated SQL script.
SET PAGESIZE 0
SET FEEDBACK OFF
SET VERIFY OFF
SPOOL temp.sql
SPOOL OFF
SET PAGESIZE 14
SET FEEDBACK ON
SET VERIFY ON