反编译之JD-GUI代码逻辑分析

反编译后代码:
public static void notify(Context context, String type, final String no, String money, String mark, String time) {
    if ((TextUtils.isEmpty(str2)) || (TextUtils.isEmpty(str4))) {
        sendmsg(context, "异常");
        return;
    }
    String str1 = "";
    String str3 = AbSharedUtil.getString(context, type + "balance");
    if (type.equals("ccc")) {
        str1 = AbSharedUtil.getString(context, "ccc");
    }
    for (; ; ) {
        HttpUtils httpUtils = new HttpUtils(15000);
        str4 = MD5.md5(time + mark + money + no + type + str4);
        RequestParams requestParams = new RequestParams();
        requestParams.addBodyParameter("balance", str3);
        if (!TextUtils.isEmpty(str1)) {
            requestParams.addBodyParameter("account", str1);
        }
        requestParams.addBodyParameter("sign", str4);
        httpUtils.send(HttpRequest.HttpMethod.POST, str2, requestParams, new RequestCallBack() {

            public void onFailure(HttpException paramAnonymousHttpException, String paramAnonymousString) {
                update(no, paramAnonymousString);
            }

            public void onSuccess(ResponseInfo<String> responseInfo) {
                responseInfo = (String) responseInfo.result;
                for (; ; ) {
                    update(no, responseInfo);
                    return;
                }
            }
        });
        return;
        if (type.equals("aaa")) {
            str1 = AbSharedUtil.getString(context, "aaa");
        } else if (type.equals("bbb")) {
            str1 = AbSharedUtil.getString(context, "bbb");
        }
    }
}

原代码:
public static void notify(final Context context, String type, final String no, String money, String mark, String time) {
    if (!TextUtils.isEmpty(notifyurl) && !TextUtils.isEmpty(signkey)) {
        String account = "";
        String balance = AbSharedUtil.getString(context, type + "balance");
        if (type.equals("ccc")) {
            account = AbSharedUtil.getString(context, "ccc");
        } else if (type.equals("aaa")) {
            account = AbSharedUtil.getString(context, "aaa");
        } else if (type.equals("bbb")) {
            account = AbSharedUtil.getString(context, "bbb");
        }

        HttpUtils httpUtils = new HttpUtils(15000);
        signkey = MD5.md5(time + mark + money + no + type + signkey);
        RequestParams params = new RequestParams();
        params.addBodyParameter("balance", balance);
        if (!TextUtils.isEmpty(account)) {
            params.addBodyParameter("account", account);
        }

        params.addBodyParameter("sign", signkey);
        httpUtils.send(HttpMethod.POST, notifyurl, params, new RequestCallBack<String>() {

            public void onFailure(HttpException e, String msg) {
                update(no, msg);
            }

            public void onSuccess(ResponseInfo<String> resp) {
                String result = (String)resp.result;
                update(no, result);
            }
        });
    } else {
        sendmsg(context, "异常");
    }
}

for循环前后对比:

public static boolean isServiceRunning(Context context, String serviceName) {
    if (("".equals(serviceName)) || (serviceName == null)) {
        return false;
    }
    context = (ArrayList) ((ActivityManager) context.getSystemService("activity")).getRunningServices(30);
    int i = 0;
    for (; ; ) {
        if (i >= context.size()) {
            return false;
        }
        if ((context.get(i)).service.getClassName().toString().equals(serviceName)) {
            return true;
        }
        i += 1;
    }
}


public static boolean isServiceRunning(Context context, String serviceName) {
    if (!"".equals(serviceName) && serviceName != null) {
        
        ArrayList list = (ArrayList)((ActivityManager)context.getSystemService("activity")).getRunningServices(30);
        for(int i = 0; i < list.size(); ++i) {
            if (list.get(i).service.getClassName().toString().equals(serviceName)) {
                return true;
            }
        }
        return false;
    } else {
        return false;
    }
}



while循环前后对比:

