首頁  >  文章  >  web前端  >  可疑的維護者揭露了 npm 供應鏈攻擊的線索

可疑的維護者揭露了 npm 供應鏈攻擊的線索

WBOY
WBOY原創
2024-07-18 15:06:49363瀏覽

這個故事開始於基於 React 的開源文件專案 Docusaurus 的維護者 Sébastien Lorber 注意到對套件清單的 Pull Request 變更。以下是對流行的 cliui npm 包的建議更改:

Suspicious Maintainer Unveils Threads of npm Supply Chain Attack
具體來說,讓我們注意使用不熟悉的語法的 npm 依賴項變更:

  "dependencies": {
    "string-width": "^5.1.2",
    "string-width-cjs": "npm:string-width@^4.2.0",
    "strip-ansi": "^7.0.1",
    "strip-ansi-cjs": "npm:strip-ansi@^6.0.1",
    "wrap-ansi": "^8.1.0",
    "wrap-ansi-cjs": "npm:wrap-ansi@^7.0.0"

大多數開發人員希望在套件或 Git 或基於文件的 URL 的值中看到 semver 版本範圍。然而,在這種情況下,有一個特殊的 npm: 前綴語法。是什麼意思?

因此,在此拉取請求中提出的變更的情況下,套件 string-width-cjs 將解析為版本 ^4.2.0 中的套件 string-width。這意味著將有一個 string-width-cjs 的 node_modules 目錄條目,但包含 string-width@^4.2.0 的內容以及鎖定檔案 (package-lock.json) 中的類似行為。

套件別名是 npm 套件管理器的功能,可以合法地用於此處暗示的情況(以幫助 ESM 與 CJS 支援)。

話雖如此,包別名可能會被濫用。在一篇可追溯到 2021 年的文章和安全披露中,Snyk 大使 Nishant Jain 演示瞭如何欺騙官方 npmjs 註冊表,基於包別名錯誤地提供依賴信息,作為依賴混淆和供應鏈安全問題的一部分。

這個拉取請求確實是良性的,並且不存在供應鏈攻擊的風險。 然而,Sébastien 對這樣的包名產生了懷疑,並發現還有更多值得擔心的事情。

在 npm 鎖定檔案中尋找有關惡意模組的可疑行為

當Sébastien檢查拉取請求時,他運行了一個名為lockfile-lint的工具,該工具有助於驗證鎖定文件,例如package-lock.json或yarn.lock,以確保它們不會被篡改以注入惡意包,而不是原始的npm 包。

執行該工具顯示以下警告:

npx lockfile-lint --path package-lock.json --allowed-hosts yarn npm --validate-https --validate-package-names

detected resolved URL for package with a different name: string-width-cjs
    expected: string-width-cjs
    actual: string-width

detected resolved URL for package with a different name: strip-ansi-cjs
    expected: strip-ansi-cjs
    actual: strip-ansi

detected resolved URL for package with a different name: wrap-ansi-cjs
    expected: wrap-ansi-cjs
    actual: wrap-ansi

 ✖ Error: security issues detected!

免責聲明:lockfile-lint 是我在2019 年開發的一個工具,該工具披露了鎖定文件的安全問題: 為什麼npm lockfiles 可能成為注入惡意模組的安全盲點.

高度警覺:npm 上的流行軟體包看起來很相似

鑑於上述 lockfile-lint 結果,Sébastien 在 npm 上查找了這些包名稱,並驚訝地發現它們確實存在於公共 npm 註冊表中:

  • https://www.npmjs.com/package/string-width-cjs
  • https://www.npmjs.com/package/strip-ansi-cjs
  • https://www.npmjs.com/package/wrap-ansi-cjs

Sébastien 指出,這些套件名稱不僅存在於 npm 上,而且它們還具有引起關注的指標:

  • 這些套件不綁定到任何公共原始碼儲存庫
  • 如果檢查其內容,這些套件中沒有任何實際程式碼。
  • 發布這些包的作者身份是匿名的,與任何個人或可識別資訊無關。

查看 npm 套件 strip-ansi-cjs,沒有自述文件,也沒有與該套件關聯的原始程式碼儲存庫,但有許多合法且流行的套件引用了相同的行為。

事實上,對於這個特定的軟體包,有許多依賴項(依賴於該軟體包的其他軟體包)形式的流行信號- 確切地說,有529 個依賴項,並且每週下載量也在不斷增加,總計7,274 次寫作時。

Suspicious Maintainer Unveils Threads of npm Supply Chain Attack
查看 strip-ansi-cjs 的程式碼,它顯示該套件中只有一個文件,即套件清單 package.json 文件。

那麼,為什麼一個不做任何事情的包會獲得如此多的下載,以及為什麼還有那麼多其他包依賴它?

Suspicious Maintainer Unveils Threads of npm Supply Chain Attack

讓我們繼續考察這些npm包的作者。

這三個軟體包皆屬於himanshutester002,它們的軟體包皆於去年發布,並帶有程式化版本號。有些很有趣:

  • isaacs-cliui npm package - potentially a typosquatting attempt on Isaac’s own fork of the cliui project and the legitimate npm package under their namespace: @isaacs/cliui.
  • azure-sdk-for-net npm package - potentially an attempt at dependency confusion campaign to attack private packages of the same name.
  • link-deep npm package - squatting on a popular capability related to utility packages such as lodash and others

Suspicious Maintainer Unveils Threads of npm Supply Chain Attack
You can also note that the user himanshutester002 has no identifiable information on this user profile page on npmjs.

We previously noted that the strip-ansi-cjs npm package has over 500 other packages that use it, therefore, potentially a positive indicator for popularity. Let’s look at them:

Suspicious Maintainer Unveils Threads of npm Supply Chain Attack
If you give it a glance, this might transfer some sort of legitimacy with this list, but is it?
For example, names like clazz-transformer or react-native-multiply or maybe gh-monoproject-cli seem legitimate, but are they?

Here is the react-native-multiply npm package page:

Suspicious Maintainer Unveils Threads of npm Supply Chain Attack
This package has virtually no downloads and its author is also an anonymous npm user with no identifiable information. The source URL repository this package redirects to is https://github[.]com/hasandader/react-native-multiply which doesn’t exist and the GitHub user profile looks very suspicious and lacks practical activity.

The npm package contents might seem like there’s some actual source code in there, but in reality, it looks like a generated code sample for a “hello world” application prototype.

Suspicious Maintainer Unveils Threads of npm Supply Chain Attack
You also have to wonder, if this package is just a multiplication library, then why does it need 776 dependencies to do the following:

import { multiply } from 'react-native-multiply';
const result = await multiply(3, 7);

While some may mock JavaScript for its abuse of dependencies, contributing to an astronomical tree of nested packages, it doesn’t make any sense for a project to declare 776 direct (top-level) dependencies.

Among all of these dependencies, are the 3 suspicious npm packages that our story began with: string-width-cjs, strip-ansi-cjs, and wrap-ansi-cjs:

Suspicious Maintainer Unveils Threads of npm Supply Chain Attack
We mentioned that one of the strip-ansi-cjs dependencies was named clazz-transformer. Let’s look at it:

Suspicious Maintainer Unveils Threads of npm Supply Chain Attack
Let’s explain what is happening here:

  • The npm package name is clazz-transformer, yet the package has a title of class-transformer at the top of the README’s package page here which was purposely done.
  • Similarly, the source code repository is https://github[.]com/typestack/class-transformer which might be a legitimate repository or a fake one, but it is not associating itself at all with the wording “clazz”.

The associated repository’s typstack/class-transformer on GitHub has the package.json file as  follows:

Suspicious Maintainer Unveils Threads of npm Supply Chain Attack
Looking at the package.json file on GitHub shows no declaration of dependencies, yet if we inspect the source code of the actual package on npmjs we see the 437 dependencies that this clazz-transformer is packaged with. Again, very conveniently bundling the 3 suspicious *-cjs packages:

Suspicious Maintainer Unveils Threads of npm Supply Chain Attack

Further thoughts on suspicious npm package findings

Before we draw further conclusions, it is important to mention a few of the traits of the npm packages we observed above:

  • React Native 套件似乎源自於 create-react-native-library 鷹架工具,該工具還具有預設的乘法函數範例,作為為新專案生成的庫存原始程式碼的一部分。
  • 套件具有可以從 Next.js 14 入門樣板派生的目錄和檔案結構以及依賴項,例如使用 npx create-next-app@14 建立的套件。

我們在 Sonatype 的同行之前已經發現過類似的軟體包淹沒開源註冊表的案例。在這些情況下,開發人員的最終目標是用 Tea 代幣獎勵自己,這是一個用於透過開源軟體貨幣化的 Web3 平台。

在上述包中找到一些 tea.yaml 文件進一步支持了這一論點,即活動的部分目的是透過濫用 Tea 來挖掘 Tea 代幣。

今年早些時候,2024 年 4 月 14 日,一位茶論壇用戶發表了一條評論,進一步支持了對茶濫用的擔憂:

Suspicious Maintainer Unveils Threads of npm Supply Chain Attack
在給出結論之前,我要真誠地感謝 Sébastien Lorber 謹慎的維護者心態,並幫助揭示潛在的 npm 供應鏈攻擊的線索。

string-width-cjs 發生了什麼事?

此時,我非常有信心可以繼續在其餘據稱依賴於 string-width-cjs 的包中找出漏洞,以找到非常可疑的真實合法性指標。

我的假設是,所有這些依賴套件和下載量增加的唯一目的是為3 個*-cjs 套件創建虛假合法性,以便在適當的時候,在適當的受害者參與的情況下,這些假套件將安裝,然後使用新的惡意版本。

為了幫助您在使用開源軟體時保持安全,我強烈建議採用安全實踐,特別是以下後續教育資源:

  • 為什麼 npm 鎖定檔案可能成為注入惡意模組的安全盲點
  • 10 個 npm 安全最佳實務
  • NPM 安全:防止供應鏈攻擊

我們是否在他們的不法行為中發現了供應鏈安全活動,或者這一切都與金錢軌跡有關,因此可以歸因於垃圾郵件和濫用 npm 和 GitHub 等公共註冊中心來挖掘 Tea 代幣?

無論如何,請保持警覺。

以上是可疑的維護者揭露了 npm 供應鏈攻擊的線索的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn