Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Mobile App Reverse Engineering
Mobile App Reverse Engineering

Mobile App Reverse Engineering: Get started with discovering, analyzing, and exploring the internals of Android and iOS apps

By Abhinav Mishra
$33.99 $22.99
Book May 2022 166 pages 1st Edition
eBook
$33.99 $22.99
Print
$36.99
Subscription
$15.99 Monthly
eBook
$33.99 $22.99
Print
$36.99
Subscription
$15.99 Monthly

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Buy Now

Product Details


Publication date : May 27, 2022
Length 166 pages
Edition : 1st Edition
Language : English
ISBN-13 : 9781801073394
Category :
Table of content icon View table of contents Preview book icon Preview Book

Mobile App Reverse Engineering

Chapter 1: Basics of Reverse Engineering – Understanding the Structure of Mobile Apps

All of us use cell phones in our daily lives now, and their usage has grown to such a crucial level that people frequently name cell phones as one of the three things you can't live without, after food and water. Cell phones handle almost every task, from managing funds in bank accounts and investments to travel bookings, shopping, and health appointments.

To perform these tasks, cell phones have mobile apps. These apps handle a majority of your data and help you perform tasks.

As these modern mobile apps handle sensitive user information, perform critical tasks, and provide access to a huge array of resources on the internet, the security of the data being handled and the operations performed on it also need to be improved.

A mobile application penetration tester tests the security of mobile applications in order to find vulnerabilities. To find the vulnerabilities, the tester is required to understand the internal working and logics of the application. These details can be found in the source code of the application. However, the penetration testers do not always have the source code to hand, as in the case of a black-box penetration test. During a black-box penetration test, all that the penetration tester has is the application package, that is, the Android Application Package (APK) or iOS App Store Package (IPA) file. In such a case, to understand the working of the app, they need to unpack the application package and get the source code.

Reverse engineering is the technique of dismantling an object to study its internal designs, code, logic, and so on. Reverse engineering mobile applications is the process of disassembling/dismantling an app to reveal its code and internal logic, components, and so on.

In this chapter, we're going to cover the following main topics:

  • Reverse engineering fundamentals
  • Android application fundamentals
  • iOS application fundamentals

We will learn about the basics of reverse engineering and how mobile applications are built. These fundamentals are important to understand before we can jump into the actual task of reverse engineering modern apps.

Technical requirements

Android Studio and Xcode are required to complete the relevant hands-on exercises. Xcode is Apple's integrated development environment (IDE) for macOS, used to develop software for macOS, iOS, iPadOS, watchOS, and tvOS. Android Studio is the official IDE for Google's Android operating system. An Apple laptop/desktop (Mac) can install and run both Xcode and Android Studio, whereas other laptops/desktops running Windows or Linux will only be able to support Android Studio.

For more information, please refer to the following links:

Reverse engineering fundamentals

Let's first understand the fundamentals of reverse engineering, why it is needed, and what steps are involved.

As mentioned earlier in this chapter, reverse engineering is the technique of dismantling an object to study its internal designs, code, and logic.

When a developer builds a mobile app, they choose a programming language (according to the targeted platform – Android, iOS, or both), write the code for the functionalities they want, and add resources such as images, certificates, and so on. Then the code is compiled to create the application package.

While reverse engineering the same app, the reverse engineer dismantles the application package to the components and code.

Some of the frequently used terms in reverse engineering are the following:

  • Decompilation: This is the process of translating a file from a low-level language to a higher level language. The tool used to perform decompilation is called a decompiler. A decompiler takes a binary program file and changes this program into a higher-level structured language. The following diagram illustrates the decompilation process:
Figure 1.1 – Diagram of the decompilation process

Figure 1.1 – Diagram of the decompilation process

  • Disassembling: This is the process of transforming machine code (in an object code binary file) into a human-readable mnemonic representation called assembly language. The tool used to perform disassembly is called a disassembler as it does the opposite of what an assembler does. The following diagram illustrates the disassembly process:
Figure 1.2 – Diagram of the disassembly process

Figure 1.2 – Diagram of the disassembly process

A simple binary disassembled in a disassembling tool, Hopper, looks as follows:

