0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

SID を SecurityID(Domain\User) に変換する [ Windows イベントログ イベントビューア ]

Last updated at Posted at 2024-07-05

概要

イベントログ等に含まれる
SIDの値を可読できる内容(ドメイン\ユーザーの形式)に変換したい

コマンド例

サンプル1(Powershell)

※SIDが含まれるイベントログを出力したPCで以下を実行する

Powewrshell で上から実行していく。

$SID の値は変換したい実際のSIDの値に置き換えて実行する。

powershell
$SID = "S-*-*-********-*****-*****-****"
$objSID = New-Object System.Security.Principal.SecurityIdentifier($SID)
$objUser = $objSID.Translate([System.Security.Principal.NTAccount])
$readableSecurityID = $objUser.Value

Write-Output "$SID : $readableSecurityID"
出力例
S-1-5-********-*****-*****-**** : HogeHost\PiyoUser

もっとシンプルにしたやつ(Powershell 2行)

$SIDを手書きすればワンライナーもいける

$SID = "ここにIDを入れて実行"
 (New-Object System.Security.Principal.SecurityIdentifier($SID)).Translate([System.Security.Principal.NTAccount]).Value

引数でSID渡して処理するスクリプト(.ps1)

SidToSecurityId.ps1
param (
    [Parameter(Mandatory=$true, Position=0)]
    [string]$SID
)

function Convert-SidToReadableName {
    param (
        [Parameter(Mandatory=$true)]
        [string]$SID
    )
    
    try {
        $objSID = New-Object System.Security.Principal.SecurityIdentifier($SID)
        $objUser = $objSID.Translate([System.Security.Principal.NTAccount])
        return $objUser.Value
    }
    catch {
        return "変換できませんでした: $SID"
    }
}

$readableName = Convert-SidToReadableName -SID $SID

Write-Output "$SID : $readableName"

イベントログ一括変換表示スクリプト(.ps1)

イベントログのファイルパスを引数で渡して、ログMessage内容からSIDを検出して一括で変換してリスト表示する(一意なものに絞り込んで表示)

※動作保証なし:sweat_smile:

LogToSecurityIds.ps1
param (
    [Parameter(Mandatory=$true)]
    [string]$CsvPath
)

# 入力ファイルの存在チェック
if (-not (Test-Path $CsvPath)) {
    Write-Error "指定されたCSVファイルが見つかりません: $CsvPath"
    exit 1
}

# SIDを変換する関数
function Convert-SidToName {
    param([string]$SID)
    try {
        $objSID = New-Object System.Security.Principal.SecurityIdentifier($SID)
        $objUser = $objSID.Translate([System.Security.Principal.NTAccount])
        return $objUser.Value
    }
    catch {
        return "変換不可: $SID"
    }
}

# CSVファイルを読み込む
$events = Import-Csv -Path $CsvPath

# ユニークなSIDとそのユーザー名を保持するためのハッシュテーブル
$sidDictionary = @{}

# 各行を確認
foreach ($event in $events) {
    # SIDのパターンを定義
    $sidPattern = 'S-1-5-\d+-(\d+-){1,14}\d+'

    # SIDに該当する記述があるか確認
    $sidMatches = [regex]::Matches($event.Message, $sidPattern)

    if ($sidMatches.Count -gt 0) {
        foreach ($sidMatch in $sidMatches) {
            $sid = $sidMatch.Value

            # SIDを変換
            $name = Convert-SidToName -SID $sid
            # SIDの検出ごとにSID出力をしたい場合は有効にする↓
            # Write-Output "$sid : $name"

            # ユニークな結果を保持するためにハッシュテーブルを使用
            if (-not $sidDictionary.ContainsKey($sid)) {
                $sidDictionary[$sid] = $name
            }

        }
    } else {
        # 各行ごとにSIDが見つからなかった場合に通知を出力したい場合は有効にする↓
        # Write-Output "SID not found"
    }
}

# ユニークな結果を配列に変換
$results = $sidDictionary.GetEnumerator() | ForEach-Object {
    [PSCustomObject]@{
        SID = $_.Key
        Name = $_.Value
    }
}

Write-Output "`nResults: "
$results

参考

0
2
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
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?