WEBVTT

00:00.000 --> 00:04.800
Let's go back nine years ago to the summer of 2016.

00:05.160 --> 00:21.820
It was about to be just a regular summer, you know, going to the pool, attending DEF CON, but little did we know Niantic, a mobile game company, was about to release a new game called Pokemon Go, condemning thousands of nerds to be collectively sunburned.

00:22.860 --> 00:27.560
But jokes aside, this was a dream come true for me and for many.

00:27.560 --> 00:36.100
You know, being outside, chasing Pokemon, battling for gym access, it was really, really a dream come true.

00:36.240 --> 00:38.700
Except it wasn't so much for me.

00:38.700 --> 00:43.900
I lived in a rural town back then and there weren't any Pokemon around me.

00:44.360 --> 00:51.480
And while everyone in the city was having fun, I had to chase down Pokemon very far away from my home.

00:51.520 --> 00:55.380
And all I had, in fact, was this lousy scanner.

00:55.380 --> 00:56.960
I don't know if you remember it.

00:57.780 --> 01:07.640
But you could only see the nine closest Pokemon to your location and a footprint one to two or three showing you how close they are to your location.

01:08.180 --> 01:12.400
And honestly, this sucked and all the Pokemon were ratatats anyway.

01:12.960 --> 01:15.200
These are real pictures of me and my friend.

01:15.840 --> 01:24.840
So naturally I decided to build a Pokemon scanner that would tell me where all the Pokemon are and those that I haven't caught yet so I can flex on my friends.

01:25.780 --> 01:43.480
So little did I know this idea would take me to an adventure of uncovering the Pokemon Go protocol and understanding how it works and also having to overcome very difficult defense mechanisms against fake scanners.

01:44.460 --> 01:45.480
Hi, everyone.

01:45.480 --> 01:46.580
My name is Tal Skverer.

01:46.580 --> 01:48.240
I know my last name is hard.

01:48.240 --> 01:53.220
I'm a security researcher by day and a security researcher by night as well.

01:53.220 --> 02:07.640
During the day I get paid to lead the research team at ASIC security, a cyber security start up doing identity security and by night I do independent hacking, CDFs and all sorts of crazy projects you can read about in my blog.

02:08.700 --> 02:10.540
Actually it's not the first time on DEF CON.

02:10.540 --> 02:12.900
You noticed I haven't done any shots.

02:13.340 --> 02:18.560
In DEF CON 31 I talked about a zero day in GCP and I'm very, very, very excited to be here today.

02:18.560 --> 02:20.640
Thank you all for coming to hear me.

02:22.440 --> 02:27.740
This talk will be accompanied by a two part blog post that will be live on my blog right after this talk.

02:27.740 --> 02:29.500
It might be even live right now.

02:29.620 --> 02:33.340
So you are free to go and read it afterwards.

02:33.340 --> 02:38.500
I go into details further than I am in this talk due to lack of time.

02:39.380 --> 02:51.260
I also have quality stickers, limited time edition that I will be giving away after the talk so feel free to come by, ask me questions, take your sticker and you can also reach out to me on social media.

02:51.320 --> 02:54.580
And like every good Pokemon trainer we need to collect badges.

02:54.580 --> 03:04.960
So we'll collect six badges during this talk when we overcome a very difficult gym battle and you can use this to know where we are on the talk approximately.

03:05.220 --> 03:07.000
One last thing.

03:07.560 --> 03:11.120
So this research was done independently nine years ago.

03:11.120 --> 03:16.800
Most of the techniques that I'm going to show you today no longer work on the actual app.

03:17.480 --> 03:23.620
So this talk is mostly to show you how the mobile hacking game community looks like.

03:23.620 --> 03:24.840
And I have references.

03:24.840 --> 03:31.040
So all the images that I'm going to show you today are based on publicly available data.

03:31.040 --> 03:34.520
I will be sharing the references slide in the last slide.

03:34.640 --> 03:37.200
With that out of the way, let's begin.

03:37.880 --> 03:41.200
So I hear you want to create a Pokemon scanner.

03:41.260 --> 03:41.660
Hello.

03:41.660 --> 03:43.820
So I hear you want to create a Pokemon scanner.

03:44.300 --> 03:47.980
And we know that the Pokemon Go app operates something like that.

03:47.980 --> 03:52.760
So it has to get the information that it displays in the lousy scanner.

03:52.940 --> 04:00.500
So it has to request data to get all the Pokemon and get back all the nearby Pokemon to its location.

04:00.500 --> 04:08.820
And this is a request that I want to forge to say that I'm in a different location and get all the Pokemon around that location.

04:09.180 --> 04:16.780
But who is to say that the app doesn't get only the information that is displayed on the lousy scanner.

04:16.780 --> 04:21.880
So only a list of nine Pokemon and the footprints of how far they are from you.

04:21.920 --> 04:23.400
Well, there are two hints.

04:23.400 --> 04:36.600
One, if you play Pokemon Go, you know that once you are close enough to the actual Pokemon, it immediately appears on your screen at a very specific location which kind of tells you that the app knows the exact specific location.

04:36.960 --> 04:40.080
The second hint is that servers are lazy.

04:40.080 --> 04:46.920
So no one wants to do this heavy computation that has to do with determining how far a Pokemon is to you.

04:47.560 --> 04:52.000
And Niantic would be happy to delegate this computation to your device.

04:52.000 --> 04:58.800
You have a very powerful Android device or Apple device in your pocket that can do this calculation for you.

04:58.800 --> 05:03.840
So I'm not sure that I convinced you but I did convince myself back then and I'm happy that I did.

05:04.760 --> 05:09.740
And I wanted to see how to be able to do this request.

05:09.740 --> 05:12.640
I wanted to see how the protocol looks like.

05:12.640 --> 05:20.940
And to see the protocol, I have to look at the traffic that goes between the Pokemon Go app and the Pokemon Go server.

05:20.940 --> 05:24.640
And this is a tech called sniffing attack.

05:24.980 --> 05:38.380
We can pretty guess, pretty much guess that the app and the server communicates over HTTP which means that we need to employ an HTTP proxy to look and record the information that goes back and forth.

05:38.380 --> 05:40.300
In Android, that's pretty simple.

