[Security_Android] exploit of Ad for android app代码分析


##########################################
Hacking Android Apps for Fun and Profit
##########################################
#Author: G13
#Twitter: @g13net
#Email: [email protected]
##########################################

##### 0x0 ToC #####

0x1 Intro
0x2 Dalvik Primer
0x3 Case Studies
0x4 Additional Notes
0x5 Resources

##### 0x1 Intro #####

Android is a mobile OS owned by Google.  Android allows developers to write applications("apps") for the OS and distribute them through
the Google Play Store.  These apps can be free or need to be purchased.  Free apps typically have ads in them to give the developer additional
revenue.  This paper will dive into patching disassembled Android apps for our benefit.

##### 0x2 Dalvik Primer #####

Android apps are generally written in Java.  When the app is compiled, the Java byte-code is converted into Dalvik bytecode(.dex files).  This conversion allows the apps to be run in the Dalvik VM environment that is used by Android.  

Once an app is disassembled, we are presented with Dalvik Opcodes, see the below example

## Code Snip ##

    iput-object p3, p0, Lb;->a:Ljava/io/Writer;

    .line 44
    and-int/lit8 v0, p2, 0x4

    if-eqz v0, :cond_0

    move v0, v2

    :goto_0
    iput-boolean v0, p0, Lb;->b:Z

    .line 46
    and-int/lit8 v0, p2, 0x1

    if-eqz v0, :cond_1

## End Snip ##

The if-xxx opcodes are conditional opcodes.  The :cond_1 specifies the jump point in the code when the condition is matched.  ‘move‘ moves the value of one register to another.  For more details on opcode references, see section 0x5 References for a link.

##### 0x3 Case Studies #####

#### 0x3a Coloring Book for Kids ####

App Name: Coloring Book for Kids
Goal: Remove Ads

For this app, we don‘t need to dive into Dalvik code.  We just have to inspect the contents of the layout files.  Once the app is disassembled, look in the
Res/layout/main.xml file.  This XML file describes where different widgets will be placed on the screen.  After review of the file we will come across
this section:

## Code Snip ##

<RelativeLayout android:orientation="vertical" android:id="@id/colorsLayout" android:layout_width="fill_parent" android:layout_height="fill_parent"
      xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads">
        <GridView android:gravity="center" android:id="@id/colorView" android:layout_width="fill_parent" android:layout_height="fill_parent" android:horizontalSpacing="15.0dip" android:verticalSpacing="0.0dip" android:stretchMode="columnWidth" android:columnWidth="30.0dip" android:numColumns="auto_fit" android:layout_above="@id/colorsAdMob"
          xmlns:android="http://schemas.android.com/apk/res/android" />
        <com.google.ads.AdView android:id="@id/colorsAdMob" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_alignParentBottom="true" ads:adUnitId="a14d5ae1ff5b91c" ads:adSize="BANNER" ads:testDevices="TEST_EMULATOR, TEST_DEVICE_ID" ads:loadAdOnCreate="true" />
    </RelativeLayout>

## End Snip ##

If we change the android:layout_width and android:layout_height attributes to be "0px" the ad will not be viewable on the screen.  The only downside to this approach is that the ad code will still run; so the app will still send your information off to the provider for statistics.  The changed code will look like this:

## Code Snip ##

<RelativeLayout android:orientation="vertical" android:id="@id/colorsLayout" android:layout_width="fill_parent" android:layout_height="fill_parent"
      xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads">
        <GridView android:gravity="center" android:id="@id/colorView" android:layout_width="0px" android:layout_height="0px" android:horizontalSpacing="15.0dip" android:verticalSpacing="0.0dip" android:stretchMode="columnWidth" android:columnWidth="30.0dip" android:numColumns="auto_fit" android:layout_above="@id/colorsAdMob"
          xmlns:android="http://schemas.android.com/apk/res/android" />
        <com.google.ads.AdView android:id="@id/colorsAdMob" android:layout_width="0px" android:layout_height="0px" android:layout_alignParentBottom="true" ads:adUnitId="a14d5ae1ff5b91c" ads:adSize="BANNER" ads:testDevices="TEST_EMULATOR, TEST_DEVICE_ID" ads:loadAdOnCreate="true" />
    </RelativeLayout>

