Chaining bugs in Telegram for Android app to steal sessionrelated files
Two flaws in Telegram for Android could be chained: a content provider accepted a crafted URI that pointed at an internal app file and passed the app's internal-URI check, and the sharing activity trusted a caller-supplied target chat id, sending content without user interaction. Together a malicious app could send Telegram's own internal files, including session-related preferences, to an attacker-controlled chat.
Published on dphoeniixx.medium.com
We will discuss the chaining of two bugs on the telegram android application, which can make malicious applications steal internal telegram files, including the session.
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
Telegram for Android exposes a sharing activity, LaunchActivity, which receives content shared from other applications and lets the user pick a chat to forward it to. Its intent filter accepts a broad set of MIME types, including video/*, image/*, text/plain and */*. The write-up chains two separate issues in that feature: a provider that lets an attacker reference an internal Telegram file while passing the internal-URI check, and a direct-share implementation that trusts a chat id supplied by the calling application. Together they let a malicious app on the same device read internal Telegram files, including the session, and send them to a chat without user interaction.
Setup
- Target: Telegram for Android.
- To reproduce the full exploit, the author says to downgrade Telegram to 5.4.0.
- The attacker needs a malicious application installed on the same device as Telegram, able to send intents to
LaunchActivity. - No other accounts, roles or tooling are described.
Finding it
The starting point is ordinary behaviour. Almost every messenger has an activity that receives content such as images, videos and text and forwards it to a chat or thread. Open the gallery, select a photo, tap Share or Open With, and a popup lists applications that can receive images, Telegram among them. In Telegram's manifest, LaunchActivity handles data with mimeType video/*, image/*, text/plain and */*.
The intent a gallery sends carries the Uri of the file, not the file content. When LaunchActivity receives that Uri, the file is opened in Telegram's context and the user is shown a chats list to choose where to send it. The author's idea was to send, instead of a Uri pointing at a file on the SD card, a path to an internal Telegram file.
Exploitation
Step 1: point the share at an internal file
The first attempt orders Telegram to share /data/data/org.telegram.messenger/shared_prefs/userconfing.xml to a chat. Telegram responds with a toast reading Unsupported content. The author attributes this to a check, AndroidUtilities.isInternalUri, that decides whether the Uri refers to an internal file. A week of attempts to bypass that method produced nothing.
Step 2: change the scheme instead of the check
The bypass comes from looking at what happens after the check. Once a Uri passes isInternalUri, it is forwarded to copyFileToCache, which copies the file into the cache. It opens the Uri with openInputStream, so it can open a Uri that refers to a provider. That means a provider Uri can be sent instead of a file scheme Uri.
Among Telegram's providers, the author found one that can refer to an internal Telegram file and still pass the isInternalUri check. Its openFile method takes the file path from the final_path parameter and returns a ParcelFileDescriptor for that file. The following provider Uri refers to /data/data/org.telegram.messenger/shared_prefs/userconfing.xml and passes isInternalUri:
content://org.telegram.messenger.notification_image_provider/msg_media_raw/1/test.txt?final_path=/data/data/org.telegram.messenger/shared_prefs/userconfing.xml
At this point Telegram can be forced to open an internal file and share it to a chat, but the user still has to choose the chat. That heavy interaction, picking the attacker's chat, makes the attack impractical on its own.
Step 3: abuse direct share
ChooserTargetService is an Android feature, introduced seven years before the write-up, that adds specific people from specific applications to the share popup so content can be sent to them directly by tapping their picture.

