【问题标题】:powershell get PATH variable change log in eventviewerpowershell 在事件查看器中获取 PATH 变量更改日志
【发布时间】:2017-03-22 04:14:23
【问题描述】:

我正在尝试从系统和安全事件中获取有关 PATH 环境变量更改的事件日志详细信息,但无法轻松获取。

我尝试了Get-EventLog system | Format-ListGet-EventLog Security | Format-List,但它提供了其他细节。有没有什么方法可以获取在 Windows 2008 R2 上配置服务器时 PATH 环境变量更改的详细信息日志?

【问题讨论】:

  • 问题是正在记录环境变量更改的事件,但您无法按照您想要的方式查询和显示它们,还是没有记录此类事件?如果是后者,请参阅Audit Logs for Environment Path ChangesHow to tell what is changing a windows environment variable
  • 查询和显示环境变量变化有问题。
  • 我只看到消息 An attempt was made to access an objectA handle to an object was requested 的日志,没有看到像 A registry value for modified 这样的任何日志。
  • 在我的系统上,当我通过系统属性更改 %PATH% 环境变量时,我看到带有您记下的两条消息的日志条目,然后是带有消息 Registry value deleted 的每个环境变量的日志条目,然后是通过每个环境变量的日志条目以及消息New registry variable created。两组日志条目都使用 ID 4657 和关键字 Audit Success。您是否按照我上面链接的问题中所述对 HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment 注册表项启用了审核?

标签: powershell environment-variables event-log


【解决方案1】:

假设您已将审核配置为监控存储环境变量的注册表项...

  1. Local Security Policy => Security Settings => Local Policies => Audit Policy => Audit object access 设置为SuccessSuccess, Failure
  2. HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment 注册表项上为Everyone 主体和至少Set value 权限添加审核规则。

...您可以从提升的 PowerShell 会话中运行以下命令,以检索通过修改 %PATH% 环境变量创建的事件日志条目。输出对象将包含所有事件日志条目类型以及注册表修改事件专用的选择属性,因此您可以访问更改%PATH% 的用户和进程以及更改/更改的值。

$auditingProviderName = 'Microsoft-Windows-Security-Auditing';
$registryValueModifiedEventId = 4657;

$auditingProvider = Get-WinEvent -ListProvider $auditingProviderName;
# Get the metadata for the "registry value modified" event
$registryValueModifiedEvent = $auditingProvider.Events `
    | Where-Object -Property 'ID' -EQ -Value $registryValueModifiedEventId;
# Store an array of property names in the order they will appear in each log entry
$registryValueModifiedEventPropertyNames = $registryValueModifiedEvent.Template `
    | Select-Xml `
        -Namespace @{ 'default' = 'http://schemas.microsoft.com/win/2004/08/events'; } `
        -XPath '/default:template/default:data' `
    | Select-Object -ExpandProperty 'Node' `
    | Select-Object -ExpandProperty 'name';

# Construct an XPath expression to filter for auditing events where the Path value of the Environment key has been modified
# This assumes HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet is a link to HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001
$xpathFilter = @"
    *[
        System[
            Provider[@Name = '$auditingProviderName']
                and EventID = $registryValueModifiedEventId
        ]
    ]
    [
        EventData[
            Data[@Name = 'ObjectName'] = '\REGISTRY\MACHINE\SYSTEM\ControlSet001\Control\Session Manager\Environment'
                and Data[@Name = 'ObjectValueName'] = 'Path'
        ]
    ]
"@;
Get-WinEvent -LogName 'Security' -FilterXPath $xpathFilter `
    | ForEach-Object -Process {
        $outputProperties = [Ordered] @{
            MachineName = $_.MachineName;
            TimeCreated = $_.TimeCreated;
            #TODO: Copy additional properties from the event log entry object ($_)
        };

        # Copy each log entry property value to an output object property of the same name
        for ($i = 0; $i -lt $registryValueModifiedEventPropertyNames.Length; $i++)
        {
            $name = $registryValueModifiedEventPropertyNames[$i];
            $value = $_.Properties[$i].Value;

            $outputProperties[$name] = $value;
        }

        return New-Object -TypeName 'PSObject' -Property $outputProperties;
    };

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-02-20
    • 2014-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-04
    相关资源
    最近更新 更多