Find-Instagram-Contacts-For-Any-User-On-Facebook
A GraphQL endpoint allowed querying any Instagram user's Facebook contacts by supplying only their username, without authentication. An attacker could retrieve the list of Facebook users associated with any Instagram account.
Published on philippeharewood.com
It’s possible to find out for any Instagram user, which Facebook contacts he/she is associated.
The full write-up lives on its author's site
It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.
Context
Facebook exposes a GraphQL endpoint at graph.facebook.com/graphql, and the report concerns a field reachable through it called instagram_contacts_on_facebook. The response contained the Facebook users linked to a given Instagram user through Instagram friendship connections. The researcher's concern is that this data was reachable for any Instagram username, not only for the account making the request.
Setup
The report does not describe any accounts, roles, tools or versions used. The only precondition visible in the text is that the request is sent to the GraphQL endpoint with a query naming an Instagram username.
Finding it
The researcher noticed that the instagram_contacts_on_facebook field sits under the viewer edge. In the researcher's understanding, the viewer edge should only allow requesting objects within the caller's own viewer context. Because the query takes a .username argument, the field could be pointed at an arbitrary Instagram account, which the researcher describes as the .username being used a bit too loosely.
Exploitation
Step 1: Query the contacts field for a target username
The researcher issued the following request, substituting the target Instagram username:
https://graph.facebook.com/graphql?q=viewer(){instagram_contacts_on_facebook.username(INSTAGRAM_USERNAME){instagram_contacts}}
The query walks from viewer into instagram_contacts_on_facebook, passes the target INSTAGRAM_USERNAME to the .username argument, and selects the instagram_contacts field. The response came back as a set of nodes, each describing a Facebook user:
{
"viewer": {
"instagram_contacts_on_facebook": {
"instagram_contacts": {
"nodes": [
{
"id": "1",
"url": "https://www.facebook.com/1",
"name": "Kanye West"
},
{
"id": "114308365327046",
"url": "https://www.facebook.com/114308365327046",
"name": "Travis Scott"
},
{
"id": "6885814958",
"url": "https://www.facebook.com/profile.php?id=6885814958",
"name": "Lil Wayne"
}"
}]}}}}
Each node carries an id, a url pointing at the corresponding Facebook profile, and a name. The actual responses contained Facebook users associated to the target via Instagram friendship connections.
Step 2: Consider identifying the target's own profile
The researcher notes that it might also have been possible to identify the Facebook profile belonging to the target Instagram account, given enough of these connections. The example given is Facebook users who follow Facebook friends on Instagram.
Root cause
The field is placed under the viewer edge but accepts a username argument, so the viewer context is not actually enforced on the object being requested. The GraphQL field call should have been using an Instagram access token or another form of authentication rather than accepting a username. In other words, the viewer edge did not appear to restrict the request to the caller's own context once an arbitrary username was supplied.
Impact
The data revealed was reasonably sensitive and not intended to be shown in this context. It was possible to find out, for any Instagram user, which Facebook contacts that user is associated with. With enough of these connections, it might also have been possible to identify the Facebook profile belonging to the target Instagram account.
Fix
Facebook fixed the issue on 4 April 2017, according to the timeline. The report does not describe the specific change made. The researcher's stated expectation is that the field should require an Instagram access token or another form of authentication instead of accepting a username.
Variants to test
- Query other fields under the
vieweredge that accept a username or identifier argument, and check whether they return data for accounts other than the caller's. A positive result is a response containing another user's data. - Try the same
instagram_contacts_on_facebookfield with different username formats (for example, with and without a leading@) to see whether normalisation bypasses any check. A positive result is a response for a target you do not control. - Test whether the endpoint accepts the query without any authentication token at all. A positive result is a populated response rather than an authentication error.
- Enumerate related GraphQL fields that expose contact or friend lists and check whether they enforce the viewer context. A positive result is any field returning another user's connections.
Timeline
- 24 March 2017: report sent
- 28 March 2017: escalation by Facebook
- 4 April 2017: fixed by Facebook
- 6 April 2017: bounty awarded by Facebook
What happened, in plain words
Facebook runs a service that other software can talk to, called an API. Think of an API as a restaurant menu: you do not walk into the kitchen, you pick an item from the menu and the kitchen sends it back. Here the menu is a GraphQL endpoint, a single web address that accepts questions written in a small query language and returns exactly the pieces of data you ask for.
One of the items on that menu was a field called instagram_contacts_on_facebook. Its job is to show the Facebook contacts linked to an Instagram account, meaning the Facebook users connected to a given Instagram user through Instagram friendship connections.
The problem was who you could ask about. The field sat under something called the viewer edge. In GraphQL, an edge is just a path you walk through the data, like a folder inside a folder. The viewer edge is supposed to mean "the person making this request, and only them". But the field also accepted a username as an input, so the researcher could point it at any Instagram account, not just his own. The result was that it was possible to find out which Facebook contacts are associated with any Instagram user.
The report was sent on 24 March 2017. Facebook escalated it on 28 March, fixed it on 4 April, and awarded a bounty on 6 April.
The pieces you need to know first
Before walking through the request, here are the ideas involved, from zero.
- API: a way for one piece of software to ask another for data or actions. A weather app asking a weather service for today's forecast is using an API.
- GraphQL: a style of API where the client sends a query describing exactly which fields it wants, and the server returns only those fields. Instead of many fixed addresses, there is usually one address that accepts queries.
- Field: a named piece of data inside a GraphQL query, like a column in a spreadsheet.
instagram_contactsis a field. - Edge: a path segment in a GraphQL query. Walking
viewertheninstagram_contacts_on_facebookmeans "start from the viewer, then go into their Instagram contacts on Facebook". - Argument: an input you pass to a field, written in brackets. Here
.username(INSTAGRAM_USERNAME)passes a username. - Viewer context: the idea that a request should only see data belonging to the account that made it. Like a bank app showing only your own balance, never your neighbour's.
- Authentication: proving who you are, usually with a token or a password. A token is a short string the server gives you after you log in, and you show it on each request so the server recognises you. A cookie is one common way a browser stores such a token.
- Authorisation: what you are allowed to do once you are recognised. Authentication is the bouncer checking your name; authorisation is the list of rooms your badge opens.
- Bug bounty: a programme where companies pay researchers for reporting security problems responsibly instead of exploiting them.
Finding it
The researcher noticed that instagram_contacts_on_facebook sits under the viewer edge. In his understanding, the viewer edge should only allow requesting objects within the caller's own viewer context. That is, if you are logged in as yourself, viewer should mean you, and nothing else.
But the field also takes a .username argument. That means the query can name a different Instagram account than the one making the request. The researcher described this as the .username being used a bit too loosely: the field was willing to look up any username, not just the viewer's own.
Exploitation
Step 1: Query the contacts field for a target username
The researcher sent the following request, replacing the placeholder with the target Instagram username:
https://graph.facebook.com/graphql?q=viewer(){instagram_contacts_on_facebook.username(INSTAGRAM_USERNAME){instagram_contacts}}
Line by line:
https://graph.facebook.com/graphqlis the address of Facebook's GraphQL endpoint. This is the single door that accepts queries.?q=starts the query parameter. Everything after the equals sign is the GraphQL query itself, sent as part of the URL.viewer()starts from the viewer, meaning the context of the person making the request.instagram_contacts_on_facebookwalks into the field that lists the Facebook contacts linked to an Instagram account..username(INSTAGRAM_USERNAME)passes the target Instagram username as an argument. This is the part that lets the query point at someone else.{instagram_contacts}selects the contacts field, asking the server to return the list of contacts.
The response came back as a set of nodes, each describing a Facebook user:
{
"viewer": {
"instagram_contacts_on_facebook": {
"instagram_contacts": {
"nodes": [
{
"id": "1",
"url": "https://www.facebook.com/1",
"name": "Kanye West"
},
{
"id": "114308365327046",
"url": "https://www.facebook.com/114308365327046",
"name": "Travis Scott"
},
{
"id": "6885814958",
"url": "https://www.facebook.com/profile.php?id=6885814958",
"name": "Lil Wayne"
}"
}]}}}}
Line by line:
- The outer
viewerobject mirrors the start of the query. The server echoes back the path you asked for. - Inside it,
instagram_contacts_on_facebookholds the result for that field. - Inside that,
instagram_contactsholds the list of contacts. nodesis the array of individual results. Each node is one Facebook user.- Each node has an
id, aurlpointing at the corresponding Facebook profile, and aname.
The actual responses contained Facebook users associated to the target via Instagram friendship connections. In other words, by naming someone else's Instagram username, the researcher received the Facebook contacts linked to that person.
Step 2: Consider identifying the target's own profile
The researcher also noted that it might have been possible to identify the Facebook profile belonging to the target Instagram account, given enough of these connections. The example given is Facebook users who follow Facebook friends on Instagram. The idea is that if you collect enough of a person's connections, patterns in those connections can reveal who the person is, even without a direct name.
Root cause
The field is placed under the viewer edge but accepts a username argument, so the viewer context is not actually enforced on the object being requested. The GraphQL field call should have been using an Instagram access token or another form of authentication rather than accepting a username. In other words, the viewer edge did not appear to restrict the request to the caller's own context once an arbitrary username was supplied.
A simple comparison: imagine a website where the page at /my-orders shows your orders, but it also accepts ?user=bob and shows Bob's orders instead. The page looks personal, but the check that it is really you is missing. That is the same shape of mistake here.
Impact
The data revealed was reasonably sensitive and not intended to be shown in this context. It was possible to find out, for any Instagram user, which Facebook contacts that user is associated with. With enough of these connections, it might also have been possible to identify the Facebook profile belonging to the target Instagram account.
Fix
Facebook fixed the issue on 4 April 2017, according to the timeline. The report does not describe the specific change made. The researcher's stated expectation is that the field should require an Instagram access token or another form of authentication instead of accepting a username.
Variants to test
If you want to look for similar problems yourself, here are the kinds of checks the researcher's approach suggests. Each one is a way of asking "does this field really only show me my own data?"
- Query other fields under the
vieweredge that accept a username or identifier argument, and check whether they return data for accounts other than the caller's. A positive result is a response containing another user's data. - Try the same
instagram_contacts_on_facebookfield with different username formats (for example, with and without a leading@) to see whether normalisation bypasses any check. A positive result is a response for a target you do not control. - Test whether the endpoint accepts the query without any authentication token at all. A positive result is a populated response rather than an authentication error.
- Enumerate related GraphQL fields that expose contact or friend lists and check whether they enforce the viewer context. A positive result is any field returning another user's connections.
Timeline
- 24 March 2017: report sent
- 28 March 2017: escalation by Facebook
- 4 April 2017: fixed by Facebook
- 6 April 2017: bounty awarded by Facebook
Glossary
- Argument: an input passed to a GraphQL field, written in brackets after the field name.
- Authentication: proving who you are, usually with a token or password.
- Bug bounty: a programme where companies pay researchers for reporting security problems responsibly.
- Cookie: a small piece of data a browser stores for a website, often used to hold a session token.
- Edge: a path segment in a GraphQL query, like a folder inside a folder.
- Field: a named piece of data inside a GraphQL query.
- GraphQL: a style of API where the client sends a query describing exactly which fields it wants.
- Node: one individual result inside a list returned by GraphQL.
- Token: a short string the server gives you after you log in, shown on each request so the server recognises you.