0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

PowerShell で sudo モドキを作ってみた[4. 権限設定]

0
Last updated at Posted at 2026-01-21

注意

 自己責任でお願いします。これら記事の内容を実行して何か不都合を被ったとしても筆者は責任を負いません。
 性質上セキュリティホールを自分で開けているようなものなので、実装は実験目的かつ仮想環境を推奨します。また当たり前ですが実装自体には管理者権限が必要です。また自分の所有する端末のみで行ってください。
 もし sudo を使いたいなら、Windows 標準 か Grignoli 氏の gsudo を使えばよいと思います。

構成

目次
1. 変数の宣言と代入
2. 接続設定
3. 構成ファイル作成・登録
4. 権限設定
5. 証明書作成
6. 証明書紐付け
7. 実行・関数作成
8. 後始末

説明

 エンドポイント・役割構成ファイルのアクセス権を再構築し、管理者権限の無いユーザーからはファイルの変更及び読み取りを制限しています。選択したユーザーには実行アクセス権のみを、Administrators グループに所属しているユーザーには読み取りアクセス権のみを与えています。また管理者権限があれば削除も許可されます(ディレクトリに「サブフォルダーとファイルの削除」権限があるため)。

内容

使用する変数:
$RoleConfig $PhrasePath $Enrollment $UserSid $SddlBkName $BackupSddl #$TargetUser

$RoleName   = "Mock"

$ModDirName = "JEA"
$EntirePath = ($Env:PSModulePath -split ";" -like "$Env:ProgramFiles*")[-1]
$ModulePath = Join-Path $EntirePath $ModDirName
$RoleInPath = Join-Path $ModulePath RoleCapabilities
$RoleConfig = Join-Path $RoleInPath "${RoleName}.psrc"
$PhrasePath = Join-Path $RoleInPath "phrase.uid"

$Enrollment = $RoleName + "Endpoint"

$SelectUser = [Security.Principal.WindowsIdentity]::GetCurrent().Name
$UserObject = [Security.Principal.NTAccount]::new($SelectUser)
$UserSid    = $UserObject.Translate([Security.Principal.SecurityIdentifier]).Value

$SddlBkName = "${Enrollment}." + (Get-Date -Format "yyyy-MM-dd_HH-mm-ss") + ".sddl"
$BackupSddl = Join-Path $Env:UserProfile $SddlBkName

# $TargetUser = Get-Credential

↓すべて管理者権限で実行します↓

SDDL の厳格化
# 元にする ACE 群・SDDL を取得
$ExistedSddl = (Get-PSSessionConfiguration $Enrollment).SecurityDescriptorSddl
$RawSdObject = (ConvertFrom-SddlString $ExistedSddl).RawDescriptor

# SDDL 構成内容(# $RawSdObject.GetSddlForm("All"))
$Owner = $RawSdObject.GetSddlForm("Owner")
$Group = $RawSdObject.GetSddlForm("Group")
$DaclAces = @(
  "(A;;GR;;;BA)",  # Builtin Administrators(Read Access)
  "(A;;GX;;;$UserSid)"  # 実行権限付与(Invoke 相当)
)
$SACL = $RawSdObject.GetSddlForm("Audit")

# SDDL 作成
$DefinitionSddl = $Owner + $Group + "D:P$($DaclAces -join '')" + $SACL

# 検証
try {
  if ([string]::IsNullOrWhiteSpace($DefinitionSddl)) { throw }
  [void][Security.AccessControl.RawSecurityDescriptor]::new($DefinitionSddl)
  echo "Success!"
} catch {
  echo "Failed."
}

# 反映
$AuthNZ = @{
  Name = $Enrollment
  # RunAsCredential = $TargetUser  # 完全な Administrators の権限が欲しい場合
  SecurityDescriptorSddl = $DefinitionSddl
}
Set-PSSessionConfiguration @AuthNZ -Force

# 結果の確認
($ChangedSd = Get-PSSessionConfiguration $Enrollment) | fl Name,SecurityDescriptorSddl
($ChangedSd.SecurityDescriptorSddl | ConvertFrom-SddlString).DiscretionaryAcl

# フレーズファイルは管理者の読み取りアクセスのみに制限する
$PhraseSddl = $Owner + $Group + "D:P$($DaclAces[0])" + $SACL
$PhraseAcl = Get-Acl $PhrasePath
$PhraseAcl.SetSecurityDescriptorSddlForm($PhraseSddl)
$PhraseAcl | Set-Acl
# 確認
(Get-Acl $PhrasePath).GetSecurityDescriptorSddlForm("All")
SDDL を文字列でファイルにバックアップ
# 古い SDDL を書き込み
New-Item $BackupSddl -Value "バックアップ`r`n" -Force > $null
$ExistedSddl | Add-Content $BackupSddl -Encoding UTF8

# 新しい SDDL を書き込み
Add-Content $BackupSddl "`r`n書き換え後" -Encoding UTF8
$DefinitionSddl | Add-Content $BackupSddl -Encoding UTF8

# 確認
type $BackupSddl -Encoding UTF8
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?