Upgrade to Pro — share decks privately, control downloads, hide ads and more …

オフェンシブセキュリティの体験談 / My Offensive Security Experience

オフェンシブセキュリティの体験談 / My Offensive Security Experience

2024年6月25日のMWSプレミーティングで発表した「オフェンシブセキュリティの体験談」の講演資料です https://www.iwsec.org/mws/2024/mws20240625.html

NTT Communications

June 30, 2024
Tweet

More Decks by NTT Communications

Other Decks in Technology

Transcript

  1. © NTT Communications Corporation All Rights Reserved. [MWSプレミーティング2024] オフェンシブセキュリティ の体験談

    NTTコミュニケーションズ株式会社 イノベーションセンター 久保 佑介
  2. © NTT Communications Corporation All Rights Reserved. 2 $ whoami

    Name ︓ Yusuke Kubo Company ︓ NTT Communications Department ︓ Innovation Center (R&D) Role ︓ RedTeam - TeamV Product Manager Additional Info ︓ - Ex-MWS CUP Committee member (2021, 2022) - Ex-MWS CUP Participant(GOTO LOVE, UN頼み) - NTT Group Certified Security Principal - MITRE ATT&CK Contributor(T1562.009 Safe Mode Boot) - Red Team Summit 2023 Speaker - BSides Las Vegas 2023 Speaker - AppSec Village, Cloud Vilage@DEFCON31 Speaker 今⽇の体験談で話すネタ
  3. © NTT Communications Corporation All Rights Reserved. 3 今⽇のトピック 業務中に

    CI/CDサービスを悪⽤するTTP を思いついて調査した体験談
  4. © NTT Communications Corporation All Rights Reserved. Dark Playground of

    CI/CD: Attack Delivery by GitHub Actions 2023年に海外の産業系カンファレンスで発表をした を⽇本語版で講演しつつ、 「その時の私の気持ち」を⾃問⾃答する発表 ※ 2023年当時の仕様に基づいた内容です
  5. © NTT Communications Corporation All Rights Reserved. 7 CI/CD l

    CI/CD = Continuous Integration / Continuous Delivery and/or Deployment l ソフトウェア開発プロセスを⾃動化し効率するための仕組み Developer SCM (e.g., GitHub) Build Test Deploy Production CI/CD Pipeline
  6. © NTT Communications Corporation All Rights Reserved. 8 CI/CD Security

    Risks https://owasp.org/www-project-top-10-ci-cd-security-risks/ l Top 10 CI/CD Security Risks CI/CD Pipelineで使⽤する トークンなどが窃取 されるリスク CI/CD Pipelineへ 悪意のあるコードが注⼊ されるリスク
  7. © NTT Communications Corporation All Rights Reserved. 10 GitHub Actions

    l GitHub Incが提供するCI/CDプラットフォーム l GitHubで管理しているコードを⾃動でビルド・テスト・デプロイすることができる https://docs.github.com/ja/actions/learn-github-actions/understanding-github-actions The Component of GitHub Actions ワークフローを実⾏するサーバ ワークフローのトリガー (e.g., Pull Requestの作成) ランナーで実⾏される⼀連のステップ =ワークフロー GitHub Actions⽤の カスタムアプリケーション
  8. © NTT Communications Corporation All Rights Reserved. 11 Runner Raising

    an Event (e.g., create PR) Workflow の実⾏ User Runner GitHubが管理している リソース エージェント (Runner Application) を実⾏ Runner Raising an Event (e.g., create PR) User Workflow の実⾏ ユーザが管理している リソース Self-Hosted Runner GitHub-Hosted Runner l 2種類のランナータイプ︓GitHub-Hosted Runner or Self-Hosted Runner l OSはWindows / Linux / macOSから選択可能 今回はRedTeamで対象とすることが多いWindowsにフォーカス
  9. © NTT Communications Corporation All Rights Reserved. 12 Action l

    GitHub Actions⽤のカスタムアプリケーション l 複雑で頻繁に繰り返されるようなタスクを簡単に再利⽤することができる https://docs.github.com/ja/actions/learn-github-actions/understanding-github-actions https://github.com/marketplace/actions/checkout アクションを使⽤するワークフローの例 Checkout Action
  10. © NTT Communications Corporation All Rights Reserved. 13 Action Docker

    Container、JavaScript、Composite Actionの3種類が⽤意されている ポイント① 種類 リポジトリ内、またはGitHub Marketplaceで公開することができる コンテナでアクションを パッケージング Nodejsで JavaScriptを実⾏ 複数のアクションを 1つのアクションで内包 ポイント② 公開 l ユーザが独⾃のアクションを作成しワークフローで使⽤できる = Custom Action l 作成したアクションをコミュニティで公開できる リポジトリ プライベートリポジトリ の場合は⾮公開
  11. © NTT Communications Corporation All Rights Reserved. 15 TTPs -

    GitHub Actions C2 - Malicious Custom Action
  12. © NTT Communications Corporation All Rights Reserved. 16 TTPs -

    GitHub Actions C2 - Malicious Custom Action
  13. © NTT Communications Corporation All Rights Reserved. 17 GitHub Actions

    C2 Attacker Command and Control Pushing malicious Workflow for C2 Attackerʻs own repository Self-Hosted Runner Application Victim l GitHubが提供するRunner ApplicationをC2のエージェントとして悪⽤する攻撃 l CI/CDサービスを介してコマンドの実⾏、実⾏結果の収集を⾏う l CI/CDサービスの機能を使⽤するためC2サーバやエージェントを⽤意する必要がない
  14. © NTT Communications Corporation All Rights Reserved. 18 GitHub Actions

    C2 Workflow Example hostlabel 任意のコマンド実⾏を 定義しているステップ OSのシェルを利⽤したコマンド実⾏を GitHub Actionsがデフォルトでサポート GitHub APIを使⽤したWebhook Event によるトリガー コマンドを実⾏する ターゲットをラベルで指定
  15. © NTT Communications Corporation All Rights Reserved. 21 Detection: GitHub

    Actions C2 l 正規のプロセスと通信先であるため、C2そのものを検知することは困難 l プロセス︓Runner.Worker.exe, Runner.Listener.exe l 通信︓github.com, api.github.com, *.actions.githubusercontent.com… l C2による悪性な振る舞いを検知することは可能 l マルウェアのダウンロード & 実⾏ l 認証情報へのアクセス … l GitHub Actionsが利⽤するプロセスと通信先がアラートのIoCと⼀致した場合に、 TTPとしてGitHub Actions C2が使⽤されていることを疑うことが重要 = 正規のプロセス、通信先であっても信⽤しない(正常性バイアスの回避)
  16. © NTT Communications Corporation All Rights Reserved. 22 その他のSaaSを悪⽤するC2との⽐較 l

    SaaSを悪⽤するC2⼿法は既に多く存在する l SaaS C2のためのツールやフレームワークがGitHubで公開されている l SaaSのAPIを利⽤する独⾃のエージェントを開発していることが⼀般的 ➡GitHub Actions C2の場合はエージェントの開発が不要 https://github.com/WithSecureLabs/C3
  17. © NTT Communications Corporation All Rights Reserved. 23 [体験談1] Q.

    GitHub Actions C2を思いついたきっかけは︖ プロダクト開発をする中でSelf-Hosted Runnerを使うことがあり、 「これC2として使えるな」と思った セキュリティエンジニア C2フレームワーク プロダクトエンジニア CI/CDサービス プロダクト開発の業務をしているから CI/CDサービスに触れる機会があった RedTeamの業務をしているから、 SaaSを悪⽤するC2の発想があった RedTeamの業務で使⽤する C2フレームワークの1つとしてGitHub Actions C2を開発 セキュリティ以外の業務からTTPを思いつくこともある
  18. © NTT Communications Corporation All Rights Reserved. 24 GitHub Actions

    C2の課題 GitHub Actionsでは基本的なOSコマンドを実⾏することしかできない = GitHub Actions C2から任意のTTPを実⾏するのは難しい︖ NO Malicious Custom Action ActionによるC2の機能拡張
  19. © NTT Communications Corporation All Rights Reserved. 25 TTPs -

    GitHub Actions C2 - Malicious Custom Action
  20. © NTT Communications Corporation All Rights Reserved. 26 (再掲)Custom Action

    Docker Container、JavaScript、Composite Actionの3種類が⽤意されている ポイント① 種類 リポジトリ内、またはGitHub Marketplaceで共有し利⽤することができる コンテナでアクションを パッケージング Nodejsで JavaScriptを実⾏ 複数のアクションを 1つのアクションで内包 ポイント② 共有 l 独⾃のアクションを作成しワークフローで使⽤できる l 作成したアクションをコミュニティで共有できる リポジトリ JavaScript Actionに着⽬
  21. © NTT Communications Corporation All Rights Reserved. 27 JavaScript Action

    l アクションで実⾏したい内容をJavaScriptで実装できる l 実装するJavaScriptコードは純粋なJavaScriptである必要がある https://docs.github.com/ja/actions/creating-actions/creating-a-javascript-action アクションのメタデータ アクションの実態となるJavaScriptコードの例
  22. © NTT Communications Corporation All Rights Reserved. 28 Q. JScriptは実⾏できる︖

    A. (ドキュメントに書いてある内容的には)できない • JScriptはMicrosoft独⾃の拡張であり、”純粋”なJavaScriptではない • 互換性はあるもののJScriptとJavaScriptは異なる⾔語 • e.g., • JScriptはnpmモジュールを使⽤できない • JavaScriptはCOM(Component Object Model)を使⽤できない Windows Script Host (WSH)で実⾏されるJScriptは、 Windowsのシステムリソースやコンポーネントにアクセスできるため攻撃の幅が広がる Q2. 本当にJavaScript ActionでJScriptは実⾏できないのか︖
  23. © NTT Communications Corporation All Rights Reserved. 29 動的解析で⾒るGitHub Actions

    Runner.Listener.exe がプロセスとして常駐する Azure DevOps Serviceに紐づく ドメインと定期的に通信をする Workflowを実⾏する Runner.Worker.exeを ⼦プロセスとして⽴ち上げる Runner.Worker.exeは ⼀連のワークフローの実⾏が終わると ⾃⾝をプロセスキルする イベントの確認などを ⽬的とした通信 l Runner.Listener.exeの動き
  24. © NTT Communications Corporation All Rights Reserved. 30 動的解析で⾒るGitHub Actions

    api.github.com との通信 codeload.github.com との通信 Workflowの各ステップを ⼦プロセスとして実⾏ ステップに関連する⼦プロセスの起動 l Runner.Worker.exeの動き
  25. © NTT Communications Corporation All Rights Reserved. 31 動的解析で⾒るGitHub Actions

    l JavaScript Actionの動き l Runner.Worker.exeがJavaScriptのランタイムとなるnode.exeを実⾏する l Entry Pointとして定義されたJSファイルがnode.exeに引数として渡される l node.exeはRunner Applicationファイル(zip)に内包されている l ”externals¥node12¥bin¥node.exe” and ”externals¥node16¥bin¥node.exe”に展開される Entry Point アクションメタデータ https://docs.github.com/en/actions /creating-actions/creating-a-javascript-action Runtime Runtime Runner Application Package [binary] node.exe [argument] index.js Runner.Worker.exe
  26. © NTT Communications Corporation All Rights Reserved. 32 Malicious JScript

    Custom Action wscript.exe node.exe l node.exeのBinary Hijacking + JScriptによる任意のTTP実⾏ l node.exeをwscript.exeでHijackすることでJScriptを実⾏可能にする l 新たなバイナリを必要とせず (LOLBIN)、JavaScript Actionの実⾏フローも変えない [index.js] WScript.Echo(“Hello, World”); [.github/actions/mal-action] Attacker JavaScript Action [.github/workflows/mal.yaml] step1:wscript.exeのコピー step2:mal-actionの実⾏ GitHub Actions Workflow Victim ワークフローの実⾏ Step1︓Binary Hijacking wscript.exeをexternals¥node12¥bin¥node.exeに コピーする Step2︓カスタムアクションの実⾏ index.jsがnode.exe(wscript.exe)から実⾏される index.js node.exe (wscript.exe) エンドポイントの動き 中⾝はJScript
  27. © NTT Communications Corporation All Rights Reserved. 33 Composite Action

    l ワークフロー間で共通して使われるステップを再利⽤可能にするための仕組み l 複数のステップを1つのアクションとして実装することが可能 l (Composite Actionを組み合わせる理由は後述) [index.js] WScript.Echo(“Hello, World”); [.github/actions/mal-action] [.github/workflows/mal.yaml] step1:mal-compsite-actionの実⾏ GitHub Actions Workflow [.github/actions/mal-composite-action] step1:wscript.exeのコピー step2:mal-actionの実⾏ Without Composite Action With Composite Action [index.js] WScript.Echo(“Hello, World”); [.github/actions/mal-action] JavaScript Action [.github/workflows/mal.yaml] step1:wscript.exeのコピー step2:mal-actionの実⾏ GitHub Actions Workflow JavaScript Action Composite Action
  28. © NTT Communications Corporation All Rights Reserved. 34 Malicious JScript

    Composite Action Attacker Victim [index.js] WScript.Echo(“Hello, World”); [.github/actions/mal-action] [.github/workflows/mal.yaml] step1:mal-compsite-actionの実⾏ GitHub Actions Workflow [.github/actions/mal-composite-action] step1:wscript.exeのコピー step2:mal-actionの実⾏ Composite Action ワークフローの実⾏ JavaScript Action
  29. © NTT Communications Corporation All Rights Reserved. 35 Malicious JScript

    Composite Action PoC .github/workflows/mal.yaml .github/actions/mal-composite-action/action.yaml .github/actions/mal-action/action.yaml .github/actions/mal-action/index.js 1. GitHub Actions Workflow 2. Composite Action 3. JavaScript Action 4. Execute JScript
  30. © NTT Communications Corporation All Rights Reserved. 36 Detection: Malicious

    JScript Composite Action l Binary Hijacking(Masquerading)はEDRに検知される l JSファイルはホストに展開されるためAVに検知される このテクニックにおいて 変更できない振る舞いであり 検知ポイントとして有効 悪性スクリプトの実装次第でBypassされるため、 検知ポイントとして有効とは⾔えない
  31. © NTT Communications Corporation All Rights Reserved. 37 JScriptのAMSI Bypass

    l JScriptの⼀部を変更するだけでAMSI Scan BufferをBypassできる l 今回はGadgetToJScriptというツールによりProcess Injectionを⾏うJSファイルを⽣成 l ツール特有の⼀部の特徴的な⽂字列を変更するだけで検知されなくなる https://twitter.com/an0n_r0/status/1633620693504913408 @an0n_r0によってtwitterで報告された 変更前(検知される)と変更後(検知されない)のdiff
  32. © NTT Communications Corporation All Rights Reserved. 38 [体験談2] Q.

    Malicious Custom Actionを思いついたきっかけは︖ JavaScriptがどういうフローで実⾏されているのか疑問に思った. 調べたがUndocumentedな内容なため⾃分で解析をした. Malicious Custom Actionを発⾒(=結果論) Red Team JScriptのコーディング Blue Team 動的解析 JScriptを⽤いた攻撃の経験があったから JavaScript Actionに関⼼を持った マルウェア解析に関して 基本的なスキルセットは有していた とりあえず⼿を動かしてみる
  33. © NTT Communications Corporation All Rights Reserved. 39 [余談] GitHub

    Actionsはコードが公開されている わざわざ動的解析をする必要はなかった(かもしれない) https://github.com/actions/runner
  34. © NTT Communications Corporation All Rights Reserved. 40 Malicious JScript

    Composite Actionの課題 EDRのような振る舞いを検知する製品は検知してアラートをあげる =セキュリティソリューションが導⼊されている環境では 任意のTTPを実⾏できない︖ NO Malicious JavaScript Custom Action JavaScriptで任意のTTPを実⾏する
  35. © NTT Communications Corporation All Rights Reserved. 41 Malicious JavaScript

    Custom Action l JavaScriptとC/C++ライブラリ間のインタフェースを提供するNode.js C++ addons l Node.js C++ addonsによりJavaScriptからC/C++コードを実⾏できる l C/C++コードであれば任意のTTPがほぼ実装できる = JavaScriptから任意のTTPを実⾏できる l 攻撃に悪⽤可能なMemoryjsというライブラリが既に存在 https://github.com/Rob--/memoryjs/ OpenProcess by memoryjs スクリプトの実⾏結果 DLL InjectionがC++で実装されている 1. VirtualAlloc 2. WriteProcessMemory 3. LoadLibrary 4. CreateRemoteThread injectDLL@memoryjs
  36. © NTT Communications Corporation All Rights Reserved. 42 Malicious JavaScript

    Custom Action PoC .github/workflows/mal.yaml .github/actions/mal-action/action.yaml .github/actions/mal-action/index.js Fils of .github/actions/mal-action node modules (include “memoryjs”) Simple DLL to launch calculator 1. GitHub Actions Workflow 2. JavaScript Action 3. Execute JavaScript 攻撃に使⽤するコードは Custom Actionに設置
  37. © NTT Communications Corporation All Rights Reserved. 43 Detection: Malicious

    JavaScript Custom Action l node.exeのBinary Hijackingを必要としない l node.exeで実⾏するJavaScriptはAMSIのスキャン対象はならない l AMSIをサポートしていないスクリプトエンジンを使うことは AMSI Bypassの有効な⼿段であると⾔われている ➡ JavaScript(C++ addons)で実装された攻撃の検知勝負 l 攻撃の振る舞いを検知できるか︖ l JSファイルやC++ addonsの悪性コードを検知できるか︖ ”AmsiScanBuffer”をOracleとして実験 powershellはアラートがあがる node.jsはアラートがあがらない node.exeは amsi.dllをロードしない
  38. © NTT Communications Corporation All Rights Reserved. 44 Advanced Technique:

    Persistence l Runner Applicationはワークフロー終了時に関連するプロセスをすべてKillする l プロセス環境変数(RUNNER_TRACKING_ID)で関連プロセスをトラッキングする = 環境変数を上書きすることでトラッキングを無効化できる(Persistence) プロセス環境変数の上書き(JScript) Persistent C2 Process Runner Application Earlybird Injection + PPID Spoofing “GITHUB_” や“RUNNER_”から 始まる特徴的なプロセス環境変数を持つ 永続化に成功した様⼦ Runner.Worker.exeは起動していない = ワークフローは終了済 RUNNER_TRACKING_IDが0になっている = Persistenceされたプロセスであることを⽰す
  39. © NTT Communications Corporation All Rights Reserved. 45 Malicious Custom

    Actionの公開の脅威 l 攻撃者もGitHub MarketplaceにCustom Actionを簡単に公開できる = 正規のCustom Actionを装って悪意のあるActionを公開する可能性がある e.g., Formatterとして動作するように⾒えるがバックドアが仕込まれている l 公開された悪意のあるActionをユーザーがワークフローで使⽤するシナリオが起こり得る Runner ④Action Xの取得 +実⾏ User ②ワークフローに Action Xを設定 ③ワークフロー の実⾏ Attacker ①正規のActionに偽装して Action Xを公開 (Malicious Custom Action)
  40. © NTT Communications Corporation All Rights Reserved. 46 (再掲)Composite Action

    l Composite Actionを使⽤することで1つのカスタムアクションとして攻撃を抽象化できる l ワークフロー上で他のステップを必要とせず1つのステップで攻撃が完結する = Malicious JScript Composite Actionが現実的なシナリオになる [index.js] WScript.Echo(“Hello, World”); mal-action@Marketplace [.github/workflows/workflow.yaml] step1:mal-compsite-actionの実⾏ GitHub Actions Workflow mal-composite-action@Marketplace step1:wscript.exeのコピー step2:mal-actionの実⾏ Without Composite Action With Composite Action [index.js] WScript.Echo(“Hello, World”); mal-action@Marketplace JavaScript Action [.github/workflows/workflow.yaml] step1:wscript.exeのコピー step2:mal-actionの実⾏ GitHub Actions Workflow JavaScript Action Composite Action このステップをユーザ⾃らが 設定することはない MarketPlaceに公開された 偽のアクションをユーザが使⽤ してしまうだけで攻撃が成⽴
  41. © NTT Communications Corporation All Rights Reserved. 47 2023/5/2 「問題ない」

    2023/5/2 情報を公開しても 問題ないか確認 2023/4/4 テクニカルレポートを送付 Responsible Disclosure 2023/3/25 社内メンバーから GitHubの知り合いにメール 2023/3/27 Bug Bountyに誘導 2023/3/27 攻撃⼿法の概要と 脆弱性ではないことを伝える 2023/4/12 GitHub Actionsの セキュリティ担当を紹介 2023/4/14 セキュティ担当者に テクニカルレポートを送付 2023/4/22 「コンテンツをレビューして 返信する」 2023/4/27 「GitHub Actionsのセキュリティ に関するドキュメントでカバー されている」 GitHub NTT Com
  42. © NTT Communications Corporation All Rights Reserved. 48 [体験談3] Q.

    Responsible Disclosureを経験してみて︖ 脆弱性ではないため進め⽅が難しい (最近の悩み) • 正規の機能を悪⽤する攻撃をどう扱うべきなのか • Bug Bounty Programに誘導されがちだけどBugではない… • 「使い⽅の問題」でありサービス事業者としても報告されて困るような… • 正規の機能を悪⽤する攻撃をサービスとしてどう対策すべきなのか • 事業者による根本的な対策の情報がない(⾃分調べ) • そもそも根本的な対策ができるのか…
  43. © NTT Communications Corporation All Rights Reserved. 49 まとめ 1.

    Continuous Attack Integration & Continuous Attack Delivery • リポジトリにcommitした攻撃がCI/CDの仕組みで⾃動的にデリバリされる • 実装した攻撃を⾃動でC2の機能として取り込むことができる 2. Legitimate Process and Traffic • 通信元プロセス(エージェント)はGitHubが作成した正規のアプリケーション • 通信先はGitHubが保有するドメインおよびIPアドレス 3. Resource-Less • C2サーバの構築、C2エージェントの作成、(基本的に)バイナリの作成が不要 4. Easy Weaponization • GitHubアカウント、リポジトリの作成、Runner Applicationダウンロードで準備完了 GitHub Actions C2 + Malicious Custom Actionという⼿法を発⾒ Features
  44. © NTT Communications Corporation All Rights Reserved. 50 Takeaways l

    By utilizing the features offered by CI/CD, such as command and script execution, it is possible to launch an attack within the legitimate CI/CD process. l Malicious code may infiltrate the CI/CD pipeline without notice through a publicly available malicious plugin (Malicious Custom Action). l There is a risk of enabling C2 through the legitimate features offered by CI/CD which add user-owned hosts for executing tasks (GitHub Actions C2).
  45. © NTT Communications Corporation All Rights Reserved. 51 [体験談4] Q.

    苦労したことは︖ 現在進⾏形でどうやって研究にするか苦労している(基本的にいつも) 業務や案件をきっかけにテーマを発⾒する 業務や案件を前提とするため特定のTTPにフォーカスする e.g.) GitHub Actions C2 + Malicious Custom Action, Safe Mode Boot, etc. どうやって研究にするか… ←今ここ ※研究が⽬的になってはいるが研究成果として世の中に価値を提供したい気持ちがある