Hide / obfuscate PL/SQL code (WRAP)
Sometimes it is necessary to hide (obfuscate) our PL/SQL source code. This utility should be used judiciously; it is not a matter of hiding all available code, but only that which, for security reasons, should not be shared with third parties.
The method used has traditionally been carried out with the WRAP tool, which we detail in this post Starting with version 10g R2, it is possible to perform this action with the DBMS_DDL.WRAP package, which we will discuss in another post .
It is important to be aware of some of its limitations before applying it, especially in production environments. It is very important to have a copy of the PL/SQL code and version control before applying the "wrap" method to our code.
Basically, the limitations of the WRAP utility are as follows:
Los ficheros ofuscados, en aquellos donde hemos usado wrap, no son compatibles entre releases de Oracle Databases.
No se puede usar wrap en código de triggers.
No es seguro para securizar password o nombres de tablas.
No detecta problemas de sintaxis o errores de código, generados en nuestro código sin ofuscar.
El código ofuscado es más largo que el original.
Wrap solamente ofusca el body de un package o el type, pero no su especificación.
To see how it works we are going to create a fairly simple function, which we will later obfuscate with wrap.
CREATE OR REPLACE FUNCTION dia_de_hoy RETURN VARCHAR2 AS
BEGIN
RETURN TO_CHAR(SYSDATE, 'DD-MON-YYYYYY HH24:MI:SS');
END dia_de_hoy;
/
We check that it works:
select today_day from dual;
TODAY_DAY
------- -------
31-AUG-2023 20:47:53
We copy the code of our function into the operating system.
[oracle@oracleconraul u01]$ cat today_date_function.sql
CREATE OR REPLACE FUNCTION today_date RETURN VARCHAR2 AS
BEGIN
RETURN TO_CHAR(SYSDATE, ‘DD-MON-YYYY HH24:MI:SS’);
END today_date;
/
We can see in SQL-Developer the normal aspect of the function before applying the obfuscation.

In the operating system we launch the command to obfuscate the PL-SQL code.
La sintaxis es:
wrap iname=<fichero entrada> oname=<fichero de salida ofuscado>
[oracle@localhost u01]$ wrap iname=today's_function.sql oname=today's_function.out
PL/SQL Wrapper: Release 19.0.0.0.0 – Production on Thu Aug 31 21:17:23 2023
Version 19.3.0.0.0
Copyright (c) 1982, 2019, Oracle and/or its affiliates. All rights reserved.
Processing function_today.sql to function_today.out
The utility will report the obfuscated code for execution.
CREATE OR REPLACE FUNCTION dia_de_hoy wrapped
a000000
1
abcd
abcd
abcd
abcd
abcd
abcd
abcd
abcd
abcd
abcd
abcd
abcd
abcd
abcd
abcd
8
74 b6
LH4SgLm3AMbYAySR+WVH9QmeSoAwg8eZgcfLCNL+Xlr6WV+uofByhWTD58CyvbKbXufHdMAz
uHRlCbh0i8DAMv7ShgmpoQLOxtYaIazv1kx2hHEyjndMcfUQc3P17zdnr2pqhduXsevFV1RM
66tN+j1y6a+V638ZlSoZaiQl7Pumz30sVg==
/
Copy the code and run it (SqlPlus, SqlDeveloper, etc).

From now on, all new sessions connecting to the database will not have access to the code in a readable form, and the code will be hidden from users.

