java class 还原_Java .class文件反编译后的特殊代码还原

本文详细介绍了Java .class文件反编译后遇到的一些特殊代码,如循环、异常处理等的还原规则。通过实例展示了如何理解并重构jad反编译后的不正常代码,帮助开发者更好地进行代码分析和维护。
摘要由CSDN通过智能技术生成

java class反编译后的代码还原(一)

Java class 利用jad 反编译之后,偶尔回碰到一些不正常的代码,例如:label0 :_L1 MISSING_BLOCK_LABEL_30、JVM INSTR ret 7 、JVM INSTR tableswitch 1 3: default 269、 JVM INSTR monitorexit、JVM INSTR monitorenter,这些一般是由特殊的for循环、try catch finally语句块、synchronized语句反编译后产生的。下面,就简单介绍一下,一些反编译后的特殊代码的还原规则。

第一部分、for、while循环

1、普通的循环,原始

public voidf1() {boolean flag = false;if (Boolean.getBoolean("sys")) {

System.out.println("sys");

}else{for (int i = 0; i < 10; i++) {

flag= Boolean.getBoolean("sys");if(flag) {

System.exit(0);

}

}

}

}

反编译后的代码

public voidf1()

{boolean flag = false;if(Boolean.getBoolean("sys"))

{

System.out.println("sys");

}else{for(int i = 0; i < 10; i++)

{

flag= Boolean.getBoolean("sys");if(flag)

System.exit(0);

}

}

}

2、反编译后代码变的很古怪,这是java原代码

public voidf2() {int[] list = new int[] { 1, 2, 3, 4};if (Boolean.getBoolean("sys")) {

System.out.println("sys");

}else{

check:while (true) {for (int i = 0; i < list.length; i++) {if (list[i] == 2) {continuecheck;

}else{break;

}

}

}

}

}

Java反编译后的代码,部分逻辑丢失。

public voidf2()

{int list[] ={1, 2, 3, 4};if(Boolean.getBoolean("sys"))

System.out.println("sys");else

do{int i = 0;if(i >= list.length || list[i] != 2);

}while(true);

}

3、就是比f2()多了一行System.out.println("list[i]");,反编译后也挺怪的。源码如下:

public voidf3() {int[] list = new int[] { 1, 2, 3, 4};if (Boolean.getBoolean("sys")) {

System.out.println("sys");

}else{

check:while (true) {for (int i = 0; i < list.length; i++) {

System.out.println("list[i]");if (list[i] == 2) {continuecheck;

}else{break;

}

}

}

}

}

反编译后的代码:

public voidf3()

{int list[] ={1, 2, 3, 4};if(Boolean.getBoolean("sys"))

System.out.println("sys");else

do{inti;doi= 0;while(i >=list.length);

System.out.println("list[i]");if(list[i] != 2);

}while(true);

}

4、f2()中的break语言,移动了位置。源码如下:

public voidf4() {int[] list = new int[] { 1, 2, 3, 4};if (Boolean.getBoolean("sys")) {

System.out.println("sys");

}else{

check:while (true) {for (int i = 0; i < list.length; i++) {if (list[i] == 2) {continuecheck;

}

}break;

}

}

}

反编译后代码:

public voidf4()

{int list[] ={1, 2, 3, 4};inti;if(Boolean.getBoolean("sys"))

System.out.println("sys");elselabel0:do{for(i = 0; i < list.length; i++)if(list[i] == 2)continuelabel0;break;

}while(true);

}

5、就是比f4()多了一行System.out.println("list[i]");,反编译后相当怪的。源码如下:

public voidf5() {int[] list = new int[] { 1, 2, 3, 4};if (Boolean.getBoolean("sys")) {

System.out.println("sys");

}else{

check:while (true) {for (int i = 0; i < list.length; i++) {

System.out.println("list[i]");if (list[i] == 2) {continuecheck;

}

}break;

}

}

}

反编译后比较晕的代码:

public voidf5()

{int list[] ={1, 2, 3, 4};if(!Boolean.getBoolean("sys")) goto _L2; else goto_L1

_L1:

System.out.println("sys");goto_L3

_L2:int i = 0;goto_L4

_L6:

System.out.println("list[i]");if(list[i] != 2) goto _L5; else goto_L2

_L5:

i++;

_L4:if(i < list.length) goto _L6; else goto_L3

_L3:

}

6、就是比f5()多了一行System.exit(0);代码,但是差异确很大。源码如下:

public voidf6() {int[] list = new int[] { 1, 2, 3, 4};if (Boolean.getBoolean("sys")) {

System.out.println("sys");

}else{

check:while (true) {for (int i = 0; i < list.length; i++) {

System.out.println("list[i]");if (list[i] == 2) {continuecheck;

}

}

System.exit(0);break;

}

}

}

编译后代码,比f5()差异太大了。

public voidf6()

{intlist[];

list= (new int[] {1, 2, 3, 4});if(Boolean.getBoolean("sys"))

{

System.out.println("sys");breakMISSING_BLOCK_LABEL_75;

}

_L2:int i = 0;goto_L1

_L5:

System.out.println("list[i]");if(list[i] != 2) goto _L3; else goto_L2

_L3:

i++;

_L1:if(i < list.length) goto _L5; else goto_L4

_L4:

System.exit(0);

}

7、差异就是f6()中的System.exit(0);移动了位置,但是差异确很大。源码如下:

public voidf7() {int[] list = new int[] { 1, 2, 3, 4};if (Boolean.getBoolean("sys")) {

System.out.println("sys");

}else{

check:while (true) {for (int i = 0; i < list.length; i++) {

System.out.println("list[i]");if (list[i] == 2) {continuecheck;

}

}break;

}

System.exit(0);

}

}

编译后代码,比f6()差异太大了。

public voidf7()

{intlist[];

list= (new int[] {1, 2, 3, 4});if(Boolean.getBoolean("sys"))

{

System.out.println("sys");breakMISSING_BLOCK_LABEL_75;

}

_L2:int i = 0;goto_L1

_L5:

System.out.println("list[i]");if(list[i] != 2) goto _L3; else goto_L2

_L3:

i++;

_L1:if(i < list.length) goto _L5; else goto_L4

_L4:

System.exit(0);

}

8、逻辑和f7比没有变,只是多了一些System.out.println()代码。

public voidf8() {int[] list = new int[] { 1, 2, 3, 4};if (Boolean.getBoolean("sys")) {

System.out.println("sys");

}else{

System.out.println(":check while");

check:while (true) {

System.out.println("for");for (int i = 0; i < list.length; i++) {

System.out.println("list[i]");if (list[i] == 2) {continuecheck;

}

}

System.out.println("break");break;

}

System.out.println("exit(0)");

System.exit(0);

}

}

反编译后的代码:和f7()比较一下,基本就可以确定反编译后的代码对应关系了。

public voidf8()

{intlist[];

list= (new int[] {1, 2, 3, 4});if(Boolean.getBoolean("sys"))

{

System.out.println("sys");breakMISSING_BLOCK_LABEL_107;

}

System.out.println(":check while");

_L2:inti;

System.out.println("for");

i= 0;goto_L1

_L5:

System.out.println("list[i]");if(list[i] != 2) goto _L3; else goto_L2

_L3:

i++;

_L1:if(i < list.length) goto _L5; else goto_L4

_L4:

System.out.println("break");

System.out.println("exit(0)");

System.exit(0);

}

9、逻辑和f8比没有变,只是多了一行System.out.println()代码,导致了反编译后的。

public voidf9() {int[] list = new int[] { 1, 2, 3, 4};if (Boolean.getBoolean("sys")) {

System.out.println("sys");

}else{

System.out.println(":check while");

check:while (true) {

System.out.println("for");for (int i = 0; i < list.length; i++) {

System.out.println("list[i]");if (list[i] == 2) {

System.out.println("continue check");continuecheck;

}

}

System.out.println("break");break;

}

System.out.println("exit(0)");

System.exit(0);

}

}

}

反编译后的代码:

public voidf9()

{int list[] ={1, 2, 3, 4};if(!Boolean.getBoolean("sys")) goto _L2; else goto_L1

_L1:

System.out.println("sys");goto_L3

_L2:

System.out.println(":check while");

_L5:

System.out.println("for");for(int i = 0; i < list.length; i++)

{

System.out.println("list[i]");if(list[i] != 2)continue;

System.out.println("continue check");continue;

}

System.out.println("break");

System.out.println("exit(0)");

System.exit(0);

_L3:return;if(true) goto _L5; else goto_L4

_L4:

}

}

java class反编译后的代码还原(二)

Java class 利用jad 反编译之后,偶尔回碰到一些不正常的代码,例如:label0 :_L1 MISSING_BLOCK_LABEL_30、JVM INSTR ret 7、JVM INSTR tableswitch 1 3: default 269、 JVM INSTR monitorexit、JVM INSTR monitorenter,这些一般是由特殊的for循环、try catch finally语句块、synchronized语句反编译后产生的。下面,就简单介绍一下,一些反编译后的特殊代码的还原规则。本文在Jdk 1.4.2_08+jad 1.58f下测试。jad 1.5.8f可以到这里http://download.csdn.net/source/470540 下载。

第二部分、异常

下面的代码前提是类中有如下属性,  Calendar cal = Calendar.getInstance();。

1、Exceptioin的还原    反编译后的代码如下:

public booleanf1()

{return cal.getTime().after(newDate());

Exception e;

e;

e.printStackTrace();return false;

}

还原后的Java代码

public booleanf1()

{try{return cal.getTime().after(newDate());

}catch(Exception e)

{

e.printStackTrace();return false;

}

}

2、finally代码的还原 反编译后的Java代码如下:

public booleanf2()

{boolean flag = cal.getTime().after(newDate());

System.out.println("finally");returnflag;

Exception e;

e;

e.printStackTrace();

System.out.println("finally");return false;

Exception exception;

exception;

System.out.println("finally");throwexception;

}

