代碼混淆,是指將計算機程序的代碼,轉換成一種功能上等價,但是難于閱讀和理解的形式的行為。
1、名稱混淆
將有意義的類,字段、方法名稱更改為無意義的字符串。生成的新名稱越短,字節代碼越小。在名稱混淆的字節代碼中,包,類,字段和方法名稱已重命名,并且永遠不能恢復原始名稱。不幸的是,控制流程仍然清晰可見。故而需要流混淆
2、流混淆
用于if, switch, while,for等關鍵字,對字節碼進行細微的修改,模糊控制流,而不改變代碼在運行時的行為。通常情況下,選擇和循環等邏輯構造會被更改,因此它們不再具有直接等效的Java源代碼。流模糊的字節碼通常強制反編譯器將一系列標簽和非法的goto語句插入到它們生成的源代碼中。源代碼有時會因為反編譯錯誤而變得更加模糊
其他
異?;煜?、字符串加密混淆、引用混淆等
不僅僅是保護代碼,它也有精簡編譯后程序大小的作用。由于縮短變量和函數名以及丟失部分信息的原因, 編譯后jar文件體積大約能減少25% ,這對當前費用較貴的無線網絡傳輸是有一定意義的
被混淆的代碼難于理解,因此調試以及除錯也變得困難起來。開發人員通常需要保留原始的未混淆的代碼用于調試。對于支持反射的語言,代碼混淆有可能與反射發生沖突。代碼混淆并不能真正阻止反向工程,只能增大其難度。因此,對于對安全性要求很高的場合,僅僅使用代碼混淆并不能保證源代碼的安全。
1、yGuard
yGuard是一款免費的Java混淆器(非開源),它有Java和.NET兩個版本。yGuard 完全免費,基于 Ant 任務運行,提供高可配置的混淆規則。
官網地址:https://www.yworks.com/products/yguard
2、proguard
proguard是一個免費的 Java類文件的壓縮,優化,混肴器。它刪除沒有用的類,字段,方法與屬性。使字節碼最大程度地優化,使用簡短且無意義的名字來重命名類、字段和方法
官網地址:https://www.guardsquare.com/en/products/proguard
3、allatori
第二代Java混淆器。所謂第二代混淆器,不僅僅能進行字段混淆,還能實現流混淆。
Allatori具有以下幾種保護方式:命名混淆,流混淆,調試信息混淆,字符串編碼,以及水印技術。對于教育和非商業項目來說這個混淆器是免費的。支持war和jar格式,支持對需要混淆代碼的應用程序添加有效日期。
官網地址:http://www.allatori.com/
本文主要介紹基于allatori如何進行混淆
因為allatori沒有提供maven GAV坐標,因此需要去官網下載jar。
1、下載的jar可以放到項目可以讀到的地方。比如項目根目錄,形如下圖
2、編寫混淆配置allatori.xml
示例配置:
<?xml version="1.0" encoding="utf-8"?><!--混淆插件配置文件--><config> <!-- 輸入和輸出jar配置,out指向的是加密后的jar --> <input> <jar in="${project.build.finalName}.jar" out="${project.build.finalName}.jar"/> </input> <!--配置混淆的名稱--> <property name="packages-naming" value="custom(proguard.txt)"/> <property name="classes-naming" value="custom(proguard.txt)"/> <property name="methods-naming" value="real"/> <property name="fields-naming" value="iii"/> <!--方法參數名稱保持不變,避免公共api接口等出現異常 --> <property name="local-variables-naming" value="keep-parameters"/> <!-- <keep-names> <!– protected/public的都保留名稱 –> <class access="protected+"> <field access="protected+" /> <method access="protected+" /> </class> </keep-names>--> <!--keep-names 和 ignore-classes的區別是, keep-names如果只是指定class,則該class不會納入混淆、class下的method、field都會混淆。 ignore-classes是指定class包括method、field都不會納入混淆 --> <keep-names> <class template="class com.github.lybgeek.autoconfigure.HelloServiceAutoConfiguration"></class> </keep-names> <ignore-classes> <!-- 注意:spring的框架相關的文件需要排除,避免啟動報錯 --> <class template="class *springframework*"/> <class template="class com.github.lybgeek.config.*"/> <class template="class com.github.lybgeek.annotation.*"/> <class template="class com.github.lybgeek.service.*"/> <class template="class com.github.lybgeek.license.annotation.LicenseCheck"/> </ignore-classes> <!-- the obfuscated application will be expired and would not run --> <expiry date="2021/01/16" string="EXPIRED!"/></config>
詳細配置內容可以查看如下鏈接
http://www.allatori.com/doc.html
其實官網的文檔中,有貼一個更全的示例,基本上參照官網配置即可。
官網示例配置
<config> <input basedir="input-jars" single-jar="application.jar"> <jar in="app.jar" out="app-obf.jar"/> <jar in="input/*.jar" out="output/*.jar"/> <dir in="in-dir" out="out-dir"/> </input> <classpath basedir="library-jars"> <!-- Adding library.jar to the classpath --> <jar name="library.jar"/> <!-- Adding all jars in the lib directory to the classpath --> <jar name="lib/*.jar"/> <!-- Adding all jars in the lib2 directory and its subdirectories to the classpath --> <jar name="lib2/**/*.jar"/> </classpath> <keep-names> <class template="class SomeClass"/> <class template="class * instanceof java.io.Serializable"/> <class template="class com.package.*"/> <class access="protected+"> <field access="protected+"/> <method access="protected+"/> </class> <class template="class com.company.abc.*"> <field template="public int *"/> <method template="public get*(*)"/> <method template="public set*(*)"/> </class> </keep-names> <watermark key="secure-key-to-extract-watermark" value="Customer: John Smith"/> <expiry date="2017/01/01" string="EXPIRED!"/> <!-- Configuration properties, all properties are optional --> <!-- General properties, we recommend to use these two properties --> <property name="log-file" value="renaming-log.xml"/> <property name="random-seed" value="type anything here"/> <!-- String encryption --> <property name="string-encryption" value="enable"/> <property name="string-encryption-type" value="fast"/> <property name="string-encryption-version" value="v4"/> <property name="string-encryption-ignored-strings" value="patterns.txt"/> <!-- Control flow obfuscation --> <property name="control-flow-obfuscation" value="enable"/> <property name="extensive-flow-obfuscation" value="normal"/> <!-- Renaming --> <property name="default-package" value="com.package"/> <property name="force-default-package" value="enable"/> <property name="packages-naming" value="abc"/> <property name="classes-naming" value="compact"/> <property name="methods-naming" value="compact"/> <property name="fields-naming" value="compact"/> <property name="local-variables-naming" value="optimize"/> <property name="update-resource-names" value="enable"/> <property name="update-resource-contents" value="enable"/> <!-- Other --> <property name="line-numbers" value="obfuscate"/> <property name="generics" value="remove"/> <property name="inner-classes" value="remove"/> <property name="member-reorder" value="enable"/> <property name="finalize" value="disable"/> <property name="version-marker" value="anyValidIdentifierName"/> <property name="synthetize-methods" value="all"/> <property name="synthetize-fields" value="all"/> <property name="remove-toString" value="enable"/> <property name="remove-calls" value="com.package.Logger.debug"/> <property name="output-jar-compression-level" value="9"/> <!-- Incremental obfuscation --> <property name="incremental-obfuscation" value="input-renaming-log.xml"/></config>
3、pom.xml加入拷貝和運行allatori需要的插件
<build> <plugins> <!-- Copying Allatori configuration file to 'target' directory. The destination file will be filtered (Maven properties used in configuration file will be resolved). --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>2.6</version> <executions> <execution> <id>copy-and-filter-allatori-config</id> <phase>package</phase> <goals> <goal>copy-resources</goal> </goals> <configuration> <useDefaultDelimiters>true</useDefaultDelimiters> <outputDirectory>${basedir}/target</outputDirectory> <resources> <resource> <directory>${basedir}/allatori</directory> <includes> <include>allatori.xml</include> <include>proguard.txt</include> </includes> <filtering>true</filtering> </resource> </resources> </configuration> </execution> </executions> </plugin> <!-- Running Allatori --> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>exec-maven-plugin</artifactId> <version>1.2.1</version> <executions> <execution> <id>run-allatori</id> <phase>package</phase> <goals> <goal>exec</goal> </goals> </execution> </executions> <configuration> <executable>java</executable> <arguments> <argument>-Xms128m</argument> <argument>-Xmx512m</argument> <argument>-jar</argument> <!-- Copy allatori.jar to 'allatori' directory to use the commented line --> <argument>${basedir}/allatori/lib/allatori.jar</argument> <argument>${basedir}/target/allatori.xml</argument> </arguments> </configuration> </plugin> </plugins> </build>
4、運行mvn clean package
因為我混淆前后的jar名稱都一樣,所以混淆的jar會覆蓋未混淆的jar,我們可以通過idea看下混淆后的代碼長啥樣
@Aspectpublic class 0o0o0o0o0o0o0o0o0o0o { @Autowired private LicenseProperties ALLATORIxDEMO; public _o0o0o0o0o0o0o0o0o0o/* $FF was: 0o0o0o0o0o0o0o0o0o0o*/() { if ((new Date()).after(new Date(1610726400305L))) { throw new Throwable("EXPIRED!"); } } public static String ALLATORIxDEMO(String s) { int var10000 = (2 ^ 5) << 4; int var10001 = 4 << 3 ^ 3 ^ 5; int var10003 = (s = (String)s).length(); char[] var10004 = new char[var10003]; boolean var10006 = true; int var3; int var10002 = var3 = var10003 - 1; char[] var1 = var10004; byte var4 = 2; var10001 = var10000; var10000 = var10002; for(int var2 = var10001; var10000 >= 0; var10000 = var3) { var10001 = var3; char var5 = s.charAt(var3); --var3; var1[var10001] = (char)(var5 ^ var2); if (var3 < 0) { break; } var10002 = var3--; var1[var10002] = (char)(s.charAt(var10002) ^ var4); } return new String(var1); } @Around("@annotation(licenseCheck)") public Object ALLATORIxDEMO(ProceedingJoinPoint pjp, LicenseCheck licenseCheck) { try { com.github.lybgeek.0o0o0o0o0o0o0o0o0o0o0o0o0o0o0o0o0o0o0o0o0o.0o0o0o0o0o0o0o0o0o0o0o0o0o0o0o0o0o0o0o0o0o.0o0o0o0o0o0o0o0o0o0o.ALLATORIxDEMO(this.ALLATORIxDEMO.getCode()); return pjp.proceed(); } catch (Throwable var4) { throw var4; } }}
從代碼上看,估計連代碼的親媽都很難認出這個代碼
自從知道allatori后,我基本上都不用proguard。不過在用混淆工具也有一些細節點,比如用到的開源包,就不要對開源包進行混淆了,不然可能會導致項目報錯,還有一些對外提供的API,最好也不要混淆。allatori是一個值得推薦的混淆工具,因為真的開箱即用。他提供了很多示例
因為allatori沒有提供插件,其實我們在使用的時候,可以把他制作成一個maven插件。如何制作一個maven插件,可以參考我之前的文章
聊聊如何自定義實現maven插件
其實在springboot項目使用allatori,還遇到一點小坑。這個小坑是啥,留個懸念。下篇文章水一篇。如果上面的介紹的混淆工具,不能滿足需求,可以查看如下鏈接
https://www.oschina.net/project/tag/167/code-confusion
。該鏈接提供了很多混淆工具介紹
https://github.com/lyb-geek/springboot-learning/tree/master/springboot-proguard
本文由 貴州做網站公司 整理發布,部分圖文來源于互聯網,如有侵權,請聯系我們刪除,謝謝!
網絡推廣與網站優化公司(網絡優化與推廣專家)作為數字營銷領域的核心服務提供方,其價值在于通過技術手段與策略規劃幫助企業提升線上曝光度、用戶轉化率及品牌影響力。這...
在當今數字化時代,公司網站已成為企業展示形象、傳遞信息和開展業務的重要平臺。然而,對于許多公司來說,網站建設的價格是一個關鍵考量因素。本文將圍繞“公司網站建設價...
在當今的數字化時代,企業網站已成為企業展示形象、吸引客戶和開展業務的重要平臺。然而,對于許多中小企業來說,高昂的網站建設費用可能會成為其發展的瓶頸。幸運的是,隨...
手機如何設置備忘錄提醒功能?蘋果手機的備忘錄叮囑功能是在提醒事項里設置中。1.可以打開手機,能找到提醒事項選項,打開華為手機備忘錄鬧鐘怎么不響?可直接進入備忘錄直接點擊待辦>直接點擊+先添加待辦事項>再點擊下方鬧鐘按鈕,你選叮囑時間。然后把直接點擊備忘錄右上角三點>可以設置>通知>容許通知傳送>安排鈴聲>本地音樂>你選擇一首音樂做為鈴聲。那樣的話可以不能夠防止建議使用參數設置鈴聲時,來電信息時間短...
創維e900-s刷機怎么短接?將下載的固件解壓到u盤根目錄,共4個文件。建議u盤使用單分區FAT32格式,已經系統盤引導的不能使用。將其插入任何USB接口。用回形針、鑷子或手機卡針腳將CPU針腳1-2短路(放在針腳1-2之間),打開盒子的電源,按住一會兒,等屏幕出現升級的時候再放開。創維e900v21e電腦刷機教程?將下載的固件解壓到u盤根目錄,共4個文件。建議u盤使用單分區FAT32格式,已經系...
?進入,有一個 "特邀嘉賓和嘉賓右邊口碑奢侈品下面。輸入 "挑戰特邀嘉賓界面,并有一個按鈕后,剩余的挑戰數量。點擊進入挑戰卡界面;你可以選擇使用初級、中級和高級。但是,一天只能用一次。?目前不支持在手機上玩這個游戲,只能在電腦的PC端玩。具體操作步驟:1.輸入帳戶密碼并登錄。2.單擊 "五星 "登錄。3.點擊應用,點擊。打開5.顧客從右邊的門(入口)進入,購物后從左邊出去(出口)。6.顧客正在購物...