05:40.300 --> 05:52.680
You spin up an HTTP proxy on one of your machines, you go to your Wi-Fi settings, you put the IP address of your machine in this proxy settings and immediately you start seeing traffic.

05:52.940 --> 05:55.320
I used filler classic back then.

05:55.320 --> 05:56.400
I still use it today.

05:56.400 --> 05:59.040
Call me an oldie but I really like this program.

05:59.200 --> 06:03.220
And this is how the real communication looks like in filler.

06:03.220 --> 06:14.300
As we can see, the app starts by requesting a generic end point and then switches to a specific end point probably for load balancing issues.

06:14.600 --> 06:20.180
And now we are able to see the communication between the app and the server and for this we get our first badge.

06:20.180 --> 06:21.920
The badge of sniffing.

06:23.580 --> 06:24.220
Okay.

06:24.220 --> 06:25.500
We have the communication.

06:25.500 --> 06:28.020
Let's dig deep into the actual protocol.

06:28.020 --> 06:37.720
So as you can see here, we can see the request content type that is reported by the app is actually form URL encoded.

06:37.720 --> 06:40.120
As you can see, it is lying.

06:40.120 --> 06:43.920
Because if you ever saw form URL encoded, it's textual protocol.

06:43.920 --> 06:47.100
And here the protocol is clearly binary.

06:47.580 --> 06:49.060
So what's going on?

06:49.060 --> 06:54.200
Let's look a bit deeper into the actual binary of the request.

06:54.200 --> 07:04.020
On the left, maybe it's on the left for you, you can see the bytes being sent on the wire and on the right you can see the ASCII presentation of them when it is possible.

07:04.320 --> 07:14.520
And it probably means nothing to you but luckily at the time I happened to look at this exact scenario multiple times so I immediately noticed some very telling clues here.

07:14.520 --> 07:28.360
The first one is that the first byte of the request is 08 and the second is that there are some glimpses of readable text which means that it's not encrypted but rather serialized and I immediately realized that this is protobuf.

07:28.420 --> 07:30.860
Protobuf is short for protocol buffers.

07:30.860 --> 07:50.340
This is a very well-known framework developed by Google and maintained by Google that takes very complex objects and knows how to serialize them so two separate systems like an Android app and a server can communicate with each other even though the objects can be relatively difficult.

07:50.340 --> 07:58.980
So let's take a second and switch the screen to this very, very nice pink hue to talk about protobuf.

07:58.980 --> 08:10.540
So as a developer you define a protobuf message which is the object you are going to send and the definition looks something like that so this is just an example definition from the protobuf documentation.

08:10.560 --> 08:12.440
We define a search request message.

08:12.440 --> 08:13.700
It has four fields.

08:13.700 --> 08:16.080
The first field is a query of type string.

08:16.080 --> 08:23.520
The second field is a page number of type integer and then there are actually two optional fields that don't have to appear in every object.

08:23.680 --> 08:32.500
And as you can see, the fourth field actually, the search options, is a repeated field which means it can appear more than once, kind of like a list.

08:32.620 --> 08:38.500
As you can see, its type is a search option so you can embed messages within other messages.

08:38.620 --> 08:42.820
And we don't have a declaration of a search option here and that's on purpose.

08:42.920 --> 08:49.320
And to use it, you use a tool, as I said, developed by Google named Proto C.

08:49.320 --> 08:54.900
You take this definition, you kind of compile it into your favorite language.

08:54.900 --> 09:03.100
For this example I compiled it into Python and then you can simply import the resulting file as you can see here.

09:04.000 --> 09:16.000
On the left, this short code imports this library, creates a new search request object, fills it with information, serializes it and sends it on the wire.

09:16.880 --> 09:17.680
Good.

09:17.680 --> 09:21.500
So we know now about Protobuf but we have a problem.

09:21.500 --> 09:26.480
We see the serialized data on the wire but we don't have the original definitions.

09:26.480 --> 09:28.420
So what we can do about that?

09:28.420 --> 09:41.180
Luckily Proto C supports a flag named decode raw, it's the best flag ever, which basically tells Proto C do your best to deserialize this message without having access to the actual definitions.

09:41.180 --> 09:50.940
If we would take the message that I created here that was sent on the wire and decode it, use the decode raw flag on it, we would get something like that.

09:50.940 --> 10:11.180
So as you can see, it's not looking like the original, we don't have the field names, maybe some of the types are wrong, but we can still learn a lot about the underlying definition just from seeing the deserialized message without the declaration.

10:11.820 --> 10:16.740
And we can even tell some things about the message that we didn't see the declaration of, right?

10:16.740 --> 10:19.600
You can see it has two fields, both of them are strings.

10:19.840 --> 10:25.280
So let's apply this technique to the actual message that we just saw for Pokemon Go.

10:25.280 --> 10:26.100
So here it is.

10:26.100 --> 10:29.060
It's very loud, you probably can't see anything but that's fine.

10:31.280 --> 10:46.700
So at first this might look a bit difficult to understand and today you have tools like PBTK that basically goes and looks at the binary of an Android app and extract all the definitions, all the compiled definitions from it.

10:46.700 --> 10:51.220
Back then understanding protocol definitions was nothing short of an art.

10:51.580 --> 10:58.020
And I use several techniques to understand how messages look like and how the protocol works.

10:58.020 --> 11:05.320
I go deeper into them in my blog post but here I just cover the three main tips and tricks that I use.

11:05.320 --> 11:08.860
The first one is you can tell a lot about fields using their values.

11:08.860 --> 11:21.520
So if you have very clear values like the strings at the bottom there, that you can immediately, if you know this, you know that this is an open ID token, then maybe field 10 is an authentication field.

11:22.300 --> 11:32.840
The second trick is actually knowing that some of the different types, like hex code and integers that you can see there and floats share the same code.

11:32.840 --> 11:39.600
So sometimes protocol C when it tries to decode them, it doesn't know what the original type is, so it just tries its best.

11:39.600 --> 11:46.600
And if you would go and convert those hex encoded integers into floats, you would just see that they are coordinates.

11:46.600 --> 11:48.000
So this is my location.

11:49.220 --> 12:00.300
And the third technique is actually by interacting differently with the app, like making a different action or moving to a new location, I can see the messages are changing slightly.

12:00.300 --> 12:05.460
And using these changes I can infer a lot of information about the underlying declaration.

