Java混淆編譯器(轉apusic.com)

來源:互聯網
上載者:User
編譯 最近試用了幾個Java混淆器(Java Obfuscator),感覺沒有一個完全另人滿意的,於是想乾脆自己寫一個得了。翻了幾頁Java虛擬機器規範之後突發奇想,別的混淆器都是在編譯好的byte code上做文章,能不能從源碼直接編譯成經過混淆的class檔案呢?就這樣花了一個多星期的時間寫了一個Java混淆編譯器(Java Obfuscator Compiler)。


Q: 什麼是混淆器?
A: 由於Java程式運行時是動態串連的,因此編譯成的目標檔案中包含有符號表,使得Java程式很容易被反編譯,混淆器可以打亂class檔案中的符號資訊,使反向工程變得非常困難。


Q: 現有的混淆器有什麼問題?
A: 現有的混淆器都是對編譯好的class檔案進行混淆,這樣就需要編譯和混淆兩個步驟。並不是所有的符號都需要混淆,如果你開發的是一個類庫,或者某些類需要動態裝載,那些公用API就必須保留符號不變,這樣別人才能使用你的類庫。現有的混淆器提供了GUI或指令碼的方式來對那些需要保留的符號名稱進行配置,如果程式較大時配置工作變得很複雜,而程式一旦修改配置工作又要重新進行。某些混淆器能夠調整位元組碼的順序,使反編譯更加困難,但我經曆過混淆之後的程式運行出錯的情況。


Q: Java混淆編譯器是如何工作的?
A: Java混淆編譯器是在Sun JDK中提供的Java編譯器(javac)的基礎上完成的,修改了代碼產生過程,對編譯器產生的中間代碼進行混淆,最後再產生class檔案,這樣編譯和混淆只需要一個步驟就可以完成。另外可以在來源程式中插入符號保留指令來控制哪些符號需要保留,不需要單獨的配置。


Q: 如何安裝和運行JOC?
A: 下載joc.jar (http://www.apusic.com/product/cpsy.htm),運行java -jar joc.jar就可以啟動Java混淆編譯器,joc的命令列參數和javac完全相同,但增加了一個新的參數-Xobfuscate,它的用法如下:
       -Xobfuscate:<level>
其中<level>指定混淆層級,可以是以下幾種層級:
       -Xobfuscate:none        不進行混淆
       -Xobfuscate:private     對所有private存取層級的元素進行混淆
       -Xobfuscate:package     對所有private或package private元素進行混淆
       -Xobfuscate:protected   對所有private, package private, protected元素進行混淆
       -Xobfuscate:public      對所有的元素都進行混淆
       -Xobfuscate:all         相當於-Xobfuscate:public
如果使用-Xobfuscate不帶層級參數,則相當於-Xobfuscate:package


Q: 如何使用符號保留指令?
A: 除了在命令列用-Xobfuscate參數控制符號混淆層級外,還可以在原始碼中使用符號保留指令來控制那些符號需要保留,符號保留指令是一個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指令,則根據混淆層級及成員的存取層級來確定符號是否保留。


對於類的符號保留指令可以附帶一個保留層級參數,來控制類成員的符號保留,包括:
       @preserve            僅對類名進行保留,類成員的保留根據-Xobfuscate命令列參數決定
       @preserve public     保留所有public成員
       @preserve protected  保留所有public和protected成員
       @preserve package    保留所有public, protected, package private成員
       @preserve private    保留所有成員
       @preserve all        相當於@preserve private


Q: JOC有哪些限制?
A: 不支援分別編譯,必須對所有的源檔案進行混淆編譯。




最後給出一個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();
   }
}


經Javac編譯後用JAD反編譯的結果:


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();
   }
}


經JOC混淆編譯後用JAD反編譯的結果:


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;
           }
   }
}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.