Figure 1.3 – Disassembled binary in Hopper

Figure 1.3 – Disassembled binary in Hopper

  • Debugging: This is a technique that allows the user to view and modify the state of a program at runtime. The following diagram illustrates the debugging process:
Figure 1.4 – Diagram of the debugging process

Figure 1.4 – Diagram of the debugging process

Understanding the different methodologies and approaches used in reverse engineering is very important. We will be using all these concepts in further chapters of this book.

Now that we have seen the fundamentals of reverse engineering, let's explore how mobile applications, that is, Android and iOS apps, are developed. We will now be looking into the components, structure, and concepts behind the mobile application fundamentals.

Android application fundamentals

Native Android applications are written mainly in Java or Kotlin. The Android SDK tools compile the code along with any data and resource files into an APK or an Android App Bundle. The compiled application is in a specific format, specified by the extension .apk. That is, an Android package is an archive file containing multiple application files and metadata.

Fun Fact

Rename the file extension of an APK to .zip and use unzip to open. You will be able to see its contents.

The following are the major components of an APK:

  • AndroidManifest.xml: The application manifest file containing app details such as the name, version, referenced libraries, and component details in XML format. The Android operating system relies on the presence of this file to identify relevant information about the application and related files.
  • Dalvik executable files (classes.dex files).
  • META-INF:
    • MANIFEST.MF (manifest file)
    • CERT.RSA (certificate of the application)
    • CERT.SF (list of resources with SHA-1 digest of the corresponding lines in the MANIFEST.MF file)
  • lib: This contains the compiled code that is specific to a selection of processors, as follows:
    • armeabi: Compiled code for all ARM-based processors
    • armeabi-v7a: Compiled code for all processors based on ARMv7 and above
    • x86: Compiled code for x86 processors
    • mips: Compiled code for MIPS processors
  • res: Resources that are not compiled into resources.arsc.
  • assets: Contains application assets.
  • resources.arsc: Pre-compiled resources.

    Important Note

    Java code in Android devices does not run in the Java Virtual Machine (JVM). Rather, it is compiled in the Dalvik Executable (DEX) bytecode format. A DEX file contains code that is ultimately executed by Android Runtime.

Let's see how to create a simple hello world application for Android and then unzip it to look at its components:

  1. Android apps are developed using Android Studio. Download and install the latest version of Android Studio from https://developer.android.com/studio:
Figure 1.5 – Creating a new project in Android Studio

Figure 1.5 – Creating a new project in Android Studio

  1. Let's choose the New Project option and select the Empty Activity option:
Figure 1.6 – Selecting project type

Figure 1.6 – Selecting project type

  1. On the next screen, fill in all the details as shown in the following screenshot. You can choose the name as you please:
Figure 1.7 – Project details

Figure 1.7 – Project details

  1. Once you click Finish, a new project will be created for a default activity/screen app.
  2. You can now try to run the app on any attached Android device, or the virtual Android emulator. For the latter, create a virtual Android device from the AVD menu.
  3. Once the app runs successfully, we will try to extract the application package for this app from Android Studio:
Figure 1.8 – Running the app on the emulator

Figure 1.8 – Running the app on the emulator

  1. To get the APK from Android Studio, go to the Build | Build Bundle(s)/APK(s) | Build APK(s) menu option. Once generated, navigate to the folder mentioned in the Locate option and copy the APK.
  2. Once the APK is copied, change the extension of the file to .zip:
Figure 1.9 – Diagram of rename process

Figure 1.9 – Diagram of rename process

  1. Use any archive tool to unzip the file and extract its contents:
    # unzip MARE-Chapter-1.zip

For reference, the result is as follows:

Figure 1.10 – Extracting the content of the APK, after renaming it to .zip

Figure 1.10 – Extracting the content of the APK, after renaming it to .zip

  1. Let's analyze the components inside the APK and compare it with the list here (Android application fundamentals):
Figure 1.11 – Extracted content of the APK

Figure 1.11 – Extracted content of the APK

The following diagram shows the processes of forward and reverse engineering an Android application:

Figure 1.12 – The forward and reverse engineering processes with an Android application

