Ios App Reverse Engineering
Ios App Reverse Engineering
Ios App Reverse Engineering
snakeninny, hangcom
Translated by Ziqi Wu, 0xBBC, tianqing and Fei Cheng
Table of Contents
Recommendation ..................................................................................................................................................... 1
Preface ....................................................................................................................................................................... 2
Foreword ................................................................................................................................................................... 7
Chapter 8 Practice 2: Mark user specific emails as read automatically ........................................................... 273
In our lives, we pay very little attention to things that work. Everything we interact with
hides a fractal of complexity—hundreds of smaller components, all of which serve a vital role,
each disappearing into its destined form and function. Every day, millions of people take to the
streets with phones in their hands, and every day hardware, firmware, and software blend into
one contiguous mass of games, photographs, phone calls, and text messages.
It holds, then, that each component retains leverage over the
others. Hardware owns firmware, firmware loads and reins in software, and software
in turn directs hardware. If you could take control of one of them, could you influence
a device to enact your own desires?
iOS App Reverse Engineering provides a unique view inside the software running on iOS™,
the operating system that powers the Apple iPhone® and iPad®. Within, you will learn what
makes up application code and how each component fits into the software ecosystem at large.
You will explore the hidden second life your phone leads, wherein it is a full-fledged computer
and software development platform and there is no practical limit to its functionality.
So, young developer, break free of restricted software and find out exactly what makes your
phone tick!
Dustin L. Howett
iPhone Tweak Developer
1
Preface
I’m a man who loves traveling by myself. On every vacation in university, I spent about 7 to
10 days as a backpacker, traveling around China. Since it was self-guiding tours, no guide would
come to help me arrange anything. As a result, before traveling, my friends and I had to prepare
everything by ourselves, such as scheduling, confirming the routes and buying tickets. We also
needed to put deep thought into our plans, and thought about their dangers.
It’s a commonly held belief that traveling, especially backpacking, is a great way to expand
one’s horizons. What I see during my trips can make me more knowledgeable about the world
around me. More importantly, before start traveling, I need to get everything prepared for this
journey. My mind has arrived at the destination, even if my body is still at the starting point.
This way of thinking is good for cultivating a holistic outlook as well as making us think about
problems from a wider, longer term perspective.
Before pursuing my master degree in 2009, I thought deeply about what I wanted to study.
My major was computer science. From the beginning of undergraduate year, most of my
classmates engaged in the study of Windows. As a student who wasn’t good at programming
then, there were two alternatives for me to choose—one was to continue the study of
Windows, and the other was to explore something else. If I chose the former, there were at least
two benefits for me. Firstly, there were lots of documents for reference. The second one was
that there were numerous people engaging in the study of Windows. When I met problems, I
could consult and discuss with them. However, from the other side, there were also some
disadvantages. More references possibly led to less creativity, and the more people engaged in
studying Windows, the more competition I would face.
In a nutshell, if I engaged in Windows related work, I could start my career very easily.
However, there was no guarantee that I could be outstanding among the researchers. If I chose
to do something else, it might be very difficult at the beginning. But as long as I persist with my
goal, I could make something different.
2
Fortunately, my mentor had the same idea. He recommended me to work on mobile
development. At that time, there were very few people engaging in this area in China and I had
no idea about smart phones. My mobile phone was an out of date Philips phone, so that it was
very hard for me to start to develop applications. Despite the difficulties, I trusted my mentor
and myself. Not only because I had only chosen him after careful research and
recommendations by my senior fellow students, but also that we shared the same opinions. So I
started to search online for mobile development related information. After learning only a few
concepts about smart phones and mobile Internet, I faintly found that this industry was
conductive to the theory that computers and Internet would become smaller, faster and more
tightly related with our lives. Many things could be done in this area. So I chose to study iOS.
Everything was hard in the beginning. There were lots of differences between iOS and
Windows. For example, iOS was an UNIX-like operating system, which was a complete, but
closed, ecosystem. Its main programming language Objective-C, and jailbreak, were all strange
fields lacking of information at that point. So I learned by myself, week by week, in a
hackintosh. And this lasted for almost a year. During this period of time, I read the book “Learn
Objective-C on the Mac”, input the code on the book into Xcode and checked the result by
running the simulator. However, the code and the UI were hard to be associated with each
other. Besides, I searched those half-UNIX concepts like backgrounding on Google and tried to
understand them, but they were really hard to understand. When my classmates published their
papers, I even wondered what I was doing during these several months. When they went out
and party all night, I decided to code alone in the dormitory. When they had fallen asleep, I had
to keep on working in the lab. Although these things made me feel lonely, they benefitted me a
lot. I learnt a lot and became more informative during this period. As well, it made me become
confident. The more knowledge I got, the less lonely I felt. A man can be excellent when he can
bear the loneliness. What you pay will finally return and enrich yourself. After one-year of
practice, in March 2011, the obscure code suddenly became understandable. The meaning of
every word and the relationship of every sentence became clearer. All fragmented knowledge
appeared to be organized in my head and the logic of the whole system became explicit.
So I sped up my research. In April 2011, I finished the prototype of my master thesis and got
high praise from my mentor who didn’t keep high expectation on my iOS research. Since then, I
changed from a person who felt good to a man who was really good, which signified my pass of
entry level of iOS research.
3
In the past few years, I made friends with the author of Theos, DHowett, consulted
questions with the father of Activator, rpetrich and quarreled with the admin of TheBigBoss
repo, Optimo. They were the people who solved most of my problems along the way. During
the development of SMSNinja, I met Hangcom, the second author of this book. As research
continues, I met a group of people who was doing excellent things but keeping low profile and
finally I realized I’m not alone—We stand alone together.
Taking a look back at the past five years, I’m glad that I made the right choice. It’s hard to
imagine that you can publish a book related to Windows with only 5-years of research.
However, this dream comes true with iOS. The fierce competition among Apple, Microsoft and
Google and the feedback from market both prove that this industry will definitely play a leading
role in the next 10 years. I feel very lucky that I can be a witness and participant. So, iOS fans,
don’t hesitate, come and join us, right now!
When received the invitation from Hangcom to write this book, I was a bit hesitant. Due to
the large population of China, there were fierce competitions in all walks of life. I summarized
all accumulated knowledge from countless failures and if I shared all of them in details, would it
result in more competitors? Would my advantages be handed over to others? But throughout
the history of jailbreak, from Cydia and CydiaSubstrate to Theos, all these pieces of software
were open source and impressed me a lot. It was because these excellent engineers shared their
“advantages” that we could absorb knowledge from and then gradually grew better.
‘TweakWeek’ led by rpetrich and ‘OpenJailbreak’ led by posixninja also shared their valuable
core source code so that more fans could participate in building up the ecosystem of jailbroken
iOS. They were the top developers in this area and their advantages didn’t get reduced by
sharing. I was a learner who benefitted a lot from this sharing chain. Moreover, I intended to
continue my research. If I didn’t stop, my advantage would stay and the only competitor was
myself. I believed sharing would help a lot of developers who were stuck at the entry level
where I used to be. And sharing could also combine all wisdom together to make science and
technology serve people better. Meanwhile, I could make more friends. From this point of view,
writing this book can be regarded as a long term thought, just like what I did as a backpacker.
Ok, What I said above is too serious for the preface. Let me say something about this book.
The content of the book is suitable for the majority of iOS developers who are not satisfied with
developing Apps. To be honest, this book is techinically better than my master thesis. And if you
4
want to follow up, please focus on our official website http://bbs.iosre.com and our IRC
channel #Theos on irc.saurik.com. Together, let us build the jailbreak community!
Here, I want to say thank you to my mother. Without her support, I cannot focus on my
research and study. Thanks to my grandpa for the enlightenment of my English studying,
having good command of the English language is essential for communicating internationally.
Thanks to my mentor for his guidance that helped me grew fast during the three-year master
career. Thanks to DHowett, rpetrich, Optimo and those who gave me much help as well as
sharp criticism. They helped me grew fast and made me realized that I still had a lot to do.
Thanks to britta, Codyd51, DHowett, Haifisch, Tyilo, uroboro and yrp for suggestions and
review. Also, I would like to say thank you to my future girlfriend. It is the absence of you that
makes me focus on my research. So, I will share half of this book’s royalty with you :)
Career, family, friendship, love are life-long pursuits of ordinary people. However, most of
us would fail to catch them all, we have to partly give up. If that offends someone, I would like
to sincerely apologize for my behaviors and thank you for your forgiveness.
At last, I want to share a poem that I like very much. Despite regrets, life is amazing.
6
Foreword
iOS research and made big progress. I once asked him why not choose to develop iOS Apps
since there were already lots of people engaging in it and had made large amount of money. He
said that compared with making money, he’d rather be a top developer in the world. Oh boy,
how ambitious!
Most of time we solved problems independently. Although we just occasionally discussed
with each other on the Internet, we still made some valuable collaborations. Before we started
to write this book, we once cracked MOMO (a social App targeting Chinese) by reverse
engineering and made a tweak that could show position of girls on the map. Of course, we were
harmless developers and we submitted this bug to MOMO and they soon fixed it. This time, we
cooperate again, summarize our knowledge into this book and present it to you.
During these years of research on jailbreak development and reverse engineering, the
biggest payoff for me is that when I look at an iOS App, I always try to analyze it from
underlying architecture and its performance. Both can directly reflect the skill level of its
development team. Not only can reverse engineering experiences be applied to jailbreak
development, but also they are suitable for App development. Of course, we must admit there
are both positive and negative impacts on reverse engineering. However, we cannot deny the
necessity of this area even if Apple doesn’t advocate jailbreak development. If we blindly believe
that the security issues exposed in this book don’t actually exist, we’re just lying to ourselves.
Every experienced developer understands that the more knowledge you know, the more
likely you have to deal with underlying technologies. For example, what does sandbox do? Is it a
pity that we only study the mechanism of runtime theoretically?
In the field of Android development, the underlying technologies are open source.
However, for iOS, only the tip of the iceberg has been exposed. Although there are some iOS
security related books such as Hacking and Securing iOS Applications and iOS Hacker’s
Handbook, they are too hard for most App developers to understand. Even those who already
have some experience in reverse engineering, like us, have difficulties reading these books.
Since those books are too hard for most people, why not write a book consists of more
junior stage details and examples? So concepts, tools, theories and practices make up the
contents of this book in a serialized and methodological way. We illustrate our experience and
knowledge from easy to hard accompanying with lots of examples, helping readers explore the
internals of Apps step by step. We do not try to analyze only a piece of code snippets in depth
like some tech blogs. Also, we don’t want to puzzle you with how many similar solutions can
8
we use to fix the same problem. What we want to do is to provide readers with a complete
system of knowledge and a methodology of iOS reverse engineering. We believe that readers
will gain a lot from this book.
Recently, more and more programming experts are joining the jailbreak development
community. Although they keep low profile, their works, such as jailbreak tools, App assistants
and Cydia tweaks, have great influence on iOS. Their technique level is far beyond mine. But
I’m more eager to share knowledge in the hope of helping others.
• iOS enthusiasts.
• Senior iOS developers, who have good command of App development and have the desire to
understand iOS better.
• Architects. During the process of reverse engineering, they can learn architectures of those excellent
Apps so that they can improve their ability of architecture design.
9
Because all authors, translators and the editor (snakeninny himself) are not native English
speakers, this book may be linguistically ugly. But we promise that this book is techinically
pretty. So if you think anything needs to be reworded, please get to us. Thank you!
Acknowledgements
In the first place, I want to say thank you to evad3rs, PanguTeam, TaiG, saurik and other
top teams and experts.
Also thanks to Dustin Howett. His Theos is a powerful tool that helped me to step into iOS
reverse engineering.
Thanks to Security Manager for providing me with a nice atmosphere for studying reverse
engineering. Although I have left this company, I do wish it a better future.
Thanks to everyone who offers help to me. Thanks for your support and encouragement.
This book is dedicated to my dearest family, and many friends who love iOS development.
Hangcom
10
It’s more fun to be a pirate than to join the Navy.
- Steve Jobs
11
I
Concepts
Software reverse engineering refers to the process of deducing the implementation and
design details of a program or a system by analyzing the functions, structures or behaviors of it.
When we are very interested in a certain software feature while not having the access to the
source code, we can try to analyze it by reverse engineering.
For iOS developers, Apps on iOS are one of the most complex but fantastic virtual items as
far as we know. They are elaborate, meticulous and creative. As developers, when you see an
exquisite App, not only will you be amazed by its implementation, but also you will be curious
about what kind of techniques are used in this App and what we can learn from it.
12
1
Introduction to iOS reverse engineering
Although the recipe of Coca-Cola is highly confidential, some other companies can still
copy its taste. Although we don’t have access to the source code of others’ Apps, we can dig into
their details by reverse engineering.
Figure 1-1 Strong fortress, taken from Assassin’s Creed
But if we step onto another higher dimension and overlook into the castle where the App
resides, you find that structure inside the castle is no longer a secret, as shown in figure 1-2.
14
Figure 1-2 Overlook the castle, taken from Assassin’s Creed
All Objective-C interfaces, all properties, all exported functions, all global variables, even all
logics are exposed in front of us, which means all protections have became useless. So if we are
in this dimension, walls are no longer hindrances. What we should focus on is how can we find
our targets inside the huge castle.
At this point, by using reverse engineering techniques, you can enter the low dimension
castle from any high dimension places without damaging walls of the castle, which is definitely
tricky while not laborious. By monitoring and even changing the logics of Apps, you can learn
the core information and design details easily.
Sounds very incredible? But this is true. According to the experiences and achievements I’ve
got from the study of iOS reverse engineering, I can say that reverse engineering can break the
protection of most Apps, all their implementation and design details can be completely exposed.
The metaphor above is only my personal viewpoint. However, it vividly illustrates how
powerful iOS reverse engineering is. In a nutshell, there are two major functions in iOS reverse
engineering as below:
• Analyze the target App and get the core information. This can be concluded as security related
reverse engineering.
• Learn from other Apps’ features and then make use of them in our own Apps. This can be
concluded as development related reverse engineering.
15
1.2.1 Security related iOS reverse engineering
Security related IT industry would generally make extensive use of reverse engineering. For
example, reverse engineering plays the key roles in evaluating the security level of a financial
App, finding solutions of killing viruses, and setting up a spam phone call firewall on iOS, etc.
16
For anti-virus software developers, they can analyze samples of viruses through reverse
engineering, observe the behaviors of viruses and then try to kill them in the infected hosts as
well as summarize the methods to protect against viruses.
18
1.3 The process of iOS reverse engineering
When we want to reverse an App, how should we think? Where should we start? The
purpose of this book is to guide the beginners into the field of iOS reverse engineering, and
cultivate readers to think like reversers.
Generally speaking, reverse engineering can be regarded as a combination of analysis on
two stages, which are system analysis and code analysis, respectively. In the phase of system
analysis, we can find our targets by observing behavioral characteristics of program and
organizations of files. During code analysis, we need to restore the core code and then
ultimately achieve our goals.
Open them with SQLite tools, we can find some followers’ information in it, as shown in
figure 1-3.
19
Figure 1-3 Sina Weibo database
Such information provides us with clues for reverse engineering. Database file names, Sina
Weibo user IDs, URLs of user information, all can be used as cut-in points for reverse
engineering. Finding and organizing these clues, then tracking down to what we are interested
in, is often the first step of iOS reverse engineering.
20
engineering. Tools can be divided into 4 major categories; they are monitors, disassemblers,
debuggers and development kit.
1.4.1 Monitors
In the field of iOS reverse engineering, tools used for sniffing, monitoring and recording
targets’ behaviors can all be concluded as monitors. These tools generally record and display
certain operations performed by the target programs, such as UI changes, network activities and
file accesses. Reveal, snoop-it, introspy, etc., are frequently used monitors.
Reveal, as shown in figure 1-4, is a tool to see the view hierarchy of an App in real-time.
Figure 1- 4 Reveal
Reveal can assist us in locating what we are interested in an App so that we can quickly
approach the code from the UI.
1.4.2 Disassemblers
After approaching the code from the UI, we have to use disassembler to sort out the code.
Disassemblers take binaries as input, and output assembly code after processing the files. IDA
and Hopper are two major disassemblers in iOS reverse engineering.
As an evergreen disassembler, IDA is one of the most commonly used tools in reverse
engineering. It supports Windows, Linux and OSX, as well as multiple processor architectures,
as shown in figure 1-5.
21
Figure 1- 5 IDA
Hopper is a disassembler that came out in recent years, which mainly targets Apple family
operating systems, as shown in figure 1-6.
Figure 1- 6 Hopper
After disassembling binaries, we have to read the generated assembly code. This is the most
challenging task as well as the most interesting part in iOS reverse engineering, which will be
explained in detail in chapters 6 to 10. We will use IDA as the main disassembler in this book
and you can reference the experience of Hopper on http://bbs.iosre.com.
22
1.4.3 Debuggers
iOS developers should be familiar with debuggers because we often need to debug our own
code in Xcode. We can set a breakpoint on a line of code so that process will stop at that line and
display the current status of the process in real time. We constantly use LLDB for debugging
during both App development and reverse engineering. Figure 1-7 is an example of debugging in
LLDB.
Figure 1- 7 LLDB
1.5 Conclusion
In this chapter, we have introduced some concepts about iOS reverse engineering in order
to provide readers with a general idea of what we’ll be focusing on. More details and examples
will be covered in the following chapters. Stay tuned with us!
23
2
Introduction to jailbroken iOS
Compared with what we see on Apps’ UI, we are more interested in their low-level
implementation, which is exactly the motivation of reverse engineering. But as we know, non-
jailbroken iOS is a closed blackbox, it has not been exposed to the public until dev teams like
evad3rs, PanguTeam and TaiG jailbroke it, then we’re able to take a peek under the hood.
24
Figure 2- 1 iFile
With the help of AFC2, we can also access the whole iOS filesystem via software like
iFunBox on PC, as shown in figure 2-2.
Figure 2- 2 iFunBox
Because our reverse engineering targets come right from iOS, being able to access the
whole iOS filesystem is the prerequisite of our work.
25
2.1.1 iOS filesystem
iOS comes from OSX, which is based on UNIX. Although there are huge differences among
them,they are somehow related to each other. We can get some knowledge of iOS filesystem
from Filesystem Hierarchy Standard and hier(7).
Filesystem Hierarchy Standard (hereafter referred to as FHS) provides a standard for all
*NIX filesystems. The intention of FHS is to make the location of files and directories
predictable for users. Evolving from FHS, OSX has its own standard, called hier(7). Common
*NIX filesystem is as follows.
• /
Root directory. All other files and directories expand from here.
• /bin
Short for “binary”. Binaries that provide basic user-level functions, like ls and ps are stored
here.
• /boot
Stores all necessary files for booting up. This directory is empty on iOS.
• /dev
Short for “device”, stores BSD device files. Each file represents a block device or a character
device. In general, block devices transfer data in block, while character devices transfer data in
character.
• /sbin
Short for “system binaries”. Binaries that provide basic system-level functions, like netstat
and reboot are stored here.
• /etc
Short for “Et Cetera”. This directory stores system scripts and configuration files like
passwd and hosts. On iOS, this is a symbolic link to /private/etc.
• /lib
This directory stores system-level lib files, kernel files and device drivers. This directory is
empty on iOS.
26
• /mnt
Short for “mount”, stores temporarily mounted filesystems. On iOS, this directory is empty.
• /private
Only contains 2 subdirectories, i.e. /private/etc and /private/var.
• /tmp
Temporary directory. On iOS, this directory is a symbolic link to /private/var/tmp.
• /usr
A directory containing most user-level tools and programs. /usr/bin is used for other basic
functions which are not provided in /bin or /sbin, like nm and killall. /usr/include contains all
standard C headers, and /usr/lib stores lib files.
• /var
Short for “variable”, stores files that frequently change, such as log files, user data and
temporary files. /var/mobile/ is for mobile user and /var/root/ is for root user, these 2
subdirectories are our main focus.
Most directories listed above are rather low-level that they’re difficult to reverse engineer.
As beginners, it’s better for us to start with something much easier. As App developers, most of
our daily work is dealing with iOS specific directories. Reverse engineering becomes more
approachable when it comes to these familiar directories:
• /Applications
Directory for all system Apps and Cydia Apps, excluding StoreApps, as shown in figure 2-3.
27
Figure 2- 3 /Applications
• /Developer
If you connect your device with Xcode and can see it in “Devices” category like figure 2-4
shows, a “/Developer” directory will be created automatically on device, as shown in figure 2-5.
Inside this directory, there are some data files and tools for debugging.
28
Figure 2- 4 Enable debugging on device
Figure 2- 5 /Developer
• /Library
This directory contains some system-supported data as shown in figure 2-6. One
subdirectory of it named MobileSubstrate is where all CydiaSubstrate (formerly known as
MobileSubstrate) based tweaks are.
29
Figure 2- 6 /Library
• /System/Library
One of the most important directories on iOS, stores lots of system components, as shown
in figure 2-7.
Figure2- 7 /System/Library
30
² /System/Library/Frameworks and /System/Library/PrivateFrameworks
Stores most iOS frameworks. Documented APIs are only a tiny part of them, while
countless private APIs are hidden in those frameworks.
² /System/Library/CoreServices/SpringBoard.app
iOS’ graphical user interface, as is explorer to Windows. It is the most important
intermediate between users and iOS.
More directories under “/System” deserve our attention. For more advanced contents,
please visit http://bbs.iosre.com.
• /User
User directory, it’s a symbolic link to /var/mobile, as shown in figure 2-8.
Figure 2- 8 /User
31
Another major subdirectory is /var/mobile/Containers, which holds StoreApps.
It is noteworthy that bundles containing Apps’ executables reside in
/var/mobile/Containers/Bundle, while Apps’ data files reside in
/var/mobile/Containers/Data, as shown in figure 2-9.
Figure 2- 9 /var/mobile/Containers
It’s helpful to have a preliminary knowledge of iOS filesystem when we discover some
interesting functions and want to further locate their origins. What we’ve introduced above is
only a small part of iOS filesystem. For more details, please visit http://bbs.iosre.com, or just
type “man hier” in OSX terminal.
• This user is not the owner of this file, but he is a member of the (owner) group.
• This user is neither the owner nor a member of the (owner) group.
So we need 3 * 3 bits to represent a file’s permission in all situations. If a bit is set to 1, it
means the corresponding permission is granted. For instance, 111101101 represents rwxr-xr-x, in
other words, the owner has r, w and x permission, but the (owner) group and other users only
have r and x permission. Binary number 111101101 equals to octal number 755, which is another
common representation form of permission.
Actually, besides r, w, x permission, there are 3 more special permission, i.e. SUID, SGID
and sticky. They are not used in most cases, so they don’t take extra permission bits, but instead
reside in x permission’s bit. As beginners, there are slim chances that we will have to deal with
these special permission, so don’t worry if you don’t fully understand this. For those of you who
are interested, http://thegeekdiary.com/what-is-suid-sgid-and-sticky-bit/ is good to read.
2.2.1 Application
Application, namely App, is our most familiar iOS component. Although most iOS
developers deal with Apps everyday, our main focus on App is different in iOS reverse
engineering. Knowing the following concepts is a prerequisite for reverse engineering.
1. bundle
The concept of bundle originates from NeXTSETP. Bundle is indeed not a single file but a
well-organized directory conforming to some standards. It contains the executable binary and all
running necessities. Apps and frameworks are packed as bundles. PreferenceBundles (as shown
in figure 2-10), which are common in jailbroken iOS, can be seen as a kind of Settings dependent
App, which is also a bundle.
33
Figure 2- 10 PreferenceBundle
Frameworks are bundles too, but they contain dylibs instead of executables. Relatively
speaking, frameworks are more important than Apps, because most parts of an App work by
calling APIs in frameworks. When you target a bundle in reverse engineering, most of the work
can be done inside the bundle, saving you significant time and energy.
34
Figure 2- 11 Browse Info.plist in Xcode
Or with plutil:
snakeninnysiMac:~ snakeninny$ plutil -p
/Users/snakeninny/Code/iOSSystemBinaries/8.1_iPhone5/SiriViewService.app/Info.plist |
grep CFBundleExecutable
"CFBundleExecutable" => "SiriViewService"
• lproj directories
Localized strings are saved in lproj directories. They are important clues of iOS reverse
engineering. plutil tool can also parse those .string files.
snakeninnysiMac:~ snakeninny$ plutil -p
/Users/snakeninny/Code/iOSSystemBinaries/8.1_iPhone5/SiriViewService.app/en.lproj/Locali
zable.strings
{
"ASSISTANT_INITIAL_QUERY_IPAD" => "What can I help you with?"
"ASSISTANT_BOREALIS_EDUCATION_SUBHEADER_IPAD" => "Just say “Hey Siri” to learn more."
"ASSISTANT_FIRST_UNLOCK_SUBTITLE_FORMAT" => "Your passcode is required when %@
restarts"
……
You will see how we make use of .strings in reverse engineering in chapter 5.
35
3. System App VS. StoreApp
/Applications contains system Apps and Cydia Apps (We treat Cydia Apps as system Apps),
and /var/mobile/Containers/Bundle/Application is where StoreApps reside. Although all of
them are categorized as Apps, they are different in some ways:
• Directory hierarchy
Both system Apps and StoreApps share the similar bundle hierarchy, including Info.plist
files, executables and lproj directories, etc. But the path of their data directory is different, for
StoreApps, their data directories are under /var/mobile/Containers/Data, while for system
Apps running as mobile, their data directories are under /var/mobile; for system Apps running
as root, their data directories are under /var/root.
• Sandbox
Broadly speaking, sandbox is a kind of access restriction mechanism, we can see it as a form
of permission. Entitlements are also a part of sandbox. Sandbox is one of the core components
of iOS security, which possesses a rather complicated implementation, and we’re not going to
discuss it in details. Generally, sandbox restricts an App’s file access scope inside the App itself.
Most of the time, an App has no idea of the existence of other Apps, not to mention accessing
them. What’s more, sandbox restricts an App’s function. For example, an App has to ask for
sandbox’s permission to take iCloud related operations.
Sandbox is not suitable to be beginners’ target, it’d be enough for us to know its existence.
In iOS reverse engineering, jailbreak has already removed most security protections of iOS, and
reduced sandbox’s constraints in some degree, so we are likely to ignore the existence of
sandbox, hence leading to some strange phenomena such as a tweak cannot write to a file, or
calls a function but it’s not functioning as expected. If you can make sure your code is 100%
correct, then you should recheck if the problem is because of your misunderstanding of tweak’s
36
permission or sandbox issues. Concepts about Apps cannot be fully described in this book, so if
you have any questions, feel free to raise it on http://bbs.iosre.com.
If we shift our attention to jailbroken iOS, all the tweaks in Cydia work as dylibs. It is those
tweaks’ existence that makes it possible for us to customize our iPhones. In reverse engineering,
we’ll be dealing with all kinds of dylibs a lot, so it’d be good for us to know some basic concepts.
On iOS, libs are divided into two types, i.e. static and dynamic. Static libs will be integrated
into an App’s executable during compilation, therefore increases the App’s size. Now that we
have a bigger executable, iOS needs to load more data into memory during App launching, so
the result is that, not surprisingly, App’s launch time increased, too. Dylibs are relatively
“smart”, it doesn’t affect executable’s size, and iOS will load a dylib into memory only when an
App needs it right away, then the dylib becomes part of the App.
It’s worth mentioning that, although dylibs exist everywhere on iOS, and they are the main
targets of reverse engineering, they are not executables. They cannot run individually, but only
serve other processes. In other words, they live in and become a part of other processes. Thus,
dylibs’ permission depends on the processes they live in, the same dylib’s permission is different
when it lives in a system App or a StoreApp. For instance, suppose you write an Instagram
tweak to save your favorite pictures locally, if the destination path is this App’s documents
directory under /var/mobile/Containers/Data, there won’t be a problem because Instagram is
a StoreApp, it can write to its own documents. But if the destination path is
/var/mobile/Documents, then when you save pictures happily and want to review them
wistfully, you’ll find nothing under /var/mobile/Documents. All the tweak operations are
banned by sandbox.
37
2.2.3 Daemon
Since your first day doing iOS development, Apple has been telling you “There is no real
backgrounding on iOS and your App can only operate with strict limitations.” If you are a pure
App Store developer, following Apple’s rules and announcements can make the review process
much easier! However, since you’re reading this book, you likely want to learn reverse
engineering and this means straying into undocumented territory. Stay calm and follow me:
• When I’m browsing reddit or reading tweets on my iPhone, suddenly a phone call comes in. All
operations are interrupted immediately, and iOS presents the call to me. If there is no real
backgrounding on iOS, how can iOS handle this call in real time?
• For those who receive spam iMessages a lot, firewalls like SMSNinja are saviors. If a firewall fails to
stay in the background, how could it filter every single iMessages instantaneously?
• Backgrounder is a famous tweak on iOS 5. With the help of this tweak, we can enable real
backgrounding for Apps! Thanks to this tweak, we don’t have to worry about missing WhatsApp
messages because of unreliable push notifications any more. If there is no real backgrounding, how
could Backgrounder even exist?
All these phenomena indicate that real backgrounding does exist on iOS. Does that mean
Apple lied to us? I don’t think so. For a StoreApp, when user presses the home button, this App
enters background, most functions will be paused. In other words, for App Store developers,
you’d better view iOS as a system without real backgrounding, because the only thing Apple
allows you to do doesn’t support real backgrounding. But iOS originates from OSX, and like all
*NIX systems, OSX has daemons (The same thing is called service on Windows). Jailbreak opens
the whole iOS to us, thus reveals all daemons.
Daemons are born to run in the background, providing all kinds of services. For example,
imagent guarantees the correct sending and receiving of iMessages, mediaserverd handles
almost all audios and videos, and syslogd is used to record system logs. Each daemon consists of
two parts, one executable and one plist file. The root process on iOS is launchd, which is also a
daemon, checks all plist files under /System/Library/LaunchDaemons and
/Library/LaunchDaemons after each reboot, then run the corresponding executable to launch
the daemon. A daemons’ plist file plays a similar role as an App’s Info.plist file, it records the
daemon’s basic information, as shown in the following:
snakeninnys-MacBook:~ snakeninny$ plutil -p
/Users/snakeninny/Code/iOSSystemBinaries/8.1.1_iPhone5/System/Library/LaunchDaemons/com.
apple.imagent.plist
38
{
"WorkingDirectory" => "/tmp"
"Label" => "com.apple.imagent"
"JetsamProperties" => {
"JetsamMemoryLimit" => 3000
}
"EnvironmentVariables" => {
"NSRunningFromLaunchd" => "1"
}
"POSIXSpawnType" => "Interactive"
"MachServices" => {
"com.apple.hsa-authentication-server" => 1
"com.apple.imagent.embedded.auth" => 1
"com.apple.incoming-call-filter-server" => 1
}
"UserName" => "mobile"
"RunAtLoad" => 1
"ProgramArguments" => [
0 => "/System/Library/PrivateFrameworks/IMCore.framework/imagent.app/imagent"
]
"KeepAlive" => {
"SuccessfulExit" => 0
}
}
Compared with Apps, daemons provide much much lower level functions, accompanying
with much much greater difficulties reverse engineering them. If you don’t know what you’re
doing for sure, don’t even try to modify them! It may break your iOS, leading to booting
failures, so you’d better stay away from daemons as reverse engineering newbies . After you get
some experiences reverse engineering Apps, it’d be OK for you to challenge daemons. After all,
it takes more time and energy to reverse a daemon, but great rewards pay off later. The
community acknowledged “first iPhone call recording App”, i.e. Audio Recorder, is
accomplished by reversing mediaserverd.
2.3 Conclusion
This chapter simply introduces iOS system hierarchy and file types, which are not
necessities for App Store developers, who don’t even have an official way to learn about the
concepts. This chapter’s intention is to introduce you the very important yet undocumented
system level knowledge, which is essential in iOS reverse engineering.
In fact, every section in this chapter can be extended into another full chapter, but as
beginners, knowing what we’re talking about and what to google when you encounter
problems during iOS reverse engineering is enough. If you have anything to say, welcome to
http://bbs.iosre.com.
39
II
Tools
In the 1st part, we’ve introduced the basic concepts of iOS reverse engineering. In this part,
we will introduce the toolkit of iOS reverse engineering.
Compared with App development, the main feature of iOS reverse engineering is it’s more
“mixed”. When you are writing Apps, most work can be done within Xcode, since it is the
product of Apple, it’s convenient to download, install and use. As for some other tools and
plugins, they are just some kind of icing on the cake, thus useful but non-essential.
But, in iOS reverse engineering, we have to face so many complicated tools. Let me make
an example, there are two dinner tables in front of you, on the first table there’s simply a pair of
chopsticks, it’s named Xcode; the other one is full of knives and forks, in which some of the big
shots are Theos, Reveal, IDA and etc…
Unlike Xcode, there is no tight connection among those reverse engineering tools; they are
separated from each other, so we need to integrate them manually. We cannot cover all reverse
engineering tools in this part, but I think you will have the ability to find and use proper tools
according to the situation you face when you finish reading this book. You can also share your
findings with us on http://bbs.iosre.com.
Because the tools to be introduced are quite disordered, we split this part to two chapters,
one is for OSX tools, the other is for iOS. The device used in this part is iPhone 5 with iOS 8.1.
40
3
OSX toolkit
Tools used for iOS reverse engineering have different functions, and they play different
roles. These tools mainly help us develop and debug on OSX. Because of the small screen size of
iOS devices, they are not suitable for development or debug.
In this chapter, 4 major tools will be introduced, they’re class-dump, Theos, Reveal and
IDA. Other tools are assistants for them.
3.1 class-dump
class-dump, as the name indicates, is a tool used for dumping the class information of the
specified object. It makes use of the runtime mechanism of Objective-C language to extract the
headers information stored in Mach-O files, and then generates .h files.
class-dump is simple to use. Firstly, you need to download the latest version from
http://stevenygard.com/projects/class-dump, as figure 3-1 shows:
The targets of class-dump are Mach-O binaries, such as library files of frameworks and
executables of Apps. Now, I will show you an example of how to use class-dump.
42
snakeninnysiMac:SMSNinja.app snakeninny$ class-dump -S -s -H SMSNinja -o
/path/to/headers/SMSNinja/
Repeat this on your own App, and compare the original headers with class-dump headers,
aren’t they very similar? You will see all the methods are nearly the same except that some
arguments’ types have been changed to id and their names are missing. With “-S” and “-s”
options, the headers are even more readable.
class-dumping our own Apps doesn’t make much sense; since class-dump works on closed-
source Apps of our own, it can also be used to analyze others’ Apps.
From the dumped headers, we can take a peek at the architecture of an App; information
under the skin is the cornerstone of iOS reverse engineering. Now that App sizes have become
bigger and bigger, more and more third-party libraries are integrated into our own projects,
class-dump often produces hundreds and thousands of headers. It’d be a great practice analyzing
them one by one manually, but that’s overwhelming workload. In the following chapters, we
will show you several ways to lighten our workload and focus on the core problems.
It’s worth mentioning that, Apps downloaded from AppStore are encrypted by Apple,
executables are “shelled” like walnuts, protecting class-dump from working, class-dump will fail
in this situation. To enable it again, we need other tools to crack the shell at first, and I’ll leave
this to the next chapter. To learn more about class-dump, please refer to http://bbs.iosre.com.
3.2 Theos
43
reverse engineering.
2. Download Theos
Download Theos from GitHub using the following commands:
snakeninnysiMac:~ snakeninny$ export THEOS=/opt/theos
snakeninnysiMac:~ snakeninny$ sudo git clone git://github.com/DHowett/theos.git $THEOS
Password:
Cloning into '/opt/theos'...
remote: Counting objects: 4116, done.
remote: Total 4116 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (4116/4116), 913.55 KiB | 15.00 KiB/s, done.
Resolving deltas: 100% (2063/2063), done.
Checking connectivity... done
3. Configure ldid
ldid is a tool to sign iOS executables; it replaces codesign from Xcode in jailbreak
development. Download it from http://joedj.net/ldid to “/opt/theos/bin/”, then grant it
execute permission using the following command:
snakeninnysiMac:~ snakeninny$ sudo chmod 777 /opt/theos/bin/ldid
4. Configure CydiaSubstrate
First run the auto-config script in Theos:
snakeninnysiMac:~ snakeninny$ sudo /opt/theos/bin/bootstrap.sh substrate
Password:
Bootstrapping CydiaSubstrate...
Compiling iPhoneOS CydiaSubstrate stub... default target?
failed, what?
44
Compiling native CydiaSubstrate stub...
Generating substrate.h header...
Here we’ll meet a bug that Theos cannot generate a working libsubstrate.dylib, which
requires our manual fixes. Piece of cake: first search and install CydiaSubstrate in Cydia, as
shown in figure 3-2.
Figure 3- 2 CydiaSubstrate
5. Configure dpkg-deb
The standard installation package format in jailbreak development is deb, which can be
manipulated by dpkg-deb. Theos uses dpkg-deb to pack projects to debs.
Download dm.pl from
https://raw.githubusercontent.com/DHowett/dm.pl/master/dm.pl, rename it dpkg-deb and
move it to “/opt/theos/bin/”, then grant it execute permission using the following command:
snakeninnysiMac:~ snakeninny$ sudo chmod 777 /opt/theos/bin/dpkg-deb
45
6. Configure Theos NIC templates
It is convenient for us to create various Theos projects because Theos NIC templates have 5
different Theos project templates. You can also get 5 extra templates from
https://github.com/DHowett/theos-nic-templates/archive/master.zip and put the 5 extracted
.tar files under “/opt/theos/templates/iphone/”. Some default values of NIC can be
customized, please refer to
http://iphonedevwiki.net/index.php/NIC#How_to_set_default_values.
There are 10 templates available, among which 1, 4, 6, 8, 9 are Theos embedded, and 2, 3, 5,
7, 10 are downloaded in the previous section. At the beginning stage of iOS reverse engineering,
we’ll be writing tweaks most of the time, usage of other templates can be discussed on
http://bbs.iosre.com.
2) Chose “9” to create a tweak project:
Choose a Template (required): 9
6) Enter “MobileSubstrate Bundle filter”, i.e. bundle identifier of the tweak target:
[iphone/tweak] MobileSubstrate Bundle filter [com.apple.springboard]:
com.apple.springboard
After these 7 simple steps, a folder named iosreproject is created in the current directory,
which contains the tweak project we just created.
There are only 4 files except one symbolic link pointing to Theos. To be honest, when I first
created a tweak project with Theos as a newbie, the simplicity of this project actually attracted
me instead of freaking me out, which surprised me. Less is more, Theos does an amazing job in
good user experience.
4 files are enough to build a roughcast house, yet more decoration is needed to make it
flawless. We’re going to extend the 4 files for now.
• Makefile
The project files, frameworks and libraries are all specified in Makefile, making the whole
compile process automatic. The Makefile of iOSREProject is shown as follows:
include theos/makefiles/common.mk
TWEAK_NAME = iOSREProject
iOSREProject_FILES = Tweak.xm
include $(THEOS_MAKE_PATH)/tweak.mk
after-install::
install.exec "killall -9 SpringBoard"
47
Let’s do a brief introduction line by line.
include theos/makefiles/common.mk
The tweak name, i.e. the “Project name” in NIC when we create a Theos project. It
corresponds to the “Name” field of the control file, please don’t change it.
iOSREProject_FILES = Tweak.xm
Source files of the tweak project, excluding headers; multiple files should be separated by
spaces, like:
iOSREProject_FILES = Tweak.xm Hook.xm New.x ObjC.m ObjC++.mm
According to different types of Theos projects, different .mk files will be included. In the
beginning stage of iOS reverse engineering, 3 types of projects are commonly created, they are
Application, Tweak and Tool, whose corresponding files are application.mk, tweak.mk and
tool.mk respectively. It can be changed on demand.
after-install::
install.exec "killall -9 SpringBoard"
I guess you know what’s the purpose of these two lines of code from the literal meaning,
which is to kill SpringBoard after the tweak is installed during development, and to let
CydiaSubstrate load the proper dylibs into SpringBoard when it relaunches.
The content of Makefile seems easy, right? But it’s too easy to be enough for a real tweak
project. How do we specify the SDK version? How do we import frameworks? How do we link
libs? These questions remain to be answered. Don’t worry, the bread will have of, the milk will
also have of.
Different CPU architectures should be separated by spaces in the above configuration. Note,
Apps with arm64 instructions are not compatible with armv7/armv7s dylibs, they have to link
dylibs of arm64. In the vast majority of cases, just leave it as “armv7 arm64”.
For example:
48
export TARGET = iphone:clang:8.1:8.0
It specifies the base SDK version of this project to 8.1, as well deployment target to iOS 8.0.
We can also specify “Base SDK” to “latest” to indicate that the project will be compiled with the
latest SDK of Xcode, like:
export TARGET = iphone:clang:latest:8.0
² Import frameworks
iOSREProject_FRAMEWORKS = framework name
For example:
iOSREProject_FRAMEWORKS = UIKit CoreTelephony CoreAudio
For example:
iOSREProject_PRIVATE_FRAMEWORKS = AppSupport ChatKit IMCore
Although it seems to be only one inserted word “ PRIVATE “, there’s more to notice.
Importing private frameworks is not allowed in AppStore development, most of us are not
familiar with them. Private frameworks change a lot in each iOS version, so before importing
them, please make sure of the existence of the imported frameworks. For example, if you want
your tweak to be compatible with both iOS 7 and iOS 8, then Makefile could be written as
follows:
export ARCHS = armv7 arm64
export TARGET = iphone:clang:latest:7.0
include theos/makefiles/common.mk
TWEAK_NAME = iOSREProject
iOSREProject_FILES = Tweak.xm
iOSREProject_PRIVATE_FRAMEWORK = BaseBoard
include $(THEOS_MAKE_PATH)/tweak.mk
after-install::
install.exec "killall -9 SpringBoard"
This tweak can be compiled and linked successfully without any error. However,
BaseBoard.framework only exists in SDK of iOS 8 and above, so this tweak would fail to work
on iOS 7 because of the lack of specified frameworks. In this case, “weak linking” or dyld series
functions like dlopen(), dlsym() and dlclose() can solve this problem.
49
² Link Mach-O Objects
iOSREProject_LDFLAGS = -lx
Theos use GNU Linker to link Mach-O objects, including .dylib, .a and .o files. Input “man
ld” in Terminal and locate to “-lx”, it is described as follows:
“-lx This option tells the linker to search for libx.dylib or libx.a in the library search
path. If string x is of the form y.o, then that file is searched for in the same places, but
without prepending `lib' or appending `.a' or `.dylib' to the filename.”
As shown in figure 3-3, all Mach-O objects are named in the formats of “libx.dylib” and
“y.o”, who’re fully compatible with GNU Linker.
So, linking Mach-O objects becomes convenient. For example, if you want to link
libsqlite3.0.dylib, libz.dylib and dylib1.o, you can do it like this:
iOSREProject_LDFLAGS = -lz –lsqlite3.0 –dylib1.o
There is still one more field to introduce later, but without it Makefile is good to work for
now. For more Makefile introductions, you can refer to
http://www.gnu.org/software/make/manual/html_node/Makefiles.html.
• Tweak.xm
The default source file of a tweak project created by Theos is Tweak.xm. “x” in “xm”
50
indicates that this file supports Logos syntax; if this file is suffixed with an only “x”, it means
Tweak.x will be processed by Logos, then preprocessed and compiled as objective-c; if the suffix
is “xm”, Tweak.xm will be processed by Logos, then preprocessed and compiled as objective-
c++, just like the differences between “m” and “mm” files. There are 2 more suffixes as “xi” and
“xmi”, you can refer to
http://iphonedevwiki.net/index.php/Logos#File_Extensions_for_Logos for details.
The default content of Tweak.xm is as follows:
/* How to Hook with Logos
Hooks are written with syntax similar to that of an Objective-C @implementation.
You don't need to #include <substrate.h>, it will be done automatically, as will
the generation of a class list and an automatic constructor.
%hook ClassName
%orig; // Call through to the original function with its original arguments.
%orig(nil); // Call through to the original function with a custom argument.
// If you use %orig(), you MUST supply all arguments (except for self and _cmd,
the automatically generated ones.)
}
return awesome;
}
// Always make sure you clean up after yourself; Not doing so could have grave
consequences!
%end
*/
These are the basic Logos syntax, including 3 preprocessor directives: %hook, %log and
%orig. The next 3 examples show how to use them.
² %hook
%hook specifies the class to be hooked, must end with %end, for example:
51
%hook SpringBoard
- (void)_menuButtonDown:(id)down
{
NSLog(@"You’ve pressed home button.");
%orig; // call the original _menuButtonDown:
}
%end
² %log
This directive is used inside %hook to write the method arguments to syslog. We can also
append anything else with the format of %log([(<type>)<expr>, …]), for example:
%hook SpringBoard
- (void)_menuButtonDown:(id)down
{
%log((NSString *)@"iOSRE", (NSString *)@"Debug");
%orig; // call the original _menuButtonDown:
}
%end
² %orig
%orig is also used inside %hook; it executes the original hooked method, for example:
%hook SpringBoard
- (void)_menuButtonDown:(id)down
{
NSLog(@"You’ve pressed home button.");
%orig; // call the original _menuButtonDown:
}
%end
If %orig is removed, the original method will not be executed, for example:
%hook SpringBoard
- (void)_menuButtonDown:(id)down
{
52
NSLog(@"You’ve pressed home button but it’s not functioning.");
}
%end
It can also be used to replace arguments of the original method, for example:
%hook SBLockScreenDateViewController
- (void)setCustomSubtitleText:(id)arg1 withColor:(id)arg2
{
%orig(@"iOS 8 App Reverse Engineering", arg2);
}
%end
The lock screen looks like figure 3-4 with the new argument:
Besides %hook, %log and %orig, there are other common preprocessor directives such as
%group, %init, %ctor, %new and %c.
² %group
This directive is used to group %hook directives for better code management and
conditional initialization (We’ll talk about this soon). %group must end with %end, one %group
can contain multiple %hooks, all %hooks that do not belong to user-specific groups will be
grouped into %group _ungrouped. For example:
%group iOS7Hook
%hook iOS7Class
- (id)iOS7Method
{
id result = %orig;
53
NSLog(@"This class & method only exist in iOS 7.");
return result;
}
%end
%end // iOS7Hook
%group iOS8Hook
%hook iOS8Class
- (id)iOS8Method
{
id result = %orig;
NSLog(@"This class & method only exist in iOS 8.");
return result;
}
%end
%end // iOS8Hook
%hook SpringBoard
-(void)powerDown
{
%orig;
}
%end
² %init
This directive is used for %group initialization; it must be called inside %hook or %ctor. If a
group name is specified, it will initialize %group SpecifiedGroupName, or it will initialize
%group _ungrouped, for example:
#ifndef kCFCoreFoundationVersionNumber_iOS_8_0
#define kCFCoreFoundationVersionNumber_iOS_8_0 1140.10
#endif
%hook SpringBoard
- (void)applicationDidFinishLaunching:(id)application
{
%orig;
54
² %ctor
The constructor of a tweak, it is the first function to be called in the tweak. If we don’t
define a constructor explicitly, Theos will create one for us automatically, and call
%init(_ungrouped) inside it.
%hook SpringBoard
- (void)reboot
{
NSLog(@"If rebooting doesn’t work then I’m screwed.");
%orig;
}
%end
The above code works fine, because Theos has called %init implicitly like this:
%ctor
{
%init(_ungrouped);
}
However,
%hook SpringBoard
- (void)reboot
{
NSLog(@"If rebooting doesn’t work then I’m screwed.");
%orig;
}
%end
%ctor
{
// Need to call %init explicitly!
}
This %hook never works, because we’ve defined %ctor explicitly without calling %init
explicitly, there lacks a %group(_ungrouped). Generally, %ctor is used to call %init and
MSHookFunction, for example:
#ifndef kCFCoreFoundationVersionNumber_iOS_8_0
#define kCFCoreFoundationVersionNumber_iOS_8_0 1140.10
#endif
%ctor
{
%init;
if (kCFCoreFoundationVersionNumber >= kCFCoreFoundationVersionNumber_iOS_7_0 &&
kCFCoreFoundationVersionNumber <
kCFCoreFoundationVersionNumber_iOS_8_0) %init(iOS7Hook);
if (kCFCoreFoundationVersionNumber >=
kCFCoreFoundationVersionNumber_iOS_8_0) %init(iOS8Hook);
MSHookFunction((void *)&AudioServicesPlaySystemSound,
(void *)&replaced_AudioServicesPlaySystemSound,
(void **)&original_AudioServicesPlaySystemSound);
}
Attention, %ctor doesn’t end with %end.
55
² %new
%new is used inside %hook to add a new method to an existing class; it’s the same as
class_addMethod, for example:
%hook SpringBoard
%new
- (void)namespaceNewMethod
{
NSLog(@"We’ve added a new method to SpringBoard.");
}
%end
Some of you may wonder, category in Objective-C can already add new methods to classes,
why do we still need %new? The difference between category and %new is that the former is
static while the latter is dynamic. Well, does static adding or dynamic adding matter? Yes,
especially when the class to be added is from a certain executable, it matters. For example, the
above code adds a new method to SpringBoard. If we use category, the code should look like
this:
@interface SpringBoard (iOSRE)
- (void)namespaceNewMethod;
@end
We will get “error: cannot find interface declaration for ‘SpringBoard’” when trying to
compile the above code, which indicates that the compiler cannot find the definition of
SpringBoard. We can compose a SpringBoard class to cheat the compiler:
@interface SpringBoard : NSObject
@end
56
ld: symbol(s) not found for architecture armv7
clang: error: linker command failed with exit code 1 (use -v to see invocation)
ld cannot find the definition of SpringBoard. Normally, when there’s “symbol(s) not found”,
most of you may think, if this is because I forget to import any framework? But, SpringBoard is a
class of SpringBoard.app rather than a framework, how do we import an executable? I bet you
know %new’s usage right now.
² %c
This directive is equal to objc_getClass or NSClassFromString, it is used in %hook or %ctor
to dynamically get a class by name.
Other Logos preprocessor directives including %subclass and %config are seldom used, at
least I myself have never used them before. Nonetheless, if you’re interested in them, you can
refer to http://iphonedevwiki.net/index.php/Logos, or go to http://bbs.iosre.com for
discussion.
• control
The contents of control file are basic information of the current deb package; they will be
packed into the deb package. The contents of iOSREProject’s control file are shown as follows:
Package: com.iosre.iosreproject
Name: iOSREProject
Depends: mobilesubstrate
Version: 0.0.1
Architecture: iphoneos-arm
Description: An awesome MobileSubstrate tweak!
Maintainer: snakeninny
Author: snakeninny
Section: Tweaks
² Package field is the name of the deb package, it has the similar naming convention to bundle
identifier, i.e. reverse DNS format. It can be changed on demand.
² Name field is used to describe the name of the project; it also can be changed.
² Depends field is used to describe the dependency of this deb package. Dependency means the basic
condition to run this tweak, if the current environment does not meet the condition described in
depends field, this tweak cannot run properly. For example, the following code means the tweak
must run on iOS 8.0 or later with CydiaSubstrate installed.
Depends: mobilesubstrate, firmware (>= 8.0)
² Version field is used to describe the version of the deb package; it can be changed on demand.
57
² Architecture field is used to describe the target device architecture, do not change it.
² Description field is used to give a brief introduction of the deb package; it can be changed on
demand.
² Maintainer field is used to describe the maintainer of the deb package, say, all deb packages on
TheBigBoss are maintained by BigBoss instead of the author. This field can be changed on demand.
² Author field is used to describe the author of the tweak, which is different from the maintainer. It
can be changed on demand.
² Section field is used to describe the program type of the deb package, don’t change it.
There are still some other fields in control file, but the above fields are enough for Theos
projects. For more information, please refer to the official site of debian,
http://www.debian.org/doc/debian-policy/ch-controlfields.html, or control files in other deb
packages. It’s worth mentioning that Theos will further edit control file when packaging:
Package: com.iosre.iosreproject
Name: iOSREProject
Depends: mobilesubstrate
Architecture: iphoneos-arm
Description: An awesome MobileSubstrate tweak!
Maintainer: snakeninny
Author: snakeninny
Section: Tweaks
Version: 0.0.1-1
Installed-Size: 104
During editing, Theos changes the Version field to indicate packaging times; adds an
Installed-Size field to indicate the size of the package. This size may not be exactly the same to
the actual size, but don’t change it.
The information of control file will show in Cydia directly, as shown in figure 3-5:
58
Figure 3- 5 Control informaton in Cydia
• iOSREProject.plist
This plist file has the similar function to Info.plist of an App, which records some
configuration information. Specifically in a tweak, it describes the functioning scope of the
tweak. It can be edited with plutil or Xcode.
iOSREProject.plist consists of a “Root” dictionary, which has a key named “Filter”, as
shown in figure 3-6:
Figure 3- 6 iOSREProject.plist
There’s a series of arrays under “Filter”, which can be categorized into 3 types.
² “Bundles” specifies several bundles as the tweak’s targets, as shown in figure 3-7.
59
Figure 3- 7 Bundles
² “Classes” specifies several classes as the tweak’s targets, as shown in figure 3-8.
Figure 3- 8 Classes
According to figure 3-8, this tweak targets 3 classes, i.e. NSString, SBAwayController and
SBIconModel.
² “Executables” specifies several executables as the tweak’s targets, as shown in figure 3-9.
Figure 3- 9 Executables
According to figure 3-9, this tweak targets 3 executables, i.e. callservicesd, imagent and
mediaserverd.
These 3 types can be used together, as shown in figure 3-10.
60
Figure 3- 10 A Mix-targeted tweak
Attention, when there’re different kinds of arrays in “Filter”, we have to add an extra “Mode
: Any” key-value pair.
• Compile
“make” command is used to compile Theos project. Just run “make” under our Theos
project directory:
snakeninnysiMac:iosreproject snakeninny$ make
Making all for tweak iOSREProject...
Preprocessing Tweak.xm...
Compiling Tweak.xm...
Linking tweak iOSREProject...
Stripping iOSREProject...
Signing iOSREProject...
From the output, we know Theos has finished preprocessing, compiling, linking, stripping
and signing. After that, an “obj” folder appears in the current folder.
snakeninnysiMac:iosreproject snakeninny$ ls -l
total 32
-rw-r--r-- 1 snakeninny staff 262 Dec 3 09:20 Makefile
-rw-r--r-- 1 snakeninny staff 0 Dec 3 11:28 Tweak.xm
-rw-r--r-- 1 snakeninny staff 223 Dec 3 09:05 control
-rw-r--r--@ 1 snakeninny staff 175 Dec 3 09:48 iOSREProject.plist
drwxr-xr-x 5 snakeninny staff 170 Dec 3 11:28 obj
lrwxr-xr-x 1 snakeninny staff 11 Dec 3 09:05 theos -> /opt/theos
• Package
Theos uses “make package” command to pack Theos projects. In fact, “make package”
executes “make” and “dpkb-deb” in sequence to finish its job.
snakeninnysiMac:iosreproject snakeninny$ make package
Making all for tweak iOSREProject...
Preprocessing Tweak.xm...
Compiling Tweak.xm...
Linking tweak iOSREProject...
Stripping iOSREProject...
Signing iOSREProject...
Making stage for tweak iOSREProject...
dm.pl: building package `com.iosre.iosreproject' in `./com.iosre.iosreproject_0.0.1-
7_iphoneos-arm.deb'.
What’s this folder for? Open it, we can see 2 subfolders in it, namely “DEBIAN” and
“Library”:
snakeninnysiMac:iosreproject snakeninny$ ls -l _
total 0
drwxr-xr-x 3 snakeninny staff 102 Dec 3 11:35 DEBIAN
drwxr-xr-x 3 snakeninny staff 102 Dec 3 11:35 Library
62
Fire 3- 11 Library directory structure
We can see that they have the same directory structures, and you may have already guessed
that this deb package is simply a combination of “DEBIAN” which contains debian information,
and “Library” which contains the actual files. In fact, we can also create a “layout” folder under
the current project directory before packaging and installing the project on iOS. In this way, all
files in “layout” will be extracted to the same positions of iOS filesystem (“layout” mentioned
63
here acts as root directory, i.e. “/” on iOS), enhancing the functionality of deb packages lot.
Let’s take an example to see the magic of “layout”.
Go back to iOSREProject, input “make clean” and “rm *.deb” in Terminal to restore the
project to the original state:
snakeninnysiMac:iosreproject snakeninny$ make clean
rm -rf ./obj
rm -rf "/Users/snakeninny/Code/iosreproject/_"
snakeninnysiMac:iosreproject snakeninny$ rm *.deb
snakeninnysiMac:iosreproject snakeninny$ ls -l
total 32
-rw-r--r-- 1 snakeninny staff 262 Dec 3 09:20 Makefile
-rw-r--r-- 1 snakeninny staff 0 Dec 3 11:28 Tweak.xm
-rw-r--r-- 1 snakeninny staff 223 Dec 3 09:05 control
-rw-r--r--@ 1 snakeninny staff 175 Dec 3 09:48 iOSREProject.plist
lrwxr-xr-x 1 snakeninny staff 11 Dec 3 09:05 theos -> /opt/theos
At last, run “make package” to pack, then copy the deb package to iOS, and install it via
iFile. Now you can inspect files of iOSREProject in Cydia, as shown in figure 3-13.
64
Figure 3-13 Installed files of iOSREProject
As we can see, all the files except “DEBIAN” are extracted to the same positions of iOS
filesystem, all necessary subfolders are also created automatically. There are still many things
about deb package we didn’t mention, please refer to http://www.debian.org/doc/debian-
policy for more information.
• Installation
Last but not least, we need to install this deb package on iOS. There are several ways to
install, but installation through GUI and installation through command line are two of the most
typical installation methods. Most of you may think the GUI way is easier, well, let’s take a look
at it first.
Then enter “make package install” to compile, package and install in one click:
snakeninnysiMac:iosreproject snakeninny$ make package install
Making all for tweak iOSREProject...
Preprocessing Tweak.xm...
Compiling Tweak.xm...
Linking tweak iOSREProject...
Stripping iOSREProject...
Signing iOSREProject...
Making stage for tweak iOSREProject...
dm.pl: building package `com.iosre.iosreproject:iphoneos-arm' in
`./com.iosre.iosreproject_0.0.1-15_iphoneos-arm.deb'
install.exec "cat > /tmp/_theos_install.deb; dpkg -i /tmp/_theos_install.deb && rm
/tmp/_theos_install.deb" < "./com.iosre.iosreproject_0.0.1-15_iphoneos-arm.deb"
root@iOSIP's password:
Selecting previously deselected package com.iosre.iosreproject.
(Reading database ... 2864 files and directories currently installed.)
Unpacking com.iosre.iosreproject (from /tmp/_theos_install.deb) ...
Setting up com.iosre.iosreproject (0.0.1-15) ...
install.exec "killall -9 SpringBoard"
root@iOSIP's password:
Among the above information, Theos has asked for the root password twice. Although it
seems safe, it’s inconvenient. Fortunately, we can skip the input of password over and over by
configuring the authorized_keys on iOS, as follows:
² Remove the entry of iOSIP in “/Users/snakeninny/.ssh/known_hosts”.
Assume that your iOS IP address is iOSIP. Edit “/Users/snakeninny/.ssh/known_hosts”,
and locate the entry of iOSIP:
iOSIP ssh-rsa
hXFscxBCVXgqXhwm4PUoUVBFWRrNeG6gVI3Ewm4dqwusoRcyCxZtm5bRiv4bXfkPjsRkWVVfrW3uT52Hhx4RqIuC
OxtWE7tZqc1vVap4HIzUu3mwBuxog7WiFbsbbaJY4AagNZmX83Wmvf8li5aYMsuKeNagdJHzJNtjM3vtuskK4jKz
BkNuj0M89TrV4iEmKtI4VEoEmHMYzWwMzExXbyX5NyEg5CRFmA46XeYCbcaY0L90GExXsWMMLA27tA1Vt1ndHrKN
xZttgAw31J90UDnOGlMbWW4M7FEqRWQsWXxfGPk0W7AlA54vaDXllI5CD5nLAu4VkRjPIUBrdH5O1fqQ3qGkPayh
sym3g0VZeYgU4JAMeFc3
² Generate authorized_keys.
Execute the following commands in Terminal:
snakeninnysiMac:~ snakeninny$ ssh-keygen -t rsa
Generating public/private rsa key pair.
66
Enter file in which to save the key (/Users/snakeninny/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /Users/snakeninny/.ssh/id_rsa.
Your public key has been saved in /Users/snakeninny/.ssh/id_rsa.pub.
……
snakeninnysiMac:~ snakeninny$ cp /Users/snakeninny/.ssh/id_rsa.pub ~/authorized_keys
² Configure iOS
Execute the following commands in Terminal:
FunMaker-5:~ root# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/var/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /var/root/.ssh/id_rsa.
Your public key has been saved in /var/root/.ssh/id_rsa.pub.
……
FunMaker-5:~ root# logout
Connection to iOSIP closed.
snakeninnysiMac:iosreproject snakeninny$ scp ~/authorized_keys root@iOSIP:/var/root/.ssh
The authenticity of host 'iOSIP (iOSIP)' can't be established.
RSA key fingerprint is 75:98:9a:05:a3:27:2d:23:08:d3:ee:f4:d1:28:ba:1a.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'iOSIP' (RSA) to the list of known hosts.
root@iOSIP's password:
authorized_keys
100% 408 0.4KB/s 00:00
ssh into iOS again to see if any passwords are required. Now, “make package install”
becomes “one time configuration, one click installation”, yay!
² Clean
Theos provides a convenient command “make clean” to clean our project. It indeed excutes
“rm -rf ./obj” and “rm -rf “/Users/snakeninny/Code/iosre/_”” in turn, thereby removes
folders generated by “make” and “make package”. Of course, you can further use “rm *.deb” to
remove all deb packages generated by “make package”.
2. Edit Tweak.xm
The edited Tweak.xm looks like this:
%hook SpringBoard
- (void)applicationDidFinishLaunching:(id)application
{
%orig;
%end
include theos/makefiles/common.mk
TWEAK_NAME = iOSREGreetings
68
iOSREGreetings_FILES = Tweak.xm
iOSREGreetings_FRAMEWORKS = UIKit
include $(THEOS_MAKE_PATH)/tweak.mk
after-install::
install.exec "killall -9 SpringBoard"
Yes, with just some tiny modifications, the behaviors of Apps are changed. Now, iOS is
69
opening its long closed door to us… The common scenarios of Theos and Logos are mostly
covered in this section, and for a more thorough introduction, please refer to
http://iphonedevwiki.net/index.php/Theos and http://iphonedevwiki.net/index.php/Logos.
Because of Theos, it has never been easier to modify a closed-source App. As we have
already mentioned, with the increase of App sizes, class-dump produces a greater amount of
headers. It has became much more difficult to locate our targets than pure coding. Facing
thousands lines of code, if there are no other tools to aid our analysis, reverse engineering would
be a nightmare. Now, it’s showtime of these auxiliary analysis tools.
3.3 Reveal
Figure 3- 15 Reveal
Reveal, as shown in figure 3-15, is a UI analysis tool by ITTY BITTY, enabling us to see the
view hierarchy of an App intuitively. The official purpose of Reveal is to “See your application’s
view hierarchy at runtime with advanced 2D and 3D visualisations”, but as reverse engineers,
seeing our own Apps’ view hierarchies is obviously not enough, we should be able to see other
Apps’ view hierarchies. Figure 3-16 shows the effect of seeing AppStore’s view hierarchy using
Reveal.
70
Figure 3-16 See the view hierarchy of AppStore
On the left side of Reveal, the UI layout of AppStore is presented as a tree, when choosing a
control object, the corresponding UI element will be marked on the right side of Reveal in real
time. At the same time, Reveal also parses the class name of this control object, as shown in
figure 3-16, the class name of the selected object is SKUIAttributedStringView. To analyze the
view hierarchies of other’s Apps, we need to make some configurations in Reveal.
71
Figure 3-17 Reveal Loader
Remarkably, when installing Reveal Loader, it will download a necessary file libReveal.dylib
from Reveal’s official website automatically. If the network condition is not good, this file may
not be downloaded successfully, and Reveal Loader is not fault tolerant to download failures. As
a result, Cydia may stuck for a long time and stop responding. Therefore, after download
completes, you’d better check whether there is a “RHRevealLoader” folder under the iOS
directory “/Library/”.
FunMaker-5:~ root# ls -l /Library/ | grep RHRevealLoader
drwxr-xr-x 2 root admin 102 Dec 6 11:10 RHRevealLoader
Then open Reveal, click “Help” menu, choose “Show Reveal Library in Finder”, as shown
in figure 3-18.
72
Figure 3- 18 Show Reveal Library in Finder
Figure 3- 19 libReveal.dylib
73
Figure 3- 20 Reveal Loader
Click “Enabled Applications” to enter the configuration view. Turn on the switch of the App
you want to analyze. Here we’ve turned on AppStore and Calculator’s switches, as shown in
figure 3-22.
74
Figure 3-22 configuration of Reveal Loader
That’s it. The configuration of Reveal Loader is simple and straightforward, isn’t it?
75
Figure 3-23 View hierarchy of Calculator
Reveal is not complicate and quite user-friendly. But in iOS reverse engineering, analysis on
UI is not enough, Apps’ inner implementations under the hood are our final goals. From part 3
of this book, we will use recursiveDescription function, which is the “command line” version of
Reveal, together with Cycript to find the corresponding code snippets of UI, then you will know
the real power of iOS reverse engineering.
3.4 IDA
76
Figure 3-24 Official website of IDA
You can click “OK” or wait for a few seconds to close the window, after that you will see
the main screen of IDA, as shown in figure 3-26.
77
Figure 3-26 Main screen of IDA
In this screen, you don’t have to search for “Open File” in the menu and locate the file to be
disassembled folder by folder, but just drag the target file to the gray zone with the placeholder
“Drag a file here to disassemble it”. After opening the file, there is still something to be
configured, as shown in figure 3-27.
78
Figure 3-27 Initial configurations
There’s one thing to mention: For a fat binary (which refers to the binary that contains
different instruction sets for the purpose of being compatible with different CPU architectures),
the white frame in figure 3-27 will list several Mach-O files. I suggest you read table 4-1 to find
the ARM type of your device. For example, my iPhone 5 corresponds to ARMv7S. If the ARM
type of your device is not in the white frame, you should choose the backward compatible one,
i.e. for ARMv7S devices, choose ARMv7S if there is ARMv7S in the list, otherwise choose
ARMv7. This selection method handles 99% of all cases, if you happen to be the 1%, please
come to http://bbs.iosre.com, we’ll solve the problem together.
Here, I’ve chosen ARMv7S, then click “OK”. Several windows will popup, just click “YES”
or “OK” to close them, as shown in figure 3-28 and 3-29.
79
Figure 3-28 IDA launch option
Since we cannot save our configurations in the evaluation version of IDA, checking the box
“Don’t display this message again” doesn’t work at all, it will still show in the next launch.
After clicking all the “OK” and “YES” buttons, the dazzling main screen shows up as in
figure 3-30.
80
Figure 3-30 IDA main screen
When entering the screen in figure 3-30, you will see the progress bar at the top loading, the
output window at the bottom printing the analysis progress. When the main color of the
progress bar changes to blue, and the output window shows the message “The initial
autoanalysis has been finished”, it indicates the initial analysis is completed.
At the beginning stage, IDA is mainly used for static analysis, the output window is quite
useless, we can close it for now.
Now that there are two major windows, on the left is “Functions window” as shown in
figure 3-31, on the right is “Main window” as shown in figure 3-32. Now, let’s take a look at
them one by one.
81
Figure 3-31 Functions window
• Functions window
As its name indicates, this window shows all functions (More accurately, Objective-C
functions should be called methods, but we’re referring them to functions hereafter), double
click one function name, the main window will show its implementation. When click “Search”
menu of Function Window, a submenu will show up as figure 3-33.
82
Figure 3-33 Search functions
Choose “Search…”, then type in what you want to search as shown in figure 3-34, to search
for your specified string in all function names. When the string appears in several function
names, you can click “Search again” to go through all of them. Of course, all above operations
can be done by shortcuts.
The method names in functions window are the same as names exported by class-dump.
Besides Objective-C methods, IDA lists all subroutines that we cannot get with class-dump. All
class-dump contents are method names of Objective-C, it’s easy to learn and read for beginners;
the names of subroutines are just combinations of “sub_” and addresses, they don’t have any
literal meaning, hence are hard to learn and read, freaking many rookies out. However, low-
level iOS is implemented in C and C++, which generate subroutines rather than Objective-C
methods. In this situation, class-dump is entirely defeated, our only choices are tools like IDA. If
we want to go deeper into iOS, we must get familiar with IDA.
• Main window
Most iOS developers who have never used IDA before are shocked by the “delirious”
contents presented by main window. It seems a real mess for all beginners; some of them may
83
close IDA immediately, and never open it again. This perplexed feeling is similar to the first time
when you write code. In fact, it is like every project needs a main function, in iOS reverse
engineering, we also need to specify the entry function that we are interested in. Double click
this entry function in function window, main window will show the function body, then select
main window and press space key, the main window will become much clearer and more
readable as shown in figure 3-35.
There are 2 display modes in main window, i.e. graph view and text view, which can be
switched by space key. Graph view focuses on the logics; you can use control button and mouse
wheel on it to zoom in and out. Graph view provides intuitive visualization of the relationship
among different subroutines. Execution flows of different subroutines are presented by lines
with arrows. When there’s a conditional branch, subroutine that meets the condition will be
connected with green line, otherwise with red line; for an unconditional branch, the next
subroutine will be connected with blue line. For example, in figure 3-36, when the execution
flow comes to the end of loc_1C758, it judges whether R0 is equal to 0, if R0 != 0, the condition
of BNE is satisfied, it will branch to the right, otherwise it will branch to the left. This is one
difficult point of IDA; it will be explained again and again in the following examples.
84
Figure 3- 36 Branches in IDA
Careful readers may have noticed that the fonts of IDA are colorful. In fact, different colors
have different meanings, as shown in figure 3-37.
When we choose a symbol, all the same symbols will be highlighted in yellow, making it
convenient for us to track this symbol, as shown in figure 3-38.
Double click a symbol to see its implementation as shown in figure 3-35. Right click a
symbol to display a menu shown in figure 3-39.
85
Figure 3-39 Right click on a symbol
Among the menu options, there is a very frequently used function “Jump to xref to
operand…” with the shortcut X (meaning “cross”), click this option, all information explicitly
cross referenced to this symbol will be displayed as shown in figure 3-40.
If you think this way is not straightforward and clear enough, yet prefer graph view, you
can choose option “Xrefs graph to…”. However, if this symbol is cross-referenced too much, the
86
graph view becomes a mess, just like figure 3-41 shows.
In figure 3-41, the irregular patterns in black are constructed by lines; lines are melting
together on both sides. So we know the symbol _objc_msgSendSuper2_stret is cross-referenced
many times.
Relatively, if we choose “Xrefs graph from...” , it will show all symbols cross referenced by
the symbol you choose, as shown in figure 3-42.
87
Figure 3-43 Tracking the source of external symbols
In most cases, when we discover an interesting symbol, we want to find every related clue.
One clumsy but effective way is to select main window and click “Search” on the menu bar. A
submenu is shown like figure 3-44.
88
Figure 3-45 Text search
There’re other searching options available, you can check them out according to your
situations. Then check “Find all occurences” and click “OK”. IDA will search the whole binary
and show all the matching strings.
Graph view provides us with so many features; I’ve only introduced some common ones,
proficiency in them ensures deeper research. Graph view is simple and clear, it’s easy to see the
logics between different subroutines. As newbies, we mostly use graph view. When using LLDB
for debugging, we’ll switch to text view to get the address of a symbol listed on the left side, as
shown in figure 3-46.
It should be noted that one bug of IDA will cause the incomplete display of a subroutine at
the end of its graph view (For example, one subroutine has 100 lines of instructions but only
displays 80 lines). When you are suspicious about instructions in graph view, just switch to text
view to see whether some code is missing. This bug occurs by very little chance, if you happen
to encounter it unfortunately, welcome to http://bbs.iosre.com for discussion and solution.
89
3.4.3 An analysis example of IDA
Having introduced so many features of IDA, now I will use a simple example to show the
real power of IDA. Jailbreak users know, Cydia will suggest us “Restart SpringBoard” when a
tweak finishes installation. How does Cydia perform a respring? Please go through section 3.5
quickly and copy “/System/Library/CoreServices/SpringBoard.app/SpringBoard” from iOS to
OSX using iFunBox, then open it with IDA. When the initial analysis is finished, search
“relaunchSpringBoard” in function window, double click it to jump to its function body, as
shown in figure 3-47.
As we can see in figure 3-47, this method’s implementation is simple and clear. According to
the execution flow from top to bottom, firstly it calls beginIgnoringInteractionEvents to ignore
90
all user interaction events; secondly, it calls hideSpringBoardStatusBar to hide the status bar in
SpringBoard, then it executes two subroutines, they are sub_35D2C and sub_350B8. Now,
double click sub_35D2C to jump to its implementation, as shown in figure 3-48.
Figure 3- 48 sub_35D2C
In figure 3-48, “log” appears a lot: First “initialize”, then check whether something is
“enabled”, at last “log” something. From those keywords, we can guess that this subroutine is
used for logging respring related operations, it has nothing to do with the essential function of
respring. Click the blue back button of IDA menu bar (as shown in figure 3-49), or just press
ESC, to go back to the implementation of “relaunchSpringBoard” and continue our analysis.
We know from figure 3-50 that this subroutine is just preparing for calling sub_350C4.
Double click sub_350C4 to jump to its implementation, you will find the top half of sub_350C4
looks very similar to sub_35D2C as shown in figure 3-48, which only does some logging job. But
what’s different is that sub_350C4 additionally does something essential, as shown in figure 3-51.
92
Figure 3-51 sub_350C4
Now that we know little about assembly language, but from the literal meaning of these
keywords, it can be concluded that the function of this subroutine is to generate an event named
“TerminateApplicationGroup”, specify sub_351F8 to be the handler of it, and then append this
event to a queue for sequential execution, thus close all Apps by this way. This makes sense:
Before a mall closes, we need to close all its shops; before respring, we need to close all Apps.
Let’s go to sub_351F8 to see its implementation, as shown in figure 3-52.
93
Figure 3-52 sub_351F8
94
3.5 iFunBox
iFunBox (as shown in figure 3-53) is an evergreen iOS file management tool on
Windows/OSX. In this book, we mainly make use of its file transfer feature. One thing to
mention is that we must install “Apple File Conduit 2” (or AFC2 for short, as shown in figure 3-
54) on iOS to browse the entire iOS file system, which is the prerequisite of the following
operations in this book.
95
Figure 3-54 Apple File Conduit 2
3.6 dyld_decache
After installing iFunBox and AFC2, most of you would be eager to start browsing the iOS
filesystem to explore the secrets hidden in iOS. But soon you’ll discover that there are no library
files under “/System/Library/Frameworks/” or “/System/Library/PrivateFrameworks/”.
What’s going on?
From iOS 3.1, many library files including frameworks are combined into a big cache, which
is located in “/System/Library/Caches/com.apple.dyld/ dyld_shared_cache_armx” (i.e.
dyld_shared_cache_armv7, dyld_shared_cache_armv7s or dyld_shared_cache_arm64). We can
use dyld_decache by KennyTM to extract the separate binaries from this cache, which
guarantees that the files we analyze are right from iOS, avoiding the possibility that static and
dynamic analysis targets mismatch each other. More about this cache, please refer to DHowett’s
blog at http://blog.howett.net/2009/09/cache-or-check/.
Before using dyld_decache, please use iFunBox (not scp) to copy
“/System/Library/Caches/com.apple.dyld/dyld_shared_cache_armx” from iOS to OSX, then
download dyld_decache from
https://github.com/downloads/kennytm/Miscellaneous/dyld_decache[v0.1c].bz2 and grant
execute permission to the decompressed executable:
96
snakeninnysiMac:~ snakeninny$ chmod +x /path/to/dyld_decache\[v0.1c\]
3.7 Conclusion
This chapter focuses on 4 tools, which are class-dump, Theos, Reveal and IDA. Familiarity
with them is the prerequisite of iOS reverse engineering.
97
4
iOS toolkit
In chapter 3, we’ve introduced the OSX toolkit for iOS reverse engineering. To get our
work done, we still need to install and configure several tools on iOS to combine both
platforms. All operations in this chapter are finished on iPhone 5, iOS 8.1, if you encounter any
problems, please talk to us on http://bbs.iosre.com.
4.1 CydiaSubstrate
Figure 4- 1 Logo of CydiaSubstrate
CydiaSubstrate (as shown in figure 4-1) is the infrastructure of most tweaks. It consists of
MobileHooker, MobileLoader and Safe mode.
4.1.1 MobileHooker
MobileHooker is used to replace system calls, or namely, hook. There are two major
functions:
void MSHookMessageEx(Class class, SEL selector, IMP replacement, IMP *result);
void MSHookFunction(void* function, void* replacement, void** p_original);
Figure 4- 2 Normal execution flow of a process
As shown in figure 4-2, a process executes some instructions, then calls function A, and
afterward executes the remaining instructions. If we hook function A and replace it with
function B, then this process’ execution flow changes to figure 4-3.
99
Figure 4- 3 Replace Function A with B
We can see in figure 4-3 that this process executes some instructions at first, but then calls
function B at where it’s supposed to call function A, with function A stored elsewhere. Inside
function B, it’s up to you whether and when to call function A. After function B finishes
execution, the process will continue to execute the remaining instructions.
There’s one more thing to notice. MSHookFunction has a requirement on the length of the
function it hooks, the total length of all its instructions must be bigger than 8 bytes (This
number is not officially acknowledged). So here comes the question, how to hook these less-
than-8-byte short functions?
One workaround is hooking functions inside the short functions. The reason why a function
is short is often because it calls other functions and they’re doing the actual job. Some of the
other functions are long enough to be hooked, so we can choose these functions to be
MSHookFunction’s targets, then do some logical judgements in “replacement” to tell if the
short function is the caller. If we can make sure the short function is calling the “replacement”,
then we can write our modification to the short function right inside “replacement”.
If you are still confused about MSHookFunction, here is a simple example. To be honest,
this example contains too much low-level knowledge, hence is quite hard for beginners to
understand. Don’t worry if you happen to be a newbie, just skip to section 4.1.2. When you
encounter a similar situation later in practice, review this section and you’ll know what we’re
100
talking about. Anyway, welcome to http://bbs.iosre.com for further discussion.
Follow me:
class CPPClass
{
public:
void CPPFunction(const char *);
};
101
{
u_int32_t randomNumber;
if (i % 3 == 0) randomNumber = arc4random_uniform(i);
NSProcessInfo *processInfo = [NSProcessInfo processInfo];
NSString *hostName = processInfo.hostName;
int pid = processInfo.processIdentifier;
NSString *globallyUniqueString = processInfo.globallyUniqueString;
NSString *processName = processInfo.processName;
NSArray *junks = @[hostName, globallyUniqueString, processName];
NSString *junk = @"";
for (int j = 0; j < pid; j++)
{
if (pid % 6 == 0) junk = junks[j % 3];
}
if (i % 68 == 1) NSLog(@"Junk: %@", junk);
}
NSLog(@"iOSRE: CFunction: %s", arg0);
}
@implementation RootViewController
- (void)loadView {
self.view = [[[UIView alloc] initWithFrame:[[UIScreen mainScreen]
applicationFrame]] autorelease];
self.view.backgroundColor = [UIColor redColor];
}
- (void)viewDidLoad
{
[super viewDidLoad];
CPPClass cppClass;
cppClass.CPPFunction("This is a C++ function!");
CFunction("This is a C function!");
ShortCFunction("This is a short C function!");
}
@end
APPLICATION_NAME = iOSRETargetApp
iOSRETargetApp_FILES = main.m iOSRETargetAppApplication.mm RootViewController.mm
iOSRETargetApp_FRAMEWORKS = UIKit CoreGraphics
include $(THEOS_MAKE_PATH)/application.mk
after-install::
install.exec "su mobile -c uicache"
In the above code, “su mobile - C uicache” is used to refresh the UI cache of SpringBoard so
that iOSRETargetApp’s icon can be shown on SpringBoard. Run “make package install” in
Terminal to install this tweak on the device. Launch iOSRETargetApp, ssh into iOS after the red
background shows, and see whether it outputs as expected:
FunMaker-5:~ root# grep iOSRE: /var/log/syslog
Nov 18 11:13:34 FunMaker-5 iOSRETargetApp[5072]: iOSRE: CPPFunction: This is a C++
function!
Nov 18 11:13:34 FunMaker-5 iOSRETargetApp[5072]: iOSRE: CFunction: This is a C function!
Nov 18 11:13:35 FunMaker-5 iOSRETargetApp[5072]: iOSRE: CPPFunction: This is a short C
function!
%ctor
{
@autoreleasepool
{
MSImageRef image =
MSGetImageByName("/Applications/iOSRETargetApp.app/iOSRETargetApp");
void *__ZN8CPPClass11CPPFunctionEPKc = MSFindSymbol(image,
"__ZN8CPPClass11CPPFunctionEPKc");
if (__ZN8CPPClass11CPPFunctionEPKc) NSLog(@"iOSRE: Found CPPFunction!");
MSHookFunction((void *)__ZN8CPPClass11CPPFunctionEPKc, (void
*)&new__ZN8CPPClass11CPPFunctionEPKc, (void **)&old__ZN8CPPClass11CPPFunctionEPKc);
104
“symbol table”. “symbol” is the name of the function, according to which the process locates the
function’s address in memory and then jumps there to execute it.
Imagine such a scenario: Your App calls a lookup function in a dylib to query information
on your server. If another App gets to know the symbol of “lookup”, then it can import the
dylib, and call the function as it wishes, causing great consumption of your server resources.
To avoid this, symbols are divided into 2 types, i.e. public symbols and private symbols
(Besides, there are stripped symbols, but they have little to do with this chapter. If you are
interested in stripped symbols, please visit the following reference links or google by
yourselves). Private symbols are not property of yours, you can not make use of them as you
wish. That’s to say, MSHookFunction will fail on private symbols without further manipulation.
So saurik provides the MSFindSymbol function to access private symbols. If the concept of
symbol is still beyond comprehension, just keep the following code pattern in mind:
MSImageRef image =
MSGetImageByName("/path/to/binary/who/contains/the/implementation/of/symbol");
void *symbol = MSFindSymbol(image, "symbol");
The parameter of MSGetImageByName is “The full path of the binary which contains the
implementation of the function”. For example, the implementation of NSLog is in the
Foundation framework, so the parameter should be
“/System/Library/Frameworks/Foundation.framework/Foundation”. Get it?
You can refer to the official document at
http://www.cydiasubstrate.com/api/c/MSFindSymbol/ for a more detailed explanation of
MSFindSymbol. As for the types and definition of symbols, please read
http://msdn.microsoft.com/en-us/library/windows/hardware/ff553493(v=vs.85).Aspx and
http://en.wikibooks.org/wiki/Reverse_Engineering /Mac_OS_X#Symbols_Types.
Figure 4- 4 Functions window
Figure 4- 5 CPPClass::CPPFunction(char const*)
The underline prefixed string in line 4 is exactly the symbol we’re looking for. In the same
way, where _CFunction and _ShortCFunction come from is obviously shown in figure 4-6 and
figure 4-7.
106
Figure 4- 6 CFunction
Figure 4- 7 ShortCFunction
This approach of symbol locating applies to all kinds of symbols. In the beginning stage, we
suggest you keep in mind that a symbol and its corresponding function name are different, while
ignore the hows and whys. During your whole process of studying reverse engineering, the
concept of symbol will imperceptibly goes into your knowledge system, thus there is no need to
push it for now.
returnType (*old_symbol)(args);
returnType new_symbol(args)
{
// Whatever
}
107
void InitializeMSHookFunction(void) // This function is often called in %ctor i.e.
constructor
{
MSImageRef image =
MSGetImageByName("/path/to/binary/who/contains/the/implementation/of/symbol");
void *symbol = MSFindSymbol(image, "symbol");
if (symbol) MSHookFunction((void *)symbol, (void *)&new_ symbol, (void **)&old_
symbol);
else NSLog(@"Symbol not found!");
}
include theos/makefiles/common.mk
TWEAK_NAME = iOSREHookerTweak
iOSREHookerTweak_FILES = Tweak.xm
include $(THEOS_MAKE_PATH)/tweak.mk
after-install::
install.exec "killall -9 iOSRETargetApp"
Now please relaunch iOSRETargetApp and see if the output matches our expectation:
FunMaker-5:~ root# grep iOSRE: /var/log/syslog
Nov 18 11:29:14 FunMaker-5 iOSRETargetApp[5327]: iOSRE: Found CPPFunction!
Nov 18 11:29:14 FunMaker-5 iOSRETargetApp[5327]: iOSRE: Found CFunction!
Nov 18 11:29:14 FunMaker-5 iOSRETargetApp[5327]: iOSRE: Found ShortCFunction!
Nov 18 11:29:14 FunMaker-5 iOSRETargetApp[5327]: iOSRE: CPPFunction: This is a hijacked
C++ function!
Nov 18 11:29:14 FunMaker-5 iOSRETargetApp[5327]: iOSRE: CFunction: This is a hijacked C
function!
Nov 18 11:29:14 FunMaker-5 iOSRETargetApp[5327]: iOSRE: CPPFunction: This is a hijacked
short C function from new__ZN8CPPClass11CPPFunctionEPKc!
It is worth mentioning that, we failed hooking the short function (i.e. ShortCFunction),
otherwise it would print “This is a hijacked short C function from new_ShortCFunction!”. But
we succeeded in hooking other functions (i.e. CPPClass::CPPFunction) inside the short
108
function. We could tell if the caller was ShortCFuncation by judging the callee’s argument, thus
indirectly hooked short function and met our needs. The introduction of MSHookFunction
above covers almost every situation a beginner may encounter. Since Theos only provides
encapsulation for MSHookMessageEx, thorough understanding of the use of MSHookFunction
is particularly important. If MSHookFunction still confuses you, get to us on
http://bbs.iosre.com.
4.1.2 MobileLoader
The role of MobileLoader is to load third-party dylibs. When iOS launches, launchd will
load MobileLoader into memory, then MobileLoader will call dlopen according to tweaks’ plist
filters to load dylibs under /Library/MobileSubstrate/DynamicLibraries/ into different
processes. The format of the plist filter here has been explained in details in the previous Theos
section, which saves my words here. For most rookie iOS reverse engineers, MobileLoader
works transparently, knowing the existence of it is enough.
109
Figure 4- 8 Safe mode
In safe mode, all third-party tweaks that base on CydiaSubstrate will be disabled for
troubleshooting. But safe mode can’t guarantee absolute safety, in many cases, devices fail to
boot because of problematic third-party dylibs. In this situation, you can perform a hard reboot
by pressing and holding the home and lock buttons, then completely disable CydiaSubstrate by
holding the volume “+” button. After iOS successfully reboots, you can start error checking.
When the problems are fixed, reboot iOS again to enable CydiaSubstrate.
110
4.2 Cycript
Figure 4- 9 Cycript
Cycript (As shown in figure 4-9) is a scripting language developed by saurik. You can view
Cycript as Objective-JavaScript. A lot of you may not be familiar with JavaScript, then
subconsciously think Cycript is very obscure. In fact, I, as a lazy learner, do not know JavaScript
either, so in a long time, I’ve ignored Cycript deliberately. It wasn’t until not long ago when I
was playing with MTerminal during my company’s boring meeting and tested a few Objective-
C methods in Cycript, then I had a new awareness of this simple yet powerful language. In fact,
for Objective-C programmers, scripting languages are not difficult to use, as long as we
overcome our fear of difficulty, we will be able to handle them very quickly, and Cycript is no
exception. Cycript has the convenience of scripting language, you can even write App in
Cycript, but saurik himself said, “This isn’t quite ‘ready for primetime’”. In my humble opinion,
the most practical usage of Cycript is testing private methods in an easy manner, possessing
both safety and efficiency. Therefore, this book will only use Cycript to test methods. For its
complete manual, please visit the official website http://www.cycript.org.
We can launch Cycript either in MTerminal or via ssh. Input “cycript” and it outputs “cy#”,
which indicates Cycript’s successful launch.
FunMaker-5:~ root# cycript
cy#
111
After that, you can start coding. Instead of writing Apps, we mainly use Cycript to test
methods, so we need to inject and run code in an existing process. Let’s exit Cycript by pressing
“control + D” for now. Generally speaking, which process to inject depends on what methods
we’re testing: Suppose the methods to be tested are from class A, and class A exists in process B,
then you should inject into process B to test the methods. Stop beating around the bush, let’s see
an example to make everything more straightforward.
If now we want to test the class method +sharedNumberFormatter in class
PhoneApplication to reconstruct its prototype, we have to inject into the process MobilePhone
because PhoneApplication only exists in MobilePhone; Similarly, for the instance method
[SBUIController lockFromSource:], we have to inject into SpringBoard; Naturally, for [NSString
length], we can inject into any process that imports Foundation.framework. Because most of the
methods we test are private, so the general rules are that if the methods you’re testing are from
a process, inject right into that process; If they’re from a lib, inject into the processes that import
this lib.
Testing methods via process injection is rather simple. Take SpringBoard for an example,
first we need to find out its process name or process ID (PID):
FunMaker-5:~ root# ps -e | grep SpringBoard
4567 ?? 0:27.45 /System/Library/CoreServices/SpringBoard.app/SpringBoard
4634 ttys000 0:00.01 grep SpringBoard
It’s easy to convert the above Objective-C code into Cycript code:
FunMaker-5:~ root# cycript -p SpringBoard
cy# alertView = [[UIAlertView alloc] initWithTitle:@"iOSRE" message:@"snakeninny"
delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]
#"<UIAlertView: 0x1700e580; frame = (0 0; 0 0); layer = <CALayer: 0x164146c0>>"
cy# [alertView show]
cy# [alertView release]
No need to declare the type of an object, no need to add a semicolon at the end of each line,
112
that’s Cycript. If a function has a return value, Cycript will print its memory address and
description in real time, which is very intuitive. After Cycript executes the above code, a popup
shows on SpringBoard, as shown in figure 4-10.
Figure 4- 10 Code execution in Cycript
If we already know the memory address of an object, we can use “#” operator to access the
object like this:
cy# [[UIAlertView alloc] initWithTitle:@"iOSRE" message:@"snakeninny" delegate:nil
cancelButtonTitle:@"OK" otherButtonTitles:nil]
#"<UIAlertView: 0x166b4fb0; frame = (0 0; 0 0); layer = <CALayer: 0x16615890>>"
cy# [#0x166b4fb0 show]
cy# [#0x166b4fb0 release]
If we know an object of a certain class exists in the current process but don’t know its
memory address, we cannot obtain the object with “#”. Under such circumstance, we can try
“choose” out:
cy# choose(SBScreenShotter)
[#"<SBScreenShotter: 0x166e0e20>"]
cy# choose(SBUIController)
[#"<SBUIController: 0x16184bf0>"]
“choose” a class, Cycript returns its objects. This command is so convenient that it doesn’t
succeed all the time. When it fails to get you a usable object, you’re on your own. We’ll talk
about how to get our target objects manually in chapter 6, please stay tuned.
When it comes to testing private methods, a combination of the above Cycript commands
113
would be enough. Let’s summarize the use of Cycript with an example of logging in to iMessage
with my Apple ID. First we need to get an instance of iMessage login controller:
FunMaker-5:~ root# cycript -p SpringBoard
cy# controller = [CNFRegController controllerForServiceType:1]
#"<CNFRegController: 0x166401e0>"
Figure 4- 11 Log in iMessage
This method returns a logged in IMAccount, i.e my iMessage account. Then select the
addresses for sending and receiving iMessages:
cy# [controller setAliases:@[@"[email protected]"] onAccount:#0x166e7b30]
1
114
Figure 4- 12 Select iMessage addresses
The return value indicates our correctness by far. Finally, let’s check if my iMessage account
is ready to rock!
cy# [#0x166e7b30 CNFRegSignInComplete]
1
115
• Launch the program under the conditions you specify;
• Modify the program when it stops, and observe the modification of its execution flow.
LLDB is a command line tool, it does not have a graphical interface. Its mass output in
Terminal scares off beginners easily, but once you master the basic commands of LLDB, you’ll
be surprised by its formidable combination with IDA. LLDB runs in OSX, so to debug iOS, we
need another tool’s assistance on iOS, which is debugserver.
Name ARM
iPhone 4s armv7
iPhone 5 armv7s
iPhone 5c armv7s
iPhone 5s arm64
iPhone 6 Plus arm64
iPhone 6 arm64
116
iPad 2 armv7
iPad mini armv7
The New iPad armv7
iPad with Retina display armv7s
iPad Air arm64
iPad Air 2 arm64
iPad mini with Retina display arm64
iPad mini 3 arm64
iPod touch 5 armv7
Table 4-1 iOS 8 Compatible devices
My device is iPhone 5, its matching ARM type is armv7s. Copy the raw debugserver from
iOS to “/Users/snakeninny/” on OSX.
snakeninnysiMac:~ snakeninny$ scp root@iOSIP:/Developer/usr/bin/debugserver
~/debugserver
Note that you need to change “armv7s” here to the corresponding ARM type of your
device.
One thing to clarify, the reason we put the modified debugserver under “/usr/bin/” instead
of overriding the original one is because, first, the original debugserver is not writable, we just
cannot override it; Second, we don’t need to input full paths to execute commands under
“/usr/bin/”, just run “debugserver” wherever you want, and debugserver is ready to roll out.
debugserver will launch the specific executable and open the specific port, then wait for
LLDB’s connection from IP.
debugserver IP:port -a "ProcessName"
debugserver will attach to process with the name “ProcessName” and open the specific
port, then wait for LLDB’s connection from IP.
For example:
FunMaker-5:~ root# debugserver -x backboard *:1234 /Applications/MobileSMS.app/MobileSMS
debugserver-@(#)PROGRAM:debugserver PROJECT:debugserver-320.2.89
for armv7.
Listening to port 1234 for a connection from *...
The above command will launch MobileSMS and open port 1234, then wait for LLDB’s
connection from any IP. And for the following command:
FunMaker-5:~ root# debugserver 192.168.1.6:1234 -a "MobileSMS"
debugserver-@(#)PROGRAM:debugserver PROJECT:debugserver-320.2.89
for armv7.
Attaching to process MobileNotes...
Listening to port 1234 for a connection from 192.168.1.6...
debugserver will attach to MobileSMS and open port 1234, then wait for LLDB’s connection
from 192.168.1.6.
If something goes wrong when executing the above commands, such as:
FunMaker-5:~ root# debugserver *:1234 -a "MobileSMS"
dyld: Library not loaded:
/Developer/Library/PrivateFrameworks/ARMDisassembler.framework/ARMDisassembler
Referenced from: /usr/bin/debugserver
Reason: image not found
Trace/BPT trap: 5
118
menu. You can fix the issue by re-enabling development mode on this device.
When you exit debugserver, the process being debugged also exits. The configuration of
debugserver is over for now, the following operation are performed on LLDB.
Then the old LLDB will launch and you can connect it to the waiting debugserver:
(lldb) process connect connect://iOSIP:1234
Process 790987 stopped
* thread #1: tid = 0xc11cb, 0x3995b4f0 libsystem_kernel.dylib`mach_msg_trap + 20, queue
= 'com.apple.main-thread, stop reason = signal SIGSTOP
frame #0: 0x3995b4f0 libsystem_kernel.dylib`mach_msg_trap + 20
libsystem_kernel.dylib`mach_msg_trap + 20:
-> 0x3995b4f0: pop {r4, r5, r6, r8}
0x3995b4f4: bx lr
libsystem_kernel.dylib`mach_msg_overwrite_trap:
0x3995b4f8: mov r12, sp
0x3995b4fc: push {r4, r5, r6, r8}
Note, the execution of “process connect connect://iOSIP:1234” will take a rather long time
(approximately more than 3 minutes in a WiFi environment) to connect to debugserver, please
be patient. In section 4.6, there will be an introduction to connecting to debugserver through
USB, which will save a lot of time. When the process is stopped by debugserver, we can start
debugging. Let’s have a look at the commonly used commands in LLDB.
1. image list
“image list” is similar to “info shared” in GDB, which is used to list the main executable and
all dependent libraries (hereinafter referred to as images) in the debugged process. Because of
ASLR (Address Space Layout Randomization, see http://theiphonewiki.com/wiki/ASLR),
119
every time the process launches, a random offset will be added to the starting address of all
images in that process, making their virtual memory addresses hard to predict.
For example, suppose there is an image B in process A, and image B is 100 bytes in size.
When process A launches for the 1st time, image B may be loaded into virtual memory at 0x00
to 0x64; For the 2nd time, image B may be loaded into 0x10 to 0x74, and 0x60 to 0xC4 for the
3rd time. That is to say, although image B’s size stays 100 bytes, every launch changes the
starting address, which happens to be a key value in our following operations. Then comes the
question, how do we get this key value?
The answer is”image list -o -f”. After LLDB has connected to debugserver, run “image list -o
-f” to view its output:
(lldb) image list -o -f
[ 0] 0x000cf000
/private/var/db/stash/_.29LMeZ/Applications/SMSNinja.app/SMSNinja(0x00000000000d3000)
[ 1] 0x0021a000 /Library/MobileSubstrate/MobileSubstrate.dylib(0x000000000021a000)
[ 2] 0x01645000 /usr/lib/libobjc.A.dylib(0x00000000307b5000)
[ 3] 0x01645000
/System/Library/Frameworks/Foundation.framework/Foundation(0x0000000023c4f000)
[ 4] 0x01645000
/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation(0x0000000022f0b000)
[ 5] 0x01645000 /System/Library/Frameworks/UIKit.framework/UIKit(0x00000000264c1000)
[ 6] 0x01645000
/System/Library/Frameworks/CoreGraphics.framework/CoreGraphics(0x0000000023238000)
……
[235] 0x01645000
/System/Library/Frameworks/CoreGraphics.framework/Resources/libCGXType.A.dylib(0x0000000
0233a2000)
[236] 0x0008a000 /usr/lib/dyld(0x000000001fe8a000)
In the above output, the 1st column, [X], is the sequence number of the image; the 2nd
column is the image’s random offset generated by ASLR (hereinafter referred to as the ASLR
offset); the 3rd column is the full path of this image, the content in brackets is the original
starting address plus the ASLR offset. Do all these offsets and addresses confuse you? Take it
easy, hopefully you’ll sort it through after an example.
Suppose the virtual memory is a shooting range with 1000 target positions. You can regard
the images in a process as targets and now there are 600 of them. All these targets are uniformly
arranged in a row with target 1 in position 1, target 2 in position 2, target 600 in position 600,
etc. And positions 601 to 1000 are all empty. You can see the layout in figure 4-13 (The number
at the top is the target position number, and the target number is at the bottom).
120
Figure 4- 13 Shooting range (1)
The images’ starting addresses in virtual memory are like the target positions of the 600
targets, which are named image base addresses in terminology. Now the owner of this shooting
range thinks the previous targets are arranged rashly, shooters will hit all bulls’ eyes as soon he
gets familiar with the arrangement. So the owner relocates all these targets randomly. After
relocation, target 1 is placed in position 5, target 2 is placed in position 6, target 3 is placed in
position 8, target 4 is placed in position 13, target 5 is placed in position 15...... Target 600 is
placed in position 886, as shown in figure 4-14.
Figure 4- 14 Shooting range (2)
That’s to say, the offsets for target 1, 2, 3, 4, 5 and 600 are 4, 4, 5, 9, 10 and 286 respectively.
This random (ASLR) offset greatly increases the shooting difficulty. For target 1, it used to be at
position 1, and it is at position 5 for now, so the offset is 4, i.e.
image base address with offset = image base address without offset + ASLR offset
Back to the reverse engineering scene, let’s take the 4th image (i.e. Foundation) in the
output of “image list -o -f” as an example, its ASLR offset is 0x1645000, its image base address
with offset is 0x23c4f000, so according to the above formula, its image base address without
offset is 0x23c4f000 - 0x1645000 = 0x2260A000.
You may wonder, where does 0x2260A000 come from? Drag and drop Foundation’s binary
into IDA, after the initial analysis, IDA looks like figure 4-15.
121
Figure 4- 15 Analyze Foundation in IDA
Scroll to the top of IDA View-A, do you see “HEADER:2260A000” in the first line? This is
the origin of 0x2260A000.
Now that we’ve known “base address” means “starting address”, let’s talk about another
concept which is similar to “image base address”, i.e. “symbol base address”. Return to IDA and
search for “NSLog” in the Functions window, and then jump to its implementation, as shown in
figure 4-16.
Figure 4- 16 NSLog
Because the base address of Foundation is a known number, and NSLog is in a fixed position
inside Foundation, we can get the base address of NSLog according to the following formula:
base address of NSLog = relative address of NSLog in Foundation + base address of Foundation
What does “relative address of NSLog in Foundation” mean? Let’s go back to figure 4-16
and find the first instruction of NSLog, i.e. “SUB SP, SP, #0xC”. On the left, do you see the
number 0x2261AB94? This the “address of NSLog in Foundation”. Subtract Foundation’s image
base address without offset, i.e. 0x2260A000 from it, we get the “relative address of NSLog in
Foundation”, i.e. 0x10B94.
122
Hence, the base address of NSLog is 0x10B94 + 0x23c4f000 = 0x23C5FB94. I guess some of
you have already noticed that the formula
image base address with offset = image base address without offset + ASLR offset
Naturally, symbol base address is the base address of the first instruction of the symbol’s
corresponding function.
In the following content, base addresses with offset will be frequently used. Make sure you
understand all concepts in this section then keep in mind: Base address without offset can be
viewed in IDA, ASLR offset can be viewed in LLDB, add them together we get base address
with offset. As for where in IDA and LLDB to search for the values, I bet you’ll get it after
thoroughly reading this section.
2. breakpoint
“breakpoint” is similar to “break” in GDB, it’s used to set breakpoints. In reverse
engineering, we usually set breakpoints like these:
b function
Or
br s –a address
Or
br s –a ‘ASLROffset+address’
The former command is to set a breakpoint at the beginning of a function, for instance:
(lldb) b NSLog
Breakpoint 2: where = Foundation`NSLog, address = 0x23c5fb94
The latter two commands are to set a breakpoint at a specific address, for instance:
(lldb) br s -a 0xCCCCC
Breakpoint 5: where = SpringBoard`___lldb_unnamed_function303$$SpringBoard, address =
0x000ccccc
123
(lldb) br s -a '0x6+0x9'
Breakpoint 6: address = 0x0000000f
Note that the “X” in the output “Breakpoint X:” is an integer id of that breakpoint, and we
will use this number soon. When the process stops at a breakpoint, the line of code holding the
breakpoint hasn’t been executed yet.
In reverse engineering, we’ll be debugging assembly code, so in most cases we’ll be setting
breakpoint on a specific assembly instruction instead of a function. To set a breakpoint on an
assembly instruction, we have to know its base address with offset, which we have already
explained in details. Now let’s take -[SpringBoard _menuButtonDown:] for an example and set a
breakpoint on the first instruction as a demonstration.
Figure 4- 17 [SpringBoard _menuButtonDown:]
As we can see, the base address without offset of the first instruction “PUSH {R4-R7, LR}” is
0x17730.
124
Then connect to debugserver with LLDB on OSX, and find the ASLR offset:
snakeninnysiMac:~ snakeninny$ /Applications/OldXcode.app/Contents/Developer/usr/bin/lldb
(lldb) process connect connect://iOSIP:1234
Process 93770 stopped
* thread #1: tid = 0x16e4a, 0x30dee4f0 libsystem_kernel.dylib`mach_msg_trap + 20, queue
= 'com.apple.main-thread, stop reason = signal SIGSTOP
frame #0: 0x30dee4f0 libsystem_kernel.dylib`mach_msg_trap + 20
libsystem_kernel.dylib`mach_msg_trap + 20:
-> 0x30dee4f0: pop {r4, r5, r6, r8}
0x30dee4f4: bx lr
libsystem_kernel.dylib`mach_msg_overwrite_trap:
0x30dee4f8: mov r12, sp
0x30dee4fc: push {r4, r5, r6, r8}
(lldb) image list -o -f
[ 0] 0x000b5000
/System/Library/CoreServices/SpringBoard.app/SpringBoard(0x00000000000b9000)
[ 1] 0x006ea000 /Library/MobileSubstrate/MobileSubstrate.dylib(0x00000000006ea000)
[ 2] 0x01645000
/System/Library/PrivateFrameworks/StoreServices.framework/StoreServices(0x000000002ca700
00)
[ 3] 0x01645000
/System/Library/PrivateFrameworks/AirTraffic.framework/AirTraffic(0x0000000027783000)
……
[419] 0x00041000 /usr/lib/dyld(0x000000001fe41000)
(lldb) c
Process 93770 resuming
When the process stops, you can use “c” command to “continue” (running) the process.
125
Compared to GDB, a significant improvement in LLDB is that you can enter commands while
the process is running. But be careful, some processes (such as SpringBoard) will automatically
relaunch because of timeout after stopping for a period of time. For this kind of processes, you
should try to keep it running to avoid unexpected automatic relaunching.
You can also use commands like “br dis”, “br en” and “br del” to disable, enable and delete
breakpoints. The command to disable all breakpoints is as follows:
(lldb) br dis
All breakpoints disabled. (2 breakpoints)
After executing the above command, LLDB will ask for a series of commands, ending with
“DONE”.
Enter your debugger command(s). Type 'DONE' to end.
> po [$r0 class]
> p (char *)$r1
> c
> DONE
Here we’ve input 3 commands, once breakpoint 1 is hit, LLDB will execute them one by
one:
(lldb) c
126
Process 97048 resuming
__NSArrayM
(char *) $11 = 0x26c6bbc3 "count"
Process 97048 resuming
Command #3 'c' continued the target.
“br com add” is often used to automatically obverse the changes in the context of a
breakpoint when it is hit, which often implies valuable reverse engineering clues. We’ll see how
to use this command in the latter half of this book.
3. print
Thanks to “print” command, “inspecting the internal status of a program when it stops” is
possible. As its name implies, this command can print the value of a register, variable,
expression, etc. Again, let’s illustrate the use of “print” with “-[SpringBoard
_menuButtonDown:]”, as shown in figure 4-18.
Figure 4- 18 [SpringBoard _menuButtonDown:]
The base address with offset of “MOVS R6, #0” is known to be 0xE37DE, let’s set a
breakpoint on it and print R6’s value when we hit the breakpoint:
(lldb) br s -a 0xE37DE
Breakpoint 2: where = SpringBoard`___lldb_unnamed_function299$$SpringBoard + 174,
address = 0x000e37de
Process 99787 stopped
* thread #1: tid = 0x185cb, 0x000e37de
SpringBoard`___lldb_unnamed_function299$$SpringBoard + 174, queue = 'com.apple.main-
thread, stop reason = breakpoint 2.1
frame #0: 0x000e37de SpringBoard`___lldb_unnamed_function299$$SpringBoard + 174
SpringBoard`___lldb_unnamed_function299$$SpringBoard + 174:
-> 0xe37de: movs r6, #0
0xe37e0: movt r0, #75
0xe37e4: movs r1, #1
0xe37e6: add r0, pc
(lldb) p $r6
(unsigned int) $1 = 364526080
After this instruction is executed, R6 should be set to 0. Input “ni” to execute this instruction
127
and reprint the value of R6:
(lldb) ni
Process 99787 stopped
* thread #1: tid = 0x185cb, 0x000e37e0
SpringBoard`___lldb_unnamed_function299$$SpringBoard + 176, queue = 'com.apple.main-
thread, stop reason = instruction step over
frame #0: 0x000e37e0 SpringBoard`___lldb_unnamed_function299$$SpringBoard + 176
SpringBoard`___lldb_unnamed_function299$$SpringBoard + 176:
-> 0xe37e0: movt r0, #75
0xe37e4: movs r1, #1
0xe37e6: add r0, pc
0xe37e8: cmp r5, #0
(lldb) p $r6
(unsigned int) $2 = 0
(lldb) c
Process 99787 resuming
Figure 4- 19 objc_msgSend([SBTelephonyManager class], @selector(sharedTelephonyManager))
The address with offset of “BLX _objc_msgSend” is known to be 0xCC8A2. Set a breakpoint
on it and print the arguments of “objc_msgSend” when we hit this breakpoint:
(lldb) br s -a 0xCC8A2
Breakpoint 1: where = SpringBoard`___lldb_unnamed_function299$$SpringBoard + 370,
address = 0x000cc8a2
Process 103706 stopped
* thread #1: tid = 0x1951a, 0x000cc8a2
SpringBoard`___lldb_unnamed_function299$$SpringBoard + 370, queue = 'com.apple.main-
thread, stop reason = breakpoint 1.1
frame #0: 0x000cc8a2 SpringBoard`___lldb_unnamed_function299$$SpringBoard + 370
SpringBoard`___lldb_unnamed_function299$$SpringBoard + 370:
-> 0xcc8a2: blx 0x3e3798 ; symbol stub for: objc_msgSend
0xcc8a6: mov r6, r0
0xcc8a8: movw r0, #31088
0xcc8ac: movt r0, #74
(lldb) po [$r0 class]
SBTelephonyManager
(lldb) po $r0
SBTelephonyManager
(lldb) p (char *)$r1
(char *) $2 = 0x0042eee6 "sharedTelephonyManager"
(lldb) c
128
Process 103706 resuming
As you can see, we’ve used “po” command to print the Objective-C object, and “p (char *)”
to print the C object by casting. Quite simple, right? It’s worth mentioning that when the
process stops on a “BL” instruction, LLDB will automatically parse this instruction and display
the corresponding symbol:
-> 0xcc8a2: blx 0x3e3798 ; symbol stub for: objc_msgSend
However, sometimes LLDB’s parsing is wrong, mistaking the symbol. In this case, please
refer to IDA’s static analysis of that symbol.
Finally, we can use “x” command to print the value stored in a specific address:
(lldb) p/x $sp
(unsigned int) $4 = 0x006e838c
(lldb) x/10 $sp
0x006e838c: 0x00000000 0x22f2c975 0x00000000 0x00000000
0x006e839c: 0x26c6bf8c 0x0000000c 0x17a753c0 0x17a753c8
0x006e83ac: 0x000001c8 0x17a75200
(lldb) x/10 0x006e838c
0x006e838c: 0x00000000 0x22f2c975 0x00000000 0x00000000
0x006e839c: 0x26c6bf8c 0x0000000c 0x17a753c0 0x17a753c8
0x006e83ac: 0x000001c8 0x17a75200
Figure 4- 20 [SpringBoard _menuButtonDown:]
The base address without offset of “movw r0, #33920” is 0x226654, as shown in figure 4-21.
Figure 4- 21 SpringBoard__accessibilityObjectWithinProximity__0
5. register write
“register write” is used to write a specific value to a specific register, hence “modify the
program when it stops, and observe the modification of its execution flow”. According to the
code in figure 4-22, the base address with offset of “TST.W R0, offset #0xFF” is known to be
0xEE7A2, if R0’s value is 0, the process will branch to the left, or to the right if R0 is not 0.
Figure 4- 22 Branches
Because the value of R0 is 0, BNE makes the process branch to the left:
(lldb) ni
Process 731 stopped
* thread #1: tid = 0x02db, 0x000ee7a6
SpringBoard`___lldb_unnamed_function299$$SpringBoard + 118, queue = ‘com.apple.main-
thread, stop reason = instruction step over
frame #0: 0x000ee7a6 SpringBoard`___lldb_unnamed_function299$$SpringBoard + 118
SpringBoard`___lldb_unnamed_function299$$SpringBoard + 118:
-> 0xee7a6: bne 0xee7b2 ; ___lldb_unnamed_function299$$SpringBoard
+ 130
0xee7a8: bl 0x10d340 ;
___lldb_unnamed_function1110$$SpringBoard
0xee7ac: tst.w r0, #255
0xee7b0: beq 0xee7da ; ___lldb_unnamed_function299$$SpringBoard
+ 170
(lldb) ni
Process 731 stopped
* thread #1: tid = 0x02db, 0x000ee7a8
SpringBoard`___lldb_unnamed_function299$$SpringBoard + 120, queue = ‘com.apple.main-
thread, stop reason = instruction step over
frame #0: 0x000ee7a8 SpringBoard`___lldb_unnamed_function299$$SpringBoard + 120
SpringBoard`___lldb_unnamed_function299$$SpringBoard + 120:
-> 0xee7a8: bl 0x10d340 ;
___lldb_unnamed_function1110$$SpringBoard
0xee7ac: tst.w r0, #255
0xee7b0: beq 0xee7da ; ___lldb_unnamed_function299$$SpringBoard
+ 170
0xee7b2: movw r0, #2174
Trigger that breakpoint again, change R0’s value to 1 by “register write”, and see if the
branch changes:
Process 731 stopped
* thread #1: tid = 0x02db, 0x000ee7a2
SpringBoard`___lldb_unnamed_function299$$SpringBoard + 114, queue = ‘com.apple.main-
thread, stop reason = breakpoint 3.1
frame #0: 0x000ee7a2 SpringBoard`___lldb_unnamed_function299$$SpringBoard + 114
SpringBoard`___lldb_unnamed_function299$$SpringBoard + 114:
-> 0xee7a2: tst.w r0, #255
0xee7a6: bne 0xee7b2 ; ___lldb_unnamed_function299$$SpringBoard
+ 130
0xee7a8: bl 0x10d340 ;
___lldb_unnamed_function1110$$SpringBoard
0xee7ac: tst.w r0, #255
(lldb) p $r0
(unsigned int) $5 = 0
(lldb) register write r0 1
(lldb) p $r0
(unsigned int) $6 = 1
(lldb) ni
132
Process 731 stopped
* thread #1: tid = 0x02db, 0x000ee7a6
SpringBoard`___lldb_unnamed_function299$$SpringBoard + 118, queue = ‘com.apple.main-
thread, stop reason = instruction step over
frame #0: 0x000ee7a6 SpringBoard`___lldb_unnamed_function299$$SpringBoard + 118
SpringBoard`___lldb_unnamed_function299$$SpringBoard + 118:
-> 0xee7a6: bne 0xee7b2 ; ___lldb_unnamed_function299$$SpringBoard
+ 130
0xee7a8: bl 0x10d340 ;
___lldb_unnamed_function1110$$SpringBoard
0xee7ac: tst.w r0, #255
0xee7b0: beq 0xee7da ; ___lldb_unnamed_function299$$SpringBoard
+ 170
(lldb)
Process 731 stopped
* thread #1: tid = 0x02db, 0x000ee7b2
SpringBoard`___lldb_unnamed_function299$$SpringBoard + 130, queue = ‘com.apple.main-
thread, stop reason = instruction step over
frame #0: 0x000ee7b2 SpringBoard`___lldb_unnamed_function299$$SpringBoard + 130
SpringBoard`___lldb_unnamed_function299$$SpringBoard + 130:
-> 0xee7b2: movw r0, #2174
0xee7b6: movt r0, #63
0xee7ba: add r0, pc
0xee7bc: ldr r0, [r0]
• Shortcuts in LLDB
133
If you want to repeat the last command in LLDB, you can simply press “enter”. If you want
to review all history commands, just press up and down on your keyboard.
LLDB commands are simple, but it’s not easy to solve complicated problems with these
simples commands. In chapter 6, we will introduce more common scenarios of using LLDB, and
before that, please be sure to understand the knowledge of this section.
4.4 dumpdecrypted
When introducing class-dump, we’ve mentioned that Apple encrypts all Apps from
AppStore, protecting them from being class-dumped. If we want to class-dump StoreApps, we
have to decrypt their executables at first. A handy tool, dumpdecrypted, by Stefan Esser
(@i0n1c) is commonly used in iOS reverse engineering.
dumpdecrypted is open sourced on GitHub, you have to compile it by yourselves. Now let’s
start from scratch to class-dump a virtual target, i.e. TargetApp.app to show you the steps of
decrypting an App, please follow me.
After “make”, a dumpdecrypted.dylib will be generated under the current directory. This
dylib can be reused, there’s no need to recompile.
134
3. Locate the executable to be decrypted with “ps” command
On iOS 8, all StoreApps are under /var/mobile/Containers/, and TargetApp.app’s
executable is under /var/mobile/Containers/Bundle/Application/XXXXXXXX-XXXX-XXXX-
XXXX-XXXXXXXXXXXX/TargetApp.app/. Since X is unknown, it’d be a great amount of work
to locate the executable manually. But a simple trick will save our days: First close all StoreApps
on iOS, then launch TargetApp and ssh into iOS to print all processes:
snakeninnysiMac:~ snakeninny$ ssh root@iOSIP
FunMaker-5:~ root# ps -e
PID TTY TIME CMD
1 ?? 3:28.32 /sbin/launchd
……
5717 ?? 0:00.21
/System/Library/PrivateFrameworks/MediaServices.framework/Support/mediaartworkd
5905 ?? 0:00.20 sshd: root@ttys000
5909 ?? 0:01.86 /var/mobile/Containers/Bundle/Application/03B61840-2349-4559-
B28E-0E2C6541F879/TargetApp.app/TargetApp
5911 ?? 0:00.07 /System/Library/Frameworks/UIKit.framework/Support/pasteboardd
5907 ttys000 0:00.03 -sh
5913 ttys000 0:00.01 ps –e
Because now there is only one running StoreApp, the only path that contains
“/var/mobile/Containers/Bundle/Application/” is the full path of TargetApp’s executable.
Here we’re using scp instead of iFunBox, anyway tools don’t matter.
135
6. Start decrypting
The usage of dumpdecrypted.dylib is as follows:
DYLD_INSERT_LIBRARIES=/path/to/dumpdecrypted.dylib /path/to/executable
For instance:
FunMaker-5:~ root# cd /var/mobile/Containers/Data/Application/D41C4343-63AA-4BFF-904B-
2146128611EE/Documents/
FunMaker-5:/var/mobile/Containers/Data/Application/D41C4343-63AA-4BFF-904B-
2146128611EE/Documents root# DYLD_INSERT_LIBRARIES=dumpdecrypted.dylib
/var/mobile/Containers/Bundle/Application/03B61840-2349-4559-B28E-
0E2C6541F879/TargetApp.app/TargetApp
mach-o decryption dumper
DISCLAIMER: This tool is only meant for security research purposes, not for application
crackers.
Copy TargetApp.decrypted to OSX ASAP. class-dump and IDA have been waiting for ages!
I think these 6 steps are clear enough, but some of you may still wonder, why to copy
dumpdecrypted.dylib to Documents directory?
Good question. We all know that StoreApps don’t have write permission to most of the
directories outside the sandbox. Since dumpdecrypted.dylib needs to write a decrypted file while
residing in a StoreApp and they have the same permission, so the destination of its write
operation should be somewhere writable. StoreApp can write to its Documents directory, so
dumpdecrypted.dylib should be able to work under this directory.
Let’s see what happens if dumpdecrypted.lib is not working under Documents directory:
136
FunMaker-5: /var/mobile/Containers/Data/Application/D41C4343-63AA-4BFF-904B-
2146128611EE/Documents root# mv dumpdecrypted.dylib /var/tmp/
FunMaker-5: /var/mobile/Containers/Data/Application/D41C4343-63AA-4BFF-904B-
2146128611EE/Documents root# cd /var/tmp
FunMaker-5:/var/tmp root# DYLD_INSERT_LIBRARIES=dumpdecrypted.dylib
/private/var/mobile/Containers/Bundle/Application/03B61840-2349-4559-B28E-
0E2C6541F879/TargetApp.app/TargetApp
dyld: could not load inserted library ‘dumpdecrypted.dylib’ because no suitable image
found. Did find:
dumpdecrypted.dylib: stat() failed with errno=1
Trace/BPT trap: 5
4.5 OpenSSH
Figure 4- 23 OpenSSH
OpenSSH will install SSH service on iOS (as shown in figure 4-23). Only 2 commands are
the most commonly used: ssh is used for remote logging, scp is used for remote file transfer.
The usage of ssh is as follows:
ssh user@iOSIP
For instance:
snakeninnysiMac:~ snakeninny$ ssh [email protected]
For instance:
snakeninnysiMac:~ snakeninny$ scp ~/1.png [email protected]:/var/tmp/
For instance:
snakeninnysiMac:~ snakeninny$ scp [email protected]:/var/log/syslog ~/iOSlog
These two commands are relatively simple and intuitive. After installing OpenSSH, make
sure to change the default login password “alpine”. There’re 2 users on iOS, i.e. root and
mobile, we need to change both passwords like this:
FunMaker-5:~ root# passwd root
Changing password for root.
New password:
Retype new password:
FunMaker-5:~ root# passwd mobile
Changing password for mobile.
New password:
Retype new password:
If we forget to change the default password, there’re chances that viruses like Ikee login as
root via ssh. This leads to very serious security disasters: all data on iOS including SMS, contacts,
AppleID passwords and so on is at the risk of leaking, the intruder can take control over your
device and do whatever he wants. Therefore, promise me you’ll change the default password
after installing OpenSSH, OK?
4.6 usbmuxd
Most of you ssh into iOS via WiFi, which leads to slow responses in remote debugging or
file copying. This is because of the instability of wireless network and the limitation of
transmission speed. The well-known hacker, Nikias Bassen (@pimskeks) has written a tool
named usbmuxd to forward local OSX/Windows port to remote iOS port. With this tool, we
can ssh into iOS via USB, greatly increasing the speed of SSH connection. usbmuxd is easy to
use:
138
1.0.8.tar.gz and decompress it. The files we are going to use are tcprelay.py and usbmux.py.
Copy them to the same directory such as:
/Users/snakeninny/Code/USBSSH/
usbmuxd speeds up ssh connection to less than 15 seconds in general, and should be your
first ssh choice.
139
4.7 iFile
Figure 4- 24 iFile
iFile is a very powerful file management App, you can view it as Finder’s parallel on iOS.
iFile is capable of all kinds of file operation including browsing, editing, cutting, copying and deb
installing, possessing great convenience.
iFile is rather user-friendly. Before installing a deb, remember to close Cydia at first, then tap
the deb file to be installed and choose “Installer” in the action sheet, as shown in figure 4-25.
140
Figure 4- 25 Install deb file
4.8 MTerminal
Figure 4- 26 MTerminal
MTerminal is an open sourced Terminal on iOS with all basic functions available. The usage
of MTerminal is no much difference to Terminal, if we put the screen and keyboard size aside. I
141
think the most practical scene of MTerminal is to test private methods in Cycript when we’re
blanking out on the subway or something.
syslogd is a daemon to record system logs on iOS, and “syslogd to /var/log/syslog” is used
to write the logs to a file at “/var/log/syslog”. You need to reboot iOS after you install this
tweak to automatically create the file “/var/log/syslog”. This file gets larger as time goes by,
you can zero clear it with the following command:
FunMaker-5:~ root# cat /dev/null > /var/log/syslog
4.10 Conclusion
We’ve introduced 9 tools in this chapter, among which CydiaSubstrate, LLDB and Cycript
are the top priorities. It is because of the existence of these iOS tools, along with the OSX toolkit
in chapter 3, that we get a complete iOS reverse engineering environment. There’s a famous
Chinese saying that we should know how as well as know why. Now that we’ve already known
how by finishing part 2 of this book, it’s time for us to know why in the next part. Stay tuned!
142
III
Theories
After you have learned the basic concepts of iOS reverse engineering from part 1 and then
have tried tools mentioned in part 2 by yourself, you now are equipped with the fundamental
knowledge of iOS reverse engineering. Once you’ve completed all previous examples in the
book, you may be frustrated because you don’t know what to do next. Actually, learning
reverse engineering is a process of getting our hands dirty, but where and how to do that?
Luckily, there are some good patterns for us to follow. In chapter 5 and 6, we will start from the
perspective of Objective-C and ARM respectively, combine unique theories in iOS reverse
engineering with tools we’ve mentioned before, then summarize a universal methodology of
iOS reverse engineering. Let’s get started!
143
Objective-C related iOS reverse engineering
5
Objective-C is a typical object-oriented programming language and most developers are
surely proficient with its basic usage. Using Objective-C in the introductory phase of iOS reverse
engineering can help us get a smooth transition from App development to reverse engineering.
Fortunately, the file format used in iOS is Mach-O and it consists of enough raw data for us to
restore the headers of binaries through class-dump or some other tools. With this information,
we can start reverse engineering from the level of Objective-C, and writing tweaks is
undoubtedly the most popular amusement at this stage. So let’s start from writing tweaks.
Figure 5- 1 Tweak icon
144
components and each component is an object. For example, every single icon, message and
photo is an object. Besides these visible objects, there are also many objects working in the
background, providing a variety of support for foreground objects. For instance, some objects
are responsible for communicating with servers of Apple and some others are responsible for
reading and writing files. One object can own other objects, such as an icon object owns a label
object, which displays the name of the App. In general, each object has its own significance. By
combination of different objects, developers can implement different features. In Objective-C,
we call the function of an object “method”. The behavior of method is called “implementation”.
The relationship among objects, methods and implementation is where tweaks take effect.
If an object is provided with some certain function, we can send it a message like [object
method] which lets the object perform its function, i.e. we can call the method of the object. So
far, you may wonder that “object” and “method” are both nouns, where is the verb that used to
perform the function? Good point, we lack a verb representing the implementation of
“method”. So here, the word “implementation” can be the missing verb and it means that when
we call the method, what does iOS do inside the method, or in other words, what code is
executed. In Objective-C, the relationship between method and its implementation is decided
during run time rather than compile time.
During development, method in [object method] may not be a noun. Instead, it can be a
verb. However, with only a brief [object method], we still don’t know how this method works.
Let’s take a look at the following examples.
• When here comes a phone call, we may say that “Mom, answer the phone, please”. When we want
to translate this sentence into Objective-C, it will be [mom answerThePhone]. Here, the object is
“mom” and the method is “answerThePhone”. The implementation could be “Mom stops cooking
and goes to the sitting room to answer the phone”.
• "snakeninny, come here and help me move out this box". This could be translated into [snakeninny
moveOutTheBox]. The object here is “snakeninny” and method is “moveOutTheBox” while the
implementation could be “snakeninny stops working and goes to the boss’ office to move a box
downstairs”.
145
dictionary and the implementation to be the meaning of that word. When you look up the
dictionary, you always want to find what does a certain abstruse word mean. When it comes to
programming, the implementation of a method does exactly the same as a word’s meaning in
the dictionary. Easier to understand, right? Lets’ move on.
As time goes on, the contents of dictionary have changed a lot and some old phrases have
been given some new interpretations. For example, when talking along with Apple, which
doesn’t refer to the fruit, jailbreak is not considered a crime, and SpringBoard has nothing to do
with a swimming pool. This phenomenon embodies in iOS especially. We can change the
associated implementation of a method in order to change function of the object. As long as
someone looks up a word in our modified dictionary, he or she will get the new meaning of the
word. For example, in LowPowerBanner as shown in figure 5-2, the system will show a
notification banner as a reminder to users when the device is in low battery. Interesting? It is
because I have changed the implementation of low battery reminder from popup alerts to
banners.
Figure 5- 2 LowPowerBanner
Another example is SMSNinja, as shown in figure 5-3. When you receive a spam message,
SMSNinja puts the spam message into trash box automatically. This feature is achieved by
changing the implementation of delegate method of receiving a message; I’ve added extra spam
146
detecting function to the original method. This kind of approach is similar to changing the
contents of dictionary and can be realized through the hook function provided by
CydiaSubstrate. The usage of CydiaSubstrate has been explained in the last two chapters, so if
you’ve already forgotten about it, you should go back and have a review.
Figure 5- 3 SMSNinja
147
uncomfortable when we don’t know where to use our abilities, so here are some tips to help
you look for inspiration for your first tweak.
• Use more, observe more
Play with your iPhone and take a look at every corner of iOS whenever you have spare time
rather than waste your time on social networks. Although iOS consists of lots of amazing
features, it still cannot meet the exact requirements of every single user. So the more you use,
the more you know about iOS and you are more likely to find where in iOS the user experience
is not that good, which turns out to be inspirations. With huge base of iOS users, you will surely
find some users who share the same thoughts with you. In other words, if you have a problem
to solve, regard it as a tweak inspiration. That’s how Characount for Notes was born on iOS 6.
At that time, I always saved the content of a tweet into a note. Since a tweet has an 140
characters limit, I’ve written a tweak to show the character count of per note as a reminder.
There was an Arabic user who sent mail to me to express his appreciation of this tweak and
asked me to add more features to make it work like MSWord. But I was not interested in this
idea, I had to say sorry to him.
Figure 5- 4 Characount for Notes
148
Different people use iOS in different ways, which depends on their own requirements. If
you don’t have much inspiration, you can listen to the requirements of users. As long as there
are requirements, there are potential users of your tweaks that meet these requirements.
If large projects have been done, you can write customized tweaks for minority. If you are
not qualified to reverse low-level functions, you can start from simple functions of higher level.
After each release, listen to your users’ feedbacks humbly and improve your tweaks with rapid
iteration. Trust me, your effort will pay off. Take LowPowerBanner as an example, the idea of
LowPowerBanner came from the suggestion of a user PrimeCode. I finished the first version of
LowPowerBanner in less than 5 hours and it had no more than 50 lines of code. However,
within 8 hours after the release, downloads had approached 30,000 (as shown in figure 5-5), the
popularity of it was far beyond my expectation. Remember, users’ wisdom is inexhaustible. If
you don’t have any good ideas, listening to users would be surprisingly helpful!
• Anatomize iOS
The greater your ability is, the more things you can do. Starting from writing small Apps,
with more and more practices you will have deeper and deeper understanding of iOS. iOS is a
closed operating system and only a tip of iceberg has been exposed to us. There are still far too
many features that are worth to be further explored. Every time a new jailbreak comes out,
someone will post the latest class-dump headers on the Internet. We can easily find the
download link by searching “iOS private headers” on Google, which eliminates the trouble of
class-dumping by ourselves. Objective-C methods follow a regular naming convention, making
it possible for us to guess the meanings of most methods. For example, in SpringBoard.h:
- (void)reboot;
- (void)relaunchSpringBoard;
And in UIViewController.h:
- (void)attentionClassDumpUser:(id)arg1
yesItsUsAgain:(id)arg2
althoughSwizzlingAndOverridingPrivateMethodsIsFun:(id)arg3
itWasntMuchFunWhenYourAppStoppedWorking:(id)arg4
149
pleaseRefrainFromDoingSoInTheFutureOkayThanksBye:(id)arg5;
Browsing method names is an important source of inspiration as well as a shortcut for you
to get familiar with low-level iOS functions. The more implementation details of iOS you
master, the more powerful tweaks you can write. Audio Recorder, developed by limneos, is a
best example. Even though the launch of iOS dates back to 2007, there is no feature like phone
call recording until Audio Recorder’s born 7 years later. I’m sure that there are a lot of people
who have the same idea and even have already tried to realize it by themselves. But why only
limneos succeeded? It is because limneos has a deeper understanding of iOS than others. “Talk is
cheap. Show me the code.”
² Bundles can be divided into 2 categories, which are App and framework respectively. Bundles of
AppStore Apps are stored in “/var/mobile/Containers/Bundle/Application/”, bundles of system
Apps are stored in “/Applications/”, and bundles of frameworks are stored in
“/System/Library/Frameworks” and “/System/Library/PrivateFrameworks”. For bundles of other
types, you can discuss with us on http://bbs.iosre.com.
² Configuration files of daemons, which are plist formatted, are all stored in
“/System/Library/LaunchDaemons/”, “/Library/LaunchDaemons” and
“/Library/LaunchAgents/”. The “ProgramArguments” fields in these files are the absolute paths of
daemon exectuables, such as:
snakeninnys-MacBook:~ snakeninny$ plutil -p
/Users/snakeninny/Desktop/com.apple.backboardd.plist
{
……
"ProgramArguments" => [
0 => "/usr/libexec/backboardd"
]
……
}
150
• Locate with Cydia
Deb packages installed through command “dpkg –I” will be recorded by Cydia. You can
locate these debs in Cydia’s “Expert” view under “Installed” category, as shown in figure 5-6.
Then you can choose the target App and go to “Details” view, as shown in figure 5-7.
151
After that, choose “Filesystem Content” and you will see all files in the deb package, as
shown in figure 5-8.
152
• PreferenceBundle
PreferenceBundle resides in the Settings App and its functionality is somehow vague. It can
be either used as a configuration of another process such as “DimInCall”, shown in figure 5-9.
Figure 5- 9 DimInCall
Or it can perform some actual operations and function like an executable such as “WLAN”,
shown in figure 5-10.
153
Figure 5- 10 WLAN
Our attention lies on actual operations of an App for sure. As a result, how to locate
PreferenceBundle binaries that perform the actual operations is one topic for us to study. Third
party PreferenceBundles that come from AppStore can be only used as configuration of their
corresponding Apps, they don’t provide any actual functions, there’s no need to locate them.
PreferenceBundles from Cydia are also not problems because the solution was already
introduced in “locate by Cydia”. However, when it comes to the iOS stock PreferenceBundles,
the process of locating their binaries is a bit complicated.
The UI of a PreferenceBundle can be written programmatically or be constructed from a
plist file with a fixed format (You can refer to
http://iphonedevwiki.net/index.php/Preferences_specifier_plist for the format). When we try
to reverse engineer a PreferenceBundle, if all control object types in the PreferenceBundle UI
come from preferences specifier plist, such as the “About” view shown in figure 5-11, we should
pay attention to distinguish whether it is written programmatically or constructed from plist.
154
Figure 5- 11 About
• grep
Grep is a command line tool from UNIX and it is capable of searching files that match a
given regular expression. Grep is a built-in command on OSX; on iOS, it is ported by Saurik and
installed accompanying with Cydia by default. grep can quickly narrow down the search scope
when we want to find the source of a string. For example, if we want to find which binaries call
[IMDAccount initWithAccountID:defaults:service:], we can rely on grep after we sshed into
iOS:
FunMaker-5:~ root# grep -r initWithAccountID:defaults:service: /System/Library/
155
Binary file /System/Library/Caches/com.apple.dyld/dyld_shared_cache_armv7s matches
grep: /System/Library/Caches/com.apple.dyld/enable-dylibs-to-override-cache: No such
file or directory
grep: /System/Library/Frameworks/CoreGraphics.framework/Resources/libCGCorePDF.dylib: No
such file or directory
grep: /System/Library/Frameworks/CoreGraphics.framework/Resources/libCMSBuiltin.dylib:
No such file or directory
grep: /System/Library/Frameworks/CoreGraphics.framework/Resources/libCMaps.dylib: No
such file or directory
grep: /System/Library/Frameworks/System.framework/System: No such file or directory
From the result, we can see that the method appears in dyld_shared_cache_armv7s. Now,
we can use grep again in the decached dyld_shared_cache_armv7s:
snakeninnysiMac:~ snakeninny$ grep -r initWithAccountID:defaults:service:
/Users/snakeninny/Code/iOSSystemBinaries/8.1_iPhone5
Binary file
/Users/snakeninny/Code/iOSSystemBinaries/8.1_iPhone5/dyld_shared_cache_armv7s matches
grep:
/Users/snakeninny/Code/iOSSystemBinaries/8.1_iPhone5/System/Library/Caches/com.apple.xpc
/sdk.dylib: Too many levels of symbolic links
grep:
/Users/snakeninny/Code/iOSSystemBinaries/8.1_iPhone5/System/Library/Frameworks/OpenGLES.
framework/libLLVMContainer.dylib: Too many levels of symbolic links
Binary file
/Users/snakeninny/Code/iOSSystemBinaries/8.1_iPhone5/System/Library/PrivateFrameworks/IM
DaemonCore.framework/IMDaemonCore matches
In default case, all text files containing the keyword “proximity” will be listed in Finder, as
shown in 5-13.
You can also narrow down the scope of your search by choosing recursively search the file
name in current directory. The remaining task is to open the result files and locate the target
methods inside.
• grep
157
Yes, it’s grep again! Since we have already mentioned that we can use grep to search strings
in binaries, it’s just a piece of cake for grep to deal with text files. Let’s try grep with previous
example:
snakeninnysiMac:~ snakeninny$ grep -r -i proximity
/Users/snakeninny/Code/iOSPrivateHeaders/8.1
/Users/snakeninny/Code/iOSPrivateHeaders/8.1/Frameworks/CoreLocation/CDStructures.h:
char proximityUUID[512];
/Users/snakeninny/Code/iOSPrivateHeaders/8.1/Frameworks/CoreLocation/CLBeacon.h:
NSUUID *_proximityUUID;
……
/Users/snakeninny/Code/iOSPrivateHeaders/8.1/SpringBoard/SpringBoard.h:-
(_Bool)proximityEventsEnabled;
/Users/snakeninny/Code/iOSPrivateHeaders/8.1/SpringBoard/SpringBoard.h:-
(void)_proximityChanged:(id)arg1;
Although the results of grep are comprehensive, it looks a little messy. Here, I recommend
using the built-in search function in OSX. After all, graphical interface looks more
straightforward than command line.
158
Set the tweak filter to “com.apple.springboard”, package it into a deb using Theos and
install it on iOS, then respring. If you feel a bit rusty, don’t worry, that’s normal; what we care
about is stability rather than speed. After lock screen appears, press the home button and lock
button at the same time to take a screenshot and then ssh into iOS to view the syslog:
FunMaker-5:~ root# grep iOSRE: /var/log/syslog
Nov 24 16:22:06 FunMaker-5 SpringBoard[2765]: iOSRE: saveScreenshot: is called
You can see that our message is shown in syslog, which means saveScreenshot: is called
during screenshot. Since the method name is so explicit, I think most of you still wonder can we
really take a screenshot by calling this method?
In iOS reverse engineering, don’t be afraid of your curiosity; try Cycript to satisfy your
curiosity.
• Cycript
Before I get to know Cycript, I used Theos to test methods. For example, to test
saveScreenshot:, I might write a tweak as follows:
%hook SpringBoard
- (void)_menuButtonDown:(id)down
{
%orig;
SBScreenShotter *shotter = [%c(SBScreenShotter) sharedInstance];
[shotter saveScreenshot:YES]; // For the argument here, I guess it’s YES; later
we’ll see what happens if it’s NO
}
%end
After the tweak takes effect, press the home button and saveScreenShot: will be called. Then
you can check whether there is a white flash on screen and whether there is a screenshot in your
album. After that, uninstall the tweak in Cydia.
This approach looked pretty simple before I use Cycript. However, after I’ve achieved the
same goal with Cycript, how regretful I was that I had wasted so much time.
The usage of Cycript has already been introduced in chapter 4. Since SBScreenShotter is a
class in SpringBoard, we should inject Cycript into SpringBoard and call the method directly to
test it out. Unlike tweaks, Cycript doesn’t ask for compilation and clearing up, which saves us
great amount of time.
ssh to iOS and then execute the following commands:
FunMaker-5:~ root# cycript -p SpringBoard
cy# [[SBScreenShotter sharedInstance] saveScreenshot:YES]
159
Do you see a white flash on your screen with a shutter sound and a screenshot in your
album, just like pressing home button and lock button together? OK, now it’s sure that calling
this method manages to take a screenshot. To further satisfy our curiosity, press the up key on
keyboard to repeat the last Cycript command and change YES to No. What is the execution
result? We will disclose the details in next section.
160
After the tweak is installed, respring once and take a screenshot. Then ssh to iOS to check
the syslog again, you should find information as follows:
FunMaker-5:~ root# grep iOSRE: /var/log/syslog
Nov 24 16:40:33 FunMaker-5 SpringBoard[2926]: iOSRE: flashColor:
UICachedDeviceWhiteColor, UIDeviceWhiteColorSpace 1 1
It can be seen that flash color is an object of type UICachedDeviceWhiteColor, and its
description is "UIDevice WhiteColorSpace 1 1". According to the Objective-C naming
conventions, UICachedDeviceWhiteColor is a class in UIKit, but we cannot find it in the
document, meaning it is a private class. Class-dump UIKit and then open
UICachedDeviceWhiteColor.h:
@interface UICachedDeviceWhiteColor : UIDeviceWhiteColor
{
}
- (void)_forceDealloc;
- (void)dealloc;
- (id)copy;
- (id)copyWithZone:(struct _NSZone *)arg1;
- (id)autorelease;
- (BOOL)retainWeakReference;
- (BOOL)allowsWeakReference;
- (unsigned int)retainCount;
- (id)retain;
- (oneway void)release;
@end
161
- (void)dealloc;
- (id)initWithCGColor:(struct CGColor *)arg1;
- (id)initWithWhite:(float)arg1 alpha:(float)arg2;
@end
UIDeviceWhiteColor inherits from UIColor. Since UIColor is a public class, stop our
analysis at this level is enough for us to get the result. For other id type arguments, we can apply
the same solution.
After we have known the effect of calling a method and analyzed its arguments, we can
write our own documents. I suggest you make some simple notes on the analysis results of
private methods so that you can recall it quickly next time you use the same private method.
Next, let’s use Cycript to test this method and see what effect it is when we pass [UIColor
magentaColor] as the argument.
FunMaker-5:~ root# cycript -p SpringBoard
cy# [[SBScreenFlash mainScreenFlasher] flashColor:[UIColor magentaColor]
withCompletion:nil]
A magenta flash scatters on the screen and it is much cooler than the original white flash.
Check the album and we don’t find a new screenshot. Therefore we guess that this method is
just for flashing the screen without actually performing the screenshot operation. Aha, a new
tweak inspiration arises, we can hook flashColor:withCompletion: and pass it a custom color to
enrich the screen flash with more colors. Also, we present it as an exercise and ask you to write a
tweak.
All above methodologies are summary of my 5-year experience. Because there is no official
documentations for iOS reverse engineering, my personal experiences will inevitably be biased
and impossible to cover everything. So you are welcome to http://bbs.iosre.com for further
discussions if you have any questions.
162
flashColor:withCompletion:. Neither can we know anything about implementation details and
their relationship, nor can we verify our guesses.
So far, we should celebrate for a while since we have just finished a tweak. Starting from the
idea, to target binaries, to interested methods and eventually to the tweak, all reverse
engineering on the level of Objective-C follows this methodology; the only differences lie in
implementation details. Even if you haven’t worked on jailbreak development at all, you can
still master this methodology, it’s nothing harder than App development. However, lower the
threshold is, fiercer the competition is. After you have mastered methodologies of iOS reverse
engineering on the level of Objective-C and want to step to a higher level, you will find class-
dump is not enough.
With a finished tweak, we still need to realize that we don’t fully understand the knowledge
related to this tweak, and class-dump headers is insufficient to satisfy our requirements to
master all knowledge. It’s like we are in a forest, class-dump just provide us with a shelter while
it is not able to help us go out. To find the exit, we further need a map and a compass, which are
IDA and LLDB. But these two tools are two high mountains in front of us. Most rookie reverse
engineers failed to climb over them and gave up in the half way. For those who have
successfully conquered the mountains of IDA and LLDB, they have finally enjoyed a
magnificent vista just like a dream has come true. A dream you dream alone is only a dream. A
dream we dream together is reality. Let’s stay together to climb over the mountains!
163
Notice: The following steps no longer work on iOS 8. However, the thinking pattern is
good to know.
164
Despite so many outputs, files shown below with suffix "strings" were very attractive to me:
Binary file /Applications/Preferences.app/English.lproj/General-Simulator.strings
matches
Binary file /Applications/Preferences.app/English.lproj/General~iphone.strings matches
Binary file /Applications/Preferences.app/General-Simulator.plist matches
Binary file /Applications/Preferences.app/General.plist matches
Binary file /Applications/Preferences.app/Preferences matches
Binary file /Applications/Preferences.app/en_GB.lproj/General-Simulator.strings matches
Binary file /Applications/Preferences.app/en_GB.lproj/General~iphone.strings matches
If nothing went wrong, they were localization files for Apps, which should contain the code
name of “Accessibility”. It was very convenient for us to inspect localization files with plutil. So
let’s take a look at "/Applications/Preferences.app/English.lproj/General~iphone.strings" first.
snakeninnys-MacBook:~ snakeninny$ plutil -p ~/General\~iphone.strings
{
"Videos..." => "• Videos..."
"Wallpaper" => "Wallpaper"
"TV_OUT" => "TV Out"
"SOUND_EFFECTS" => "Sound Effects"
"d_MINUTES" => "%@ Minutes"
……
"ACCESSIBILITY" => "Accessibility"
"Multitasking_Gestures" => "Multitasking Gestures"
……
}
• Discover General.plist
With new clues, I re-greped the filesystem with keyword “ACCESSIBILITY”:
FunMaker-4s:~ root# grep -r ACCESSIBILITY /
grep: /Applications/Activator.app/Default-568h@2x~iphone.png: No such file or directory
grep: /Applications/Activator.app/Default.png: No such file or directory
grep: /Applications/Activator.app/Default~iphone.png: No such file or directory
grep: /Applications/Activator.app/[email protected]: No such file or directory
grep: /Applications/Activator.app/[email protected]: No such file or
directory
grep: /Applications/Activator.app/[email protected]: No such file or
directory
Binary file /Applications/Preferences.app/Dutch.lproj/General-Simulator.strings matches
Binary file /Applications/Preferences.app/Dutch.lproj/General~iphone.strings matches
Binary file /Applications/Preferences.app/English.lproj/General-Simulator.strings
matches
Binary file /Applications/Preferences.app/English.lproj/General~iphone.strings matches
166
Binary file /Applications/Preferences.app/French.lproj/General-Simulator.strings matches
Binary file /Applications/Preferences.app/French.lproj/General~iphone.strings matches
Binary file /Applications/Preferences.app/General-Simulator.plist matches
Binary file /Applications/Preferences.app/General.plist matches
Binary file /Applications/Preferences.app/German.lproj/General-Simulator.strings matches
Binary file /Applications/Preferences.app/German.lproj/General~iphone.strings matches
……
• Discover AccessibilitySetting.bundle
167
As expected, this file was a standard preferences specifier plist and the capitalized
“ACCESSIBILITY” was in the 25th item. Compared with preferences specifier plist, I had locked
my target in the bundle of AccessibilitySettings. From the name of AccessibilitySettings, I
guessed that this bundle assumed the responsibility for all features in Accessibility. According to
the fixed file location theory in section 5.2.2, AccessibilitySettings must be under
“/System/Library/PreferenceBundles/” and we could locate it easily.
Took a look inside “/System/Library/PreferenceBundles/AccessibilitySetting.bundle”:
FunMaker-4s:~ root# ls -la
/System/Library/PreferenceBundles/AccessibilitySettings.bundle
total 240
drwxr-xr-x 37 root wheel 2414 Mar 10 2013 .
drwxr-xr-x 40 root wheel 1360 Jan 14 2014 ..
-rw-r--r-- 1 root wheel 2146 Mar 10 2013 Accessibility.plist
-rwxr-xr-x 1 root wheel 438800 Mar 10 2013 AccessibilitySettings
-rw-r--r-- 1 root wheel 238 Dec 22 2012 BluetoothDeviceConfig.plist
-rw-r--r-- 1 root wheel 252 Mar 10 2013 BrailleStatusCellSettings.plist
-rw-r--r-- 1 root wheel 4484 Dec 22 2012 [email protected]
-rw-r--r-- 1 root wheel 916 Dec 22 2012 [email protected]
drwxr-xr-x 2 root wheel 646 Feb 7 2013 Dutch.lproj
drwxr-xr-x 2 root wheel 646 Dec 22 2012 English.lproj
drwxr-xr-x 2 root wheel 646 Feb 7 2013 French.lproj
drwxr-xr-x 2 root wheel 646 Dec 22 2012 German.lproj
-rw-r--r-- 1 root wheel 703 Mar 10 2013 GuidedAccessSettings.plist
-rw-r--r-- 1 root wheel 807 Mar 10 2013 HandSettings.plist
-rw-r--r-- 1 root wheel 652 Mar 10 2013 HearingAidDetailSettings.plist
-rw-r--r-- 1 root wheel 507 Mar 10 2013 HearingAidSettings.plist
-rw-r--r-- 1 root wheel 383 Dec 22 2012 HomeClickSettings.plist
-rw-r--r-- 1 root wheel 447 Dec 22 2012 [email protected]
-rw-r--r-- 1 root wheel 1113 Dec 22 2012 [email protected]
-rw-r--r-- 1 root wheel 170 Dec 22 2012 [email protected]
-rw-r--r-- 1 root wheel 907 Mar 10 2013 Info.plist
drwxr-xr-x 2 root wheel 646 Feb 7 2013 Italian.lproj
drwxr-xr-x 2 root wheel 646 Feb 7 2013 Japanese.lproj
-rw-r--r-- 1 root wheel 364 Dec 22 2012 LargeFontsSettings.plist
-rw-r--r-- 1 root wheel 217 Mar 10 2013 NavigateImagesSettings.plist
-rw-r--r-- 1 root wheel 1030 Dec 22 2012 QuickSpeakSettings.plist
-rw-r--r-- 1 root wheel 346 Dec 22 2012 RegionNamesNonLocalized.strings
drwxr-xr-x 2 root wheel 646 Feb 7 2013 Spanish.lproj
-rw-r--r-- 1 root wheel 394 Dec 22 2012 [email protected]
-rw-r--r-- 1 root wheel 622 Mar 10 2013 TripleClickSettings.plist
-rw-r--r-- 1 root wheel 467 Dec 22 2012 VoiceOverBrailleOptions.plist
-rw-r--r-- 1 root wheel 2477 Mar 10 2013 VoiceOverSettings.plist
-rw-r--r-- 1 root wheel 540 Mar 10 2013 VoiceOverTypingFeedback.plist
-rw-r--r-- 1 root wheel 480 Dec 22 2012 ZoomSettings.plist
drwxr-xr-x 2 root wheel 102 Dec 22 2012 _CodeSignature
drwxr-xr-x 2 root wheel 646 Feb 7 2013 ar.lproj
-rw-r--r-- 1 root wheel 8371 Dec 22 2012 bottombar@2x~iphone.png
-rw-r--r-- 1 root wheel 2701 Dec 22 2012 bottombarblue@2x~iphone.png
-rw-r--r-- 1 root wheel 2487 Dec 22 2012 bottombarblue_pressed@2x~iphone.png
-rw-r--r-- 1 root wheel 2618 Dec 22 2012 bottombarred@2x~iphone.png
-rw-r--r-- 1 root wheel 2426 Dec 22 2012 bottombarred_pressed@2x~iphone.png
-rw-r--r-- 1 root wheel 2191 Dec 22 2012 bottombarwhite@2x~iphone.png
-rw-r--r-- 1 root wheel 2357 Dec 22 2012 bottombarwhite_pressed@2x~iphone.png
168
drwxr-xr-x 2 root wheel 646 Feb 7 2013 ca.lproj
drwxr-xr-x 2 root wheel 646 Feb 7 2013 cs.lproj
drwxr-xr-x 2 root wheel 646 Feb 7 2013 da.lproj
drwxr-xr-x 2 root wheel 646 Feb 7 2013 el.lproj
drwxr-xr-x 2 root wheel 646 Feb 7 2013 en_GB.lproj
drwxr-xr-x 2 root wheel 646 Feb 7 2013 fi.lproj
-rw-r--r-- 1 root wheel 955 Dec 22 2012 [email protected]
drwxr-xr-x 2 root wheel 646 Feb 7 2013 he.lproj
drwxr-xr-x 2 root wheel 646 Feb 7 2013 hr.lproj
drwxr-xr-x 2 root wheel 646 Feb 7 2013 hu.lproj
drwxr-xr-x 2 root wheel 646 Feb 7 2013 id.lproj
drwxr-xr-x 2 root wheel 646 Feb 7 2013 ko.lproj
drwxr-xr-x 2 root wheel 646 Feb 7 2013 ms.lproj
drwxr-xr-x 2 root wheel 646 Feb 7 2013 no.lproj
drwxr-xr-x 2 root wheel 646 Feb 7 2013 pl.lproj
drwxr-xr-x 2 root wheel 646 Feb 7 2013 pt.lproj
drwxr-xr-x 2 root wheel 646 Feb 7 2013 pt_PT.lproj
drwxr-xr-x 2 root wheel 646 Feb 7 2013 ro.lproj
drwxr-xr-x 2 root wheel 646 Feb 7 2013 ru.lproj
drwxr-xr-x 2 root wheel 646 Feb 7 2013 sk.lproj
drwxr-xr-x 2 root wheel 646 Feb 7 2013 sv.lproj
drwxr-xr-x 2 root wheel 646 Feb 7 2013 th.lproj
drwxr-xr-x 2 root wheel 646 Feb 7 2013 tr.lproj
-rw-r--r-- 1 root wheel 998 Dec 22 2012 [email protected]
drwxr-xr-x 2 root wheel 646 Feb 7 2013 uk.lproj
drwxr-xr-x 2 root wheel 646 Feb 7 2013 vi.lproj
drwxr-xr-x 2 root wheel 646 Feb 7 2013 zh_CN.lproj
drwxr-xr-x 2 root wheel 646 Feb 7 2013 zh_TW.lproj
Here, words like GuidedAccess, HomeClick and HearingAid corresponded with contents
we saw in “Accessibility” (as shown in figure 5-16), which confirmed my speculation.
The search result was very similar to the one at the beginning of this section. Open
“/System/Library/PreferenceBundles/
AccessibilitySettings.bundle/English.lproj/Accessibility~iphone.strings” and see what’s inside.
snakeninnys-MacBook:~ snakeninny$ plutil -p ~/Accessibility\~iphone.strings
{
"HAC_MODE_POWER_REDUCTION_N90" => "Hearing Aid Mode improves performance with some
hearing aids, but may reduce cellular reception."
"LEFT_RIGHT_BALANCE_SPOKEN" => "Left-Right Stereo Balance"
"QUICKSPEAK_TITLE" => "Speak Selection"
"LeftStereoBalanceIdentifier" => "L"
"ACCESSIBILITY_DEFAULT_HEADSET" => "Incoming Calls"
"HEADSET" => "Headset"
"CANCEL" => "Cancel"
"ON" => "On"
"CUSTOM_VIBRATIONS" => "Custom Vibrations"
"CONFIRM_INVERT_COLORS_REMOVAL" => "Are you sure you want to disable inverted colors?"
"SPEAK_AUTOCORRECTIONS" => "Speak Auto-text"
"DEFAULT_HEADSET_FOOTER" => "Choose route for incoming calls."
"HEARING_AID_COMPLIANCE_INSTRUCTIONS" => "Improves compatibility with hearing aids in
some circumstances. May reduce 2G cellular coverage."
"DEFAULT_HEADSET" => "Default to headset"
"ROOT_LEVEL_TITLE" => "Accessibility"
"HEARING_AID_COMPLIANCE" => "Hearing Aid Mode"
"CUSTOM_VIBES_INSTRUCTIONS" => "Assign unique vibration patterns to people in
Contacts. Change the default pattern for everyone in Sounds settings."
"VOICEOVERTOUCH_TEXT" => "VoiceOver is for users with
blindness or vision disabilities."
"IMPORTANT" => "Important"
"COGNITIVE_HEADING" => "Learning"
"HAC_MODE_EQUALIZATION_N94" => "Hearing Aid Mode improves audio quality with some
hearing aids."
"SAVE" => "Save"
"HOME_CLICK_TITLE" => "Home-click Speed"
"AIR_TOUCH_TITLE" => "AssistiveTouch"
"CONFIRM_ZOT_REMOVAL" => "Are you sure you want to disable Zoom?"
"VOICEOVER_TITLE" => "VoiceOver"
"OFF" => "Off"
"GUIDED_ACCESS_TITLE" => "Guided Access"
"ZOOMTOUCH_TEXT" => "Zoom is for users with low-vision acuity."
"INVERT_COLORS" => "Invert Colors"
"ACCESSIBILITY_SPEAK_AUTOCORRECTIONS" => "Speak Auto-text"
"LEFT_RIGHT_BALANCE_DETAILS" => "Adjust the audio volume balance between left and
right channels."
"MONO_AUDIO" => "Mono Audio"
"CONTRAST" => "Contrast"
"ZOOM_TITLE" => "Zoom"
"TRIPLE_CLICK_HEADING" => "Triple-click"
170
"OK" => "OK"
"SPEAKER" => "Speaker"
"AUTO_CORRECT_TEXT" => "Automatically speak auto-corrections
and auto-capitalizations."
"HEARING" => "Hearing"
"LARGE_FONT" => "Large Text"
"CONFIRM_VOT_USAGE" => "VoiceOver"
"CONFIRM_VOT_REMOVAL" => "Are you sure you want to disable VoiceOver?"
"HEARING_AID_TITLE" => "Hearing Aids"
"FLASH_LED" => "LED Flash for Alerts"
"VISION" => "Vision"
"CONFIRM_ZOOM_USAGE" => "Zoom"
"DEFAULT" => "Default"
"MOBILITY_HEADING" => "Physical & Motor"
"TRIPLE_CLICK_TITLE" => "Triple-click Home"
"RightStereoBalanceIdentifier" => "R"
}
• Locate Accessibility.plist
As you think, I’ve searched “ACCESSIBILITY_DEFAULT_HEADSET”:
FunMaker-4s:~ root# grep -r ACCESSIBILITY_DEFAULT_HEADSET
/System/Library/PreferenceBundles/AccessibilitySettings.bundle
Binary file
/System/Library/PreferenceBundles/AccessibilitySettings.bundle/Accessibility.plist
matches
Binary file
/System/Library/PreferenceBundles/AccessibilitySettings.bundle/Dutch.lproj/Accessibility
~iphone.strings matches
……
All were localization files except one plist file. So that should be what I was look for. Its
contents are as follows:
snakeninnys-MacBook:~ snakeninny$ plutil -p ~/Accessibility.plist
{
"title" => "ROOT_LEVEL_TITLE"
"items" => [
0 => {
"label" => "VISION"
"cell" => "PSGroupCell"
"footerText" => "AUTO_CORRECT_TEXT"
}
1 => {
"cell" => "PSLinkListCell"
"label" => "VOICEOVER_TITLE"
"detail" => "VoiceOverController"
"get" => "voiceOverTouchEnabled:"
}
2 => {
"cell" => "PSLinkListCell"
"label" => "ZOOM_TITLE"
"detail" => "ZoomController"
"get" => "zoomTouchEnabled:"
171
}
……
18 => {
"cell" => "PSLinkListCell"
"label" => "HOME_CLICK_TITLE"
"detail" => "HomeClickController"
"get" => "homeClickSpeed:"
}
19 => {
"detail" => "PSListItemsController"
"set" => "accessibilitySetPreference:specifier:"
"validValues" => [
0 => 0
1 => 1
2 => 2
]
"get" => "accessibilityPreferenceForSpecifier:"
"validTitles" => [
0 => "DEFAULT"
1 => "HEADSET"
2 => "SPEAKER"
]
"requiredCapabilities" => [
0 => "telephony"
]
"cell" => "PSLinkListCell"
"label" => "ACCESSIBILITY_DEFAULT_HEADSET"
"key" => "DefaultRouteForCall"
}
]
}
It was another standard preferences specifier plist and I knew that the getter and setter for
“Incoming Calls” were accessibilitySetPreference:specifier: and
accessibilityPreferenceForSpecifier:. So it was time to move on to the next step.
172
I was stuck on this problem for nearly half a month. During that period, I was always
thinking, what could iOS do inside the setter? Since preferences specifier plist used
PostNotification to notify changes of configuration files to other processes, and the
configuration of AccessibilitySettings was associated with MobilePhone, which happened to be
the mode of inter-process communication. Would accessibilitySetPreference:specifier: change
the configuration file and post a notification? To verify my guesses, I made use of
LibNotifyWatch by limneos to observe if there were any related notifications through manually
changing the configuration of “Incoming Calls”. Unexpectedly, it really made me a lucky hit.
FunMaker-4s:~ root# grep LibNotifyWatch: /var/log/syslog
Nov 26 00:09:20 FunMaker-4s Preferences[6488]: LibNotifyWatch: <CFNotificationCenter
0x1e875600 [0x39b4b100]> postNotificationName:UIViewAnimationDidCommitNotification
object:UIViewAnimationState userInfo:{
Nov 26 00:09:20 FunMaker-4s Preferences[6488]: LibNotifyWatch: <CFNotificationCenter
0x1e875600 [0x39b4b100]> postNotificationName:UIViewAnimationDidStopNotification
object:<UIViewAnimationState: 0x1ea74f20> userInfo:{
……
Nov 26 00:09:21 FunMaker-4s Preferences[6488]: LibNotifyWatch:
CFNotificationCenterPostNotification center=<CFNotificationCenter 0x1dd86bd0
[0x39b4b100]> name=com.apple.accessibility.defaultrouteforcall userInfo=(null)
deliverImmediately=1
Nov 26 00:09:21 FunMaker-4s Preferences[6488]: LibNotifyWatch: notify_post
com.apple.accessibility.defaultrouteforcall
……
173
After finding the most suspicious notification, I still had one more question: Where was the
configuration file?
In chapter 2, I have mentioned that there were plenty of user data in “/var/mobile/”. All
App related data were in “/var/mobile/Containers”; all media files were in
“/var/mobile/Media/”; and in “/var/mobile/Library/”, we can easily find the directory
“/var/mobile/library/Preferences/” then further locate “com.apple.Accessibility.plist”, whose
contents are as follows:
snakeninnys-MacBook:~ snakeninny$ plutil -p ~/com.apple.Accessibility.plist
{
……
"DefaultRouteForCallPreference" => 2
"VOTQuickNavEnabled" => 1
"CurrentRotorTypeWeb" => 3
"PunctuationKey" => 2
……
"ScreenCurtain" => 0
"VoiceOverTouchEnabled" => 0
"AssistiveTouchEnabled" => 0
}
174
After compiling, installing and respring, I pressed home button with my eyes closed, and
then checked “Settings” →“General” →“Accessibility” →“Incoming Calls” with excitement. Aha,
“Speaker” was chosen. I’ve made it!
// Required
extern "C" BOOL isCapable() {
if (kCFCoreFoundationVersionNumber >= kCFCoreFoundationVersionNumber_iOS_5_0 &&
[[[UIDevice currentDevice] model] isEqualToString:@"iPhone"])
return YES;
return NO;
}
// Required
extern "C" BOOL isEnabled() {
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] initWithCont
entsOfFile:ACCESSBILITY];
BOOL result = [[dictionary objectForKey:@"DefaultRouteForCallPreference"]
intValue] == 0 ? NO : YES;
[dictionary release];
return result;
}
// Optional
// Faster isEnabled. Remove this if it’s not necessary. Keep it if isEnabled() is
expensive and you can make it faster here.
extern "C" BOOL getStateFast() {
return isEnabled();
}
// Required
extern "C" void setState(BOOL enabled) {
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] initWithCont
entsOfFile:ACCESSBILITY];
[dictionary setObject:[NSNumber numberWithInt:(enabled ? 2 : 0)] forKey:@"D
efaultRouteForCallPreference"];
[dictionary writeToFile:ACCESSBILITY atomically:YES]; [dictionary release];
notify_post("com.apple.accessibility.defaultrouteforcall");
}
// Required
// How long the toggle takes to toggle, in seconds.
extern "C" float getDelayTime() {
return 0.6f;
}
175
Because the inspiration of this tweak came from Shoghian, I’ve signed his name as the
coauthor, as shown in figure 5-18. He was very happy and hence we made friends with each
other. Speaker SBSettings Toggle is my third public tweak on Cydia, with very simple functions
and no advertising, it still accumulated nearly 10,000 downloads, (as shown in figure 5-19),
which was a happy ending. More importantly, it was unexpectedly exhausting writing this
tweak. My target looked so simple until I really got my hands dirty, which gave me a warning
that actions spoke louder than words, I still had a long way to go. Not until the similar situations
happened again and again in later days then I finally realized that class-dump was only a
supporting role in iOS reverse engineering, and it indirectly encouraged me to dig into IDA and
LLDB, which helped me step onto a new stage in iOS reverse engineering.
Figure 5- 19 Neary 10,000 downloads
5.4 Conclusion
In this chapter, we’ve comprehensively introduced how a tweak works as well as the
thought and process of writing a tweak, accompanied with practical examples, I believe these
contents can help beginners learn iOS reverse engineering better. iOS reverse engineering in
176
Objective-C level is the first hurdle of this book; without knowing IDA and LLDB, we are not
able to go very deep into iOS reverse engineering, and our thinking logic is somehow
disordered. I think you can feel from the example that our ability at that stage is not adequate to
conduct elegant reverse engineering on binaries, so we have to guess a lot when we encounter
problems. Although the code we wrote just now was far cry from the official implementation, it
worked at least. The only reason is that Objective-C method names are very readable and
meaningful so that we can achieve our goals by guessing the functions of class-dump headers,
then test them with Cycript and Theos. Although the methodology in this chapter is kind of
“dirty”, it offers a totally different view from App development, which refreshes our mind and
broadens our horizon.
As beginners of iOS reverse engineering, our main purpose is to get familiar with jailbreak
environment and knowledge points in previous chapters. Also, we need to master the usage of a
variety of tools and deliberately cultivate our thinking patterns on reverse engineering. If you
have a lot of free time, I strongly recommend you to browse all class-dump headers and test the
private methods you are interested in, which will greatly enhance your familiarity with low-
level iOS and help you yield twice the result with half the effort after you learn IDA and LLDB.
As long as we try to think reversely and practice more, we can surely summarize effective
methodologies of ourselves, which helps us step onto a higher level both on iOS reverse
engineering and App development.
177
6
ARM related iOS reverse engineering
In previous chapters we have already introduced the fundamental knowledge and tool
usage in iOS reverse engineering. Now, you should be able to satisfy your curiosity by playing
with private methods and develop some mini tweaks. However, since you’ve come so far, I
believe you have a strong delving spirit and truly want to improve your programmatic ability. If
so, it’d be better for you to try something more challenging. Well, starting from this chapter,
iOS reverse engineering will enter polar night, and you’ll have to face the most arcane yet
magical hieroglyphics in the programming world. Take a deep breath first, and then ask
yourself, “Is iOS reverse engineering a right choice for me?” After finishing this chapter,
hopefully you will get the answer.
Next, we’ll meet the first advanced challenge in iOS reverse engineering: reading ARM
assembly. According to the previous chapters, you have already got the idea that Objective-C
code would become machine code after compiling, and then will be executed directly by CPU.
It is overwhelming work to read machine code let alone write them. However, it’s lucky that
there is assembly, which bridges Objective-C code with machine code. Even though the
readability of assembly is not as good as Objective-C, it’s much better than machine code. If you
can crash this hard nut, congratulations, you have the talents to be a reverse engineer.
Conversely, if you cannot, AppStore may suit you better.
178
hand, however, as a human readable language, it is no much difference with other human
languages, namely, if we use it more often, we will get familiar with it quicker.
As App developers, chances are rare for us to deal with assembly in our daily work. In this
situation, if we don’t practice deliberately, we cannot handle it for sure. In a nutshell, it’s all
about whether our time and energy is poured into learning it. Well, iOS reverse engineering
offers us a great chance to learn ARM assembly. When we’re reversing a function, we need to
analyze massive lines of ARM assembly, and translate them to high-level language manually to
reconstruct the functions. Even though there is no need to write assembly yet, a vast reading
will definitely improve our understanding of it. ARM assembly is a necessity in iOS reverse
engineering; you have to master it if you really want to be a member of this field. Like English,
basic ARM assembly concepts correspond to 26 letters and phonetic symbols in English; its
instructions correspond to words, and instructions’ variants correspond to different word tenses;
its calling conventions correspond to grammars, which define the connection between words.
Sounds not that bad, right? Let’s delve into it step by step.
179
In fact, stack is in memory as well. But it works like a stack, i.e. follows the “first in last out”
rule. The stack of ARM is full descending, meaning that the stack grows towards lower address,
the latest object is placed at the bottom, which is at the lowest address, as shown in the figure 6-
1.
Figure 6-1 The stack of ARM
A register, named “stack pointer” (hereafter referred to as SP), holds the bottom address of
stack, i.e. the stack address. We can push a register into stack to save its value, or pop a register
out of stack to load its value. During process running, SP changes a lot, but before and after a
block of code is executed, SP should stay the same, otherwise there will be a fatal problem.
Why? Let’s take an example:
static int global_var0;
static int global_var1;
void foo(void)
{
bar();
// other operations;
}
In the above code snippet, suppose that foo() uses registers A, B, C, and D; foo() calls bar(),
and suppose that bar() uses registers A, B, and C. Because registers A, B and C are overlapped in
foo() and bar(), bar() needs to save values of A, B, and C into stack before it starts execution.
180
Also, it needs to restore these 3 registers from stack before it ends execution, to make sure foo()
can work correctly. Let’s look at some pseudo code:
// foo()
foo:
// Push A, B, C, D into stack, save their values
push {A, B, C, D}
// Use A ~ D
move A, #1 // A = 1
move B, #2 // B = 2
move C, #3 // C = 3
call bar
move D, global_var0
// global_var1 = A + B + C + D
add A, B // A = A + B, notice A’s value
add A, C // A = A + C, notice A’s value
add A, D // A = A + D, notice A’s value
move global_var1, A
// Pop A, B, C, D out of stack, restore their values
pop {A-D}
return
// bar()
bar:
// Push A、B、C into the stack, store their values
push {A-C}
// Use A ~ C
move A, #2 // Do you know what this instruction do?
move B, #5
move C, A
add C, B // C = 7
// global_var0 = A + B + C (== 2 * C)
add C, C
move global_var0, C // A = 2,B = 5,C = 14
Let’s shortly explain this snippet of pseudo code: firstly, foo() sets registers A, B and C to 1, 2
and 3 respectively, then calls bar(), which changes values of A, B and C as well sets global_var0,
a global variable, to the sum of registers A, B and C. If we directly use the current values of A, B
and C to calculate the value of global_var1 for now, then the result would be wrong. So before
executing bar(),values of A, B and C should be pushed into stack first, and pop them out after
the execution of bar() for restoration, then we can get a correct global_var1. Notice that, for the
same reason, foo() has done the same operations on A, B, C and D, which saves its callers’ days.
• Preserved registers
Some registers in ARM processors must preserve their values after a function call, as shown
below:
181
R0-R3 Passes arguments and return values
R7 Frame pointer, which points to the previously saved stack frame and the
saved link register
R9 Reserved by system before iOS 3.0
R12 IP register,used by dynamic linker
R13 Stack Pointer, i.e. SP
R14 Link Register, i.e. LR, saves function return address
R15 Program Counter, i.e. PC
We’re not writing ARM assembly yet, so treat the above table as a reference would be
enough.
• Branches
The process saves the address of the next instruction in PC register. Usually, CPU will
execute instructions in order. When it has done with one instruction, PC will increase 1 to point
to the next instruction, as shown in figure 6-2.
Figure 6-2 Execute instructions in order
The processor will execute instructions from 1 to 5 in a plain and trivial way. However, if
we change the value of PC, the execution order will be very different, as shown in figure 6-3.
182
Figure 6-3 Execute instructions out of order
The instructions’ execution has been disordered to 1, 5, 4, 2, 3 and 6, which is bizarre and
remarkable. This kind of “disorder” is officially called “branch” or “jump”, which makes loop
and subroutine possible. For example:
// endless()
endless:
operate op1, op2
branch endless
return // Dead loop, we cannot reach here!
In actual cases, conditional branches, which are triggered under some specific conditions,
are the most practical branches. “if else” and “while” are both based on conditional branches. In
ARM assembly, there are 4 kinds of conditional branches:
² The operation overflows (for example, the sum of two positive numbers exceeds 32 bits).
These operation results are often represented as flags and are saved in the Program Status
Register (PSR). Some instructions will change these flags according to their operation results,
and conditional branches decide whether to branch according to these flags. The pseudo code
below shows an example of for loop:
183
for:
add A, #1
compare A, #16
bne for // If A - 16 != 0 then jump to for
The above code compares A and #16, if they’re not equal, increase A by 1 and compare
again. Otherwise break out the loop and go on to the next instruction.
“cond” and “s” are two optional suffixes. “cond” decides the execution condition of “op”,
and there are 17 conditions:
EQ The result equals to 0 (EQual to 0)
NE The result doesn’t equal to 0 (Not Equal)
CS The operation has carry or borrow (Carry Set)
HS Same to CS (unsigned Higher or Same)
CC The operation has no carry or borrow (Carry Clear)
LO Same to CC (unsigned LOwer)
MI The result is negative (MInus)
PL The result is greater than or equal to 0 (PLus)
VS The operation overflows (oVerflow Set)
VC The operation doesn’t overflow (oVerflow Clear)
HI If operand1 is unsigned HIgher than operand2
LS If operand1 is unsigned Lower or Same than operand2
GE If operand1 is signed Greater than or Equal to operand2
LT If operand1 is signed Less Than operand2
GT If operand1 is signed Greater Than operand2
LE If operand1 is signed Less than or Equal operand2
AL ALways,this is the default
Compare R0 with R1, if R0 is greater than or equal to R1, then R2 = R0, otherwise R2 = R1.
184
“s” decides whether “op” sets flags or not, there are 4 flags:
N (Negative)
If the result is negative then assign 1 to N, otherwise assign 0 to N.
Z (Zero)
If the result is zero then assign 1 to Z, otherwise assign 0 to Z.
C (Carry)
For add operations (including CMN), if they have carry then assign 1 to C, otherwise
assign 0 to C; for sub operations (including CMP), Carry acts as Not-Borrow, if borrow
happens then assign 0 to C, otherwise assign 1 to C; for shift operations (excluding add
or sub), assign C the last bit to be shifted out; for the rest of operations, C stays
unchanged.
V (oVerflow)
If the operation overflows then assign 1 to V, otherwise assign 0 to V.
One thing to note, C flag works on unsigned calculations, whereas V flag works on signed
calculations.
Data processing instructions can be divided into 4 kinds:
• Arithmetic instructions
ADD R0, R1, R2 ; R0 = R1 + R2
ADC R0, R1, R2 ; R0 = R1 + R2 + C(arry)
SUB R0, R1, R2 ; R0 = R1 - R2
SBC R0, R1, R2 ; R0 = R1 - R2 - !C
RSB R0, R1, R2 ; R0 = R2 - R1
RSC R0, R1, R2 ; R0 = R2 - R1 - !C
All arithmetic instructions are based on ADD and SUB. RSB is the abbreviation of “Reverse
SuB”, which just reverse the two operands of SUB; instructions ending with “C” stands for ADD
with carry or SUB with borrow, and they will assign 1 to C flag when there is carry or there isn’t
borrow.
There is not much to explain about these instructions with their corresponding C operators.
You may have noticed that there’s no shift instruction, because ARM uses barrel shift with 4
instructions:
LSL Logical Shift Left, as shown in figure 6-4
185
Figure 6-4 LSL
Figure 6-5 LSR
Figure 6-6 ASR
Figure 6-7 ROR
• Compare instructions
CMP R1, R2 ; Set flag according to the result of R1 - R2
CMN R1, R2 ; Set flag according to the result of R1 + R2
TST R1, R2 ; Set flag according to the result of R1 & R2
TEQ R1, R2 ; Set flag according to the result of R1 ^ R2
Compare instructions are just arithmetic or logical operation instructions that change flags,
but they don’t save the results in registers.
• Multiply instructions
MUL R4, R3, R2 ; R4 = R3 * R2
MLA R4, R3, R2, R1 ; R4 = R3 * R2 + R1
186
• Register processing instructions
The basic syntax of register processing instructions is:
op{cond}{type} Rd, [Rn, Op2]
Rn, the base register, stores base address; the function of “cond” is the same to data
processing instructions; “type” decides the data type which “op” operates, there are 4 types:
B (unsigned Byte)
Extends to 32 bits when executing,filled with 0.
SB (Signed Byte)
For LDR only;extends to 32 bits when executing,filled with the sign bit.
H (unsigned Halfword)
Extends to 32 bits when executing,filled with 0.
SH (Signed Halfword)
For LDR only;extends to 32 bits when executing,filled with the sign bit.
² STR
STR Rt, [Rn {, #offset}] ; *(Rn {+ offset}) = Rt
STR Rt, [Rn, #offset]! ; *(Rn {+ offset}) = Rt; Rn = Rn + offset
STR Rt, [Rn], #offset ; *Rn = Rt; Rn = Rn + offset
Besides, LDRD and STRD, the variants of LDR and STR, can operate doubleword, namely,
LDR or STR two registers at once. The syntax of them is:
op{cond} Rt, Rt2, [Rn {, #offset}]
The use of LDRD and STRD is just like LDR and STR:
² STRD
STRD R4, R5, [R9,#offset] ; *(R9 + offset) = R4; *(R9 + offset + 4) = R5
² LDRD
LDRD R4, R5, [R9,#offset] ; R4 = *(R9 + offset); R5 = *(R9 + offset + 4)
Beside LDR and STR, LDM (LoaD Multiple) and STM (STore Multiple) can process several
registers at the same time like this:
op{cond}{mode} Rd{!}, reglist
187
Rd is the base register, and the optional “!” decides whether the modified Rd is written back
to the original Rd if “op” modifies Rd; reglist is a list of registers which are curly braced and
separated by “,”, or we can use “-” to represent a scope, such as {R4 – R6, R8} stands for R4, R5,
R6 and R8; these registers are ordered according to their numbers, regardless of their positions
inside the braces.
Attention, the operation direction of LDM and STM is opposite to LDR and STR; LDM
reads memory starting from Rd then write to reglist, while STM reads from reglist then write to
memory starting from Rd. This is a little confusing; please don’t mess up.
The function of “cond” is the same to data processing instructions. And, “mode” specifies
how Rd is modified, including 4 cases:
IA (Increment After)
Increment Rd after “op”.
IB (Increment Before)
Increment Rd before “op”.
DA (Decrement After)
Decrement Rd after “op”.
DB (Decrement Before)
Decrement Rd before “op”.
What do they mean? We will use LDM as an example. As figure 6-8 shows, R0 points to 5
currently.
Figure 6-8 Simulation of LDM
After executing the following instructions, R4, R5 and R6 will change to:
LDMIA R0, {R4 – R6} ; R4 = 5, R5 = 6, R6 = 7
LDMIB R0, {R4 – R6} ; R4 = 6, R5 = 7, R6 = 8
LDMDA R0, {R4 – R6} ; R4 = 5, R5 = 4, R6 = 3
LDMDB R0, {R4 – R6} ; R4 = 4, R5 = 3, R6 = 2
STM works similarly. Notice again, the operation direction of LDM and STM is just
opposite to LDR and STR.
188
• Branch instructions
Branch instructions can be divided into 2 kinds: unconditional branches and conditional
branches.
² Unconditional branches
B Label ; PC = Label
BL Label ; LR = PC – 4; PC = Label
BX Rd ; PC = Rd ,and switch instruction set
² Conditional branches
The “cond” of conditional branches are decided by the 4 flag mentioned in section 6.2.1,
their correspondences are:
cond flag
EQ Z = 1
NE Z = 0
CS C = 1
HS C = 1
CC C = 0
LO C = 0
MI N = 1
PL N = 0
VS V = 1
VC V = 0
HI C = 1 & Z = 0
LS C = 0 | Z = 1
GE N = V
LT N != V
GT Z = 0 & N = V
LE Z = 1 | N != V
Before every conditional branch there will be a data processing instruction to set the flag,
which determines if the condition is met or not, hence influence the code execution flow.
Label:
LDR R0, [R1], #4
CMP R0, 0 ; If R0 == 0 then Z = 1; else Z = 0
BNE Label ; If Z == 0 then jump
• THUMB instructions
THUMB instruction set is a subset of ARM instruction set. Every THUMB instruction is 16
bits long, so THUMB instructions are more space saving than ARM instructions, and can be
faster transferred on 16-bit data bus. However, you can’t make an omelet without breaking
eggs. All THUMB instructions except “b” can’t be executed conditionally; barrel shift can’t
189
cooperate with other instructions; most THUMB instructions can only make use of registers R0
to R7, etc. Compared with ARM instructions, the features of THUMB instructions are:
² There’re less THUMB instructions than ARM instructions
Since THUMB is just a subset, the number of THUMB instructions is definitely less. For
example, among all multiply instructions, only MUL is kept in THUMB.
² No conditional execution
Except branch instructions, other instructions cannot be executed conditionally.
But cannot:
ADD R0, R1, LSL #2
² Limitation of registers
Unless declared explicitly, THUMB instructions can only make use of R0 to R7. However,
there are exceptions: ADD, MOV, and CMP can use R8 to R15 as operands; LDR and STR can
use PC or SP; PUSH can use LR, POP can use PC; BX can use all registers.
190
6.1.3 ARM calling conventions
After a brief look at the commonly used ARM instructions, I believe you can barely read the
assembly of a function for now. When a function calls another function, arguments and return
values need to be passed between the caller and the callee. The rule of how to pass them is
called ARM calling conventions.
² PUSH LR;
² PUSH R7;
² R7 = SP;
² POP R7;
191
“The first 4 arguments are saved in R0, R1, R2 and R3; the rest are saved on the stack; the
return value is saved in R0.”
A concise but informative sentence, right? To make a deeper impression, let’s see an
example:
// clang -arch armv7 -isysroot `xcrun --sdk iphoneos --show-sdk-path` -o MainBinary
main.m
#include <stdio.h>
Save this code snippet as main.m, and compile it with the sentence in comments. Then drag
and drop MainBinary into IDA and locate to main, as shown in figure 6-9.
Figure 6-9 main in assembly
“BLX _printf” calls printf, and its 6 arguments are stored in R0, R1, R2, R3, [SP, #0x20 +
var_20], and [SP, #0x20 + var_1C] respectively; the return value is stored in R0. Because var_20
= -0x20,var_1C = -0x1C, 2 arguments in the stack are at [SP] and [SP, #0x4].
I don’t think we need further explanation.
“The first 4 arguments are saved in R0, R1, R2 and R3; the rest are saved on the stack; the
return value is saved in R0.”
192
Promise me you’ll remember “sentence of the book”, which is the key to most problems in
iOS reverse engineering!
This section just walked you through the most basic knowledge about ARM assembly; there
were omissions for sure. However, to be honest, with “sentence of the book” and the official
site of ARM, you can start reversing 99% of all Apps. Next, it’s time for us to figure out how to
use the knowledge we have just learned in practical iOS reverse engineering.
193
Figure 6- 10 Mail
Figure 6- 11 MobilePhoneSettings
Facing such demands, class-dump is quite helpless. Luckily, we have already learned how to
use Cycript, IDA and LLDB, and gained some basic knowledge about ARM assembly; with their
help, there are patterns to follow for “locate target functions”. For most of us, among all iOS
software, we know Apps the best, so if we’re to choose something as our junior reverse targets,
194
nothing is more appropriate than Apps. As a result, in the following sections, we will take Apps
as examples, and try to refine “locate target functions” with ARM level reverse engineering, as
well enhance the methodology of writing a tweak.
6.2.1 Cut into the target App and find the UI function
For an App, what we’re interested in are regularly presented on UI, which exhibits
execution processes and results. The relationship between function and UI is very tight, if we
can get the UI object that interests us, we can find its corresponding function, which is referred
to as “UI function”. The process of getting the programmatic UI object of our interested visual
UI control object, then further getting the UI function of the programmatic UI object is usually
implemented with Cycript, with the magic private method “recursiveDescription” in UIView
and the undervalued public method “nextResponder” in UIResponder. In the rest of this
chapter, I will explain this process by taking Mail as the example to summarize the
methodology, and then apply the methodology to MobilePhoneSettings to give you a deeper
impression. All the work is finished on iPhone 5, iOS 8.1.
195
First of all, execute “?expand” in Cycript to turn on “expand”, so that Cycript will translate
control characters such as “\n” to corresponding formats and give the output a better
readability.
cy# [[UIApp keyWindow] recursiveDescription]
Of course, through other methods of UIApplication and UIView, it is also feasible to get
views we are interested in, for example:
cy# [UIApp windows]
@[#"<UIWindow: 0x14587a70; frame = (0 0; 320 568); gestureRecognizers = <NSArray:
0x147166b0>; layer = <UIWindowLayer: 0x14587e30>>",#"<UITextEffectsWindow: 0x15850570;
frame = (0 0; 320 568); opaque = NO; gestureRecognizers = <NSArray: 0x147503e0>; layer =
<UIWindowLayer: 0x1474ff10>>"]
The above code can get subviews and superviews. In a word, we can get any view objects
that are visible on UI by combining the above methods, which lays the foundation for our next
steps.
In order to locate “compose” button, we need to find out the corresponding control object.
To accomplish this, the regular approach is to examine control objects one by one. For views
like <UIView: viewAddress; …>, we call [#viewAddress setHidden:YES] for everyone of them,
and the disappeared control object is the matching one. Of course, some tricks could accelerate
the examination; because on the left side of this button there’re two lines of sentences, we can
infer that the button shares the same superview with this two sentences; if we can find out the
superview, the rest of work is only examining subviews of this superview, hence reduce our
work burden. Commonly, texts will be printed in description, so we can directly search “3
Unsent Messages” in recursiveDescription:
| | | | | | | | <MailStatusUpdateView: 0x146e6060; frame = (0 0;
182 44); opaque = NO; autoresize = W+H; layer = <CALayer: 0x146c8840>>
| | | | | | | | | <UILabel: 0x14609610; frame = (40 21.5; 102
13.5); text = ‘3 Unsent Messages’; opaque = NO; userInteractionEnabled = NO; layer =
<_UILabelLayer: 0x146097f0>>
However, only the sentences are hidden, the button remains visible, as shown in figure 6-12:
197
Figure 6-12 Two lines of sentences are hidden
This indicates that the level of MailStatusUpdateView is lower than or equal to the button,
right? So, next let’s check the superview of MailStatusUpdateView. From recursiveDescription,
we realize that its superview is MailStatusBarView:
| | | | | | | <MailStatusBarView: 0x146c4110; frame = (69 0; 182
44); opaque = NO; autoresize = BM; layer = <CALayer: 0x146f9f90>>
| | | | | | | | <MailStatusUpdateView: 0x146e6060; frame = (0 0;
182 44); opaque = NO; autoresize = W+H; layer = <CALayer: 0x146c8840>>
It’s disappointing; two sentences are hidden but not the button, which means that the level
of MailStatusBarView is still not high enough, let’s keep looking for its superview, i.e.
UIToolBar:
| | | | | | <UIToolbar: 0x146f62a0; frame = (0 524; 320 44); opaque =
NO; autoresize = W+TM; layer = <CALayer: 0x146f6420>>
| | | | | | | <_UIToolbarBackground: 0x14607ed0; frame = (0 0; 320
44); autoresize = W; userInteractionEnabled = NO; layer = <CALayer: 0x14607d40>>
| | | | | | | | <_UIBackdropView: 0x15829590; frame = (0 0; 320
44); opaque = NO; autoresize = W+H; userInteractionEnabled = NO; layer =
<_UIBackdropViewLayer: 0x158297e0>>
| | | | | | | | | <_UIBackdropEffectView: 0x14509020; frame =
(0 0; 320 44); clipsToBounds = YES; opaque = NO; autoresize = W+H;
userInteractionEnabled = NO; layer = <CABackdropLayer: 0x145a68d0>>
| | | | | | | | | <UIView: 0x147335c0; frame = (0 0; 320 44);
hidden = YES; opaque = NO; autoresize = W+H; userInteractionEnabled = NO; layer =
<CALayer: 0x145f3ab0>>
198
| | | | | | | <UIImageView: 0x14725730; frame = (0 -0.5; 320 0.5);
autoresize = W+BM; userInteractionEnabled = NO; layer = <CALayer: 0x1472be40>>
| | | | | | | <MailStatusBarView: 0x146c4110; frame = (69 0; 182
44); opaque = NO; autoresize = BM; layer = <CALayer: 0x146f9f90>>
Figure 6-13 UIToolBar is hidden
This time, the button is hidden, which means the button is a subview of UIToolBar. Look
for keyword “button” in subviews of UIToolBar, and we can easily locate UIToolbarButton:
| | | | | | | <MailStatusBarView: 0x146c4110; frame = (69 0; 182
44); opaque = NO; autoresize = BM; layer = <CALayer: 0x146f9f90>>
| | | | | | | | <MailStatusUpdateView: 0x146e6060; frame = (0 0;
182 44); opaque = NO; autoresize = W+H; layer = <CALayer: 0x146c8840>>
| | | | | | | | | <UILabel: 0x14609610; frame = (40 21.5; 102
13.5); text = ‘3 Unsent Messages’; opaque = NO; userInteractionEnabled = NO; layer =
<_UILabelLayer: 0x146097f0>>
| | | | | | | | | <UILabel: 0x145f3020; frame = (43 8; 96.5
13.5); text = ‘Updated Just Now’; opaque = NO; userInteractionEnabled = NO; layer =
<_UILabelLayer: 0x145f2e50>>
| | | | | | | <UIToolbarButton: 0x14798410; frame = (285 0; 23 44);
opaque = NO; gestureRecognizers = <NSArray: 0x14799510>; layer = <CALayer: 0x14798510>>
199
Figure 6-14 Button is hidden
200
@["_sendAction:withEvent:"]
Be careful, Settings App’s name is Preferences. It will show frequently in this chapter, please
keep an eye.
2. Examine the view hierarchy of “Phone” view, and locate the top cell
As usual, let’s take a look at the view hierarchy first:
cy# ?expand
expand == true
cy# [[UIApp keyWindow] recursiveDescription]
@"<UIWindow: 0x17d62e00; frame = (0 0; 320 568); autoresize = H; gestureRecognizers =
<NSArray: 0x17d589b0>; layer = <UIWindowLayer: 0x17d21c60>>
| <UILayoutContainerView: 0x17d86620; frame = (0 0; 320 568); autoresize = W+H; layer
= <CALayer: 0x17d863b0>>
| | <UIView: 0x17ef2430; frame = (0 0; 320 0); layer = <CALayer: 0x17ef24a0>>
| | <UILayoutContainerView: 0x17d7eb80; frame = (0 0; 320 568); clipsToBounds =
YES; gestureRecognizers = <NSArray: 0x17eb6400>; layer = <CALayer: 0x17d7ed60>>
……
| | | | | | | | | | | <PSTableCell: 0x17f92890;
baseClass = UITableViewCell; frame = (0 35; 320 44); text = ‘My Number’; autoresize = W;
tag = 2; layer = <CALayer: 0x17f92a60>>
| | | | | | | | | | | | <UITableViewCellContentView:
0x17f92ad0; frame = (0 0; 287 43.5); gestureRecognizers = <NSArray: 0x17f92ce0>; layer =
<CALayer: 0x17f92b40>>
| | | | | | | | | | | | | <UITableViewLabel:
0x17f92d30; frame = (15 12; 90 20.5); text = ‘My Number’; userInteractionEnabled = NO;
layer = <_UILabelLayer: 0x17f92df0>>
| | | | | | | | | | | | | <UITableViewLabel:
0x17f93060; frame = (132.5 12; 152.5 20.5); text = ‘+86PhoneNumber’;
userInteractionEnabled = NO; layer = <_UILabelLayer: 0x17f93120>>
201
It’s easy to locate the control object that shows “+86PhoneNumber”, and we can say for
sure its cell is a PSTableCell object without test. Try to hide this cell to verify our guesses:
cy# [#0x17f92890 setHidden:YES]
Figure 6-15 Hide the top cell
202
id a = function(dataSource);
id b = function(a);
id c = function(b);
…
id z = function(y);
NSString *myPhoneNumber = function(z); // tail
The variable is already known, and we’re at the tail of the call chain. Reverse engineering, as
its name suggests, enables us to track from the tail back to the head. In this process we will find
out every function in this chain, so that we can regenerate the whole algorithm. In a nutshell, to
regenerate the algorithm is to record every data source (data source’s data source, and so on.
Hereafter referred to as the Nth data source) and the trace of function calls along the trip. When
the Nth data source of the variable is a determined data (say in this chapter, the Nth data source
is the SIM card), the functions between Nth data source and variable is the algorithm. Confused?
It’ll become clearer after this example.
Figure 6-16 MVC design pattern, taken from Stanford CS 193P
According to MVC design pattern (as shown in figure 6-16), M stands for model, namely,
the data source, which is unknown; V stands for view, namely, the top cell, which is known; C
stands for controller, which is unknown. M and V has no direct relations, while C can directly
access both M and V, hence is the communication center of MVC. If we can make use of the
known V to acquire C, can’t we access M via C to get the data source? This method is logically
accessible, is it practicable?
Based on my professional experiences so far, getting C from V is 100% doable; the key is the
public method [UIResponder nextResponder], which has the same position to
recursiveDescription in my heart. Its description is:
203
“The UIResponder class does not store or set the next responder automatically, instead
returning nil by default. Subclasses must override this method to set the next responder.
UIView implements this method by returning the UIViewController object that manages it (if
it has one) or its superview (if it doesn’t); UIViewController implements the method by
returning its view’s superview; UIWindow returns the application object, and UIApplication
returns nil.”
It means that for a V, the return value of nextResponder is either the corresponding C or its
superview. Because none of M, V or C can be absent in an App, C exists fore sure, namely, there
must be a [V nextResponder] that returns a C. Besides, we can get all Vs from
recursiveDescription, so getting C from known V is approachable, then M is not far from us.
Therefore, our current target is to get C of the top cell, and it’s relatively easy; keep calling
nextResponder from cell, until a C is returned:
cy# [#0x17f92890 nextResponder]
#"<UITableViewWrapperView: 0x17eb4fc0; frame = (0 0; 320 504); gestureRecognizers =
<NSArray: 0x17ee5230>; layer = <CALayer: 0x17ee5170>; contentOffset: {0, 0};
contentSize: {320, 504}>"
cy# [#0x17eb4fc0 nextResponder]
#"<UITableView: 0x16c69e00; frame = (0 0; 320 568); autoresize = W+H; gestureRecognizers
= <NSArray: 0x17f4ace0>; layer = <CALayer: 0x17f4ac20>; contentOffset: {0, -64};
contentSize: {320, 717.5}>"
cy# [#0x16c69e00 nextResponder]
#"<UIView: 0x17ebf2b0; frame = (0 0; 320 568); autoresize = W+H; layer = <CALayer:
0x17ebf320>>"
cy# [#0x17ebf2b0 nextResponder]
#"<PhoneSettingsController 0x17f411e0: navItem <UINavigationItem: 0x17dae890>, view
<UITableView: 0x16c69e00; frame = (0 0; 320 568); autoresize = W+H; gestureRecognizers =
<NSArray: 0x17f4ace0>; layer = <CALayer: 0x17f4ac20>; contentOffset: {0, -64};
contentSize: {320, 717.5}>>"
As soon as we get C, we can search in C’s header for clues of M. In this example, first we
need to locate the binary that contains PhoneSettingsController, we aren’t sure whether it
comes from Preferences.app or a certain PreferenceBundle. In this case, a simple test would be
all good:
FunMaker-5:~ root# grep -r PhoneSettingsController /Applications/Preferences.app/
FunMaker-5:~ root# grep -r PhoneSettingsController /System/Library/
Binary file /System/Library/Caches/com.apple.dyld/dyld_shared_cache_armv7s matches
grep: /System/Library/Caches/com.apple.dyld/enable-dylibs-to-override-cache: No such
file or directory
grep: /System/Library/Frameworks/CoreGraphics.framework/Resources/libCGCorePDF.dylib: No
such file or directory
grep: /System/Library/Frameworks/CoreGraphics.framework/Resources/libCMSBuiltin.dylib:
No such file or directory
grep: /System/Library/Frameworks/CoreGraphics.framework/Resources/libCMaps.dylib: No
such file or directory
grep: /System/Library/Frameworks/System.framework/System: No such file or directory
204
Binary file /System/Library/PreferenceBundles/MobilePhoneSettings.bundle/Info.plist
matches
It seems that this class comes from MobilePhoneSettings.bundle. Next, class-dump its binary
and open PhoneSettingsController.h:
@interface PhoneSettingsController : PhoneSettingsListController
<TPSetPINViewControllerDelegate>
……
- (id)myNumber:(id)arg1;
- (void)setMyNumber:(id)arg1 specifier:(id)arg2;
……
- (id)tableView:(id)arg1 cellForRowAtIndexPath:(id)arg2;
@end
From the above snippet, we know the first 2 methods have obvious relationships with my
number. While in a more general manner, the 3rd method is used for initializing all cells, it can
be regarded as the UI function of cells. Therefore, data source of the top cell certainly lies in
these 3 methods, and we’ll take the 3rd method as an example. Let’s set a breakpoint at the end
of [PhoneSettingsController tableView:cellForRowAtIndexPath:] with LLDB, and see if the
return value contains my number. Attach debugserver to Preferences, then connect LLDB to
debugserver, and check the ASLR offset of MobilePhoneSettings:
(lldb) image list -o -f
[ 0] 0x00078000
/private/var/db/stash/_.29LMeZ/Applications/Preferences.app/Preferences(0x000000000007c0
00)
[ 1] 0x00231000 /Library/MobileSubstrate/MobileSubstrate.dylib(0x0000000000231000)
[ 2] 0x06db3000 /Users/snakeninny/Library/Developer/Xcode/iOS DeviceSupport/8.1
(12B411)/Symbols/System/Library/PrivateFrameworks/BulletinBoard.framework/BulletinBoard
[ 3] 0x06db3000 /Users/snakeninny/Library/Developer/Xcode/iOS DeviceSupport/8.1
(12B411)/Symbols/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation
……
[322] 0x06db3000 /Users/snakeninny/Library/Developer/Xcode/iOS DeviceSupport/8.1
(12B411)/Symbols/System/Library/PreferenceBundles/MobilePhoneSettings.bundle/MobilePhone
Settings
……
As we can see, the ASLR offset of MobilePhoneSettings is 0x6db3000. Then check the
address of the last instruction in [PhoneSettingsController tableView:cellForRowAtIndexPath:],
as shown in figure 6-17:
205
Figure 6-17 [PhoneSettingsController tableView:cellForRowAtIndexPath:]
Because the return value is stored in R0, let’s set the breakpoint at “ADD SP, SP, #8”, then
re-enter MobilePhoneSettings to trigger the breakpoint. Print R0 out when the process stops, an
initialized cell should be ready by then:
(lldb) br s -a 0x2c965c2c
Breakpoint 2: where = MobilePhoneSettings`-[PhoneSettingsController
tableView:cellForRowAtIndexPath:] + 236, address = 0x2c965c2c
Process 115525 stopped
* thread #1: tid = 0x1c345, 0x2c965c2c MobilePhoneSettings`-[PhoneSettingsController
tableView:cellForRowAtIndexPath:] + 236, queue = ‘com.apple.main-thread, stop reason =
breakpoint 2.1
frame #0: 0x2c965c2c MobilePhoneSettings`-[PhoneSettingsController
tableView:cellForRowAtIndexPath:] + 236
MobilePhoneSettings`-[PhoneSettingsController tableView:cellForRowAtIndexPath:] + 236:
-> 0x2c965c2c: add sp, #8
0x2c965c2e: pop {r4, r5, r6, r7, pc}
MobilePhoneSettings`-[PhoneSettingsController applicationWillSuspend]:
0x2c965c30: push {r7, lr}
0x2c965c32: mov r7, sp
(lldb) po $r0
<PSTableCell: 0x15f41440; baseClass = UITableViewCell; frame = (0 0; 320 44); text = ‘My
Number’; tag = 2; layer = <CALayer: 0x15f4c930>>
(lldb) po [$r0 subviews]
<__NSArrayM 0x17060e50>(
<UITableViewCellContentView: 0x15ed0660; frame = (0 0; 320 44); gestureRecognizers =
<NSArray: 0x15f491e0>; layer = <CALayer: 0x15ed06d0>>,
<UIButton: 0x15f26f50; frame = (302 16; 8 13); opaque = NO; userInteractionEnabled = NO;
layer = <CALayer: 0x15f27050>>
)
206
6.2.2 Locate the target function from the UI function
Successfully getting the UI function is a perfect beginning. UI functions have close ties with
UI, namely, if we call [ComposeButtonItem _sendAction:withEvent:] to compose an email, or
call [PhoneSettingsController tableView:cellForRowAtIndexPath:] to get my number, a lot of
correlated events will happen on UI, such as the view will be refreshed, the layout will be
updated, etc. It is over reacting. In most cases, we just want to stay low and perform the
functions without interrupting the UI. So what should we do when facing this kind of challenge?
As developers, I assume you have the most basic programmatic knowledge: the lowest level
functions are written directly in assembly, which are far from us for now; the remaining
functions are all nested called. Since UI functions are rather high level functions, they certainly
nested call our target functions, which can be shown as the following pseudo code:
drink GetRegular(water arg)
{
Functions();
return MakeRegular(arg);
}
drink GetDiet(void)
{
Functions();
return MakeDiet();
}
drink GetZero(void)
{
Functions();
return MakeZero();
}
drink Get7Up(void)
{
Functions();
return Make7Up();
}
drink GetMirinda(void)
{
Functions();
return MakeMirinda();
}
207
{
if (arg3 == clear) Get7Up();
else if (arg3 == orange) GetMirinda();
return GetRegular(arg2);
}
We don’t want to be served with coke and pepsi at the same time (you can regard them as
UI functions). If we only want to drink 7Up (data), we need to find Get7Up (target function
which generates the data); if we want to know how Zero is made (function), we need to find
MakeZero (target function which provides function). Actually, the “nest” of nested called
functions are also consists of chains, so if we can get to know any link of the chain, we can
regenerate the whole chain by reverse engineering, and the tools we mainly use are IDA and
LLDB. Let’s continue with the previous 2 examples to learn how to find target functions of
“compose email” and “get my number” by referring to [ComposeButtonItem
_sendAction:withEvent:] and [PhoneSettingsController tableView:cellForRowAtIndexPath:].
Figure 6-18 [ComposeButtonItem _sendAction:withEvent:] is not found
208
+(id)composeButtonItem;
@end
- (void)_attachGestureRecognizerToView:(id)arg1;
- (id)createViewForNavigationItem:(id)arg1;
- (id)createViewForToolbar:(id)arg1;
- (void)longPressGestureRecognized:(id)arg1;
- (void)setLongPressTarget:(id)arg1 action:(SEL)arg2;
@end
It doesn’t implement this method either, so let’s proceed to its super class. Open
UIBarButtonItem.h:
@interface UIBarButtonItem : UIBarItem <NSCoding>
……
- (void)_sendAction:(id)arg1 withEvent:(id)arg2;
……
@end
UIBarButtonItem does implement this method, so it’s UIKit that we should analyze. Drag
and drop the binary into IDA, since UIKit is big in size, it takes a rather long time to be analyzed.
During waiting time, how about dropping in http://bbs.iosre.com for a chat?
After the initial analysis of UIKit, let’s go to the implementation of [UIBarButtonItem
_sendAction:withEvent:], as shown in figure 6-19.
Figure 6-19 [UIBarButtonItem _sendAction:withEvent:]
210
Figure 6-20 R0’s evolution
Similarly, before “BLX.W _objc_msgSend”, the latest assignment of R1 comes from “MOV
R1, R4”, thus R1 comes from R4; the latest assignment of R4 comes from “LDR R4, [R0]”, thus
R4 comes from *R0, i.e. “action” which is already commented out in IDA. The evolution of R1
is shown in figure 6-21:
Figure 6-21 R1’s change process
So after reproduction, the first objc_msgSend becomes [self action], and the return value is
stored in R0, right? Next, the process judges whether [self action] is 0. If it is 0, there will be no
actions; otherwise, it branches to figure 6-22:
211
Figure 6-22 [UIBarButtonItem _sendAction:withEvent:]
There’re 4 objc_msgSends, let’s analyze them with the same thought one by one:
R0 of the 1st objc_msgSend comes from “LDR R0, [R2]”, and IDA has already figured out
that [R2] is a UIApplication class; R1 comes from “LDR R1, [R0]”, i.e. “sharedApplication”. So
the 1st objc_msgSend is actually [UIApplication sharedApplication], and the return value is
stored in R0.
R0 of the 2nd objc_msgSend comes from “MOV R0, R10”, i.e. R10; in figure 6-20, we can
see that R10 is UIBarButtonItem; R1 comes from “MOV R1, R4”, i.e. R4; in figure 6-21, R4 is
“action”. So, the 2nd objc_msgSend is actually [UIBarButtonItem action], and the return value is
stored in R0.
R0 of the 3rd objc_msgSend comes from “MOV R0, R10”, i.e. UIBarButtonItem; R1 comes
from “LDR R1, [R0]”, i.e. “target”. Therefore, the 3rd objc_msgSend is actually
[UIBarButtonItem target], and the return value is stored in R0.
R0 of the 4th objc_msgSend comes from “MOV R0, R5”, i.e. R5; R5 comes from “MOV R5,
R0” under the 1st objc_msgSend, i.e. R0. What’s R0? Because the 1st objc_msgSend stores its
return value in R0, R0 is the return value of [UIApplication sharedApplication] as well the 1st
argument of the 4th objc_msgSend. R1 comes from “LDR R1, [R0]”, i.e.
“sendAction:to:from:forEvent:”, which has 4 arguments. Since objc_msgSend already has 2
arguments, there’re 6 arguments in total, R0~R3 are not enough to hold all arguments, the last
2 arguments have to be stored on the stack. R2 comes from “MOV R2, R4”, i.e. R4; R4 comes
from “MOV R4, R0” under the 2nd objc_msgSend, i.e. R0; R0 comes from the return value of
the 2nd objc_msgSend, i.e. [UIBarButtonItem action], which is the 3rd argument. R3 comes
212
from “MOV R3, R0” under the 3rd objc_msgSend, i.e. R0; R0 comes from the return value of
the 3rd objc_msgSend, i.e. [UIBarButtonItem target], which is the 4th argument. The rest 2
arguments come from the stack, and before the 4th objc_msgSend, the latest change of stack
comes from “STRD.W R10, R11, [SP]”, i.e. R10 and R11 are saved onto the stack; therefore, the
rest 2 arguments are R10 and R11. R10 is UIBarButtonItem, which is discussed several times;
whereas R11 comes from “MOV R11, R3” in figure 6-21, i.e. R3, which is another unassigned
register, so it must come from the caller of [UIBarButtonItem _sendAction:withEvent:]. Based
on our previous analysis, R11 is the 2nd argument of _sendAction:withEvent:, i.e. event. The
relationship of these 4 arguments is a little complicated, hope figure 6-23 and 6-24 can give you a
better illustration.
213
Figure 6-23 The relationship of objc_msgSend’s arguments
Figure 6-24 The relationship of objc_msgSend’s arguments
214
[ 0] 0x0008e000
/private/var/db/stash/_.29LMeZ/Applications/MobileMail.app/MobileMail(0x0000000000092000
)
[ 1] 0x00393000 /Library/MobileSubstrate/MobileSubstrate.dylib(0x0000000000393000)
[ 2] 0x06db3000 /Users/snakeninny/Library/Developer/Xcode/iOS DeviceSupport/8.1
(12B411)/Symbols/usr/lib/libarchive.2.dylib
……
[ 45] 0x06db3000 /Users/snakeninny/Library/Developer/Xcode/iOS DeviceSupport/8.1
(12B411)/Symbols/System/Library/Frameworks/UIKit.framework/UIKit
……
ASLR offset of UIKit is 0x6db3000. Let’s check out the address of the 4th objc_msgSend, as
shown in figure 6-25.
Figure 6-25 Check out address of objc_msgSend
215
<UITouchesEvent: 0x176a8ce0> timestamp: 58147.4 touches: {(
<UITouch: 0x1895e2b0> phase: Ended tap count: 1 window: <UIWindow: 0x17759c30; frame
= (0 0; 320 568); gestureRecognizers = <NSArray: 0x1775c7a0>; layer = <UIWindowLayer:
0x1752e190>> view: <UIToolbarButton: 0x1776ff50; frame = (285 0; 23 44); opaque = NO;
gestureRecognizers = <NSArray: 0x17758670>; layer = <CALayer: 0x17770160>> location in
window: {308, 534} previous location in window: {304.5, 534} location in view: {23, 10}
previous location in view: {19.5, 10}
)}
The first 4 arguments of objc_msgSend, i.e. R0~R3 are intuitive. They’re self,
@selector(sendAction:to:from:forEvent:), the argument of sendAction:, and the argument of
to:. One thing to mention is that when I entered “po $r2”, LLDB said “no Objective-C
description available”, indicating R2 wasn’t an Objective-C object. Thus, combining with the
meaning of “action”, I guessed it was a SEL, so I used “p (char *)$r2” to print it. How to analyze
those arguments in the stack? Because SP points to the bottom of stack while the rest 2
arguments are on the stack, and they are both one word long, I’ve printed out the continuous 10
words from the bottom of the stack using “x/10 $sp”, and the first 2 were the arguments on
stack. Most Objective-C arguments are one word long pointers, which point at Objective-C
objects, so I’ve “po”ed the first 2 words, they were the arguments. For ease of understanding,
the relationship of SP, values on the stack and arguments are shown in figure 6-26.
216
Figure 6-26 The relationship of SP, value in the stack and arguments
In most cases, the number of arguments on stack will not exceed 10, so “x/10 $sp” is
enough. Print them in order, we can get all arguments on stack.
With the combination of IDA and LLDB, we have figured out that the core in
[UIBarButtonItem _sendAction:withEvent:] is [MailAppController
sendAction:@selector(composeButtonClicked:) to:nil from:ComposeButtonItem
forEvent:UITouchesEvent], which is one step closer to our target function of “composing
email”. Next let’s figure out what does [UIApplication sendAction:to:from:forEvent:] do with
IDA, as shown in figure 6-27:
217
Figure 6- 27 [UIApplication sendAction:to:from:forEvent:]
219
As we can see, arguments of performSelector:withObject:withObject: have changed, and
[MailAppController composeButtonClicked:ComposeButtonItem] was called. If we “c” again,
the breakpoint will not be triggered, so we can confirm it’s composeButtonClicked: that
performs the actual operation. Because inside MobileMail, we can get an MailAppController
object from [UIApplication sharedApplication], and at the beginning of this section, we’ve seen a
class method +composeButtonItem in ComposeButtonItem.h, which returns a
ComposeButtonItem object, now we’re able to get all necessary objects to call
[MailAppController composeButtonClicked:ComposeButtonItem]; what’s more, we can call it
anywhere inside MobileMail. Therefore, composeButtonClicked: can be regarded as the target
function of “compose email”.
Finally, let’s test this method in Cycript to see if it works:
FunMaker-5:~ root# cycript -p MobileMail
cy# [UIApp composeButtonClicked:[ComposeButtonItem composeButtonItem]]
After the above commands, the “New Message” view shows in Mail. In this example, we’ve
tracked the call chain with IDA until the target function was located, and then we’ve analyzed
its arguments with LLDB. I call it a complex process rather than a difficult one, do you agree? In
the next section, we will find out the target function of “my number” with the similar pattern,
please try to sum up the experiences.
220
Figure 6-28 Source of R4
Figure 6-29 Source of objc_msgSendSuper2
221
ASLR offset of MobilePhoneSettings is 0x6db3000. Then take a look at
objc_msgSendSuper2’s address, as shown in figure 6-30.
Figure 6-30 Check out address of objc_msgSendSuper2
It’s worth mentioning that the 1st argument of objc_msgSendSuper2 is not an Objective-C
object. I’m not sure whether it is a bug of LLDB or it is the actual case. Anyway, it doesn’t
influence our analysis, just ignore it for now. If you’re really interested in this detail, you are
welcome to share your research on http://bbs.iosre.com.
Back on track, the output of LLDB indicates that the return value of objc_msgSendSuper2 is
an initialized cell, which contains my number already. Similar to what happened in the last
section, let’s check out the implementation of tableView:cellForRowAtIndexPath: in
PhoneSettingsController’s superclass. First of all let’s figure out who’s the superclass in
PhoneSettingsController.h:
@interface PhoneSettingsController : PhoneSettingsListController
<TPSetPINViewControllerDelegate>
……
@end
- (id)bundle;
- (void)dealloc;
- (id)init;
- (void)pushController:(Class)arg1 specifier:(id)arg2;
- (id)setCellEnabled:(BOOL)arg1 atIndex:(unsigned int)arg2;
- (id)setCellLoading:(BOOL)arg1 atIndex:(unsigned int)arg2;
- (id)setControlEnabled:(BOOL)arg1 atIndex:(unsigned int)arg2;
- (id)sheetSpecifierWithTitle:(id)arg1 controller:(Class)arg2 detail:(Class)arg3;
- (void)simRemoved:(id)arg1;
- (id)specifiers;
- (void)updateCellStates;
- (void)viewWillAppear:(BOOL)arg1;
@end
223
Figure 6-31 Locate PSListController.h
Note, PSListController.h comes from Preferences.framework, which shares the name with
Preferences.app, make sure to distinguish them. Open it, and check if there is
tableView:cellForRowAtIndexPath:.
@interface PSListController : PSViewController <UITableViewDelegate,
UITableViewDataSource, UIActionSheetDelegate, UIAlertViewDelegate,
UIPopoverControllerDelegate, PSSpecifierObserver, PSViewControllerOffsetProtocol>
……
- (id)tableView:(id)arg1 cellForRowAtIndexPath:(id)arg2;
……
@end
As we see, it has implemented this method, so drag and drop the binary of
Preferences.framework into IDA and jump to tableView:cellForRowAtIndexPath:, as shown in
figure 6-32.
224
Figure 6-32 [PSListController tableView:cellForRowAtIndexPath:]
Its execution logic is complicated. To play it safe, let’s set a breakpoint at the end of this
method to check if “my number” is contained in the return value, so that we can make sure
objc_msgSendSuper2 calls [PSListController tableView:cellForRowAtIndexPath:]. First, let’s
check out ASLR offset of Preferences.framework:
(lldb) image list -o -f
[ 0] 0x00079000
/private/var/db/stash/_.29LMeZ/Applications/Preferences.app/Preferences(0x000000000007d0
00)
[ 1] 0x00232000 /Library/MobileSubstrate/MobileSubstrate.dylib(0x0000000000232000)
[ 2] 0x06db3000 /Users/snakeninny/Library/Developer/Xcode/iOS DeviceSupport/8.1
(12B411)/Symbols/System/Library/PrivateFrameworks/BulletinBoard.framework/BulletinBoard
[ 3] 0x06db3000 /Users/snakeninny/Library/Developer/Xcode/iOS DeviceSupport/8.1
(12B411)/Symbols/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation
……
[ 42] 0x06db3000 /Users/snakeninny/Library/Developer/Xcode/iOS DeviceSupport/8.1
(12B411)/Symbols/System/Library/PrivateFrameworks/Preferences.framework/Preferences
……
Its ASLR offset is 0x6db3000. Then find the address of the last instruction of
[PSListController tableView:cellForRowAtIndexPath:], as shown in figure 6-33.
225
Figure 6-33 [PSListController tableView:cellForRowAtIndexPath:]
Because the return value is stored in R0 and R0 comes from “MOV R0, R6”, we can simply
set a breakpoint on this instruction and print out R6. The address of this instruction is
0x2A9F79E6, so set the breakpoint at 0x6db3000 + 0x2A9F79E6 = 0x317AA9E6. Re-enter
MobilePhoneSettings to trigger the breakpoint:
(lldb) br s -a 0x317AA9E6
Breakpoint 5: where = Preferences`-[PSListController tableView:cellForRowAtIndexPath:] +
1026, address = 0x317aa9e6
Process 268587 stopped
* thread #1: tid = 0x4192b, 0x317aa9e6 Preferences`-[PSListController
tableView:cellForRowAtIndexPath:] + 1026, queue = ‘com.apple.main-thread, stop reason =
breakpoint 5.1
frame #0: 0x317aa9e6 Preferences`-[PSListController
tableView:cellForRowAtIndexPath:] + 1026
Preferences`-[PSListController tableView:cellForRowAtIndexPath:] + 1026:
-> 0x317aa9e6: mov r0, r6
0x317aa9e8: add sp, #28
0x317aa9ea: pop.w {r8, r10, r11}
0x317aa9ee: pop {r4, r5, r6, r7, pc}
(lldb) po $r6
<PSTableCell: 0x15f8c6a0; baseClass = UITableViewCell; frame = (0 0; 320 44); text = ‘My
Number’; tag = 2; layer = <CALayer: 0x15f7c0b0>>
(lldb) po [$r6 detailTextLabel]
<UITableViewLabel: 0x15f7b8d0; frame = (0 0; 0 0); text = ‘+86PhoneNumber’;
userInteractionEnabled = NO; layer = <_UILabelLayer: 0x15f7b990>>
226
Figure 6-34 Multiple occurrences of R6
Keep looking upwards, you will find that R6 are assigned with various initialized objects, as
shown in figure 6-35, figure 6-36, and figure 6-37.
227
Figure 6-35 The assignment of R6
Figure 6-36 The assignment of R6
Figure 6-37 The assignment of R6
228
Figure 6-38 [PSListController tableView:cellForRowAtIndexPath:]
Dichotomy increases the efficiency of our investigation, but it brings a new question:
[PSListController tableView:cellForRowAtIndexPath:] branches a lot, where should we choose
as the investigation starting point to avoid missing any branches? Because [PSListController
tableView:cellForRowAtIndexPath:] will definitely execute code in the red block in figure 6-38,
if we start from this block, we can make sure every branch is investigated. Next let’s investigate
the 1st objc_msgSend in this block, if [$r6 detailTextLabel] contains my number, then we should
investigate upwards, otherwise we should investigate downwards. Take a look at the assembly
in the red block, as shown in figure 6-39.
Figure 6-39 loc_2a9f7966
There are 2 objc_msgSends, so we start from the top one, as shown in figure 6-40.
229
Figure 6-40 Check out address of objc_msgSend
ASLR offset of Preferences is 0x6db3000 as we have just seen it. So the breakpoint should be
set at 0x6db3000 + 0x2A9F797E = 0x317AA97E. Trigger it and see if PSTableCell contains my
number already:
(lldb) br s -a 0x317AA97E
Breakpoint 10: where = Preferences`-[PSListController tableView:cellForRowAtIndexPath:]
+ 922, address = 0x317aa97e
Process 268587 stopped
* thread #1: tid = 0x4192b, 0x317aa97e Preferences`-[PSListController
tableView:cellForRowAtIndexPath:] + 922, queue = ‘com.apple.main-thread, stop reason =
breakpoint 10.1
frame #0: 0x317aa97e Preferences`-[PSListController
tableView:cellForRowAtIndexPath:] + 922
Preferences`-[PSListController tableView:cellForRowAtIndexPath:] + 922:
-> 0x317aa97e: blx 0x31825f04 ; symbol stub for:
____NETRBClientResponseHandler_block_invoke
0x317aa982: mov r2, r0
0x317aa984: movw r0, #59804
0x317aa988: movt r0, #1736
(lldb) po [$r6 detailTextLabel]
<UITableViewLabel: 0x15f7e490; frame = (0 0; 0 0); userInteractionEnabled = NO; layer =
<_UILabelLayer: 0x15fd1c90>>
The cell doesn’t hold my number yet, which indicates that my number is generated after the
red block, i.e. somewhere in the last 3 blocks of code in figure 6-38. Based on this conclusion,
let’s keep executing “ni” command, then “po [$r6 detailTextLabel]” before and after each
objc_msgSend:
(lldb) ni
Process 268587 stopped
* thread #1: tid = 0x4192b, 0x317aa982 Preferences`-[PSListController
tableView:cellForRowAtIndexPath:] + 926, queue = ‘com.apple.main-thread, stop reason =
instruction step over
frame #0: 0x317aa982 Preferences`-[PSListController
tableView:cellForRowAtIndexPath:] + 926
Preferences`-[PSListController tableView:cellForRowAtIndexPath:] + 926:
-> 0x317aa982: mov r2, r0
0x317aa984: movw r0, #59804
0x317aa988: movt r0, #1736
0x317aa98c: add r0, pc
(lldb) po [$r6 detailTextLabel]
230
<UITableViewLabel: 0x15f7e490; frame = (0 0; 0 0); userInteractionEnabled = NO; layer =
<_UILabelLayer: 0x15fd1c90>>
(lldb) ni
……
Process 268587 stopped
* thread #1: tid = 0x4192b, 0x317aa992 Preferences`-[PSListController
tableView:cellForRowAtIndexPath:] + 942, queue = ‘com.apple.main-thread, stop reason =
instruction step over
frame #0: 0x317aa992 Preferences`-[PSListController
tableView:cellForRowAtIndexPath:] + 942
Preferences`-[PSListController tableView:cellForRowAtIndexPath:] + 942:
-> 0x317aa992: blx 0x31825f04 ; symbol stub for:
____NETRBClientResponseHandler_block_invoke
0x317aa996: tst.w r0, #255
0x317aa99a: beq 0x317aa9e6 ; -[PSListController
tableView:cellForRowAtIndexPath:] + 1026
0x317aa99c: movw r0, #60302
(lldb) po [$r6 detailTextLabel]
<UITableViewLabel: 0x15f7e490; frame = (0 0; 0 0); userInteractionEnabled = NO; layer =
<_UILabelLayer: 0x15fd1c90>>
(lldb) ni
Process 268587 stopped
* thread #1: tid = 0x4192b, 0x317aa996 Preferences`-[PSListController
tableView:cellForRowAtIndexPath:] + 946, queue = ‘com.apple.main-thread, stop reason =
instruction step over
frame #0: 0x317aa996 Preferences`-[PSListController
tableView:cellForRowAtIndexPath:] + 946
Preferences`-[PSListController tableView:cellForRowAtIndexPath:] + 946:
-> 0x317aa996: tst.w r0, #255
0x317aa99a: beq 0x317aa9e6 ; -[PSListController
tableView:cellForRowAtIndexPath:] + 1026
0x317aa99c: movw r0, #60302
0x317aa9a0: mov r2, r11
(lldb) po [$r6 detailTextLabel]
<UITableViewLabel: 0x15f7e490; frame = (0 0; 0 0); userInteractionEnabled = NO; layer =
<_UILabelLayer: 0x15fd1c90>>
(lldb) ni
……
Process 268587 stopped
* thread #1: tid = 0x4192b, 0x317aa9ac Preferences`-[PSListController
tableView:cellForRowAtIndexPath:] + 968, queue = ‘com.apple.main-thread, stop reason =
instruction step over
frame #0: 0x317aa9ac Preferences`-[PSListController
tableView:cellForRowAtIndexPath:] + 968
Preferences`-[PSListController tableView:cellForRowAtIndexPath:] + 968:
-> 0x317aa9ac: blx 0x31825f04 ; symbol stub for:
____NETRBClientResponseHandler_block_invoke
0x317aa9b0: movw r0, #60822
0x317aa9b4: mov r2, r11
0x317aa9b6: movt r0, #1736
(lldb) po [$r6 detailTextLabel]
<UITableViewLabel: 0x15f7e490; frame = (0 0; 0 0); userInteractionEnabled = NO; layer =
<_UILabelLayer: 0x15fd1c90>>
(lldb) ni
Process 268587 stopped
* thread #1: tid = 0x4192b, 0x317aa9b0 Preferences`-[PSListController
tableView:cellForRowAtIndexPath:] + 972, queue = ‘com.apple.main-thread, stop reason =
instruction step over
frame #0: 0x317aa9b0 Preferences`-[PSListController
tableView:cellForRowAtIndexPath:] + 972
231
Preferences`-[PSListController tableView:cellForRowAtIndexPath:] + 972:
-> 0x317aa9b0: movw r0, #60822
0x317aa9b4: mov r2, r11
0x317aa9b6: movt r0, #1736
0x317aa9ba: add r0, pc
(lldb) po [$r6 detailTextLabel]
<UITableViewLabel: 0x15f7e490; frame = (0 0; 0 0); userInteractionEnabled = NO; layer =
<_UILabelLayer: 0x15fd1c90>>
(lldb) ni
……
Process 268587 stopped
* thread #1: tid = 0x4192b, 0x317aa9c0 Preferences`-[PSListController
tableView:cellForRowAtIndexPath:] + 988, queue = ‘com.apple.main-thread, stop reason =
instruction step over
frame #0: 0x317aa9c0 Preferences`-[PSListController
tableView:cellForRowAtIndexPath:] + 988
Preferences`-[PSListController tableView:cellForRowAtIndexPath:] + 988:
-> 0x317aa9c0: blx 0x31825f04 ; symbol stub for:
____NETRBClientResponseHandler_block_invoke
0x317aa9c4: movw r0, #4312
0x317aa9c8: movt r0, #1737
0x317aa9cc: add r0, pc
(lldb) po [$r6 detailTextLabel]
<UITableViewLabel: 0x15f7e490; frame = (0 0; 0 0); userInteractionEnabled = NO; layer =
<_UILabelLayer: 0x15fd1c90>>
(lldb) ni
Process 268587 stopped
* thread #1: tid = 0x4192b, 0x317aa9c4 Preferences`-[PSListController
tableView:cellForRowAtIndexPath:] + 992, queue = ‘com.apple.main-thread, stop reason =
instruction step over
frame #0: 0x317aa9c4 Preferences`-[PSListController
tableView:cellForRowAtIndexPath:] + 992
Preferences`-[PSListController tableView:cellForRowAtIndexPath:] + 992:
-> 0x317aa9c4: movw r0, #4312
0x317aa9c8: movt r0, #1737
0x317aa9cc: add r0, pc
0x317aa9ce: ldr r0, [r0]
(lldb) po [$r6 detailTextLabel]
<UITableViewLabel: 0x15f7e490; frame = (0 0; 0 0); text = ‘+86PhoneNumber’;
userInteractionEnabled = NO; layer = <_UILabelLayer: 0x15fd1c90>>
Figure 6-41 The configuration objc_msgSend
As it name suggests, this method refreshes the cell contents with something specific. Let’s
uncover this “something specific”: set a breakpoint at this objc_msgSend, then trigger it and
print its argument:
(lldb) br s -a 0x317AA9C0
Breakpoint 11: where = Preferences`-[PSListController tableView:cellForRowAtIndexPath:]
+ 988, address = 0x317aa9c0
Process 268587 stopped
232
* thread #1: tid = 0x4192b, 0x317aa9c0 Preferences`-[PSListController
tableView:cellForRowAtIndexPath:] + 988, queue = ‘com.apple.main-thread, stop reason =
breakpoint 11.1
frame #0: 0x317aa9c0 Preferences`-[PSListController
tableView:cellForRowAtIndexPath:] + 988
Preferences`-[PSListController tableView:cellForRowAtIndexPath:] + 988:
-> 0x317aa9c0: blx 0x31825f04 ; symbol stub for:
____NETRBClientResponseHandler_block_invoke
0x317aa9c4: movw r0, #4312
0x317aa9c8: movt r0, #1737
0x317aa9cc: add r0, pc
(lldb) p (char *)$r1
(char *) $97 = 0x318362d2 "refreshCellContentsWithSpecifier:"
(lldb) po $r2
My Number ID:myNumberCell 0x170ece60 target:<PhoneSettingsController
0x170ed760: navItem <UINavigationItem: 0x170d0b40>, view <UITableView: 0x16acb200; frame
= (0 0; 320 568); autoresize = W+H; gestureRecognizers = <NSArray: 0x15d232d0>; layer =
<CALayer: 0x15fc9110>; contentOffset: {0, -64}; contentSize: {320, 717.5}>>
(lldb) po [$r2 class]
PSSpecifier
As the output shows, “something specific”, i.e. specifier, is a PSSpecifier object, and it’s
tightly related to my number. If you have carefully read the preferences specifier plist standard
in section PreferenceBundle of the last chapter, you would know that the contents of a
PSTableCell are specified by a PSSpecfier. Further more, we can acquire the setter and getter of
PSSpecifier through [PSSpecifier propertyForKey:@“set”] and [PSSpecifier
propertyForKey:@“get”] like this:
(lldb) po [$r2 propertyForKey:@"set"]
setMyNumber:specifier:
(lldb) po [$r2 propertyForKey:@"get"]
myNumber:
233
Figure 6-42 [PhoneSettingsController myNumber:]
Now, press home button to quit Preferences, then terminate it completely and make sure
it’s not running in the background. After that, launch it again and don’t enter
MobilePhoneSettings for now, let’s test this method again:
FunMaker-5:~ root# cycript -p Preferences
cy# [[PhoneSettingsTelephony telephony] myNumber]
ReferenceError: Can’t find variable: PhoneSettingsTelephony
Figure 6-43 [PhoneSettingsTelephony myNumber]
This method is also very simple. It judges if the instance variable _myNumber is nil; if not,
branches left and records “My Number requested, returning cached value: %@”, namely,
returns a data in cache; or else branches right, first get my number by calling
PhoneSettingsCopyMyNumber, then records “My Number requested, no cached value, fetched:
%@”, namely, my number is not in cache, so it returns a newly fetched data. In consequence,
PhoneSettingsCopyMyNumber is able to get my number, but as its name suggests, it is still a
function inside MobilePhoneSettings.bundle, we can’t call it from outside this bundle. We’re
one step further, but not far enough. Let’s continue by digging into
PhoneSettingsCopyMyNumber, as shown in figure 6-44.
235
Figure 6-44 PhoneSettingsCopyMyNumber
This snippet first calls CTSettingCopyMyPhoneNumber and autoreleases the return value,
then calls PhoneSettingsCopyFormattedNumberBySIMCountry, which seems to format the
phone number according to the country of the SIM card. Judging from the name and context,
CTSettingCopyMyPhoneNumber looks like the target function we are looking for. And the
prefix CT implies that it comes from CoreTelephony rather than MobilePhoneSettings. Double
click this function to see its implementation, as shown in figure 6-45.
Figure 6-45 CTSettingCopyMyPhoneNumber
236
branches right, R0 is permanently 0, if R0 is an argument, it’s meaningless. Therefore,
PhoneSettingsCopyMyNumber doesn’t seem to have any argument either. To play it safe, let’s
reconfirm our guesses by checking the implementation of CTSettingCopyMyPhoneNumber in
CoreTelephony, as shown in figure 6-46.
Figure 6-46 CTSettingCopyMyPhoneNumber
237
Figure 6-47 CTSettingCopyMyPhoneNumber
Then quit Preferences and terminate it completely in the background, then relaunch it and
don’t enter MobilePhoneSettings. Next attach debugserver to it and take a look at
CoreTelephony’s ASLR offset with LLDB:
(lldb) image list -o -f
[ 0] 0x000b3000
/private/var/db/stash/_.29LMeZ/Applications/Preferences.app/Preferences(0x00000000000b70
00)
[ 1] 0x0026c000 /Library/MobileSubstrate/MobileSubstrate.dylib(0x000000000026c000)
[ 2] 0x06db3000 /Users/snakeninny/Library/Developer/Xcode/iOS DeviceSupport/8.1
(12B411)/Symbols/System/Library/PrivateFrameworks/BulletinBoard.framework/BulletinBoard
[ 51] 0x06db3000 /Users/snakeninny/Library/Developer/Xcode/iOS DeviceSupport/8.1
(12B411)/Symbols/System/Library/Frameworks/CoreTelephony.framework/CoreTelephony
……
The breakpoint should be set at 0x6db3000 + 0x2226763A = 0x2901A63A, right? Then enter
MobilePhoneSettings to trigger the breakpoint:
(lldb) br s -a 0x2901A63A
Breakpoint 1: where = CoreTelephony`CTSettingCopyMyPhoneNumber + 78, address =
0x2901a63a
Process 330210 stopped
* thread #1: tid = 0x509e2, 0x2901a63a CoreTelephony`CTSettingCopyMyPhoneNumber + 78,
queue = ‘com.apple.main-thread, stop reason = breakpoint 1.1
frame #0: 0x2901a63a CoreTelephony`CTSettingCopyMyPhoneNumber + 78
CoreTelephony`CTSettingCopyMyPhoneNumber + 78:
-> 0x2901a63a: add sp, #28
0x2901a63c: pop.w {r8, r10, r11}
0x2901a640: pop {r4, r5, r6, r7, pc}
0x2901a642: nop
(lldb) po $r0
+86PhoneNumber
(lldb) po [$r0 class]
__NSCFString
This is our target function, as well the data source of PSTableCell. We’ve finally found it
through analyzing the call chain of [PhoneSettingsController
tableView:cellForRowAtIndexPath:], hurray! Just remember to release the return value when
you make use of this function. At last, let’s write a tweak to test this function.
238
NIC 2.0 - New Instance Creator
------------------------------
[1.] iphone/application
[2.] iphone/cydget
[3.] iphone/framework
[4.] iphone/library
[5.] iphone/notification_center_widget
[6.] iphone/preference_bundle
[7.] iphone/sbsettingstoggle
[8.] iphone/tool
[9.] iphone/tweak
[10.] iphone/xpc_service
Choose a Template (required): 9
Project Name (required): iOSREGetMyNumber
Package Name [com.yourcompany.iosregetmynumber]: com.iosre.iosregetmynumber
Author/Maintainer Name [snakeninny]: snakeninny
[iphone/tweak] MobileSubstrate Bundle filter [com.apple.springboard]:
com.apple.Preferences
[iphone/tweak] List of applications to terminate upon installation (space-separated, ‘-’
for none) [SpringBoard]: Preferences
Instantiating iphone/tweak in iosregetmynumber/...
Done.
%hook PreferencesAppController
- (BOOL)application:(id)arg1 didFinishLaunchingWithOptions:(id)arg2
{
BOOL result = %orig;
NSLog(@"iOSRE: my number = %@", [CTSettingCopyMyPhoneNumber() autorelease]);
return result;
}
%end
include theos/makefiles/common.mk
TWEAK_NAME = iOSREGetMyNumber
iOSREGetMyNumber_FILES = Tweak.xm
iOSREGetMyNumber_FRAMEWORKS = CoreTelephony # CTSettingCopyMyPhoneNumber is from here
include $(THEOS_MAKE_PATH)/tweak.mk
after-install::
install.exec "killall -9 Preferences"
239
Name: iOSREGetMyNumber
Depends: mobilesubstrate, firmware (>= 8.0)
Version: 1.0
Architecture: iphoneos-arm
Description: Get my number just like MobilePhoneSettings!
Maintainer: snakeninny
Author: snakeninny
Section: Tweaks
Homepage: http://bbs.iosre.com
4. Test
Compile and install the tweak on iOS, then launch Preferences without entering
MobilePhoneSettings. After that, ssh into iOS and take a look at the syslog:
FunMaker-5:~ root# grep iOSRE: /var/log/syslog
Nov 29 23:23:01 FunMaker-5 Preferences[2078]: iOSRE: my number = +86PhoneNumber
5. P.S.
I have set the region of my iPhone 5 to US, so
PhoneSettingsCopyFormattedNumberBySIMCountry has formatted my number from
“+86PhoneNumber” to “+86 Pho-neNu-mber”, which is the American phone number format.
You’ll run into CTSettingCopyMyPhoneNumber more frequently as you reverse more.
Actually, the prototype of CTSettingCopyMyPhoneNumber should be:
CFStringRef CTSettingCopyMyPhoneNumber(void);
Since NSString * and CFStringRef are toll-free bridged, our prototype is OK.
Because there is a keyword “copy” in the name of CTSettingCopyMyPhoneNumber and it
returns a CoreData object, we are responsible to release the return value according to Apple’s
“Ownership Policy”.
In this section, we have shed considerable light to refine “locate target functions” with ARM
level reverse engineering and enhanced the methodology of writing a tweak. Specifically, we’ve
divided “locate target functions” into 2 steps, i.e. “cut into the target App and find the UI
function” and “locate the target function from the UI function”. By combining Cycript, IDA and
LLDB, we have not only located the target functions, but also analyzed their arguments and
return values to reconstruct their prototypes. The methodology we used in the examples can
work on at least 95% of all Apps; however, if you unfortunately encounter those 5%, please
share and discuss with us on http://bbs.iosre.com.
240
6.3 Advanced LLDB usage
I bet the last section has opened a new chapter of iOS reverse engineering for you. The
combination of IDA and LLDB can easily beat them all, and with the help of ARM architecture
reference manual, you can conquer almost all Apps. I know you’re already desperate to practice
what you have just learned.
Hold your horses for now. Although the examples in section 6.2 have synthetically made
use of IDA and LLDB, they haven’t covered LLDB’s common usage yet. In the next section,
we’ll go over some short LLDB examples for a better comprehension, which can greatly reduce
our workload in practice.
#include <stdio.h>
#include <dlfcn.h>
#import <Foundation/Foundation.h>
241
Save this snippet as a file named main.m, and compile it with the sentence in the comments.
Drag and drop MainBinary into IDA, and then check the cross references of NSLog, as shown in
figure 6-48.
Figure 6-48 Check the cross references of NSLog
As we can see, NSLog appears in 4 functions. If we see “iOSRE: 0” in syslog when we are
reversing, how can we know which NSLog it’s from? When there’re only tens lines of code, we
can figure out by hand that only TestFunction3 is called, and it further calls NSLog. What if
there are 20 TestFunctions that are called by 8 separate functions? When the amount of code
increases, it’ll be too complicate to analyze manually. If we want to find the caller of NSLog
under such circumstances, LLBD will be very helpful. Generally, there are 2 main methods.
• Inspect LR
Still remember LR register introduced in section 6.1? Its function is to save the return
address of a function. So what’s a return address? Take an example:
void FunctionA()
{
……
FunctionB();
……
}
In the above pseudo code, FunctionA calls FunctionB, while A and B are located in 2
different memory areas, and their addresses have no direct connection. After the execution of B,
the process needs to go back to A to continue execution, as shown in figure 6-49.
242
Figure 6-49 An illustration of return address
The address that the process returns to after the execution of FunctionB, is the return
address, i.e. LR. Because it’s inside FunctionB’s caller, if we know the value of LR we can track
to the caller. Let’s explain this theory with an example. Drag and drop Foundation.framework’s
binary into IDA; locate to NSLog after the initial analysis, and check out its base address, as
shown in figure 6-50.
Figure 6-50 Check out NSLog’s base address
Its base address is 0x2261ab94, we will set a breakpoint on it shortly and print out the value
of LR. Next, launch MainBinary with debugserver:
FunMaker-5:~ root# debugserver -x backboard *:1234 /var/tmp/MainBinary
debugserver-@(#)PROGRAM:debugserver PROJECT:debugserver-320.2.89
for armv7.
Listening to port 1234 for a connection from *...
243
dyld`_dyld_start:
-> 0x1fec7000: mov r8, sp
0x1fec7004: sub sp, sp, #16
0x1fec7008: bic sp, sp, #7
0x1fec700c: ldr r3, [pc, #112] ; _dyld_start + 132
(lldb) image list -f
[ 0] /Users/snakeninny/Library/Developer/Xcode/iOS DeviceSupport/8.1
(12B411)/Symbols/usr/lib/dyld
Right at this moment, MainBinary is not run yet, and we are inside dyld. Next, keep
entering “ni” until LLDB outputs “error: invalid thread”:
(lldb) ni
Process 450336 stopped
* thread #1: tid = 0x6df20, 0x1fec7004 dyld`_dyld_start + 4, stop reason = instruction
step over
frame #0: 0x1fec7004 dyld`_dyld_start + 4
dyld`_dyld_start + 4:
-> 0x1fec7004: sub sp, sp, #16
0x1fec7008: bic sp, sp, #7
0x1fec700c: ldr r3, [pc, #112] ; _dyld_start + 132
0x1fec7010: sub r0, pc, #8
(lldb)
Process 450336 stopped
* thread #1: tid = 0x6df20, 0x1fec7008 dyld`_dyld_start + 8, stop reason = instruction
step over
frame #0: 0x1fec7008 dyld`_dyld_start + 8
dyld`_dyld_start + 8:
-> 0x1fec7008: bic sp, sp, #7
0x1fec700c: ldr r3, [pc, #112] ; _dyld_start + 132
0x1fec7010: sub r0, pc, #8
0x1fec7014: ldr r3, [r0, r3]
……
(lldb)
error: invalid thread
No more “ni” when the error occurs; now dyld begins to load MainBinary. Wait a moment,
the process will stop again, and we are inside MainBinary, it’s okay to debug then:
Process 450336 stopped
* thread #1: tid = 0x6df20, 0x1fec7040 dyld`_dyld_start + 64, queue = ‘com.apple.main-
thread, stop reason = instruction step over
frame #0: 0x1fec7040 dyld`_dyld_start + 64
dyld`_dyld_start + 64:
-> 0x1fec7040: ldr r5, [sp, #12]
0x1fec7044: cmp r5, #0
0x1fec7048: bne 0x1fec7054 ; _dyld_start + 84
0x1fec704c: add sp, r8, #4
244
As usual, we should set the breakpoint at 0x6db3000 + 0x2261ab94 = 0x293CDB94. Execute
“c” to trigger the breakpoint:
(lldb) br s -a 0x293CDB94
Breakpoint 1: where = Foundation`NSLog, address = 0x293cdb94
(lldb) c
Process 450336 resuming
Process 450336 stopped
* thread #1: tid = 0x6df20, 0x293cdb94 Foundation`NSLog, queue = ‘com.apple.main-thread,
stop reason = breakpoint 1.1
frame #0: 0x293cdb94 Foundation`NSLog
Foundation`NSLog:
-> 0x293cdb94: sub sp, #12
0x293cdb96: push {r7, lr}
0x293cdb98: mov r7, sp
0x293cdb9a: sub sp, #4
Because the base address of MainBinary is 0x000fc000, open MainBinary in IDA and jump to
0x107f8d - 0xfc000 = 0xBF8D, as shown in figure 6-51.
Figure 6-51 TestFunction3
0xBF8D is right below “BLX _NSLog”, so we have found the caller of NSLog. One thing
should be noted is that because LR may change in the caller, the breakpoint should be set at the
base address. Pretty easy, huh?
Its ASLR offset is 0x6db3000. According to figure 6-50, the address of the last instruction of
NSLog is 0x2261ABB6, so set a breakpoint at 0x6db3000 + 0x2261ABB6 = 0x293CDBB6, then
enter “c” to trigger the breakpoint:
(lldb) br s -a 0x293CDBB6
Breakpoint 1: where = Foundation`NSLog + 34, address = 0x293cdbb6
(lldb) c
Process 452269 resuming
(lldb) 2014-11-30 23:45:37.070 MainBinary[3454:452269] iOSRE: 1
Process 452269 stopped
* thread #1: tid = 0x6e6ad, 0x293cdbb6 Foundation`NSLog + 34, queue = ‘com.apple.main-
thread, stop reason = breakpoint 1.1
frame #0: 0x293cdbb6 Foundation`NSLog + 34
Foundation`NSLog + 34:
-> 0x293cdbb6: bx lr
Foundation`NSLogv:
0x293cdbb8: push {r4, r5, r6, r7, lr}
0x293cdbba: add r7, sp, #12
0x293cdbbc: sub sp, #12
Notice the texts above “->“, it implies the present image. Keep executing “ni”:
(lldb) ni
Process 452269 stopped
* thread #1: tid = 0x6e6ad, 0x00017fa6 MainBinary`main + 22, queue = ‘com.apple.main-
thread, stop reason = instruction step over
frame #0: 0x00017fa6 MainBinary`main + 22
MainBinary`main + 22:
-> 0x17fa6: movs r0, #0
0x17fa8: movt r0, #0
0x17fac: add sp, #12
0x17fae: pop {r7, pc}
Here comes MainBinary and the process stops at 0x17fa6. 0x17fa6 – 0xc000 = 0xbfa6, so
again, we have found NSLog’s caller TestFunction3 according to figure 6-51.
Both methods are simple and direct; choose whatever you like.
246
6.3.2 Change process execution flow
Why do we need to change process execution flow? Commonly it’s because the code we
want to debug could only be executed in specific conditions, which are hard to meet in the
original execution flow. Under such circumstances, we have to change the flow to redirect the
process to execute the target code for debugging. Reads awkward? Let’s see an example.
// clang -arch armv7 -isysroot `xcrun --sdk iphoneos --show-sdk-path` -framework
Foundation -framework UIKit -o MainBinary main.m
#include <stdio.h>
#include <dlfcn.h>
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
Save this snippet as main.m, and compile it with the sentence in the comments, then copy
MainBinary to “/var/tmp/” on iOS:
snakeninnys-MacBook:6 snakeninny$ scp MainBinary root@iOSIP:/var/tmp/
MainBinary 100% 49KB
48.6KB/s 00:00
Run it:
FunMaker-5:~ root# /var/tmp/MainBinary
FunMaker-5:~ root#
Because I’m using iOS 8.1, there is no output for sure. What if I am interested in
ImportantAndComplicatedFunction but don’t have iOS 8.1.1 in hand? Then I have to
dynamically change the execution flow to make this function get called. I’ll show you how,
please keep focused. Drag and drop MainBinary into IDA, then locate to the branch before
ImportantAndComplicatedFunction, as shown in figure 6-52.
247
Figure 6-52 Before ImportantAndComplicatedFunction
Because the address of “CMP R0, #0” in figure 6-52 is 0xBF46, the breakpoint should be set
at 0xbf46 + 0xe000 = 0x19F46. Trigger it with “c”, and print R0:
(lldb) br s -a 0x19F46
Breakpoint 1: where = MainBinary`main + 134, address = 0x00019f46
(lldb) c
Process 456316 resuming
Process 456316 stopped
* thread #1: tid = 0x6f67c, 0x00019f46 MainBinary`main + 134, queue = ‘com.apple.main-
thread, stop reason = breakpoint 1.1
frame #0: 0x00019f46 MainBinary`main + 134
MainBinary`main + 134:
-> 0x19f46: cmp r0, #0
0x19f48: beq 0x19f4e ; main + 142
0x19f4a: bl 0x19ea4 ; ImportantAndComplicatedFunction
0x19f4e: movs r0, #0
(lldb) p $r0
(unsigned int) $0 = 0
As we can see, we’ve changed the process execution flow by modifying the value of a
register, thus achieved our goal.
248
6.4 Conclusion
The combination of IDA and LLDB is far more powerful than what we have introduced in
this chapter, their usage ranges from App analysis to jailbreak, showing their omnipotence.
Nonetheless, in the beginning stage of iOS reverse engineering, their usage is not likely to
exceed the scope of this book. As soon as you can use them proficiently, your understanding of
iOS would rise to a new level and you’ll be able to summarize your own methodologies.
There’re still lots and lots of topics in ARM related iOS reverse engineering to further explore,
and we’re unable to cover them all in one book. Therefore, we will leave them to
http://bbs.iosre.com, please stay focused.
To be honest, this chapter is rather difficult to understand, but this is the only path to be a
real iOS reverse engineer. In part 4 of this book, we will turn methodologies in part 3 into
practices and write 4 tweaks. I hope you know from the bottom of your heart whether you are
talented enough to be an iOS reverse engineer after finishing all 4 practices. As Steve Jobs said,
“It’s more fun to be a pirate than to join the Navy”. IMHO, being an iOS reverse engineer is way
more fun than being just an App developer, but after all, it’s up to you. Good luck!
249
IV
Practices
The first 3 parts of this book have introduced the concepts, tools and theories of iOS reverse
engineering, along with examples to give you a better understanding of them. I believe you
have the same feeling that only if concepts, tools and theories are combined together can we get
the best out reverse engineering.
So far, you may still feel unsatisfied with the fragmented and conservative examples. So in
this part, we’ve prepared 4 original and serialized examples to show you the combination of
concepts, tools and theories. They are:
250
7
Practice 1: Characount for Notes 8
7.1 Notes
I bet Notes App (hereafter referred to as Notes) is one of your most familiar iOS Apps. Its UI
and functionality have experienced very few changes since iOS came out. The simplicity and
convenience of Notes win my heart, all my secrets are sealed in it, as shown in figure 7-1.
Figure 7- 1 Notes
Being a power user of Notes, not only do I save secrets in it, but also compose SMS or
tweets in it. Since there is word limit on SMS and tweets, I really wish Notes can display each
note’s character count as a reminder. DIY is a born spirit of reverse engineers, so I’ve developed
Characount for Notes, which is one of my daily necessities on iOS 6. It’s not a difficult tweak,
hence can be a stepping-stone for beginners like you. Our goal in this chapter is to rewrite
251
Characount for Notes on iOS 8, and all the following operations are performed on iPhone 5, iOS
8.1.
If we’re to choose a place to display the character count of this note, where do you think
looks better? If you used to be an iOS 6 user, do you remember that each note has a centered
title as shown in figure 7-3?
252
Figure 7- 3 Note browsing view on iOS 6
However, Notes on iOS 8 has removed the title, leaving a blank navigation bar. Why don’t
we just display the character count here, as shown in figure 7-4?
253
Figure 7- 4 Note browsing view with a title
It looks good! So, what exactly should we do to make Notes look like this? Hope you
remember the saying in chapter 5 that everything you see on iOS is an object. Keep that in mind
and think with me:
Every note is an object, and note browsing view contains the content and modification time
of a note object. Since note browsing view is a subclass of UIView, we can trace back to its view
controller via nextResponder, and further access all note concerned data via its view controller
according to MVC design pattern. With the note data, we can initialize the character count
when this view appears.
While we are editing a note, a “Done” button will appear on the right side of the navigation
bar, as shown in figure 7-5.
254
Figure 7- 5 “Done” button
After tapping “Done”, the current note is saved. This phenomenon indicates that a note is
not saved in real time during editing, or we just don’t need this button at all. Of course,
character count changes instantly with the editing content would be the ideal visual effect, so to
accomplish this goal, we need to find a specific method which monitors the changes of the
current note. In addition, we should be able to get the character count of this note and update
the title just in time within this method. Because this kind of methods are usually defined in
protocols, we should keep an eye on protocols in Notes.
Suppose we can get the current note’s character count, how do we put it on the navigation
bar? Usually, the note browsing view controller inherits from UIViewController, which
possesses a “title” property. So, “setTitle:” is the answer.
If we managed to solve these 3 problems, there’ll be no more technical difficulties for
Characount for Notes. Code speaks louder than words, let’s move it!
255
dumpdecrypted section? Yeah, it’s ps command again: first close all Apps, then open Notes and
ssh to iOS to list all system processes with ps:
FunMaker-5:~ root# ps -e | grep /Applications/
592 ?? 0:37.70 /Applications/MobileMail.app/MobileMail
761 ?? 0:02.78
/Applications/MessagesNotificationViewService.app/MessagesNotificationViewService
1807 ?? 0:00.55
/private/var/db/stash/_.29LMeZ/Applications/MobileSafari.app/webbookmarksd
2016 ?? 0:05.23 /Applications/InCallService.app/InCallService
2619 ?? 0:02.66 /Applications/MobileSMS.app/MobileSMS
2672 ?? 0:01.20 /Applications/MobileNotes.app/MobileNotes
2678 ttys000 0:00.01 grep /Applications/
Among those processes, MobileNotes attracts us most. How to verify our guess? We can
simply kill it and see whether Notes quit.
FunMaker-5:~ root# killall MobileNotes
We’ve got 88 headers in total. Let’s take a brief look to see what we can discover, as shown
in figure 7-6.
Do you see the selected file in figure 7-6? I am not sure if it is a key clue of this chapter for
now, but we’ll see.
256
7.2.3 Find the controller of note browsing view using Cycript
Again, recursiveDescription makes our days:
FunMaker-5:~ root# cycript -p MobileNotes
cy# ?expand
expand == true
cy# [[UIApp keyWindow] recursiveDescription]
@"<UIWindow: 0x17688db0; frame = (0 0; 320 568); gestureRecognizers = <NSArray:
0x17689620>; layer = <UIWindowLayer: 0x17688fc0>>
| <UILayoutContainerView: 0x175bb880; frame = (0 0; 320 568); autoresize = W+H; layer
= <CALayer: 0x175bb900>>
| | <UILayoutContainerView: 0x17699350; frame = (0 0; 320 568); clipsToBounds =
YES; gestureRecognizers = <NSArray: 0x1769cf60>; layer = <CALayer: 0x17699530>>
| | | <UINavigationTransitionView: 0x176564c0; frame = (0 0; 320 568);
clipsToBounds = YES; autoresize = W+H; layer = <CALayer: 0x17658ec0>>
| | | | <UIViewControllerWrapperView: 0x176d13b0; frame = (0 0; 320 568);
layer = <CALayer: 0x176d1530>>
| | | | | <UILayoutContainerView: 0x1769dd80; frame = (0 0; 320 568);
clipsToBounds = YES; gestureRecognizers = <NSArray: 0x176a16f0>; layer = <CALayer:
0x1769de00>>
| | | | | | <UINavigationTransitionView: 0x1769ebb0; frame = (0 0; 320
568); clipsToBounds = YES; autoresize = W+H; layer = <CALayer: 0x1769ec40>>
| | | | | | | <UIViewControllerWrapperView: 0x175109e0; frame = (0
0; 320 568); layer = <CALayer: 0x175109b0>>
| | | | | | | | <NotesBackgroundView: 0x175ee3e0; frame = (0 0;
320 568); gestureRecognizers = <NSArray: 0x17510a70>; layer = <CALayer: 0x175ee580>>
| | | | | | | | | <NotesTextureBackgroundView: 0x175ee5b0;
frame = (0 0; 320 568); clipsToBounds = YES; layer = <CALayer: 0x175ee630>>
| | | | | | | | | | <NotesTextureView: 0x175ee940; frame =
(0 -64; 320 640); layer = <CALayer: 0x175ee9c0>>
| | | | | | | | | <NoteContentLayer: 0x176c5110; frame = (0
0; 320 568); layer = <CALayer: 0x176ca850>>
| | | | | | | | | | <UIView: 0x175f2130; frame = (16 0;
288 0); hidden = YES; layer = <CALayer: 0x175dd2b0>>
| | | | | | | | | | <NotesScrollView: 0x175f2a10;
baseClass = UIScrollView; frame = (0 0; 320 568); clipsToBounds = YES;
gestureRecognizers = <NSArray: 0x175f1b70>; layer = <CALayer: 0x175f28d0>;
contentOffset: {0, -64}; contentSize: {320, 460}>
| | | | | | | | | | | <UIView: 0x175f09a0; frame = (0
0; 320 0); layer = <CALayer: 0x175f2790>>
| | | | | | | | | | | <UIView: 0x175f27e0; frame = (0
0; 0 460); layer = <CALayer: 0x175f2850>>
| | | | | | | | | | | <NoteDateLabel: 0x175f3400;
baseClass = UILabel; frame = (69 5.5; 182 18); text = 'November 24, 2014, 20:44';
userInteractionEnabled = NO; layer = <_UILabelLayer: 0x175f3560>>
| | | | | | | | | | | <NoteTextView: 0x175ee3e0;
baseClass = _UICompatibilityTextView; frame = (6 28; 308 418); text = 'Secret';
clipsToBounds = YES; gestureRecognizers = <NSArray: 0x176c7ed0>; layer = <CALayer:
0x176d88e0>; contentOffset: {0, 0}; contentSize: {308, 52}>
……
Okay, NoteDisplayController is the one. Let’s see if setTitle: really changes the title of note
browsing view:
cy# [#0x17edc340 setTitle:@"Characount = Character count"]
258
After going over this large header, we’ve found a property of NoteObject type. Since a note
is exactly an object, NoteObject seems to be too obvious to believe… Hehe, let’s print it in
Cycript:
cy# [#0x17edc340 note]
#'<NoteObject: 0x176aa170> (entity: Note; id: 0x176a9040 <x-coredata://4B88CC7C-7A5F-
4F15-9275-53C6D0ABE0C3/Note/p15> ; data: {\n attachments = (\n );\n author
= nil;\n body = "0x176a8b20 <x-coredata://4B88CC7C-7A5F-4F15-9275-
53C6D0ABE0C3/NoteBody/p15>";\n containsCJK = 0;\n contentType = 0;\n
creationDate = "2014-11-24 05:00:59 +0000";\n deletedFlag = 0;\n externalFlags =
0;\n externalSequenceNumber = 0;\n externalServerIntId = "-4294967296";\n guid
= "781B6C87-2855-4512-8864-50618754333A";\n integerId = 3865;\n isBookkeepingEntry
= 0;\n modificationDate = "2014-11-24 12:44:08 +0000";\n serverId = nil;\n
store = "0x175a2b60 <x-coredata://4B88CC7C-7A5F-4F15-9275-53C6D0ABE0C3/Store/p1>";\n
summary = nil;\n title = Secret;\n})'
Needless to say, NoteObject is exactly the current note. Each field in the description is
explicit, let’s take a look at its header:
@interface NoteObject : NSManagedObject
{
}
- (BOOL)belongsToCollection:(id)arg1;
@property(nonatomic) unsigned long long sequenceNumber;
- (BOOL)containsAttachments;
@property(retain, nonatomic) NSString *externalContentRef;
@property(retain, nonatomic) NSData *externalRepresentation;
@property(readonly, nonatomic) BOOL hasValidServerIntId;
@property(nonatomic) long long serverIntId;
@property(nonatomic) unsigned long long flags;
@property(readonly, nonatomic) NSURL *noteId;
@property(readonly, nonatomic) BOOL isBeingMarkedForDeletion;
@property(readonly, nonatomic) BOOL isMarkedForDeletion;
- (void)markForDeletion;
@property(nonatomic) BOOL isPlainText;
- (id)contentAsPlainTextPreservingNewlines;
@property(readonly, nonatomic) NSString *contentAsPlainText;
@property(retain, nonatomic) NSString *content;
// Remaining properties
@property(retain, nonatomic) NSSet *attachments; // @dynamic attachments;
@property(retain, nonatomic) NSString *author; // @dynamic author;
@property(retain, nonatomic) NoteBodyObject *body; // @dynamic body;
@property(retain, nonatomic) NSNumber *containsCJK; // @dynamic containsCJK;
@property(retain, nonatomic) NSNumber *contentType; // @dynamic contentType;
@property(retain, nonatomic) NSDate *creationDate; // @dynamic creationDate;
@property(retain, nonatomic) NSNumber *deletedFlag; // @dynamic deletedFlag;
@property(retain, nonatomic) NSNumber *externalFlags; // @dynamic externalFlags;
@property(retain, nonatomic) NSNumber *externalSequenceNumber; // @dynamic
externalSequenceNumber;
@property(retain, nonatomic) NSNumber *externalServerIntId; // @dynamic
externalServerIntId;
@property(readonly, retain, nonatomic) NSString *guid; // @dynamic guid;
@property(retain, nonatomic) NSNumber *integerId; // @dynamic integerId;
@property(retain, nonatomic) NSNumber *isBookkeepingEntry; // @dynamic
isBookkeepingEntry;
@property(retain, nonatomic) NSDate *modificationDate; // @dynamic modificationDate;
259
@property(retain, nonatomic) NSString *serverId; // @dynamic serverId;
@property(retain, nonatomic) NoteStoreObject *store; // @dynamic store;
@property(retain, nonatomic) NSString *summary; // @dynamic summary;
@property(retain, nonatomic) NSString *title; // @dynamic title;
@end
Great! Lots of properties indicate that NoteObject is a very standard model. How do we get
its text? Among its properties, we can see a possible property named contentAsPlainText. Let’s
check what it is:
cy# [#0x176aa170 contentAsPlainText]
@"Secret"
For further confirmation, let’s change the text of this note and add a picture, as shown in
figure 7-8.
Now we’re certain that this method can correctly return the text of the current note. With a
further length method, we’re able to get the character count of this note:
cy# [[#0x176aa170 contentAsPlainText] length]
13
260
7.2.5 Find a method to monitor note text changes in real time
At the beginning of this chapter we’ve mentioned that “this kind of methods are usually
defined in protocols”. Because both setTitle: and NoteObject are found in
NotesDisplayController.h, if we can find the “monitor” method inside this header too, our code
will be greatly simplified. Open NotesDisplayController.h and check what protocols it has
implemented.
@interface NotesDisplayController : UIViewController <NoteContentLayerDelegate,
UIActionSheetDelegate, AFContextProvider, UIPopoverPresentationControllerDelegate,
UINavigationControllerDelegate, UIImagePickerControllerDelegate,
NotesQuickLookActivityItemDelegate, ScrollViewKeyboardResizerDelegate,
NSUserActivityDelegate, NotesStateArchiving>
……
@end
@optional
- (void)noteContentLayerKeyboardDidHide:(id)arg1;
@end
261
and size of it are indeed changing, thus those 2 methods may be called when changes occur, and
actually they’re implemented in NotesDisplayController.h. Let’s use LLDB to make sure they’re
called when a note changes.
Attach to MobileNotes with LLDB, and check its ASLR offset:
(lldb) image list -o -f
[ 0] 0x00035000
/private/var/db/stash/_.29LMeZ/Applications/MobileNotes.app/MobileNotes(0x00000000000390
00)
[ 1] 0x00197000 /Library/MobileSubstrate/MobileSubstrate.dylib(0x0000000000197000)
[ 2] 0x06db3000 /Users/snakeninny/Library/Developer/Xcode/iOS DeviceSupport/8.1
(12B411)/Symbols/System/Library/Frameworks/QuickLook.framework/QuickLook
……
The ASLR offset is 0x35000. Drag and drop MobileNotes into IDA, then check the base
addresses of [NotesDisplayController noteContentLayer:didChangeContentSize:] and
[NotesDisplayController noteContentLayerContentDidChange:updatedTitle:] after the initial
analysis, as shown in figure 7-9 and figure 7-10.
The base addresses are 0x16E70 and 0x1AEB8 respectively, so breakpoints should be set at
0x4BE70 and 0x4FEB8. Then try to edit a note and see whether these breakpoints are triggered:
(lldb) br s -a 0x4BE70
Breakpoint 1: where = MobileNotes`___lldb_unnamed_function382$$MobileNotes, address =
0x0004be70
(lldb) br s -a 0x4FEB8
Breakpoint 2: where = MobileNotes`___lldb_unnamed_function458$$MobileNotes, address =
0x0004feb8
Great eyes see alike: These two breakpoints are hit a lot! The reason a protocol method gets
called is generally that the corresponding event mentioned in the method name happened. And
262
what triggers the event is usually the method’s arguments. In this case, [NotesDisplayController
noteContentLayer:didChangeContentSize:] and [NotesDisplayController
noteContentLayerContentDidChange:updatedTitle:] are called because didChangeContentSize
and ContentDidChange events happened, and content itself is probably the arguments of both
methods. Let’s verify our guess in LLDB.
(lldb) br com add 1
Enter your debugger command(s). Type 'DONE' to end.
> po $r2
> c
> DONE
(lldb) br com add 2
Enter your debugger command(s). Type 'DONE' to end.
> po $r2
> c
> DONE
(lldb) c
263
> c
> DONE
(lldb) br com add 2
Enter your debugger command(s). Type 'DONE' to end.
> po [$r2 textView]
> c
> DONE
Continue editing this note and keep watching the output. Our editing shows in the output
in real time:
Process 24577 resuming
Command #2 'c' continued the target.
<NoteTextView: 0x15aace00; baseClass = _UICompatibilityTextView; frame = (6 28; 308
209); text = 'Secre'; clipsToBounds = YES; gestureRecognizers = <NSArray: 0x14eddfc0>;
layer = <CALayer: 0x14ee7da0>; contentOffset: {0, 0}; contentSize: {308, 52}>
Process 24577 resuming
Command #2 'c' continued the target.
<NoteTextView: 0x15aace00; baseClass = _UICompatibilityTextView; frame = (6 28; 308
209); text = 'Secret'; clipsToBounds = YES; gestureRecognizers = <NSArray: 0x14eddfc0>;
layer = <CALayer: 0x14ee7da0>; contentOffset: {0, 0}; contentSize: {308, 52}>
There’s not much content in this header, and there’re only 2 delegates with the keyword
“text”. Obviously, delegates don’t return NSString objects. If we cannot get text in
NoteTextView, it gets to be in NoteTextView’s super class. Open _UICompatibilityTextView
then:
@interface _UICompatibilityTextView : UIScrollView <UITextLinkInteraction, UITextInput>
……
@property(nonatomic) int textAlignment;
@property(copy, nonatomic) NSString *text;
- (BOOL)hasText;
@property(retain, nonatomic) UIColor *textColor;
@property(retain, nonatomic) UIFont *font;
@property(copy, nonatomic) NSAttributedString *attributedText;
……
OK, here comes NSString *text. Let’s use LLDB for a final confirmation:
(lldb) br com add 1
Enter your debugger command(s). Type 'DONE' to end.
264
> po [[$r2 textView] text]
> c
> DONE
(lldb) br com add 2
Enter your debugger command(s). Type 'DONE' to end.
> po [[$r2 textView] text]
> c
> DONE
Secret
Process 24577 resuming
Command #2 'c' continued the target.
Secret i
Process 24577 resuming
Command #2 'c' continued the target.
By now, we’ve successfully found 2 methods to monitor note text changes in real time, you
can choose either of them, and [NotesDisplayController
noteContentLayerContentDidChange:updatedTitle:] is my choice. All 3 previous problems are
solved, iOS reverse engineering is way easier than you originally thought, isn’t it?
265
3. Find protocol methods to monitor note text changes in real time
Event related methods with keywords like “did” or “will” are often defined in protocols.
Due to the high readability of Objective-C methods’ names, we didn’t use IDA or LLDB to find
methods that meet our needs, but instead went over all headers with the keyword “protocol”.
With a 1st round filtering by header names and a 2nd round filtering by LLDB, we’ve found our
target methods. This is the charm of reverse engineering, regardless of fortune or guess.
This header is composed by picking snippets from other class-dump headers. The existence
of this header is simply for avoiding any warnings or errors when compiling the tweak.
%hook NotesDisplayController
- (void)viewWillAppear:(BOOL)arg1 // Initialze title
{
%orig;
NSString *content = self.note.contentAsPlainText;
NSString *contentLength = [NSString stringWithFormat:@"%lu", (unsigned
long)[content length]];
self.title = contentLength;
}
- (void)noteContentLayerContentDidChange:(NoteContentLayer *)arg1
updatedTitle:(BOOL)arg2 // Update title
{
%orig;
NSString *content = self.contentLayer.textView.text;
NSString *contentLength = [NSString stringWithFormat:@"%lu", (unsigned
long)[content length]];
self.title = contentLength;
}
%end
include theos/makefiles/common.mk
TWEAK_NAME = CharacountForNotes8
CharacountForNotes8_FILES = Tweak.xm
267
include $(THEOS_MAKE_PATH)/tweak.mk
after-install::
install.exec "killall -9 MobileNotes"
7.4.5 Test
After packaging and installing Characount for Notes 8, let’s test it by editing a random note
and see if the character count changes in real time, as shown in figure 7-11 to figure 7-17.
268
Figure 7- 11 Characount for Notes 8
269
Figure 7- 13 Characount for Notes 8
270
Figure 7- 15 Characount for Notes 8
271
Figure 7- 17 Characount for Notes 8
It works as we expected.
7.5 Conclusion
As a veteran on iOS, Notes is simple yet popular, a great number of people use this App
frequently in their daily lives. Characount for Notes 8 is so simple that we don’t even need
advanced reverse engineering tools to finish the whole project, I hope you don’t have difficulty
reading this chapter. It’s energy-and-time-consuming to learn assembly-level reverse engineering
when you are not familiar with IDA and LLDB, I suggest beginners carry out some simple
reverse engineering projects just like the example in this chapter first. In this way, not only can
you form a thinking pattern of reverse engineering, but also gain a sense of achievement, so
why not get your hands dirty right now?
272
Practice 2: Mark user specific emails as read automatically
8
8.1 Mail
Email is one of the most popular communication channels in the era of Internet. Many
people send and receive emails every day. Although there are lots of good email Apps on
AppStore, such as Sparrow, Inbox, etc, they are not as highly integrated as the stock Mail App
(hereafter referred to as Mail). Therefore, Mail is still the top choice during my daily life.
Among all emails we receive every day, most of them are valueless subscription emails like
notifications and advertisements, which comes from our inadvertently clicks of subscriptions on
various websites, as shown in figure 8-1.
Figure 8- 1 Mail
These emails always make me entangled. If we are kind enough to not think of them as
spam messages, they are actually distracting our attention. However, if we mark them as spam
273
messages, we may miss some useful information. So how to deal with these messages can be a
real headache. I have an idea that we can add a whitelist feature to Mail, which saves our
frequent contacts. Other emails outside whitelist will be marked as read automatically. With this
solution, we can highlight the most valuable messages while not missing any useful information,
as shown in figure 8-2.
Figure 8- 2 Mark messages outside whitelist as read
Our task for this chapter is to finish this tweak. We can divide the task into following 2
steps.
• Add a button on the Mail UI and present an editable whitelist after pressing the button in order to
add or delete entries in whitelist.
• Every time the inbox get refreshed, mark all emails outside whitelist as read.
Simple and clear, let’s get started. All operations in this chapter are carried out on iPhone 5,
iOS 8.1.1.
274
Figure 8- 3 Initial view of Mail
Where should we place the whitelist button for a better user-experience? In the “All
Inboxes” view in figure 8-3, we can see that the left bottom corner is blank; maybe we can put
the button here. Let’s try it out and the effect is shown in figure 8-4.
Figure 8- 4 Add whitelist button at the left bottom corner
Although the whitelist button is aligned with the compose button in right bottom corner,
275
the former is text and the latter is an icon. They are in different forms and looks inharmonious.
Therefore, we can see the left bottom corner is not suitable for text button. How about
changing it to an icon? The problem is that there isn’t an accustomed icon to represent whitelist,
while a random one may cause confusion. So in this view, no matter icon or text we use, we
cannot get both understandability and harmony. Let’s click “Mailboxes” and go to the upper
view, as shown in figure 8-5.
Figure 8- 5 Mailboxes
The top left and bottom left areas are both empty, as shown in figure 8-5. The bottom left is
not suitable for the whitelist button as we’ve discussed just now. So let’s put the button on top
left corner to see how it looks, as shown in figure 8-6.
276
Figure 8- 6 Add whitelist button at top left corner
Not bad, this is it. To customize the view like figure 8-6, we just need to find the controller
of “Mailboxes” view and then add the button by calling [controller.navigationItem
setLeftBarButtonItem:]. We have repeated the process of finding C from V for many times
previously and it has been proved as a feasible solution. After we know how to add the button,
we can try to implement the function of whitelist. It can be divided into three steps.
1) Get all emails.
2) Extract their senders’ addresses.
3) Mark them as read according to whitelist.
Let’s analyze them step by step, hope you can still catch up.
How can we get all emails? As we know, we can pull to refresh the inbox, as shown in figure
8-7.
277
Figure 8-7 Pull to refresh
During refreshing, Mail will fetch all latest emails from mail servers. After refreshing, the UI
will restore to the normal state as shown in figure 8-3, and at this moment, we’ve got all emails.
As long as we can catch the refresh completion event and read the inbox after that, we can get
all emails. Therefore, we can divide “getting all emails” into 2 steps: first, try to capture the
refresh completion event; second, read the inbox. Normally, the refresh completion event
handler would be a callback method in some protocols. So when analyzing the class-dump
headers, we should pay attention to whether there are protocol methods with keywords like
“didRefresh”, “didUpdate” or “didReload” in their names. By hooking such methods and read
the inbox after their execution, we’ll be able to get all emails.
An email is an object and it is generally abstracted as a class. From this class, we can extract
information like the receiver, sender, title, content and whether it is read. If we can get this
object, we can finish the second and third step together.
The overall ideas are not complicated, let’s realize them one by one.
Figure 8- 7 class-dump headers
279
Figure 8- 8 Import headers into Xcode
Next, let’s start to find the point to cut into code from UI.
280
| | | | | | | | | | <MailboxTableCell: 0x1572ad50;
baseClass = UITableViewCell; frame = (0 28; 320 44.5); autoresize = W; layer = <CALayer:
0x168299f0>>
| | | | | | | | | | | <UITableViewCellContentView:
0x16829b70; frame = (0 0; 286 44); gestureRecognizers = <NSArray: 0x1682b060>; layer =
<CALayer: 0x16829be0>>
| | | | | | | | | | | | <UILabel: 0x1682b0a0; frame
= (55 12; 84.5 20.5); text = ‘All Inboxes’; userInteractionEnabled = NO; layer =
<_UILabelLayer: 0x1682b160>>
……
The text of the UILabel at the bottom is “All Inboxes”, indicating its corresponding
MailBoxTableCell is the top one in figure 8-5. Keep calling nextResponder until we get the
controller:
cy# [#0x1572ad50 nextResponder]
#"<UITableViewWrapperView: 0x1572fe60; frame = (0 0; 320 568); gestureRecognizers =
<NSArray: 0x15730370>; layer = <CALayer: 0x157301a0>; contentOffset: {0, 0};
contentSize: {320, 568}>"
cy# [#0x1572fe60 nextResponder]
#"<UITableView: 0x1585a000; frame = (0 0; 320 568); clipsToBounds = YES; autoresize =
W+H; gestureRecognizers = <NSArray: 0x1572fa20>; layer = <CALayer: 0x1572f540>;
contentOffset: {0, -64}; contentSize: {320, 371}>"
cy# [#0x1585a000 nextResponder]
#"<MailboxPickerController: 0x156e9260>"
Aha. It’s very easy to get MailboxPickerController. Let’s try whether we can add a
leftBarButtonItem:
cy# #0x156e9260.navigationItem.leftBarButtonItem =
#0x156e9260.navigationItem.rightBarButtonItem
#"<UIBarButtonItem: 0x15729f00>"
281
Figure 8- 9 After setLeftBarButtonItem:
No problem! We’ve successfully added the button. Therefore, we can confirm that
MailboxPickerController is the controller of “Mailboxes” view.
8.2.4 Find the delegate of “All Inboxes” view using Reveal and
Cycript
After adding the whitelist button, we need to implement the function of it. First let’s take a
look at how to capture the refresh completion event. Since the event is straightly showed on
“All Inboxes” view, it is very likely that the callback method is defined in the delegate of this
view. Now let’s turn to “All Inboxes” view in figure 8-3 and use Reveal rather than repeating
what we’ve done with Cycript in section 8.2.3, to locate a cell of this view, and then turn back to
Cycript to find its associated UITableView as well delegate.
With Reveal, we can easily locate the top cell, as shown in figure 8-11.
282
Figure 8- 10 See the view hierarchy using Reveal
MailboxContentViewCell is the cell class to show the sender, title and summary of an email.
Next, we use Cycript to find its associated UITableView. Since we know there must be at least
one MailboxContentViewCell object in current view, we can try to find these cells through
command “choose” without using recursiveDescription.
FunMaker-5:~ root# cycript -p MobileMail
cy# choose(MailboxContentViewCell)
[#"<MailboxContentViewCell: 0x161f4000> cellContent",#"<MailboxContentViewCell:
0x1621c400> cellContent",#"<MailboxContentViewCell: 0x1621d000>
cellContent",#"<MailboxContentViewCell: 0x16234800>
cellContent",#"<MailboxContentViewCell: 0x1623ee00>
cellContent",#"<MailboxContentViewCell: 0x1623f200>
cellContent",#"<MailboxContentViewCell: 0x159c2c00> cellContent"]
Its delegate is MailboxContentViewController. Keep calling nextResonder and find what its
controller is.
From the output, we can see that both the controller and delegate of MFMailboxTableView
are MailboxContentViewController. Let’s validate the controller as below.
cy# [#0x16106000 setTitle:@"iOSRE"]
Figure 8- 11 After setTitle:
So far, we can confirm that our deduction is correct. Playing 2 important roles at the same
time, it is very likely that we can find both the refresh completion event handler and inbox
reading method in MailboxContentViewController. Let’s focus on this class from now on.
It looks like the function of this protocol is to read the data source rather than refresh it.
Let’s move on to MessageMegaMallObserver, its contents are as below:
@protocol MessageMegaMallObserver <NSObject>
- (void)megaMallCurrentMessageRemoved:(id)arg1;
- (void)megaMallDidFinishSearch:(id)arg1;
- (void)megaMallDidLoadMessages:(id)arg1;
- (void)megaMallFinishedFetch:(id)arg1;
- (void)megaMallGrowingMailboxesChanged:(id)arg1;
- (void)megaMallMessageCountChanged:(id)arg1;
- (void)megaMallMessagesAtIndexesChanged:(id)arg1;
- (void)megaMallStartFetch:(id)arg1;
@end
There are many perfect tense verbs in the method names. Meanwhile, judging from the
name like “LoadMessages”, “FinishedFetch” and “MessageCountChanged”, we guess that they
may get called before or after refresh completion. So let’s set breakpoints at the beginning of
these three methods using LLDB and pull to refresh the inbox to check if these methods are
called. In the first place, attach LLDB to MobileMail and inspect its ASLR offset.
(lldb) image list -o -f
[ 0] 0x000b2000
/private/var/db/stash/_.lnBgU8/Applications/MobileMail.app/MobileMail(0x00000000000b6000
)
[ 1] 0x003b7000 /Library/MobileSubstrate/MobileSubstrate.dylib(0x00000000003b7000)
[ 2] 0x090d1000 /Users/snakeninny/Library/Developer/Xcode/iOS DeviceSupport/8.1
(12B411)/Symbols/usr/lib/libarchive.2.dylib
[ 3] 0x090c3000 /Users/snakeninny/Library/Developer/Xcode/iOS DeviceSupport/8.1.1
(12B435)/Symbols/System/Library/Frameworks/CloudKit.framework/CloudKit
……
285
We can see the ASLR offset is 0x000b2000. Then drag and drop MobileMail into IDA and
after the initial analysis has been finished, check the base addresses of
[MailboxContentViewController megaMallDidLoadMessages:],
[MailboxContentViewController megaMallFinishedFetch:] and
[MailboxContentViewController megaMallMessageCountChanged:], as shown in figure 8-13, 8-
14 and 8-15.
Figure 8- 12 [MailboxContentViewController megaMallDidLoadMessages:]
Figure 8- 13 [MailboxContentViewController megaMallFinishedFetch:]
Figure 8- 14 [MailboxContentViewController megaMallMessageCountChanged:]
Their base addresses are 0x3dce0, 0x3d860 and 0x3de48 respectively. Set breakpoints on
these addresses with LLDB and refresh the inbox to trigger the breakpoints:
(lldb) br s –a ‘0x000b2000+0x3dce0’
Breakpoint 1: where = MobileMail`___lldb_unnamed_function992$$MobileMail, address =
0x000efce0
(lldb) br s -a ‘0x000b2000+0x3d860’
Breakpoint 2: where = MobileMail`___lldb_unnamed_function987$$MobileMail, address =
0x000ef860
(lldb) br s -a ‘0x000b2000+0x3de48’
Breakpoint 3: where = MobileMail`___lldb_unnamed_function993$$MobileMail, address =
0x000efe48
Some of you may meet the same problem as me, which is none of three breakpoints get
286
triggered. If you have experience in network development, you may have the idea that in order
to reduce the burden of servers and save the network traffic of iOS, Mail may not fetch emails
remotely on every refresh. If the time interval between two refreshes is very short, it will use
cached content as data source of inbox; and as a result, methods in MessageMegaMallObserver
will not get called. In order to validate our assumption, send an email to yourself and refresh to
check whether breakpoints get triggered:
Process 73130 stopped
* thread #44: tid = 0x14c10, 0x000ef860
MobileMail`___lldb_unnamed_function987$$MobileMail, stop reason = breakpoint 2.1
frame #0: 0x000ef860 MobileMail`___lldb_unnamed_function987$$MobileMail
MobileMail`___lldb_unnamed_function987$$MobileMail:
-> 0xef860: push {r7, lr}
0xef862: mov r7, sp
0xef864: sub sp, #24
0xef866: movw r1, #44962
(lldb) c
Process 73130 resuming
Process 73130 stopped
* thread #44: tid = 0x14c10, 0x000ef860
MobileMail`___lldb_unnamed_function987$$MobileMail, stop reason = breakpoint 2.1
frame #0: 0x000ef860 MobileMail`___lldb_unnamed_function987$$MobileMail
MobileMail`___lldb_unnamed_function987$$MobileMail:
-> 0xef860: push {r7, lr}
0xef862: mov r7, sp
0xef864: sub sp, #24
0xef866: movw r1, #44962
(lldb) c
Process 73130 resuming
Process 73130 stopped
* thread #1: tid = 0x11daa, 0x000efe48
MobileMail`___lldb_unnamed_function993$$MobileMail, queue = ‘MessageMiniMall.0x157c2d90,
stop reason = breakpoint 3.1
frame #0: 0x000efe48 MobileMail`___lldb_unnamed_function993$$MobileMail
MobileMail`___lldb_unnamed_function993$$MobileMail:
-> 0xefe48: push {r4, r5, r6, r7, lr}
0xefe4a: add r7, sp, #12
0xefe4c: push.w {r8, r10, r11}
0xefe50: sub.w r4, sp, #24
(lldb)
Process 73130 resuming
Process 73130 stopped
* thread #1: tid = 0x11daa, 0x000efe48
MobileMail`___lldb_unnamed_function993$$MobileMail, queue = ‘MessageMiniMall.0x157c2d90,
stop reason = breakpoint 3.1
frame #0: 0x000efe48 MobileMail`___lldb_unnamed_function993$$MobileMail
MobileMail`___lldb_unnamed_function993$$MobileMail:
-> 0xefe48: push {r4, r5, r6, r7, lr}
0xefe4a: add r7, sp, #12
0xefe4c: push.w {r8, r10, r11}
0xefe50: sub.w r4, sp, #24
(lldb)
Process 73130 resuming
Process 73130 stopped
287
* thread #44: tid = 0x14c10, 0x000ef860
MobileMail`___lldb_unnamed_function987$$MobileMail, stop reason = breakpoint 2.1
frame #0: 0x000ef860 MobileMail`___lldb_unnamed_function987$$MobileMail
MobileMail`___lldb_unnamed_function987$$MobileMail:
-> 0xef860: push {r7, lr}
0xef862: mov r7, sp
0xef864: sub sp, #24
0xef866: movw r1, #44962
(lldb) c
Process 73130 resuming
289
(int) $8 = 580
(lldb) p (int)[[$r2 object] messageCount]
(int) $0 = 553
(lldb) po [[[$r2 object] copyAllMessages] class]
__NSSetM
Figure 8- 15 Find MFLibraryMessage
290
@interface MFLibraryMessage : MFMailMessage
……
- (id)copyMessageInfo;
……
- (void)markAsNotViewed;
- (void)markAsViewed;
- (id)account;
……
- (unsigned long long)uniqueRemoteId;
- (unsigned long)uid;
- (unsigned int)hash;
- (id)remoteID;
- (void)_updateUID;
- (unsigned int)messageSize;
- (id)originalMailboxURL;
- (unsigned int)originalMailboxID;
- (unsigned int)mailboxID;
- (unsigned int)libraryID;
- (id)persistentID;
- (id)messageID;
@end
In MFLibraryMessage.h, there are various IDs, but our target information seems to be
missing. This doesn’t make sense: we have already found the email summary in the description
of MFLibraryMessage, but haven’t found the corresponding methods to read the summary in
MFLibraryMessage.h. Therefore, something must be ignored in our analysis. Recheck
MFLibraryMessage.h, we notice that there is a method called copyMessageInfo. Let’s take a
look at it.
Process 73130 stopped
* thread #1: tid = 0x11daa, 0x000efe48
MobileMail`___lldb_unnamed_function993$$MobileMail, queue = ‘MessageMiniMall.0x157c2d90,
stop reason = breakpoint 3.1
frame #0: 0x000efe48 MobileMail`___lldb_unnamed_function993$$MobileMail
MobileMail`___lldb_unnamed_function993$$MobileMail:
-> 0xefe48: push {r4, r5, r6, r7, lr}
0xefe4a: add r7, sp, #12
0xefe4c: push.w {r8, r10, r11}
0xefe50: sub.w r4, sp, #24
(lldb) po [[[[$r2 object] copyAllMessages] anyObject] copyMessageInfo]
<MFMessageInfo: 0x157c8040> uid=89, conversation=594030790676622907
We’ve got an object of MFMessageInfo, which has been mentioned in section 8.2.5. Is email
summary in MFMessageInfo.h? Let’s try it.
@interface MFMessageInfo : NSObject
{
unsigned int _flagged:1;
unsigned int _read:1;
unsigned int _deleted:1;
unsigned int _uidIsLibraryID:1;
unsigned int _hasAttachments:1;
unsigned int _isVIP:1;
unsigned int _uid;
unsigned int _dateReceivedInterval;
291
unsigned int _dateSentInterval;
unsigned int _mailboxID;
long long _conversationHash;
long long _generationNumber;
}
+ (long long)newGenerationNumber;
@property(readonly, nonatomic) long long generationNumber; // @synthesize
generationNumber=_generationNumber;
@property(nonatomic) unsigned int mailboxID; // @synthesize mailboxID=_mailboxID;
@property(nonatomic) long long conversationHash; // @synthesize
conversationHash=_conversationHash;
@property(nonatomic) unsigned int dateSentInterval; // @synthesize
dateSentInterval=_dateSentInterval;
@property(nonatomic) unsigned int dateReceivedInterval; // @synthesize
dateReceivedInterval=_dateReceivedInterval;
@property(nonatomic) unsigned int uid; // @synthesize uid=_uid;
- (id)description;
- (unsigned int)hash;
- (BOOL)isEqual:(id)arg1;
- (int)generationCompare:(id)arg1;
- (id)initWithUid:(unsigned int)arg1 mailboxID:(unsigned int)arg2
dateReceivedInterval:(unsigned int)arg3 dateSentInterval:(unsigned int)arg4
conversationHash:(long long)arg5 read:(BOOL)arg6 knownToHaveAttachments:(BOOL)arg7
flagged:(BOOL)arg8 isVIP:(BOOL)arg9;
- (id)init;
@property(nonatomic) BOOL isVIP;
@property(nonatomic, getter=isKnownToHaveAttachments) BOOL knownToHaveAttachments;
@property(nonatomic) BOOL uidIsLibraryID;
@property(nonatomic) BOOL deleted;
@property(nonatomic) BOOL flagged;
@property(nonatomic) BOOL read;
@end
MFMessageInfo can tell if an email is read, but it still doesn’t contain the summary. Go back
to MFLibraryMessage.h again, we see it inherits from MFMailMessage. Judging from its name,
MailMessage is certainly more appropriate to represent an email than LibraryMessage. Take a
look at MFMailMessage.h:
@interface MFMailMessage : MFMessage
……
- (BOOL)shouldSetSummary;
- (void)setSummary:(id)arg1;
- (void)setSubject:(id)arg1 to:(id)arg2 cc:(id)arg3 bcc:(id)arg4 sender:(id)arg5
dateReceived:(double)arg6 dateSent:(double)arg7 messageIDHash:(long long)arg8
conversationIDHash:(long long)arg9 summary:(id)arg10 withOptions:(unsigned int)arg11;
- (id)subject;
@end
summary, subject, sender, cc, bcc and some other frequently used phrases in emails come
into our eyes. However, except subject, most of them are only setters, where are the getters? If
you still remember how we’ve shifted our attention from MFLibraryMessage.h to
MFMailMessage.h, you will notice that MFMailMessage inherits from MFMessage. Before
292
inspecting MFMessage.h, let’s take a look at the return value of [MFMailMessage subject]
through LLDB to verify the analysis by now.
Process 73130 stopped
* thread #1: tid = 0x11daa, 0x000efe48
MobileMail`___lldb_unnamed_function993$$MobileMail, queue = ‘MessageMiniMall.0x157c2d90,
stop reason = breakpoint 3.1
frame #0: 0x000efe48 MobileMail`___lldb_unnamed_function993$$MobileMail
MobileMail`___lldb_unnamed_function993$$MobileMail:
-> 0xefe48: push {r4, r5, r6, r7, lr}
0xefe4a: add r7, sp, #12
0xefe4c: push.w {r8, r10, r11}
0xefe50: sub.w r4, sp, #24
(lldb) po [[[[$r2 object] copyAllMessages] anyObject] subject]
Asian Morning: Told to End Protests, Organizers in Hong Kong Vow to Expand Them
We can see that the return value of [MFMailMessage subject] is exactly the email title. Take
a look at MFMessage.h (Attention, MFMessage is a class in MIME.framework).
@interface MFMessage : NSObject <NSCopying>
……
- (id)headerData;
- (id)bodyData;
- (id)summary;
- (id)bccIfCached;
- (id)bcc;
- (id)ccIfCached;
- (id)cc;
- (id)toIfCached;
- (id)to;
- (id)firstSender;
- (id)sendersIfCached;
- (id)senders;
- (id)dateSent;
- (id)subject;
- (id)messageData;
- (id)messageBody;
- (id)headers;
……
@end
to, sender, subject, messageBody, getters for all email information are available now. It’s
time to check their values with LLDB.
Process 73130 stopped
* thread #1: tid = 0x11daa, 0x000efe48
MobileMail`___lldb_unnamed_function993$$MobileMail, queue = ‘MessageMiniMall.0x157c2d90,
stop reason = breakpoint 3.1
frame #0: 0x000efe48 MobileMail`___lldb_unnamed_function993$$MobileMail
MobileMail`___lldb_unnamed_function993$$MobileMail:
-> 0xefe48: push {r4, r5, r6, r7, lr}
0xefe4a: add r7, sp, #12
0xefe4c: push.w {r8, r10, r11}
0xefe50: sub.w r4, sp, #24
(lldb) po [[[[$r2 object] copyAllMessages] anyObject] firstSender]
NYTimes.com <[email protected]>
(lldb) po [[[[$r2 object] copyAllMessages] anyObject] sendersIfCached]
<__NSArrayI 0x16850850>(
293
NYTimes.com <[email protected]>
)
Figure 8- 16 [MessageMegaMall markMessagesAsViewed:]
Its base address is 0x13b648. Since the ASLR offset of MobileMail is 0xb2000, we can set a
breakpoint like this:
(lldb) br s -a ‘0x000b2000+0x0013B648’
Breakpoint 4: where = MobileMail`___lldb_unnamed_function7357$$MobileMail, address =
0x001ed648
Process 103910 stopped
* thread #1: tid = 0x195e6, 0x001ed648
MobileMail`___lldb_unnamed_function7357$$MobileMail, queue = ‘com.apple.main-thread,
stop reason = breakpoint 4.1
294
frame #0: 0x001df648 MobileMail`___lldb_unnamed_function7357$$MobileMail
MobileMail`___lldb_unnamed_function7357$$MobileMail:
-> 0x1ed648: push {r4, r5, r6, r7, lr}
0x1ed64a: add r7, sp, #12
0x1ed64c: str r8, [sp, #-4]!
0x1ed650: mov r8, r0
(lldb) po $r2
{(
<MFLibraryMessage 0x157b70b0: library id 906, remote id 13142, 2014-12-17 22:34:30
+0000, ‘Asian Morning: Obama Announces U.S. and Cuba Will Resume Relations’>
)}
(lldb) po [$r2 class]
__NSSetI
295
3. Get all emails from MessageMegaMall.
According to the experience that “The reason a protocol method gets called is generally that
the corresponding event mentioned in the method name happened. And the thing that triggers
the event is usually the method’s arguments”, we’ve found class MessageMegaMall from the
argument of megaMallMessageCountChanged:. The name, MegaMall, was very obscure. With
wild guesses and programmatic checks, we’ve discovered that it was the model for email
managements. By calling [MessageMegaMall copyAllMessages], we could get all emails.
296
[5.] iphone/notification_center_widget
[6.] iphone/preference_bundle
[7.] iphone/sbsettingstoggle
[8.] iphone/tool
[9.] iphone/tweak
[10.] iphone/xpc_service
Choose a Template (required): 9
Project Name (required): iOSREMailMarker
Package Name [com.yourcompany.iosremailmarker]: com.iosre.mailmarker
Author/Maintainer Name [sam]: sam
[iphone/tweak] MobileSubstrate Bundle filter [com.apple.springboard]:
com.apple.mobilemail
[iphone/tweak] List of applications to terminate upon installation (space-separated, ‘-’
for none) [SpringBoard]: MobileMail
Instantiating iphone/tweak in iosremailmarker/...
Done.
This header is composed by picking snippets from other class-dump headers. The existence
of this header is simply for avoiding any warnings or errors when compiling the tweak.
%hook MailboxPickerController
%new
- (void)iOSREShowWhitelist
{
UIAlertController *alertController = [UIAlertController
alertControllerWithTitle:@"Whitelist" message:@"Please input an email address"
preferredStyle:UIAlertControllerStyleAlert];
297
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"OK"
style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
UITextField *whitelistField = alertController.textFields.firstObject;
if ([whitelistField.text length] != 0) [[NSUserDefaults standardUserDefaults]
setObject:whitelistField.text forKey:@"whitelist"];
}];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel"
style:UIAlertActionStyleCancel handler:nil];
[alertController addAction:okAction];
[alertController addAction:cancelAction];
[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.placeholder = @"[email protected]";
textField.text = [[NSUserDefaults standardUserDefaults]
objectForKey:@"whitelist"];
}];
[self presentViewController:alertController animated:YES completion:nil];
}
- (void)viewWillAppear:(BOOL)arg1
{
self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc]
initWithTitle:@"Whitelist" style:UIBarButtonItemStylePlain target:self
action:@selector(iOSREShowWhitelist)] autorelease];
%orig;
}
%end
%hook MailboxContentViewController
- (void)megaMallMessageCountChanged:(NSConcreteNotification *)arg1
{
%orig;
NSMutableSet *targetMessages = [NSMutableSet setWithCapacity:600];
NSString *whitelist = [[NSUserDefaults standardUserDefaults]
objectForKey:@"whitelist"];
MessageMegaMall *mall = [arg1 object];
NSSet *messages = [mall copyAllMessages]; // Remember to release it later
for (MFLibraryMessage *message in messages)
{
MFMessageInfo *messageInfo = [message copyMessageInfo]; // Remember to
release it later
for (NSString *sender in [message senders]) if (!messageInfo.read && [sender
rangeOfString:[NSString stringWithFormat:@"<%@>", whitelist]].location == NSNotFound)
[targetMessages addObject:message];
[messageInfo release];
}
[messages release];
[mall markMessagesAsViewed:targetMessages];
}
%end
include theos/makefiles/common.mk
298
TWEAK_NAME = iOSREMailMarker
iOSREMailMarker_FILES = Tweak.xm
iOSREMailMarker_FRAMEWORKS = UIKit
include $(THEOS_MAKE_PATH)/tweak.mk
after-install::
install.exec "killall -9 MobileMail"
8.4.5 Test
Compile the tweak and install it on iOS. Open Mail but it seems nothing changed. That is
because we haven’t configured iOSREMailMarker yet. As shown in figure 8-18, there are 44
unread messages currently.
Figure 8- 17 44 unread emails
After entering the “Mailboxes” view, there is a new whitelist button on the left side of
navigation bar. Press it and a new whitelist dialog will pop up, as shown in 8-19.
299
Figure 8- 18 Whitelist dialog
I’ve subscribed a copy of NYTimes and will spend about 15 minutes reading it every day.
Let’s add NYTimes into whitelist, as shown in figure 8-20.
Figure 8- 19 Add NYTimes into whitelist
300
Figure 8-21 iOSREMailMarker marked all emails except NYTimes as read
8.5 Conclusion
In this chapter, we’ve taken Mail as an example and added a feature that can automatically
mark emails outside whitelist as read, which helps us highlight the important emails. The filter
condition of iOSREMailMarker is somewhat simple, and it may not be a good solution for
everyone to simply mark emails as read. So I hope you can learn this chapter by analogy and
intimidate the ideas to make your own unique tweaks. Of course, you are welcome to share
your works on our website.
So far, we have gone through 2 practices. I hope everyone enjoyed them and had the feeling
that our brains should keep one step ahead of our hands in iOS reverse engineering. Only when
you get fully prepared during early stage analysis can you write an excellent tweak later. TiGa, a
veteran reverse engineer, once said: “A reverser should know how/what is done before
grabbing tools to complete the tasks automatically.” I believe that everyone will gradually
realize the meaning of this sentence during continuously studying reverse engineering.
301
Practice 3: Save and share Sight in WeChat
9
9.1 WeChat
WeChat is one of the most outstanding IM App in the mobile Internet industry. In China, it
is the daily necessity of most netizens. WeChat’s launch image is as shown in figure 9-1; it seems
that there is a little sorrow in its great power.
In October 3rd, 2014, WeChat has updated to version 6.0 and added a new feature, Sight i.e.
short videos. It was so fun that my WeChat friends started to share all kinds of Sights, as shown
in figure 9-2.
302
Figure 9-2 Sight
Although we can already mark our interested Sights via long press menus (as shown in
figure 9-3), I’m not satisfied yet; it’d be better if those Sights can be downloaded or shared on
other platforms. So, the goal of this chapter is adding two options to long press menu of Sight,
which are “Save to Disk” and “Copy URL” respectively.
303
The size of WeChat 6.0 is bigger than 80 MB; it’s rather complicated reversing it. As usual,
before reversing, we need to analyze and modeling the target, then make a plan and carry it out.
The following operations are done on WeChat 6.0 on iOS 8.1, iPhone 5. After the publication of
this book, WeChat will probably update to a higher version, there will be some tiny changes in
the following operations, but the general ideas stay the same. For the analysis of the latest
WeChat, please keep following http://bbs.iosre.com.
Let’s review figure 9-3 and think together: “Favorite” and “Report Abuse” will pop up after
we long press the Sight view. Doesn’t this phenomenon indicate that the Sight view can already
respond to long press gestures? So, we only need to find the gesture action selector and hook it,
then we can pop up our custom menu with options “Save to Disk” and “Copy URL” just inside
this function.
There is a line of words “Tap to download” under the play button in Sight view, which
304
means WeChat will download the Sight to iOS first, and then play it offline. Therefore, we can
conclude that a download URL already exists in a Sight, and the downloaded Sight is saved
somewhere on iOS. Luckily, the URL and the downloaded Sight happen to be our goal of this
chapter, if we can find their locations in WeChat, our job is done. After the previous 2 practices,
I believe your understanding of MVC has become deeper: If we manage to get the V of a Sight,
we can get its M, which contains the Sight’s download URL and video objects.
OK, now we know that WeChat has already invented the wheel, we just need to find and
make use of it. In order to speed up our reversing process, we won’t be overly sticking to the
execution logic of WeChat with IDA or LLDB, but try our best to look for clues in class-dump
headers, and then verify our guesses to reach the goal of locating the Sight.
305
Figure 9-5 Headers of WeChat 6.0
WeChat has the most headers among all Apps I have ever seen, going through all these files
one by one is mission impossible for a single person. Such a big project is unlikely to be
completed by one single team, perhaps Tencent just splits WeChat into several subprojects, for
example, Moments subproject, IM subproject, drift bottle subproject, Sight subproject, etc. For
each subproject, there’s one team in charge. At last, all subprojects are merged into one big
project, namely WeChat.
306
Figure 9-6 WeChat headers in Xcode
The built-in search and highlight functions in Xcode help display the code beautifully and
neatly. Now, let’s cut into the code via WeChat’s UI.
307
Figure 9-7 Use Reveal to see the UI layout of WeChat
In figure 9-7, text “LLBean shirt with nice fabric” can be discovered easily in both sides,
indicating their correspondence. Keep checking around RichTextView, the Sight view is very
conspicuous, as shown in figure 9-8.
In the header, methods with keywords “LongTouch” and “LongPressed” are very likely to
be our targets. Now, IDA should have finished the initial analysis, let’s take a look at the
implementation of these methods. onLongTouch first, as shown in figure 9-9.
The execution flow of this method is very simple. Look through the method body quickly,
“UIMenuController” can be easily found, as shown in figure 9-10.
309
Figure 9-11 onLongTouch
It seems that it records some information, then calls onShowSightAction. Double click
onShowSightAction to see its implementation, as shown in figure 9-13.
310
Figure 9-13 onShowSightAction
In figure 9-13, we know that a WCActionSheet object is created from the very beginning.
From its name, we can guess that WCActionSheet acts like UIActionSheet, but we didn’t see
any action sheet when we long press the Sight, so onLongPressedWCSightFullScreenWindow:
is probably not the method we want.
The last method, onLongPressedWCSight:, is shown in figure 9-14.
From figure 9-14, we can see it records some information, then calls onLongTouch, which
proves our conjecture indirectly. Now, let’s debug onLongPressedWCSightFullScreenWindow:
and onLongTouch using LLDB. Firstly, attach debugserver to MicroMessenger:
311
snakeninnysiMac:Documents snakeninny$ ssh root@localhost -p 2222
FunMaker-5:~ root# debugserver *:1234 -a MicroMessenger
debugserver-@(#)PROGRAM:debugserver PROJECT:debugserver-320.2.89
for armv7.
Attaching to process MicroMessenger...
Listening to port 1234 for a connection from *...
Waiting for debugger instructions for process 0.
Then check the ASLR offset of WeChat:
(lldb) image list -o -f
[ 0] 0x00000000 /private/var/mobile/Containers/Bundle/Application/E4EBD049-1A75-4830-
BC65-0132C0EBC1CA/MicroMessenger.app/MicroMessenger(0x0000000000004000)
[ 1] 0x022dc000 /Library/MobileSubstrate/MobileSubstrate.dylib(0x00000000022dc000)
……
The ASLR offset of WeChat is surprisingly 0x0. Next, let’s check the base addresses of
onLongPressedWCSightFullScreenWindow: and onLongTouch, as shown in figure 9-15 and 9-
16.
The base addresses of them are 0x21e484 and 0x21e7ec. Set 2 breakpoints on them then long
press the Sight view to see whether these breakpoints are triggered:
(lldb) br s -a 0x21e484
Breakpoint 3: where = MicroMessenger`___lldb_unnamed_function9789$$MicroMessenger,
address = 0x0021e484
(lldb) br s -a 0x21e7ec
Breakpoint 4: where = MicroMessenger`___lldb_unnamed_function9791$$MicroMessenger,
address = 0x0021e7ec
Process 184500 stopped
* thread #1: tid = 0x2d0b4, 0x0021e7ec
MicroMessenger`___lldb_unnamed_function9791$$MicroMessenger, queue = 'com.apple.main-
thread, stop reason = breakpoint 4.1
312
frame #0: 0x0021e7ec MicroMessenger`___lldb_unnamed_function9791$$MicroMessenger
MicroMessenger`___lldb_unnamed_function9791$$MicroMessenger:
-> 0x21e7ec: push {r4, r5, r6, r7, lr}
0x21e7ee: add r7, sp, #12
0x21e7f0: push.w {r8, r10, r11}
0x21e7f4: sub sp, #32
(lldb) p (char *)$r1
(char *) $0 = 0x017fdc2b "onLongTouch"
(lldb) c
Process 184500 resuming
Process 184500 stopped
* thread #1: tid = 0x2d0b4, 0x0021e7ec
MicroMessenger`___lldb_unnamed_function9791$$MicroMessenger, queue = 'com.apple.main-
thread, stop reason = breakpoint 4.1
frame #0: 0x0021e7ec MicroMessenger`___lldb_unnamed_function9791$$MicroMessenger
MicroMessenger`___lldb_unnamed_function9791$$MicroMessenger:
-> 0x21e7ec: push {r4, r5, r6, r7, lr}
0x21e7ee: add r7, sp, #12
0x21e7f0: push.w {r8, r10, r11}
0x21e7f4: sub sp, #32
(lldb) p (char *)$r1
(char *) $1 = 0x017fdc2b "onLongTouch"
Figure 9- 17 onLongPressedWCSight:
313
0x21e41a: mov r4, r0
(lldb) p (char *)$r1
(char *) $2 = 0x0182c799 "onLongPressedWCSight:"
(lldb) c
Process 184500 resuming
Process 184500 stopped
* thread #1: tid = 0x2d0b4, 0x0021e414
MicroMessenger`___lldb_unnamed_function9788$$MicroMessenger, queue = 'com.apple.main-
thread, stop reason = breakpoint 5.1
frame #0: 0x0021e414 MicroMessenger`___lldb_unnamed_function9788$$MicroMessenger
MicroMessenger`___lldb_unnamed_function9788$$MicroMessenger:
-> 0x21e414: push {r4, r5, r6, r7, lr}
0x21e416: add r7, sp, #12
0x21e418: sub sp, #16
0x21e41a: mov r4, r0
(lldb) p (char *)$r1
(char *) $3 = 0x0182c799 "onLongPressedWCSight:"
(lldb) po $r2
<WCSightView: 0x2454dc0; baseClass = UIControl; frame = (0 3; 200 150);
gestureRecognizers = <NSArray: 0x87e5110>; layer = <CALayer: 0xd3be460>>
onLongPressedWCSight: was also called twice, and its argument was an object of
WCSightView. By now, we have located the response method of long press gestures, which is
onLongPressedWCSight: or onLongTouch. Next, we need to go further to find the trace of the
Sight.
314
Figure 9-18 Download the Sight
315
| | | | | | | | | | | | | | | |
<MMUILabel: 0x7ee7530; baseClass = UILabel; frame = (0 103; 200 20); text = 'Tap to
play'; hidden = YES; userInteractionEnabled = NO; tag = 10040; layer = <_UILabelLayer:
0x7e50dd0>>
……
cy# [#0xd3be3e0 nextResponder]
#"<WCTimeLineCellView: 0x872c530; frame = (0 0; 313 243); tag = 1048577; layer =
<CALayer: 0x872ce80>>"
cy# [#0x872c530 nextResponder]
#"<UITableViewCellContentView: 0x8729d80; frame = (0 0; 320 251); gestureRecognizers =
<NSArray: 0x8729f80>; layer = <CALayer: 0x8729df0>>"
cy# [#0x8729d80 nextResponder]
#"<MMTableViewCell: 0x8729be0; baseClass = UITableViewCell; frame = (0 1164.33; 320
251); autoresize = W; layer = <CALayer: 0x8729b50>>"
cy# [#0x8729be0 nextResponder]
#"<UITableViewWrapperView: 0xab09890; frame = (0 0; 320 568); gestureRecognizers =
<NSArray: 0xab09b00>; layer = <CALayer: 0x7e6e4b0>; contentOffset: {0, 0}; contentSize:
{320, 568}>"
cy# [#0xab09890 nextResponder]
#"<MMTableView: 0x30c3200; baseClass = UITableView; frame = (0 0; 320 568);
gestureRecognizers = <NSArray: 0xab09600>; layer = <CALayer: 0xab09160>; contentOffset:
{0, 1090}; contentSize: {320, 3186.3333}>"
cy# [#0x30c3200 nextResponder]
#"<UIView: 0x7e3b040; frame = (0 0; 320 568); autoresize = W+H; layer = <CALayer:
0x7e3afd0>>"
cy# [#0x7e3b040 nextResponder]
#"<WCTimeLineViewController: 0x28bd200>"
Seems like we’ve lost. Is it time to give up? No! Let’s keep calm and carry on: Because
Moments are presented as table views, and there’s a method named
tableView:cellForRowAtIndexPath: in WCTimeLineViewController, which means this class
implements UITableViewDataSource protocol, so it must have a close relationship with M.
Now, jump to this method in IDA, as shown in figure 9-19.
316
Figure 9-19 [WCTimeLineViewController tableView:cellForRowAtIndexPath:]
Look through this method, you will find 3 red squares in figure 9-17 are the core of this
method, other parts are just setting the background, theme and color of the cell. Let’s take a
look at these 3 red squares closely, as shown in figure 9-20.
317
Figure 9- 21 [WCTimeLineViewController genNormalCell:indexPath:]
The logic is not complicated, if you look through the method body, a suspicious method
comes up, as shown in figure 9-22.
Infer from the name, getTimelineDataItemOfIndex: in figure 9-22 is probably the data
318
source of the current cell. Set a breakpoint at the bottom instruction, i.e. “__text:002A091C
BLX.W j__objc_msgSend”, then think of a way to trigger it. We have already known that
tableView:cellForRowAtIndexPath: is called when UITableView needs to display a new cell. In
order to make this breakpoint break on a cell with Sight, we just need to scroll the Sight out of
screen, then scroll it back. When the Sight is scrolled out, a new cell will scroll in, hence triggers
the breakpoint; there’s no Sight on this cell, this kind of breakpoint doesn’t meet our
requirement, so what we do is to disable the breakpoint first, then enable the breakpoint after
the Sight is scrolled out of the screen completely. Now we can scroll the Sight back, the
breakpoint will break on a cell with Sight:
(lldb) br s -a 0x2A091C
Breakpoint 6: where = MicroMessenger`___lldb_unnamed_function11980$$MicroMessenger +
208, address = 0x002a091c
Process 184500 stopped
* thread #1: tid = 0x2d0b4, 0x002a091c
MicroMessenger`___lldb_unnamed_function11980$$MicroMessenger + 208, queue =
'com.apple.main-thread, stop reason = breakpoint 6.1
frame #0: 0x002a091c MicroMessenger`___lldb_unnamed_function11980$$MicroMessenger +
208
MicroMessenger`___lldb_unnamed_function11980$$MicroMessenger + 208:
-> 0x2a091c: blx 0xe08e0c ;
___lldb_unnamed_function70162$$MicroMessenger
0x2a0920: mov r11, r0
0x2a0922: movw r0, #32442
0x2a0926: movt r0, #436
(lldb) ni
Process 184500 stopped
* thread #1: tid = 0x2d0b4, 0x002a0920
MicroMessenger`___lldb_unnamed_function11980$$MicroMessenger + 212, queue =
'com.apple.main-thread, stop reason = instruction step over
frame #0: 0x002a0920 MicroMessenger`___lldb_unnamed_function11980$$MicroMessenger +
212
MicroMessenger`___lldb_unnamed_function11980$$MicroMessenger + 212:
-> 0x2a0920: mov r11, r0
0x2a0922: movw r0, #32442
0x2a0926: movt r0, #436
0x2a092a: add r0, pc
(lldb) po $r0
Class name: WCDataItem, addr: 0x80f52b0
tid: 11896185303680028954
username: wxid_hqouu9kgsgw3e6
createtime: 1418135798
commentUsers: (
)
contentObj: <WCContentItem: 0x8724c20>
We’ve got a WCDataItem object, with a WCContentItem object in it. Is the “Data” in
WCDataItem a Sight? Let’s test it with LLDB by setting this WCDataItem object to NULL and
see what happens. Repeat the previous operations to trigger the breakpoint on a Sight cell:
Process 184500 stopped
319
* thread #1: tid = 0x2d0b4, 0x002a091c
MicroMessenger`___lldb_unnamed_function11980$$MicroMessenger + 208, queue =
'com.apple.main-thread, stop reason = breakpoint 6.1
frame #0: 0x002a091c MicroMessenger`___lldb_unnamed_function11980$$MicroMessenger +
208
MicroMessenger`___lldb_unnamed_function11980$$MicroMessenger + 208:
-> 0x2a091c: blx 0xe08e0c ;
___lldb_unnamed_function70162$$MicroMessenger
0x2a0920: mov r11, r0
0x2a0922: movw r0, #32442
0x2a0926: movt r0, #436
(lldb) ni
Process 184500 stopped
* thread #1: tid = 0x2d0b4, 0x002a0920
MicroMessenger`___lldb_unnamed_function11980$$MicroMessenger + 212, queue =
'com.apple.main-thread, stop reason = instruction step over
frame #0: 0x002a0920 MicroMessenger`___lldb_unnamed_function11980$$MicroMessenger +
212
MicroMessenger`___lldb_unnamed_function11980$$MicroMessenger + 212:
-> 0x2a0920: mov r11, r0
0x2a0922: movw r0, #32442
0x2a0926: movt r0, #436
0x2a092a: add r0, pc
(lldb) register write r0 0
(lldb) br del
About to delete all breakpoints, do you want to do that?: [Y/n] y
All breakpoints removed. (1 breakpoint)
(lldb) c
The first Sight totally disappeared, as shown in figure 9-23. This phenomenon indicates that
the data source of the Sight is indeed WCDataItem. Before analyzing WCDataItem, there
remains one problem to be solved: How can we get a WCDataItem object from the hooked
320
method [WCContentItemViewTemplateNewSight onLongTouch]?
New problems emerge: How do we call getService: and calcDataItemIndex:? Let’s start
from getService:. From the instruction “MOV R0, R6”, we know the caller is R6; R6 is the return
value of [MMServiceCenter defaultCenter]. The argument is from the return value of
[WCFacade class], as shown in figure 9-25.
321
Figure 9-25 Analyze getTimelineDataItemOfIndex:
322
Figure 9-26 Analyze getTimelineDataItemOfIndex:
323
Figure 9-27 Analyze getTimelineDataItemOfIndex
324
NSString *sourceUserName;
NSString *sourceNickName;
NSString *contentDesc;
NSString *contentDescPattern;
int contentDescShowType;
int contentDescScene;
WCActionInfo *actionInfo;
unsigned int hash;
SnsObject *snsObject;
BOOL isBidirectionalFan;
BOOL noChange;
BOOL isRichText;
NSMutableDictionary *extData;
int uploadErrType;
NSString *statisticsData;
}
+ (id)fromBuffer:(id)arg1;
+ (id)fromServerObject:(id)arg1;
+ (id)fromUploadTask:(id)arg1;
@property(retain, nonatomic) WCActionInfo *actionInfo; // @synthesize actionInfo;
@property(retain, nonatomic) WCAppInfo *appInfo; // @synthesize appInfo;
@property(retain, nonatomic) NSArray *blackUsers; // @synthesize blackUsers;
@property(nonatomic) int cid; // @synthesize cid;
@property(nonatomic) int commentCount; // @synthesize commentCount;
@property(retain, nonatomic) NSMutableArray *commentUsers; // @synthesize commentUsers;
- (int)compareDesc:(id)arg1;
- (int)compareTime:(id)arg1;
@property(retain, nonatomic) NSString *contentDesc; // @synthesize contentDesc;
@property(retain, nonatomic) NSString *contentDescPattern; // @synthesize
contentDescPattern;
@property(nonatomic) int contentDescScene; // @synthesize contentDescScene;
@property(nonatomic) int contentDescShowType; // @synthesize contentDescShowType;
@property(retain, nonatomic) WCContentItem *contentObj; // @synthesize contentObj;
@property(nonatomic) int createtime; // @synthesize createtime;
- (void)dealloc;
- (id)description;
- (id)descriptionForKeyPaths;
- (void)encodeWithCoder:(id)arg1;
@property(retain, nonatomic) NSMutableDictionary *extData; // @synthesize extData;
@property(nonatomic) unsigned long extFlag; // @synthesize extFlag;
@property(nonatomic) int flag; // @synthesize flag;
- (id)getDisplayCity;
- (id)getMediaWraps;
- (BOOL)hasSharedGroup;
- (unsigned int)hash;
- (id)init;
- (id)initWithCoder:(id)arg1;
@property(nonatomic) BOOL isBidirectionalFan; // @synthesize isBidirectionalFan;
- (BOOL)isEqual:(id)arg1;
@property(nonatomic) BOOL isPrivate; // @synthesize isPrivate;
- (BOOL)isRead;
@property(nonatomic) BOOL isRichText; // @synthesize isRichText;
- (BOOL)isUploadFailed;
- (BOOL)isUploading;
- (BOOL)isValid;
- (id)itemID;
- (int)itemType;
- (id)keyPaths;
@property(nonatomic) int likeCount; // @synthesize likeCount;
325
@property(nonatomic) BOOL likeFlag; // @synthesize likeFlag;
@property(retain, nonatomic) NSMutableArray *likeUsers; // @synthesize likeUsers;
- (void)loadPattern;
@property(retain, nonatomic) WCLocationInfo *locationInfo; // @synthesize locationInfo;
- (void)mergeLikeUsers:(id)arg1;
- (void)mergeMessage:(id)arg1;
- (void)mergeMessage:(id)arg1 needParseContent:(BOOL)arg2;
@property(retain, nonatomic) NSString *nickname; // @synthesize nickname;
@property(nonatomic) BOOL noChange; // @synthesize noChange;
- (void)parseContentForNetWithDataItem:(id)arg1;
- (void)parseContentForUI;
- (void)parsePattern;
@property(retain, nonatomic) NSString *publicUserName; // @synthesize publicUserName;
- (id)sequence;
- (void)setCreateTime:(unsigned long)arg1;
- (void)setHash:(unsigned int)arg1;
- (void)setIsUploadFailed:(BOOL)arg1;
- (void)setSequence:(id)arg1;
@property(retain, nonatomic) NSMutableArray *sharedGroupIDs; // @synthesize
sharedGroupIDs;
@property(retain, nonatomic) SnsObject *snsObject; // @synthesize snsObject;
@property(retain, nonatomic) NSString *sourceNickName; // @synthesize sourceNickName;
@property(retain, nonatomic) NSString *sourceUrl2; // @synthesize sourceUrl2;
@property(retain, nonatomic) NSString *sourceUrl; // @synthesize sourceUrl;
@property(retain, nonatomic) NSString *sourceUserName; // @synthesize sourceUserName;
@property(retain, nonatomic) NSString *statisticsData; // @synthesize statisticsData;
@property(retain, nonatomic) NSString *tid; // @synthesize tid;
@property(nonatomic) int type; // @synthesize type;
@property(nonatomic) int uploadErrType; // @synthesize uploadErrType;
@property(retain, nonatomic) NSString *username; // @synthesize username;
@property(retain, nonatomic) NSArray *visibleUsers; // @synthesize visibleUsers;
@property(nonatomic) int withCount; // @synthesize withCount;
@property(retain, nonatomic) NSMutableArray *withUsers; // @synthesize withUsers;
- (id)toBuffer;
@end
Now let’s inspect their return values via LLDB. Repeat the previous operations to trigger
the breakpoint on a Sight cell:
Process 184500 stopped
* thread #1: tid = 0x2d0b4, 0x002a091c
MicroMessenger`___lldb_unnamed_function11980$$MicroMessenger + 208, queue =
'com.apple.main-thread, stop reason = breakpoint 7.1
frame #0: 0x002a091c MicroMessenger`___lldb_unnamed_function11980$$MicroMessenger +
208
MicroMessenger`___lldb_unnamed_function11980$$MicroMessenger + 208:
-> 0x2a091c: blx 0xe08e0c ;
___lldb_unnamed_function70162$$MicroMessenger
0x2a0920: mov r11, r0
0x2a0922: movw r0, #32442
0x2a0926: movt r0, #436
(lldb) ni
326
Process 184500 stopped
* thread #1: tid = 0x2d0b4, 0x002a0920
MicroMessenger`___lldb_unnamed_function11980$$MicroMessenger + 212, queue =
'com.apple.main-thread, stop reason = instruction step over
frame #0: 0x002a0920 MicroMessenger`___lldb_unnamed_function11980$$MicroMessenger +
212
MicroMessenger`___lldb_unnamed_function11980$$MicroMessenger + 212:
-> 0x2a0920: mov r11, r0
0x2a0922: movw r0, #32442
0x2a0926: movt r0, #436
0x2a092a: add r0, pc
(lldb) po [$r0 descriptionForKeyPaths]
Class name: WCDataItem, addr: 0x80f52b0
tid: 11896185303680028954
username: wxid_hqouu9kgsgw3e6
createtime: 1418135798
commentUsers: (
)
contentObj: <WCContentItem: 0x8724c20>
Seems there is nothing special in the return values, but WCContentItem has showed up so
many times and grabs my attention. Obviously, the meaning of “content” is more specific than
“data”, the content of the Sight may be supplied by this object. Now, take a look at
WCContentItem.h:
@interface WCContentItem : NSObject <NSCoding>
{
NSString *title;
NSString *desc;
NSString *titlePattern;
NSString *descPattern;
NSString *linkUrl;
NSString *linkUrl2;
int type;
int flag;
NSString *username;
NSString *nickname;
int createtime;
NSMutableArray *mediaList;
}
@end
We can get a WCContentItem object via [WCDataItem contentObj], then use LLDB to
print the values of the above 2 properties. Repeat the previous operations to trigger the
breakpoint on a Sight cell:
Process 184500 stopped
* thread #1: tid = 0x2d0b4, 0x002a091c
MicroMessenger`___lldb_unnamed_function11980$$MicroMessenger + 208, queue =
'com.apple.main-thread, stop reason = breakpoint 8.1
frame #0: 0x002a091c MicroMessenger`___lldb_unnamed_function11980$$MicroMessenger +
208
MicroMessenger`___lldb_unnamed_function11980$$MicroMessenger + 208:
-> 0x2a091c: blx 0xe08e0c ;
___lldb_unnamed_function70162$$MicroMessenger
0x2a0920: mov r11, r0
0x2a0922: movw r0, #32442
0x2a0926: movt r0, #436
(lldb) ni
Process 184500 stopped
* thread #1: tid = 0x2d0b4, 0x002a0920
MicroMessenger`___lldb_unnamed_function11980$$MicroMessenger + 212, queue =
'com.apple.main-thread, stop reason = instruction step over
frame #0: 0x002a0920 MicroMessenger`___lldb_unnamed_function11980$$MicroMessenger +
212
MicroMessenger`___lldb_unnamed_function11980$$MicroMessenger + 212:
-> 0x2a0920: mov r11, r0
0x2a0922: movw r0, #32442
0x2a0926: movt r0, #436
0x2a092a: add r0, pc
(lldb) po [[$r0 contentObj] linkUrl]
https://support.weixin.qq.com/cgi-bin/mmsupport-
bin/readtemplate?t=page/common_page__upgrade&v=1
(lldb) po [[$r0 contentObj] linkUrl2]
nil
Type this URL in browser to see what we’ve got, as shown in figure 9-28.
328
Figure 9-28 [[$r0 contentObj] linkUrl]
The result is not what we want. Since there is not too much content in WCContentItem.h,
where could the Sight be? Back to this file, a property named mediaList attracts my attention. It
is even more accurate than “content”, is the Sight hidden in it? LLDB will answer us. Repeat the
previous operations to trigger the breakpoint on a Sight cell:
Process 184500 stopped
* thread #1: tid = 0x2d0b4, 0x002a091c
MicroMessenger`___lldb_unnamed_function11980$$MicroMessenger + 208, queue =
'com.apple.main-thread, stop reason = breakpoint 8.1
frame #0: 0x002a091c MicroMessenger`___lldb_unnamed_function11980$$MicroMessenger +
208
MicroMessenger`___lldb_unnamed_function11980$$MicroMessenger + 208:
-> 0x2a091c: blx 0xe08e0c ;
___lldb_unnamed_function70162$$MicroMessenger
0x2a0920: mov r11, r0
0x2a0922: movw r0, #32442
0x2a0926: movt r0, #436
(lldb) ni
Process 184500 stopped
* thread #1: tid = 0x2d0b4, 0x002a0920
MicroMessenger`___lldb_unnamed_function11980$$MicroMessenger + 212, queue =
'com.apple.main-thread, stop reason = instruction step over
frame #0: 0x002a0920 MicroMessenger`___lldb_unnamed_function11980$$MicroMessenger +
212
MicroMessenger`___lldb_unnamed_function11980$$MicroMessenger + 212:
-> 0x2a0920: mov r11, r0
0x2a0922: movw r0, #32442
0x2a0926: movt r0, #436
0x2a092a: add r0, pc
(lldb) po [[$r0 contentObj] mediaList]
329
<__NSArrayM 0x8725580>(
<WCMediaItem: 0x7e78490>
)
- (id).cxx_construct;
- (id)cityForData;
@property(nonatomic) int commentCount; // @synthesize commentCount;
@property(retain, nonatomic) NSMutableArray *commentUsers; // @synthesize commentUsers;
- (id)comparativePathForPreview;
@property(nonatomic) int createTime; // @synthesize createTime;
@property(retain, nonatomic) WCUrl *dataUrl; // @synthesize dataUrl;
- (void)dealloc;
@property(retain, nonatomic) NSString *desc; // @synthesize desc;
@property(retain, nonatomic) NSString *descPattern; // @synthesize descPattern;
- (void)encodeWithCoder:(id)arg1;
- (BOOL)hasData;
- (BOOL)hasPreview;
- (BOOL)hasSight;
- (id)hashPathForString:(id)arg1;
- (id)imageOfSize:(int)arg1;
@property(nonatomic) struct CGSize imgSize; // @synthesize imgSize;
- (id)init;
- (id)initWithCoder:(id)arg1;
- (BOOL)isValid;
@property(nonatomic) int likeCount; // @synthesize likeCount;
@property(nonatomic) BOOL likeFlag; // @synthesize likeFlag;
@property(retain, nonatomic) NSMutableArray *likeUsers; // @synthesize likeUsers;
- (CDStruct_c3b9c2ee)locationForData;
@property(retain, nonatomic) WCUrl *lowBandUrl; // @synthesize lowBandUrl;
- (id)mediaID;
- (int)mediaType;
@property(retain, nonatomic) NSString *mid; // @synthesize mid;
- (id)pathForData;
- (id)pathForPreview;
330
- (id)pathForSightData;
@property(retain, nonatomic) NSMutableArray *previewUrls; // @synthesize previewUrls;
- (BOOL)saveDataFromData:(id)arg1;
- (BOOL)saveDataFromMedia:(id)arg1;
- (BOOL)saveDataFromPath:(id)arg1;
- (BOOL)savePreviewFromData:(id)arg1;
- (BOOL)savePreviewFromMedia:(id)arg1;
- (BOOL)savePreviewFromPath:(id)arg1;
- (BOOL)saveSightDataFromData:(id)arg1;
- (BOOL)saveSightDataFromMedia:(id)arg1;
- (BOOL)saveSightDataFromPath:(id)arg1;
- (BOOL)saveSightPreviewFromMedia:(id)arg1;
@property(retain, nonatomic) NSString *source; // @synthesize source;
@property(nonatomic) int subType; // @synthesize subType;
@property(retain, nonatomic) NSString *title; // @synthesize title;
@property(retain, nonatomic) NSString *titlePattern; // @synthesize titlePattern;
@property(nonatomic) int type; // @synthesize type;
@property(retain, nonatomic) NSString *userData; // @synthesize userData;
@property(nonatomic) int withCount; // @synthesize withCount;
@property(retain, nonatomic) NSMutableArray *withUsers; // @synthesize withUsers;
- (id)videoStreamForData;
- (id)voiceStreamForData;
@end
From the file names, I am pretty sure that they are the Sight information we’ve been
looking for. Whatever it is ssh or iFunBox that opens the local files; whether it be MobileSafari
or Chrome that opens the URL, you can come to these conclusions:
• The value of pathForData is the local path of the Sight without suffix.
• The value of pathForPreview is the path of the Sight’s preview image without suffix.
• The value of pathForSightData is the local path of the Sight with suffix.
• The value of lowBandUrl is nil, but I guess this value is not nil when the network condition is not
good. In order to save bandwidth, file from this URL may be smaller than file from dataURL on size.
• The value of previewUrls is the Internet URL of the Sight’s preview images.
The prototyping of tweak is finished for now. Let’s comb our thoughts before coding.
332
9.3 Result interpretation
This practice covers Cycript, IDA and LLDB, we’ve prototyped the tweak without strictly
deducing the execution logic of WeChat. Now I will do a brief summary of our thoughts.
333
[5.] iphone/notification_center_widget
[6.] iphone/preference_bundle
[7.] iphone/sbsettingstoggle
[8.] iphone/tool
[9.] iphone/tweak
[10.] iphone/xpc_service
Choose a Template (required): 9
Project Name (required): iOSREWCVideoDownloader
Package Name [com.yourcompany.iosrewcvideodownloader]: com.iosre.iosrewcvideodownloader
Author/Maintainer Name [sam]: sam
[iphone/tweak] MobileSubstrate Bundle filter [com.apple.springboard]: com.tencent.xin
[iphone/tweak] List of applications to terminate upon installation (space-separated, '-'
for none) [SpringBoard]: MicroMessenger
Instantiating iphone/tweak in iosrewcvideodownloader/...
Done.
334
@interface MMTableView : UITableView
@end
This header is composed by picking snippets from other class-dump headers. The existence
of this header is simply for avoiding any warnings or errors when compiling the tweak.
%hook WCContentItemViewTemplateNewSight
%new
- (WCMediaItem *)iOSREMediaItemFromSight
{
id responder = self;
while (![responder isKindOfClass:NSClassFromString(@"WCTimeLineViewController")])
{
if ([responder isKindOfClass:NSClassFromString(@"MMTableViewCell")])
iOSRECell = responder;
else if ([responder isKindOfClass:NSClassFromString(@"MMTableView")])
iOSREView = responder;
responder = [responder nextResponder];
}
iOSREController = responder;
if (!iOSRECell || !iOSREView || !iOSREController)
{
NSLog(@"iOSRE: Failed to get video object.");
return nil;
}
NSIndexPath *indexPath = [iOSREView indexPathForCell:iOSRECell];
int itemIndex = [iOSREController calcDataItemIndex:[indexPath section]];
WCFacade *facade = [(MMServiceCenter *)[%c(MMServiceCenter) defaultCenter]
getService:[%c(WCFacade) class]];
WCDataItem *dataItem = [facade getTimelineDataItemOfIndex:itemIndex];
WCContentItem *contentItem = dataItem.contentObj;
WCMediaItem *mediaItem = [contentItem.mediaList count] != 0 ?
(contentItem.mediaList)[0] : nil;
return mediaItem;
}
%new
- (void)iOSREOnSaveToDisk
{
NSString *localPath = [[self iOSREMediaItemFromSight] pathForSightData];
UISaveVideoAtPathToSavedPhotosAlbum(localPath, nil, nil, nil);
}
%new
- (void)iOSREOnCopyURL
{
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
pasteboard.string = [self iOSREMediaItemFromSight].dataUrl.url;
335
}
- (void)onLongTouch
{
iOSRECounter++;
if (iOSRECounter % 2 == 0) return;
[self becomeFirstResponder];
[saveToDiskMenuItem release];
[copyURLMenuItem release];
}
%end
include theos/makefiles/common.mk
TWEAK_NAME = iOSREWCVideoDownloader
iOSREWCVideoDownloader_FILES = Tweak.xm
iOSREWCVideoDownloader_FRAMEWORKS = UIKit
include $(THEOS_MAKE_PATH)/tweak.mk
after-install::
install.exec "killall -9 MicroMessenger"
336
9.4.5 Test
Compile and install the tweak, then launch WeChat and long press a Sight, it will show our
custom menu, as shown in figure 9-29.
Click “Save to Disk”, the Sight will be saved to local album, as shown in figure 9-30.
337
Figure 9-30 Save the Sight to local album
Click “Copy URL”, and paste it in OPlayer (or any other Apps that support online video
playing), as shown in figure 9-31.
338
9.5 Easter eggs
From figure 9-32, we see our familiar WCDataItem, contentObj and mediaList at the
beginning of this method. If we’ve started with this method, the whole analysis workload would
be reduced by half at least. It more or less enlightens us that although MVC design pattern is a
common trail of thinking in iOS App reverse engineering, if we can occasionally think off the
track, we may get something unexpected and have more fun.
339
9.5.2 Historical transition of WeChat’s headers count
From the historical transition of WeChat’s headers count as figure 9-33 to figure 9-38 show,
we can see how WeChat becomes excellent step by step. A journey of a thousand miles begins
with a single step, kudos to WeChat!
340
Figure 9-23 Headers directories of different WeChat versions
341
Figure 9-35 WeChat 4.5, 2267 headers
342
Figure 9-37 WeChat 5.1, 3537 headers
From WeChat 3.0 to WeChat 6.0, the number of headers has increased from less than 1,000
to more than 5,000, which is a 5+ times amplification. With the global popularity of WeChat, its
headers count is expected to surpass 10,000 sooner or later.
9.6 Conclusion
WeChat is the target of this chapter, we’ve enriched Sight by adding 2 new features, i.e. “
Save to Disk” and “Copy URL”. As a powerful platform, WeChat possesses complicated
343
structure and large amount of code; it was beautifully designed with clearly separated modules
and well organized code. We have already learnt so much from it by just going through its
headers, we can even see the different coding styles of different developers. I believe all of us can
benefit a lot from studying WeChat’s design pattern by reversing it. We will discuss what we
find reversing WeChat on http://bbs.iosre.com, you are welcome to join us.
344
10
Practice 4: Detect And Send iMessages
10.1 iMessage
iMessage is an IM service that Apple implements seamlessly into the stock Messages App
(hereafter referrered to as MobileSMS). It was born in iOS 5 and became better ever since.
Whether it’s plain text, image, audio, or even video, iMessage can handle them with high speed,
security and efficiency. We all love iMessage!
Among all functions of iMessage, detecting if an address supports iMessage, and sending an
iMessage are 2 most interesting functions without doubt. Surprisingly, there are even companies
that make profit from sending spam iMessages, and that’s one of the main reasons that I
developed the Cydia tweak “SMSNinja”. You can’t understand how to defense without knowing
how to attack. In this chapter, we will combine all knowledge points we’ve studied by far and
start from scratch to reverse the functions of detecting and sending iMessages, as sublimation of
the book. All the following operations are finished on iPhone 5, iOS 8.1.
• When you start to compose a message by just finishing recipient’s address without entering the
message body, if iOS detects that the address is iMessage supportive, the placeholder will change
from “Text Message” to “iMessage”, as shown in figure 10-1.
345
Figure 10- 1 Change of placeholder
• When you start to input message body, if the address only supports SMS, the “Send” button beside
the input box will be green; if it supports iMessage, the button will be blue.
• When you hit the “Send” button to send this message, if this is an SMS, the message bubble will be
green, otherwise it will be blue.
These 3 phenomena will appear one after another. Since the process of detecting iMessage
has already happened in the 1st phenomenon, it is enough to act as the cut-in point. We’ll focus
on the 1st phenomenon from now on.
After locating the cut-in point, let’s think together to concretize the phenomenon into a
reverse engineering idea.
What we can observe is visible on UI, i.e. the change from “Text Message” to “iMessage”.
As we’ve already known, visualizations on UI don’t come from nowhere but the data source,
hence by referring to visualizations, we can find the data source, i.e. placeholder, using Cycript.
Placeholder doesn’t come from nowhere but its data source either. The reason why
placeholder changes is that its data source (data source’s data source, and so on. Hereafter
referred to as the Nth data source) changes, like the following pseudo code presents:
id dataSource = ?;
id a = function(dataSource);
id b = function(a);
id c = function(b);
…
id z = function(y);
NSString *placeholder = function(z);
From the above snippet we can know that the original data source is dataSource, its change
in turn results in the change of placeholder. Well, what’s the original data source? In the 1st
phenomenon, our only input is the address, so the original data source is sure to be the address.
346
For detecting iMessages, there should be a conversion from dataSource to placeholder, and this
conversion process is the actual meaning of “detecting iMessages” as well our target in this
section, as shown in figure 10-2.
You may wonder, since figure 10-2 is so straightforward and dataSource is already known,
why don’t we start from it directly and trace placeholder? Then we can reproduce the process
and achieve our goal. Actually, we’re not living in a fairy tale, the real world is usually not
idealized. For one thing, we don’t have the source code of MobileSMS; for the other thing, in
general cases, the conversion is much more complex, as can be illustrated in figure 10-3.
347
Figure 10- 3 Real conversion from dataSource to placeholder
348
Figure 10- 4 Create a new message
Since we are using Cycript to find placeholder, first we should find the view that displays the
current placeholder “Text Message”; they must have a close connection, so get one, get the
other. Right? Let’s do it.
FunMaker-5:~ root# cycript -p MobileSMS
cy# ?expand
expand == true
cy# [[UIApp keyWindow] recursiveDescription]
The view hierarchy of keyWindow is quite rich in content, so we’re not pasting it here. If
you search “Text Message” in the output, you will find that there isn’t any match. Why? Maybe
you’ve already guessed the answer: “Text Message” isn’t in keyWindow. For verification, let’s
see how many windows are there in the current view:
cy# [UIApp windows]
@[#"<UIWindow: 0x1575ca10; frame = (0 0; 320 568); gestureRecognizers = <NSArray:
0x15629c60>; layer = <UIWindowLayer: 0x156e36f0>>",#"<UITextEffectsWindow: 0x1579ab70;
frame = (0 0; 320 568); opaque = NO; autoresize = W+H; gestureRecognizers = <NSArray:
0x1579b300>; layer = <UIWindowLayer: 0x1579adf0>>",#"<CKJoystickWindow: 0x1552bf90;
baseClass = UIAutoRotatingWindow; frame = (0 0; 320 568); hidden = YES;
gestureRecognizers = <NSArray: 0x1552b730>; layer = <UIWindowLayer:
0x1552bdc0>>",#"<UITextEffectsWindow: 0x1683a2e0; frame = (0 0; 320 568); hidden = YES;
gestureRecognizers = <NSArray: 0x1688b9e0>; layer = <UIWindowLayer: 0x168b9ad0>>"]
As we can see, each item starting with “#” is a window, there are 4 of them, and the 1st is
keyWindow. Well, which one contains “Text Message”? As the names suggest, the 2nd and 4th
windows with the keyword “Text” in their names may be our targets. However, the 4th
349
window is even invisible because of its hidden property. This leaves us with the 2nd window
only, let’s test it out in Cycript.
cy# [#0x1579ab70 setHidden:YES]
After this command, not only the input box but also the whole keyboard are hidden, as
shown in figure 10-5:
Now we can confirm that “Text Message” is located right in this window. Keep looking for
it using Cycript.
cy# [#0x1579ab70 setHidden:NO]
cy# [#0x1579ab70 subviews]
@[#"<UIInputSetContainerView: 0x1551fb10; frame = (0 0; 320 568); autoresize = W+H;
layer = <CALayer: 0x1551f950>>"]
cy# [#0x1551fb10 subviews]
@[#"<UIInputSetHostView: 0x1551f5e0; frame = (0 250; 320 318); layer = <CALayer:
0x1551f480>>"]
cy# [#0x1551f5e0 subviews]
@[#"<UIKBInputBackdropView: 0x16827620; frame = (0 65; 320 253); userInteractionEnabled
= NO; layer = <CALayer: 0x1681c3f0>>",#"<_UIKBCompatInputView: 0x157b88d0; frame = (0
65; 320 253); layer = <CALayer: 0x157b8a10>>",#"<CKMessageEntryView: 0x1682ca50; frame =
(0 0; 320 65); opaque = NO; autoresize = W; layer = <CALayer: 0x168ec520>>"]
There are 3 subviews in the above code, which one does “Text Message” reside? Let’s test
them one by one.
cy# [#0x16827620 setHidden:YES]
After the above command, the view looks like figure 10-6, indicating that this view is just
350
keyboard background.
351
Figure 10- 7 Keyboard is hidden
OK, this view is keyboard itself. Thus, we can infer that UIKBInputBackdropView and
UIKBCompatInputView work together to form a keyboard’s view. This official design mode can
be a good reference for 3rd-party keyboard developers and WinterBoard theme makers.
Now that there is the last subview with an explicit name “CKMessageEntryView”, waiting
for our test:
cy# [#0x157b88d0 setHidden:NO]
cy# [#0x1682ca50 setHidden:YES]
The view looks like figure 10-8 after the above commands.
352
Figure 10- 8 Message entry view is hidden
Again, let’s hide these views one by one to locate “Text Message”, and I’ll leave the work to
you as an exercise. After locating “UIView: 0x168dcf10” (Notice, it’s the 2nd UIView object) as
the target, let’s continue with its subviews.
cy# [#0x168dcf10 subviews]
@[#"<CKMessageEntryContentView: 0x16389000; baseClass = UIScrollView; frame = (3 -4;
203.5 57.5); clipsToBounds = YES; opaque = NO; gestureRecognizers = <NSArray:
0x168f0730>; layer = <CALayer: 0x168e41a0>; contentOffset: {0, 0}; contentSize: {203.5,
57}>"]
By hiding these views one by one, we can find that when executing “[#0x16295200
setHidden:YES]”, only “Text Message” is hidden, other control objects are not affected, as
shown in figure 10-9.
354
Figure 10- 10 CKMessageEntryRichTextView.h
Aha, there are lots of placeholders in this file. Among them placeholderLabel and
placeholderText are quite noticeable, is anyone of them our target placeholder? Let’s verify with
Cycript:
cy# [#0x16295200 setPlaceholderText:@"iOSRE"]
355
Now, the view looks like figure 10-12.
Great! placeholderText is exactly the placeholder we’re looking for. To avoid confusion,
hereafter we will refer to placeholder as placeholderText. So far, we have taken the first step in a
long march. Well done!
10.2.3 Find the 1st data source of placeholderText using IDA and LLDB
placeholderText is a property. To modify a property, our first reaction is to use its setter.
We have already changed placeholderText from “Text Message” to “iOSRE” by calling
setPlaceholderText:, does MobileSMS also call this setter to change placeholderText? To verify
our guesses, we need the help of IDA and LLDB.
Since CKMessageEntryTextView comes from ChatKit, our next focus should turn to
framework ChatKit in process MobileSMS, can you get it? OK, drag and drop ChatKit into IDA.
After the initial analysis, locate to [CKMessageEntryTextView setPlaceholderText:], as shown in
figure 10-13.
356
Figure 10- 13 [CKMessageEntryTextView setPlaceholderText:]
libsystem_kernel.dylib`mach_msg_overwrite_trap:
0x316554f8: mov r12, sp
0x316554fc: push {r4, r5, r6, r8}
(lldb) c
Process 200596 resuming
The ASLR offset is 0x1eac000. With this offset, we can set a breakpoint on
[CKMessageEntryTextView setPlaceholderText:] to check whether it is called or not, and if it’s
called, who’s the caller. The base address of this method is shown in figure 10-14, as we can see,
it’s 0x2693BCE0.
357
Figure 10- 14 [CKMessageEntryTextView setPlaceholderText:]
This command is very straightforward; when the breakpoint gets triggered, LLDB prints the
Objective-C description of R2, i.e. the only argument of setPlaceholderText:, then prints LR in
hexadecimal, i.e. the return address of [CKMessageEntryTextView setPlaceholderText:]. If R2 is
“iMessage”, it indicates that the argument is the 1st data source. Meanwhile, since LR is inside
the caller, we can trace the 2nd data source from inside the caller. Clear the address entry and
enter “[email protected]”, then observe when LLDB prints “iMessage”:
<object returned empty description>
(unsigned int) $11 = 0x28768b33
Process 200596 resuming
Command #3 'c' continued the target.
<object returned empty description>
(unsigned int) $13 = 0x28768b33
Process 200596 resuming
Command #3 'c' continued the target.
<object returned empty description>
(unsigned int) $15 = 0x28768b33
Process 200596 resuming
Command #3 'c' continued the target.
Text Message
(unsigned int) $17 = 0x28768b33
Process 200596 resuming
Command #3 'c' continued the target.
358
iMessage
(unsigned int) $19 = 0x28768b33
Process 200596 resuming
Command #3 'c' continued the target.
This address is located in ChatKit. OK, we’ve found the 1st data source of placeholder,
which is the argument of setPlaceholder:, as well got on the way to find the 2nd data source.
What an uneventful achievement, meh.
10.2.4 Find the Nth data source of placeholderText using IDA and
LLDB
I don’t know if you’ve noticed that placeholderText was blank during address editing. Not
until we’ve pressed “return” on the keyword that the placeholderText became “Text Message”
or “iMessage”. In other words, iOS will not detect whether current address supports iMessage
until editing is over; from the perspective of energy saving, this makes sense. Based on this
design, we can firstly edit the recipient’s address, then set a breakpoint and at last press “return”
to finish editing. If the breakpoint gets triggered under such circumstance, we can say that
MobileSMS is stopped during the process of detecting iMessage. Now, let’s search upward from
figure 10-15 to see who is the caller of [CKMessageEntryTextView setPlaceholderText:], as
shown in figure 10-16.
Set placeholder text when updating entry view, this is rather reasonable. However, without
359
any argument, how does [CKMessageEntryView updateEntryView] know whether it should set
placeholderText to “Text Message” or “iMessage”? Judging from this, we can say that
[CKMessageEntryView updateEntryView] must have conducted some internal judges to get the
conclusion that the address supports iMessage, hence changed the 2nd data source. Let’s get
back to IDA to see where the 2nd data source comes from, as shown in figure 10-17.
R2 is the argument of setPlaceholderText:, which is also the 1st data source. And R2 comes
from R5, therefore R5 is the 2nd data source. Where does R5 come from? There is a branch
here, so let’s take a look at its condition, as shown in figure 10-18.
We can see that the branch condition is “[$r0 recipientCount] == 0”. The meaning of
“recipient” is very obvious that it represents the receiver of message. When the recipient count
is 0, namely there’s no recipient, MobileSMS will branch right, otherwise left. In the current
360
case, because there is already one recipient, MobileSMS will probably branch left. It’s very
simple to verify our assumption: input “[email protected]” in the address entry, then set a
breakpoint on any instruction in the right branch and at last press “return” to finish editing. We
can see that the breakpoint is not triggered; as a result, we can confirm that R5 comes from [$r8
__ck_displayName] in the left branch. In other words, [$r8 __ck_displayName] is the 3rd data
source. Where does R8 come from? Scroll up in IDA, we can find that R8 is from [[self
conversation] sendingService] at the beginning of [CKMessageEntryView updateEntryView], as
shown in figure 10-19.
Therefore, [[self conversation] sendingService] is the 4th data source. Let’s verify our
analysis so far with LLDB: input “[email protected]” in the address entry, then set a
breakpoint on “MOV R8, R0” in figure 10-19 and at last press “return” to finish editing. Execute
“po [$r0 __ck_displayName]” when the breakpoint gets triggered and then see whether LLDB
outputs “iMessage”:
(lldb) br s -a 0x28768962
Breakpoint 14: where = ChatKit`-[CKMessageEntryView updateEntryView] + 54, address =
0x28768962
(lldb) br com add 14
Enter your debugger command(s). Type 'DONE' to end.
> po [$r0 __ck_displayName]
> c
> DONE
Text Message
Process 200596 resuming
Command #2 'c' continued the target.
iMessage
Process 200596 resuming
Command #2 'c' continued the target.
From the output, we know that the breakpoint has been triggered twice, and iMessage
361
support was detected in the 2nd time. Since iMessage comes from [[[self conversation]
sendingService] __ck_displayName], what is the return value of [self conversation] and [[self
conversation] sendingService]? No hurry, we will get to them one by one.
Reinput the address and set 2 breakpoints on the first 2 objc_msgSends in
[CKMessageEntryView updateEntryView] respectively. Then press “return” to trigger the
breakpoints:
Process 14235 stopped
* thread #1: tid = 0x379b, 0x2b528948 ChatKit`-[CKMessageEntryView updateEntryView] +
28, queue = 'com.apple.main-thread, stop reason = breakpoint 1.1
frame #0: 0x2b528948 ChatKit`-[CKMessageEntryView updateEntryView] + 28
ChatKit`-[CKMessageEntryView updateEntryView] + 28:
-> 0x2b528948: blx 0x2b5f5f44 ; symbol stub for:
MarcoShouldLogMadridLevel$shim
0x2b52894c: mov r6, r0
0x2b52894e: movw r0, #51162
0x2b528952: movt r0, #2547
(lldb) p (char *)$r1
(char *) $6 = 0x2b60cc16 "conversation"
(lldb) ni
Process 14235 stopped
* thread #1: tid = 0x379b, 0x2b52894c ChatKit`-[CKMessageEntryView updateEntryView] +
32, queue = 'com.apple.main-thread, stop reason = instruction step over
frame #0: 0x2b52894c ChatKit`-[CKMessageEntryView updateEntryView] + 32
ChatKit`-[CKMessageEntryView updateEntryView] + 32:
-> 0x2b52894c: mov r6, r0
0x2b52894e: movw r0, #51162
0x2b528952: movt r0, #2547
0x2b528956: add r0, pc
(lldb) po $r0
CKPendingConversation<0x1587e870>{identifier:'(null)' guid:'(null)'}(null)
The return value of [self conversation] is a CKPendingConversation object. OK, now look at
the next one:
(lldb) c
Process 14235 resuming
Process 14235 stopped
* thread #1: tid = 0x379b, 0x2b52895e ChatKit`-[CKMessageEntryView updateEntryView] +
50, queue = 'com.apple.main-thread, stop reason = breakpoint 2.1
frame #0: 0x2b52895e ChatKit`-[CKMessageEntryView updateEntryView] + 50
ChatKit`-[CKMessageEntryView updateEntryView] + 50:
-> 0x2b52895e: blx 0x2b5f5f44 ; symbol stub for:
MarcoShouldLogMadridLevel$shim
0x2b528962: mov r8, r0
0x2b528964: movw r0, #52792
0x2b528968: movt r0, #2547
(lldb) p (char *)$r1
(char *) $8 = 0x2b6105e1 "sendingService"
(lldb) ni
Process 14235 stopped
* thread #1: tid = 0x379b, 0x2b528962 ChatKit`-[CKMessageEntryView updateEntryView] +
54, queue = 'com.apple.main-thread, stop reason = instruction step over
frame #0: 0x2b528962 ChatKit`-[CKMessageEntryView updateEntryView] + 54
362
ChatKit`-[CKMessageEntryView updateEntryView] + 54:
-> 0x2b528962: mov r8, r0
0x2b528964: movw r0, #52792
0x2b528968: movt r0, #2547
0x2b52896c: add r0, pc
(lldb) po $r0
IMService[SMS]
(lldb) po [$r0 class]
IMServiceImpl
The implementation logic is not too complicated. But there are several branches so that we
can’t make sure which one MobileSMS actually goes. Debug again with LLDB and pay attention
to every branch condition as well as the address of the next instruction.
Process 14235 stopped
* thread #1: tid = 0x379b, 0x2b5f0264 ChatKit`-[CKPendingConversation sendingService],
queue = 'com.apple.main-thread, stop reason = breakpoint 3.1
frame #0: 0x2b5f0264 ChatKit`-[CKPendingConversation sendingService]
ChatKit`-[CKPendingConversation sendingService]:
-> 0x2b5f0264: push {r4, r5, r7, lr}
363
0x2b5f0266: add r7, sp, #8
0x2b5f0268: sub sp, #8
0x2b5f026a: mov r4, r0
(lldb) ni
Process 14235 stopped
……
* thread #1: tid = 0x379b, 0x2b5f027e ChatKit`-[CKPendingConversation sendingService] +
26, queue = 'com.apple.main-thread, stop reason = instruction step over
frame #0: 0x2b5f027e ChatKit`-[CKPendingConversation sendingService] + 26
ChatKit`-[CKPendingConversation sendingService] + 26:
-> 0x2b5f027e: cbz r0, 0x2b5f02a4 ; -[CKPendingConversation
sendingService] + 64
0x2b5f0280: movw r0, #38082
0x2b5f0284: movt r0, #2535
0x2b5f0288: str r4, [sp]
(lldb) p $r0
(unsigned int) $11 = 0
(lldb) ni
Process 14235 stopped
……
* thread #1: tid = 0x379b, 0x2b5f02b8 ChatKit`-[CKPendingConversation sendingService] +
84, queue = 'com.apple.main-thread, stop reason = instruction step over
frame #0: 0x2b5f02b8 ChatKit`-[CKPendingConversation sendingService] + 84
ChatKit`-[CKPendingConversation sendingService] + 84:
-> 0x2b5f02b8: cbz r0, 0x2b5f02c4 ; -[CKPendingConversation
sendingService] + 96
0x2b5f02ba: mov r0, r4
0x2b5f02bc: mov r1, r5
0x2b5f02be: blx 0x2b5f5f44 ; symbol stub for:
MarcoShouldLogMadridLevel$shim
(lldb) p $r0
(unsigned int) $12 = 341691792
(lldb) ni
Process 14235 stopped
……
* thread #1: tid = 0x379b, 0x2b5f02c2 ChatKit`-[CKPendingConversation sendingService] +
94, queue = 'com.apple.main-thread, stop reason = instruction step over
frame #0: 0x2b5f02c2 ChatKit`-[CKPendingConversation sendingService] + 94
ChatKit`-[CKPendingConversation sendingService] + 94:
-> 0x2b5f02c2: cbnz r0, 0x2b5f032c ; -[CKPendingConversation
sendingService] + 200
0x2b5f02c4: movw r0, #35464
0x2b5f02c8: movt r0, #2535
0x2b5f02cc: add r0, pc
(lldb) p $r0
(unsigned int) $13 = 341691792
(lldb) ni
Process 14235 stopped
……
* thread #1: tid = 0x379b, 0x2b5f032e ChatKit`-[CKPendingConversation sendingService] +
202, queue = 'com.apple.main-thread, stop reason = instruction step over
frame #0: 0x2b5f032e ChatKit`-[CKPendingConversation sendingService] + 202
ChatKit`-[CKPendingConversation sendingService] + 202:
-> 0x2b5f032e: pop {r4, r5, r7, pc}
ChatKit`-[CKPendingConversation refreshStatusForAddresses:withCompletionBlock:]:
0x2b5f0330: push {r4, r5, r6, r7, lr}
0x2b5f0332: add r7, sp, #12
0x2b5f0334: push.w {r8, r10, r11}
364
The execution flow of MobileSMS is very evident now. There are 3 conditional branches,
which are CBZ, CBZ and CBNZ respectively. At each time, the value of R0 is 0, 341691792 and
341691792 respectively. As a result, we can know that the execution flow is shown in figure 10-
21.
365
instance variable _composeSendingService. In other words, _composeSendingService is the 6th
data source. In that case, we just need to find where this instance variable is written and there
comes the 7th data sources.
Click _OBJC_IVAR_$_CKPendingConversation._composeSendingService to focus the
cursor on it. Then press “x” to inspect xrefs to this variable, as shown in figure 10-23.
In Objective-C, write operation of a property is often carried out through its setter. Thus to
find the 7th data source, we should set a breakpoint on [CKPendingConversation
setComposeSendingService:] and check out who’s the caller. Repeat our previous operations:
366
reinput the address, set breakpoint at the beginning of [CKPendingConversation
setComposeSendingService:], and then press “return” to trigger the breakpoint:
Process 30928 stopped
* thread #1: tid = 0x78d0, 0x30b3665c ChatKit`-[CKPendingConversation
setComposeSendingService:], queue = 'com.apple.main-thread, stop reason = breakpoint 1.1
frame #0: 0x30b3665c ChatKit`-[CKPendingConversation setComposeSendingService:]
ChatKit`-[CKPendingConversation setComposeSendingService:]:
-> 0x30b3665c: movw r1, #41004
0x30b36660: movt r1, #2535
0x30b36664: add r1, pc
0x30b36666: ldr r1, [r1]
(lldb) p/x $lr
(unsigned int) $0 = 0x30b3656d
R6 is from R1, so R1 is the 9th data source. And where does R1 come from? Since we are
inside sub_26984530 and R1 is read without being written, so R1 comes from the caller of
sub_26984530, right? Let’s take a look at the cross references to sub_26984530 to look for its
possible callers, as shown in figure 10-27.
367
Figure 10- 27 Inspect cross references
Refresh sending service? This name is very informative. Let’s head directly to
[CKPendingConversation refreshComposeSendingServiceForAddresses:withCompletionBlock:]
as shown in figure 10-28 for more details. In this method, sub_26984530 is obviously the 2nd
argument of refreshStatusForAddresses:withCompletionBlock:, namely the completionBlock, as
shown in figure 10-28.
368
Figure 10- 29 Caller of sub_26984530
As we can see, sub_26984530 isn’t called explicitly. Instead, its address is stored in R6 to
where the execution flow jumps, and then sub_26984530 is called implicitly. As a result, the 9th
data source comes from sub_26984444. Well done! We have achieved a lot so far. Let’s keep
searching for the occurrences of the 9th data source, as shown in figure 10-30.
There are several branches inside this subroutine to determine whether it should assign
[IMServiceImpl smsService] or [IMServiceImpl iMessageService] to R1. Let’s figure out the
branch conditions, starting from figure 10-31.
369
Figure 10- 31 Look for the 10th data source
That is to say, the value of the 10th data source is determined by the combination of R0 and
R1, both of whom assume the responsibility of being the 11th data source, hereafter referred to
as 11th data source A and 11th data source B respectively. At the same time, the above pseudo
code can also be written as the following:
- (BOOL)supportIMessage
{
if (11thDataSourceA == 2 || 11thDataSourceB != 0) return YES;
return NO;
}
Get back to figure 10-31 to trace the 11th data source; R0 comes from "UXTB.W R0, R8".
370
Figure 10- 32 UXTB
According to the ARM official document in figure 10-32, UXTB is used to zero extend the 8-
bit value in R8 to a 32-bit value and then put it into R0, who is a 32-bit register. In other words,
R0 comes from R8, so R8 is the 12th data source A; and from the facts that arg_0 = 0x8, R8 =
*(R7 + arg_0) = *(R7 + 0x8), R7 = SP + 0xC, we can know that R8 = *(SP + 0x14), which
means *(SP + 0x14) is the 13th data source A. Well, where does *(SP + 0x14) come from? It
definitively doesn’t come from nowhere, so before “LDR.W R8, [R7,#8]”, there must be an
instruction writing something into *(SP + 0x14), right? That instruction is where the 14th data
source A resides. As a result, we have to trace back to the instruction that writes to *(SP + 0x14).
Although the idea sounds straightforward, things are much harder than you think. The
reason is that SP, unlike those rarely used registers, is affected by lots of instructions. Say, push
and pop both change the value of SP, so *(SP + 0x14) may appears in the form of *(SP’ + offset)
in other instructions due to the change of SP. And what’s even worse is that the value of offset is
undetermined yet. Sounds like we’re getting into troubles! From now on, we have to find every
single operation that writes into *(SP’ + offset) before “LDR.W R8, [R7,#8]”, and then check
whether (SP + 0x14) equals to (SP’ + offset). Thanks to the frequent and irregular changes of SP,
the following section is the hardest part of this book. So please stay very close! Let’s start from
“LDR.W R8, [R7,#8]” and trace back every single operation that writes into *(SP’ + offset) for
now.
In sub_26984444, the first 4 instructions before “LDR.W R8, [R7,#8]” are all SP related. We
use SP1~SP4 to mark the values of SP before the execution of the current instruction, as shown
371
in figure 10-33.
Before and after the execution of “PUSH {R4-R7,LR}”, the values of SP are SP1 and SP2
respectively, can you understand? Next, we will try to deduce how SP changes instruction by
instruction.
“PUSH {R4-R7,LR}” pushes 5 registers, i.e. R4, R5, R6, R7 and LR into stack. Every register
is 32-bit i.e. 4 bytes. Since the ARM stack is full descending, therefore SP2 = SP1 - 5 * 0x4 = SP1 -
0x14. “ADD R7, SP, #0xC” is equivalent to R7 = SP2 + 0xC, which has no influence on SP. The
value of var_10 in “STR.W R8, [SP,#0xC+var_10]!” is -0x10, so this instruction equals to
“STR.W R8, [SP,#-4]”, i.e. *(SP2 - 0x4) = R8 and this instruction doesn’t have impact on SP
either. “SUB SP, SP, #4” equals to SP3 = SP2 - 0x4. According to our marking rules, 13th data
source A is *(SP2 + 0x14). No instruction inside sub_26984444 has written to this address before
“LDR.W R8, [R7,#8]”, so the value of *(SP2 + 0x14) must come from the caller of
sub_26984444. Similarly, R1 is read without being written inside sub_26984444, it must also
come from the caller of sub_26984444, right? If you are still confused, please review this
paragraph until you understand it clearly, and then you’re allowed to continue.
Alright, both the 13th data source A and the 11th data source B come from the caller of
sub_26984444. So our next specific task is to find the 14th data source A and the 12th data source
B in the caller of sub_26984444.
Reinput the recipient’s address, set a breakpoint at the beginning of sub_26984444, then
372
press “return” to trigger the breakpoint:
Process 30928 stopped
* thread #1: tid = 0x78d0, 0x30b36444 ChatKit`__71-[CKPendingConversation
refreshStatusForAddresses:withCompletionBlock:]_block_invoke, queue = 'com.apple.main-
thread, stop reason = breakpoint 7.1
frame #0: 0x30b36444 ChatKit`__71-[CKPendingConversation
refreshStatusForAddresses:withCompletionBlock:]_block_invoke
ChatKit`__71-[CKPendingConversation
refreshStatusForAddresses:withCompletionBlock:]_block_invoke:
-> 0x30b36444: push {r4, r5, r6, r7, lr}
0x30b36446: add r7, sp, #12
0x30b36448: str r8, [sp, #-4]!
0x30b3644c: sub sp, #4
(lldb) p/x $lr
(unsigned int) $39 = 0x331f0d75
ChatKit`__copy_helper_block_:
0x30b364c4: ldr r1, [r1, #20]
0x30b364c6: adds r0, #20
(lldb) ni
Process 30928 stopped
* thread #1: tid = 0x78d0, 0x331f0d74 IMCore`___lldb_unnamed_function425$$IMCore + 1360,
queue = 'com.apple.main-thread, stop reason = instruction step over
frame #0: 0x331f0d74 IMCore`___lldb_unnamed_function425$$IMCore + 1360
IMCore`___lldb_unnamed_function425$$IMCore + 1360:
-> 0x331f0d74: movw r0, #26972
0x331f0d78: movt r0, #2081
0x331f0d7c: add r0, pc
0x331f0d7e: ldr r1, [r0]
We’re inside IMCore now. Since we have just calculated the value of LR without offset to
be 0x2903ED75, as well IMCore shares the same ASLR offset with ChatKit, so just drag and drop
IMCore into IDA and jump to 0x2903ED75 when the initial analysis has been finished, as shown
in figure 10-34.
373
Figure 10- 34 Caller of sub_26984444
See, another implicit call from sub_2903E824, and 2 of 4 instructions before “BLX R6” has
relation with SP. To make it more convenient for reading, I’ll take instructions before and after
calling "BLX R6" from their respective images and put them together into one figure. The
process and result is shown in figure 10-35 and figure 10-36.
374
Figure 10- 35 Before instructions of 2 images are put together
Let’s keep looking for the 14th data source A, which has been written into *(SP2 + 0x14), do
you still remember? OK, mark the SPs in loc_2903ED6A just like what we’ve done, as shown in
figure 10-37.
375
Figure 10- 37 Mark SPs
Then we should go through loc_2903ED6A from its 1st instruction to check how SP
changes here.
“LDR R3, [SP,#0xA8+var_98]” equals to R3 = *(SP1 + 0xA8 + var_98). And var_98 = -
0x98, as shown in figure 10-38.
As a result, R3 = *(SP1 + 0x10) and this instruction has no influence on the value of SP.
“MOV R2, R8” has nothing to do with SP; the value of var_A8 in “STR R1,
[SP,#0xA8+var_A8]” is -0xA8, so *SP1 = R1, which doesn’t influence SP too; “MOV R1, R5” has
nothing to do with SP either. These SPs are really confusing for sure, so take a break and let me
summarize it.
Our goal is to find where *(SP2 + 0x14) is written.
376
Because SP2 = SP1 - 0x14 and *SP1 = R1,
Therefore, “STR R1, [SP,#0xA8+var_A8]” is the place where *(SP2 + 0x14) is written, and
R1 in this instruction is the 14th data source A! Also, we can easily find that R5 in “MOV R1, R5”
is the 12th data source B. The logics of tracing from 13th data source A to 14th data source A
and from 11th data source B to 12th data source B go across images, bringing high complexity.
With the illustration of figure 10-39, I hope everything is more intuitive. We strongly suggest
you comb through everything by referring to this figure before moving on to the next
paragraph.
Before we continue our analysis, let’s verify our deduction so far with LLDB: reinput the
address and set the breakpoint on “STR R1, [SP,#0xA8+var_A8]” to print R1, i.e. the 14th data
source A. Next, execute “ni” until we reach “MOV R1, R5”, print R5 i.e. the 12th data source B.
Then we’ll experience an image switch from IMCore to ChatKit, so execute “si” to reach “CMP
R0, #2” and print R0, i.e. the 13th data source A. Finally, we execute “ni” until “TST.W R1,
#0xFF” to print R1, i.e. the 11th data source B. Press “return” to trigger the breakpoint and
follow the above steps to check whether their values equal to each other like figure 10-39 shows.
(lldb) br s -a 0x30230D6E
Process 37477 stopped
* thread #1: tid = 0x9265, 0x30230d6e IMCore`___lldb_unnamed_function425$$IMCore + 1354,
queue = 'com.apple.main-thread, stop reason = breakpoint 11.1
frame #0: 0x30230d6e IMCore`___lldb_unnamed_function425$$IMCore + 1354
IMCore`___lldb_unnamed_function425$$IMCore + 1354:
-> 0x30230d6e: str r1, [sp]
377
0x30230d70: mov r1, r5
0x30230d72: blx r6
0x30230d74: movw r0, #26972
(lldb) p $r1
(unsigned int) $27 = 0
(lldb) ni
Process 37477 stopped
* thread #1: tid = 0x9265, 0x30230d70 IMCore`___lldb_unnamed_function425$$IMCore + 1356,
queue = 'com.apple.main-thread, stop reason = instruction step over
frame #0: 0x30230d70 IMCore`___lldb_unnamed_function425$$IMCore + 1356
IMCore`___lldb_unnamed_function425$$IMCore + 1356:
-> 0x30230d70: mov r1, r5
0x30230d72: blx r6
0x30230d74: movw r0, #26972
0x30230d78: movt r0, #2081
(lldb) p $r5
(unsigned int) $28 = 1
(lldb) ni
Process 37477 stopped
* thread #1: tid = 0x9265, 0x30230d72 IMCore`___lldb_unnamed_function425$$IMCore + 1358,
queue = 'com.apple.main-thread, stop reason = instruction step over
frame #0: 0x30230d72 IMCore`___lldb_unnamed_function425$$IMCore + 1358
IMCore`___lldb_unnamed_function425$$IMCore + 1358:
-> 0x30230d72: blx r6
0x30230d74: movw r0, #26972
0x30230d78: movt r0, #2081
0x30230d7c: add r0, pc
(lldb) si
Process 37477 stopped
* thread #1: tid = 0x9265, 0x2db76444 ChatKit`__71-[CKPendingConversation
refreshStatusForAddresses:withCompletionBlock:]_block_invoke, queue = 'com.apple.main-
thread, stop reason = instruction step into
frame #0: 0x2db76444 ChatKit`__71-[CKPendingConversation
refreshStatusForAddresses:withCompletionBlock:]_block_invoke
ChatKit`__71-[CKPendingConversation
refreshStatusForAddresses:withCompletionBlock:]_block_invoke:
-> 0x2db76444: push {r4, r5, r6, r7, lr}
0x2db76446: add r7, sp, #12
0x2db76448: str r8, [sp, #-4]!
0x2db7644c: sub sp, #4
(lldb) ni
……
Process 37477 stopped
* thread #1: tid = 0x9265, 0x2db7645c ChatKit`__71-[CKPendingConversation
refreshStatusForAddresses:withCompletionBlock:]_block_invoke + 24, queue =
'com.apple.main-thread, stop reason = instruction step over
frame #0: 0x2db7645c ChatKit`__71-[CKPendingConversation
refreshStatusForAddresses:withCompletionBlock:]_block_invoke + 24
ChatKit`__71-[CKPendingConversation
refreshStatusForAddresses:withCompletionBlock:]_block_invoke + 24:
-> 0x2db7645c: cmp r0, #2
0x2db7645e: bne 0x2db7647a ; __71-[CKPendingConversation
refreshStatusForAddresses:withCompletionBlock:]_block_invoke + 54
0x2db76460: movw r0, #19376
0x2db76464: movt r0, #2535
(lldb) p $r0
(unsigned int) $29 = 0
(lldb) ni
……
Process 37477 stopped
378
* thread #1: tid = 0x9265, 0x2db7647e ChatKit`__71-[CKPendingConversation
refreshStatusForAddresses:withCompletionBlock:]_block_invoke + 58, queue =
'com.apple.main-thread, stop reason = instruction step over
frame #0: 0x2db7647e ChatKit`__71-[CKPendingConversation
refreshStatusForAddresses:withCompletionBlock:]_block_invoke + 58
ChatKit`__71-[CKPendingConversation
refreshStatusForAddresses:withCompletionBlock:]_block_invoke + 58:
-> 0x2db7647e: tst.w r1, #255
0x2db76482: movt r0, #2535
0x2db76486: add r0, pc
0x2db76488: ldr r0, [r0]
(lldb) p $r1
(unsigned int) $30 = 1
The output verifies our analysis, the 14th data source A is 0 and 12th data source B is 1.
Next, we need to focus on IMCore to keep looking for 15th data source A and 13th data source
B. Let’s get started from the 15th data source A.
The 15th data source A is presented in figure 10-40 intuitively.
It comes either from "MOVS R1, #1" or "MOVS R1, #0". In other words, the 15th data
source A is either 0 or 1. Things are getting interesting.
If I remember correctly, since the 11th data source A, the value of data source A has never
changed, the values of 11th, 12th, 13th, 14th and 15th data source A are all the same, which are
either 0 or 1. However, the previous pseudo code is like this:
- (BOOL)supportIMessage
{
if (11thDataSourceA == 2 || 11thDataSourceB!= 0) return YES;
return NO;
}
Because the 11th data source A is either 0 or 1, under no circumstance can it be 2. In that
case, data source A becomes meaningless in our tracing, right? Hence the pseudo code can be
simplified as follows:
- (BOOL)supportIMessage
379
{
if (11thDataSourceB != 0) return YES;
return NO;
}
As a result, we can ignore data source A and concentrate on the finding of the 13th data
source B, hereafter referred to as the 13th data source. Since the 12th data source B is R5, we can
confirm that 13th data source must be written into R5 by a certain instruction, right? Click R5
and IDA will highlight all R5s as yellow to make it more convenient for tracing in the sea of
ARM assembly. Keep reversing to find where R5 is written.
When we’re searching upward to locate the 13th data source, we see there’re 4 branches to
loc_2903EAE0, as shown in figure 10-41.
In figure 10-41, the left 3 branches all contain a "MOVS R5, #0", which contradicts the
result of R5 = 1, so loc_2903EAE0 must be reached via the rightmost branch, and the 13th data
source should be located in this branch. Follow this branch for R5.
When we trace into loc_2903EA3E, the situation is similar to loc_2903EAE0. Although
there are 3 branches upon it, the 1st and 2nd branches both contain a "MOVS R5, #0" as shown
in figure 10-42, so they can be excluded for now.
As a result, the actual upstream is the 3rd branch, i.e. loc_2903E9C4, which has 2 branches
380
upon it. Now that both branches contain “MOVS R5, #1”, which is the actual one?
Reinput the address and set breakpoints on both branches. Then press “return” to see which
breakpoint will be triggered, that’s our answer. Here, I’ll leave the LLDB operation to you,
please finish it independently. After you’ve done, you will have a deeper understanding and find
that the left branch is the actual one MobileSMS chose, as shown in figure 10-43.
Now, we have found the 13th data source, it’s a constant with value 1. You may wonder, if
13th data source is a constant, does 14th data source still exist? The data source clues seem to be
interrupted, what should we to do next? Good point.
In the previous figures, there’re several “MOVS R5, #0”. Although the 13th data source
comes from “MOVS R5, #1”, which seems to be a constant, according to programmatic
paradigm, there should be a conditional branch to determine whether “MOVS R5, #0” or
“MOVS R5, #1” gets executed, just like the pseudo code below.
if (iMessageIsAvailable) R5 = 1;
else R5 = 0;
To represent in our familiar IDA graph view, it looks like figure 10-44.
381
Figure 10- 44 Pseudo IDA graph view
From a macro point of view, this conditional branch is actually the 14th data source, right? I
bet you’ve realized that the above pseudo code can be rewritten as below:
R5 = iMessageIsAvailable;
If you can understand this, then our next task is to keep tracing back to analyze every
branch we meet. If different branches result in writing different values into R5, we need to
figure out what’s the branch condition, and this condition is our target data source. Let’s head to
figure 10-45 and start from here.
If the process branches left, R5 is possibly to be set 0. Since the branch condition is the
382
return value of objc_msgSend, let’s set a breakpoint here and see what method it is:
Process 132234 stopped
* thread #1: tid = 0x2048a, 0x331f092e IMCore`___lldb_unnamed_function425$$IMCore + 266,
queue = 'com.apple.main-thread, stop reason = breakpoint 5.1
frame #0: 0x331f092e IMCore`___lldb_unnamed_function425$$IMCore + 266
IMCore`___lldb_unnamed_function425$$IMCore + 266:
-> 0x331f092e: blx 0x332603b0 ; symbol stub for: objc_msgSend
0x331f0932: mov r8, r0
0x331f0934: cmp.w r8, #0
0x331f0938: bne 0x331f08e2 ; ___lldb_unnamed_function425$$IMCore +
190
(lldb) p (char *)$r1
(char *) $6 = 0x2f7d81d9 "countByEnumeratingWithState:objects:count:"
(lldb) po $r0
<__NSArrayI 0x16706930>(
mailto:[email protected]
)
As we can see, this method returns the count of the recipient array. If the array is not
empty, MobileSMS will branch right. Actually, the recipient array is not empty, therefore this
branch condition is not met, MobileSMS will branch right, which doesn’t change R5. OK, search
upward for the next branch, as shown in figure 10-46.
In figure 10-46, what are R11 and R8 respectively? We can get a straightforward answer
from IDA that R11 is from figure 10-47.
The initial value of R11 is 0. Each time before executing “CMP R11, R8”, R11 will increase
by 1. In this way, R11 plays the role of a counter. “CMP” performs subtraction operation, if
there’s borrow, then carry flag will be set 0, otherwise carry flag will be set 1. The branch
instruction here is “BCC”, in which “CC” means “Carry Clear”, i.e. “if carry flag is 0”.
383
Therefore, if R11 - R8 produces borrow, i.e. R8 is greater than R11, then MobileSMS will branch
right, otherwise it will branch left. So the key here is R8, as shown in figure 10-48.
NSArray is an array of recipients, thus R8 is the recipient count. If there’s more than 1
recipients, then since R11 is 1 when “CMP R11, R8” gets executed for the first time, we can
know that R8 is greater than R11 and MobileSMS will branch right, as shown in figure 10-49.
384
Figure 10- 49 Branch
The branch condition inside loc_2903E8E6 is R0. If R0 == 0, then branch left, meaning this
address doesn’t support iMessage. Otherwise branch right and reach figure 10-50.
The branch condition in figure 10-50 is still R0. If R0 == 2 then branch left, iMessage is not
supported. Otherwise branch right and go back to figure 10-46. Note, these 3 blocks of code
don’t change the value of R8. As a result, R0 at the bottom of loc_2903E8E6 is very import; as
long as R0 != 0 && R0 != 2, the branch in figure 10-46 is useless. That’s because R11 keeps
increasing while R8 stays the same, MobileSMS will eventually branch left and come to the
conclusion that iMessage is supported. So judging from all information above, we can think of
R0 as the essential branch condition in this loop. Do you still remember what I’ve just said? “If
different branches result in writing different values into R5, we need to figure out what’s the
branch condition, and this condition is our target data source”. Thus, R0 is the 14th data source.
Next, let’s check with LLDB what are these objc_msgSends in figure 10-49, as well the
385
source of R0:
Process 154446 stopped
* thread #1: tid = 0x25b4e, 0x331f0900 IMCore`___lldb_unnamed_function425$$IMCore + 220,
queue = 'com.apple.main-thread, stop reason = breakpoint 1.1
frame #0: 0x331f0900 IMCore`___lldb_unnamed_function425$$IMCore + 220
IMCore`___lldb_unnamed_function425$$IMCore + 220:
-> 0x331f0900: blx 0x332603b0 ; symbol stub for: objc_msgSend
0x331f0904: ldr r0, [sp, #40]
0x331f0906: mov r2, r4
0x331f0908: ldr r1, [sp, #20]
(lldb) p (char *)$r1
(char *) $7 = 0x2f7d897a "removeObject:"
(lldb) po $r0
<__NSArrayM 0x170ec120>(
mailto:[email protected]
)
(lldb) po $r2
mailto:[email protected]
(lldb) ni
……
Process 154446 stopped
* thread #1: tid = 0x25b4e, 0x331f090a IMCore`___lldb_unnamed_function425$$IMCore + 230,
queue = 'com.apple.main-thread, stop reason = instruction step over
frame #0: 0x331f090a IMCore`___lldb_unnamed_function425$$IMCore + 230
IMCore`___lldb_unnamed_function425$$IMCore + 230:
-> 0x331f090a: blx 0x332603b0 ; symbol stub for: objc_msgSend
0x331f090e: ldr r1, [sp, #24]
0x331f0910: blx 0x332603b0 ; symbol stub for: objc_msgSend
0x331f0914: cbz r0, 0x331f0946 ; ___lldb_unnamed_function425$$IMCore +
290
(lldb) p (char *)$r1
(char *) $10 = 0x2f7d8113 "valueForKey:"
(lldb) po $r2
mailto:[email protected]
(lldb) po $r0
{
"mailto:[email protected]" = 1;
}
(lldb) po [$r0 class]
__NSCFDictionary
(lldb) ni
……
Process 154446 stopped
* thread #1: tid = 0x25b4e, 0x331f0910 IMCore`___lldb_unnamed_function425$$IMCore + 236,
queue = 'com.apple.main-thread, stop reason = instruction step over
frame #0: 0x331f0910 IMCore`___lldb_unnamed_function425$$IMCore + 236
IMCore`___lldb_unnamed_function425$$IMCore + 236:
-> 0x331f0910: blx 0x332603b0 ; symbol stub for: objc_msgSend
0x331f0914: cbz r0, 0x331f0946 ; ___lldb_unnamed_function425$$IMCore +
290
0x331f0916: cmp r0, #2
0x331f0918: beq 0x331f09ca ; ___lldb_unnamed_function425$$IMCore +
422
(lldb) p (char *)$r1
(char *) $14 = 0x2f7de6f3 "integerValue"
(lldb) po $r0
1
(lldb) po [$r0 class]
386
__NSCFNumber
(lldb) c
As we can see, only one instruction writes into this address. Double click this instruction to
jump to the beginning of sub_2903E824, as shown in figure 10-52.
387
Figure 10- 52 sub_2903E824
The 16th data source comes from R5, which is the 17th data source. The 17th data source is
from R1, which is the 18th data source, and it is read without being written, meaning R1 comes
from the caller of sub_2903E824, right? Let’s take a look at the subroutine’s cross references, as
shown in figure 10-53.
“Calculate service for sending new compose”, as the name suggests, its function is quite
clear. Double click the first cross reference to check its caller, as shown in figure 10-54.
388
Figure 10- 54 Caller of sub_2903E824
To avoid any implicit calling, let’s first make sure the caller of sub_2903E824 is actually
IMChatCalculateServiceForSendingNewCompose. Reinput the address, set a breakpoint at the
first instruction of sub_2903E824 and then press “return” to trigger the breakpoint:
Process 154446 stopped
* thread #1: tid = 0x25b4e, 0x331f0824 IMCore`___lldb_unnamed_function425$$IMCore, queue
= 'com.apple.main-thread, stop reason = breakpoint 2.1
frame #0: 0x331f0824 IMCore`___lldb_unnamed_function425$$IMCore
IMCore`___lldb_unnamed_function425$$IMCore:
-> 0x331f0824: push {r4, r5, r6, r7, lr}
0x331f0826: add r7, sp, #12
0x331f0828: push.w {r8, r10, r11}
0x331f082c: sub sp, #144
(lldb) p/x $lr
(unsigned int) $17 = 0x331f067b
(lldb)
The ASLR offset is 0xa1b2000, so LR without offset is 0x2903E67B, which is exactly inside
IMChatCalculateServiceForSendingNewCompose. OK, since the 18th data source is from R5,
then R5 is the 19th data source. Further, the 19th data source is from the return value of
objc_msgSend, so this return value is the 20th data source. With everything ready, let’s reveal
this mysterious objc_msgSend:
Process 154446 stopped
* thread #1: tid = 0x25b4e, 0x331f0668 IMCore`IMChatCalculateServiceForSendingNewCompose
+ 688, queue = 'com.apple.main-thread, stop reason = breakpoint 3.1
frame #0: 0x331f0668 IMCore`IMChatCalculateServiceForSendingNewCompose + 688
IMCore`IMChatCalculateServiceForSendingNewCompose + 688:
-> 0x331f0668: blx 0x332603b0 ; symbol stub for: objc_msgSend
0x331f066c: mov r5, r0
0x331f066e: add r0, sp, #44
0x331f0670: mov r1, r5
(lldb) p (char *)$r1
389
(char *) $18 = 0x33274340 "_currentIDStatusForDestinations:service:listenerID:"
(lldb) po $r0
<IDSIDQueryController: 0x15dcb010>
(lldb) po $r2
<__NSArrayM 0x170e7900>(
mailto:[email protected]
)
(lldb) po $r3
com.apple.madrid
(lldb) po [$r3 class]
__NSCFConstantString
(lldb) x/10 $sp
0x001e4548: 0x3b3f52b8 0x001e459c 0x3b4227b4 0x3c01b05c
0x001e4558: 0x00000001 0x00000000 0x170828d0 0x001e4594
0x001e4568: 0x2baac821 0x00000000
(lldb) po 0x3b3f52b8
__kIMChatServiceForSendingIDSQueryControllerListenerID
(lldb) po [0x3b3f52b8 class]
__NSCFConstantString
(lldb) c
390
* thread #1: tid = 0x19052, 0x30230668 IMCore`IMChatCalculateServiceForSendingNewCompose
+ 688, queue = 'com.apple.main-thread, stop reason = breakpoint 6.1
frame #0: 0x30230668 IMCore`IMChatCalculateServiceForSendingNewCompose + 688
IMCore`IMChatCalculateServiceForSendingNewCompose + 688:
-> 0x30230668: blx 0x302a03b0 ; symbol stub for: objc_msgSend
0x3023066c: mov r5, r0
0x3023066e: add r0, sp, #44
0x30230670: mov r1, r5
(lldb) po $r2
<__NSArrayM 0x17820560>(
tel:+86PhoneNumber
)
Aha, the output clearly supports our statements: 2 iMessage supportive emails and 1
iMessage supportive phone number all return 1, while the other 3 iMessage unsupportive
addresses return 2. What’s more, we know the code name of iMessage is “Madrid”. Mission
complete! Cheers!
391
Figure 10- 55 “Send” button
We can send an iMessage by pressing “Send”, and the whole process will be animated on
UI. Like what we did in section 10.2, let’s consider how to turn clues on UI into ideas in reverse
engineering:
“Send” button is supposed to be a UIView object, or more specifically and possibly, a
UIButton object; we press this button to call its response method; overall response actions
include refreshing UI, sending the iMessage, adding a sending record and so on. That’s to say,
the action of sending iMessages is only a subset of all response actions.
In “New Message” view, our inputs include recipient addresses and message contents,
they’re the original data source. Since we can get all response actions, and the action of sending
iMessages is supposed to take the original data source as arguments, so they can be references
for us to filter the action of sending iMessages out of all response actions. Unlike what we’ve
done in the last section, which was tracing back from tail to head, in the following sections,
we’re tracing from head to tail, showing you another common scenario of iOS reverse
engineering.
In a nutshell, our thoughts are: first uncover response method of “Send” button with
Cycript, then overview all response actions with IDA and class-dump, as well filter those
suspicious methods out. Finally, test the filtered methods and locate our target.
392
10.3.2 Find response method of “Send” button using Cycript
Since we’ve already known that the superview of “Send” button is a CKMessageEntryView
object in section 10.2, we can repeat what we’ve done in section 10.2.2 and get the superview
without further tests:
FunMaker-5:~ root# cycript -p MobileSMS
cy# ?expand
expand == true
cy# [UIApp windows]
@[#"<UIWindow: 0x14e12fa0; frame = (0 0; 320 568); gestureRecognizers = <NSArray:
0x14e11f50>; layer = <UIWindowLayer: 0x14ee4570>>",#"<UITextEffectsWindow: 0x14fa6000;
frame = (0 0; 320 568); opaque = NO; gestureRecognizers = <NSArray: 0x14fa66d0>; layer =
<UIWindowLayer: 0x14fa5fc0>>",#"<CKJoystickWindow: 0x14d22310; baseClass =
UIAutoRotatingWindow; frame = (0 0; 320 568); hidden = YES; gestureRecognizers =
<NSArray: 0x14d21ab0>; layer = <UIWindowLayer: 0x14d22140>>"]
cy# [#0x14fa6000 subviews]
@[#"<UIInputSetContainerView: 0x14d03930; frame = (0 0; 320 568); autoresize = W+H;
layer = <CALayer: 0x14d03770>>"]
cy# [#0x14d03930 subviews]
@[#"<UIInputSetHostView: 0x14d033f0; frame = (0 250; 320 318); layer = <CALayer:
0x14d03290>>"]
cy# [#0x14d033f0 subviews]
@[#"<UIKBInputBackdropView: 0x160441a0; frame = (0 65; 320 253); userInteractionEnabled
= NO; layer = <CALayer: 0x16043b60>>",#"<_UIKBCompatInputView: 0x14f78a20; frame = (0
65; 320 253); layer = <CALayer: 0x14f78920>>",#"<CKMessageEntryView: 0x160c6180; frame =
(0 0; 320 65); opaque = NO; autoresize = W; layer = <CALayer: 0x16089920>>"]
cy# [#0x160c6180 subviews]
@[#"<_UIBackdropView: 0x16069d40; frame = (0 0; 320 65); opaque = NO; autoresize = W+H;
userInteractionEnabled = NO; layer = <_UIBackdropViewLayer: 0x14d627c0>>",#"<UIView:
0x16052920; frame = (0 0; 320 0.5); layer = <CALayer: 0x160529d0>>",#"<UIButton:
0x1605a8b0; frame = (266 27; 53 33); opaque = NO; layer = <CALayer:
0x16052a00>>",#"<UIButton: 0x14d0b2c0; frame = (266 30; 53 26); hidden = YES; opaque =
NO; gestureRecognizers = <NSArray: 0x160f9800>; layer = <CALayer:
0x1605a140>>",#"<UIButton: 0x1606f040; frame = (15 33.5; 25 18.5); opaque = NO;
gestureRecognizers = <NSArray: 0x14d07970>; layer = <CALayer:
0x1605aaa0>>",#"<_UITextFieldRoundedRectBackgroundViewNeue: 0x160e5ed0; frame = (55 8;
209.5 49.5); opaque = NO; userInteractionEnabled = NO; layer = <CALayer:
0x160d3a10>>",#"<UIView: 0x160a3390; frame = (55 8; 209.5 49.5); clipsToBounds = YES;
opaque = NO; layer = <CALayer: 0x160b8ab0>>",#"<CKMessageEntryWaveformView: 0x160c4750;
frame = (15 25.5; 251 35); alpha = 0; opaque = NO; userInteractionEnabled = NO; layer =
<CALayer: 0x160c47e0>>"]
Among these views, “UIView: 0x16052920” is where “iMessage” resides, do you remember?
As a result, the following 2 UIButtons are quite suspicious, my intuition tells me “Send” is one of
them. Meanwhile, the hidden property of the 2nd UIButton is set to YES, indicating its
invisibility. Well, let’s test the 1st UIButton, “UIButton: 0x1605a8b0” with Cycript:
cy# [#0x1605a8b0 setHidden:YES]
393
Figure 10- 56 Hide “Send”
Neat. After pressing this UIButton, an iMessage will be sent; a UIButton and its response
action are always associated with [UIControl addTarget:action:forControlEvents:]. Since
UIControl offers another method actionsForTarget:forControlEvent: to find its own response
method, let’s see what method will get called after pressing “Send” with this method:
cy# [#0x1605a8b0 setHidden:NO]
cy# button = #0x1605a8b0
#"<UIButton: 0x1605a8b0; frame = (266 27; 53 33); hidden = YES; opaque = NO; layer =
<CALayer: 0x16052a00>>"
cy# [button allTargets]
[NSSet setWithArray:@[#"<CKMessageEntryView: 0x160c6180; frame = (0 0; 320 65); opaque =
NO; autoresize = W; layer = <CALayer: 0x16089920>>"]]]
cy# [button allControlEvents]
64
cy# [button actionsForTarget:#0x160c6180 forControlEvent:64]
@["touchUpInsideSendButton:"]
394
Figure 10- 57 [CKMessageEntryView touchUpInsideSendButton:button]
Go to [CKTranscriptController messageEntryViewSendButtonHit:CKMessageEntryView]
in IDA. This is a pretty simple method, as shown in figure 10-58.
By overviewing this method, I bet you can easily locate the actual sending action in [self
sendComposition:[CKMessageEntryView compositionWithAcceptedAutocorrection]]. Let’s see
what’s [self compositionWithAcceptedAutocorrection] in Cycript:
395
#"<CKComposition: 0x160b79d0> text:'iMessage {\n}' subject:'(null)'"
It’s an object of CKComposition, which clearly contains message text and subject. Keep
digging into sendComposition:, as shown in figure 10-59.
The implementation is rather complicated. As we said earlier in this section, we’ll try to
avoid massive use of LLDB, thus let’s first go over all branches in this method to glimpse the
possible execution flows, then debug the uncertain ones with LLDB. We start from
loc_268D427C, as shown in figure 10-60.
If the iMessage “hasContent”, branches right. According to figure 10-56, our content is
“iMessage”, so branch right and arrive at figure 10-61.
396
Figure 10- 61 branch
What’s R0? Get back to the beginning of sendComposition:, as shown in figure 10-63.
So the result of “TST.W R0, #4” is 0, branch right and arrive at loc_268D4604, as shown in
figure 10-64.
397
Figure 10- 64 loc_268D4604
Whether iOS “isSendingMessage”? We don’t know if the timing is before or after pressing
“Send” button, so let’s test them both. Before pressing “Send”:
cy# [#0x15537200 isSendingMessage]
0
So, [self isSendingMessage] returns 0 anyway. Branch left and arrive at loc_268D4636, as
shown in figure 10-65.
Can we send the iMessage to the recipient? Since the recipient is a valid iMessage account,
of course we can! Branch left and arrive at figure 10-66.
Can we send the composition? Since we’ve already printed the CKComposition object,
there doesn’t seem to be any problems. Branch left and arrive at figure 10-67.
398
Figure 10- 67 Branch
The branch condition R0 comes from the return value of the 2nd objc_msgSend. Search
upwards, we can find R5 in figure 10-60; it’s determining if the iMessage “hasContent” again.
Therefore, branch right and arrive at figure 10-68.
This is an informative figure. If you look close, you’ll discover that most objc_msgSends are
just refreshing UI, making the last objc_msgSend, i.e. [R4 sendMessage:R2] more eye-catching.
What’s R4 and R2? Look upwards, you’ll see they’re CKTranscriptController and the argument
of [self sendComposition:], respectively. Let’s continue analyzing from [CKTranscriptController
399
sendMessage:], as shown in figure 10-69.
Another method full of branches. But after giving a glimpse to the possible execution flows
just like what we did to sendComposition:, we can find that most branches are just making
preparations, only “_startCreatingNewMessageForSending:” looks promising. Let’s take a look
at its implementation, as shown in figure 10-70.
400
Figure 10- 70 [CKTranscriptController _startCreatingNewMessageForSending:]
Again, it’s a method full of branches. Overview the implementation, I think you’ll notice the
method “sendMessage:newComposition:” just like me. The method occurs twice in
[CKTranscriptController _startCreatingNewMessageForSending:], as shown in the 2 red blocks
in figure 10-71.
401
Figure 10- 71 [CKTranscriptController _startCreatingNewMessageForSending:]
402
Figure 10- 73 [CKConversation sendMessage:onService:newComposition:]
The execution flow of this method is more straightforward than the previous ones. Skim it
briefly, we can see phrases like “Sending message with guid: %@”, “ => Sending account: %@”
and “=> Recipients: [%@]”, most of which are arguments of _CKLogExternal. If MobileSMS
has already started recording these into syslog, doesn’t it prove that “send iMessage” is
happening? What’s more, we’ve seen the suspicious keyword “sendMessage:” again in figure 10-
74:
What’s the receiver and arguments of “sendMessage:”? Let’s find them in IDA; the receiver,
R0, comes from R5. Where does R5 come from? Keep looking upwards until loc_2691F726, as
shown in figure 10-75.
403
Figure 10- 75 loc_2691f726
Double click the first xref to jump to “STR R0, [SP,#0xA4+var_98]”. Around here, R0
comes from [R6 chat]; R6 first appears in the beginning of [CKConversation
sendMessage:onService:newComposition:], it’s “self”; so the receiver of “sendMessage:” is [self
chat]. Back to figure 10-74, we can see the argument of “sendMessage:” is from R6. Go a little
upwards to loc_2691F6F4, R6 is set in “LDR R6, [SP,#0xA4+var_80]”, as shown in figure 10-77.
What’s next? We’ve performed the same operation just now, so I’ll leave some figures (from
404
10-78 to 10-80) rather than texts as references for you to follow:
405
Figure 10- 78 Inspect cross references
406
So the argument of [[self chat] sendMessage:] is exactly the first argument of [self
sendMessage:onService:newComposition:]. Well, what’re the types and values of [self chat] and
the argument? We’ve gone out of clue in IDA, so it’s time to bring out LLDB.
First compose an iMessage, then set a breakpoint on the objc_msgSend right under
“sendMessage:” in figure 10-74, which is at the end of [CKConversation
sendMessage:onService:newComposition:]. After that, press “Send” button to trigger the
breakpoint:
Process 233590 stopped
* thread #1: tid = 0x39076, 0x30ad1846 ChatKit`-[CKConversation
sendMessage:onService:newComposition:] + 686, queue = 'com.apple.main-thread, stop
reason = breakpoint 1.1
frame #0: 0x30ad1846 ChatKit`-[CKConversation sendMessage:onService:newComposition:]
+ 686
ChatKit`-[CKConversation sendMessage:onService:newComposition:] + 686:
-> 0x30ad1846: blx 0x30b3bf44 ; symbol stub for:
MarcoShouldLogMadridLevel$shim
0x30ad184a: movw r0, #49322
0x30ad184e: movt r0, #2541
0x30ad1852: add r0, pc
(lldb) p (char *)$r1
(char *) $0 = 0x32b26146 "sendMessage:"
(lldb) po $r0
<IMChat 0x5ef2ce0> [Identifier: [email protected] GUID:
iMessage;-;[email protected] Persistent ID: [email protected] Account:
26B3EC90-783B-4DEC-82CF-F58FBBB22363 Style: - State: 3 Participants: 1 Room Name:
(null) Display Name: (null) Last Addressed: (null) Group ID: F399B0B5-800F-47A4-A66C-
72C43ACC0428 Unread Count: 0 Failure Count: 0]
(lldb) po $r2
IMMessage[from=(null); msg-subject=(null); account:(null); flags=100005; subject='<<
Message Not Loggable >>' text='<< Message Not Loggable >>' messageID: 0 GUID:'966C2CD6-
3710-4D0F-BCEF-BCFEE8E60FE9' date:'437730968.559627' date-delivered:'0.000000' date-
read:'0.000000' date-played:'0.000000' empty: NO finished: YES sent: NO read: NO
delivered: NO audio: NO played: NO from-me: YES emote: NO dd-results: NO dd-scanned: YES
error: (null)]
(lldb) ni
The output contains exactly what we want: [IMChat sendMessage:IMMessage]. There’s one
thing to mention: after printing out all necessary information, I’ve executed an extra “ni”
command and heard a familiar “message sent” text tone. This phenomenon indicates that the
operation of “send iMessage” is indeed performed inside [IMChat sendMessage:IMMessage].
Because the prefixes of IMChat and IMMessage are both IM, they come from a library other
than ChatKit; the lowest level “send iMessage” function in ChatKit stops at [CKConversation
sendMessage:onService:newComposition:]. We can confirm for now that if we’re able to
construct an IMChat object and an IMMessage object, we can successfully send an iMessage.
Old problems solved, new problems occur: how do we compose these 2 objects? Let’s see if
407
there’re any clues in class-dump headers.
To compose objects of IMChat and IMMessage from scratch, we need to see if there’re any
constructors or initializers in their headers. Let’s start from IMChat.h and search for methods
with the name “init”:
- (id)_initWithDictionaryRepresentation:(id)arg1 items:(id)arg2
participantsHint:(id)arg3 accountHint:(id)arg4;
- (id)init;
- (id)_initWithGUID:(id)arg1 account:(id)arg2 style:(unsigned char)arg3
roomName:(id)arg4 displayName:(id)arg5 items:(id)arg6 participants:(id)arg7;
Although they seem to be initializers, there’re various arguments, which we don’t know
how to compose. The clues break, what’s next?
Do you still remember how we managed to find the receiver of “sendMessage:”? Yes, it’s
[self chat]; self is a CKConversation object. Since [CKConversation chat] returns an IMChat
object, let’s analyze this method in IDA to see if there’s any clue, as shown in figure 10-81.
[CKConversation chat] simply returns the instance variable _chat. This scenario is quite
familiar, isn’t it? We’ve met a similar situation analyzing _composeSendingService in figure 10-
22. Once again, we have to let LLDB take the job for now. Delete this iMessage conversation (to
delete this CKConversation obejct) and create a new iMessage (to create a new CKConversation
object), then set a breakpoint on [CKConversation setChat:]. Press “Send” to trigger the
breakpoint:
Process 248623 stopped
* thread #1: tid = 0x3cb2f, 0x30ad277c ChatKit`-[CKConversation setChat:], queue =
'com.apple.main-thread, stop reason = breakpoint 13.1
frame #0: 0x30ad277c ChatKit`-[CKConversation setChat:]
ChatKit`-[CKConversation setChat:]:
-> 0x30ad277c: movw r3, #55168
0x30ad2780: movt r3, #2541
0x30ad2784: add r3, pc
0x30ad2786: ldr r3, [r3]
(lldb) po $r2
408
<IMChat 0x1594f7e0> [Identifier: [email protected] GUID:
iMessage;-;[email protected] Persistent ID: [email protected] Account:
26B3EC90-783B-4DEC-82CF-F58FBBB22363 Style: - State: 0 Participants: 1 Room Name:
(null) Display Name: (null) Last Addressed: (null) Group ID: (null) Unread Count: 0
Failure Count: 0]
(lldb) p/x $lr
(unsigned int) $20 = 0x30acf625
The keyword appears in the cache. Naturally, let’s grep those decached files:
snakeninnys-MacBook:~ snakeninny$ grep -r __kIMChatRegistryDidRegisterChatNotification
/Users/snakeninny/Code/iOSSystemBinaries/8.1_iPhone5/
Binary file
/Users/snakeninny/Code/iOSSystemBinaries/8.1_iPhone5//dyld_shared_cache_armv7s matches
grep:
/Users/snakeninny/Code/iOSSystemBinaries/8.1_iPhone5//System/Library/Caches/com.apple.xp
c/sdk.dylib: Too many levels of symbolic links
grep:
/Users/snakeninny/Code/iOSSystemBinaries/8.1_iPhone5//System/Library/Frameworks/OpenGLES
.framework/libLLVMContainer.dylib: Too many levels of symbolic links
410
Binary file
/Users/snakeninny/Code/iOSSystemBinaries/8.1_iPhone5//System/Library/PrivateFrameworks/I
MCore.framework/IMCore matches
You may have already guessed from the results that both IMCore and ChatKit are in charge
of iMessage related operations, but IMCore is lower level than ChatKit; ChatKit receives the
commands from the user and hands them to IMCore for processing, then IMCore passes the
result back to ChatKit for UI animation. By way of analogy, you can consider MobileSMS as a
restaurant, ChatKit as the waiter and IMCore as the cook. Can you get it?
Naturally, drag and drop IMCore into IDA and search for
“__kIMChatRegistryDidRegisterChatNotification” globally, the results are shown in figure 10-
82.
Good. Let’s double click the first row and take a look at its context, as shown in figure 10-83.
After seeing the keyword “PostNotification”, we know the notification that ChatKit
received is right from here. Since IMChat is the 2nd argument, i.e. R3, and R3 comes from [SP,
#0x98+var_60]. You know what to do by referring to figure 10-84 and figure 10-85.
411
Figure 10- 84 Inspect cross references
According to the above figures, IMChat comes from the 2nd argument of [IMChatRegistry
_registerChatDictionary:forChat:isIncoming:newGUID:], whose caller is:
Process 248623 stopped
* thread #1: tid = 0x3cb2f, 0x33235944 IMCore`___lldb_unnamed_function2048$$IMCore,
queue = 'com.apple.main-thread, stop reason = breakpoint 17.1
412
frame #0: 0x33235944 IMCore`___lldb_unnamed_function2048$$IMCore
IMCore`___lldb_unnamed_function2048$$IMCore:
-> 0x33235944: push {r4, r5, r6, r7, lr}
0x33235946: add r7, sp, #12
0x33235948: push.w {r8, r10, r11}
0x3323594c: sub.w r4, sp, #64
(lldb) po $r3
<IMChat 0x147c7f30> [Identifier: [email protected] GUID:
iMessage;-;[email protected] Persistent ID: [email protected] Account:
26B3EC90-783B-4DEC-82CF-F58FBBB22363 Style: - State: 0 Participants: 1 Room Name:
(null) Display Name: (null) Last Addressed: (null) Group ID: (null) Unread Count: 0
Failure Count: 0]
(lldb) p/x $lr
(unsigned int) $27 = 0x3323646f
413
Figure 10- 86 IMChatRegistry.h
According to line 44, we know that IMChatRegistry is a singleton, we can get the registry by
calling [IMChatRegistry sharedInstance]. So easy!
Next question, where does the argument of chatForIMHandle: come from? It definitely
comes from its caller. It’s LLDB’s show time again.
Process 248623 stopped
* thread #1: tid = 0x3cb2f, 0x33236d8c IMCore`___lldb_unnamed_function2054$$IMCore,
queue = 'com.apple.main-thread, stop reason = breakpoint 21.1
frame #0: 0x33236d8c IMCore`___lldb_unnamed_function2054$$IMCore
IMCore`___lldb_unnamed_function2054$$IMCore:
-> 0x33236d8c: push {r4, r5, r6, r7, lr}
0x33236d8e: add r7, sp, #12
0x33236d90: str r11, [sp, #-4]!
0x33236d94: sub sp, #20
(lldb) po $r2
[IMHandle: <[email protected]:<None>:cn> (Person: <No AB Match>) (Account:
P:+86PhoneNumber]
(lldb) p/x $lr
(unsigned int) $32 = 0x30a8dca5
414
IMCore anymore. Like we’ve just said, we’re jumping between IMCore and ChatKit, and
ChatKit’s ASLR offset happens to be 0xa1b2000 too, so let’s head to ChatKit to see if 0x268dbca5
is there:
415
Figure 10- 88 Tracing the NSArray
R2 comes from R6, and R6 comes from [SP, #0xA8+var_80]. The same pattern has
reappeared, so as usual, I’ll replace text illustration with figure references, as shown in figure 10-
89 and 10-90.
You may have already found that things are getting a little bit different. “STR R0,
[SP,#0xA8+var_80]” is just storing an initialized NSMutableArray into [SP, #0xA8+var_80], it
doesn’t contain any IMHandle yet. Hehe, since it’s an NSMutableArray, it can be extended by
addObject:, which could happen in the 2nd “LDR R0, [SP,#0xA8+var_80]” of figure 10-89. Let’s
jump there for a look, as shown in figure 10-91.
416
Figure 10- 91 Trace IMHandle
You’ll find it is indeed an addObject:, and by its context, you’ll see its argument comes from
imHandleWithID:alreadyCanonical:. As the name suggests, it returns an IMHandle object. It’s
getting closer, let’s set a breakpoint on the first objc_msgSend in figure 10-91 to reconstruct the
prototype of imHandleWithID:alreadyCanonical:.
Process 343388 stopped
* thread #1: tid = 0x53d5c, 0x30a84e98 ChatKit`-[CKTranscriptController sendMessage:] +
516, queue = 'com.apple.main-thread, stop reason = breakpoint 1.1
frame #0: 0x30a84e98 ChatKit`-[CKTranscriptController sendMessage:] + 516
ChatKit`-[CKTranscriptController sendMessage:] + 516:
-> 0x30a84e98: blx 0x30b3bf44 ; symbol stub for:
MarcoShouldLogMadridLevel$shim
0x30a84e9c: mov r2, r0
0x30a84e9e: ldr r0, [sp, #40]
0x30a84ea0: mov r1, r11
(lldb) p (char *)$r1
(char *) $0 = 0x30b55fb4 "imHandleWithID:alreadyCanonical:"
(lldb) po $r0
IMAccount: 0x145e30d0 [ID: 26B3EC90-783B-4DEC-82CF-F58FBBB22363 Service:
IMService[iMessage] Login: P:+86PhoneNumber Active: YES LoginStatus: Connected]
(lldb) po $r2
[email protected]
(lldb) p $r3
(unsigned int) $3 = 0
Both arguments are revealed; the 1st is my iMessage address, the 2nd is 0, i.e. NO in BOOL.
The receiver is an IMAccount object, where is it from? As shown in figure 10-91, R0 comes from
[SP, #0xA8+var_84], so according to figure 10-92 and 10-93, IMAccount comes from
[[IMAccountController sharedInstance] __ck_defaultAccountForService:[CKConversation
sendingService]].
417
Figure 10- 92 Inspect cross references
OK, let’s figure out what’s [CKConversation sendingService]. Set a breakpoint on the 2nd
objc_msgSend of figure 10-93 and trigger it:
Process 343388 stopped
* thread #1: tid = 0x53d5c, 0x30a84e08 ChatKit`-[CKTranscriptController sendMessage:] +
372, queue = 'com.apple.main-thread, stop reason = breakpoint 2.1
frame #0: 0x30a84e08 ChatKit`-[CKTranscriptController sendMessage:] + 372
ChatKit`-[CKTranscriptController sendMessage:] + 372:
-> 0x30a84e08: blx 0x30b3bf44 ; symbol stub for:
MarcoShouldLogMadridLevel$shim
0x30a84e0c: str r0, [sp, #36]
0x30a84e0e: movw r0, #23756
0x30a84e12: add r2, sp, #44
(lldb) p (char *)$r1
(char *) $4 = 0x30b55f95 "__ck_defaultAccountForService:"
(lldb) po $r2
IMService[iMessage]
(lldb) po [$r2 class]
IMServiceImpl
So it’s an IMServiceImpl object. How do we get such an object? In fact, we’ve already done
this in section 10.2. Open IMServiceImpl.h, as shown in figure 10-94.
418
Figure 10- 94 IMServiceImpl.h
By far, we’ve completely reversed the generation of an IMChat object. Let’s try it out in
Cycript:
FunMaker-5:~ root# cycript -p MobileSMS
cy# service = [IMServiceImpl iMessageService]
#"IMService[iMessage]"
cy# account = [[IMAccountController sharedInstance]
__ck_defaultAccountForService:service]
#"IMAccount: 0x145e30d0 [ID: 26B3EC90-783B-4DEC-82CF-F58FBBB22363 Service:
IMService[iMessage] Login: P:+86PhoneNumber Active: YES LoginStatus: Connected]"
cy# handle = [account imHandleWithID:@"[email protected]" alreadyCanonical:NO]
#"[IMHandle: <[email protected]:<None>:cn> (Person: <No AB Match>) (Account: P:+86
MyPhoneNumber]"
cy# chat = [[IMChatRegistry sharedInstance] chatForIMHandle:handle]
#"<IMChat 0x15809000> [Identifier: [email protected] GUID:
iMessage;-;[email protected] Persistent ID: [email protected] Account:
26B3EC90-783B-4DEC-82CF-F58FBBB22363 Style: - State: 3 Participants: 1 Room Name:
(null) Display Name: (null) Last Addressed: (null) Group ID: 6592DD84-4B34-4D54-BB40-
E2AB17B2FC67 Unread Count: 0 Failure Count: 0]"
Gorgeous! To finally make it, we need to construct an IMMessage object for sending. Let’s
move it now.
Open IMMessage.h, as shown in figure 10-96.
419
Figure 10- 96 IMMessage.h
Although the “text” is “not loggable”, the “flag” is 100005. Let’s try it out in Cycript:
cy# [IMMessage instantMessageWithText:@"iOSRE test" flags:100005]
-[__NSCFString string]: unrecognized selector sent to instance 0x1468c140
Cycript reminds us that NSString failed to respond to @selector(string). In other words, the
1st argument is not an NSString object, but instead some class that can respond to
@selector(string). Let’s try to get some clues in figure 10-96, do you see “NSAttributedString
*_text” in line 17? According to the official documents, NSAttributedString does have a property
named “string”, whose getter is “- (NSString *)string”, as shown in figure 10-97.
420
Figure 10- 97 [NSAttributedString string]
An IMMessage object appears, but as you can see, the value of flags is presented in
hexadecimal instead of decimal. Only a tiny fix is needed to make it correct:
cy# message = [IMMessage instantMessageWithText:attributedString flags:1048581]
#"IMMessage[from=(null); msg-subject=(null); account:(null); flags=100005; subject='<<
Message Not Loggable >>' text='<< Message Not Loggable >>' messageID: 0 GUID:'61012DF3-
1C0F-4DED-9451-975E5771D493' date:'447412682.028256' date-delivered:'0.000000' date-
read:'0.000000' date-played:'0.000000' empty: NO finished: YES sent: NO read: NO
delivered: NO audio: NO played: NO from-me: YES emote: NO dd-results: NO dd-scanned: YES
error: (null)]”
421
Figure 10- 98 iMessage delivered
422
know a certain private class better. In this chapter, when we came across some suspicious
variables, we didn’t strictly analyze them with IDA and LLDB, but by only browsing
corresponding headers, guessing their prototypes and usages, then testing with Cycript to
achieve our goals. The famous leader in my country Deng Xiaoping once said:"It doesn’t matter
whether a cat is white or black, as long as it catches mice", which applies to iOS reverse
engineering too.
4. Pick out the exact line, i.e. call chain from the plane with LLDB
LLDB plays a significant role throughout the whole chapter. Although we’ve tried to limit
its usage in section 10.3, we have to bring it out when tracing function callers and dynamically
analyzing arguments. Compared with GDB, LLDB is more iOS supportive, there’re rare crashes
and bugs; it works great on Objective-C objects, making our debugging much smoother. When
we were working on the detecting and sending of iMessages, LLDB helped us clarify great
amounts of details; based on the careful analysis of tightly related data sources, we’ve abstracted
a short piece of the working principles and designing ideas of iMessage: MobileSMS plays the
423
role of a post office; its building materials, office equipments and clerks are all from ChatKit,
while IMCore is the postman. When I have a letter to send, I’ll go to the post office and put the
letter in the mailbox. Then a clerk will sort the letters out and hand them to the postman; later
the postman will give feedback of the delivery progress and result to the clerk, who are in
charge of informing me what’s happening to my letter. This kind of closed-loop service is very
Apple-ish; MobileSMS, ChatKit and IMCore play different roles, bringing Apple fans terrific user
experiences. If we can learn how Apple design and implement all kinds of services via iOS
reverse engineering, put them together and make them our own, it’ll bring dramatically
improvement to the elegance, design and robustness of our products, which is unattainable by
only reading the official documents.
@class IMHandle;
425
if ([address rangeOfString:@"@"].location != NSNotFound) formattedAddress =
[@"mailto:" stringByAppendingString:address];
else formattedAddress = [@"tel:" stringByAppendingString:address];
NSDictionary *status = [[IDSIDQueryController sharedInstance]
_currentIDStatusForDestinations:@[formattedAddress] service:@"com.apple.madrid"
listenerID:@"__kIMChatServiceForSendingIDSQueryControllerListenerID"];
return [status[formattedAddress] intValue];
}
%new
- (void)sendMadridMessageToAddress:(NSString *)address withText:(NSString *)text
{
IMServiceImpl *service = [IMServiceImpl iMessageService];
IMAccount *account = [[IMAccountController sharedInstance]
__ck_defaultAccountForService:service];
IMHandle *handle = [account imHandleWithID:address alreadyCanonical:NO];
IMChat *chat = [[IMChatRegistry sharedInstance] chatForIMHandle:handle];
NSAttributedString *attributedString = [[NSAttributedString alloc]
initWithString:text];
IMMessage *message = [IMMessage instantMessageWithText:attributedString
flags:1048581];
[chat sendMessage:message];
[attributedString release];
}
%end
include theos/makefiles/common.mk
TWEAK_NAME = iOSREMadridMessenger
iOSREMadridMessenger_FILES = Tweak.xm
iOSREMadridMessenger_PRIVATE_FRAMEWORKS = IDS ChatKit IMCore
include $(THEOS_MAKE_PATH)/tweak.mk
after-install::
install.exec "killall -9 MobileSMS"
426
10.5.5 Test with Cycript
Compile and install the tweak, then ssh into iOS and execute the following commands:
FunMaker-5:~ root# cycript -p MobileSMS
cy# [UIApp madridStatusForAddress:@"[email protected]"]
1
cy# [UIApp sendMadridMessageToAddress:@"[email protected]" withText:@"Sent from
iOSREMadridMessenger"]
10.6 Conclusion
Being one of the key services since iOS 5, iMessage is greatly enhanced in iOS 8. Whether
it’s plain text, image, audio, or even video, iMessage can handle them all. Although detecting
and sending iMessages is only a tip of iceberg in all iMessage operations, we’ve switched among
IDS, ChatKit and IMCore, as well felt the high complexity of the entire iMessage service.
According to our analysis, the class in charge of iMessage accounts is IMAccountController; the
class of iMessage accounts is IMAccount; recipient class is IMHandle; a message conversation is
427
an IMChat or CKConversation object; IMChatRegistry is responsible for managing all IMChats;
an iMessage is an IMMessage or CKComposition object. For those IM developers, the design of
iMessage can be a precious reference.
If you’re still unsatisfied with this chapter and want to dig deeper into iMessage, try the
following bonuses:
• Send iMessage with ChatKit (Tip: you can get a CKConversationfrom object from an IMChat
object).
If you can digest the contents of this chapter inside-out, and prototype the tweak without
referring to the book, congratulations, you’re a fairly outstanding iOS reverse engineer now,
you are qualified and encouraged to step toward a higher goal, say, jailbreak. Before you begin
the new journey, share your knowledge and experiences with us on http://bbs.iosre.com to
help build the jailbreak community. Thank you!
428
Jailbreaking for Developers, An Overview
Much has been said about Apple’s closed approach to selling devices and running an app
platform. But what few know is that behind closed doors there’s a massive ecosystem of libraries
and hardware features waiting to be unlocked by developers. All of the APIs Apple uses
internally to build their services, applications, and widgets are available once the locks are
broken via a process called jailbreaking. Most of them are written in Objective-C, a dynamic
language that provides very rich introspection capabilities and has a culture of self-describing
code. Further tearing down barriers, most people install something called CydiaSubstrate
shortly after jailbreaking, which allows running custom code inside every existing process on the
device. This is very powerful—not only have we broken out of the walled garden into the rest
of the forest, all of the footpaths are already labeled. Building code that targets jailbroken iOS
devices involves unique ways of inspecting APIs, injecting code into processes, and writing code
that modifies existing classes and finalized behaviors of the system.
The APIs implemented on iOS can be divided into four categories: framework-level
Objective-C APIs, app-level Objective-C classes, C-accessible APIs and JavaScript-accessible
APIs. Objective-C frameworks are the most easily accessible. Normally the structure of a
component is only accessible to the programmer and those the source code or documentation
have been made available to, but compiled Objective-C binaries include method tables
describing all of the classes, protocols, methods and instance variables contained in the binary.
An entire family of “class-dump” tools exists to take these method tables and convert them to
header-like output for easy consumption by adventurous programmers. Calling these APIs is as
simple as adding the generated headers to your project and linking with the framework or
library. The second category of app internal classes may be inspected via the same tools, but are
not linkable via standard tools. To get to those classes, one has to have code injected into the
app in question and use the Objective-C runtime function objc_getClass to get a reference to the
class; from there, one can call APIs via the headers generated by the tool. Inspecting C-level
429
functions are more difficult. No information about what the parameters or data structures are
baked into the binaries, only the names of exported functions. The developer tools that ship
with OS X come with a disassembler named “otool” which can dump the instructions used to
implement the code in the device. Paired with knowledge of ARM assembly, the type
information can be reconstructed by hand with much effort. This is much more cumbersome
than with Objective-C code. Luckily, some of the components implemented in C are shared
with OS X and have headers available in the OS X SDK, or are available as open-source from
Apple. JavaScript-level APIs are most often facades over Objective-C level APIs to make
additional functionality accessible to web pages hosted inside the iTunes, App Store, iCloud and
iAd sections of the operating system.
Putting the APIs one has uncovered to use often requires having code run inside the process
where their implementations are present. This can be done using the
DYLD_INSERT_LIBRARIES environment variable on systems that use dyld, but this facility
offers very few provisions for crash protection and can easily leave a device in a state where a
restore is necessary. Instead, the gold standard on iOS devices is a system known as Cydia
Substrate, a package that standardizes process injection and offers safety features to limit the
damage testing new code can do. Once Cydia Substrate is installed, one needs only to drop a
dynamic library compiled for the device in /Library/ MobileSubstrate/DynamicLibraries, and
substrate will load it automatically in every process on the device. Filtering to only a specific
process can be achieved by dropping a property list of the same name alongside it with details
on which process or grouping of processes to filter to. Once inside, one can register for events,
call system APIs and perform any of the same behaviors that the process normally could. This
applies to apps that come preinstalled on the device, apps available from the App Store, the
window manager known as SpringBoard, UI services that apps can make use of such as the mail
composer, and background services such as the media decoder daemon. It is important to note
that any state that the injected code has will be unique to the process it’s injected into and to
share state mandates use inter-process communication techniques such as sockets, fifos, mach
ports and shared memory.
Modifying existing code is where it really starts to get powerful and allows tweaking existing
functionality of the device in simple or even radical ways. Because Objective-C method lookup
is all done at runtime and the runtime offers APIs to modify methods and classes, it is really
430
straightforward to replace the implementations of existing methods with new ones that add new
functionality, suppress the original behavior or both. This is known as method hooking and in
Objective-C is done through a complicated dance of calls to the class_addMethod, class_
getInstanceMethod, method_getImplementation and method_setImplementation runtime
functions. This is very unwieldy; tools to automate this have been built. The simplest is Cydia
Substrate’s own MSHookMessage function. It takes a class, the name of the method you want to
replace, the new implementation, and gives back the original implementation of the function so
that the replacement can perform the original behavior if necessary. This has been further
automated in the Logos Objective-C preprocessor tool, which introduces syntax specifically for
method hooking and is what most tweaks are now written in. Writing Logos code is as simple
as writing what would normally be an Objective-C method implementation, and sticking it
inside of a %hook ClassName ... %end block instead of an @implementation
ClassName ... %end block, and calling %orig() instead of [super ...]. Simple tweaks to how the
system behaves can often done by replacing a single method with a different implementation,
but complicated adjustments often require assembling numerous method hooks. Since most of
iOS is implemented in Objective-C, the vast majority of tweaks need only these building blocks
to apply the modifications they require. For the lower levels of the system that are written in C,
a more complicate hooking approach is required. The lowest level and most compatible way of
doing so is to simply rewrite the assembly instructions of the victim function. This is very
dangerous and does not compose well when many developers are modifying the same parts of
the system. Again, CydiaSubstrate introduces an API to automate this in form of
MSHookFunction. Just like MSHookMessage, one needs only to pass in the target function, new
replacement implementation function, and it applies the hook and returns the old
implementation that the new replacement can call if necessary. With the tools the community
has made available, the details of the very complex mechanics of hooking have been abstracted
and simplified to the point where they’re hidden from view and a developer can concentrate on
what new features they’re adding.
Combining these techniques unique to the jailbreak scene, with those present in the
standard iOS and OS X development communities yields a very flexible and powerful tool chest
for building features and experiences that the world hasn’t seen yet.
Ryan Petrich
431
Evading the Sandbox
As a security measure and to keep apps on the device from sharing data or interfering with
each other, iOS includes a security system known as the sandbox. The sandbox blocks access to
files, network sockets, bootstrap service names, and the ability to spawn subprocesses. Part of
the jailbreaking process involves modifying the sandbox so that all processes can load Cydia
Substrate, but much of the sandbox is left intact to respect the security and privacy of the user’s
data.
With each new release, Apple further improves the sandbox to improve privacy and
security. When building extensions or tweaks that need to share information across processes or
persist data to disk, this can be restrictive. One approach is to survey the sandbox restrictions
that exist on the processes where the extension is to be run, and choose file paths and names
based on them. This is common, but can leave oneself stranded when Apple tightens the
tourniquet and as of iOS 8 there is no location that all processes can read and write successfully.
A better approach is to do all of the interesting work inside a privileged process such as
SpringBoard, backboardd or even a manually created launch daemon of your own. Child
processes can then send work to the privileged service. This ensures that as the sandbox
tightens, your extension will still behave properly as long as it can communicate with the
service.
Oddly enough, as of iOS 8 Apple has also decided to limit which services an app store
process may query. This makes nearly all forms of inter-process communication ineffective on
iOS, outside of the well-defined static services that Apple has designated. RocketBootstrap was
created as a way around this that simultaneously allows additional services to be registered and
respects the security and privacy of the user’s data. Services registered with RocketBootstrap are
made globally accessible even in spite of very restrictive sandbox rules and it will serve as a
432
single project that needs updating as the rules change.
Ryan Petrich
Code Wrangler, Father of Activator
433
Tweaking is the new-age hacking
I am not a prolific programmer by any means. I have a programmer’s mind, and I have
proven in my days I am capable of writing working solutions. I have a few tweaks in my name,
and more ideas to be realized. Creating more has been about having more free time. However,
my time has been spent becoming familiar with iOS-internals, because I find that I am a good
learner. I have a fair understanding due to the tools we have available, made by great
programmers before our time, and from documentation and examples shared by the
community. Because of the nature of Cocoa and Objective-C, we can take a great adventure and
introspection into the workings of third-party software, and Apple’s operating system. This
provides a foundation and skills for making tweaks. We want to encourage tweak making
because it has been the driving initiative behind the audience that wants to have jailbroken
devices, besides for the groups that wish to only have a jailbreak for pirating apps and games.
The growth of this jailbreak ecosystem has gone with the proliferation of new tweaks, ever
pushing the boundaries of modification while maintaining a safe environment for the end-users.
The jailbreak development scene has given a unique opportunity to developers to express
themselves in a new way. In the days before CydiaSubstrate, apps and games were not tweaked.
This is a new concept; examining and debugging existing software and then rewriting portions
of it with the least invasive tools available, the changes are nonpermanent and for the most part
free of worry for breaking something with any lasting effect. Tweaks allow for a redefining of
how software works and behaves. We do this with tweaks, and there has really been nothing
like it before in the world of programming, even on the PC. There were opportunities
throughout previous decades to make game patches, hacks and so forth, but it’s only with the
emergence of the audience of jailbreakers and iOS that we find our unique situation. Only
recently has it become feasible to make small adjustments to existing UI and modify how things
work without requiring the replacement of whole parts of the code - CydiaSubstrate allows
434
careful targeting of methods and functions.
It’s a lot of fun to discover how things work, and tweak making is the embodiment of that
fun time for developers. One of the challenges for tweak making is coming up with new ideas to
create, and sometimes these ideas only arise after studying the internals in some detail. If you
make tweaks as a hobby, and not as a profession, you’re free to do as you wish and to focus on
projects that interest you. For new tweak makers, there’re quite a lot of existing projects to learn
from, but a lot of the easier projects have already been realized. Creating new original ideas that
are unique is a task of being familiar with the available tweaks on Cydia, and then going to work
discovering how the internal parts work, debugging and testing until you have a diagram or
picture in your mind how it’s put together. When you reach a near complete understanding,
you are primed to tackle whatever challenge you make for yourself.
Some of our greatest tools and resources are free: Apple’s own documentation is excellent,
and for tweak makers we have a wiki and the opportunity to use class-dump to examine what
methods are exposed for hooking inside the target app or process. Debugging and disassembly
tools that vary from free to paid, all can be great assets for tweak makers. A well-studied
programmer with some prior experience with standard projects will be in a good position to
continue learning from these materials. To the contrary, a newcomer programmer, even a
person with some good ideas will struggle at first with the learning curve. We recommend a
core understanding of Objective-C and Cocoa principles for aspiring young tweak makers; this
can be a significant investment of time, but it is really a hurdle for new tweak makers that
haven’t a clue where to start. To the uninitiated, the object-oriented nature of the programming
involved can be a daunting thing to realize. Generating tweak ideas can be a task for amateurs,
but the writing of the code for the tweak implementation is often the result of planning and
research and testing for a significant time. We find that many young new programmers are
impatient because their ideas for new tweaks do take more time to materialize than they were
willing to invest. Patience is a virtue of course, and the best-made tweaks are all products of
careful programmers.
The greatest tweak is Activator (libactivator). Based on a commonsense idea of having more
triggers system-wide, activator is also a graciously open-sourced project; the product of many
months and years of work by our most senior tweak maker, Ryan Petrich. His dedication and
expertise shines through in Activator, which doubles as a platform for third-parties to harness
435
the powerful triggers from anyplace to use in their own projects. It represents a lot of research
and understanding of the most obscure internals on iOS: SpringBoard and backboard. If there is
one shining example to point to as a goal for a tweak maker to show how much research and
careful planning can go into a tweak, that is the example to look towards. It’s a lofty project that
none should consider as being trivial to do, however. For some aspiring developers it can be a
great encouragement to see what is possible. Kudos to Ryan Petrich for making it, and for all he
does to further the jailbreak development community.
As the repo maintainer for TheBigBoss, I have a job description for myself. Doing my job
has given tremendous opportunity to be an influence or guidance for new tweak makers. Often
their first experience with another member of the jailbreak development community is with
myself when they first contact me or submit to the repo. We wish that all developers can be
involved in the social channels of this scene: chat, forums, twitter et al., however, it’s not
uncommon that some developers work in relative isolation from these social groups. My
involvement then can be seen as important: I may be the only other voice that the programmer
will hear, and I will give an opinion on the technical merits of new tweak projects; often this first
encounter is invaluable because those developers that work in isolation are not wise to many of
the caveats and conventions we hold as important in this community. Our documentation and
wikis have improved to make these details more available, but still I am often the first time a
developer has some interaction with someone with a greater expertise than their own. I try to
give my wisdom and guidance to the developers because its in our best interest to support, if not
groom, newcomer developers so they feel as part of the group of jailbreak developers, and they
can be pointed towards ways to avoid some of the pitfalls that many newcomers make. I take
some pride in doing this and helping in part to strengthen the developer community that is
based around the tweak-making culture. I want the jailbreak platform to continue to grow and
mature by the great ideas that are envisioned and the expertise to realize them.
Do not be discouraged when the task seems difficult. We have some developers with years
and decades of programming experience, and we also have some with only a few weeks or
months. I come from the school of thought that it should be well made and well tested, and not
rushed or forced. If you have a goal, it should not be merely to have something of yours
published on a Cydia repo, but to give something to the public, which will enrich their jailbreak
experiences - that is for hobbyists like myself. If you have some commercial interest in Cydia,
436
and for making an selling tweaks, do wide testing with users and alongside other tweaks to help
assure a product that works for many users and their combinations of tweaks; your duty as a
responsible tweak maker is to be careful while you modify the insides of others’ programs or
apps, and to be thorough in testing compatibility with others’ tweaks.
Tweak making is the new-age hacking. There’re already enough reasons for you to get
started with tweak development, and we need tweaks to keep the jailbreak community in
bloom. Join us, learn from others, work hard, be patient, and have fun.
Optimo
Administrator at TheBigBoss repo
437