Professional Documents
Culture Documents
Insight Consulting
Introduction
My name is Pete Finnigan
I specialise in researching and securing Oracle
databases
The PL/SQL wrapping process has particularly interested
me for some years
I wanted to investigate why the method chosen to secure
intellectual property written in PL/SQL is weak
I also felt it was intriguing that Oracle has made it easy for
anyone to understand how to recover source code in 9i and
lower
I also find it interesting that Oracle has shipped APIs since
the beginning of PL/SQL that can be used to unwrap
Insight Consulting
The agenda
Oracles PL/SQL language a sample procedure
How PL/SQL is wrapped, the language internals, the
database tables and files used, the events that can reveal
information
Why it is possible to read wrapped code driven by history
and design choice!
How it is possible to read wrapped code some sample
code shipped by Oracle
The built in APIs shipped by Oracle
10g, the changes
What can be done to protect PL/SQL source code
Insight Consulting
Insight Consulting
Insight Consulting
Insight Consulting
Insight Consulting
Insight Consulting
begin
null;
end;
Procedure created.
SQL>
Insight Consulting
All Rights
G:\code>
Insight Consulting
Insight Consulting
The relationships in 9i
PL/SQL Source
Wrap utility
Wrapped File
create or replace procedure aa
wrapped
0
abcd
abcd
abcd
abcd
abcd
Abcd
.
Insight Consulting
SYS.IDL_CHAR$
Name
SYS.IDL_UB1$
Null?
Type
SYS.IDL_UB2$
SYS.IDL_SB4$
SYS.SOURCE$
Null?
PART
VERSION
OBJ#
Type
LINE
SOURCE
VARCHAR2(4000)
NUMBER
PIECE#
LENGTH
PIECE
From $OH/rdbms/admin/sql.bsq
/* part: 0 = diana, 1 = portable
pcode, 2 = machine-dependent pcode
*/
Insight Consulting
Recursive SQL
What is recursive SQL? background supporting SQL
needed to execute the submitted statement
When compiling PL/SQL there are other background SQL
statements that need to run as SYS
Check for users privileges and roles
Triggers
Retrieving the PL/SQL code to run
Indexes
How can we see the complete picture?
Using traces, dumps and events
Insight Consulting
Procedure created.
SQL> alter session set events '10046 trace name context off';
Session altered.
SQL>
Insight Consulting
Those numbers
look familiar!
Insight Consulting
select count(*),'CHAR$',part,object_type
from idl_char$,dba_objects
where obj#=object_id
and part=0
group by part,object_type
union
select count(*),'UB1$',part,object_type
from idl_ub1$,dba_objects
where obj#=object_id
and part=0
group by part,object_type
union
select count(*),'UB2$',part,object_type
from idl_ub2$,dba_objects
where obj#=object_id
and part=0
group by part,object_type
union
select count(*),'SB4$',part,object_type
from idl_sb4$,dba_objects
where obj#=object_id
and part=0
group by part,object_type
order by 2
/
SQL> /
COUNT(*)
---------------28
44
50
72
91
101
329
481
559
728
778
'CHAR
PART OBJECT_TYPE
----- ---------- ----------CHAR$
CHAR$
CHAR$
CHAR$
CHAR$
CHAR$
CHAR$
CHAR$
CHAR$
CHAR$
CHAR$
0
0
0
0
0
0
0
0
0
0
0
OPERATOR
PROCEDURE
TYPE BODY
SEQUENCE
LIBRARY
FUNCTION
VIEW
TABLE
PACKAGE
SYNONYM
TYPE
COUNT(*) 'CHAR
PART OBJECT_TYPE
---------- ----- ---------- ----------------56 SB4$
0 OPERATOR
88 SB4$
0 PROCEDURE
{output snipped}
Insight Consulting
SQL> @rowid
BLK FNO RNUM 'CHAR
PART
VERSION
PIECE#
LENGTH
------ --- ---- ----- ---------- ---------- ---------- ---------49951
1
24 sb4$
0 153092096
0
14
49951
1
48 sb4$
0 153092096
1
2
42671
1
21 ub1$
0 153092096
2
3
35792
1
36 CHAR$
0 153092096
3
5
50581
1
8 UB2$
0 153092096
4
123
50581
1
9 UB2$
0 153092096
5
10
49951
1
50 sb4$
2 153092096
0
18
42671
1
10 ub1$
2 153092096
1
112
42671
1
13 ub1$
2 153092096
2
1
`
9 rows selected.
Insight Consulting
0, row 8, @0x11b1
271 fb: --H-FL-- lb: 0x1 cc:
0: [ 4] c3 04 05 0a
1: [ 1] 80
2: [ 6] c5 02 36 0a 15 61
3: [ 2] c1 05
4: [ 3] c2 02 18
5: [246]
00 03 00 0c 00 0d 00 11 00 25
00 00 17 00 1d 00 68 00 9a 00
00 00 00 01 00 01 00 01 00 01
01 00 00 00 01 00 01 00 01 00
00 01 00 00 00 00 00 00 00 02
00 00 00 00 04 00 05 00 08 00
00 00 00 ff 00 01 00 00 00 03
01 00 06 00 00 00 00 00 03 00
00 00 00 00 00 00 00 04 00 03
08 00 03 00 08 00 00 00 0b 00
00
55
00
0b
00
01
00
00
00
00
29
00
01
00
03
00
01
00
00
00
00
b4
00
01
00
01
00
00
00
00
2a
00
01
00
07
00
20
00
00
00
00
b5
00
01
00
05
00
00
00
00
2c
00
01
00
00
00
00
00
00
00
00
11
00
01
00
08
00
09
00
01
38
00
01
00
00
00
00
00
08
00
00
b1
00
01
00
00
00
0b
00
0c
3a
00
01
00
00
00
00
00
00
00
00
a4
00
01
00
00
00
0a
00
3e
00
01
00
00
00
00
00
08
00
b7
00
01
00
04
00
00
00
40
00
01
00
00
00
00
00
00
Insight Consulting
OWNE
---SYS
SYS
SYS
SYS
SYS
SYS
SYS
SYS
SYS
NAME
-------------------ALL_PROBE_OBJECTS
CODE_PIECES
INITJVMAUX
ORA_KGLR7_IDL_CHAR
ORA_KGLR7_IDL_SB4
ORA_KGLR7_IDL_UB1
ORA_KGLR7_IDL_UB2
PARSED_PIECES
RMJVM
TYPE
-----------VIEW
VIEW
PACKAGE BODY
VIEW
VIEW
VIEW
VIEW
VIEW
PACKAGE BODY
Insight Consulting
NUMBER
NUMBER
SQL> /
TEXT
-------------------------------select i.obj#, i.length
from sys.idl_ub1$ i
where i.part in (1,2)
union all
select i.obj#, i.length
from sys.idl_ub2$ i
where i.part in (1,2)
union all
select i.obj#, i.length
from sys.idl_sb4$ i
where i.part in (1,2)
union all
select i.obj#, i.length
from sys.idl_char$ i
where i.part in (1,2)
Insight Consulting
:=
:=
:=
:=
1;
2;
3;
4;
Insight Consulting
In/Out Default?
------ -------IN
OUT
OUT
In/Out
-----IN
IN
IN
IN
IN/OUT
OUT
IN
IN
Default?
--------
DEFAULT
DEFAULT
Insight Consulting
Insight Consulting
PD1(4):D_COMP_U [
L_SRCPOS : row 1 col 1
A_CONTEX :
PD2(4):
D_CONTEX [
L_SRCPOS : row 1 col 1
AS_LIST : < >
]
A_UNIT_B :
PD3(4):
D_S_BODY [
L_SRCPOS : row 1 col 1
A_D_ :
PD4(4):
DI_PROC [
L_SRCPOS : row 1 col
11
L_SYMREP : AA,
S_SPEC : PD5^(4),
S_BODY : PD8^(4),
S_LOCATI : 0,
S_STUB :
S_FIRST : PD4^(4),
C_OFFSET : 0,
C_FIXUP : NOT YET,
C_FRAME_ : 255,
Structural attribute
Semantic
attribute
: 32,
Lexical
attribute
Code
attrib
C_ENTRY_ : 1,
S_FRAME :
A_UP : PD3^(4),
S_LAYER : 1,
L_RESTRICT_REFERENCES
A_METH_FLAGS : 0,
SS_PRAGM_L :
S_INTRO_VERSION : 0,
A_PARALLEL_SPEC :
C_VT_INDEX : 0,
C_ENTRY_PT : 1
]
A_HEADER :
PD5(4):
D_P_ [
L_SRCPOS : row 1 col 1
{output snipped}
Insight Consulting
PD9(4):
PDB(4):
PDC(4):
D_BLOCK [
L_SRCPOS : row 1 col 1
AS_ITEM :
DS_ITEM [
L_SRCPOS : row 1 col 1
AS_LIST : < >
A_UP : PD8^(4)
]
AS_STM :
DS_STM [
L_SRCPOS : row 1 col 0
AS_LIST : <
D_NULL_S [
L_SRCPOS : row 1 col 1
C_OFFSET : 0,
A_UP : PDB^(4)
]
>
A_UP : PD8^(4)
AS_ALTER :
PDA(4):
DS_ALTER [
L_SRCPOS : row 1 col 1
AS_LIST : < >
S_BLOCK : PD8^(4),
S_SCOPE :
A_UP : PD8^(4)
]
Insight Consulting
Diana Rules
: DS_ITEM,
: D_STM,
: DS_ALTER;
Insight Consulting
End;
Insight Consulting
Dec
0
154
180
85
106
79
183
164
177
17
104
79
29
23
181
name
?
DI_PROC
DS_PARAM
D_P_
D_S_DECL
D_NULL_S
DS_STM
DS_ALTER
DS_ITEM
D_BLOCK
D_S_BODY
D_NULL_S
D_CONTEX
D_COMP_U
DS_PRAGM
D_P_
D_BLOCK
L_symrep = AA
DS_PARAM
DS_ITEM
DS_STM
D_NULL_S
DS_ALTER
Insight Consulting
Insight Consulting
Insight Consulting
Insight Consulting
Insight Consulting
Insight Consulting
Insight Consulting
Keywords
SQL> desc v$reserved_words
Name
Null?
Type
----------------------- -------- ------------------KEYWORD
LENGTH
VARCHAR2(64)
NUMBER
Insight Consulting
Procedure created.
SQL> alter session set events '10928 trace name context forever,
level 1';
Session altered.
SQL> exec ab;
PL/SQL procedure successfully completed.
SQL> alter session set events '10928 trace name context off';
Session altered.
SQL>
Insight Consulting
Insight Consulting
Insight Consulting
DI_PROC = AA
D_P_ = params
{not implemented}
DS_PARAM
D_BLOCK
DS_ITEM local variable
DS_STM
D_NULL_S
DS_ALTER
{not implemented}
IS BEGIN %{} EXCEPTION %{}
END;
{not implemented}
No output
NULL;
{not implemented}
Insight Consulting
procedure
Insight Consulting
Procedure created.
SQL> save aa.sql replace
Wrote file aa.sql
SQL> !wrap iname=aa.sql oname=aa.pls
SQL> @aa.pls
Procedure created.
SQL> @rowid
Insight Consulting
Insight Consulting
Scripts used
Rowid.sql lists the contents of the IDL$ tables
Idl.sql lists the IDL contents for all parsed objects
Unwrap.sql test program to walk the DIANA nodes
Unwrap_r.sql Proof of concept PL/SQL unwrapper
Ah.sql test program
Aa.sql test program
Attrib.sql dumps DIANA types and attributes
All scripts are available on http://www.petefinnigan.com
add the script name to the URL
Insight Consulting
Insight Consulting
Insight Consulting
www.siemens.co.uk/insight
+44 (0)1932 241000
Siemens Communications