public static int isActivityTop(Context context) {
    try {
        Iterator iterator = ((ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE)).getRunningTasks(100).iterator();
        ActivityManager.RunningTaskInfo taskInfo;
        do {
            if (!iterator.hasNext()) {
                return 0;
            }
            taskInfo = (ActivityManager.RunningTaskInfo) iterator.next();
        }
        while (!taskInfo.topActivity.getClassName().equals("xxxyActivity"));
        int i = taskInfo.numActivities;
        return i;
    } catch (SecurityException e) {
        sendmsg(context, e.getMessage());
    }
    return 0;
}

public static int isActivityTop(Context context) {
    try {
        Iterator iterator = ((ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE)).getRunningTasks(100).iterator();

        while(iterator.hasNext()) {
            RunningTaskInfo taskInfo = (RunningTaskInfo)iterator.next();
            if (taskInfo.topActivity.getClassName().equals("xxxyActivity"));
                int num = taskInfo.numActivities;
                return num;
            }
        }

        return 0;
    } catch (SecurityException e) {
        sendmsg(context, e.getMessage());
        return 0;
    }
}

用混淆器打乱的代码,反编译后,要想看懂也不是一件容易的事。因为大部人都会用免费的混淆器来混淆源代码,大部份反编译过来的代码就有一定的规则可寻:

反编译后的代码一般会产生以下结构的代码,(代码结构是个人总结的,如有雷同,纯属巧合)比较难看懂,本文章根据实现情况对这几种结构做个简单说明。

1,反编译后的代码:if while结构

if (BTActivity.access$2300(this.this$0))
    {
      int i = BTActivity.access$700(this.this$0).sendCommand(1028, 4, paramInt);
      BTActivity.access$2500(this.this$0).notifyDataSetChanged();
    }
    while (true)
    {
      return;
      Bluetooth localBt = BTActivity.access$700(this.this$0);
      int k = BTActivity.access$600(this.this$0);
      int l = localBt.sendCommand(1026, paramInt, k);
     }

 (1)只分析逻辑
这里应该有一个if else的逻辑。从上面的代码分析,return下的代码应该是永远不会被执行,但这是不可能的。所以上面的代码逻辑(我们先不看语句)应该是这样的,  去掉while和return,加上else,修改后如下:

 if (BTActivity.access$2300(this.this$0))
    {
       int i = BTActivity.access$700(this.this$0).sendCommand(1028, 4, paramInt);
       BTActivity.access$2500(this.this$0).notifyDataSetChanged();
    }
  else
  {
      Bluetooth localBt = BTActivity.access$700(this.this$0);
      int k = BTActivity.access$600(this.this$0);
      int l = localBt.sendCommand(1026, paramInt, k);
    }

  (2)逻辑分析完了,继续分析语句.
      A)像:int i = BTActivity.access$700(this.this$0).sendCommand(1028, 4, paramInt);
            这个是引用外部的一个类中的一个方法。然后返回整型,但这个返回值用不到,所以去掉int i 
      还原应该这样:
      BTActivity.access$700(this.this$0).sendCommand(1028, 4, paramInt);
      
      B)access$700是什么意思呢?这是BTActivity类中的一个对象,对象有sendCommand方法。这样就可以查BTActivity类定义的哪个对象有这样一个方法了,这好理解。不好理解的是:int k = BTActivity.access$600(this.this$0);  中的access$600,它对应的是BTActivity中的哪个方法呢?这很难确定,所以只能猜:
      1,看这个类中有多少个方法,如果只有一个,那指定就是它了。
      2,如果有多个,那就要看参数。如果只有一个方法的参数与之相对应,那一定是它。
      3,如果参数一样的也有多个。那看逻辑。如果看不出来,只有猜或添加自己的逻辑。
   
   (3)为什么会有access$700这样的方法/属性名称?原因是,调用者和被调者不在同一个类中。如果两者在同一个类(包括内部类),反编译后方法/属性名称不会有数字。
   
   (4) 再举一个更简单的if else例

if (paramBoolean)
        paramTextView.setTextColor(-16727809);
       while (true)
       {
         return;
         paramTextView.setTextColor(-1315861);
       }

            还原成真正的原始代码:

 if (paramBoolean)
         {
           paramTextView.setTextColor(-16727809);
         }
         else
         {
            paramTextView.setTextColor(-1315861);
         }  

