WEBVTT
Kind: captions
Language: en

00:00:00.160 --> 00:00:05.759 align:start position:19%
A fundamental security feature of modern
operating systems is some form of

00:00:03.840 --> 00:00:10.559 align:start position:19%
isolation enforced upon running
applications. And on iOS, this is known

00:00:08.000 --> 00:00:14.160 align:start position:19%
as the app sandbox. The sandbox is what
stops random apps that you install from

00:00:12.320 --> 00:00:18.240 align:start position:19%
being able to read all of your messages
or access each other's files. But it

00:00:16.320 --> 00:00:22.080 align:start position:19%
also helps to reduce attack surface to
the wider system as much as possible by

00:00:20.240 --> 00:00:25.760 align:start position:19%
providing access only to system
functionality that is absolutely

00:00:23.920 --> 00:00:29.599 align:start position:19%
required by a given process and this
makes exploitation more difficult for

00:00:27.439 --> 00:00:33.360 align:start position:19%
attackers. The iOS kernel contains a
huge amount of code in its core and also

00:00:31.679 --> 00:00:37.760 align:start position:19%
in various kernel extensions to help
with things such as hardware accelerated

00:00:35.440 --> 00:00:41.120 align:start position:19%
graphics, media decoding, and more. It
would be wildly irresponsible to allow

00:00:39.440 --> 00:00:45.520 align:start position:19%
every application to call into these
kernel subsystems as they please because

00:00:43.200 --> 00:00:48.559 align:start position:19%
doing so would expose deep complex code
to attackers and the chance of them

00:00:47.280 --> 00:00:52.079 align:start position:19%
being able to reach a vulnerability
would be high. To deal with this, the

00:00:50.239 --> 00:00:55.920 align:start position:19%
sandbox implements a filter that
restricts what processes are actually

00:00:53.760 --> 00:00:59.920 align:start position:19%
allowed to do by either allowing or
denying a requested operation depending

00:00:58.160 --> 00:01:04.320 align:start position:19%
on which process is trying to perform
it. This filtering logic is implemented

00:01:02.160 --> 00:01:08.640 align:start position:19%
at the kernel level in the form of small
hooks. These hooks are added to kernel

00:01:06.799 --> 00:01:12.320 align:start position:19%
function handlers for pretty much every
operation that a process in userland

00:01:10.720 --> 00:01:17.200 align:start position:19%
might want to perform, including
everything from file access to system

00:01:14.640 --> 00:01:21.439 align:start position:19%
calls and IOKit driver interfaces. For
example, when a process wants to open a

00:01:19.040 --> 00:01:25.119 align:start position:19%
file at a specified path, the open
handler function in the kernel will

00:01:23.360 --> 00:01:29.920 align:start position:19%
first call out to a sandbox hook
function to evaluate if this requested

00:01:27.520 --> 00:01:34.079 align:start position:19%
file path is allowed to be accessed by
this process. The same is implemented

00:01:32.000 --> 00:01:38.000 align:start position:19%
for executing system calls or opening
connections to IOKit kernel drivers. The

00:01:36.560 --> 00:01:42.880 align:start position:19%
logic that actually performs this
evaluation traverses that process's

00:01:40.400 --> 00:01:47.200 align:start position:19%
sandbox profile to find the requested
action, evaluate any entitlement

00:01:45.200 --> 00:01:52.240 align:start position:19%
requirements, and eventually reach a
determination of either allow or deny. A

00:01:49.920 --> 00:01:56.720 align:start position:19%
sandbox profile is a preset list of
rules defined for a given process.

00:01:54.479 --> 00:02:01.600 align:start position:19%
Traditionally on Mac OS, these profiles
are stored in plain text in SB files and

00:01:59.840 --> 00:02:06.399 align:start position:19%
they are easily readable. They use a
lisp style syntax to define actions,

00:02:04.000 --> 00:02:10.800 align:start position:19%
parameters, and basic logic. On iOS,
these profiles are actually compiled

00:02:08.080 --> 00:02:15.120 align:start position:19%
down to a proprietary binary format and
stored directly in the iOS kernel cache

00:02:12.800 --> 00:02:19.440 align:start position:19%
binary under the symbol name collection
data. This binary data is obviously not

00:02:17.360 --> 00:02:23.360 align:start position:19%
human readable and so it must be first
decompiled by custom tooling if you

00:02:22.000 --> 00:02:27.520 align:start position:19%
actually want to investigate which
actions are allowed for a given process.

