spencernorton commited on
Commit
598a369
ยท
verified ยท
1 Parent(s): f598187

fix(malware-guard): refine Office/OneNote trap scope, sized course media, and stream claims

Browse files
Files changed (1) hide show
  1. csam_guard.py +30 -14
csam_guard.py CHANGED
@@ -251,7 +251,7 @@ _EXTREME_VIOLENCE_PATTERNS = [
251
  # High-risk dropper extensions that are NEVER legitimate standalone software/game/media torrents
252
  _ALWAYS_MALWARE_EXTENSIONS = {
253
  ".scr", ".pif", ".hta", ".vbs", ".vbe", ".wsf", ".wsh",
254
- ".cpl", ".gadget", ".application", ".one", ".xll", ".xlam",
255
  ".appref-ms", ".library-ms", ".search-ms", ".settingcontent-ms", ".diagcab",
256
  }
257
 
@@ -310,16 +310,19 @@ _ANIME_OR_EP_ZIP_CG_RE = re.compile(
310
  r"\s-\s\d{1,3}\s*(?:\(|\[).*?\b(?:1080p|720p|2160p|480p)\b)"
311
  )
312
  _VIDEO_RIP_CLAIM_CG_RE = re.compile(
313
- r"(?i)\b(?:1080p|720p|2160p|480p)\b.*?\b(?:web-?dl|webrip|bluray|bdrip|brrip|hdrip|hdtv|hdcam|camrip|x264|x265|h\.?264|h\.?265|hevc|av1|xvid|divx)\b"
 
 
 
 
314
  )
315
  _OPTICAL_DISC_TITLE_CG_RE = re.compile(
316
- r"(?i)\b(?:dvd(?:[59]|rip|r|-r|-rom)?|bd(?:25|50|66|100|mv|rom|-r)|blu-?ray[\s._-]*disc|full[\s._-]*bd|untouched|remux|pal|ntsc|iso|udf|sacd|dvd-?audio|3d[\s._-]*iso)\b"
317
  )
318
  _BENIGN_ZIP_CG_RE = re.compile(
319
  r"(?i)\b(?:batch|complete|season[ ._-]*\d+|s\d{1,2}[ ._-]*complete|e?\d{1,3}[ ._-]*[~-][ ._-]*e?\d{1,3}|์‹œ์ฆŒ\s*\d+|์™„๊ฒฐ|ๅ…จ้›†|ๅˆ้›†|"
320
  r"ost|soundtrack|flac|mp3|wav|lossless|album|discography|artbook|scans?|wallpapers?|fonts?|subs?|subtitles?|"
321
- r"cbz|cbr|comic|manga|magazine|ebook|epub|pdf|audiobook|m4b|templates?|presets?|samples?|stems|"
322
- r"repack|fitgirl|dodi|gog|codex|rune|tenoke|skidrow|portable|installer|setup|linux|windows|macos|android|apk)\b"
323
  )
324
 
325
  _SINGLE_ARCHIVE_TRAP_EXTS = {".zip", ".zipx", ".rar", ".7z", ".ace", ".cab"}
@@ -552,10 +555,16 @@ class CSAMGuard:
552
  sanitized_title=res.sanitized_title,
553
  )
554
 
 
 
555
  # 7. Title-level disguised media double-extension & unconditional dropper extension checks
556
  t_ext = _match_malware_ext(title_clean)
557
  if t_ext is not None:
558
- if any(stem in title_clean for stem in _DISGUISED_MEDIA_STEMS) or t_ext in _ALWAYS_MALWARE_EXTENSIONS:
 
 
 
 
559
  return ContentFilterResult(
560
  is_safe=False,
561
  violation_type=CSAMViolationType.MALWARE_PAYLOAD,
@@ -576,6 +585,7 @@ class CSAMGuard:
576
  has_bdmv_structure = "bdmv" in title_clean
577
  has_renpy_structure = False
578
  benign_course_files = 0
 
579
  has_password_companion = False
580
  image_file_count = 0
581
  image_total_bytes = 0
@@ -590,8 +600,9 @@ class CSAMGuard:
590
  has_password_companion = True
591
  dot = f_posix.rfind(".")
592
  f_ext = f_posix[dot:] if dot != -1 else ""
593
- if f_ext in _BENIGN_COURSE_EXTS:
594
  benign_course_files += 1
 
595
  if f_ext in (".jpg", ".jpeg", ".png", ".webp") and fsize > 0:
596
  image_file_count += 1
597
  image_total_bytes += fsize
@@ -600,13 +611,13 @@ class CSAMGuard:
600
  if fsize >= 100_000_000:
601
  has_large_real_video = True
602
  has_real_image_set = image_file_count >= 5 and image_total_bytes >= 5_000_000
 
603
 
604
- # Multi-lesson courses (>= 5 files and >= 3 benign video/doc/audio files) legitimately include
605
- # .js, .ps1, .bat, or interactive player .exe files; small fake-course droppers are still checked.
606
  check_media_executables = is_strict_media_category or (
607
- is_course_category and (len(parsed_files) < 5 or benign_course_files < 3)
608
  )
609
- title_claims_video = bool(_ANIME_OR_EP_ZIP_CG_RE.search(title_clean) or _VIDEO_RIP_CLAIM_CG_RE.search(title_clean))
610
 
611
  # Contextual codec-pack lure check: blocks codec-pack installers disguised as or bundled with media
612
  # while sparing legitimate standalone codec-pack software utilities.
@@ -698,10 +709,15 @@ class CSAMGuard:
698
  # Exempt legitimate Blu-ray BD-J Java menu JARs (BDMV/JAR/00000.jar)
699
  if ext == ".jar" and has_bdmv_structure and _BDMV_JAR_PATH_RE.search(f_posix):
700
  continue
701
- # Exempt companion tracker bookmark (.url) when real video/audio media is present
702
- if ext == ".url" and (has_any_video or benign_course_files > 0):
703
  continue
704
- if check_media_executables or is_disguised or ext in _ALWAYS_MALWARE_EXTENSIONS:
 
 
 
 
 
705
  return ContentFilterResult(
706
  is_safe=False,
707
  violation_type=CSAMViolationType.MALWARE_PAYLOAD,
 
251
  # High-risk dropper extensions that are NEVER legitimate standalone software/game/media torrents
252
  _ALWAYS_MALWARE_EXTENSIONS = {
253
  ".scr", ".pif", ".hta", ".vbs", ".vbe", ".wsf", ".wsh",
254
+ ".cpl", ".gadget", ".application",
255
  ".appref-ms", ".library-ms", ".search-ms", ".settingcontent-ms", ".diagcab",
256
  }
257
 
 
310
  r"\s-\s\d{1,3}\s*(?:\(|\[).*?\b(?:1080p|720p|2160p|480p)\b)"
311
  )
312
  _VIDEO_RIP_CLAIM_CG_RE = re.compile(
313
+ r"(?i)(?:"
314
+ r"\b(?:1080p|720p|2160p|480p|576p|4k)\b.*?\b(?:web-?dl|webrip|bluray|bdrip|brrip|hdrip|hdtv|hdcam|camrip|dvdrip|x264|x265|h\.?264|h\.?265|hevc|av1|xvid|divx)\b|"
315
+ r"\b(?:web-?dl|webrip|bdrip|brrip|hdrip|hdtv|hdcam|camrip|dvdrip)\b(?:.*?\b(?:x264|x265|h\.?264|h\.?265|hevc|av1|xvid|divx|aac|ac3|ddp?5\.?1))?|"
316
+ r"\b(?:19\d\d|20\d\d|s\d{1,2}[._ -]?e\d{1,3}|\d{1,2}x\d{2})\b.*?\b(?:x264|x265|h\.?264|h\.?265|hevc|av1|xvid|divx)\b"
317
+ r")"
318
  )
319
  _OPTICAL_DISC_TITLE_CG_RE = re.compile(
320
+ r"(?i)\b(?:dvd(?:[59]|r|-r|-rom)?|bd(?:25|50|66|100|mv|rom|-r)|blu-?ray[\s._-]*disc|full[\s._-]*bd|untouched|remux|pal|ntsc|iso|udf|sacd|dvd-?audio|3d[\s._-]*iso)\b"
321
  )
322
  _BENIGN_ZIP_CG_RE = re.compile(
323
  r"(?i)\b(?:batch|complete|season[ ._-]*\d+|s\d{1,2}[ ._-]*complete|e?\d{1,3}[ ._-]*[~-][ ._-]*e?\d{1,3}|์‹œ์ฆŒ\s*\d+|์™„๊ฒฐ|ๅ…จ้›†|ๅˆ้›†|"
324
  r"ost|soundtrack|flac|mp3|wav|lossless|album|discography|artbook|scans?|wallpapers?|fonts?|subs?|subtitles?|"
325
+ r"cbz|cbr|comic|manga|magazine|ebook|epub|pdf|audiobook|m4b|templates?|presets?|samples?|stems)\b"
 
326
  )
327
 
328
  _SINGLE_ARCHIVE_TRAP_EXTS = {".zip", ".zipx", ".rar", ".7z", ".ace", ".cab"}
 
555
  sanitized_title=res.sanitized_title,
556
  )
