前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【Windows时间跳变数十天后】问题分析

【Windows时间跳变数十天后】问题分析

原创
作者头像
Windows技术交流
发布2024-02-06 13:14:22
4240
发布2024-02-06 13:14:22
举报
文章被收录于专栏:Windows技术交流

问题现象

Server2019系统出现时间跳变,时间变到几十天甚至上百天后,在变化前会有这样的日志

事件1056,TerminalServices-RemoteConnectionManager:生成了将用于对 SSL 连接进行 RD 会话主机服务器身份验证的新的自签名证书。此证书上的名称为 下划线内网IP。该证书的 SHA1 哈希在事件数据中。

问题分析

朋友这台机器从现有日志看发生了2次(最近一次重启机器后的时间是2023.12.18 22:30)

机器从12月18日 22:30启动后,Windows Time服务的5分钟校时一开始是正常的,但持续不久就出现乱象了

22:30

22:35

22:40

22:45

没有22:50

没有22:55

没有23:00

23:05

23:10

往后则比较乱:校时日志除过System.evtx里有,Microsoft-Windows-Time-Service%4Operational.evtx也有,但间隔不稳定,很乱,有时5分钟校时一次,有时10分钟校时一次,有时15分钟校时一次,有时20分钟校时一次,甚至有连续若干个小时没有校时日志的情况

总之,最开始单从校时间隔看,Windows Time服务就有疑点。

下面详细展开:

1、11月11日凌晨发生时,4点11分左右出现异常,5点23分左右短暂恢复正常后又异常,6点12分左右完全恢复

2023.11.11凌晨4点多-6点多那次,当时processid是2720

【系统日志】

更改原因: An application or system component changed the time。

进度:“\Device\HarddiskVolume2\Windows\System32\svchost.exe”(PID 2720)。

2、12月28日下午发生时,16点53分左右出现异常,17点04分左右恢复

2023.12.28下午那次,processid是2468就是2023.12.18 22:30重启机器后的那个processid

【系统日志】

更改原因: An application or system component changed the time。

进度:“\Device\HarddiskVolume2\Windows\System32\svchost.exe”(PID 2468)。

总结:由于processid固定且进程名匹配,非常确定问题发生跟windows time服务直接相关,并且用最新的公共镜像尝试如下思路均排除

①用系统命令改时间

②用程序改时间

time.py

代码语言:py
复制
# -*- coding: utf-8 -*-
import win32api
import datetime

# 要设置的新时间
new_time = datetime.datetime(2023, 12, 29, 1, 0, 0)