还原后的代码如下:

public booleanf2()

{try{return cal.getTime().after(newDate());

}catch(Exception e)

{

e.printStackTrace();return false;

}finally{

System.out.println("finally");

}

}

3、MISSING_BLOCK_LABEL_的还原反编译后的代码

publicObject f22()

{

Date date=cal.getTime();

System.out.println("finally");returndate;

Exception e;

e;

e.printStackTrace();

System.out.println("finally");breakMISSING_BLOCK_LABEL_45;

Exception exception;

exception;

System.out.println("finally");throwexception;return null;

}

还原后的Java代码

publicObject f22()

{try{returncal.getTime();

}catch(Exception e)

{

e.printStackTrace();

}finally{

System.out.println("finally");

}return null;

}

4、异常中:label的还原反编译后的代码

publicString f4()throwsException

{

l0:

{try{

Integer i= new Integer(1);if(i.intValue() > 0)

{

System.out.println(i);breaklabel0;

}

System.err.println(i);

}catch(Exception dae)

{

System.err.println(dae);throw newRuntimeException(dae);

}return null;

}return "Hello";

}

注意,这个代码有点诡异,实际代码如下:

public String f4() throwsException

{try{

Integer i= new Integer(1);if (i.intValue() > 0)

{

System.out.println(i);

}else{

System.err.println(i);return null;

}return "Hello";

}catch(Exception dae)

{

System.err.println(dae);throw newRuntimeException(dae);

}

}

5、典型数据库操作代码还原 反编译后代码

publicHashMap f5()

{

Connection conn= null;

HashMap hashmap;

HashMap map= newHashMap();

Class.forName("");

conn= DriverManager.getConnection("jdbc:odbc:");

PreparedStatement pstmt= conn.prepareStatement("select * from table");

pstmt.setString(1, "param");

String columnVallue;for(ResultSet rs = pstmt.executeQuery(); rs.next(); map.put(columnVallue, ""))

columnVallue= rs.getString("column");

hashmap=map;if(conn != null)try{

conn.close();

}catch(SQLException sqlce)

{

sqlce.printStackTrace();

}returnhashmap;

ClassNotFoundException cnfe;

cnfe;

cnfe.printStackTrace();if(conn != null)try{

conn.close();

}catch(SQLException sqlce)

{

sqlce.printStackTrace();

}breakMISSING_BLOCK_LABEL_188;

SQLException sqle;

sqle;

sqle.printStackTrace();if(conn != null)try{

conn.close();

}catch(SQLException sqlce)

{

sqlce.printStackTrace();

}breakMISSING_BLOCK_LABEL_188;

Exception exception;

exception;if(conn != null)try{

conn.close();

}catch(SQLException sqlce)

{

sqlce.printStackTrace();

}throwexception;return null;

}

实际代码如下:

publicHashMap f5()

{

Connection conn= null;try{

HashMap map= newHashMap();

Class.forName("");

conn= DriverManager.getConnection("jdbc:odbc:");

PreparedStatement pstmt= conn.prepareStatement("select * from table");

pstmt.setString(1, "param");

ResultSet rs=pstmt.executeQuery();while(rs.next())

{

String columnVallue= rs.getString("column");

map.put(columnVallue,"");

}returnmap;

}catch(ClassNotFoundException cnfe)

{

cnfe.printStackTrace();

}catch(SQLException sqle)

{

sqle.printStackTrace();

}finally{if (conn != null)

{try{

conn.close();

}catch(SQLException sqlce)

{

sqlce.printStackTrace();

}

}

}return null;

}

6、两层异常嵌套代码还原 反编译后的代码

public intf6()

{int i = cal.getTime().compareTo(newDate());

System.out.println("finally");returni;

Exception e1;

e1;

e1.printStackTrace();

System.out.println("finally");return -1;

Exception e2;

e2;

e2.printStackTrace();

System.out.println("finally");return -2;

Exception exception;

exception;

System.out.println("finally");throwexception;

}

实际代码

public intf6()

{try{try{return cal.getTime().compareTo(newDate());

}catch(Exception e1)

{

e1.printStackTrace();return -1;

}

}catch(Exception e2)

{

e2.printStackTrace();return -2;

}finally{

System.out.println("finally");

}

}

7、非常诡异的代码 反编译后的代码

public intf7()

{int i = cal.getTime().compareTo(newDate());

System.out.println("finally");returni;

Exception e1;

e1;

e1.printStackTrace();

_L2:

System.out.println("finally");return -1;

Exception e2;

e2;

e2.printStackTrace();if(true) goto _L2; else goto_L1

_L1:

Exception exception;

exception;

System.out.println("finally");throwexception;

}

原始代码

public intf7()

{try{try{return cal.getTime().compareTo(newDate());

}catch(Exception e1)

{

e1.printStackTrace();return -1;

}

}catch(Exception e2)

{

e2.printStackTrace();return -1;

}finally{

System.out.println("finally");

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值