00:02:25.440 --> 00:02:32.640 align:start position:19%
The popular tool Sand Blaster has been
capable of decompiling iOS sandbox

00:02:29.760 --> 00:02:36.640 align:start position:19%
profiles since around iOS 7, and the
tool has been consistently maintained by

00:02:34.640 --> 00:02:41.040 align:start position:19%
a few different parties over the years
with Celbrite's latest fork being

00:02:38.480 --> 00:02:45.680 align:start position:19%
updated to support iOS 26. Running the
tool for version 26.2, you can see a

00:02:43.599 --> 00:02:49.200 align:start position:19%
bunch of SV files in our output
directory. These files are each named

00:02:47.840 --> 00:02:53.040 align:start position:19%
after the process that they are
associated with and they contain that

00:02:51.280 --> 00:02:56.400 align:start position:19%
plain text lisp style language
representation of the profile that we

00:02:54.640 --> 00:03:00.400 align:start position:19%
can actually read. As you can see, each
process on the system is given its own

00:02:58.239 --> 00:03:05.280 align:start position:19%
sandbox profile tailored to its specific
needs. There are many userland helper

00:03:02.480 --> 00:03:10.640 align:start position:19%
processes on iOS that act as a proxy for
kernel interfaces. For example, is a

00:03:08.480 --> 00:03:14.640 align:start position:19%
proxy process for making Apple neural
engine requests. It implements the logic

00:03:12.879 --> 00:03:18.560 align:start position:19%
to communicate with the neural engine
interface in the kernel which includes

00:03:16.319 --> 00:03:22.640 align:start position:19%
the loading of machine learning models,
executing them and passing data to and

00:03:20.560 --> 00:03:26.720 align:start position:19%
from the neural engine itself. A regular
application like Instagram does not need

00:03:24.720 --> 00:03:30.879 align:start position:19%
direct access to this kernel driver.
Normal applications are assigned a very

00:03:28.560 --> 00:03:34.080 align:start position:19%
generic and restrictive sandbox profile,
giving them access only to some

00:03:32.319 --> 00:03:38.720 align:start position:19%
essential drivers, some userland
processes, and some specific file path

00:03:36.000 --> 00:03:42.720 align:start position:19%
access. Using a proxy process to talk to
risky kernel code is a much smarter

00:03:40.720 --> 00:03:46.239 align:start position:19%
decision from a security standpoint. If
an app like Instagram does want to run

00:03:44.560 --> 00:03:49.519 align:start position:19%
some kind of machine learning code and
it requires the use of the neural

00:03:47.680 --> 00:03:54.799 align:start position:19%
engine, then it can make a request to
the A&amp;E system process via an IPC

00:03:52.159 --> 00:03:59.439 align:start position:19%
message. It will ask the A&amp;E process to
perform a specific task and A&amp;E will in

00:03:57.680 --> 00:04:03.360 align:start position:19%
turn make the appropriate request
directly to the kernel driver. Any

00:04:01.760 --> 00:04:07.120 align:start position:19%
results that need to be sent back can be
delivered to the calling process also

00:04:04.959 --> 00:04:11.120 align:start position:19%
via an IPC message. So, how are these
sandbox profiles actually attached to

00:04:09.280 --> 00:04:15.200 align:start position:19%
specific processes? Let's explore the
sandbox on a real jailbroken phone so we

00:04:13.280 --> 00:04:18.000 align:start position:19%
can see how it works live in kernel
memory. By the way, if you appreciate

00:04:16.560 --> 00:04:21.680 align:start position:19%
the research effort that goes into
making these technical videos, leave a

00:04:19.840 --> 00:04:25.680 align:start position:19%
like on this video and subscribe to help
support this channel. Here I have a

00:04:23.520 --> 00:04:30.160 align:start position:19%
normal iOS app that is trying to open a
connection to a privileged kernel driver

00:04:27.759 --> 00:04:35.040 align:start position:19%
interface. The IO accessory manager
driver used for dealing with connected

00:04:32.479 --> 00:04:39.919 align:start position:19%
accessory devices. This app is installed
as a regular application and so upon

00:04:37.600 --> 00:04:44.160 align:start position:19%
launch the kernel assigns it the default
container sandbox profile. This is the

00:04:42.080 --> 00:04:48.000 align:start position:19%
restrictive sandbox profile for all app
store and third party apps. And you can

00:04:46.160 --> 00:04:52.560 align:start position:19%
think of it as similar to the untrusted
app SE Linux policy used on Linux and

