Zephir編寫C動態擴展庫將PHP源代碼編譯加密
概述
PHP源代碼編譯加密通常是出于保護知識產權、防止源代碼泄露和被非法修改的目的。加密PHP源代碼可以使用先進的加密工具如Zend Guard或ionCube、使用代碼混淆技術、或開發自定義編譯擴展。
最關鍵的是,在加密的同時,確保服務器有相應的解密擴展來執行代碼,否則加密代碼將無法運行。換言之,加密和執行是一個密不可分的過程。本章節我們將主要展開討論使用Zephir語言編寫動態擴展庫進行代碼加密。
Zephir 使用PHP編譯器將源代碼編譯成二進制文件,這樣可以隱藏代碼邏輯和實現細節??梢园淹ㄓ脴I務類,都編譯成二進制拓展,對外只提供.so文件,也就不需要加密代碼了。
Zephir
Zephir是一個開源的高級語言(區別于匯編等機器語言),它的設計是為了減輕PHP原生擴展的創建難度和可維護性,另外還帶來了強類型與內存安全的支持。
Zephir是一門定位于大多數想通過編寫和編譯能夠被PHP執行的代碼的PHPer需求的語言。它是一種動態和靜態類型結合的語言,對于PHP來說,它的一些功能可能會感覺時曾相識。
應用
本章節使用個人倉庫 https://github.com/Tinywan/zephir-lang-php-extension 進行簡單實現一個業務代碼的認證授權加密代碼。
機密案例代碼
git clone https://github.com/Tinywan/zephir-lang-php-extension.git
目錄結構
.
├── zephirencrypt -- 加密擴展庫目錄
│ ├── Auth
│ │ └── License.zep -- 權限認證
│ ├── Common
│ │ └── AbstractModule.zep -- 模塊抽象類
│ └── Module -- 業務模塊目錄
│ └── LiveModule.zep
├── config.json -- 編譯擴展配置文件
└── ext -- 編譯生成的.so文件
授權許可證License.zep
/**
* @desc 授權許可證
* @author Tinywan(ShaoBo Wan)
* @date 2024/6/16 20:14
*/
namespace ZephirEncrypt\Auth;
class License
{
/**
* @desc 授權認證
* @author Tinywan(ShaoBo Wan)
*/
public static function check(string service_uuid, string auth_license, int uid = 0, int rand = 0)
{
var private_key = "tinywan2024";
var expire_time = substr(auth_license, 0, 10);
var current_time = time();
var sequest_hash_value = substr(auth_license, -32);
var res_hash_value = md5(service_uuid . "-" . expire_time . "-" . rand . "-" . uid . "-" . private_key);
if expire_time < current_time {
return -1;
}
if sequest_hash_value != res_hash_value {
return 0;
}
return 1;
}
}
業務模塊抽象類AbstractModule.zep
/**
* @desc 抽象業務模塊
* @author Tinywan(ShaoBo Wan)
* @date 2024/6/16 20:14
*/
namespace ZephirEncrypt\Common;
use ZephirEncrypt\Auth\License;
abstract class AbstractModule
{
protected service_uuid;
protected auth_license;
/** 1 true , 0 false */
public auth_status = 0;
/**
* @desc 構架函數
* @author Tinywan(ShaoBo Wan)
*/
public function __construct(string service_uuid, string auth_license)
{
let this->service_uuid = service_uuid;
let this->auth_license = auth_license;
var checkRes = License::check(this->service_uuid, this->auth_license);
if checkRes == -1 {
let this->auth_status = -1;
}
if checkRes == 0 {
let this->auth_status = 0;
}
if checkRes == 1 {
let this->auth_status = 1;
}
}
/** 獲取權限狀態 */
abstract public function getAuthStatus();
}
直播業務模塊LiveModule.zep
/**
* @desc 直播業務模塊
* @author Tinywan(ShaoBo Wan)
* @date 2024/6/16 20:14
*/
namespace ZephirEncrypt\Module;
use ZephirEncrypt\Common\AbstractModule;
class LiveModule extends AbstractModule
{
/**
* @desc 構架函數
* @author Tinywan(ShaoBo Wan)
*/
final public function __construct(string service_uuid, string auth_license)
{
parent::__construct(service_uuid, auth_license);
}
/**
* @desc 獲取權限狀態
* @author Tinywan(ShaoBo Wan)
*/
public function getAuthStatus()
{
return $this->auth_status;
}
/**
* @desc 業務測試
* @author Tinywan(ShaoBo Wan)
*/
public function start()
{
var res = [];
if this->auth_status != 1 {
let res = ["code":this->auth_status, "msg":"no permission to access"];
return json_encode(res);
}
echo "直播開始成功..............";
echo "這里開始寫你的業務代碼.....";
echo "這里開始寫你的業務代碼.....";
echo "這里開始寫你的業務代碼.....";
}
}
編譯并生成擴展
cd zephir-lang-php-extension/
# 進入加密擴展庫目錄
cd zephirencrypt
編譯并生成擴展
zephir build
如果一切順利,您將在輸出的末尾看到以下消息:
zephir-lang-php-extension/zephirencrypt# zephir build
Cleaning old kernel files...
Copying new kernel files...
Preparing for PHP compilation...
Preparing configuration file...
Compiling...
Zephir version has changed, use "zephir fullclean" to perform a full clean of the project
Installing...
Extension installed.
Add "extension=zephirencrypt.so" to your php.ini
! [NOTE] Don't forget to restart your web server
在上面的步驟中,您可能需要提供root密碼才能安裝擴展。
最后,必須將擴展添加到php.ini才能由PHP加載。這是通過添加初始化指令:extension=zephirencrypt.so來實現的。
注意:您也可以在命令行中使用-d extension=zephirencrypt.so加載它,但它只會為單個請求加載,因此每次您想要在CLI中測試擴展時都需要包含它。將指令添加到php.ini將確保從那時起為每個請求加載它。
測試
現在擴展已添加到您的 php.ini 中,請執行以下命令檢查擴展是否正確加載:
php -m
[PHP Modules]
apcu
....
zephir_parser
zephirencrypt
...
[Zend Modules]
擴展名zephirencrypt應該是表明擴展名已正確加載。
如何使用
新建測試文件zephir.php
<?php
/**
* @desc zephir.php 描述信息
* @author Tinywan(ShaoBo Wan)
* @date 2024/8/5 22:51
*/
declare(strict_types=1);
$serviceUuid = "13c7c8e1-3ac2-41a6-95dc-ff954b431bbf";
$authLicense = "1728869954-0-0-eabfb0fb52c429d4fa037585f7afd512";
$liveModule = new \ZephirEncrypt\Module\LiveModule($serviceUuid, $authLicense);
print_r($liveModule);
$res = $liveModule->start();
var_dump($res);
以上授權碼時間為1528869954 ,即:2018-06-13 14:05:54,預期結果是該授權碼已過期,執行代碼預期結果
ZephirEncrypt\Module\LiveModule Object
(
[service_uuid:protected] => 13c7c8e1-3ac2-41a6-95dc-ff954b431bbf
[auth_license:protected] => 1528869954-0-0-eabfb0fb52c429d4fa037585f7afd512
[auth_status] => -1
)
可以看到此時授權碼狀態是-1表示授權碼過期。嘗試授權碼為未來某一天1788689954 ,即:2026-09-06 18:19:14
$authLicense = "1788689954-0-0-eabfb0fb52c429d4fa037585f7afd512";
預期結果相同
ZephirEncrypt\Module\LiveModule Object
(
[service_uuid:protected] => 13c7c8e1-3ac2-41a6-95dc-ff954b431bbf
[auth_license:protected] => 1788689954-0-0-eabfb0fb52c429d4fa037585f7afd512
[auth_status] => 0
)
正確的授權碼
ZephirEncrypt\Module\LiveModule Object
(
[service_uuid:protected] => 13c7c8e1-3ac2-41a6-95dc-ff954b431bbf
[auth_license:protected] => 178909089954-0-0-eabfb0fb52c429d4fa037585f7afd512
[auth_status] => 1
)
string(42) "直播開始成功............."
string(42) "這里開始寫你的業務代碼...."
string(42) "這里開始寫你的業務代碼...."
string(42) "這里開始寫你的業務代碼...."