寫在前面的話
小編前段時間刷博客的時候看到了一篇這樣的文章,大概的內容是開發者辛辛苦苦開發小半年的作品,上線沒幾天,就被人盜版上線了,甚至連代碼都是扒的自己的,有興趣的朋友可以去看看。點我查看
至於說怎麼去扒微信小程序的源碼,小編在這裏不做過多的闡述,自行百度“小程序代碼反編譯”
今天咱們來講講如何在提交審覈之前,給自己的小程序代碼做混淆,代碼安全加固。
詳情請諮詢我 vx:a21544182123
核心思路
- javascript-obfuscator對小程序的代碼進行混淆
- bat腳本對項目下所有的js文件進行混淆操作
首先咱們來介紹一下javascript-obfuscator
在百度上對它的定義是這樣的“javascript-obfuscator是一個免費的JavaScript代碼混淆工具,它功能強大,可以把你的源代碼變得“面目全非”,完全沒有可讀性。還具有部分防調試功能,給JavaScript代碼多一層保護。”
事實上也確實是這樣,小編通過它來對項目進行混淆之後,代碼的可讀性變得極其差。
const a0_0x1cf1 = ['getStorageSync', 'scope.userInfo', 'getSystemInfo', 'now', 'userInfo', 'unshift', 'getSystemInfo:ok', 'log', 'getSetting', 'logs', 'setStorageSync', 'systemInfo', 'userInfoReadyCallback', 'authSetting', 'errMsg'];
(function (_0x371ccf, _0x1cf103) {
const _0x5d052b = function (_0x13fa3f) {
while (--_0x13fa3f) {
_0x371ccf['push'](_0x371ccf['shift']());
}
};
_0x5d052b(++_0x1cf103);
}(a0_0x1cf1, 0x14b));
const a0_0x5d05 = function (_0x371ccf, _0x1cf103) {
_0x371ccf = _0x371ccf - 0x0;
let _0x5d052b = a0_0x1cf1[_0x371ccf];
return _0x5d052b;
};
App({
'onLaunch': function () {
let _0x2e1d11 = this;
var _0x451dd8 = wx[a0_0x5d05('0xe')]('logs') || [];
_0x451dd8[a0_0x5d05('0x4')](Date[a0_0x5d05('0x2')]()), wx[a0_0x5d05('0x9')](a0_0x5d05('0x8'), _0x451dd8), wx[a0_0x5d05('0x1')]({
'complete': _0x1f36a4 => {
console[a0_0x5d05('0x6')](_0x1f36a4, a0_0x5d05('0x1')), (_0x1f36a4[a0_0x5d05('0xd')] = a0_0x5d05('0x5')) && (_0x2e1d11[a0_0x5d05('0xa')] = _0x1f36a4);
}
}), wx[a0_0x5d05('0x7')]({
'success': _0x3eebf4 => {
_0x3eebf4[a0_0x5d05('0xc')][a0_0x5d05('0x0')] && wx['getUserInfo']({
'success': _0x306423 => {
this['globalData'][a0_0x5d05('0x3')] = _0x306423[a0_0x5d05('0x3')], this[a0_0x5d05('0xb')] && this['userInfoReadyCallback'](_0x306423);
}
});
}
});
},
'globalData': {
'userInfo': null
},
'systemInfo': {}
});
話不多說,上乾貨
javascript-obfuscator的安裝
- 官網地址 點我進入
- 民間安裝方式(建議用cnpm安裝)
npm init -f
npm install formidable --save
npm install -g javascript-obfuscator
- 官網安裝方式
npm install --save-dev javascript-obfuscator
javascript-obfuscator的用法
- 默認不用參數的加密直接執行
javascript-obfuscator input.js --output output.js
- 也可以使用部分自定義參數
--target 'browser-no-eval'
--disable-console-output true
--debug-protection true
--debug-protection-interval true
--identifier-names-generator 'hexadecimal'
--string-array true
--rotate-string-array true
--string-array-encoding 'rc4'
--string-array-threshold 0.8
- 當然還能直接讀取配置json文件
javascript-obfuscator a.js --config test.json --output b.js
官方推薦的三種配置
- 性能將比沒有混淆的情況下慢50-100%
{
compact: true,
controlFlowFlattening: true,
controlFlowFlatteningThreshold: 1,
deadCodeInjection: true,
deadCodeInjectionThreshold: 1,
debugProtection: true,
debugProtectionInterval: true,
disableConsoleOutput: true,
identifierNamesGenerator: 'hexadecimal',
log: false,
renameGlobals: false,
rotateStringArray: true,
selfDefending: true,
stringArray: true,
stringArrayEncoding: 'rc4',
stringArrayThreshold: 1,
transformObjectKeys: true,
unicodeEscapeSequence: false
}
- 性能將比沒有混淆的情況下降低30-35%
{
compact: true,
controlFlowFlattening: true,
controlFlowFlatteningThreshold: 0.75,
deadCodeInjection: true,
deadCodeInjectionThreshold: 0.4,
debugProtection: false,
debugProtectionInterval: false,
disableConsoleOutput: true,
identifierNamesGenerator: 'hexadecimal',
log: false,
renameGlobals: false,
rotateStringArray: true,
selfDefending: true,
stringArray: true,
stringArrayEncoding: 'base64',
stringArrayThreshold: 0.75,
transformObjectKeys: true,
unicodeEscapeSequence: false
}
- 性能會比沒有混淆的情況稍微慢一些
{
compact: true,
controlFlowFlattening: false,
deadCodeInjection: false,
debugProtection: false,
debugProtectionInterval: false,
disableConsoleOutput: true,
identifierNamesGenerator: 'hexadecimal',
log: false,
renameGlobals: false,
rotateStringArray: true,
selfDefending: true,
stringArray: true,
stringArrayEncoding: false,
stringArrayThreshold: 0.75,
unicodeEscapeSequence: false
}
當然以上所說,都只是針對單個js文件的處理方法,要想針對整個項目中的js文件,則還需要藉助bat腳本來實現
思路
- 獲取小程序項目名,複製到新的混淆文件夾
- 對混淆文件夾下所有的js文件執行 混淆命令
小編親測混淆後代碼正常審覈通過
詳情請諮詢我 vx:a21544182123
最終效果