00:04:50.639 --> 00:04:56.800 align:start position:19%
Android systems. Trying to open a
connection to IO accessory manager from

00:04:54.560 --> 00:05:01.360 align:start position:19%
with inside this app fails. And in the
system log, we can see the sandbox deny

00:04:58.960 --> 00:05:05.280 align:start position:19%
message stating that the IO kit open
operation is not permitted for this

00:05:03.280 --> 00:05:09.120 align:start position:19%
kernel driver. One of the only processes
that is actually granted permission to

00:05:07.039 --> 00:05:14.320 align:start position:19%
talk to this specific kernel interface
is the user space demon accessoryd. The

00:05:12.080 --> 00:05:18.400 align:start position:19%
sandbox profile for accessoryd allows
this action. So what if we could just

00:05:16.400 --> 00:05:22.000 align:start position:19%
borrow that profile to use it for our
normal app? It turns out this is a nice

00:05:20.400 --> 00:05:25.360 align:start position:19%
experiment. you can actually try on an
older iPhone if you already have kernel

00:05:23.840 --> 00:05:29.120 align:start position:19%
read and write capabilities on a
jailbroken device and it will provide

00:05:27.360 --> 00:05:33.199 align:start position:19%
some nice insight into how the sandbox
profile is actually handled at the low

00:05:30.960 --> 00:05:37.919 align:start position:19%
level. Firstly, all running processes on
iOS are represented by a proc structure

00:05:35.759 --> 00:05:41.919 align:start position:19%
in the kernel. The kernel stores all of
these proc structures in a doubly linked

00:05:39.840 --> 00:05:46.400 align:start position:19%
list. And you can traverse this to find
a given proc structure by starting at

00:05:44.160 --> 00:05:50.240 align:start position:19%
the kernel's own proc and then following
the next pointer, checking the process

00:05:48.240 --> 00:05:54.320 align:start position:19%
ID of each subsequent proc along the
way. We can do this and find the proc

00:05:52.560 --> 00:05:59.600 align:start position:19%
structure for our app that's running on
the device. Each proc structure has a

00:05:56.880 --> 00:06:03.840 align:start position:19%
field known as the Ukra, which holds a
pointer to the credentials for this

00:06:01.199 --> 00:06:07.680 align:start position:19%
process. Inside the UK structure is a
pointer to that process's MACF f label

00:06:06.319 --> 00:06:12.080 align:start position:19%
which eventually leads to the
entitlements for this process and a

00:06:09.759 --> 00:06:15.840 align:start position:19%
structure holding the sandbox data. The
first member in the sandbox data

00:06:13.840 --> 00:06:19.840 align:start position:19%
structure is a pointer to the static
sandbox collection binary blob, the one

00:06:18.240 --> 00:06:24.080 align:start position:19%
that is stored directly in the kernel
cache. And then just below that we see a

00:06:22.000 --> 00:06:28.000 align:start position:19%
much smaller value. This is the offset
value which is relative to the start of

00:06:25.840 --> 00:06:31.840 align:start position:19%
the sandbox collection to get to the
specific sandbox profile within the

00:06:30.160 --> 00:06:36.080 align:start position:19%
collection that this process has been
assigned. So on our jailbroken device,

00:06:33.840 --> 00:06:40.880 align:start position:19%
we can actually just borrow or steal the
sandbox profile offset from another

00:06:38.319 --> 00:06:44.800 align:start position:19%
process such as the accessoryd process
and write it into our own processes

00:06:42.800 --> 00:06:47.840 align:start position:19%
sandbox structure. Or better yet, if you
want the entitlements of the other

00:06:46.080 --> 00:06:51.520 align:start position:19%
process too, then you can actually just
replace the entire ukid which

00:06:49.680 --> 00:06:55.520 align:start position:19%
encapsulates both. This effectively
copies all of the in-memory entitlements

00:06:53.440 --> 00:06:59.759 align:start position:19%
and the sandbox profile of that process
and applies it to our own process at

00:06:57.600 --> 00:07:04.560 align:start position:19%
runtime which ultimately has the effect
of upgrading our credentials to that of

00:07:02.560 --> 00:07:08.720 align:start position:19%
accessoryd. So without doing anything
else with our app still open and running

00:07:06.639 --> 00:07:13.120 align:start position:19%
on the device now when we try to open
the connection to IO accessory manager

00:07:10.960 --> 00:07:16.639 align:start position:19%
the sandbox filter allows our requested
action and we successfully get a

00:07:14.800 --> 00:07:20.000 align:start position:19%
connection port to the privileged IO kit
user client from that driver. This