Figure 1.12 – The forward and reverse engineering processes with an Android application

Android applications are mainly developed using Java and Kotlin. The internals of an Android package are the same whether it is based on Java or Kotlin. Therefore, the approach to reverse engineer the application is also the same.

We've now learned about the fundamentals of Android applications. iOS apps are also packaged into a specific format and have a specific structure. Let's look into the iOS application fundamentals now.

iOS application fundamentals

Similar to Android, iOS applications also come in a specific zipped format called IPA, or an iOS App Store Package. iOS application packages can also be renamed by changing the extension to ZIP and then the components can be extracted, though the components of an iOS application package differ from those of an Android one.

iOS apps are mainly built using Objective-C and Swift, both of which can be disassembled using a disassembler such as Hopper or Ghidra. In Objective-C applications, methods are called via dynamic function pointers, which are resolved by name during runtime. These names are stored intact in the binary, making the disassembled code more readable. Unlike Android, in iOS, the application code is compiled to machine code that can be analyzed using a disassembler.

The following are the major components of an iOS application package:

  • Info.plist: Similar to the Android manifest file in an APK, this information property list file contains key-value pairs that specify essential runtime-configuration information for the application. The iOS operating system relies on the presence of this file to identify relevant information about the application and related files.
  • Executable: The file that runs on the device, containing the application's main entry point and code that was statically linked to the application target.
  • Resource files: Files that are required by the executable file, and are required for the application to properly run. This may contain images, nib files, string files, and configuration files.

The following diagram illustrates the iOS architecture overview:

Figure 1.13 – iOS architecture

Figure 1.13 – iOS architecture

Let's see how to create a simple hello world application for iOS and then unzip it and look at its components:

  1. iOS apps are developed using Xcode. Download the latest version of Xcode from the App Store on Mac.
Figure 1.14 – Creating an Xcode project

Figure 1.14 – Creating an Xcode project

  1. On the next screen, choose the default App template for your new project:
Figure 1.15 – Selecting the project template

Figure 1.15 – Selecting the project template

  1. On the next screen, provide a product name (any name you like), select a team, and provide an organization identifier. To create and export an IPA from Xcode, you need to have an Apple Developer license:
Figure 1.16 – Providing project details

Figure 1.16 – Providing project details

  1. Select a location to save the project on your computer.

Xcode will now create a simple hello world application and you will see the following default code in the Xcode window:

Figure 1.17 – Project details

Figure 1.17 – Project details

  1. Now you can try and run this app on one of the built-in iOS simulators. To do so, select one of the available simulators (just click on the name of simulator from top bar, and a list will open) as shown in the following screenshot:
Figure 1.18 – Selecting a simulator

Figure 1.18 – Selecting a simulator

The app should run on the selected simulator:

Figure 1.19 – App running on the simulator

Figure 1.19 – App running on the simulator

  1. Now, let's export the IPA from this Xcode project. To do so, select the Any iOS Device (arm64) option from the simulator options.
  2. Then, go to Product | Archive and select the Distribute App option:
Figure 1.20 – Exporting the application package

Figure 1.20 – Exporting the application package

  1. On the next screen, select Development and leave the options on the subsequent screens at their defaults.
  2. Finally, you will be able to export the IPA together with some other compiled project files:
Figure 1.21 – Exporting the application package (cont.)

Figure 1.21 – Exporting the application package (cont.)

  1. Once the IPA is exported, simply change the extension of the file to .zip:
Figure 1.22 – Diagram explaining the application (IPA) extraction process via renaming

Figure 1.22 – Diagram explaining the application (IPA) extraction process via renaming

  1. Use any tool to unzip the file and extract its contents:
    # unzip MARE-Chapter-1.zip

The following screenshot shows the results for reference:

Figure 1.23 – Extracting the content of the IPA after renaming it to ZIP

Figure 1.23 – Extracting the content of the IPA after renaming it to ZIP

  1. Go into the Payload directory and then inside the MobileAppReverseEngg-App-1.app file:
    # cd Payload 
    # cd MobileAppReverseEngg-App-1.app
  2. Let's analyze the components inside the IPA and compare it with the list here (iOS application fundamentals):
