Skip to content

Zero Breach — local, read-only scanner that checks a computer for indicators of the AdFection / Candiru ad-delivered spyware campaign. No network, no admin, no dependencies.

Handle Windows 3.12 file identity consistently

interplainsdfa70860d28c
diff --git a/DETECTION-UPGRADE.md b/DETECTION-UPGRADE.md
index fec353c..6dd2468 100644
--- a/DETECTION-UPGRADE.md
+++ b/DETECTION-UPGRADE.md
@@ -83,10 +83,10 @@ pattern/integration tests without failures or skips. A small clean corpus of
200 Debian files and 26 official Python Windows PE files produced no matches.
The revised development candidate is SHA-256
-`57a1d4293e9f3a32c4dfc36393ecf007efef7cc4e5609bd42ba088f173f5cfd5`.
-After the prune correction and documentation changes, the Windows suite ran
-268 tests with no failures and one permission skip; without the optional engine
-it ran 268 tests with seven expected skips. These are development receipts;
+`2b30cc710a6f1fc326ef29d57e38e9f98279c428d8713d7aa85d0c905b54e549`.
+After the prune correction and a Windows Python 3.12 file-metadata compatibility
+fix, the Windows suite ran 269 tests with no failures and one permission skip;
+without the optional engine it ran 269 tests with seven expected skips. These are development receipts;
version and packaging changes require a new final hash and checks.
Two hash-verified MalwareBazaar downloads were scanned as static bytes inside
diff --git a/scan/adint_scan/modules/patternscan.py b/scan/adint_scan/modules/patternscan.py
index 9ff242f..5d2c166 100644
--- a/scan/adint_scan/modules/patternscan.py
+++ b/scan/adint_scan/modules/patternscan.py
@@ -129,6 +129,13 @@ def _pattern_identity(info):
getattr(info, "st_ctime_ns", info.st_ctime))
+def _pattern_shared_identity(info):
+ # Windows Python 3.12 can report different st_ctime_ns values for lstat
+ # and fstat on the same file. Compare ctime only between calls to the same
+ # API; volume, file ID, size and mtime remain comparable across APIs.
+ return _pattern_identity(info)[:4]
+
+
def _pattern_read(path):
"""Bound the read, reject special files, and detect observable replacements.
@@ -157,7 +164,8 @@ def _pattern_read(path):
with stream:
opened = _pattern_os.fstat(stream.fileno())
if (not _pattern_stat.S_ISREG(opened.st_mode)
- or _pattern_identity(before) != _pattern_identity(opened)):
+ or _pattern_shared_identity(before) != _pattern_shared_identity(opened)
+ or _pattern_identity(before) != _pattern_identity(_pattern_os.lstat(path))):
raise OSError("file changed before bounded read")
problem = _pattern_problem(path)
if problem:
@@ -172,7 +180,8 @@ def _pattern_read(path):
if problem:
raise OSError(problem)
if (len(data) > _PATTERN_MAX_BYTES or len(data) != before.st_size
- or _pattern_identity(before) != _pattern_identity(after)
+ or _pattern_shared_identity(before) != _pattern_shared_identity(after)
+ or _pattern_identity(opened) != _pattern_identity(after)
or _pattern_identity(before) != _pattern_identity(final)):
raise OSError("file grew or changed during bounded read")
return data
diff --git a/scan/tests/test_patterns.py b/scan/tests/test_patterns.py
index f5b20e2..8dcbcf3 100644
--- a/scan/tests/test_patterns.py
+++ b/scan/tests/test_patterns.py
@@ -244,17 +244,20 @@ class PatternTests(unittest.TestCase):
self.assertEqual(len(patterns._pattern_read(str(target))), patterns._PATTERN_MAX_BYTES)
@unittest.skipUnless(sys.platform == "win32", "Windows file identity check")
- def test_windows_identity_consistent_on_runner_volume(self):
+ def test_windows_path_and_handle_identity_use_common_fields(self):
target = self.file(data=b"harmless identity fixture")
before = os.lstat(target)
flags = os.O_RDONLY | getattr(os, "O_BINARY", 0) | getattr(os, "O_NONBLOCK", 0)
fd = os.open(target, flags)
try:
opened = os.fstat(fd)
+ second_path = os.lstat(target)
finally:
os.close(fd)
+ self.assertEqual(patterns._pattern_shared_identity(before),
+ patterns._pattern_shared_identity(opened))
self.assertEqual(patterns._pattern_identity(before),
- patterns._pattern_identity(opened))
+ patterns._pattern_identity(second_path))
def test_missing_and_empty_requested_roots_are_unknown(self):
for path in (self.root / "absent", self.root):