2, 反编译后的代码:switch case while结构   

switch (this.mBand)
    {
     default:
     case 0:
     case 1:
     case 2:
    }
    while (true)
    {
      return;
      this.mBand.setText("FM1");
      continue;
      this.mBand.setText("FM2");
      continue;
      this.mBand.setText("AM");
    }

   (1)分析逻辑:根据mBand的不同值,设置文本的显示内容。还原成原始代码:  

  switch (mBand)
    {

     case 0:
      mBand.setText("FM1");
      break;
     case 1:
       mBand.setText("FM2");
       break;
     case 2:
       mBand.setText("AM");
       break;
     default:
    }

   (2)关键:一个continue对你应着一个case的结束;。

3,反编译后代码如下:if for while结构 

   int i15 = this.freq;
      int i16 = this.rmin;
      if (i15 < i16)
        i17 = this.min;
      int i29;
      for (this.freq = i17; ; this.freq = i29)
      {
        int i27;
        int i28;
        do
        {
          this.x = getWidth();
          this.y = -1;
          break label32:
          i27 = this.freq;
          i28 = this.max;
        }
        while (i27 <= i28);
        i29 = this.max;
      }
      this.y = 0;
      invalidate();

  (1)代码逻辑分析:保证freq的值在min和max之间。分析后得到的原始源代码:

  if (freq < min)
      {
       freq = min;
      }
      
      if (freq <= max)
      {
         x = getWidth();
         y = -1;  
      }
      else
      {
       freq  = max;
       y = 0;
       invalidate();
      }

 4, 解析switch for while结构代码

   PowerManager localPowerManager = (PowerManager)getSystemService("power");
    switch (paramInt)
    {
    default:
    case 0:
    case 1:
    }
    for (String str = "on"; ; str = "other")
      while (true)
      {
        PowerManager.WakeLock localWakeLock = localPowerManager.newWakeLock(6, str);
        localWakeLock.acquire();
        localWakeLock.release();
        return;
        str = "off";
      }

还原源代码:    

 PowerManager localPowerManager = (PowerManager)getSystemService("power");
     String str = null;
     switch (paramInt)
     {
      case 0:
       str = "on";
       break;
      case 1:
       str = "off";
       break;
      default:
       str = "other";
       break;
     }

     PowerManager.WakeLock localWakeLock = localPowerManager.newWakeLock(6, str);
        localWakeLock.acquire();
        localWakeLock.release();

5, 分析返编译后的代码(if while结构)

 例1:  

 if (paramInt1 == 0)
      this.mMessage.setText("");
    while (true)
    {
      this.mAdditionalMessage.setVisibility(8);
      int i = this.mLevel.getMax();
      if (paramInt2 != i)
        this.mLevel.setMax(paramInt2);
      Toast localToast = this.mToast;
      ...... 

      return;
      TextView localTextView = this.mMessage;
      String str = "" + paramInt1;
      localTextView.setText(str);
    }

  分析:1,先去掉“this"

              2,看返编译后的按顺序逻辑走一遍。可以看出while到return这段代码,不管怎么样都会执行的。所以原始代码应该是这样的:    

setSmallIcon(paramInt1);
     paramInt1 &= 2147483647;
     if (paramInt1 == 0)
     {
       mMessage.setText("");
     }
     else
     {
       String str = "" + paramInt1;
       mMessage.setText(str);
     }
     mAdditionalMessage.setVisibility(8);
     if (paramInt2 != mLevel.getMax())
     {
       mLevel.setMax(paramInt2);
     }
     mToast.setView(mView);

      ......

 6,一个continue对应一个back原则(switch while结构)
在这种形式中,一个contiune一定是对应一个back,但一个case不一定只对应一个contiune,也有一个case对应两个或多个contiune(即back).
如以下反编译后的代码:


    分析代码:

1),上遍已对这种形式有讲过,一个continue对应一个case,但是你数一数会发现,对不上号,明显case多于contiune,原因是什么呢?其实switch里的default对应的是while中的return,在switch中default以上的case是用不着,是没有用的。