Figure 1.24 – Extracted content of the IPA

Figure 1.24 – Extracted content of the IPA

The following diagram illustrates the process of reverse engineering an iOS application:

Figure 1.25 – Overview of the reverse engineering process of an IPA

Figure 1.25 – Overview of the reverse engineering process of an IPA

Have a look at Figure 1.3 to understand how a disassembled binary looks in Hopper disassembler.

Summary

The concepts and processes of reverse engineering are very interesting. Through this chapter, you have learned the fundamentals of reverse engineering both Android and iOS applications. The concepts explored will help your understanding in the later chapters of this book as we begin to look at reverse engineering in depth.

In the next chapter, we will learn more about the modern tools used to reverse engineer iOS and Android apps.

Left arrow icon Right arrow icon

Key benefits

  • Learn the skills required to reverse engineer mobile applications
  • Understand the internals of iOS and Android application binaries
  • Explore modern reverse engineering tools such as Ghidra, Radare2, Hopper, and more

Description

Mobile App Reverse Engineering is a practical guide focused on helping cybersecurity professionals scale up their mobile security skills. With the IT world’s evolution in mobile operating systems, cybercriminals are increasingly focusing their efforts on mobile devices. This book enables you to keep up by discovering security issues through reverse engineering of mobile apps. This book starts with the basics of reverse engineering and teaches you how to set up an isolated virtual machine environment to perform reverse engineering. You’ll then learn about modern tools such as Ghidra and Radare2 to perform reverse engineering on mobile apps as well as understand how Android and iOS apps are developed. Next, you’ll explore different ways to reverse engineer some sample mobile apps developed for this book. As you advance, you’ll learn how reverse engineering can help in penetration testing of Android and iOS apps with the help of case studies. The concluding chapters will show you how to automate the process of reverse engineering and analyzing binaries to find low-hanging security issues. By the end of this reverse engineering book, you’ll have developed the skills you need to be able to reverse engineer Android and iOS apps and streamline the reverse engineering process with confidence.

What you will learn

Understand how to set up an environment to perform reverse engineering Discover how Android and iOS application packages are built Reverse engineer Android applications and understand their internals Reverse engineer iOS applications built using Objective C and Swift programming Understand real-world case studies of reverse engineering Automate reverse engineering to discover low-hanging vulnerabilities Understand reverse engineering and how its defense techniques are used in mobile applications

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Buy Now

Product Details


Publication date : May 27, 2022
Length 166 pages
Edition : 1st Edition
Language : English
ISBN-13 : 9781801073394
Category :

Table of Contents

13 Chapters
Preface Chevron down icon Chevron up icon
Section 1: Basics of Mobile App Reverse Engineering, Common Tools and Techniques, and Setting up the Environment Chevron down icon Chevron up icon
Chapter 1: Basics of Reverse Engineering – Understanding the Structure of Mobile Apps Chevron down icon Chevron up icon
Chapter 2: Setting Up a Mobile App Reverse Engineering Environment Using Modern Tools Chevron down icon Chevron up icon
Section 2: Mobile Application Reverse Engineering Methodology and Approach Chevron down icon Chevron up icon
Chapter 3: Reverse Engineering an Android Application Chevron down icon Chevron up icon
Chapter 4: Reverse Engineering an iOS Application Chevron down icon Chevron up icon
Chapter 5: Reverse Engineering an iOS Application (Developed Using Swift) Chevron down icon Chevron up icon
Section 3: Automating Some Parts of the Reverse Engineering Process Chevron down icon Chevron up icon
Chapter 6: Open Source and Commercial Reverse Engineering Tools Chevron down icon Chevron up icon
Chapter 7: Automating the Reverse Engineering Process Chevron down icon Chevron up icon
Chapter 8: Conclusion Chevron down icon Chevron up icon
Other Books You May Enjoy Chevron down icon Chevron up icon

Customer reviews

Filter icon Filter
Top Reviews
Rating distribution
Empty star icon Empty star icon Empty star icon Empty star icon Empty star icon 0
(0 Ratings)
5 star 0%
4 star 0%
3 star 0%
2 star 0%
1 star 0%

Filter reviews by


No reviews found
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.