12:05.520 --> 12:06.740
I will save you all of that.

12:06.740 --> 12:08.900
You are welcome to go to the blog to read more.

12:08.900 --> 12:17.520
But after understanding some of the fields of the request, I ended up with this declaration, which is a bit shorter so you can see it better.

12:17.760 --> 12:21.680
So how a request looks like from the Pokemon Go app.

12:22.000 --> 12:26.580
So it's actually, I call it a request container because it contains requests.

12:26.600 --> 12:43.460
The request container has some metadata fields like the request ID, my location, my authentication info and it has a repeated field that I named requests that includes all the different requests that the app wants the server to answer on.

12:43.460 --> 12:49.260
Each request has a request type and optionally request bytes if it needs to.

12:49.260 --> 12:54.460
So here it sends three different requests, 106, 126 and 4.

12:55.280 --> 12:58.940
And the server response looks relatively the same.

12:58.940 --> 13:03.980
So it has a response container with the same request ID and a repeated response field.

13:04.100 --> 13:10.200
Each response corresponding to one request so it's always matching one to one according to the same order.

13:11.100 --> 13:19.600
Okay, so now that we know how the general format looks like, we have to try and find the actual request that gets the list of Pokemon, right?

13:19.600 --> 13:21.920
That's what we wanted to do for the scanner.

13:22.020 --> 13:26.240
But there are tons of different types of requests.

13:26.400 --> 13:35.620
And I tried to do a few things like searching for Pokemon names, searching for locations around me, but I didn't really manage to find the actual request type that I wanted.

13:35.620 --> 13:39.280
And what actually solved it for me is playing Pokemon.

13:39.280 --> 13:44.100
So if you played Pokemon, you know that all of them are indexed by an incrementing number.

13:44.560 --> 13:53.060
And by looking at the lousy scanner and seeing that it scanned a PG and a Rattata, I searched for the number 16 and 19 and I found a hit.

13:53.060 --> 14:10.420
So it turns out request number 106, which was later changed to name get map object, that's the title, is a request that takes as input an identifier of a cell and as a response looks something like that.

14:10.420 --> 14:25.000
It responds with a map cell object that basically includes all the different objects that are on this specific map cell in the area, including Forts, which is the internal name for gyms, poker stops, et cetera.

14:25.000 --> 14:33.720
It also includes spawn points and Pokemon and more importantly it has the exact location of the specific Pokemon in this map cell.

14:33.720 --> 14:40.360
As you can see there is a catchable Pokemon at a very specific location and this is the Pokemon number 23.

14:40.360 --> 14:42.820
So who is that Pokemon?

14:43.780 --> 14:45.300
I hope you know it.

14:46.060 --> 14:57.180
So upon successfully understanding the protocol of how the Pokemon Go app requests and gets all the Pokemon in a specific location, we earn our second badge, the badge of protocol.

14:57.840 --> 15:01.640
So what do we know so far?

15:01.640 --> 15:09.840
We know that the communication, the protocol uses protobuf and the main message that is being sent is a request container.

15:09.840 --> 15:21.480
It contains some metadata on the request including a list of requests, one of which is request number 106 that fetches the map details for a specific cell.

15:21.480 --> 15:29.640
It gets a cell ID and responds with the exact location of Pokemon which is the best case scenario for building a scanner.

15:29.820 --> 15:31.980
But we still have three open questions.

15:31.980 --> 15:35.380
So the first one is how do you construct a request container?

15:35.380 --> 15:39.120
So it has complicated fields like authentication fields.

15:39.120 --> 15:41.520
How do you create them successfully?

15:41.660 --> 15:46.300
Second, there are still plenty of fields and request types that I just didn't know.

15:46.300 --> 15:49.160
So I called them unknown like unknown six.

15:50.500 --> 15:54.100
So do I need to create them in order to make a successful request?

15:54.100 --> 15:55.920
I don't know what they are used for.

15:55.980 --> 15:58.340
And lastly, what the hell is a cell ID?

15:58.340 --> 15:59.860
I'm used to coordinates.

15:59.860 --> 16:01.660
I don't know anything about cells.

16:01.680 --> 16:05.960
So it turns out the answer to the first two questions was actually pretty simple.

16:05.960 --> 16:07.620
The server just doesn't care.

16:07.900 --> 16:12.540
It was very, very relaxed regarding what the data that it expects in a request container.

16:12.540 --> 16:23.060
You basically only had to send a request identifier, your authentication info which is very straightforward and a single request 106 with your cell ID.

16:23.060 --> 16:26.240
It didn't care about other fields that I understood.

16:26.240 --> 16:28.380
It didn't care about all the fields that I didn't understand.

16:28.380 --> 16:30.560
It didn't care about other request types.

16:30.680 --> 16:32.460
So best case scenario for me.

16:32.660 --> 16:39.660
About that cell ID, so keen eyed of you may have noticed in the previous slide, I call this an S2 cell ID.

16:39.660 --> 16:44.140
It turns out Pokemon Go uses a framework named S2 cells.

16:44.140 --> 16:48.360
It splits a sphere, in this case the earth, into cells.

16:48.500 --> 16:53.020
You decide on a level which basically controls the size of a specific cell.

16:53.020 --> 16:57.500
Pokemon Go works with at least get map object works with level 15.

16:57.940 --> 17:20.360
And then you have a very, very simple calculation that has a lot of calculators online that take a specific coordinate on earth and converts it to a 64 bit cell identifier that you put, excuse me, in your get map request as the cell that you want to find Pokemon on.

17:20.460 --> 17:25.140
So with all the questions answered, I went and created a Pokemon scanner.

17:25.140 --> 17:27.200
This is my code from back there.

17:27.340 --> 17:28.560
A very nice code.

17:28.560 --> 17:31.200
I'm sure you can see for yourself.

17:31.980 --> 17:35.020
It gets authentication info from Google.

17:35.020 --> 17:39.720
It constructs a request container with authentication info, request identifier.

17:40.200 --> 17:44.440
So it starts on with a specific location basically all around me.

17:44.440 --> 17:48.700
I scan like a radius of a few miles.

17:49.480 --> 18:05.560
And for each location it adds a new get map object request, converts the actual coordinate to an S2 cell , gets the request, passes the response, puts all the different Pokemon that it finds on the map, and we have a request container.

