Play on Android Hook:cydia substrate combat

Source: Internet
Author: User

Author profile: Zhou San, Baidu advanced Android Development Engineer, blog address: http://blog.csdn.net/yzzst

Understanding Hooks

Have not touched the hook technology readers will certainly feel a special sense of the word hook, Hook English translation is the meaning of "hook", then when we use this "hook" it?

We know that the system maintains its own set of event distribution mechanisms in the Android operating system. Applications, including application-triggered events and background logic processing, are also executed down-to-back according to the event flow. The "hook" means intercepting and monitoring the transmission of the event before the event is delivered to the end, like a hook-and-eye event. And be able to handle some of their own specific events when the event is ticked. As shown in the following:

The hook's ability to "fit" its code into the process of a hooked program becomes a part of the target process. We also know that in the Android system using the sandbox mechanism, the ordinary user program process space is independent, the program runs between each other undisturbed.

This allows us to hope that the idea of changing certain behaviors of other programs through a program cannot be achieved directly, but the advent of hooks opens the way to solving such problems. Of course, according to the Hook object and hook after the processing of different events, hooks are divided into different types, such as message hooks, API hooks.

Cydia substrate Frame

If the user who used the iphone should be no stranger to the Cydia substrate framework, because the Cydia substrate framework provides the jailbreak-related service framework for Apple users.

Cydia substrate formerly known as MobileSubstrate (the class library is the beginning of MS), the author is the famous Jay Freeman (Saurik).

Of course, Cydia substrate also launched the Android version. Cydia substrate is a code modification platform that can modify the code of any master process, whether it is written in Java or C/s (native code).

Install Cydia substrate Framework Android Local Service

The first is to install the Cydia substrate Framework's Local Service application substrate.apk on the Android device, which we can download on its website.

Of course, after we install substrate, we need "link substrate files" (connect to the local substrate service file), this step is required root permission, after the connection will need to restart the device to be effective.

download using Cydia substrate library

Cydia substrate official recommends adding their plug-in addresses in the Android SDK manager for updates to download. For example, add http://asdk.cydiasubstrate.com/addon.xml to user-defined URLs.

After downloading the Cydia substrate framework using the ANDROID SDK Manager tool, it is stored under directory ${android_home}\sdk\extras\saurikit\cydia_substrate.

However, because the Android SDK manager in the domestic use of a lot of restrictions, the download time is not very stable, so it is recommended that you go directly to the official website to download the development library.

After the download is complete, you will get all the files (many jar packages and so libraries) that are copied to the Libs folder under the Android project and can be used directly. The substrate.h header file and the so file under the Lib folder are provided as a function support library in the development of native hook programs using the NDK.

Tips:cydia substrate Framework for the operation of the inline hook there are still some bugs, the use of the time may be a crash phenomenon, some use of domestic custom ROM device in use Cydia The substrate frame will cause the device to fail to restart or be unable to hook.
Cydia substrate How to use?

Cydia substrate how to use? In fact it is very simple, Cydia substrate provides three static method tool class, we just need to learn to use it just fine.

    1. Ms.hookclassload get notification when the specified class is loaded
    2. Ms.hookmethod using a Java method to replace another Java method
    3. Ms.moveunderclassloader using different classloder to reload objects

Specify the following:

/** * Hook a specified class *  * @param name class's package name + class name, such as Android.content.res.Resources * @param hook successfully hooks a class callback */voi D hookclassload (String name, MS. Classloadhook hook);/** * Hook a specified method and replace the code in the method *  * @param _class Hook's CALSS * @param member Hook class method parameters * @param Hook successful Hook method after the callback * @param old hook method, similar to the method pointer in C */void hookmethod (Class _class, Member Member, MS. Methodhook Hook, MS. Methodpointer old);/** * Hook a specified method and replace the code in the method *  * @param _class Hook's CALSS * @param member Hook class method parameter * @param a Lteration */void Hookmethod (Class _class, Member Member, MS. Methodalteration alteration)/** * Use a ClassLoader to reload an object *  * @param loader used ClassLoader * @param object with overloaded objects * @re Turn overloaded object */<t> T Moveunderclassloader (ClassLoader loader, t object);

Start Combat (AD injection)

Hearing this topic, I think a lot of packing the party also can't wait, a little too impatient. Rely on advertising is not earn money, the author is also a packaging party. Programmers still grow and develop, a quick money belt will let you in the programming road farther and further.

Back to the point, using the Cydia substrate framework we can arbitrarily hook the Java API in the system, of course, there are a lot of reflection mechanism, so in addition to the system to provide developers with the API, we can also hook some methods in the application? The answer is yes. Let's look at a practical example of how to hook an application.

