> For the complete documentation index, see [llms.txt](https://gitbook.cdxiaodong.life/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://gitbook.cdxiaodong.life/cicd-an-quan/yi-lai-an-quan/dependency-confusion.md).

# 依赖混淆

## 概述

公司使用内部注册表来存放内部和公共依赖。换句话说，他们尝试创建一个包含所有依赖的单一注册表，其中依赖项由公司员工添加。这种方法的目的是最小化对公共注册表的依赖，从而减少在内部使用被篡改依赖的可能性。

然而，并不总是可能完全禁止到公共注册表的所有流量。因此，如果包管理器在解析时尝试在公共注册表中查找内部依赖，这可能导致依赖混淆。

攻击按照以下方式发展：

* 公司有一个名为 `internal-lib` 的内部包。
* `internal-lib` 没有放置在公共注册表中，如 pypi、npm、rubygems 等。
* 攻击者制作一个在安装期间执行任意代码的包，并将其作为 `internal-lib` 上传到公共注册表。
* 包管理器在安装过程中使用来自公共注册表的 `internal-lib`，导致代码执行。

{% embed url="<https://0xsapra.github.io/website//Exploiting-Dependency-Confusion>" %}

## 如何在安装期间执行代码？

## 技巧

### 数据收集

为了在基于数据识别组织的能力和避免收集过多敏感信息的需求之间取得平衡，您可以收集以下数据：

* 用户名
* 主机名
* 当前路径

连同外部IP一起，这些数据应该足够帮助安全团队识别潜在易受攻击的系统，并避免将测试误认为是真实攻击。

### 数据检索

由于大多数可能的目标位于保护良好的企业网络深处，DNS渗透是检索收集数据的最佳方式。将十六进制编码的数据作为DNS查询的一部分发送到自定义权威名称服务器。您可以使用以下资源来实现DNS渗透：

* [1u.ms](https://github.com/neex/1u.ms) 是一个小型零配置DNS工具集，提供易于使用的DNS重绑定工具，以及获取任何给定内容的可解析资源记录的方法
* [Interactsh](https://github.com/projectdiscovery/interactsh) 是一个OOB交互收集服务器和客户端库

## 参考资料

* [文章：依赖混淆：我如何入侵苹果、微软和其他数十家公司](https://medium.com/@alex.birsan/dependency-confusion-4a5d60fec610)
* [报告：不安全的Bundler配置从Rubygems.org获取内部Gems（okra）](https://hackerone.com/reports/1104874)
* [Geek Freak：依赖混淆](https://dhiyaneshgeek.github.io/web/security/2021/09/04/dependency-confusion/)
* [文章：RyotaK的博客 - Cloudflare的cdnjs中的远程代码执行](https://blog.ryotak.me/post/cdnjs-remote-code-execution-en/)
* [Black Hat EU 2021 - 挑选锁定文件：攻击和防御您的供应链](https://gitlab.com/gitlab-com/gl-security/threatmanagement/redteam/redteam-public/red-team-tech-notes/-/tree/master/blackhat-eu-2021-picking-lockfiles)
* [DustiLock](https://github.com/Checkmarx/dustilock) 是一个查找哪些依赖容易受到依赖混淆攻击的工具。
* [Confuser](https://github.com/doyensec/confuser) 是一个检测依赖混淆漏洞的工具，允许扫描`packages.json`文件，生成并向NPM存储库发布payload，最后聚合来自易受攻击目标的回调。


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

```
GET https://gitbook.cdxiaodong.life/cicd-an-quan/yi-lai-an-quan/dependency-confusion.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