An application implements this by writing a service that extends ChooserTargetService, which tells the OS which items or people to list in the popup. The sharing activity then has to handle a new extra carrying the chat id or thread id to send the content to directly.
The implementation lacks security: the sharing activity does not establish whether the direct-share intent was genuinely populated. Any application that sends a sharing intent and sets the extra carrying the target chat id to any chat id causes the file to be sent to that chat without user interaction. Telegram had the same issue: sending the dialogId extra with any chat id makes Telegram send the message or file directly to that chat.
Step 4: chain both bugs
The final exploit combines the provider Uri with the dialogId extra, sending /data/data/org.telegram.messenger/shared_prefs/userconfing.xml to chat id 100000000 directly, with no user interaction. The author states that reproducing the full exploit requires downgrading Telegram to 5.4.0.
Root cause
Two design mistakes combine. First, a provider Uri whose final_path parameter names an internal file passes isInternalUri, and the provider's openFile then returns a descriptor for that internal path. Second, the sharing activity trusts the direct-share extra (dialogId) supplied by the calling application, so any app can name the destination chat instead of the user choosing it. The report does not describe the code of either check beyond the method and parameter names above.
Impact
A malicious application installed on the same device can make Telegram read internal files, including the session, and send them to a chat without user interaction. The author frames this as stealing internal Telegram files, including the session. The report does not state which Telegram versions are affected, nor any bounty or severity rating.
Fix
Based on the described mechanism, the checks that should change are the internal-URI validation, which should not be satisfiable by a provider Uri that resolves to an internal path, and the handling of the direct-share extra, which should not be trusted from an arbitrary calling application.
Variants to test
- Enumerate exported providers in the target app and look for
openFileimplementations that take a path from a query parameter. A positive result is a providerUrithat returns a descriptor for a file outside the provider's intended directory. - Send provider
Uris through any activity that copies a receivedUriinto a cache withopenInputStream. A positive result is an internal file appearing in the cache or being forwarded. - Send a share intent with the direct-share extra set to an arbitrary chat id from a separate app. A positive result is delivery to that chat with no chooser interaction.
- Try the same direct-share extra against other applications implementing
ChooserTargetService. A positive result is content delivered to an attacker-chosen target without the user selecting it.
What happened, in plain words
Telegram is a messaging app. On Android, like most messengers, it offers a convenient feature: when you are in your phone's gallery and tap Share, Telegram appears in the list of apps you can send the picture to. You pick Telegram, Telegram shows you your chats, you pick one, and the picture goes there.
This write-up describes two separate mistakes in that feature. When they are combined, an ordinary malicious app installed on the same phone can make Telegram read its own private files, including the file that keeps you logged in (the session), and send them to a chat chosen by the attacker, without the user tapping anything.
The report does not give a bounty amount or a severity rating, and it does not describe a fix or a vendor response.
Background: what a sharing activity is
Android apps are built from small pieces called activities. An activity is roughly one screen of an app. A sharing activity is a screen that other apps can hand content to, such as a photo, a video or a piece of text, so that the receiving app can do something with it, usually forward it to a chat.
Apps announce which kinds of content they accept in a file called the manifest, using an <intent-filter>. The filter lists MIME types. A MIME type is a label for a kind of content, for example image/* means any kind of image and text/plain means plain text. The */* entry means anything at all.
In Telegram's manifest, the sharing activity is called LaunchActivity, and it accepts video/*, image/*, text/plain and */*. That is why Telegram shows up in the share popup for almost any file.
How sharing actually works
When you tap Share in your gallery, the gallery does not send the picture itself. It sends a Uri. A Uri is an address, like a street address for a file or a piece of content. The receiving app uses that address to open the file later.
When LaunchActivity receives the Uri, it opens the file in Telegram's own context, meaning with Telegram's permissions, and shows the user a list of chats so the user can choose where to send it.
The author's idea was simple: instead of sending an address that points at a photo on the SD card, send an address that points at one of Telegram's own private files.
Step 1: point the share at an internal file
The first attempt told Telegram to share /data/data/org.telegram.messenger/shared_prefs/userconfing.xml to a chat. That path is inside Telegram's private storage area, where Android keeps an app's own data. The file userconfing.xml is a settings file that Telegram keeps in its shared_prefs folder.
Telegram refused, showing a toast with the message Unsupported content. A toast is the small popup message that appears briefly at the bottom of an Android screen.
The author concluded that Telegram checks whether the Uri points at an internal file, using a method called AndroidUtilities.isInternalUri. A method is a named block of code that does one job. The author spent a week trying to bypass that check and got nowhere.
Step 2: change the scheme instead of the check
The breakthrough came from looking at what happens after the check, not at the check itself. Once a Uri passes isInternalUri, Telegram forwards it to a method called copyFileToCache. The name says what it does: it copies the file into the cache, a temporary storage area.
To copy the file, it opens the Uri with openInputStream. An input stream is a way of reading data from something, like a pipe you read from. The important detail is that openInputStream can open a Uri that refers to a provider, not only a Uri that refers to a file directly.
A provider (formally a content provider) is an Android component that lends out data to other apps through addresses. Instead of a file:// address, you use a content:// address, and the provider decides what to hand back.
So the author searched Telegram's providers for one that could point at an internal Telegram file and still pass the isInternalUri check. One was found. Its openFile method takes the file path from a parameter called final_path and returns a ParcelFileDescriptor for that file. A ParcelFileDescriptor is Android's handle for an open file, something the receiving code can read from.
The following provider Uri refers to /data/data/org.telegram.messenger/shared_prefs/userconfing.xml and passes isInternalUri:
content://org.telegram.messenger.notification_image_provider/msg_media_raw/1/test.txt?final_path=/data/data/org.telegram.messenger/shared_prefs/userconfing.xml
Line by line:
content://says this is a provider address rather than a plain file address.org.telegram.messenger.notification_image_providernames the provider inside Telegram./msg_media_raw/1/test.txtis the path the provider expects for its normal job, here a placeholder name.?final_path=...is a query parameter, the part after the question mark that carries extra settings. The provider reads this value as the real file path, and the value given is Telegram's private settings file.
At this point Telegram can be forced to open an internal file and share it to a chat. But the user still has to pick the chat. That is a heavy interaction, and an attacker cannot rely on a victim choosing the attacker's chat. On its own, the attack is impractical.
Step 3: abuse direct share
Android has a feature called ChooserTargetService, introduced seven years before the write-up. It adds specific people from specific apps to the share popup, so you can send content straight to them by tapping their picture instead of navigating through the app.

To offer this, an app writes a service that extends ChooserTargetService. A service is an Android component that runs in the background without a screen. This service tells the operating system which items or people to list in the popup. The sharing activity then has to handle a new extra. An extra is a small piece of data attached to an intent, like a note stapled to a parcel. This extra carries the chat id or thread id to send the content to directly.
The implementation lacks security. The sharing activity never establishes whether the direct-share intent was genuinely populated by the system or simply crafted by another app. Any app that sends a sharing intent and sets the extra carrying the target chat id to any chat id causes the file to be sent to that chat without user interaction.
Telegram had the same issue. Sending the dialogId extra with any chat id makes Telegram send the message or file directly to that chat.
Step 4: chain both bugs
The final exploit combines the provider Uri from step 2 with the dialogId extra from step 3. It sends /data/data/org.telegram.messenger/shared_prefs/userconfing.xml to chat id 100000000 directly, with no user interaction.
The author states that reproducing the full exploit requires downgrading Telegram to 5.4.0. The report does not state which versions are affected.
Root cause
Two design mistakes combine.
- A provider
Uriwhosefinal_pathparameter names an internal file passesisInternalUri, and the provider'sopenFilethen returns a handle for that internal path. - The sharing activity trusts the direct-share extra (
dialogId) supplied by the calling app, so any app can name the destination chat instead of the user choosing it.
Impact
A malicious app installed on the same device can make Telegram read internal files, including the session, and send them to a chat without user interaction. The session is the data that keeps you logged in.
Fix
Based on the described mechanism, the checks that should change are the internal-URI validation, which should not be satisfiable by a provider Uri that resolves to an internal path, and the handling of the direct-share extra, which should not be trusted from an arbitrary calling app.
Variants to test
- Enumerate exported providers in the target app and look for
openFileimplementations that take a path from a query parameter. A positive result is a providerUrithat returns a handle for a file outside the provider's intended directory. - Send provider
Uris through any activity that copies a receivedUriinto a cache withopenInputStream. A positive result is an internal file appearing in the cache or being forwarded. - Send a share intent with the direct-share extra set to an arbitrary chat id from a separate app. A positive result is delivery to that chat with no chooser interaction.
- Try the same direct-share extra against other apps implementing
ChooserTargetService. A positive result is content delivered to an attacker-chosen target without the user selecting it.
Glossary
- Activity: one screen of an Android app, such as a chat list or a share screen.
- API: a defined way for one piece of software to talk to another, like a menu of allowed requests.
- Cache: temporary storage an app uses to keep copies of files for quick access.
- ChooserTargetService: an Android service that supplies specific people or items to the share popup for one-tap sharing.
- Content provider: an Android component that lends data to other apps through
content://addresses. - Extra: a small piece of data attached to an intent, like a note stapled to a parcel.
- Input stream: a way of reading data from something, like a pipe you read from.
- Intent: a message one Android app sends to another asking it to do something, such as open a file or share content.
- Intent filter: the manifest entry where an app declares which kinds of content and requests it accepts.
- Internal file: a file inside an app's private storage area, which other apps normally cannot read.
- Manifest: the file in an Android app that declares its components and what they accept.
- Method: a named block of code that performs one job.
- MIME type: a label for a kind of content, such as
image/*for any image ortext/plainfor plain text. openFile: a provider method that returns a handle to a file for the caller to read.openInputStream: a method that opens aUrifor reading, including providerUris.ParcelFileDescriptor: Android's handle for an open file, which code can read from.- Provider
Uri: an address starting withcontent://that points at data lent out by a content provider. - Query parameter: the part of a
Uriafter the question mark that carries extra settings, such asfinal_path. - Service: an Android component that runs in the background without a screen.
- Session: the data that keeps a user logged in to an app, which an attacker can use to take over the account.
- Toast: a small popup message that appears briefly at the bottom of an Android screen.
Uri: an address for a file or piece of content, like a street address.- Vulnerability class: a category of security mistake, here arbitrary file read on Android.