start	end	text
0	4800	Let's go back nine years ago to the summer of 2016.
5160	21820	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.
22860	27560	But jokes aside, this was a dream come true for me and for many.
27560	36100	You know, being outside, chasing Pokemon, battling for gym access, it was really, really a dream come true.
36240	38700	Except it wasn't so much for me.
38700	43900	I lived in a rural town back then and there weren't any Pokemon around me.
44360	51480	And while everyone in the city was having fun, I had to chase down Pokemon very far away from my home.
51520	55380	And all I had, in fact, was this lousy scanner.
55380	56960	I don't know if you remember it.
57780	67640	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.
68180	72400	And honestly, this sucked and all the Pokemon were ratatats anyway.
72960	75200	These are real pictures of me and my friend.
75840	84840	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.
85780	103480	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.
104460	105480	Hi, everyone.
105480	106580	My name is Tal Skverer.
106580	108240	I know my last name is hard.
108240	113220	I'm a security researcher by day and a security researcher by night as well.
113220	127640	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.
128700	130540	Actually it's not the first time on DEF CON.
130540	132900	You noticed I haven't done any shots.
133340	138560	In DEF CON 31 I talked about a zero day in GCP and I'm very, very, very excited to be here today.
138560	140640	Thank you all for coming to hear me.
142440	147740	This talk will be accompanied by a two part blog post that will be live on my blog right after this talk.
147740	149500	It might be even live right now.
149620	153340	So you are free to go and read it afterwards.
153340	158500	I go into details further than I am in this talk due to lack of time.
159380	171260	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.
171320	174580	And like every good Pokemon trainer we need to collect badges.
174580	184960	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.
185220	187000	One last thing.
187560	191120	So this research was done independently nine years ago.
191120	196800	Most of the techniques that I'm going to show you today no longer work on the actual app.
197480	203620	So this talk is mostly to show you how the mobile hacking game community looks like.
203620	204840	And I have references.
204840	211040	So all the images that I'm going to show you today are based on publicly available data.
211040	214520	I will be sharing the references slide in the last slide.
214640	217200	With that out of the way, let's begin.
217880	221200	So I hear you want to create a Pokemon scanner.
221260	221660	Hello.
221660	223820	So I hear you want to create a Pokemon scanner.
224300	227980	And we know that the Pokemon Go app operates something like that.
227980	232760	So it has to get the information that it displays in the lousy scanner.
232940	240500	So it has to request data to get all the Pokemon and get back all the nearby Pokemon to its location.
240500	248820	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.
249180	256780	But who is to say that the app doesn't get only the information that is displayed on the lousy scanner.
256780	261880	So only a list of nine Pokemon and the footprints of how far they are from you.
261920	263400	Well, there are two hints.
263400	276600	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.
276960	280080	The second hint is that servers are lazy.
280080	286920	So no one wants to do this heavy computation that has to do with determining how far a Pokemon is to you.
287560	292000	And Niantic would be happy to delegate this computation to your device.
292000	298800	You have a very powerful Android device or Apple device in your pocket that can do this calculation for you.
298800	303840	So I'm not sure that I convinced you but I did convince myself back then and I'm happy that I did.
304760	309740	And I wanted to see how to be able to do this request.
309740	312640	I wanted to see how the protocol looks like.
312640	320940	And to see the protocol, I have to look at the traffic that goes between the Pokemon Go app and the Pokemon Go server.
320940	324640	And this is a tech called sniffing attack.
324980	338380	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.
338380	340300	In Android, that's pretty simple.
340300	352680	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.
352940	355320	I used filler classic back then.
355320	356400	I still use it today.
356400	359040	Call me an oldie but I really like this program.
359200	363220	And this is how the real communication looks like in filler.
363220	374300	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.
374600	380180	And now we are able to see the communication between the app and the server and for this we get our first badge.
380180	381920	The badge of sniffing.
383580	384220	Okay.
384220	385500	We have the communication.
385500	388020	Let's dig deep into the actual protocol.
388020	397720	So as you can see here, we can see the request content type that is reported by the app is actually form URL encoded.
397720	400120	As you can see, it is lying.
400120	403920	Because if you ever saw form URL encoded, it's textual protocol.
403920	407100	And here the protocol is clearly binary.
407580	409060	So what's going on?
409060	414200	Let's look a bit deeper into the actual binary of the request.
414200	424020	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.
424320	434520	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.
434520	448360	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.
448420	450860	Protobuf is short for protocol buffers.
450860	470340	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.
470340	478980	So let's take a second and switch the screen to this very, very nice pink hue to talk about protobuf.
478980	490540	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.
490560	492440	We define a search request message.
492440	493700	It has four fields.
493700	496080	The first field is a query of type string.
496080	503520	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.
503680	512500	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.
512620	518500	As you can see, its type is a search option so you can embed messages within other messages.
518620	522820	And we don't have a declaration of a search option here and that's on purpose.
522920	529320	And to use it, you use a tool, as I said, developed by Google named Proto C.
529320	534900	You take this definition, you kind of compile it into your favorite language.
534900	543100	For this example I compiled it into Python and then you can simply import the resulting file as you can see here.
544000	556000	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.
556880	557680	Good.
557680	561500	So we know now about Protobuf but we have a problem.
561500	566480	We see the serialized data on the wire but we don't have the original definitions.
566480	568420	So what we can do about that?
568420	581180	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.
581180	590940	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.
590940	611180	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.
611820	616740	And we can even tell some things about the message that we didn't see the declaration of, right?
616740	619600	You can see it has two fields, both of them are strings.
619840	625280	So let's apply this technique to the actual message that we just saw for Pokemon Go.
625280	626100	So here it is.
626100	629060	It's very loud, you probably can't see anything but that's fine.
631280	646700	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.
646700	651220	Back then understanding protocol definitions was nothing short of an art.
651580	658020	And I use several techniques to understand how messages look like and how the protocol works.
658020	665320	I go deeper into them in my blog post but here I just cover the three main tips and tricks that I use.
665320	668860	The first one is you can tell a lot about fields using their values.
668860	681520	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.
682300	692840	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.
692840	699600	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.
699600	706600	And if you would go and convert those hex encoded integers into floats, you would just see that they are coordinates.
706600	708000	So this is my location.
709220	720300	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.
720300	725460	And using these changes I can infer a lot of information about the underlying declaration.
725520	726740	I will save you all of that.
726740	728900	You are welcome to go to the blog to read more.
728900	737520	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.
737760	741680	So how a request looks like from the Pokemon Go app.
742000	746580	So it's actually, I call it a request container because it contains requests.
746600	763460	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.
763460	769260	Each request has a request type and optionally request bytes if it needs to.
769260	774460	So here it sends three different requests, 106, 126 and 4.
775280	778940	And the server response looks relatively the same.
778940	783980	So it has a response container with the same request ID and a repeated response field.
784100	790200	Each response corresponding to one request so it's always matching one to one according to the same order.
791100	799600	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?
799600	801920	That's what we wanted to do for the scanner.
802020	806240	But there are tons of different types of requests.
806400	815620	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.
815620	819280	And what actually solved it for me is playing Pokemon.
819280	824100	So if you played Pokemon, you know that all of them are indexed by an incrementing number.
824560	833060	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.
833060	850420	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.
850420	865000	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.
865000	873720	It also includes spawn points and Pokemon and more importantly it has the exact location of the specific Pokemon in this map cell.
873720	880360	As you can see there is a catchable Pokemon at a very specific location and this is the Pokemon number 23.
880360	882820	So who is that Pokemon?
883780	885300	I hope you know it.
886060	897180	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.
897840	901640	So what do we know so far?
901640	909840	We know that the communication, the protocol uses protobuf and the main message that is being sent is a request container.
909840	921480	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.
921480	929640	It gets a cell ID and responds with the exact location of Pokemon which is the best case scenario for building a scanner.
929820	931980	But we still have three open questions.
931980	935380	So the first one is how do you construct a request container?
935380	939120	So it has complicated fields like authentication fields.
939120	941520	How do you create them successfully?
941660	946300	Second, there are still plenty of fields and request types that I just didn't know.
946300	949160	So I called them unknown like unknown six.
950500	954100	So do I need to create them in order to make a successful request?
954100	955920	I don't know what they are used for.
955980	958340	And lastly, what the hell is a cell ID?
958340	959860	I'm used to coordinates.
959860	961660	I don't know anything about cells.
961680	965960	So it turns out the answer to the first two questions was actually pretty simple.
965960	967620	The server just doesn't care.
967900	972540	It was very, very relaxed regarding what the data that it expects in a request container.
972540	983060	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.
983060	986240	It didn't care about other fields that I understood.
986240	988380	It didn't care about all the fields that I didn't understand.
988380	990560	It didn't care about other request types.
990680	992460	So best case scenario for me.
992660	999660	About that cell ID, so keen eyed of you may have noticed in the previous slide, I call this an S2 cell ID.
999660	1004140	It turns out Pokemon Go uses a framework named S2 cells.
1004140	1008360	It splits a sphere, in this case the earth, into cells.
1008500	1013020	You decide on a level which basically controls the size of a specific cell.
1013020	1017500	Pokemon Go works with at least get map object works with level 15.
1017940	1040360	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.
1040460	1045140	So with all the questions answered, I went and created a Pokemon scanner.
1045140	1047200	This is my code from back there.
1047340	1048560	A very nice code.
1048560	1051200	I'm sure you can see for yourself.
1051980	1055020	It gets authentication info from Google.
1055020	1059720	It constructs a request container with authentication info, request identifier.
1060200	1064440	So it starts on with a specific location basically all around me.
1064440	1068700	I scan like a radius of a few miles.
1069480	1085560	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.
1086280	1094480	And dumb me, I didn't really record any of the actual maps that I built with Pokemon from back there.
1094480	1098760	I only actually had this one picture of a log that I had.
1098760	1104140	And you can tell that it's not fake and real because I had to blur out my actual location.
1104300	1106200	So that's my only proof to you.
1106200	1110160	But this is an example log from the scanner that searches for different Pokemon.
1110160	1112200	I think it found a Pidgey.
1112740	1114000	And it was great.
1114000	1115880	I flexed on all my friends.
1115880	1117060	I found all the cool Pokemon.
1117060	1119960	I used it for a couple of weeks.
1120160	1125980	And upon successfully creating a Pokemon scanner, we get our third badge, the badge of Pokemon.
1127320	1136320	So one quiet Wednesday I got back home and I started my Pokemon scanner to play a nice evening of Pokemon Go.
1136320	1139440	Only to discover that it didn't work.
1140300	1145380	So my scanner got like a generic error that I really didn't understand.
1145380	1151600	So naturally I opened up Fiddler, my HTTP proxy, to see how the information, how the protocol looks like.
1151600	1156280	I knew that a few days earlier a new update was pushed to the app.
1156280	1162200	Maybe something, some stuff in the protocol has changed and I needed to update my scanner.
1162620	1165740	And Fiddler didn't record anything.
1165740	1172960	And in fact, while it was open, the Pokemon Go app refused to turn on and refused to do anything.
1173820	1176180	So okay, we have a challenge .
1176180	1177880	We have a fighter on hand.
1178320	1184040	So naturally I went online to search if anyone else has encountered this problem.
1184040	1190120	And I realized that I wasn't the only smart guy in the room that decided to build their own Pokemon scanner.
1190120	1195240	There were actually many different communities online doing this stuff.
1195240	1202420	One in particular was PogoDev, a community dedicated to creating unofficial API for Pokemon Go.
1202460	1207800	They had noticed this issue as well and started tackling it.
1207800	1217300	And in fact I found that their administrator had actually put up a Reddit post saying, well, we know our API is down.
1217300	1219340	Something has changed in the app.
1219340	1223580	We've created a Discord server to coordinate efforts around this.
1223580	1225440	So naturally I went on the Discord server.
1225440	1231220	I joined the internal hacking channel and we started working towards solving this.
1231220	1247620	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.
1247720	1252520	I will make sure to, and we used various techniques to overcome these issues.
1252520	1255120	I will make sure to stop when I introduce a new technique.
1255120	1255940	I will name it.
1255940	1257260	I will explain how it works.
1257260	1260260	And then we can see how we used it to overcome the defenses.
1260480	1265640	On top of the title you can see the timeline.
1265640	1267400	So this started around 1 a.m.
1267400	1268680	in the first day.
1268680	1272820	So you can follow along how long it took us to do every step.
1273620	1282240	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.
1282960	1293760	And we thought, and actually one researcher found this interesting class was added to the Pokemon Go app named Niantic trust manager.
1294080	1304360	And so it implemented a check server trusted function which seems like it implements a defense called certificate pinning.
1304500	1312120	And to understand how we overcome it, we first need to understand why it works against HTTP proxies and basically blocks their behavior.
1312120	1314920	We need to talk a little bit about HTTP proxy.
1314920	1319560	So here you can see a very, very high level overview of how HTTP proxy works.
1321720	1329740	Basically, normally when you work with HTTPS, the client communicates with the server in an encrypted form.
1329740	1331460	That's what the S stands for.
1331460	1332400	Secure.
1332600	1337600	Based on a certificate that is provided to the server to the client.
1337660	1341600	And besides confidentiality, HTTPS also promises you integrity.
1341600	1348740	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.
1349280	1351920	But that's exactly what HTTP proxy needs to do.
1351920	1362100	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.
1362100	1364720	So it works because you own the client.
1364720	1379800	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.
1379800	1384180	This is how the certificate of filler looks like on my Windows machine.
1384180	1385820	It works the same way for Android.
1385820	1388620	I was too lazy to pull it up from my machine.
1390040	1390540	Okay.
1390540	1392780	So now we understand how this works.
1392780	1395760	So how do certificate pinning works?
1395760	1407400	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.
1407480	1411460	And what the application does, it wants a new session of HTTPS starts.
1411460	1416740	It verifies that the public key of the server matches the public key that is embedded in the code.
1416740	1431580	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.
1431940	1433200	So how do we overcome it?
1433200	1437560	We have to use a new technique that I call dynamic reverse engineering.
1437560	1438940	So we have to learn it.
1441340	1450160	So what dynamic reverse engineering means is intervening in the running code of an application and basically changing stuff on the way.
1450160	1458060	You can think about it similarly to developing code and debugging it and changing values while it is debugging, only automatically.
1458180	1462660	And there are a few frameworks that allows you to do it for Android in particular.
1462660	1471900	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.
1471900	1474240	So we wrote this short exposed model.
1474240	1475720	It is actually very short.
1475720	1479880	I just removed the original change because it is very long.
1479880	1494600	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.
1494600	1498500	That's a good way to find certificate pinning if you ever want to do so.
1498760	1501480	We took the original chain from the actual server.
1501480	1502100	This is public.
1502100	1503420	It is very easy to get.
1503420	1506960	And what we do is we did a before hook.
1506960	1515820	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.
1515840	1522300	So that way the app always thinks it interacts with the actual server even though it is not.
1522320	1524560	So we fool it that way.
1525500	1530100	So this allowed us to see the traffic again.
1530140	1532180	And what we saw kind of surprised us.
1532180	1534220	We saw that nothing changed.
1534220	1538100	So all the protocol looked exactly the same as it was before.
1539560	1541740	So what was the problem?
1542200	1553740	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.
1553740	1561440	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.
1561580	1567300	So to figure out the specific field, we had to use a second technique that I call active probing.
1567300	1579720	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.
1579720	1587060	So what we did is we basically started removing fields that we knew were optional one by one until the server returned an error.
1587080	1597080	And that way we actually found a specific field, the sixth field of the request container became required which was optional before.
1598360	1604760	And so we did know a bit about the format of the sixth field.
1604760	1606760	We called it unknown six.
1606780	1615320	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.
1615320	1625800	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.
1625800	1631280	So we called ourselves team unknown six and we focused on cracking this.
1632020	1636120	So starting off, we wanted to know more about unknown six and how it looks like.
1636120	1641940	And to do this, we'll use our third technique which is kind of like the counterpart to the active probing.
1641940	1643640	I call this passive analysis.
1643640	1646600	Passive analysis was also used for the Pokemon scanner.
1646620	1656240	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.
1656260	1661400	Looking at all these requests, you can infer a lot of details about how the protocol works.
1661480	1666660	So what we did, we generated a bunch of unknown six examples.
1666660	1668000	This is one specific example.
1668000	1671840	As you can see, most of the information is within the one field over there.
1671840	1674980	I'll just call this unknown six for short.
1675340	1680200	And we noticed something peculiar from all of these samples.
1680200	1683500	The size of this unknown six.
1683640	1691740	So it turned out that the size of this was always a multiple of 256, one of my favorite numbers, plus 32.
1691740	1698740	So the size of this is just one block of 256 and 32, so 288 tokens.
1698740	1699400	So it's a bit peculiar.
1700860	1711520	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.
1711520	1718000	And he noticed that all of the samples of unknown six that he generated began with the same 32 bytes.
1718000	1727420	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.
1727420	1738160	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.
1738160	1743440	Which means that this unknown six is strongly tied to the request we make.
1743440	1754980	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.
1754980	1766760	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.
1767460	1777900	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.
1778280	1783520	And we knew that everything that has to do with protobuf is actually in the native part of the application.
1784320	1785640	So let me stop for a second.
1785640	1791040	I speak a lot about the Java part of the application and the native part of the application, so let me explain.
1791180	1793940	An Android application is basically split into two parts.
1793940	1800660	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.
1801100	1811160	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.
1811380	1812960	This is an interface between them.
1812960	1818220	Actually the certificate pinning called, it was a Java class that was calling a native part.
1818220	1830020	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.
1830020	1832820	Specifically Pokemon Go I think relies on unity.
1834600	1839440	Okay, so what do you do with the Java part and the native part?
1839440	1851000	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.
1851040	1862240	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.
1862400	1865020	These programs basically do the other way around.
1865020	1877560	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.
1878460	1880640	With Java we have Jadix.
1880640	1883260	It's an amazing tool.
1883260	1884260	It does an excellent job.
1884260	1894140	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.
1894140	1902440	It does an excellent job reflecting back the byte code of Java into the actual code as close as possible to the original.
1902600	1907780	And it's relatively easy because Java is only compiled into an intermediate language.
1908000	1912400	When it comes to native code this is a bit harder and you have to use different tools.
1912600	1914020	This is an example from Ida.
1914020	1916680	Today we have Gidra which is also a great tool.
1917080	1919380	They basically show you the actual assembly.
1920060	1931900	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.
1931900	1950920	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.
1951920	1958120	So we knew, as I said, we knew that everything related to protobuf including creation of unknown 6 is in the native part.
1958140	1961640	Specifically in a library named Lib Niantic manager.
1961640	1969140	So we opened it in Ida and we had to pinpoint the actual function that generates unknown 6.
1969160	1971080	And there were a few methods to do it.
1971080	1974320	I think I laid down three different methods in my blog.
1974320	1977880	Here I will only share one method due to a lack of time.
1977880	1979760	This was the method that I used.
1979800	1992960	If you recall, using static analysis, we found that the starting bytes of the unknown 6 message are only dependent on time.
1992960	1998220	So naturally the code that generates unknown 6 has to rely on time somehow.
1998220	2007160	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.
2007280	2012400	So I went to Ida, to the import table, and I did find the time function being imported.
2012480	2019380	And I looked at all the cross references meaning all the locations in the assembly in the library that calls time.
2019380	2020360	I found two.
2020360	2022520	The bottom one wasn't related at all.
2022520	2023800	It was very easy to see.
2023800	2028520	The top one, the function at offset 87444, I can't even see it.
2028520	2029640	I remember it.
2030640	2040560	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.
2040560	2044240	Then this function generates 32 random bytes.
2044240	2056680	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.
2056680	2058620	And for this, we get our fourth badge.
2058620	2060160	Badge of unknown 6.
2061260	2062220	Okay.
2062560	2067300	So at this point, we looked at a specific function that we pinpointed.
2067300	2072200	We called it sig encrypt function because we believe it encrypts a signature.
2072200	2073600	It was only natural.
2074080	2081020	And we decided to verify beyond doubt that it's exactly the function that we wanted to generate unknown 6.
2081020	2088280	So we used active probing again, excuse me, dynamic reverse engineering again to detect that.
2088340	2091560	This time, instead of exposed, we used Frida.
2091560	2099360	Frida is a very, very powerful tool that allows you to basically hook using JavaScript any function that you want.
2099360	2101600	It works on Windows, Linux, Android.
2102220	2103500	It's very strong for Android.
2103500	2109160	Here you can see a short Frida script that basically finds the ‑‑ so it hooks on the native library.
2109160	2113900	It finds the actual address of the function that we wanted, offset 87444.
2113900	2119480	It hooks both the enter of the function, so every time it runs, and the return.
2119480	2122140	And simply dumps the input and the output.
2122140	2136540	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.
2137340	2150820	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.
2150820	2152720	So I heard you like port above.
2153180	2156400	And it's a very, very long object.
2156540	2162720	And there are some interesting strings there, like one plus that was my device back then.
2163560	2167920	And it looked like field that was supposed to be in the signature, so we had that.
2167920	2177920	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.
2177920	2179880	And it turned out to be quite simple.
2179880	2182360	So I will be able to show you this today.
2182360	2186880	Here you can see the decompiled version of the sig encrypt function.
2187480	2190600	And let's start with an input of length 700.
2190600	2193120	I split it into 256 chunks.
2193120	2198040	The less chunks is a shorter one, just for easier understanding.
2198180	2207560	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.
2207560	2211240	So that's the 32 bytes that we saw in the final version.
2211700	2217040	Then it pads the input to the next closest block of 256.
2217420	2224540	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.
2224540	2226220	And this is not the interesting part.
2226220	2227440	This is the interesting part.
2227440	2228480	We have encryption.
2228480	2232400	So we have encryption at offset 9E9D8.
2232420	2234120	This is the encryption function.
2234240	2243440	And first the function starts by expanding the seed into a block of 256 using an expansion mechanism.
2243700	2245500	And then encryption works.
2245560	2256520	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.
2256520	2272120	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.
2272380	2274060	And this is nothing proprietary.
2274060	2283060	This is a very well-known cryptographic scheme named CBC mode that has to do with encrypting blocks based on initialization vector.
2283580	2287880	So now that we know how the sync and encrypt function works, we split into two groups.
2287880	2297600	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.
2297600	2307300	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.
2308040	2313220	And actually, I think it was five hours later, the first group already succeeded.
2313220	2314580	They were very, very good.
2314780	2317920	And replicating and getting the encryption to work.
2317920	2319460	So how did they do it?
2319560	2327880	They were very smart and they walked through the entire call chain of the encryption whatever numbers that came afterwards.
2327880	2351280	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
2351280	2360660	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.
2361180	2368140	And for successfully replicating the encryption function, we get our fifth badge, the badge of encryption.
2370320	2378800	So meanwhile, the second group that I was a part of actually focused on understanding how the signature works.
2378800	2386880	So there are a few subfields within the signature message that were very easy to understand according to the values in the field.
2386880	2387880	And you saw them earlier.
2387880	2403700	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.
2404460	2411060	And actually the third message that we found, we understood what it means quite funnily.
2411060	2423100	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.
2423160	2426540	So we understood that it contained sense of information.
2426540	2430280	Obviously emulators don't have any sense of information, real devices does.
2431460	2436920	It contains a lot of information regarding acceleration, altitude, gravity, etc.
2437480	2448360	And actually at this point, so this is how the signature declaration looked like at the time, what we understood so far.
2449020	2456140	And inclusion of fields like this field, like the sensor info field, actually something before that.
2456420	2464740	So people that were using active probing to understand things about the signature protobuf, started getting their account banned.
2465280	2478480	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.
2478520	2485140	And if you consider things like sensor info, this is kind of akin to tracking your mouse movement for captures.
2485420	2493740	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.
2494000	2500160	But this thought also made us wonder, does the server actually verify this?
2500160	2507320	So to verify things like sensor information based on previous sensor information is a very , very difficult computational job.
2507320	2511460	And we knew that the servers were under high load at the time.
2511460	2516360	So we thought, well, maybe not all fields in the signature are necessary.
2516360	2522720	So we employed active probing again using dynamic reverse engineering, using Frida.
2522720	2530080	We started removing fields from the signature before it was encrypted and seeing when the server starts responding with an error.
2530080	2533820	And we found out that most of the signature was not needed.
2534040	2538260	So all the fields that you can see that are marked with not required, were not required.
2538580	2541000	And actually only six fields were required.
2541000	2542600	All of these highlighted here.
2542600	2546480	Two of which were just regular time stamps, which were very simple.
2546480	2549540	So we were left with just four fields to go.
2549960	2553980	Field 10, field 20, field 22 and field 24.
2554640	2557920	At this point, I recall, the discord was going crazy.
2557920	2563580	So here we are at the end of the second day of the hackathon and we were very tired at the time.
2563920	2580200	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.
2580220	2585960	And another thing that happened, so we had this channel named RE research, which all the hackers were a part of.
2586200	2590660	And eventually, because people kept asking, it was open to the public.
2590660	2592900	But only for view, not for chatting.
2592900	2594580	So only we were able to chat.
2594580	2602680	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.
2602680	2606440	It was a very humbling experience, I can say.
2606800	2608880	But it was very, very crazy.
2609140	2627220	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,
2627220	2629660	which kind of looks like a hashing function.
2629880	2636420	And we found that field 20 was a very simple hash of this number.
2636420	2638580	We don't know what it means to this day.
2638580	2640040	If you know, please approach me.
2640040	2641400	I would be happy to know.
2641960	2646560	And the string of your location chained together .
2647240	2651140	And the output of the function was inserted in the field 20.
2651140	2653840	Field 10 was a bit more complicated, but not so much.
2653840	2662900	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.
2662900	2669120	So we have two hashes, both on the location of the user, one of which is also on the dedication info.
2671620	2674080	So at this point we tried to understand what's the hash.
2674080	2678940	And one technique to do it is to look at the constants being used in the hash and comparing them to known hashes.
2678940	2683120	Using this we very easily found that it's a very famous hash named XX hash.
2683140	2685460	And this was a 32-bit version of it.
2685460	2688000	We also found a 64-bit version of it.
2688000	2690500	And it was actually used for field 24.
2690500	2694840	So field 24 was a repeated field, at least as you can see from this.
2694980	2704460	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.
2704600	2708120	And it was the hashing, the hash was added to field 24.
2708120	2711260	So field 24 contained as many hashes as you had requests.
2711300	2714440	So for this example you had three hashes in field 24.
2714640	2718800	And then we had the last field, the final field, the toughest nuts to crack.
2718800	2726080	We actually went in the wrong direction for a while because we saw that it changes frequently when your dedication info changes.
2727000	2729500	But it turned out to be completely false.
2729500	2735880	One researcher found that the function that generates this field didn't get any input.
2736180	2740680	So this field could be basically any thing.
2740880	2744380	And once we tried to do it, we at 7 p.m.
2744380	2751240	the third day, 67 hours after the hackathon started, we finally managed to make our first successful API request.
2751260	2754600	And for this we get our final badge, the badge of API.
2756400	2757440	Thank you.
2758260	2759640	I'll just take one minute.
2760260	2767740	One minute to talk about, so I was very interested to, so this is my code from back there that implements the actual authentication.
2767740	2770400	You can laugh at it while I talk for the last minute.
2771040	2774020	I was kind of interested to know what happened afterwards.
2774020	2782560	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.
2782560	2790680	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.
2791480	2795600	And I was interested to know where the Pokemon Go hacking scene went from this.
2795600	2799460	So I kind of didn't get involved into more stuff afterwards.
2799460	2806420	So Pocodev continued maintaining their API for about a year and a half after this, after which they stopped.
2806560	2808100	Today I search online.
2808100	2810900	There's also a couple of bots that are offered for cash.
2811300	2820500	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.
2822360	2827760	I'll just like to thank both the team, Anon8, they were amazing guys.
2827760	2829540	I had fun to work with them.
2829540	2833740	I would also like EFF for supporting me and counseling me on giving this talk today.
2833740	2835580	If not for them, I was not here.
2835580	2837560	I will also thank you for coming.
2837920	2839780	And there's a reference slide.
2839780	2841620	Feel free to come, grab some stickers.
2841620	2842620	Thank you for listening.