2),如果default上面的case没有用,聪明的你可以可能会问两个问题?
A,default上面的case没有用,为什么还会有呢?原因很简单,因为反编译器也不是全智能的总会有不对的(但是执行逻辑是不会有错),呵呵真正的原因当然不会是这样,你可以自己去分析一下,从整个程序分析就可以得出结论来。
B,那按一个continue对应一个back的原则不是有错吗? 当然没有。一个continue还是对应一个back, 聪明的你一定看懂了,每两个continue中间还有一个if语句,对,没错,你理解是对的,就是在这中间满足条件时就会back,所以就会有一个continue与之相对应。
  所这里每个if + continue + continue的形式对应一个case。









 

二 错误代码还原规则

if…else 语句:

反编译代码

if (paramBoolean)
        paramTextView.setTextColor(-16727809);
       while (true)
       {
         return;
         paramTextView.setTextColor(-1315861);
       } 还原后if (paramBoolean)
         {
           paramTextView.setTextColor(-16727809);
         }
         else
         {
            paramTextView.setTextColor(-1315861);
         }  



例子2:
if (responseInfo.contains("success")) {
    sendmsg(this, "发送成功" + responseInfo);
}
for (; ; ) {
    update(no, responseInfo);
    return;
    sendmsg(this, "发送失败" + responseInfo);   // 红色部分对应下方else分支
}

还原后变成
if (result.contains("success")) {
    sendmsg(context, "发送成功" + result);
} else {
    sendmsg(context, "发送失败" + result);
}
update(no, result);
 
 

if ..esle 反编译成 if …while(true)结构.

反编译代码
if (paramInt1 != 1)
        break label185;
      if (this.countChild_1 == null)  {
        this.countChild_1 = new PokerCountChild(this.mContext);
        this.countChild_1.setCount(paramInt2);
        addOneChild(this.countChild_1);
        if (paramInt2 == 0)
          this.countChild_1.setAlpha(0);
      }
      this.countChild_1.setCount(paramInt2);
    }
    label185:
    do
      return;
    while (paramInt1 != 2);
    if (this.countChild_2 == null)  {
      this.countChild_2 = new PokerCountChild(this.mContext);
      this.countChild_2.setCount(paramInt2);
      addOneChild(this.countChild_2);
      if (paramInt2 == 0)
        this.countChild_2.setAlpha(0);
    }
    this.countChild_2.setCount(paramInt2);

还原
if(i == 1)  {
                if(countChild_1 == null)  {
                    countChild_1 = new PokerCountChild(mContext);
                    countChild_1.setCount(j);
                    addOneChild(countChild_1);
                    if(j == 0)
                        countChild_1.setAlpha(0);
                }
                countChild_1.setCount(j);
            } else  if(i == 2)  {
                if(countChild_2 == null) {
                    countChild_2 = new PokerCountChild(mContext);                    
                    countChild_2.setCount(j);
                    addOneChild(countChild_2);
                    if(j == 0)
                        countChild_2.setAlpha(0);
                }
                countChild_2.setCount(j);
                return;
            }
会将语句倒序,出现break label结构

label1:{
    label2: {
        if () {
            break label2; //是要跳出label2,也就是label2 end的下一句
        }
    }//label2 end
}


 
另外,jd-gui有时会将while语句翻译成if,要将if改成while

switch语句

反编译代码

switch (this.mBand)
    {
     default:
     case 0:
     case 1:
     case 2:
    }
    while (true)
    {
      return;
      this.mBand.setText("FM1");
      continue;
      this.mBand.setText("FM2");
      continue;
      this.mBand.setText("AM");
    }
还原
switch (mBand)
    {
     case 0:
      mBand.setText("FM1");
      break;
     case 1:
       mBand.setText("FM2");
       break;
     case 2:
       mBand.setText("AM");
       break;
     default:
    }
switch规则就是一个continue对应一个case.要注意是是要外层的continue才算数,在if里的continue不算

猜你喜欢

转载自blog.csdn.net/LVXIANGAN/article/details/83339643