Below we apply for the Android operating system browser, hook its homepage activity OnCreate method (other methods do not necessarily exist, but the OnCreate method will certainly have), and inject our ads. Based on the introduction of Cydia substrate, we have a simple idea.

First, we fill in our Androidmanifest.xml file with some ad-related IDs based on the rules of an advertising platform. and fill in the Androidmanifest.xml file with some configuration and permissions that are related to using Cydia substrate. Of course, we will also declare an activity for an ad and set the activity as a transparent background activity, why set up activity with a transparent background, such as:

OK, let's do the actual operation.

Some of the contents of its Androidmanifest.xml file are as follows:

<!--advertising-related permissions--><uses-permission android:name= "Android.permission.INTERNET"/><uses-permission Android:name= "Android.permission.ACCESS_NETWORK_STATE"/><uses-permission android:name= " Android.permission.ACCESS_WIFI_STATE "/><uses-permission android:name=" Android.permission.READ_PHONE_STATE "/><uses-permission android:name=" Android.permission.WRITE_EXTERNAL_STORAGE "/><uses-permission Android:name= "Android.permission.GET_TASKS"/><!--Add substrate permissions--><uses-permission android:name= "    Cydia.permission.SUBSTRATE "/><application android:allowbackup=" true "android:icon=" @drawable/ic_launcher "        Android:label= "@string/app_name" android:theme= "@style/apptheme" > <!--Ads Related parameters--<meta-data Android:name= "app_id" android:value= "C62bd976138fa4f2ec853bb408bb38af"/> <meta-data android:name= " App_pid "android:value=" DEFAULT "/> <!--declaration substrate injection Taste main class--<metA-data android:name= "Com.saurik.substrate.main" android:value= "Com.example.hookad.Main"/> <!--transparent No Animated ads activity--<activity android:name= "com.example.hookad.MainActivity" Android:theme= "@android: Style/theme.translucent.notitlebar "> <intent-filter> <action android:name=" android.intent.a Ction. VIEW "/> <category android:name=" Android.intent.category.DEFAULT "/> <!--ad action--&            Gt  <action android:name= "Com.example.hook.AD"/> </intent-filter> </activity></application>

For the main entry of Cydia substrate, create a new main class with the Initialize method according to the previous procedure.

This time we want to use the Ms.hookclassload method to find the activity name of the browser homepage.

Here we use the Dumpsys Activity command under the ADB shell to find the activity name of the browser home page called Com.android.browser.BrowserActivity.

Use the Ms.hookclassload method to get the browseractivity and then hook its OnCreate method, which initiates an activity that contains ads. The code for the main class is as follows:

public class Main {/** * substrate initialized entry */static void Initialize () {//hook browser's main activity,browse Ractivity ms.hookclassload ("Com.android.browser.BrowserActivity", New MS. Classloadhook () {public void classloaded (class<?> resources) {LOG.E ("test", "com.android . browser.                Browseractivity ");                Get Browseractivity method of onCreate methods onCreate;                try {onCreate = Resources.getmethod ("OnCreate", Bundle.class);                } catch (Nosuchmethodexception e) {onCreate = null; } if (onCreate! = null) {final MS. Methodpointer old = new MS.                    Methodpointer (); Hook OnCreate Method Ms.hookmethod (resources, OnCreate, new MS.                            Methodhook () {public Object invoked (object object, Object...args) throws Throwable { LOG.E ("Test", "show aD ");                            Execute the OnCreate method before hook to ensure the browser starts normally object result = Old.invoke (object, args); No context//execute a Shell to launch our ad activity Cmd.run ("AM start                            -a Com.example.hook.AD ");                        return result;                }}, old);    }            }        }); }}

For the launch of the ad mainactivity, in which is a pop-up interstitial ad. Of course, but also other forms of advertising or floating layer, the content is relatively simple here does not do a demonstration. Compile and run the entire project. This time when we restarted Android's own browser, we found that the browser will pop up an ad box.

From the picture above we can see that, Before we set interstitial ads mainactivity for untitled Transparent (Theme.Translucent.NoTitleBar) is to make the pop-up ads and browser integration, so that users feel that the browser pop-up ads, but also malicious advertising programs in order to prevent themselves from being uninstalled some common hidden means.

The injected ad shown here is a OnCreate method in the activity specified by the hook to initiate an ad activity. Of course, the activity we're demonstrating here is just a simple bounce out of an ad. If the activity initiated is malicious, such as fishing activity that does the same activity as the original activity, then it is very deceptive for mobile device users.

written in the last

In fact, I do not want to send this article, I have done advertising packaging party, know that this also for the various underground Nuggets packaging party provides ideas. But, Cydia substrate not only in order to inject into the ads, hope that we dig a lot of ideas, to learn from the common promotion.

Play on Android Hook:cydia substrate combat

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.