Advanced SQL Scripts To Assure The Utmost DB Security
Advanced SQL Scripts To Assure The Utmost DB Security
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.
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.
Second: Create sp_dbcc_check_permissions
@dbname = NULL
AS
BEGIN
IF @dbname IS NULL
BEGIN
SELECT schema_name
FROM sys.schemas
ORDER BY schema_name;
BEGIN
END;
END;
This stored procedure performs a security scan against the database. The first thing the stored
procedure does is check the permissions on the dbo schema. This is because the dbo schema is
the default schema for the database, and it contains the most important tables in the database. If
the permissions on the dbo schema are not secure, then anyone with access to the database can
potentially read, modify, or delete data in the database.
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
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
-- 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 'CREATE SYNONYM "' || a.object_name || '" FOR "' || a.owner || '"."' || a.object_name
|| '";'
FROM all_objects a
WHERE a.object_type IN ('PACKAGE','PROCEDURE','FUNCTION')
AND a.owner = UPPER('&1')
AND NOT EXISTS (SELECT '1'
FROM user_synonyms u
WHERE u.synonym_name = a.object_name
AND u.table_owner = UPPER('&1'));
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
SELECT 'CREATE SYNONYM "' || a.object_name || '" FOR "' || a.owner || '"."' || a.object_name
|| '";'
FROM all_objects a
WHERE a.object_type = 'SEQUENCE'
AND a.owner = UPPER('&1')
AND NOT EXISTS (SELECT '1'
FROM user_synonyms a1
WHERE a1.synonym_name = a.object_name
AND a1.table_owner = UPPER('&1'));
SPOOL OFF
SET PAGESIZE 14
SET FEEDBACK ON
SET VERIFY ON
SET PAGESIZE 0
SET FEEDBACK OFF
SET VERIFY OFF
SPOOL temp.sql
SELECT 'CREATE SYNONYM "' || a.table_name || '" FOR "' || a.owner || '"."' || a.table_name ||
'";'
FROM all_tables a
WHERE NOT EXISTS (SELECT '1'
FROM user_synonyms u
WHERE u.synonym_name = a.table_name
AND u.table_owner = UPPER('&1'))
AND a.owner = UPPER('&1');
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
SELECT 'CREATE SYNONYM "' || a.view_name || '" FOR "' || a.owner || '"."' || a.view_name ||
'";'
FROM all_views a
WHERE a.owner = UPPER('&1')
AND NOT EXISTS (SELECT '1'
FROM user_synonyms u
WHERE u.synonym_name = a.view_name
AND u.table_owner = UPPER('&1'));
SPOOL OFF
SET PAGESIZE 14
SET FEEDBACK ON
SET VERIFY ON