18:06.280 --> 18:14.480
And dumb me, I didn't really record any of the actual maps that I built with Pokemon from back there.

18:14.480 --> 18:18.760
I only actually had this one picture of a log that I had.

18:18.760 --> 18:24.140
And you can tell that it's not fake and real because I had to blur out my actual location.

18:24.300 --> 18:26.200
So that's my only proof to you.

18:26.200 --> 18:30.160
But this is an example log from the scanner that searches for different Pokemon.

18:30.160 --> 18:32.200
I think it found a Pidgey.

18:32.740 --> 18:34.000
And it was great.

18:34.000 --> 18:35.880
I flexed on all my friends.

18:35.880 --> 18:37.060
I found all the cool Pokemon.

18:37.060 --> 18:39.960
I used it for a couple of weeks.

18:40.160 --> 18:45.980
And upon successfully creating a Pokemon scanner, we get our third badge, the badge of Pokemon.

18:47.320 --> 18:56.320
So one quiet Wednesday I got back home and I started my Pokemon scanner to play a nice evening of Pokemon Go.

18:56.320 --> 18:59.440
Only to discover that it didn't work.

19:00.300 --> 19:05.380
So my scanner got like a generic error that I really didn't understand.

19:05.380 --> 19:11.600
So naturally I opened up Fiddler, my HTTP proxy, to see how the information, how the protocol looks like.

19:11.600 --> 19:16.280
I knew that a few days earlier a new update was pushed to the app.

19:16.280 --> 19:22.200
Maybe something, some stuff in the protocol has changed and I needed to update my scanner.

19:22.620 --> 19:25.740
And Fiddler didn't record anything.

19:25.740 --> 19:32.960
And in fact, while it was open, the Pokemon Go app refused to turn on and refused to do anything.

19:33.820 --> 19:36.180
So okay, we have a challenge .

19:36.180 --> 19:37.880
We have a fighter on hand.

19:38.320 --> 19:44.040
So naturally I went online to search if anyone else has encountered this problem.

19:44.040 --> 19:50.120
And I realized that I wasn't the only smart guy in the room that decided to build their own Pokemon scanner.

19:50.120 --> 19:55.240
There were actually many different communities online doing this stuff.

19:55.240 --> 20:02.420
One in particular was PogoDev, a community dedicated to creating unofficial API for Pokemon Go.

20:02.460 --> 20:07.800
They had noticed this issue as well and started tackling it.

20:07.800 --> 20:17.300
And in fact I found that their administrator had actually put up a Reddit post saying, well, we know our API is down.

20:17.300 --> 20:19.340
Something has changed in the app.

20:19.340 --> 20:23.580
We've created a Discord server to coordinate efforts around this.

20:23.580 --> 20:25.440
So naturally I went on the Discord server.

20:25.440 --> 20:31.220
I joined the internal hacking channel and we started working towards solving this.

20:31.220 --> 20:47.620
Little did I know this step would be the first out of a three day hackathon where I barely slept in which we tried to overcome all the defenses that were put by Niantic against unofficial API calls.

20:47.720 --> 20:52.520
I will make sure to, and we used various techniques to overcome these issues.

20:52.520 --> 20:55.120
I will make sure to stop when I introduce a new technique.

20:55.120 --> 20:55.940
I will name it.

20:55.940 --> 20:57.260
I will explain how it works.

20:57.260 --> 21:00.260
And then we can see how we used it to overcome the defenses.

21:00.480 --> 21:05.640
On top of the title you can see the timeline.

21:05.640 --> 21:07.400
So this started around 1 a.m.

21:07.400 --> 21:08.680
in the first day.

21:08.680 --> 21:12.820
So you can follow along how long it took us to do every step.

21:13.620 --> 21:22.240
So the first hurdle was the fact that no one was able to sniff the traffic and see the protocol and how it looks like.

21:22.960 --> 21:33.760
And we thought, and actually one researcher found this interesting class was added to the Pokemon Go app named Niantic trust manager.

21:34.080 --> 21:44.360
And so it implemented a check server trusted function which seems like it implements a defense called certificate pinning.

21:44.500 --> 21:52.120
And to understand how we overcome it, we first need to understand why it works against HTTP proxies and basically blocks their behavior.

21:52.120 --> 21:54.920
We need to talk a little bit about HTTP proxy.

21:54.920 --> 21:59.560
So here you can see a very, very high level overview of how HTTP proxy works.

22:01.720 --> 22:09.740
Basically, normally when you work with HTTPS, the client communicates with the server in an encrypted form.

22:09.740 --> 22:11.460
That's what the S stands for.

22:11.460 --> 22:12.400
Secure.

22:12.600 --> 22:17.600
Based on a certificate that is provided to the server to the client.

22:17.660 --> 22:21.600
And besides confidentiality, HTTPS also promises you integrity.

22:21.600 --> 22:28.740
Which means that you know that you are talking to the real domain and not a fake domain that says, hey, I'm a Pokemon Go server.

22:29.280 --> 22:31.920
But that's exactly what HTTP proxy needs to do.

22:31.920 --> 22:42.100
It needs to convince the client to speak to it and it will parse and decrypt all the communication, log it and then pass it on to the server.

22:42.100 --> 22:44.720
So it works because you own the client.

22:44.720 --> 22:59.800
The HTTP proxy generates a certificate that you install as a certificate authority on your client which basically allows the HTTP proxy to create and sign any certificate that it wants for any domain that it wants and convince the client that it is talking with the real server.

22:59.800 --> 23:04.180
This is how the certificate of filler looks like on my Windows machine.

23:04.180 --> 23:05.820
It works the same way for Android.

23:05.820 --> 23:08.620
I was too lazy to pull it up from my machine.

23:10.040 --> 23:10.540
Okay.

23:10.540 --> 23:12.780
So now we understand how this works.

23:12.780 --> 23:15.760
So how do certificate pinning works?

23:15.760 --> 23:27.400
By doing certificate pinning, basically the developer takes the public key of the actual server that it intends to talk to and simply hard codes it into the application.

23:27.480 --> 23:31.460
And what the application does, it wants a new session of HTTPS starts.

23:31.460 --> 23:36.740
It verifies that the public key of the server matches the public key that is embedded in the code.