# 更改系统时间
win32api.SetSystemTime(new_time.year, new_time.month, new_time.weekday(), new_time.day, new_time.hour, new_time.minute, new_time.second, new_time.microsecond // 1000)

Cloudbase-Init自带python环境,只需要处理环境变量增加这2个

python.exe目录:C:\Program Files\Cloudbase Solutions\Cloudbase-Init\Python\

pip.exe目录:C:\Program Files\Cloudbase Solutions\Cloudbase-Init\Python\Scripts\

然后执行

代码语言:powershell
复制
cmd.exe /c mklink "c:\python_cloudbaseinit" "C:\Program Files\Cloudbase Solutions\Cloudbase-Init\Python\" /D 2>$null
pip install pywin32 
python time.py

③用自建ntpserver设置正确时间来同步和设置未来若干天来校验朋友端的时间同步情况

powershell命令:

代码语言:powershell
复制
w32tm /debug /enable /file:C:\w32time.log /size:10000000 /entries:0-300 
w32tm /config /update 
w32tm /config /manualpeerlist:"自建ntpserver地址,0x8" /syncfromflags:manual /reliable:yes /update 
restart-service w32time 
#net stop w32time
#net start w32time
w32tm /query /source 
w32tm /query /peers 
w32tm /resync /rediscover /nowait 
w32tm /query /status /verbose

ntp server时间正确时同步:

ntp server时间设置未来若干天时校验朋友端时间同步:

时间服务检测到系统时间需要更改 256570 秒。时间服务将不会更改系统时间超过 172800 秒。请验证你的时间和时区是正确的,并且时间源 114.132.150.90,0x8 (ntp.m|0x8|0.0.0.0:123->114.132.150.90:123) 工作正常。

以上都逐个排除,并且确认这个特性:

Windows 2000、Windows XP、Windows Server 2003 和 Windows Vista 中 ,MaxPosPhaseCorrection 和 MaxNegPhaseCorrection 这2个 注册表项的默认值为0xFFFFFFF(10进制:4294967295 ),全F意味着使计算机任何时候都能自动校时。

在≥2008的Server系统中,已采用 MaxPosPhaseCorrection 和 MaxNegPhaseCorrection 注册表项的新默认值为 48 小时(172800秒,0x2A300)。

在≥Win7的PC系统,已采用 MaxPosPhaseCorrection 和 MaxNegPhaseCorrection 注册表项的新默认值为 15 小时(54000秒,0xD2F0)。

针对时间差超过15小时或48小时还想自动校时的需求,可以通过注册表实现:

代码语言:powershell
复制
reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\w32time\Config" /v MaxNegPhaseCorrection /t reg_dword /d 4294967295 /f 
reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\w32time\Config" /v MaxPosPhaseCorrection /t reg_dword /d 4294967295 /f 
reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\W32Time\Config" /v MaxNegPhaseCorrection /t reg_dword /d 4294967295 /f 
reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\W32Time\Config" /v MaxPosPhaseCorrection /t reg_dword /d 4294967295 /f 
reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Policies\Microsoft\W32Time\Config" /v MaxNegPhaseCorrection /t reg_dword /d 4294967295 /f 
reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Policies\Microsoft\W32Time\Config" /v MaxPosPhaseCorrection /t reg_dword /d 4294967295 /f

最后:毕竟朋友自定义镜像比较老,补丁并不全,于是想到windows time bug

循着windows time bug思路,准备找微软的兄弟核实之前先Google一下,看到网友类似案例,有微软文档陈述解决方案:

NT10.0系统 1511版本开始引入默认启用Secure Time Seed of High Confidence (STSHC),简称STS,在某些情况下,可能会导致时间跳变很多天。

微软给的解决方案:通过注册表禁用STS

相关注册表:

Registry Key: HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\W32Time\Config

Value Name: UtilizeSslTimeData

Value Type: REG_DWORD

查询命令:

代码语言:powershell
复制
reg query "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\W32Time\Config"|findstr UtilizeSslTimeData

启用STS:

代码语言:powershell
复制
reg add "HKLM\SYSTEM\CurrentControlSet\Services\W32Time\Config" /v UtilizeSslTimeData /t REG_DWORD /d 1 /f

禁用STS:

代码语言:powershell
复制
reg add "HKLM\SYSTEM\CurrentControlSet\Services\W32Time\Config" /v UtilizeSslTimeData /t REG_DWORD /d 0 /f

网友案例:

https://www.reddit.com/r/sysadmin/comments/61o8p0/system_time_jumping_back_on_windows_10_caused_by/?rdt=38623

微软文档:

https://learn.microsoft.com/zh-cn/archive/blogs/w32time/secure-time-seeding-improving-time-keeping-in-windows

对朋友的建议:

1、开启windows time服务的debug日志

代码语言:powershell
复制
w32tm /debug /enable /file:C:\w32time.log /size:10000000 /entries:0-300

注意日志路径指向剩余空间较大的分区

如果复现问题,日志可以辅助实锤STS

2、打全补丁

3、执行禁用STS的命令并重启windows time服务

针对时间差超过15小时或48小时还想自动校时的需求,可以通过前面提到的注册表实现

代码语言:powershell
复制
reg add "HKLM\SYSTEM\CurrentControlSet\Services\W32Time\Config" /v UtilizeSslTimeData /t REG_DWORD /d 0 /f
restart-service w32time

4、执行 c:\query-w32tm.bat > c:\query-w32tm.log 收集windows time相关配置

query-w32tm.bat

代码语言:bash
复制
time /t
@echo off
for /f "tokens=1-4 delims=:.," %%a in ("%time%") do (
    set hours=%%a
    set minutes=%%b
    set seconds=%%c
    set milliseconds=%%d
)
echo %hours%:%minutes%:%seconds%.%milliseconds%
reg query "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\W32Time\Config" 2>nul | findstr "PhaseCorrection UpdateInterval PhaseCorrectRate"
reg query "HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\W32Time\Config" 2>nul | findstr "PhaseCorrection UpdateInterval PhaseCorrectRate"
reg query HKLM\SYSTEM\CurrentControlSet\Services\W32Time\Parameters 2>nul | findstr "NtpServer Type"
reg query HKLM\SOFTWARE\Policies\Microsoft\W32Time\Parameters 2>nul | findstr "NtpServer Type"
reg query HKLM\SOFTWARE\WOW6432Node\Policies\Microsoft\W32time\Parameters 2>nul | findstr "NtpServer Type"
reg query HKLM\SYSTEM\CurrentControlSet\Services\W32Time\Config 2>nul | findstr "PollInterval"
reg query HKLM\SYSTEM\CurrentControlSet\Services\W32Time\TimeProviders\NtpClient 2>nul | findstr "PollInterval"
reg query HKLM\SOFTWARE\Policies\Microsoft\W32Time\TimeProviders\NtpClient 2>nul | findstr "PollInterval"
reg query HKLM\SOFTWARE\Wow6432Node\Policies\Microsoft\W32Time\TimeProviders\NtpClient 2>nul | findstr "PollInterval"

time /t
@echo off
for /f "tokens=1-4 delims=:.," %%a in ("%time%") do (
    set hours=%%a
    set minutes=%%b
    set seconds=%%c
    set milliseconds=%%d
)
echo %hours%:%minutes%:%seconds%.%milliseconds%
w32tm /query /source 2>nul
w32tm /query /source 2>nul
w32tm /query /source 2>nul
w32tm /query /source 2>nul
w32tm /query /source 2>nul
w32tm /query /source 2>nul
w32tm /query /source 2>nul
w32tm /query /source 2>nul

time /t
@echo off
for /f "tokens=1-4 delims=:.," %%a in ("%time%") do (
    set hours=%%a
    set minutes=%%b
    set seconds=%%c
    set milliseconds=%%d
)
echo %hours%:%minutes%:%seconds%.%milliseconds%
w32tm /query /peers 2>nul
w32tm /query /peers 2>nul
w32tm /query /peers 2>nul
w32tm /query /peers 2>nul
w32tm /query /peers 2>nul
w32tm /query /peers 2>nul
w32tm /query /peers 2>nul
w32tm /query /peers 2>nul

time /t
@echo off
for /f "tokens=1-4 delims=:.," %%a in ("%time%") do (
    set hours=%%a
    set minutes=%%b
    set seconds=%%c
    set milliseconds=%%d
)
echo %hours%:%minutes%:%seconds%.%milliseconds%
w32tm /query /status /verbose 2>nul
w32tm /query /status /verbose 2>nul



time /t
@echo off
for /f "tokens=1-4 delims=:.," %%a in ("%time%") do (
    set hours=%%a
    set minutes=%%b
    set seconds=%%c
    set milliseconds=%%d
)
echo %hours%:%minutes%:%seconds%.%milliseconds%
w32tm /query /configuration 2>nul
w32tm /query /configuration 2>nul

5、执行这句命令后提供C:\GPReport.html查看windows time相关配置

代码语言:powershell
复制
cmd.exe /c "gpresult /H C:\GPReport.html /f && C:\GPReport.html"

6、有条件的话,使用server2022系统,2022系统在健壮性、功能性、安全性、网络性能各方面优于低版本系统。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 问题现象
  • 问题分析
相关产品与服务
云服务器
云服务器(Cloud Virtual Machine,CVM)提供安全可靠的弹性计算服务。 您可以实时扩展或缩减计算资源,适应变化的业务需求,并只需按实际使用的资源计费。使用 CVM 可以极大降低您的软硬件采购成本,简化 IT 运维工作。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档