EDB*Wrap 工具是 EDB 的企業版功能,該功能用在加密資料庫裡面的 Function/Stored Procedure/Package 等程式,如此達到保護公司資產的需求。
以下利用 Oracle 相容性建立一個 PL/SQL 的 Package 示範在資料庫中顯示原始碼,並再利用 EDB*Wrap 工具後,觀察匯入後的加密原始碼。
首先,先建立一個簡單的 Package:
edb=# CREATE OR REPLACE PACKAGE pkgedbdemo
edb-# AS
edb$# function funtest() return text;
edb$# END pkgedbdemo;
CREATE PACKAGE
edb=# CREATE OR REPLACE PACKAGE BODY pkgedbdemo AS
edb$# function funtest() return text
edb$# AS
edb$# BEGIN
edb$# return cast('test' as text);
edb$# END;
edb$# END pkgedbdemo;
CREATE PACKAGE BODY
edb=# SELECT text FROM user_source
edb-# WHERE name = 'PKGEDBDEMO'
edb-# AND type = 'PACKAGE';
text
------------------------------------------------------
CREATE OR REPLACE PACKAGE enterprisedb.pkgedbdemo IS
FUNCTION funtest() RETURN text;
END
(3 rows)
edb=# SELECT text FROM user_source
edb-# WHERE name = 'PKGEDBDEMO'
edb-# AND type = 'PACKAGE BODY';
text
-----------------------------------------------------------
CREATE OR REPLACE PACKAGE BODY enterprisedb.pkgedbdemo IS
FUNCTION funtest() RETURN text IS
BEGIN
return cast('test' as text);
END;
END
(6 rows)
edb=# -- 另外一方法
edb=# SELECT text FROM user_source
edb-# WHERE name = 'PKGEDBDEMO'
edb-# AND type LIKE 'PACKAGE%';
text
-----------------------------------------------------------
CREATE OR REPLACE PACKAGE enterprisedb.pkgedbdemo IS
FUNCTION funtest() RETURN text;
END
CREATE OR REPLACE PACKAGE BODY enterprisedb.pkgedbdemo IS
FUNCTION funtest() RETURN text IS
BEGIN
return cast('test' as text);
END;
END
(9 rows)
edb=# -- 方法一
edb=# SELECT prosrc FROM pg_proc
edb-# WHERE proname LIKE '%funtest%';
prosrc
---------------------------------
+
BEGIN +
return cast('test' as text);+
END
(1 row)
edb=# -- 方法二
edb=# SELECT pg_get_functiondef(oid)
edb-# FROM pg_proc
edb-# WHERE proname = 'funtest';
pg_get_functiondef
-------------------------------------------------
CREATE OR REPLACE FUNCTION pkgedbdemo.funtest()+
RETURNS text +
LANGUAGE edbspl +
AS $function$ +
BEGIN +
return cast('test' as text); +
END$function$ +
(1 row)
edb=# -- 方法三
edb=# SELECT funsrc
edb-# FROM pg_function
edb-# WHERE funname = 'funtest';
prosrc
---------------------------------
+
BEGIN +
return cast('test' as text);+
END
(1 row)
接著,上面的 Package 想要隱藏起來不被看到,就可以使用 EDB*Wrap 工具:該工具的用法,是先將希望加密的 PL/SQL 或 PL/pgSQL 原始碼利用該工具加密之後,再將加密過的檔案載入資料庫中。
[enterprisedb@myvm ~]$ cat pkgedbdemo.txt
CREATE OR REPLACE PACKAGE enterprisedb.pkgedbdemo IS
FUNCTION funtest() RETURN text;
END;
CREATE OR REPLACE PACKAGE BODY enterprisedb.pkgedbdemo IS
FUNCTION funtest() RETURN text IS
BEGIN
return cast('test' as text);
END;
END;
[enterprisedb@myvm ~]$ edbwrap -i pkgedbdemo.txt
EDB*Wrap Utility: Release 9.5.3.8
Copyright (c) 2004-2015, EnterpriseDB Corporation. All Rights Reserved.
Using encoding UTF8 for input
Processing pkgedbdemo.txt to pkgedbdemo.plb
將產生出的檔案載入 EDB 中,取代既有的程式,並再查詢原始碼:
edb=# \i pkgedbdemo.plb
CREATE PACKAGE BODY
edb=# SELECT text FROM user_source
edb-# WHERE name = 'PKGEDBDEMO'
edb-# AND type LIKE 'PACKAGE%';
text
------------------------------------------------------------------------------
$__EDBwrapped__$
UTF8
d5u3HfdLfD5e3jdH/GMPYse0fCXj26+0NUbZn6BnouTCnfdckQOi6+UWmvOnFlULY+ej0KUpkb0b
x9i51l8XamoYErnIU2i+DIUlrvzvju8wKsLTdT8N5jfXUljOFeoNGna0kWx6zbze8j1Yg0lNLWX+
52qCNaYsysb3idAUDk23uOE=
$__EDBwrapped__$
$__EDBwrapped__$
UTF8
d86fuzolWN11OtdWbaHS8UOV52E2YISSuFNi3xS0S1LEzDbBgKzi4bJi7/CP9oXwNwfNyWlKg55s
T5Ajbb8pDYhrnJQl+4jQ0Cy7Rcqo6S0l0Tlx7fwX0ObEBabKrs0vog+s0lcSPRNxIxUiUP5C2ycJ
lsjJP+E3j9ju7+exuXnlh/Ae+8NhH9hQdlaaOV8KyOyuOo0d6XVihxkARKaF5zQn7xesiK59uA==
$__EDBwrapped__$
(12 rows)
不管是 Package,或是 Package Body,都已經被加密成功。
使用手冊建議,一般只要加密 Package Body 就好了,不然要用 Package 的人就很難查裡面有什麼內容可以用了...
[enterprisedb@myvm ~]$ cat pkgedbdemo_body.txt
CREATE OR REPLACE PACKAGE BODY enterprisedb.pkgedbdemo IS
FUNCTION funtest() RETURN text IS
BEGIN
return cast('test' as text);
END;
END;
[enterprisedb@myvm ~]$ edbwrap -i pkgedbdemo_body.txt
EDB*Wrap Utility: Release 9.5.3.8
Copyright (c) 2004-2015, EnterpriseDB Corporation. All Rights Reserved.
Using encoding UTF8 for input
Processing pkgedbdemo_body.txt to pkgedbdemo_body.plb
edb=# \i pkgedbdemo_body.plb
CREATE PACKAGE BODY
edb=# SELECT text FROM user_source
edb-# WHERE name = 'PKGEDBDEMO'
edb-# AND type LIKE 'PACKAGE%';
text
------------------------------------------------------------------------------
CREATE OR REPLACE PACKAGE enterprisedb.pkgedbdemo IS
FUNCTION funtest() RETURN text;
END
$__EDBwrapped__$
UTF8
d86fuzolWN11OtdWbaHS8UOV52E2YISSuFNi3xS0S1LEzDbBgKzi4bJi7/CP9oXwNwfNyWlKg55s
T5Ajbb8pDYhrnJQl+4jQ0Cy7Rcqo6S0l0Tlx7fwX0ObEBabKrs0vog+s0lcSPRNxIxUiUP5C2ycJ
lsjJP+E3j9ju7+exuXnlh/Ae+8NhH9hQdlaaOV8KyOyuOo0d6XVihxkARKaF5zQn7xesiK59uA==
$__EDBwrapped__$
(9 rows)
像上面這樣就兼顧使用和加密,摸蜊仔兼洗褲~。
參考: