Skip to content

fix: consolidate FILETIME conversions into wintime.py and fix qwinsta crash on Windows - #2215

Open
n3rada wants to merge 3 commits into
fortra:masterfrom
n3rada:fix/getunixtime-dedup
Open

fix: consolidate FILETIME conversions into wintime.py and fix qwinsta crash on Windows#2215
n3rada wants to merge 3 commits into
fortra:masterfrom
n3rada:fix/getunixtime-dedup

Conversation

@n3rada

@n3rada n3rada commented Jun 23, 2026

Copy link
Copy Markdown
Contributor

Hello maintainers 👋

Picking up on #2022 (and also fixing #1374 along the way).

The getUnixTime function was copy-pasted across 11 files with no single authoritative definition. Two of those copies used /= instead of //=, silently producing floats. The one in smbserver.py was a dead wrapper around smb.FTtoPOSIX that was never called. The same problem exists on the inverse side: POSIXtoFT and FTtoPOSIX lived in smb.py and were re-imported everywhere.

What this PR does:

Introduces impacket/wintime.py as the single home for Windows FILETIME conversions. It exposes three functions:

  • filetime_to_datetime(t): converts a FILETIME value to a naive UTC datetime object by computing datetime(1601, 1, 1) + timedelta(microseconds=t // 10). Avoids fromtimestamp() entirely, so it never crashes on Windows for any input value. For t=0 (uninitialised/never-set fields) it returns datetime(1601, 1, 1) rather than lying with the Unix epoch.
  • filetime_to_posix(t): converts to a POSIX integer for callers that genuinely need one (e.g. os.utime). Includes a zero-guard for values below the FILETIME epoch.
  • posix_to_filetime(t): replaces POSIXtoFT.

All callers that previously did datetime.fromtimestamp(getUnixTime(t)) or datetime.fromtimestamp(filetime_to_posix(t)) now use filetime_to_datetime(t) directly. No legacy aliases remain anywhere. smb.py still imports from wintime for its own internal use but no longer re-exports the functions.

I chose a dedicated wintime.py rather than dropping this into smb.py or a generic utils.py — the SMB module is already large and these conversions are used by ese, nspi, tsts, dpapi, and several examples that have nothing to do with SMB.

Closes #2022
Closes #1374

Best regards

@n3rada n3rada changed the title wintime: centralize FILETIME/POSIX conversions in impacket/wintime.py fix: consolidate FILETIME conversions into wintime.py and fix qwinsta crash on Windows Jun 24, 2026
@n3rada
n3rada force-pushed the fix/getunixtime-dedup branch from 9ad3caa to caf247a Compare June 24, 2026 12:18
@anadrianmanrique anadrianmanrique self-assigned this Jun 24, 2026
@anadrianmanrique anadrianmanrique added the in review This issue or pull request is being analyzed label Jun 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

in review This issue or pull request is being analyzed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Code Cleanup] getUnixTime function definition in many classes Exception when running tstool.py with verb qwinsta

2 participants