最近試用了幾個Java混淆器(Java Obfuscator),感覺沒有一個完全另人滿意的,于是想干脆自己寫一個得了。翻了幾頁Java虛擬機(jī)規(guī)范之后突發(fā)奇想,別的混淆器都是在編譯好的byte code上做文章,能不能從源碼直接編譯成經(jīng)過混淆的class文件呢?就這樣花了一個多星期的時間寫了一個Java混淆編譯器(Java Obfuscator Compiler)。
Q: 什么是混淆器? A: 由于Java程序運行時是動態(tài)連接的,因此編譯成的目標(biāo)文件中包含有符號表,使得Java程序很容易被反編譯,混淆器可以打亂class文件中的符號信息,使反向工程變得非常困難。
Q: 現(xiàn)有的混淆器有什么問題? A: 現(xiàn)有的混淆器都是對編譯好的class文件進(jìn)行混淆,這樣就需要編譯和混淆兩個步驟。并不是所有的符號都需要混淆,如果你開發(fā)的是一個類庫,或者某些類需要動態(tài)裝載,那些公共API就必須保留符號不變,這樣別人才能使用你的類庫。現(xiàn)有的混淆器提供了GUI或腳本的方式來對那些需要保留的符號名稱進(jìn)行配置,如果程序較大時配置工作變得很復(fù)雜,而程序一旦修改配置工作又要重新進(jìn)行。某些混淆器能夠調(diào)整字節(jié)碼的順序,使反編譯更加困難,但我經(jīng)歷過混淆之后的程序運行出錯的情況。
Q: Java混淆編譯器是如何工作的? A: Java混淆編譯器是在Sun JDK中提供的Java編譯器(javac)的基礎(chǔ)上完成的,修改了代碼生成過程,對編譯器生成的中間代碼進(jìn)行混淆,最后再生成class文件,這樣編譯和混淆只需要一個步驟就可以完成。另外可以在源程序中插入符號保留指令來控制哪些符號需要保留,不需要單獨的配置。
Q: 如何安裝和運行JOC? A: 下載joc.jar (http://www.apusic.com/product/cpsy.htm),運行java -jar joc.jar就可以啟動Java混淆編譯器,joc的命令行參數(shù)和javac完全相同,但增加了一個新的參數(shù)-Xobfuscate,它的用法如下: -Xobfuscate:<level> 其中<level>指定混淆級別,可以是以下幾種級別: -Xobfuscate:none不進(jìn)行混淆 -Xobfuscate:private 對所有private訪問級別的元素進(jìn)行混淆 -Xobfuscate:package 對所有private或package private元素進(jìn)行混淆 -Xobfuscate:protected 對所有private, package private, protected元素進(jìn)行混淆 -Xobfuscate:public對所有的元素都進(jìn)行混淆 -Xobfuscate:all 相當(dāng)于-Xobfuscate:public 如果使用-Xobfuscate不帶級別參數(shù),則相當(dāng)于-Xobfuscate:package
Q: 如何使用符號保留指令? A: 除了在命令行用-Xobfuscate參數(shù)控制符號混淆級別外,還可以在源代碼中使用符號保留指令來控制那些符號需要保留,符號保留指令是一個Java文檔注釋指令,可以插入在類和類成員的文檔注釋中,例如: /** * This class should preserve. * @preserve */ public class Foo { /** * You can specify which field should be preserved. * @preserve */ private int x;
/** * This field is not preserved. */ private int y;
/** * You can also preserve methods. * @preserve */ public void hello() {}
/** * This method is not preserved. */ private void collect() {} } 如果沒有@preserve指令,則根據(jù)混淆級別及成員的訪問級別來確定符號是否保留。
對于類的符號保留指令可以附帶一個保留級別參數(shù),來控制類成員的符號保留,包括: @preserve僅對類名進(jìn)行保留,類成員的保留根據(jù)-Xobfuscate命令行參數(shù)決定 @preserve public 保留所有public成員 @preserve protected保留所有public和protected成員 @preserve package保留所有public, protected, package private成員 @preserve private保留所有成員 @preserve all相當(dāng)于@preserve private
Q: JOC有哪些限制? A: 不支持分別編譯,必須對所有的源文件進(jìn)行混淆編譯。
最后給出一個JOC混淆的效果:
源文件:
import java.awt.event.*; import javax.swing.*;
public class AboutBox extends JDialog { public AboutBox() { initForm(); }
JPanel panel1 = new JPanel(); JButton button1 = new JButton(); JLabel jLabel2 = new JLabel(); JTextArea jTextArea1 = new JTextArea();
/** * NOTE: The following code is required by the form designer. * It can be modified using the form editor.Do not * modify it using the code editor. */
private void initForm() { this.setDefaultCloseOperation( WindowConstants.DISPOSE_ON_CLOSE ); this.getContentPane().setLayout( new java.awt.CardLayout()); this.setModal( true ); this.setResizable( false ); this.setTitle( "About..." ); panel1.setLayout( null ); button1.setText( "OK" ); button1.setBounds( 272, 168, 88, 24 ); panel1.add( button1 ); jLabel2.setText( "File System Viewer for Swing 1.1.1" ); jLabel2.setVerticalAlignment( SwingConstants.TOP ); jLabel2.setBounds( 64, 32, 240, 56 ); panel1.add( jLabel2 ); jTextArea1.setFont( new java.awt.Font( "Dialog", 0, 10 )); jTextArea1.setLineWrap( true ); jTextArea1.setOpaque( false ); jTextArea1.setText( "This computer program is protected by copyright law." ); jTextArea1.setWrapStyleWord( true ); jTextArea1.setBounds( 8, 112, 256, 80 ); panel1.add( jTextArea1 ); this.getContentPane().add( panel1, "Card1" ); this.setSize( 376, 228 ); button1.addActionListener( new java.awt.event.ActionListener(){ public void actionPerformed( java.awt.event.ActionEvent ev ){ button1_actionPerformed( ev ); }}); }
private void button1_actionPerformed(ActionEvent ev) { this.dispose(); } }
經(jīng)Javac編譯后用JAD反編譯的結(jié)果:
import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; import javax.swing.text.JTextComponent;
public class AboutBox extends JDialog {
JPanel panel1; JButton button1; JLabel jLabel2; JTextArea jTextArea1;
public AboutBox() { panel1 = new JPanel(); button1 = new JButton(); jLabel2 = new JLabel(); jTextArea1 = new JTextArea(); initForm(); }
private void initForm() { setDefaultCloseOperation(2); getContentPane().setLayout(new CardLayout()); setModal(true); setResizable(false); setTitle("About..."); panel1.setLayout(null); button1.setText("OK"); button1.setBounds(272, 168, 88, 24); panel1.add(button1); jLabel2.setText("File System Viewer for Swing 1.1.1"); jLabel2.setVerticalAlignment(1); jLabel2.setBounds(64, 32, 240, 56); panel1.add(jLabel2); jTextArea1.setFont(new Font("Dialog", 0, 10)); jTextArea1.setLineWrap(true); jTextArea1.setOpaque(false); jTextArea1.setText("This computer program is protected by copyright law."); jTextArea1.setWrapStyleWord(true); jTextArea1.setBounds(8, 112, 256, 80); panel1.add(jTextArea1); getContentPane().add(panel1, "Card1"); setSize(376, 228); button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent actionevent) { button1_actionPerformed(actionevent); }
}); }
private void button1_actionPerformed(ActionEvent actionevent) { dispose(); } }
經(jīng)JOC混淆編譯后用JAD反編譯的結(jié)果:
import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; import javax.swing.text.JTextComponent;
public class AboutBox extends JDialog {
JPanel _$1; JButton _$2; JLabel _$3; JTextArea _$4;
public AboutBox() { _$1 = new JPanel(); _$2 = new JButton(); _$3 = new JLabel(); _$4 = new JTextArea(); _$1(); }
private void _$1() { 2; this; JVM INSTR swap ; setDefaultCloseOperation(); getContentPane().setLayout(new CardLayout()); true; this; JVM INSTR swap ; setModal(); false; this; JVM INSTR swap ; setResizable(); "About..."; this; JVM INSTR swap ; setTitle(); _$1.setLayout(null); _$2.setText("OK"); _$2; 168; 272; JVM INSTR swap ; 24; 88; JVM INSTR swap ; setBounds(); _$1.add(_$2); _$3.setText("File System Viewer for Swing 1.1.1"); _$3.setVerticalAlignment(1); _$3; 32; 64; JVM INSTR swap ; 56; 240; JVM INSTR swap ; setBounds(); _$1.add(_$3); _$4; JVM INSTR new #13<Class Font>; JVM INSTR dup ; 0; "Dialog"; JVM INSTR swap ; 10; Font(); setFont(); _$4.setLineWrap(true); _$4.setOpaque(false); _$4.setText("This computer program is protected by copyright law."); _$4.setWrapStyleWord(true); _$4; 112; 8; JVM INSTR swap ; 80; 256; JVM INSTR swap ; setBounds(); _$1.add(_$4); getContentPane().add(_$1, "Card1"); 376; this; JVM INSTR swap ; 228; setSize(); _$2.addActionListener(new IIlIlIIIIlllIIII(this)); return; }
private void _$1(ActionEvent actionevent) { dispose(); }
/* static void access$0(AboutBox aboutbox, ActionEvent actionevent) { actionevent; aboutbox; JVM INSTR swap ; _$1(); return; }
*/
// Unreferenced inner classes:
/* anonymous class */ final class IIlIlIIIIlllIIII implements ActionListener {
public void actionPerformed(ActionEvent actionevent) { AboutBox.access$0(AboutBox.this, actionevent); }
{ AboutBox.this; this; JVM INSTR swap ; this$0; } } }
|