## End Snip ##

#### 0x3b Solitaire ####

App Name: Solitaire by Mobilityware
Goal: Remove Ads

To remove the ads from this app, we will have to modify some Dalvik code.  Whenever a new round is dealt, an ad screen will pop up to the user.  The user then has to  "dismiss" the ad before they are returned to the game.

I first started greping through the smali files looking for common keywords: displayad, viewad, getad.  I came across the following line in the com/mobilityware/solitaire/Solitaire.smali file:

## Code Snip ##

02204: invoke-virtual {v0}, Lcom/mobilityware/solitaire/AdControl;->displayAd()Z

## End Snip ##

The ‘invoke-virtual‘ opcode calls a virtual method.  In this case it is calling the displayAd function in com/mobilityware/solitaire/AdControl.  If we comment out this call, the ads will not be displayed:

## Code Snip ##

02204: #invoke-virtual {v0}, Lcom/mobilityware/solitaire/AdControl;->displayAd()Z

## Code Snip ##

#### 0x3c Chess Free ####

App Name: Chess Free by aifactory
Goal: Remove Ads

The ads in Chess are displayed while a user is playing the game.  Chess Free uses a different ad engine than the previous apps.  For this app, I decided to take a different approach: prevent the ad system from receiving ads.

After running logcat on the phone, noticed that there were calls to "adRequestWebView" being made.  After greping through the files, in google/ads/c.smali I found the following lines of code:

## Code Snip ##

01    :try_start_0
02   iget-object v0, p0, Lcom/google/ads/c;->f:Landroid/webkit/WebView;
03
04    if-eqz v0, :cond_0
05
06    iget-object v0, p0, Lcom/google/ads/c;->c:Lcom/google/ads/b;
07
08    if-nez v0, :cond_1
09
10    :cond_0
11    const-string v0, "adRequestWebView was null while trying to load an ad."
12
13    invoke-static {v0}, Lcom/google/ads/util/a;->e(Ljava/lang/String;)V
14
15    sget-object v0, Lcom/google/ads/AdRequest$ErrorCode;->INTERNAL_ERROR:Lcom/google/ads/AdRequest$ErrorCode

## End Snip ##

In the above code, there is a test on v0 to see if it is zero and if it is to jump to the :cond_0 statement.  If :cond_0 is hit, the function throws an error that the ad could not load; this seems like a great place to introduce some of our own logic!

If we can set the value of v0 to be ‘0‘ before it hits the condition in line 04, the cond_0 section will be hit.  We can introduce this value by using the ‘const‘ statement.  We will introduce "const v0, 0x0" before the "if-eqz v0, :cond_0" statement to ensure that cond_0 will be hit.  See in the below code:

## Code Snip ##

01    :try_start_0
02    iget-object v0, p0, Lcom/google/ads/c;->f:Landroid/webkit/WebView;
03
04    const v0, 0x0
05
06    if-eqz v0, :cond_0
07
08    iget-object v0, p0, Lcom/google/ads/c;->c:Lcom/google/ads/b;
09
10    if-nez v0, :cond_1
11
12    :cond_0
13    const-string v0, "adRequestWebView was null while trying to load an ad."

## End Snip ##

Now with the value introduced, the ads will not load during the game.

##### 0x4 Additional Notes #####

This paper did not discuss how to disassemble an Android application and reassemble it after the changes have been made.  There are numerous resources available that discuss how to reverse engineer Android applications.  In the Resources section I have included a link to a tool that has made the job way easier.

##### 0x5 Resources #####

http://pallergabor.uw.hu/androidblog/dalvik_opcodes.html -- 虚拟机Dalvik操作码详解
http://www.virtuousrom.com/p/ten-studio.html
[转至]http://www.exploit-db.com/papers/21325/
时间: 2024-08-06 23:19:51

[Security_Android] exploit of Ad for android app代码分析的相关文章

Android APP性能分析方法及工具

