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 モドキを作ってみた[8. 後始末]

0
Last updated at Posted at 2026-01-21

注意

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

構成

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

説明

 すべての変更をできる限り元に戻します。漏れがないようにはしたつもりですが、直らなかった箇所は各自で修正してください。

内容

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

一時的にセッションを無効化する
Disable-PSSessionConfiguration -Name MockEndpoint
WinRM を無効化する
# 今回作成したエンドポイントを登録解除
Unregister-PSSessionConfiguration -Name MockEndpoint -Force -NoServiceRestart
# 証明書認証を無効化
Set-Item WSMan:\localhost\Service\Auth\Certificate -Value $false -Force
# 信頼された WinRM クライアントの一覧を白紙にする
Set-Item WSMan:\localhost\Client\TrustedHosts -Value "" -Force
# リスナーを削除
Get-ChildItem WSMan:\localhost\Listener | Remove-Item -Recurse -Force
# Microsoft.PowerShell セッション構成を無効化
Disable-PSSessionConfiguration

# PowerShell Remote を無効化
Disable-PSRemoting -Force
# WinRM サービスを停止・無効化
Stop-Service WinRM -Force
Set-Service WinRM -StartupType Disabled
# WS-Management 通信用のファイアウォール例外を無効化
Get-NetFirewallRule -Name "WINRM-*" | Set-NetFirewallRule -Enabled False
# 昇格された権限によるリモートアクセスを無効化
Set-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System" LocalAccountTokenFilterPolicy 0 -Type DWord
証明書関連を削除
# 証明書の紐付けを解除
Remove-Item HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WSMAN\CertMapping\*

# 証明書名のワイルドカードパターン
$Pattern = "*My * - UserDef"

# My 証明書を削除(CurrentUser)
$ThumbUserMyPath  = @()
$ThumbprintUserMy = (Get-ChildItem Cert:\CurrentUser\My).Where{$_.FriendlyName -like $Pattern}.Thumbprint -as [PSObject[]]
$ThumbprintUserMy.ForEach{$ThumbUserMyPath += Join-Path Cert:\CurrentUser\My $_}
Remove-Item $ThumbUserMyPath -WhatIf

# CA 証明書を削除(CurrentUser)
$ThumbUserCAPath  = @()
$ThumbprintUserCA = (Get-ChildItem Cert:\CurrentUser\CA).Where{$_.Subject -like $Pattern}.Thumbprint -as [PSObject[]]
$ThumbprintUserCA.ForEach{$ThumbUserCAPath += Join-Path Cert:\CurrentUser\CA $_}
Remove-Item $ThumbUserCAPath -WhatIf

# My 証明書を削除(LocalMachine)
$ThumbMyPath  = @()
$ThumbprintMy = (Get-ChildItem Cert:\LocalMachine\My).Where{$_.FriendlyName -like $Pattern}.Thumbprint -as [PSObject[]]
$ThumbprintMy.ForEach{$ThumbMyPath += Join-Path Cert:\LocalMachine\My $_}
Remove-Item $ThumbMyPath -WhatIf

# CA 証明書を削除(LocalMachine)
$ThumbCAPath  = @()
$ThumbprintCA = (Get-ChildItem Cert:\LocalMachine\CA).Where{$_.FriendlyName -like $Pattern}.Thumbprint -as [PSObject[]]
$ThumbprintCA.ForEach{$ThumbCAPath += Join-Path Cert:\LocalMachine\CA $_}
Remove-Item $ThumbCAPath -WhatIf

!!!注意!!!
# Root 証明書を削除(LocalMachine)
$ThumbRootPath  = @()
$ThumbprintRoot = (Get-ChildItem Cert:\LocalMachine\Root).Where{$_.FriendlyName -like $Pattern}.Thumbprint -as [PSObject[]]
$ThumbprintRoot.ForEach{$ThumbRootPath += Join-Path Cert:\LocalMachine\Root $_}
Remove-Item $ThumbRootPath -WhatIf
今回作成した構成ファイルをすべて削除
$RoleName   = "Mock"
$ModDirName = "JEA"
$EntirePath = ($Env:PSModulePath -split ";" -like "$Env:ProgramFiles*")[-1]
$ModulePath = Join-Path $EntirePath $ModDirName
$ManifestPd = Join-Path $ModulePath "${ModDirName}.psd1"
$SessionCfg = Join-Path $ModulePath "${RoleName}Session.pssc"
$RoleInPath = Join-Path $ModulePath RoleCapabilities
$RoleConfig = Join-Path $RoleInPath "${RoleName}.psrc"
$PhrasePath = Join-Path $RoleInPath "phrase.uid"
$Enrollment = $RoleName + "Endpoint"

# モジュールファイル・セッションファイル・ロールファイル・フレーズファイルを削除
Remove-Item $ManifestPd, $SessionCfg, $RoleConfig, $PhrasePath

# 次のディレクトリから拡張子が .sddl のものを削除(エンドポイントのアクセス権バックアップ用に作ったファイル)
Get-ChildItem $Env:UserProfile "${Enrollment}*.sddl" | Remove-Item -WhatIf
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?