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?

cscの作法 その642

0
Posted at

概要

cscの作法、調べてみた。
Securityのイベントログを検索してみた。

サンプルコード


using System;
using System.Collections.Generic;
using System.Diagnostics.Eventing.Reader;

class Program
{
	static void Main() {
		int[] targetEventIds = new int[] { 
			4624, 4625, 4672, 4648, 4720, 4732, 1102, 4719 
		};
		List<string> idQueries = new List<string>();
		foreach (int id in targetEventIds)
		{
			idQueries.Add("id=" + id);
		}
		string idQuery = string.Join(" or ", idQueries.ToArray());
		//string queryString = "*[System[" + idQuery + "]]";
		string queryString = "*[System[(EventID=4624 or EventID=4625 or EventID=4672 or EventID=4648 or EventID=4720 or EventID=4732 or EventID=1102 or EventID=4719)]]";
		Console.WriteLine(queryString);
		EventLogQuery eventsQuery = new EventLogQuery("Security", PathType.LogName, queryString);
		eventsQuery.ReverseDirection = true; 
		try
		{
			using (EventLogReader logReader = new EventLogReader(eventsQuery))
			{
				Console.WriteLine("--- Security イベントログ検索開始 ---");
				EventRecord record;
				int count = 0;
				while ((record = logReader.ReadEvent()) != null)
				{
					try
					{
						Console.WriteLine("[日時]: " + record.TimeCreated);
						Console.WriteLine("[イベントID]: " + record.Id);
						Console.WriteLine("[タスク名]: " + record.TaskDisplayName);
						try 
						{
							Console.WriteLine("[メッセージ]: " + record.FormatDescription());
						}
						catch 
						{
							Console.WriteLine("[メッセージ]: 取得できませんでした。");
						}
						Console.WriteLine(new string('-', 50));
						count++;
						if (count >= 10) 
							break;
					}
					finally
					{
						record.Dispose();
					}
				}
				if (count == 0)
				{
					Console.WriteLine("該当するイベントログは見つかりませんでした。");
				}
			}
		}
		catch (EventLogException e)
		{
			Console.WriteLine("イベントログの読み込みエラー: " + e.Message);
		}
		catch (UnauthorizedAccessException)
		{
			Console.WriteLine("エラー: Security ログの閲覧には「管理者権限」が必要です。");
		}
		Console.WriteLine("Enterキーを押すと終了します。");
		Console.ReadLine();
	}
}

実行結果

>iventlog0
*[System[(EventID=4624 or EventID=4625 or EventID=4672 or EventID=4648 or EventID=4720 or EventID=4732 or EventID=1102 or EventID=4719)]]
--- Security イベントログ検索開始 ---
[日時]: 2026/10/15 5:58:02
[イベントID]: 4672
[タスク名]: Special Logon
[メッセージ]: 新しいログオンに特権が割り当てられました。

サブジェクト:
        セキュリティ ID:                S-1-5-18
        アカウント名:           SYSTEM
        アカウント ドメイン:            NT AUTHORITY
        ログオン ID:            0x3E7

特権:           SeAssignPrimaryTokenPrivilege
                        SeTcbPrivilege
                        SeSecurityPrivilege
                        SeTakeOwnershipPrivilege
                        SeLoadDriverPrivilege
                        SeBackupPrivilege
                        SeRestorePrivilege
                        SeDebugPrivilege
                        SeAuditPrivilege
                        SeSystemEnvironmentPrivilege
                        SeImpersonatePrivilege
                        SeDelegateSessionUserImpersonatePrivilege
--------------------------------------------------
[日時]: 2026/10/15 5:58:02
[イベントID]: 4624
[タスク名]: Logon
[メッセージ]: アカウントが正常にログオンしました。

サブジェクト:
        セキュリティ ID:                S-1-5-18
        アカウント名:           DESKTOP-73HG0EO$
        アカウント ドメイン:            WORKGROUP
        ログオン ID:            0x3E7

ログオン情報:
        ログオン タイプ:                5
        制限付き管理モード:     -
        仮想アカウント:         いいえ
        昇格されたトークン:             はい

偽装レベル:             偽装

新しいログオン:
        セキュリティ ID:                S-1-5-18
        アカウント名:           SYSTEM
        アカウント ドメイン:            NT AUTHORITY
        ログオン ID:            0x3E7
        リンクされたログオン ID:                0x0
        ネットワーク アカウント名:      -
        ネットワーク アカウント ドメイン:       -
        ログオン GUID:          {00000000-0000-0000-0000-000000000000}

プロセス情報:
        プロセス ID:            0x35c
        プロセス名:             C:\Windows\System32\services.exe

ネットワーク情報:
        ワークステーション名:   -
        ソース ネットワーク アドレス:   -
        ソース ポート:          -

詳細な認証情報:
        ログオン プロセス:              Advapi
        認証パッケージ: Negotiate
        移行されたサービス:     -
        パッケージ名 (NTLM のみ):       -
        キーの長さ:             0

このイベントは、ログオン セッションの作成時に生成されます。このイベントは、アクセス先のコンピューターで生成されます。

サブジェクトのフィールドは、ログオンを要求したローカル システム上のアカウントを示します。これはサーバー サービスなどのサービスまたは Winlogon.exe や Services.exe などのローカル プロセスであることが最も一般的です。

ログオン タイプのフィールドは、発生したログオンの種類を示します。最も一般的なタイプは、2 (対話型) と 3 (ネットワーク) です。

新しいログオンのフィールドは、新しいログオンを作成するアカウント、つまりログオン先のアカウントを示します。

ネットワークのフィールドは、リモート ログオン要求の送信元を示します。ワークステーション名は常に表示されるとは限らず、場 合によっては空白のままであることがあります。

偽装レベルのフィールドは、ログオン セッション内のプロセスが偽装できる程度を示します。

認証情報のフィールドは、この特定のログオン要求に関する詳細情報を示します。
        - ログオン GUID は、このイベントを KDC イベントに関連付ける場合に使用できる一意の識別子です。
        - 移行されたサービスは、このログオン要求に関与した中間サービスを示します。
        - パッケージ名は、NTLM プロトコルのうち使用されたサブプロトコルを示します。
        - キーの長さは、生成されたセッション キーの長さを示します。これは、セッション キーが要求されなかった場合は 0 に なります。
--------------------------------------------------

以上。

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?