23:36.740 --> 23:51.580
Obviously, since the HTTP proxy generates a new certificate, the public key would be different from the real deal and that way the client can detect that it is actually interacting with the HTTP proxy and not the actual server and basically block the communication.

23:51.940 --> 23:53.200
So how do we overcome it?

23:53.200 --> 23:57.560
We have to use a new technique that I call dynamic reverse engineering.

23:57.560 --> 23:58.940
So we have to learn it.

24:01.340 --> 24:10.160
So what dynamic reverse engineering means is intervening in the running code of an application and basically changing stuff on the way.

24:10.160 --> 24:18.060
You can think about it similarly to developing code and debugging it and changing values while it is debugging, only automatically.

24:18.180 --> 24:22.660
And there are a few frameworks that allows you to do it for Android in particular.

24:22.660 --> 24:31.900
One of which is exposed, which basically allows you to hook any Java class function that you want and run code before it starts and after it returns.

24:31.900 --> 24:34.240
So we wrote this short exposed model.

24:34.240 --> 24:35.720
It is actually very short.

24:35.720 --> 24:39.880
I just removed the original change because it is very long.

24:39.880 --> 24:54.600
We hook on the checked server function of the trust manager, which by the way we found because it extends a very well-known class for certificate pinning named X509 trust manager.

24:54.600 --> 24:58.500
That's a good way to find certificate pinning if you ever want to do so.

24:58.760 --> 25:01.480
We took the original chain from the actual server.

25:01.480 --> 25:02.100
This is public.

25:02.100 --> 25:03.420
It is very easy to get.

25:03.420 --> 25:06.960
And what we do is we did a before hook.

25:06.960 --> 25:15.820
Before a function is called, so every time it is called, we basically replace with the first argument which is the certificate to validate with a real certificate.

25:15.840 --> 25:22.300
So that way the app always thinks it interacts with the actual server even though it is not.

25:22.320 --> 25:24.560
So we fool it that way.

25:25.500 --> 25:30.100
So this allowed us to see the traffic again.

25:30.140 --> 25:32.180
And what we saw kind of surprised us.

25:32.180 --> 25:34.220
We saw that nothing changed.

25:34.220 --> 25:38.100
So all the protocol looked exactly the same as it was before.

25:39.560 --> 25:41.740
So what was the problem?

25:42.200 --> 25:53.740
We realized that what might have changed is one of the fields that was previously unneeded and optional started to become required and the server started verifying it.

25:53.740 --> 26:01.440
And we needed to find it and there were various different fields that we didn't know, various types of requests that we didn't know.

26:01.580 --> 26:07.300
So to figure out the specific field, we had to use a second technique that I call active probing.

26:07.300 --> 26:19.720
With active probing, and I actually used it a little bit for my scanner, you take a valid request and you start changing values with it and sending it to the server until the server starts responding with an error.

26:19.720 --> 26:27.060
So what we did is we basically started removing fields that we knew were optional one by one until the server returned an error.

26:27.080 --> 26:37.080
And that way we actually found a specific field, the sixth field of the request container became required which was optional before.

26:38.360 --> 26:44.760
And so we did know a bit about the format of the sixth field.

26:44.760 --> 26:46.760
We called it unknown six.

26:46.780 --> 26:55.320
We knew that it contained a message within and within a message there is a byte array and the byte array actually contains the information within unknown six.

26:55.320 --> 27:05.800
And we turned our sole focus of this research was turned into unknown six, which naturally, it was natural to take the name of it as the name of our group.

27:05.800 --> 27:11.280
So we called ourselves team unknown six and we focused on cracking this.

27:12.020 --> 27:16.120
So starting off, we wanted to know more about unknown six and how it looks like.

27:16.120 --> 27:21.940
And to do this, we'll use our third technique which is kind of like the counterpart to the active probing.

27:21.940 --> 27:23.640
I call this passive analysis.

27:23.640 --> 27:26.600
Passive analysis was also used for the Pokemon scanner.

27:26.620 --> 27:36.240
You generate a lot of different requests by interacting differently with the app or doing different actions or simply generating a lot of different requests from a lot of different devices.

27:36.260 --> 27:41.400
Looking at all these requests, you can infer a lot of details about how the protocol works.

27:41.480 --> 27:46.660
So what we did, we generated a bunch of unknown six examples.

27:46.660 --> 27:48.000
This is one specific example.

27:48.000 --> 27:51.840
As you can see, most of the information is within the one field over there.

27:51.840 --> 27:54.980
I'll just call this unknown six for short.

27:55.340 --> 28:00.200
And we noticed something peculiar from all of these samples.

28:00.200 --> 28:03.500
The size of this unknown six.

28:03.640 --> 28:11.740
So it turned out that the size of this was always a multiple of 256, one of my favorite numbers, plus 32.

28:11.740 --> 28:18.740
So the size of this is just one block of 256 and 32, so 288 tokens.

28:18.740 --> 28:19.400
So it's a bit peculiar.

28:20.860 --> 28:31.520
Another very nice finding that one of the researchers did is that they set the time, the clock on their machine to the exact same second every time.

28:31.520 --> 28:38.000
And he noticed that all of the samples of unknown six that he generated began with the same 32 bytes.

28:38.000 --> 28:47.420
So we managed to figure out that the first 32 bytes of unknown six only depend on time where the rest depends on other things.

28:47.420 --> 28:58.160
Actually by doing active probing and taking a valid unknown six and changing the request, just the cell ID in a get map object request, we would also have the server return an error.

28:58.160 --> 29:03.440
Which means that this unknown six is strongly tied to the request we make.

29:03.440 --> 29:14.980
In fact, the existence of numbers like 256 and the involvement of time, we kind of guessed that this is some encrypted signature over the request that we make, that now the server started to validate.

29:14.980 --> 29:26.760
It was always sent to the server, it just didn't validate it before, probably because they suffered from a lot of technical difficulties in the first weeks of Pokemon Go due to the large number of people that were playing.

29:27.460 --> 29:37.900
So the next step that we wanted to do is to find where this unknown six is generated in the code so we can understand what happens and how the encryption works and how the signature looks like.

29:38.280 --> 29:43.520
And we knew that everything that has to do with protobuf is actually in the native part of the application.

29:44.320 --> 29:45.640
So let me stop for a second.

