20145307陈俊达_安卓逆向分析_dex2jar&jd-gui的使用

20145307陈俊达_安卓逆向分析_dex2jar&jd-gui的使用

引言

这次免考选择了四个项目,难度也是从简到难,最开始先写一个工具的使用

想要开发安卓首先要会编写代码,但是想要逆向分析安卓的话就要对它的输出文件结构有所了解。首先我们要先了解安卓的输出文件结构。

start!

开发工具一般就是eclipse和官方的安卓studio,两者文件结构略有不同,这里用官方的安卓studio也就是IDEA IDE+SDK来进行讲解

app文件夹就是src文件夹用来存放java相关代码,就是源文件。build是构建文件,gradle这个东西是编译输出apk的核心,一会重点!重点!介绍。后缀名为properties的文件在混淆编译 保护代码的时候也有重大作用,一会也要重点介绍。res文件为资源文件夹用来存放代码引用的资源等。

环境配置

电脑win 10 64bit,ide选择安卓studio,sdk选择安卓7.1.1(SDK 25),下载后安装!

这里要注意我大天朝可是不能连接谷歌的,所以会报错无法连接服务器,这时候使用代理VPN或者修改proxy 推荐大连东软的mirrors.neusoft.edu.cn 端口号80.

之后你以为就完了吗?no,你想输出apk还需要gradle这个东西,还要自己去配置!官网:http://services.gradle.org/distributions/ 下载好对应的文件后解压到C盘usr目录的gradle/xxx目录下替换源文件,亲测必须要替换!不然天朝的网会编译等待一百年!

正式开始

终于开始了。。。

第一篇讲解已知apk如何逆向出元安卓代码

首先创建一个安卓的项目,选择loginactivity的xml,SDK选择7.1.1就好,之后敲代码,我们也不来难的,就写一个简单得登录代码,有id有pwd。

public class LoginActivity extends Activity {  
  
    private final String ACCOUNT="jclemo";  
    private final String PASSWORD="123456";  
    private EditText etAccount, etPassword;  
    private Button btnLogin;  
  
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_login);  
  
        etAccount=(EditText)findViewById(R.id.et_account);  
  
        etPassword=(EditText)findViewById(R.id.et_password);  
  
        btnLogin=(Button)findViewById(R.id.btn_login);  
  
        btnLogin.setOnClickListener(new View.OnClickListener() {  
            @Override  
            public void onClick(View v) {  
                if (isOK(etAccount.getText().toString(), etPassword.getText().toString())) {  
                    Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();  
                } else {  
                    Toast.makeText(LoginActivity.this, "登录失败", Toast.LENGTH_SHORT).show();  
                }  
  
            }  
        });  
  
    }  
  
    private boolean isOK(String account, String password){  
        return account.equals(ACCOUNT) && password.equals(PASSWORD);  
    }  
  
}  

.xml布局文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    xmlns:tools="http://schemas.android.com/tools"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent">  
  
    <LinearLayout  
        android:layout_width="match_parent"  
        android:layout_height="wrap_content"  
        android:orientation="vertical"  
        android:layout_centerInParent="true">  
  
        <LinearLayout  
            android:layout_width="match_parent"  
            android:layout_height="wrap_content"  
            android:gravity="center_horizontal"  
            android:orientation="horizontal">  
  
            <TextView  
                android:layout_width="wrap_content"  
                android:layout_height="wrap_content"  
                android:text="帐号:"/>  
  
            <EditText  
                android:id="@+id/et_account"  
                android:layout_width="100dp"  
                android:layout_height="wrap_content" />  
  
        </LinearLayout>  
  
        <LinearLayout  
            android:layout_width="match_parent"  
            android:layout_height="wrap_content"  
            android:gravity="center_horizontal"  
            android:orientation="horizontal">  
  
            <TextView  
                android:layout_width="wrap_content"  
                android:layout_height="wrap_content"  
                android:text="密码:"/>  
  
            <EditText  
                android:id="@+id/et_password"  
                android:layout_width="100dp"  
                android:layout_height="wrap_content" />  
  
        </LinearLayout>  
  
        <Button  
            android:id="@+id/btn_login"  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:layout_gravity="center_horizontal"  
            android:text="登录"/>  
  
    </LinearLayout>  
  
</RelativeLayout>  

签名打包 Build>Generate Signed APK

yeah,不报错

将导出的xxx.apk文件的后缀.apk改为.zip,然后解压,提取出.class文件。如果你的dex2jar版本很高的话是不需要解压的直接拖拽也行。

下载dex2jar和jd-gui 附上下载链接: http://download.csdn.net/download/ygd1994/9565391

用cmd,并在dex2jar所在窗口的dos位置输入 dex2jar 路径名+classes.dex,会在本目录下生成一个.jar后缀的名字的文件。

但是jar文件是不可以直接看的,要用到配套的工具jd-gui.exe打开。

哇!震惊全源码,这也太不安全了吧!

这可不行,思考一下解决方法!

改进 混淆编译

不加处理的话直接显示源代码,这太伤了,那如何改进呢?我们使用混淆编译!

谷歌他们自己从SDK 2.3之后就为开发者提供了一个代码混淆工具proguard,新版的adt创建的工程默认都会有多两个代码混淆文件。我们的文件夹中会带一个.properties和.cfg文件,这就是我们混淆编译的根本。你直接build&run是不会混淆编译的,我们需要在project.properties文件中添加一句 proguard.config=proguard.cfg 才能手动开启混淆编译

行吧,2234再来一次吧!

我们来看看之后逆向后的代码

public class LoginActivity extends Activity  
{  
  private final String a = "jclemo";  
  private final String b = "123456";  
  private EditText c;  
  private EditText d;  
  private Button e;  
  
  private boolean a(String paramString1, String paramString2)  
  {  
    return (paramString1.equals("jclemo")) && (paramString2.equals("123456"));  
  }  
  
  protected void onCreate(Bundle paramBundle)  
  {  
    super.onCreate(paramBundle);  
    setContentView(2130968601);  
    this.c = ((EditText)findViewById(2131492944));  
    this.d = ((EditText)findViewById(2131492945));  
    this.e = ((Button)findViewById(2131492946));  
    this.e.setOnClickListener(new a(this));  
  }  
}  

哇!最直观的一点,所有的关键变量名都变成了abcd!这就相对来说安全很多了啊,看来这一点很有用吧,算我们尝试改进成功!

总结

第一个小例子让我们了解了安卓的逆向与改进办法,之后我们来继续研究其他的逆向方法,加油嘻嘻!

posted @ 2017-06-11 09:36  20145307陈俊达  阅读(436)  评论(0编辑  收藏  举报