# Copyright 2026 SigmaSRC, Inc. All Rights Reserved.

"""Gather Windows Updates information using COM extensions."""

from win32com.client import GetObject
import logging

from sigma.util import config

logger = logging.getLogger("gather.gather_winupdate")

def Gather(collector):
    # write header to output
    table = collector.table("winupdate", ["update_name", "update_description"])
    try:
        updatelist = []
        objItems = GetObject("WinMgMts://./root/cimv2").InstancesOf("Win32_QuickFixEngineering")
        for item in objItems:
            hotfixid = item.Properties_("HotFixID").Value
            description = item.Properties_("Description").Value
            # some hotfixids appear as 'File x', don't know why, ignore these
            if hotfixid.lower().startswith("file") or hotfixid == "":
                continue
            # some hotfixes appear multiple times, ignore duplicates
            if (hotfixid, description) in updatelist:
                continue
            updatelist.append((hotfixid, description))
            table.add((hotfixid, description))        
    except:
        logger.exception("winupdate gather failed")

def _test():
    from sigma.gather import gathering
    gathering.test(Gather)

if __name__ == "__main__":
    _test()