29:45.640 --> 29:51.040
I speak a lot about the Java part of the application and the native part of the application, so let me explain.

29:51.180 --> 29:53.940
An Android application is basically split into two parts.

29:53.940 --> 30:00.660
There is the Java part, it's the more high level part that has to do with interacting with the Android framework and interacting with the user.

30:01.100 --> 30:11.160
And then there is the native part, usually coded in C or C++ that is actually compiled and inserted in the library that the Java part can call using NDK.

30:11.380 --> 30:12.960
This is an interface between them.

30:12.960 --> 30:18.220
Actually the certificate pinning called, it was a Java class that was calling a native part.

30:18.220 --> 30:30.020
And usually native code is used for more computational heavy operations and it's no wonder that mobile games mostly rely on native code because they have a lot of graphical related computations.

30:30.020 --> 30:32.820
Specifically Pokemon Go I think relies on unity.

30:34.600 --> 30:39.440
Okay, so what do you do with the Java part and the native part?

30:39.440 --> 30:51.000
To understand it further you have to use our fourth and final technique which I call static reverse engineering, kind of like the analog to the other way around of dynamic reverse engineering.

30:51.040 --> 31:02.240
You use specialized programs to inspect the actual code that is being executed by either the Java part or the native part and you use specialized programs to do so.

31:02.400 --> 31:05.020
These programs basically do the other way around.

31:05.020 --> 31:17.560
They start from an assembly, they disassemble it back to the assembly language and then they also try to decompile it to get as close as they can to the original source code that generated the app.

31:18.460 --> 31:20.640
With Java we have Jadix.

31:20.640 --> 31:23.260
It's an amazing tool.

31:23.260 --> 31:24.260
It does an excellent job.

31:24.260 --> 31:34.140
Here you can see the Pokemon Go app open in Jadix and actually the image from before of the certificate pinning class was actually from Jadix as well.

31:34.140 --> 31:42.440
It does an excellent job reflecting back the byte code of Java into the actual code as close as possible to the original.

31:42.600 --> 31:47.780
And it's relatively easy because Java is only compiled into an intermediate language.

31:48.000 --> 31:52.400
When it comes to native code this is a bit harder and you have to use different tools.

31:52.600 --> 31:54.020
This is an example from Ida.

31:54.020 --> 31:56.680
Today we have Gidra which is also a great tool.

31:57.080 --> 31:59.380
They basically show you the actual assembly.

32:00.060 --> 32:11.900
On the left there you can see the graph view of a function in Ida and on the right you can see Ida trying its best to decompile the assembly code back into C code.

32:11.900 --> 32:30.920
And these programs what they actually allow you to do is to change the names of functions, of variables, change the types that are being used, define structures, really to help you to understand difficult patches of code and get to the point where the code that you are looking at is very close to the original code that was being developed.

32:31.920 --> 32:38.120
So we knew, as I said, we knew that everything related to protobuf including creation of unknown 6 is in the native part.

32:38.140 --> 32:41.640
Specifically in a library named Lib Niantic manager.

32:41.640 --> 32:49.140
So we opened it in Ida and we had to pinpoint the actual function that generates unknown 6.

32:49.160 --> 32:51.080
And there were a few methods to do it.

32:51.080 --> 32:54.320
I think I laid down three different methods in my blog.

32:54.320 --> 32:57.880
Here I will only share one method due to a lack of time.

32:57.880 --> 32:59.760
This was the method that I used.

32:59.800 --> 33:12.960
If you recall, using static analysis, we found that the starting bytes of the unknown 6 message are only dependent on time.

33:12.960 --> 33:18.220
So naturally the code that generates unknown 6 has to rely on time somehow.

33:18.220 --> 33:27.160
And once you have a binary library that relies on a system function like time, it needs to import it usually if it's not compiled statically.

33:27.280 --> 33:32.400
So I went to Ida, to the import table, and I did find the time function being imported.

33:32.480 --> 33:39.380
And I looked at all the cross references meaning all the locations in the assembly in the library that calls time.

33:39.380 --> 33:40.360
I found two.

33:40.360 --> 33:42.520
The bottom one wasn't related at all.

33:42.520 --> 33:43.800
It was very easy to see.

33:43.800 --> 33:48.520
The top one, the function at offset 87444, I can't even see it.

33:48.520 --> 33:49.640
I remember it.

33:50.640 --> 34:00.560
So once you look at the code there, you see that the output of the time function gets fed into SRand, which sets the seed of all the randomness that is going to come.

34:00.560 --> 34:04.240
Then this function generates 32 random bytes.

34:04.240 --> 34:16.680
And finally, you can see a lot of places in the code where the number 256 as a constant appears, which really makes us believe that we found the actual code that generates unknown 6.

34:16.680 --> 34:18.620
And for this, we get our fourth badge.

34:18.620 --> 34:20.160
Badge of unknown 6.

34:21.260 --> 34:22.220
Okay.

34:22.560 --> 34:27.300
So at this point, we looked at a specific function that we pinpointed.

34:27.300 --> 34:32.200
We called it sig encrypt function because we believe it encrypts a signature.

34:32.200 --> 34:33.600
It was only natural.

34:34.080 --> 34:41.020
And we decided to verify beyond doubt that it's exactly the function that we wanted to generate unknown 6.

34:41.020 --> 34:48.280
So we used active probing again, excuse me, dynamic reverse engineering again to detect that.

34:48.340 --> 34:51.560
This time, instead of exposed, we used Frida.

34:51.560 --> 34:59.360
Frida is a very, very powerful tool that allows you to basically hook using JavaScript any function that you want.

34:59.360 --> 35:01.600
It works on Windows, Linux, Android.

35:02.220 --> 35:03.500
It's very strong for Android.

35:03.500 --> 35:09.160
Here you can see a short Frida script that basically finds the ‑‑ so it hooks on the native library.

35:09.160 --> 35:13.900
It finds the actual address of the function that we wanted, offset 87444.

35:13.900 --> 35:19.480
It hooks both the enter of the function, so every time it runs, and the return.

35:19.480 --> 35:22.140
And simply dumps the input and the output.

35:22.140 --> 35:36.540
And by matching the output that this hook recorded to the value that was actually being sent by the app afterwards in our HTTP proxy, we noticed that it is exact same value, meaning that we did find the actual function.