557
 
558
+ title_claims_video = bool(_ANIME_OR_EP_ZIP_CG_RE.search(title_clean) or _VIDEO_RIP_CLAIM_CG_RE.search(title_clean))
559
+
560
  # 7. Title-level disguised media double-extension & unconditional dropper extension checks
561
  t_ext = _match_malware_ext(title_clean)
562
  if t_ext is not None:
563
+ if (
564
+ any(stem in title_clean for stem in _DISGUISED_MEDIA_STEMS)
565
+ or t_ext in _ALWAYS_MALWARE_EXTENSIONS
566
+ or (title_claims_video and t_ext in (".one", ".xll", ".xlam"))
567
+ ):
568
  return ContentFilterResult(
569
  is_safe=False,
570
  violation_type=CSAMViolationType.MALWARE_PAYLOAD,
 
585
  has_bdmv_structure = "bdmv" in title_clean
586
  has_renpy_structure = False
587
  benign_course_files = 0
588
+ benign_course_bytes = 0
589
  has_password_companion = False
590
  image_file_count = 0
591
  image_total_bytes = 0
 
600
  has_password_companion = True
601
  dot = f_posix.rfind(".")
602
  f_ext = f_posix[dot:] if dot != -1 else ""
603
+ if f_ext in _BENIGN_COURSE_EXTS and fsize >= 65536:
604
  benign_course_files += 1
605
+ benign_course_bytes += fsize
606
  if f_ext in (".jpg", ".jpeg", ".png", ".webp") and fsize > 0:
607
  image_file_count += 1
608
  image_total_bytes += fsize
 
611
  if fsize >= 100_000_000:
612
  has_large_real_video = True
613
  has_real_image_set = image_file_count >= 5 and image_total_bytes >= 5_000_000
614
+ has_real_course_media = benign_course_files >= 3 and benign_course_bytes >= 50_000_000
615
 
616
+ # Multi-lesson courses (>= 5 files, >= 3 sized lesson files, >= 50 MB total lesson media) legitimately
617
+ # include .js, .ps1, .bat, or interactive player .exe files; zero-byte or tiny decoys are still checked.
618
  check_media_executables = is_strict_media_category or (
619
+ is_course_category and (len(parsed_files) < 5 or not has_real_course_media)
620
  )
 
621
 
622
  # Contextual codec-pack lure check: blocks codec-pack installers disguised as or bundled with media
623
  # while sparing legitimate standalone codec-pack software utilities.
 
709
  # Exempt legitimate Blu-ray BD-J Java menu JARs (BDMV/JAR/00000.jar)
710
  if ext == ".jar" and has_bdmv_structure and _BDMV_JAR_PATH_RE.search(f_posix):
711
  continue
712
+ # Exempt companion tracker bookmark (.url) when real video/audio/course media is present
713
+ if ext == ".url" and (has_large_real_video or has_real_image_set or has_real_course_media):
714
  continue
715
+ if (
716
+ check_media_executables
717
+ or is_disguised
718
+ or ext in _ALWAYS_MALWARE_EXTENSIONS
719
+ or (title_claims_video and ext in (".one", ".xll", ".xlam"))
720
+ ):
721
  return ContentFilterResult(
722
  is_safe=False,
723
  violation_type=CSAMViolationType.MALWARE_PAYLOAD,