[小ネタ] EC2 Image Builderでコンポーネントを実行するユーザー (Windows編) #reinvent

2019.12.24

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

しばたです。 先日弊社内藤により以下の記事が公開されました。

[小ネタ]EC2 Image Builderでコンポーネントを実行するユーザを調べてみた

こちらの記事ではEC2 Image BuilderでLinuxイメージを作る際のコンポーネントをビルドするユーザーについて試した内容が記載されています。 ではWindowsイメージを作る場合はどうなのか?というのをこの記事で試してみました。

TL;DR

はじめにネタバレというか結論から書いておきます。

先の記事や私のre:Growhthでの発表でも触れていますが、EC2 Image BuilderにおいてコンポーネントのビルドはSSM Agentによって行われます。 このためLinuxインスタンスにおいてはrootユーザーとなり、Windowsにおいてはローカルシステムユーザー(NT AUTHORITY\System)となります。

こちらはSSM Agentのサービスを見てみるとすぐわかります。

調べてみた

せっかくなのでLinuxの場合と似た様な感じで、以下のカスタムコンポーネントを作って調べてみました。

name: builder-check-windows
schemaVersion: 1.0
phases:
  - name: build
    steps:
      - name: check-whoami
        action: ExecutePowerShell
        inputs:
          commands:
            - |
              $logPath = Join-Path $env:TEMP "check-whoami.log"
              "pwd: $pwd" | Out-File $logPath
              $CurrentUser = [Security.Principal.WindowsIdentity]::GetCurrent()
              $isElevated = (New-Object Security.Principal.WindowsPrincipal $CurrentUser).IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)
              "Elevated: $isElevated" | Out-File $logPath -Append
              whoami /user /priv | Out-File $logPath -Append

Windowsにもwhoamiコマンドは存在しており現在のユーザーに対する多くの情報を提供してくれます。 あとは初期状態でのカレントディレクトリと昇格済みかどうかを調べています。

実行結果

Windows Server 2019の適当なイメージを作り、出力されたファイルは以下の様になりました。 (ローカルシステムユーザーの場合$env:TEMPC:\Windows\Tempになります)

pwd: C:\
Elevated: True

USER INFORMATION
----------------

User Name SID
=================== ========
nt authority\system S-1-5-18

PRIVILEGES INFORMATION
----------------------

Privilege Name Description State
========================================= ================================================================== ========
SeAssignPrimaryTokenPrivilege Replace a process level token Disabled
SeLockMemoryPrivilege Lock pages in memory Enabled
SeIncreaseQuotaPrivilege Adjust memory quotas for a process Disabled
SeTcbPrivilege Act as part of the operating system Enabled
SeSecurityPrivilege Manage auditing and security log Disabled
SeTakeOwnershipPrivilege Take ownership of files or other objects Disabled
SeLoadDriverPrivilege Load and unload device drivers Disabled
SeSystemProfilePrivilege Profile system performance Enabled
SeSystemtimePrivilege Change the system time Disabled
SeProfileSingleProcessPrivilege Profile single process Enabled
SeIncreaseBasePriorityPrivilege Increase scheduling priority Enabled
SeCreatePagefilePrivilege Create a pagefile Enabled
SeCreatePermanentPrivilege Create permanent shared objects Enabled
SeBackupPrivilege Back up files and directories Disabled
SeRestorePrivilege Restore files and directories Disabled
SeShutdownPrivilege Shut down the system Disabled
SeDebugPrivilege Debug programs Enabled
SeAuditPrivilege Generate security audits Enabled
SeSystemEnvironmentPrivilege Modify firmware environment values Disabled
SeChangeNotifyPrivilege Bypass traverse checking Enabled
SeUndockPrivilege Remove computer from docking station Disabled
SeManageVolumePrivilege Perform volume maintenance tasks Disabled
SeImpersonatePrivilege Impersonate a client after authentication Enabled
SeCreateGlobalPrivilege Create global objects Enabled
SeIncreaseWorkingSetPrivilege Increase a process working set Enabled
SeTimeZonePrivilege Change the time zone Enabled
SeCreateSymbolicLinkPrivilege Create symbolic links Enabled
SeDelegateSessionUserImpersonatePrivilege Obtain an impersonation token for another user in the same session Enabled

実行結果からもちゃんとローカルシステムユーザー(NT AUTHORITY\System)で実行されていることがわかります。 ローカルシステムユーザーはAdministratorsグループと同等の権限を持ち、この結果からも昇格済み・特権モリモリであることがわかりますね。 あと初期ディレクトリはC:\の様でした。

最後に

小ネタですがWindows向けコンポーネントを作る方の役に立てれば幸いです。