35:37.340 --> 35:50.820
Another better finding that we managed to do with this hook function is actually look at the input to the encryption function, which is our signature, and we found that it is actually port above again.

35:50.820 --> 35:52.720
So I heard you like port above.

35:53.180 --> 35:56.400
And it's a very, very long object.

35:56.540 --> 36:02.720
And there are some interesting strings there, like one plus that was my device back then.

36:03.560 --> 36:07.920
And it looked like field that was supposed to be in the signature, so we had that.

36:07.920 --> 36:17.920
But before moving on, we focused on the sig encrypt function in trying to understand how does it manipulate this input to get to the encrypted output that we know.

36:17.920 --> 36:19.880
And it turned out to be quite simple.

36:19.880 --> 36:22.360
So I will be able to show you this today.

36:22.360 --> 36:26.880
Here you can see the decompiled version of the sig encrypt function.

36:27.480 --> 36:30.600
And let's start with an input of length 700.

36:30.600 --> 36:33.120
I split it into 256 chunks.

36:33.120 --> 36:38.040
The less chunks is a shorter one, just for easier understanding.

36:38.180 --> 36:47.560
So as we knew, it starts by calling time, feeding it into SRUN and then generating 32 bytes for the seed, which it prepends to the beginning.

36:47.560 --> 36:51.240
So that's the 32 bytes that we saw in the final version.

36:51.700 --> 36:57.040
Then it pads the input to the next closest block of 256.

36:57.420 --> 37:04.540
And actually the last byte is the remainder, so the server knows how many bytes it needs to strip to get to the actual input.

37:04.540 --> 37:06.220
And this is not the interesting part.

37:06.220 --> 37:07.440
This is the interesting part.

37:07.440 --> 37:08.480
We have encryption.

37:08.480 --> 37:12.400
So we have encryption at offset 9E9D8.

37:12.420 --> 37:14.120
This is the encryption function.

37:14.240 --> 37:23.440
And first the function starts by expanding the seed into a block of 256 using an expansion mechanism.

37:23.700 --> 37:25.500
And then encryption works.

37:25.560 --> 37:36.520
So this expanded seed is actually using the initialization vector to the encryption function together with the first block to output the first encrypted block.

37:36.520 --> 37:52.120
It gets overridden to the first block, which is used as the IV with the second block to get the encrypted second block and same way for the third block until we have our encrypted value that is being used in unknown 6.

37:52.380 --> 37:54.060
And this is nothing proprietary.

37:54.060 --> 38:03.060
This is a very well-known cryptographic scheme named CBC mode that has to do with encrypting blocks based on initialization vector.

38:03.580 --> 38:07.880
So now that we know how the sync and encrypt function works, we split into two groups.

38:07.880 --> 38:17.600
One group focused on encryption 9E whatever to understand how encryption works and basically replicate it so we can encrypt our own values because we want to encrypt our own signatures.

38:17.600 --> 38:27.300
The second group I was a part of actually focused on the signature to understand how it is constructed so we can create our fake signatures to sign.

38:28.040 --> 38:33.220
And actually, I think it was five hours later, the first group already succeeded.

38:33.220 --> 38:34.580
They were very, very good.

38:34.780 --> 38:37.920
And replicating and getting the encryption to work.

38:37.920 --> 38:39.460
So how did they do it?

38:39.560 --> 38:47.880
They were very smart and they walked through the entire call chain of the encryption whatever numbers that came afterwards.

38:47.880 --> 39:11.280
Which means all the functions that could be theoretically called by following the chain of calls from this function, they automatically used IDA to decompile them into very ugly C codes but it was a C code that compiles and executes exactly the same as the original assembly and they put them all in a single C file which turned out to be of size 14,000

39:11.280 --> 39:20.660
lines which actually with a few fixes compiled successfully and allowed us to encrypt any input that we want to which was a very good job by them.

39:21.180 --> 39:28.140
And for successfully replicating the encryption function, we get our fifth badge, the badge of encryption.

39:30.320 --> 39:38.800
So meanwhile, the second group that I was a part of actually focused on understanding how the signature works.

39:38.800 --> 39:46.880
So there are a few subfields within the signature message that were very easy to understand according to the values in the field.

39:46.880 --> 39:47.880
And you saw them earlier.

39:47.880 --> 40:03.700
So one contained GPS information, basically a dump of all the GPS information that you can infer that the device can get which called location fix and the second one called device info contained a lot of different properties and string about our device.

40:04.460 --> 40:11.060
And actually the third message that we found, we understood what it means quite funnily.

40:11.060 --> 40:23.100
So people starting to generate a lot of samples of the signature, we started using emulators and we noticed that this field was always empty in emulators and always filled with regular devices.

40:23.160 --> 40:26.540
So we understood that it contained sense of information.

40:26.540 --> 40:30.280
Obviously emulators don't have any sense of information, real devices does.

40:31.460 --> 40:36.920
It contains a lot of information regarding acceleration, altitude, gravity, etc.

40:37.480 --> 40:48.360
And actually at this point, so this is how the signature declaration looked like at the time, what we understood so far.

40:49.020 --> 40:56.140
And inclusion of fields like this field, like the sensor info field, actually something before that.

40:56.420 --> 41:04.740
So people that were using active probing to understand things about the signature protobuf, started getting their account banned.

41:05.280 --> 41:18.480
And this together with the existence of fields like sensor information, fueled the fears in the community that Niantic was using the information within the signature to detect unofficial API usage.

41:18.520 --> 41:25.140
And if you consider things like sensor info, this is kind of akin to tracking your mouse movement for captures.

41:25.420 --> 41:33.740
This is a data that is very hard to forge and we really didn't know how we were going to create this for our fake signatures.

41:34.000 --> 41:40.160
But this thought also made us wonder, does the server actually verify this?

41:40.160 --> 41:47.320
So to verify things like sensor information based on previous sensor information is a very , very difficult computational job.

41:47.320 --> 41:51.460
And we knew that the servers were under high load at the time.

41:51.460 --> 41:56.360
So we thought, well, maybe not all fields in the signature are necessary.

41:56.360 --> 42:02.720
So we employed active probing again using dynamic reverse engineering, using Frida.

42:02.720 --> 42:10.080
We started removing fields from the signature before it was encrypted and seeing when the server starts responding with an error.

42:10.080 --> 42:13.820
And we found out that most of the signature was not needed.

42:14.040 --> 42:18.260
So all the fields that you can see that are marked with not required, were not required.

42:18.580 --> 42:21.000
And actually only six fields were required.

42:21.000 --> 42:22.600
All of these highlighted here.

42:22.600 --> 42:26.480
Two of which were just regular time stamps, which were very simple.

42:26.480 --> 42:29.540
So we were left with just four fields to go.

42:29.960 --> 42:33.980
Field 10, field 20, field 22 and field 24.

42:34.640 --> 42:37.920
At this point, I recall, the discord was going crazy.

42:37.920 --> 42:43.580
So here we are at the end of the second day of the hackathon and we were very tired at the time.

42:43.920 --> 43:00.200
And actually we had to go to discord and ask them to increase the limit on our server size because so many people had joined and back then it wasn't so common for servers to grow beyond 10,000 members in such a short time.

43:00.220 --> 43:05.960
And another thing that happened, so we had this channel named RE research, which all the hackers were a part of.

43:06.200 --> 43:10.660
And eventually, because people kept asking, it was open to the public.

43:10.660 --> 43:12.900
But only for view, not for chatting.

43:12.900 --> 43:14.580
So only we were able to chat.

43:14.580 --> 43:22.680
And I remember that we had a voice chat with hundreds of people within it waiting and analyzing every message that we sent in the RE research.

43:22.680 --> 43:26.440
It was a very humbling experience, I can say.

43:26.800 --> 43:28.880
But it was very, very crazy.

43:29.140 --> 43:47.220
So going back to the fields that we wanted to figure out, we actually found that two fields, field 10 and field 20, were generated based on the same function, which takes as input a 32-bit number, a byte array, a string, and outputs a 32-bit integer as well,

43:47.220 --> 43:49.660
which kind of looks like a hashing function.

43:49.880 --> 43:56.420
And we found that field 20 was a very simple hash of this number.

43:56.420 --> 43:58.580
We don't know what it means to this day.

43:58.580 --> 44:00.040
If you know, please approach me.

44:00.040 --> 44:01.400
I would be happy to know.

44:01.960 --> 44:06.560
And the string of your location chained together .

44:07.240 --> 44:11.140
And the output of the function was inserted in the field 20.

44:11.140 --> 44:13.840
Field 10 was a bit more complicated, but not so much.

44:13.840 --> 44:22.900
So first you hash your authentication info straight from the request container and then you feed the output of this hash as the integer to the second hash of your location.

44:22.900 --> 44:29.120
So we have two hashes, both on the location of the user, one of which is also on the dedication info.

44:31.620 --> 44:34.080
So at this point we tried to understand what's the hash.

44:34.080 --> 44:38.940
And one technique to do it is to look at the constants being used in the hash and comparing them to known hashes.

44:38.940 --> 44:43.120
Using this we very easily found that it's a very famous hash named XX hash.

44:43.140 --> 44:45.460
And this was a 32-bit version of it.

44:45.460 --> 44:48.000
We also found a 64-bit version of it.

44:48.000 --> 44:50.500
And it was actually used for field 24.

44:50.500 --> 44:54.840
So field 24 was a repeated field, at least as you can see from this.

44:54.980 --> 45:04.460
And actually every request that was sent took the request bytes of the request and hashed them together with the same integer that I don't know.

45:04.600 --> 45:08.120
And it was the hashing, the hash was added to field 24.

45:08.120 --> 45:11.260
So field 24 contained as many hashes as you had requests.

45:11.300 --> 45:14.440
So for this example you had three hashes in field 24.

45:14.640 --> 45:18.800
And then we had the last field, the final field, the toughest nuts to crack.

45:18.800 --> 45:26.080
We actually went in the wrong direction for a while because we saw that it changes frequently when your dedication info changes.

45:27.000 --> 45:29.500
But it turned out to be completely false.

45:29.500 --> 45:35.880
One researcher found that the function that generates this field didn't get any input.

45:36.180 --> 45:40.680
So this field could be basically any thing.

45:40.880 --> 45:44.380
And once we tried to do it, we at 7 p.m.

45:44.380 --> 45:51.240
the third day, 67 hours after the hackathon started, we finally managed to make our first successful API request.

45:51.260 --> 45:54.600
And for this we get our final badge, the badge of API.

45:56.400 --> 45:57.440
Thank you.

45:58.260 --> 45:59.640
I'll just take one minute.

46:00.260 --> 46:07.740
One minute to talk about, so I was very interested to, so this is my code from back there that implements the actual authentication.

46:07.740 --> 46:10.400
You can laugh at it while I talk for the last minute.

46:11.040 --> 46:14.020
I was kind of interested to know what happened afterwards.

46:14.020 --> 46:22.560
So it turned out that, so we all treated this effort as something that is necessary to create a community, thriving community, developing tools for Pokemon Go.

46:22.560 --> 46:30.680
We really believe that having access to this kind of tool makes it possible for people that couldn't play the game normally to have access to it.

46:31.480 --> 46:35.600
And I was interested to know where the Pokemon Go hacking scene went from this.

46:35.600 --> 46:39.460
So I kind of didn't get involved into more stuff afterwards.

46:39.460 --> 46:46.420
So Pocodev continued maintaining their API for about a year and a half after this, after which they stopped.

46:46.560 --> 46:48.100
Today I search online.

46:48.100 --> 46:50.900
There's also a couple of bots that are offered for cash.

46:51.300 --> 47:00.500
Together with some unlocked versions of the app, I wouldn't install those, which is a far cry from what could have been if Niantic would publish an official API.

47:02.360 --> 47:07.760
I'll just like to thank both the team, Anon8, they were amazing guys.

47:07.760 --> 47:09.540
I had fun to work with them.

47:09.540 --> 47:13.740
I would also like EFF for supporting me and counseling me on giving this talk today.

47:13.740 --> 47:15.580
If not for them, I was not here.

47:15.580 --> 47:17.560
I will also thank you for coming.

47:17.920 --> 47:19.780
And there's a reference slide.

47:19.780 --> 47:21.620
Feel free to come, grab some stickers.

47:21.620 --> 47:22.620
Thank you for listening.