00:07:18.319 --> 00:07:23.520 align:start position:19%
serves as a nice demonstration of how
the sandbox works, but it can actually

00:07:22.000 --> 00:07:26.880 align:start position:19%
also be very useful for research
purposes if you're digging into one of

00:07:25.039 --> 00:07:30.800 align:start position:19%
these more privileged kernel drivers and
you need that access. This technique of

00:07:29.199 --> 00:07:34.400 align:start position:19%
stealing process credentials was
actually used in many jailbreaks to

00:07:32.400 --> 00:07:38.000 align:start position:19%
elevate privileges on the system post
exploitation. Apple of course wanted to

00:07:36.479 --> 00:07:40.880 align:start position:19%
put a stop to this and so in the modern
day we see additional protections

00:07:39.599 --> 00:07:45.280 align:start position:19%
preventing this type of post
exploitation technique. Processes these

00:07:42.800 --> 00:07:49.520 align:start position:19%
days now actually have a second readonly
proc structure which holds all of the

00:07:47.280 --> 00:07:53.919 align:start position:19%
readonly data associated with a process.
Within this readonly structure is the

00:07:51.440 --> 00:07:58.080 align:start position:19%
ukar and the ukred structure itself is
also allocated in a readonly memory zone

00:07:56.479 --> 00:08:02.319 align:start position:19%
so that it cannot be tampered with once
the process has spawned. These readonly

00:08:00.319 --> 00:08:06.639 align:start position:19%
protections are in effect from A12
devices onward. Initially implemented on

00:08:04.560 --> 00:08:10.319 align:start position:19%
top of the page protection layer feature
but more recently enforced by the secure

00:08:08.560 --> 00:08:14.720 align:start position:19%
page table monitor. Therefore, on
modern-day iOS, replacing a process's

00:08:12.639 --> 00:08:17.759 align:start position:19%
sandbox profile is not nearly as
straightforward as it once was. So,

00:08:16.319 --> 00:08:21.360 align:start position:19%
we've seen how the sandbox is
implemented at the kernel level, and

00:08:19.440 --> 00:08:24.879 align:start position:19%
we've seen how given kernel read and
write on an older device, we can

00:08:23.039 --> 00:08:28.240 align:start position:19%
unsandbox our app by stealing
credentials. But this is all assuming

00:08:26.639 --> 00:08:32.240 align:start position:19%
that we already have a compromised
device. So, what about in a real exploit

00:08:30.400 --> 00:08:35.919 align:start position:19%
chain? How do attackers actually break
out of the sandbox? Hackers will

00:08:34.000 --> 00:08:39.599 align:start position:19%
typically require a sandbox escape as
part of their exploit chain because it

00:08:37.760 --> 00:08:43.360 align:start position:19%
is normally the case that interesting
kernel attack surface is simply not

00:08:41.680 --> 00:08:47.440 align:start position:19%
reachable directly from their starting
context. In order to compromise the

00:08:45.200 --> 00:08:51.839 align:start position:19%
entire system, they might first attack a
userland system process with the aim of

00:08:49.519 --> 00:08:55.360 align:start position:19%
leveraging its sandbox which may provide
them additional attack surface to the

00:08:53.440 --> 00:08:58.880 align:start position:19%
kernel. Let's assume that an attacker is
starting their exploit chain from within

00:08:57.120 --> 00:09:02.880 align:start position:19%
the browser context and they have
successfully exploited a WebKit bug to

00:09:01.040 --> 00:09:06.959 align:start position:19%
gain remote code execution within the
browser process. Check out my other

00:09:04.560 --> 00:09:10.480 align:start position:19%
video on iOS browser exploitation to see
exactly how this would work. They are

00:09:08.560 --> 00:09:14.240 align:start position:19%
now running their own code from inside
the browser process and can access

00:09:12.480 --> 00:09:17.760 align:start position:19%
whatever the browser can access.
Unfortunately for them, the browser

00:09:15.839 --> 00:09:22.399 align:start position:19%
process runs in a very restrictive
sandbox profile with very limited access

00:09:20.080 --> 00:09:25.680 align:start position:19%
to kernel attack surface. Apple does
this specifically because they know the

00:09:24.160 --> 00:09:29.040 align:start position:19%
browser is the most common entry point
for an exploit chain. So they want to

00:09:27.440 --> 00:09:32.560 align:start position:19%
mitigate this by locking down that
process as much as possible. As a