近期读到<Speed up your app>一文.这是一篇关于Android APP性能分析.优化的文章.在这篇文章中,作者介绍他的APP分析优化规则.使用的工具和方法.我觉得值得大家借鉴.英文好的读者可读原文(链接:http://blog.udinic.com/2015/09/15/speed-up-your-app). 1.作者的规则 作者每次着手处理或寻找性能问题时,遵循下列规则: 时常检测 在更新APP前后,用测试工具软件多检测几次APP性能,可快速得到测试数据.这些数字是不会说谎的

Android恶意代码分析与渗透测试

这篇是计算机类的优质预售推荐>>>><Android恶意代码分析与渗透测试> 从环境构建到分析,涵盖服务体系全过程:以线上线下技巧为基础,展现虚拟环境渗透测试真方法 编辑推荐 从环境构建到分析,涵盖服务体系全过程 以线上/线下技巧为基础,展现虚拟环境渗透测试真方法 内容简介 本书由"恶意代码分析"和"移动服务诊断"两大主题组成.各章节包含了分析步骤,作者们还亲自编写了黑客大赛应用程序试题,读者可以借此复习学过的内容. Androi

(通用)Android App代码混淆终极解决方案【转】

App虽然没有那么的高大上,但是代码的混淆是代表了程序员对App的责任心, 也是对App安全的一点点保证.今天我会将自己做Android混淆的过程和体会分享给大家,也避免大家少走弯路,少跳坑. 本篇博客混淆基于Android Studio的IDE开发环境. 其实在android Studio中做混淆,基本就是对Proguard-rules.pro文件的操作.混淆的过程也是有规律可循的.下面我将分几个部分来分别介绍混淆过程. (1)如何开启混淆. (2)混淆的公共部分. (3)需要我们不混淆的代码

Android APP代码拨打电话、打开手机分享功能等隐式意图

Android APP拨打电话: Intent intent=new Intent(Intent.ACTION_DIAL,Uri.parse("tel:"+110)); startActivity(intent); } Android APP打开电话薄: Intent intent = new Intent(Intent.ACTION_PICK,ContactsContract.Contacts.CONTENT_URI); startActivity(intent); Android

Android APP代码拨打电话和短信分享

Android APP拨打电话: Intent intent=new Intent(Intent.ACTION_DIAL,Uri.parse("tel:"+888888)); startActivity(intent); } Android APP短信分享: Intent sendIntent = new Intent(Intent.ACTION_VIEW); sendIntent.putExtra("sms_body", "#短信分享#");

Android DNS 代码分析

Android DNS 代码都在bionic/libc/netbsd中 (虽然netbsd 是个废弃的项目,但dns功能部分代码被 Android用上了) netbsd 代码晦涩难懂,只有一边写程序,一边打log验证 照抄了一个简单程序, 在android 下面用 mmm 编译可以得到 py_getaddr 可执行文件, 用adb 登陆到 devices 再到 system/bin 下去执行 [email protected]:~/njb/getaddr$ cat py_getaddr.c #i

(转)Android APP 漏洞分析

由于Android系统开源架构特性,安卓移动应用成为恶意病毒攻击的重点目标.最新调查显示, 绝大多数手机应用都存在着移动应用安全漏洞,这可能会导致它们在未来感染严重的恶意病毒.接下来我们来看一份检测报告是以什么方式进行检测分析的. 爱内测的一份报告(www.detect.cn). 报告中详细指出了apk文件中主要存在的安全问题,以及修复漏洞的建议,文中采用的智能检测方法通过Android组件检测.权限管理.dex保护.数据安全(传输.存储.输出)检测,以及危险调试信息等常见的漏洞风险检测: 全面

android逆向代码分析截图

Android利用代码清除App的数据和重启设备

/** * 利用代码清除App的数据 * 平常我们在清除App的数据时,多半在设置中找到对应的App * 然后选择其清除数据.下面给出代码实现. * * 注意事项: * 1 设备需要root * 2 注意在命令的末尾需要加上换行\n * 这就相当于我们平时在Dos中输入命令后再换行一样. * 否则命令不会执行. */ private void cleanData(String packageName){ try { System.out.println("---> 9527 开始清除 &q