Koletzke Lock It Down PLSQL Security Features
Koletzke Lock It Down PLSQL Security Features
Lock it Down:
PL/SQL Security Features for
Code Assertion, VPD, and We will bankrupt ourselves
Encryption in the vain search for
absolute security.
Peter Koletzke
Technical Director & —Dwight David Eisenhower,
Principal Instructor (1890–1969)
Scope Agenda
• In scope – 10g &11g PL/SQL techniques: • Introduction
– Data access A bit about 12c
– Functional access later, if time • Locking down application functions
– Encryption • Locking down data
• Not in scope - security techniques:
– OS, web or application server, file systems • New 12c features
– Denial of service (DOS) • Further study
– Cross-site Scripting (XSS)
– Parameter manipulation, session hijacking,
buffer overflow
3 4
Security Planning General Safeguards
• Need to develop test plans early on, • Application roles and privileges
design into system – Users in roles
– Who can do what, who can see what – Roles granted to application functions
– Application security fits into an overall plan • Application uses a non-privileged “user”
• Risk/exposure/safeguards account
• Test roles, functions, data – Not application table owner account
• Coding standards – User account cannot do DDL
• How not to handle security: roll your • Restrict access to DML (including SELECT)
to application tables
own
• Think about how users could foil safeguards
– Better: use prebuilt techniques
– For example, SQL injection
• Vendors have certified and tested them
5 6
• Further study
7 8
Simple Example “Bad” User Does This
• Search form field for “Last Name” • Fills in Last Name field as:
– Example query to process the field '||last_name; DELETE FROM employees
SELECT email, first_name WHERE 1=1||'
FROM employees • SQL becomes
WHERE last_name ='||F_LastName||'; SELECT email, first_name
• User is supposed to enter a name like FROM employees
WHERE last_name = ''|| last_name;
“Hunold”
DELETE FROM employees WHERE 1=1||'';
SELECT email, first_name
FROM employees
WHERE last_name = 'Hunold';
9 10
17 18
19 20
Data Filtering How To: Data Filtering 8i+
• Objective • Virtual Private Database (VPD)
– Users should see and interact with only the – A.k.a., Fine-grained Access Control (FGAC)
data they should see and interact with
• Not to be confused with Fine Grain Auditing (FGA)
• More granular than table grants to roles or
users – F.k.a., Row-Level Security (RLS) Oracle
Enterprise
• Examples – Implemented through DBMS_RLS Edition only
– HARRY can only see employee data from • Components
department 10 – Package function to return a WHERE clause
– AHUNOLD can only see employee data from
departments 60 – Policy object (created with DBMS_RLS) for a
– NKOCCHAR can only see employee data table
from department 100 • Associates function to table Grant to EXECUTE on
DBMS_RLS needed
21 22
23 24
About the Filtering Function Filtering Function
FUNCTION emp_pol(
obj_schema IN VARCHAR2,
• Logic that returns a predicate (WHERE obj_name IN VARCHAR2)
clause component); examples: IS
RETURN VARCHAR2
25 26
4
Results
27 28 28
Variation: Application Context Writing to Application Context
• Accessible memory variable area for • DBMS_SESSION.SET_CONTEXT(context_
each session; has a name 8i+ name, variable_name, value)
– Like SYS_CONTEXT('USERENV', – Can only store VARCHAR2 variables
'SESSION_USER') – Must be in context’s package
• USERENV is the context area
• For example,
• Secure
– Memory variables only available to session – ON_LOGON trigger can store username
for use in policy
– Can only be updated through a single
PL/SQL package – Web application writes the logged in app
CREATE CONTEXT hr_context user into the context
USING policy_pkg; • Uses the same database user for database
login
29 30
31 32
Process with App Context Application Security Policy Steps
ON_LOGON trigger OR
web application code
harry 2 harry 1. The application saves the logged-in
1
user name to the “application context”
Application SELECT *
Context
harry
FROM employees 2. The application issues the query.
Application 3
Security Tables 3 3. Database policy code adds a WHERE
VPD Policy clause predicate based on the user
Code name in the app context and security
4 table entry
Application 4 SELECT *
Data Tables
FROM employees 4. The query is run.
WHERE dept_no IN (60,90)
5. Results return to the application.
5
Results
33 34 34
35 36
Big Note Techniques for Protecting Data
• VPD policy always • Data filtering
applied EXCEPT to: – DBMS_RLS package
– SYS or any user • Data hiding (masking)
connected "as – DBMS_RLS package
sysdba" – DBMS_REDACT package
– An account granted
• Encryption
EXEMPT ACCESS
POLICY – Oracle Transparent Data Encryption
– DBMS_OBFUSCATION_TOOLKIT and
DBMS_CRYPTO packages
37 38
41 42
45 46
49 50
• Removing encryption
ALTER TABLE employees MODIFY (
social_security_number DECRYPT);
51 52
DBMS_CRYPTO Custom Encryption Package
CREATE OR REPLACE PACKAGE BODY crypto_pkg
• NOT TRANSPARENT! AS
-- Key used for SYS.DBMS_CRYPTO calls.
– This is “roll your own” folks -- This would be queried from a table which would encrypt the
-- value using a second key. DO NOT embed a plain text string here.
– You need to write code for encrypting and g_crypto_key CONSTANT RAW(32) := 'ABCDEF';
decrypting --
-- Encryption type used for SYS.DBMS_CRYPTO calls
– You need to store the encryption key g_encryption_type CONSTANT PLS_INTEGER :=
somewhere -- Advanced Encryption Standard 256-bit key
SYS.DBMS_CRYPTO.ENCRYPT_AES256 +
• https://oracle-base.com/articles/9i/storing- -- Cipher block chaining mode
passwords-in-the-database-9i SYS.DBMS_CRYPTO.CHAIN_CBC +
-- Padding of Password-Based Cryptography
– DBMS_CRYPTO.RANDOMBYTES() can -- Standard #5
SYS.DBMS_CRYPTO.PAD_PKCS5;
generate the key
53 54
57 58
59 60
INHERIT PRIVILEGES ACCESSIBLE BY Clause
• All users have this automatically 12cR1+ • Defines the PL/SQL code that can 12cR1+
run for their account: access the package
GRANT INHERIT PRIVILEGES on USER <user> TO
PUBLIC; – CREATE FUNCTION
– Everyone can access everything with invoker’s – CREATE PACKAGE
rights as before – CREATE PROCEDURE
• Dave can revoke that – CREATE TYPE
REVOKE INHERIT PRIVILEGES on USER <user> TO – ALTER TYPE
PUBLIC;
CREATE OR REPLACE PACKAGE util_pkg
– Calling the preceding function would fail ACCESSIBLE BY (
• Then grant to a single dba, Sally PACKAGE emp_pkg,
GRANT INHERIT PRIVILEGES on USER dave TO
sally;
PACKAGE policy_pkg,
TRIGGER emp_tr)
61 62
63 64
Further Study PL/SQL Security Wall
• Steven Feuerstein – 12c new PL/SQL features
– http://www.oracle.com/technetwork/issue-archive/2013/13-
sep/o53plsql-1999801.html
• Tom Kyte – SQL injection
– http://tkyte.blogspot.com/2012/02/all-about-security-sql- DBMS_RLS DBMS_REDACT DBMS_ASSERT
injection.html
• Bryn Llewelyn - SQL injection
– http://www.oracle.com/technetwork/database/features/plsql/overvie Data filtering & hiding Data hiding Anti-SQL injection
w/how-to-write-injection-proof-plsql-1-129572.pdf
• Oracle Learning Library
– oracle.com/oll
• PL/SQL Challenge
– plsqlchallenge.oracle.com
• Live SQL
– http://www.oracle.com/technetwork/database/application- DBMS_CRYPTO DBMS_OBFUSCATION_TOOLKIT
development/livesql/index.html
• Practically Perfect PL/SQL – Feuerstein videos
– https://www.youtube.com/channel/UCpJpLMRm452kVcie3RpINPw Encryption Encryption
65 66
67 69