00:09:30.880 --> 00:09:36.480 align:start position:19%
result, if the attacker wants to next
exploit the kernel, they probably need

00:09:34.399 --> 00:09:40.399 align:start position:19%
access to a kernel driver of some kind.
And since they can't do this directly,

00:09:38.399 --> 00:09:45.040 align:start position:19%
they need to first exploit a different
system process that can provide this

00:09:42.160 --> 00:09:49.360 align:start position:19%
access. Sandbox profiles define exactly
which system processes a process is

00:09:47.200 --> 00:09:54.240 align:start position:19%
allowed to communicate with over Muk
IPC. You can see these listed under the

00:09:51.839 --> 00:09:57.839 align:start position:19%
muk lookup operation in a given sandbox
profile. So let's imagine that in our

00:09:56.160 --> 00:10:02.720 align:start position:19%
theoretical scenario, the browser
process is allowed to communicate with

00:09:59.760 --> 00:10:06.880 align:start position:19%
system process A. System process A
happens to be involved in the machine

00:10:04.480 --> 00:10:10.880 align:start position:19%
learning processing on the device and so
its sandbox profile gives it direct

00:10:08.880 --> 00:10:15.200 align:start position:19%
access to a privileged iOS kernel
driver, the Apple neural engine driver.

00:10:13.519 --> 00:10:19.279 align:start position:19%
It's allowed to talk directly to this
driver by issuing commands and running

00:10:17.279 --> 00:10:22.959 align:start position:19%
machine learning models. Now if the
attacker has also identified a

00:10:21.040 --> 00:10:27.760 align:start position:19%
vulnerability, a heat buffer overflow
for example within system process A that

00:10:26.000 --> 00:10:32.240 align:start position:19%
can be triggered through mock IPC
communication, they can exploit this

00:10:29.839 --> 00:10:35.839 align:start position:19%
from their initial browser context. This
may involve using their initial

00:10:33.839 --> 00:10:40.399 align:start position:19%
arbitrary code execution ability from
their stage one exploit to send a

00:10:38.160 --> 00:10:44.480 align:start position:19%
malicious muck message to process A,
triggering the heap overflow and gaining

00:10:42.560 --> 00:10:48.079 align:start position:19%
code execution there. At this point,
they would have effectively hopped into

00:10:46.240 --> 00:10:52.079 align:start position:19%
this other process, escaping their
initial sandbox and now having access to

00:10:50.320 --> 00:10:55.680 align:start position:19%
whatever this new sandbox context
provides. Here, they might be able to

00:10:53.920 --> 00:10:59.839 align:start position:19%
trigger a kernel bug within the driver
that that system process provides access

00:10:57.680 --> 00:11:04.640 align:start position:19%
to, allowing them to compromise the
entire system. This is known as chaining

00:11:02.480 --> 00:11:08.959 align:start position:19%
exploits. With each link in the chain,
the attacker is essentially hopping into

00:11:06.720 --> 00:11:12.560 align:start position:19%
the next stages context, gradually
expanding their access to the system.

00:11:10.800 --> 00:11:15.920 align:start position:19%
Some exploit chains might even require
more than one sandbox escape. It all

00:11:14.480 --> 00:11:19.440 align:start position:19%
comes down to the requirements of the
reachable components and where the bugs

00:11:17.839 --> 00:11:22.480 align:start position:19%
are. We won't look at any specific
sandbox escape exploits in this video,

00:11:21.200 --> 00:11:25.600 align:start position:19%
but let me know if that is something
you'd be interested on seeing in a

00:11:23.920 --> 00:11:29.440 align:start position:19%
dedicated video down below in the
comments. So, in summary, this fine

00:11:27.519 --> 00:11:33.680 align:start position:19%
grain control over what processes are
allowed to do allows Mac OS and iOS to

00:11:31.920 --> 00:11:37.839 align:start position:19%
separate out functionality into
designated processes and ultimately

00:11:35.920 --> 00:11:41.680 align:start position:19%
reduce reachable attack surface across
the system. And in many cases where

00:11:39.760 --> 00:11:45.279 align:start position:19%
exploitation is still achieved, it
provides a very nice level of damage

00:11:43.279 --> 00:11:48.959 align:start position:19%
control, limiting what an attacker can
actually do and normally forcing the use

00:11:47.279 --> 00:11:51.760 align:start position:19%
of additional exploits like sandbox
escapes. Hopefully, you learned

00:11:50.240 --> 00:11:55.360 align:start position:19%
something new in this video. Thanks for
watching and I will see you in the next

