Facebookmarketingdevelopers.com: Proxies, CSRF Quandry and API Fun
An AJAX endpoint on facebookmarketingdevelopers.com acted as a proxy to Facebook's ad API and returned ad account data even when the session cookie was missing or altered. An attacker could therefore read other users' ad account, page, audience, app, business and product data without authorisation.
This post is dedicated to @fin1te and our times in Vegas talking about super secret secret secret bug finding techniques and ☃ Hacktober: Cyber Security Awareness Month☃.
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
facebookmarketingdevelopers.com is a site that, once a user is logged in, presents API samples for ad account information. The samples are served through AJAX endpoints under /common/ajax/, and those endpoints call the Facebook API on the user's behalf. The researcher describes the AJAX call as acting as a proxy: the browser talks to facebookmarketingdevelopers.com, and that site forwards the request to the API.
The write-up follows the CSRF testing method published by @fin1te, applying steps 1 to 5 from that article to this target. The researcher notes a similar earlier case: @Stephen's "Hacking Facebook's Legacy API, Part 1: Making Calls on Behalf of Any User", where the https://touch.facebook.com/api/ endpoint acted as a proxy.
Setup
The only precondition described is a logged-in user on facebookmarketingdevelopers.com. The researcher inspects the area that presents API samples for ad account information. The session carries a csrftoken and a sessionid; there is no fb_dtsg parameter in this case. No specific tools or versions are mentioned.
Finding it
The starting point is the ad account sample endpoint. The researcher performs the request without modifying any parameters to see the expected result.
HTTP GET https://www.facebookmarketingdevelopers.com/common/ajax/ad_account
Response:
{"data": [{"amount_spent": "99999","id": "act_ID_1111","account_status": 1,"name": "Ad Account ID","timezone_name": "America/Los_Angeles","currency": "USD"}]}
Because the AJAX call returns API data, the researcher concludes it is requesting the API and acting as a proxy. That makes it a candidate for the same class of issue seen in the legacy API case: a server-side component that makes calls on behalf of a user, where the authentication of the caller may not be checked properly.
Exploitation
Step 1: Baseline request
The unmodified request above returns the expected ad account for the logged-in user. This establishes the shape of a successful response: a data array containing an object with amount_spent, id, account_status, name, timezone_name and currency.
Step 2: Remove the CSRF token completely
In this case there was no fb_dtsg parameter, only a csrftoken and sessionid. The researcher notes there was no fb_dtsg parameter, only a csrftoken and sessionid, and the response was the same as before:
{"data": [{"amount_spent": "99999","id": "act_ID_1111","account_status": 1,"name": "Ad Account ID","timezone_name": "America/Los_Angeles","currency": "USD"}]
}
Step 3: Modify one character in the token, keeping the length the same
Modifying the sessionid still returned data:
{"data": [{"amount_spent": "1299999,","id": "act_ID_2222","account_status": 1,"name": "Hotline Bling","timezone_name": "America/Los_Angeles","currency": "CAD"}]}
This is not the researcher's data. Some other account's data is returned when the session is wrong. The response has a different id (act_ID_2222), a different name, a different currency (CAD instead of USD) and a different amount_spent.
Step 4: Remove the value of the token but leave the parameter in place
The researcher compares this to opening an incognito window, since no session should be present:
{"data": [{"amount_spent": "99999","id": "act_ID_1111","account_status": 1,"name": "Ad Account ID","timezone_name": "America/Los_Angeles","currency": "USD"}]}
This returns the initial data for act_ID_1111, but the session information for act_ID_1111 is not there. The researcher's conclusion is that users' data was being sent out to the public.
Step 5
Step 5 is not needed because the request is already a GET.
What this means
As long as the compromised account is in the "hot seat", anyone has control to see that data, because the access_token appears to be used at the current time even with incorrect authentication. From there an attacker can escalate through the following endpoints:
/common/ajax/ad_accountreturns all ad accounts of the compromised user, listed asact_ID./common/ajax/page?act=act_IDreturns all pages associated with that ad account./common/ajax/custom_audience?act=act_IDreturns all audiences./common/ajax/app?act=act_IDreturns a list of applications./common/ajax/business_managerreturns a list of businesses./common/ajax/product_catalog?business=BUSINESS_IDreturns a list of catalogs./common/ajax/product?catalog=CATALOG_IDreturns a list of products.
Some of these calls depend on an ID from a previous call. As long as the current account from /common/ajax/ad_account has been available without authentication, all the calls will work. The same account does not stay listed, so running a script will collect a decent set of data from marketing companies and large companies in general that have amount_spent greater than $100,000 USD, and in some cases one digit more.
Root cause
The endpoint acts as a proxy to the API and uses an access_token at request time. When the session is missing or incorrect, the server does not reject the request and returns data for whichever account is currently in the "hot seat". A request with a modified sessionid still produces a response. The researcher's phrasing is that the access_token seems to be used at the current time even with incorrect authentication. The exact mechanism behind the fallback is not described in the report.
Impact
An unauthenticated or wrongly authenticated caller can read ad account data for whichever account is currently being served, and the researcher lists further endpoints to escalate from there. The affected accounts are marketing companies and large companies, in particular those with amount_spent above $100,000 USD and sometimes one digit more. The exposure depends on the account being in the "hot seat" at the time of the request, and the same account does not stay listed, so an attacker has to script the requests to collect data across accounts.
Fix
The fix was to redirect the user to a login page when the session was incorrect or missing, and to remove the data from being seen without authorisation.
Timeline
- Oct 16, 2015: report sent
- Oct 19, 2015: additional information added
- Oct 20, 2015 at 1:43 PM: escalation by Facebook
- Oct 20, 2015 at 4:53 PM: fix confirmed by Facebook
- Oct 26, 2015: bounty awarded by Facebook
Variants to test
- Proxy endpoints that forward to an API. Look for AJAX routes that return API-shaped JSON, such as
/common/ajax/ad_accounthere. A positive result is a response containing another user's identifiers or values when the session is missing or altered. - CSRF token removal. Drop the CSRF parameter entirely and resend the request. A positive result is the same successful response as the baseline.
- Session identifier mutation. Change one character of the session identifier while keeping its length the same. A positive result is data for a different account than the one you are logged in as.
- Empty token value. Leave the parameter in place but empty its value, as if in an incognito window. A positive result is data returned without any session being present.
- Chained identifier endpoints. Take an ID from one response and feed it into endpoints such as
/common/ajax/page?act=act_ID,/common/ajax/custom_audience?act=act_ID,/common/ajax/app?act=act_ID,/common/ajax/product_catalog?business=BUSINESS_IDand/common/ajax/product?catalog=CATALOG_ID. A positive result is each endpoint returning objects for an account you do not control. - Repeated sampling. Because the served account changes, repeat the unauthenticated request over time and record which accounts appear. A positive result is a rotating set of accounts with high
amount_spentvalues.
What happened, in plain words
A researcher found that a Facebook site for marketing developers would hand out advertising account information to anyone who asked, even without a valid login. The site was supposed to show a logged-in user samples of how the API works, using their own ad account as the example. Instead, the part of the site that fetched those samples would answer requests from people who had no session, or a broken one, and it would return data belonging to whichever account happened to be loaded at that moment. That data included ad accounts, pages, audiences, apps, businesses, catalogues and products, belonging to marketing companies and large advertisers.
To understand the rest of this article you need a handful of ideas. I will introduce each one the first time it appears.
- API: an API is a way for one piece of software to ask another for data or to do something, without a human clicking around a website. Think of it as a service counter: you hand over a request in a fixed format, and you get back an answer in a fixed format. Facebook's API is the counter where programs ask Facebook for information about accounts, pages and so on.
- AJAX: AJAX is the technique a web page uses to fetch data from a server in the background, without reloading the whole page. When you scroll a feed and new posts appear, that is usually AJAX. The requests go to specific addresses, called endpoints.
- Endpoint: an endpoint is a specific address on a server that accepts a particular kind of request. If a website is a building, endpoints are the individual service windows in it.
- Proxy: a proxy is a middleman. Instead of talking to Facebook's API directly, your browser talks to facebookmarketingdevelopers.com, and that site forwards your request to the API and passes the answer back. Like asking a receptionist to phone someone for you: the receptionist makes the call, but the answer comes back through them.
- Cookie: a cookie is a small piece of data a website stores in your browser so it can recognise you on your next request. It is how a site remembers that you logged in.
- Token: a token is a value that stands in for proof of identity or permission, a bit like a cloakroom ticket. Whoever holds the ticket can collect the coat. A session token identifies your logged-in session; a CSRF token is a separate value used to prove a request really came from the site's own pages.
- CSRF: CSRF stands for Cross-Site Request Forgery. The idea is that a site accepts a state-changing or data-returning request without properly checking that the request came from its own pages and from the right user. A CSRF token is the usual defence: the site's pages include a secret value in each request, and the server rejects requests that lack it or carry the wrong one.
- GET request: a GET request is the simplest kind of web request, the one your browser makes when you type an address or click a link. It is meant for retrieving something, not for changing it.
- access_token: an access token is a credential that lets a program act on behalf of a user or an app when calling an API. It is like a key card: present it and the API lets you in.
The setup
facebookmarketingdevelopers.com is a site aimed at developers who build advertising tools. Once you log in, it shows you samples of API calls for ad account information, so you can see what a real request and a real answer look like. Those samples are fetched in the background through endpoints under /common/ajax/, and those endpoints call the Facebook API on your behalf. In other words, the site acts as the proxy described above: your browser talks to facebookmarketingdevelopers.com, and that site forwards the request to the API.
The researcher followed a published CSRF testing method by @fin1te, applying steps 1 to 5 from that article to this target. He also points to an earlier, similar case: @Stephen's "Hacking Facebook's Legacy API, Part 1: Making Calls on Behalf of Any User", where the https://touch.facebook.com/api/ endpoint acted as a proxy. The lesson from that earlier case is that when a server-side component makes calls on behalf of a user, the component may not check the caller's authentication properly.
The only precondition described is a logged-in user on facebookmarketingdevelopers.com. The session carries a csrftoken and a sessionid; there is no fb_dtsg parameter in this case. No specific tools or versions are mentioned.
Finding it
The starting point is the ad account sample endpoint. The researcher performs the request without modifying any parameters, to see what the expected result looks like. This is the baseline: you need to know what normal looks like before you can tell what abnormal looks like.
HTTP GET https://www.facebookmarketingdevelopers.com/common/ajax/ad_account
This line says: send a GET request to that address. Nothing else is attached, no parameters, no changes. It is the plainest possible version of the request the site's own pages make.
Response:
{"data": [{"amount_spent": "99999","id": "act_ID_1111","account_status": 1,"name": "Ad Account ID","timezone_name": "America/Los_Angeles","currency": "USD"}]}
Line by line: the outer braces hold a JSON object, which is just a way of writing structured data as text. Inside it there is a field called data, whose value is a list (the square brackets). The list contains one object, and that object has six fields: amount_spent (how much the account has spent, here 99999), id (the account's identifier, act_ID_1111), account_status (a number, 1), name (a label, Ad Account ID), timezone_name (America/Los_Angeles) and currency (USD).
Because the AJAX call returns API data, the researcher concludes it is requesting the API and acting as a proxy. That makes it a candidate for the same class of issue seen in the legacy API case: a server-side component that makes calls on behalf of a user, where the authentication of the caller may not be checked properly.
Exploitation
Step 1: Baseline request
The unmodified request above returns the expected ad account for the logged-in user. This establishes the shape of a successful response: a data list containing an object with amount_spent, id, account_status, name, timezone_name and currency. Everything that follows is compared against this shape. If a later response has the same shape but different values, that is a signal.
Step 2: Remove the CSRF token completely
In this case there was no fb_dtsg parameter, only a csrftoken and sessionid. The researcher notes there was no fb_dtsg parameter, only a csrftoken and sessionid, and the response was the same as before:
{"data": [{"amount_spent": "99999","id": "act_ID_1111","account_status": 1,"name": "Ad Account ID","timezone_name": "America/Los_Angeles","currency": "USD"}]
}
This is the same object as the baseline: same amount_spent of 99999, same id of act_ID_1111, same name, same timezone, same currency. The only difference is the line break before the final closing brace, which is just formatting.
Step 3: Modify one character in the token, keeping the length the same
Modifying the sessionid still returned data:
{"data": [{"amount_spent": "1299999,","id": "act_ID_2222","account_status": 1,"name": "Hotline Bling","timezone_name": "America/Los_Angeles","currency": "CAD"}]}
Look at what changed. The id is now act_ID_2222, not act_ID_1111. The name is Hotline Bling. The currency is CAD instead of USD. The amount_spent is 1299999, with a trailing comma inside the string. This is not the researcher's data. Some other account's data is returned when the session is wrong.
Why change only one character and keep the length the same? Because many systems check the shape of a token (its length, its format) before looking it up. Changing one character keeps the shape valid while making the value wrong. If the server still answers with data, it is not validating the session properly. And the fact that the data belongs to a different account shows the server is not falling back to "no data" but to somebody else's data.
Step 4: Remove the value of the token but leave the parameter in place
The researcher compares this to opening an incognito window, since no session should be present:
{"data": [{"amount_spent": "99999","id": "act_ID_1111","account_status": 1,"name": "Ad Account ID","timezone_name": "America/Los_Angeles","currency": "USD"}]}
This returns the initial data for act_ID_1111, but the session information for act_ID_1111 is not there. The researcher's conclusion is that users' data was being sent out to the public.
Why leave the parameter in place but empty? Because some servers behave differently when a parameter is missing entirely (they may reject the request as malformed) versus when it is present but empty (they may treat it as "no session" and carry on). An incognito window is a good comparison: it is a browser with no cookies, no logged-in session, nothing. If a request from that state still returns data, the data is not protected by a session at all.
Step 5
Step 5 is not needed because the request is already a GET. Here the request only reads data, so there is nothing to convert.
What this means
As long as the compromised account is in the "hot seat", anyone has control to see that data, because the access_token appears to be used at the current time even with incorrect authentication. "Hot seat" is the researcher's phrase for whichever account the server happens to be serving at that moment. From there an attacker can escalate through the following endpoints:
/common/ajax/ad_accountreturns all ad accounts of the compromised user, listed asact_ID./common/ajax/page?act=act_IDreturns all pages associated with that ad account./common/ajax/custom_audience?act=act_IDreturns all audiences./common/ajax/app?act=act_IDreturns a list of applications./common/ajax/business_managerreturns a list of businesses./common/ajax/product_catalog?business=BUSINESS_IDreturns a list of catalogues./common/ajax/product?catalog=CATALOG_IDreturns a list of products.
Some of these calls depend on an ID from a previous call. The ?act=act_ID part is a parameter: the endpoint address, then a question mark, then a name and a value. It tells the server which ad account you want pages, audiences or apps for. Likewise ?business=BUSINESS_ID asks for catalogues belonging to a business, and ?catalog=CATALOG_ID asks for products in a catalogue. As long as the current account from /common/ajax/ad_account has been available without authentication, all the calls will work. The same account does not stay listed, so running a script will collect a decent set of data from marketing companies and large companies in general that have amount_spent greater than $100,000 USD, and in some cases one digit more.
Root cause
The endpoint acts as a proxy to the API and uses an access_token at request time. When the session is missing or incorrect, the server does not reject the request and returns data for whichever account is currently in the "hot seat". A request with a modified sessionid still produces a response. The researcher's phrasing is that the access_token seems to be used at the current time even with incorrect authentication. The exact mechanism behind the fallback is not described in the report.
Impact
An unauthenticated or wrongly authenticated caller can read ad account data for whichever account is currently being served, and the researcher lists further endpoints to escalate from there. The affected accounts are marketing companies and large companies, in particular those with amount_spent above $100,000 USD and sometimes one digit more. The exposure depends on the account being in the "hot seat" at the time of the request, and the same account does not stay listed, so an attacker has to script the requests to collect data across accounts.
Fix
The fix was to redirect the user to a login page when the session was incorrect or missing, and to remove the data from being seen without authorisation. In plain terms: if you cannot prove who you are, you get sent to the login page instead of getting data.
Timeline
- Oct 16, 2015: report sent
- Oct 19, 2015: additional information added
- Oct 20, 2015 at 1:43 PM: escalation by Facebook
- Oct 20, 2015 at 4:53 PM: fix confirmed by Facebook
- Oct 26, 2015: bounty awarded by Facebook
Variants to test
- Proxy endpoints that forward to an API. Look for AJAX routes that return API-shaped JSON, such as
/common/ajax/ad_accounthere. A positive result is a response containing another user's identifiers or values when the session is missing or altered. - CSRF token removal. Drop the CSRF parameter entirely and resend the request. A positive result is the same successful response as the baseline.
- Session identifier mutation. Change one character of the session identifier while keeping its length the same. A positive result is data for a different account than the one you are logged in as.
- Empty token value. Leave the parameter in place but empty its value, as if in an incognito window. A positive result is data returned without any session being present.
- Chained identifier endpoints. Take an ID from one response and feed it into endpoints such as
/common/ajax/page?act=act_ID,/common/ajax/custom_audience?act=act_ID,/common/ajax/app?act=act_ID,/common/ajax/product_catalog?business=BUSINESS_IDand/common/ajax/product?catalog=CATALOG_ID. A positive result is each endpoint returning objects for an account you do not control. - Repeated sampling. Because the served account changes, repeat the unauthenticated request over time and record which accounts appear. A positive result is a rotating set of accounts with high
amount_spentvalues.
Glossary
- API: a way for software to ask another service for data or actions in a fixed format, like a service counter for programs.
- AJAX: a technique where a web page fetches data from a server in the background without reloading the page.
- Endpoint: a specific address on a server that accepts a particular kind of request.
- Proxy: a middleman that forwards a request to another service and passes the answer back.
- Cookie: a small piece of data stored in your browser by a website so it can recognise you on later requests.
- Token: a value that stands in for proof of identity or permission, like a cloakroom ticket.
- Session: the state of being logged in, usually tracked by a cookie or token.
- CSRF: Cross-Site Request Forgery, a class of issue where a site accepts a request without properly checking that it came from its own pages and the right user.
- CSRF token: a secret value included in requests to prove they came from the site's own pages; the server should reject requests without it.
- GET request: the simplest kind of web request, used to retrieve something, like typing an address or clicking a link.
- Parameter: a name and value attached to a request after a question mark, telling the server what you want.
- JSON: a text format for structured data, using braces, brackets, names and values.
- access_token: a credential that lets a program act on behalf of a user or app when calling an API.
- Authentication: the process of proving who you are.
- Authorisation: the process of deciding what a proven identity is allowed to see or do.
- Hot seat: the researcher's phrase for whichever account the server happens to be serving at that moment.
- Incognito window: a browser window with no stored cookies or logged-in session.
- Bounty: a reward paid by a company for a responsibly reported security issue.