### Install CPE Package Source: https://github.com/nilp0inter/cpe/blob/main/docs/install.rst Provides commands to install the CPE library. Users can choose between using the pip package manager or running the setup script manually. ```bash pip install cpe ``` ```bash python setup.py install ``` -------------------------------- ### CPE Language Matching Setup (Python) Source: https://context7.com/nilp0inter/cpe/llms.txt Shows the initial setup for CPE Language matching, including creating CPE objects and CPE sets. The actual XML expression definition is intended to follow this setup. ```python from cpe import CPE from cpe.cpeset2_2 import CPESet2_2 from cpe.cpelang2_2 import CPELanguage2_2 # Create known CPE Names for the target system cpe_solaris = CPE('cpe:/o:sun:solaris:5.9:::en-us', CPE.VERSION_2_2) cpe_weblogic = CPE('cpe:/a:bea:weblogic:8.1', CPE.VERSION_2_2) # Create set of known instances known_set = CPESet2_2() known_set.append(cpe_solaris) known_set.append(cpe_weblogic) # Define a CPE Language expression in XML ``` -------------------------------- ### CPE Parsing Examples Source: https://github.com/nilp0inter/cpe/blob/main/tests/testfile_cpe.txt Examples demonstrating how to parse different types of CPE strings, including virtual hardware, operating systems, and applications. ```APIDOC ## CPE Parsing Examples ### Description This section provides examples of parsing various CPE (Common Platform Enumeration) strings into CPE objects. ### Method `CPE(cpe_string)` ### Endpoint N/A (Library Usage) ### Parameters #### Path Parameters N/A #### Query Parameters N/A #### Request Body N/A ### Request Example ```python # Example for a virtual hardware platform str_virtual = 'cpe:/emc:vmware:esx:2.5' c = CPE(str_virtual) # Example for Microsoft Windows XP Professional SP2 str_windows_xp = 'cpe://microsoft:windows:xp:pro:sp2' c = CPE(str_windows_xp) # Example for Apple MacOS X 10.4 Tiger str_macos = 'cpe://apple:macos:10.4' c = CPE(str_macos) # Example for Fedora Core Linux 6 str_fedora = 'cpe://fedoraproject:fedora_core:6' c = CPE(str_fedora) # Example for Microsoft Windows 2000 SP4 str_win2k = 'cpe://microsoft:windows:2000::sp4' c = CPE(str_win2k) # Example for JBoss 4.0.4 Application Server str_jboss = 'cpe:///jboss:jboss:4.0.4' c = CPE(str_jboss) # Example for Oracle Database 10g Release 2 str_oracle = 'cpe:///oracle:database:10g:r2' c = CPE(str_oracle) # Example for Adobe Acrobat 6.0 (Standard or Professional) str_acrobat = 'cpe:///adobe:acrobat:6.0:std!pro' c = CPE(str_acrobat) ``` ### Response N/A (Object creation) ### Response Example N/A ``` -------------------------------- ### CPEComponent2_2: Validate and Initialize Component Source: https://github.com/nilp0inter/cpe/blob/main/tests/testfile_cpecomp2_2.txt Demonstrates the initialization of a CPEComponent2_2 object with various values and attributes. Includes examples of valid inputs and how invalid values trigger ValueErrors for attributes like 'part', 'vendor', and 'update'. ```python val = 'linux%7' comp1 = CPEComponent2_2(val, CPEComponentSimple.ATT_PRODUCT) val = 'sp2' comp1 = CPEComponent2_2(val, CPEComponentSimple.ATT_UPDATE) val = 'bad||ven~~dor' comp1 = CPEComponent2_2(val, CPEComponentSimple.ATT_VENDOR) #doctest: +IGNORE_EXCEPTION_DETAIL # ValueError: Invalid value of attribute 'vendor': bad||ven~~dor val = 'microsoft' comp1 = CPEComponent2_2(val, "badkey") #doctest: +IGNORE_EXCEPTION_DETAIL # ValueError: Invalid attribute 'badkey' ``` -------------------------------- ### Format Conversion Methods Source: https://github.com/nilp0inter/cpe/blob/main/tests/testfile_cpecomp2_3_wfn.txt Examples of converting CPE component values into different formats including filesystem (as_fs), URI (as_uri_2_3), and Well-Formed Name (as_wfn). ```python comp = CPEComponent2_3_WFN('"?firefox?"', CPEComponent2_3.ATT_UPDATE) fs_val = comp.as_fs() # Returns '?firefox?' uri_val = comp.as_uri_2_3() # Returns 'sp%01' wfn_val = comp.as_wfn() # Returns 'sp?' ``` -------------------------------- ### Instantiate CPE2_3 from formatted string Source: https://github.com/nilp0inter/cpe/blob/main/tests/testfile_cpe2_3.txt This example demonstrates creating a CPE2_3 object from a standard formatted CPE 2.3 string. This format is commonly used for representing CPE information. The input is a string, and the output is a CPE2_3 object. ```python from cpe.cpe2_3 import CPE2_3 fs = 'cpe:2.3:a:hp:insight_diagnostics:8.*:es?:*:-:-:x32:*:*' c = CPE2_3(fs) ``` -------------------------------- ### Get and Set CPE Values Source: https://github.com/nilp0inter/cpe/blob/main/tests/testfile_cpecomp2_3_wfn.txt Demonstrates how to retrieve the current value of a component and update it using the set_value method. ```python comp = CPEComponent2_3_WFN('"hp"', CPEComponent2_3.ATT_VENDOR) current = comp.get_value() comp.set_value('"new_val"', CPEComponent2_3.ATT_VENDOR) ``` -------------------------------- ### CPE URI Utility Methods Source: https://github.com/nilp0inter/cpe/blob/main/tests/testfile_cpe2_3_uri.txt Examples of using utility methods to get the length of a CPE, convert to string, and export as a URI 2.3 format. ```python uri = 'cpe:/a:hp:insight_diagnostics:7.4.0.1570::~~online~win2003~x64~' c = CPE2_3_URI(uri) length = len(c) string_rep = str(c) uri_format = c.as_uri_2_3() ``` -------------------------------- ### Create CPE Names with Automatic Version Detection - Python Source: https://context7.com/nilp0inter/cpe/llms.txt Demonstrates how to create CPE objects using the CPE class, which automatically detects the specification version (1.1, 2.2, 2.3) and supports various formats like URI, WFN, and Formatted String. It shows examples of explicit version specification and auto-detection. ```python from cpe import CPE # Auto-detect version 1.1 (operating system + application) cpe_v11 = CPE('cpe://redhat:enterprise_linux:3:as/apache:httpd:2.0.52') print(cpe_v11) # Output: CPE v1.1: cpe://redhat:enterprise_linux:3:as/apache:httpd:2.0.52 # Create version 2.2 with explicit version specification cpe_v22 = CPE('cpe:/o:redhat:enterprise_linux:4:update4', CPE.VERSION_2_2) print(cpe_v22) # Output: CPE v2.2: cpe:/o:redhat:enterprise_linux:4:update4 # Auto-detect version 2.3 URI style with packed edition cpe_uri = CPE('cpe:/a:hp:insight_diagnostics:8::~~online~win2003~x64~') print(cpe_uri) # Output: CPE v2.3 (URI): cpe:/a:hp:insight_diagnostics:8::~~online~win2003~x64~ # Auto-detect version 2.3 WFN style with wildcards cpe_wfn = CPE('wfn:[part="a", vendor="hp", product="?insight_diagnostics?", version="8\.*", target_sw=ANY, target_hw="x32"]') print(cpe_wfn) # Output: CPE v2.3 (WFN): wfn:[part="a", vendor="hp", product="?insight_diagnostics?", version="8\.*", target_sw=ANY, target_hw="x32"] # Auto-detect version 2.3 Formatted String style cpe_fs = CPE('cpe:2.3:h:cisco:ios:12.3:enterprise:*:*:*:*:*:*') print(cpe_fs) # Output: CPE v2.3 (FS): cpe:2.3:h:cisco:ios:12.3:enterprise:*:*:*:*:*:* ``` -------------------------------- ### Initialize CPE Objects Source: https://github.com/nilp0inter/cpe/blob/main/tests/testfile_cpe.txt Demonstrates how to instantiate a CPE object from a variety of CPE URI strings representing different hardware, software, and OS platforms. ```python from cpe import CPE # Example for virtual hardware c = CPE('cpe:/emc:vmware:esx:2.5') # Example for OS with service pack c = CPE('cpe://microsoft:windows:xp:pro:sp2') # Example for application server c = CPE('cpe:///jboss:jboss:4.0.4') ``` -------------------------------- ### Initialize and Inspect CPE Objects Source: https://github.com/nilp0inter/cpe/blob/main/tests/testfile_cpe.txt Demonstrates how to create a CPE object from a URI string and verify if it represents an operating system. ```python from cpe import CPE str = 'cpe://microsoft:windows:2000::sp4' c = CPE(str) print(c.is_operating_system()) ``` -------------------------------- ### Get CPE Edition (`get_edition`) Source: https://github.com/nilp0inter/cpe/blob/main/tests/testfile_cpe.txt Retrieves the edition information from a CPE string. ```APIDOC ## Get CPE Edition (`get_edition`) ### Description Extracts and returns the edition component(s) from a CPE string. ### Method `cpe_object.get_edition()` ### Endpoint N/A (Library Usage) ### Parameters N/A ### Request Example ```python # Example with edition str_win2k = 'cpe://microsoft:windows:2000::sp4' c_win2k = CPE(str_win2k) edition_win2k = c_win2k.get_edition() print(edition_win2k) # Example with multiple editions or empty edition str_complex = 'cpe://sun:sunos:5.9/bea:weblogic:8.1;mysql:server:5.0' c_complex = CPE(str_complex) edition_complex = c_complex.get_edition() print(edition_complex) ``` ### Response #### Success Response - **Output** (list of strings) - A list containing the edition information. Returns an empty list or list with empty strings if no specific edition is found. ### Response Example ``` ['sp4'] ['', '', ''] ``` ``` -------------------------------- ### Initialize and Validate CPE Components Source: https://github.com/nilp0inter/cpe/blob/main/tests/testfile_cpecomp1_1.txt Demonstrates how to instantiate a CPEComponent1_1 object with specific attributes and validate the input values against CPE standards. ```python comp1 = CPEComponent1_1('lin@x', CPEComponentSimple.ATT_PRODUCT) print(comp1._standard_value) # ['lin\\@x'] # Validation example att = CPEComponentSimple.ATT_LANGUAGE comp1 = CPEComponent1_1('es-ES', att) # Raises ValueError for invalid language formats # comp1 = CPEComponent1_1('esES', att) ``` -------------------------------- ### Initialize and Validate CPEComponent2_3_FS Source: https://github.com/nilp0inter/cpe/blob/main/tests/testfile_cpecomp2_3_fs.txt Demonstrates the instantiation of CPEComponent2_3_FS with attribute validation. It shows how invalid characters or unsupported attribute keys trigger ValueErrors. ```python val = 'bad||ven~~dor' comp1 = CPEComponent2_3_FS(val, CPEComponent2_3.ATT_VENDOR) # Raises ValueError: Invalid value of attribute 'vendor' val = 'microsoft' comp1 = CPEComponent2_3_FS(val, "badkey") # Raises ValueError: Invalid attribute 'badkey' ``` -------------------------------- ### Validate CPE Language Attribute Source: https://github.com/nilp0inter/cpe/blob/main/tests/testfile_cpecomp2_3_uri.txt Examples of validating the language attribute for CPE components, ensuring they conform to expected formats. ```python from cpe.comp.cpecomp2_3 import CPEComponent2_3 from cpe.comp.cpecomp2_3_uri import CPEComponent2_3_URI att = CPEComponent2_3.ATT_LANGUAGE val = 'es-ES' comp1 = CPEComponent2_3_URI(val, att) ``` -------------------------------- ### Initialize CPE Set and Perform Language Matching Source: https://github.com/nilp0inter/cpe/blob/main/tests/testfile_cpelang2_2.txt Demonstrates how to instantiate a CPESet2_2 object to collect CPE identifiers and utilize CPELanguage2_2 to verify if the set matches a specific document's language criteria. ```python s = CPESet2_2() s.append(c1) s.append(c2) s.append(c3) l = CPELanguage2_2(document) result = l.language_match(s) ``` -------------------------------- ### CPE Length Calculation with __len__ Source: https://github.com/nilp0inter/cpe/blob/main/tests/testfile_cpe2_3_fs.txt Demonstrates how to get the number of components in a CPE string using the len() function, which internally calls the __len__ method. ```python from cpe.cpe2_3_fs import CPE2_3_FS str_val = "cpe:2.3:a:hp:openview_network_manager:7.51:*:*:*:*:linux:*:*" c = CPE2_3_FS(str_val) print(len(c)) ``` -------------------------------- ### Initialize and Validate CPEComponent2_3_WFN Source: https://github.com/nilp0inter/cpe/blob/main/tests/testfile_cpecomp2_3_wfn.txt Demonstrates the initialization of the CPE component with various attribute parts and validation of input values. It shows how invalid values or attributes trigger a ValueError. ```python att = CPEComponent2_3.ATT_PART comp1 = CPEComponent2_3_WFN('"a"', att) comp2 = CPEComponent2_3_WFN('"?"', att) # This will raise ValueError # comp3 = CPEComponent2_3_WFN('"j"', att) ``` -------------------------------- ### Get CPE Component Value Source: https://github.com/nilp0inter/cpe/blob/main/tests/testfile_cpecomp2_3_uri.txt Tests the get_value method to retrieve the current value of a CPE component. This is a basic test to ensure the value is correctly stored and accessible. ```python >>> val = 'hp' >>> att = CPEComponent2_3.ATT_VENDOR >>> comp1 = CPEComponent2_3_URI(val, att) >>> comp1.get_value() 'hp' ``` -------------------------------- ### CPEComponent2_2: Get Component Value (get_value) Source: https://github.com/nilp0inter/cpe/blob/main/tests/testfile_cpecomp2_2.txt Demonstrates how to retrieve the current value of a CPE component using the get_value method. This method returns the stored string value associated with the component. ```python val = "hp" att = CPEComponentSimple.ATT_VENDOR comp1 = CPEComponent2_2(val, att) comp1.get_value() # 'hp' ``` -------------------------------- ### Initialize and Validate CPE Components Source: https://github.com/nilp0inter/cpe/blob/main/tests/testfile_cpecomp2_3_uri.txt Shows how to initialize CPEComponent2_3_URI objects and handle validation errors for invalid attributes. ```python from cpe.comp.cpecomp2_3 import CPEComponent2_3 from cpe.comp.cpecomp2_3_uri import CPEComponent2_3_URI # Valid initialization with escape value = 'foo%5cbar' comp = CPEComponent2_3_URI(value, CPEComponent2_3.ATT_VENDOR) # Invalid initialization example try: comp = CPEComponent2_3_URI('j', CPEComponent2_3.ATT_PART) except ValueError as e: print(e) ``` -------------------------------- ### Test CPEComponent2_3_FS _is_valid_value Method Source: https://github.com/nilp0inter/cpe/blob/main/tests/testfile_cpecomp2_3_fs.txt Tests the `_is_valid_value` method, which is a general validator for CPE component values across different attributes. It includes examples for product and update attributes with wildcard characters. ```python from __future__ import print_function from cpe.comp.cpecomp2_3_fs import CPEComponent2_3_FS from cpe.comp.cpecomp2_3 import CPEComponent2_3 # TEST val = '?firefox?' comp1 = CPEComponent2_3_FS(val, CPEComponent2_3.ATT_PRODUCT) # TEST val = 'sp2' comp1 = CPEComponent2_3_FS(val, CPEComponent2_3.ATT_UPDATE) ``` -------------------------------- ### Initialize and Validate CPE Components Source: https://github.com/nilp0inter/cpe/blob/main/tests/testfile_cpecomp2_3_wfn.txt Shows the initialization of CPEComponent2_3_WFN objects and how the library validates attribute values against expected formats, raising ValueErrors for invalid inputs. ```python from cpe.comp.cpecomp2_3_wfn import CPEComponent2_3_WFN from cpe.comp.cpecomp2_3 import CPEComponent2_3 # Valid initialization with escape value = r'"foo\\bar"' comp = CPEComponent2_3_WFN(value, CPEComponent2_3.ATT_VENDOR) # Invalid initialization example try: comp = CPEComponent2_3_WFN('"xp!vista"', 'version') except ValueError as e: print(e) ``` -------------------------------- ### CPELanguage2_2 Initialization Source: https://github.com/nilp0inter/cpe/blob/main/tests/testfile_cpelang2_2.txt Demonstrates how to initialize the CPELanguage2_2 class, either with an XML document string or a file path. ```APIDOC ## CPELanguage2_2 Initialization ### Description Initializes the CPELanguage2_2 object with a CPE language definition, which can be provided as an XML string or loaded from a file. ### Method `CPELanguage2_2(expression, isFile=False)` ### Parameters - **expression** (str) - Required - The XML document string or the path to the XML file. - **isFile** (bool) - Optional - Defaults to `False`. If `True`, `expression` is treated as a file path. ### Request Example ```python # Initialize with an XML string document_string = """...""" l = CPELanguage2_2(document_string) # Initialize with a file path l = CPELanguage2_2("path/to/your/file.xml", isFile=True) ``` ### Response - **CPELanguage2_2 object** - An instance of the CPELanguage2_2 class. ``` -------------------------------- ### Get and Set CPE Component Values Source: https://github.com/nilp0inter/cpe/blob/main/tests/testfile_cpecomp2_3_fs.txt Demonstrates retrieving and updating the internal value of a CPE component instance. The set_value method allows modifying the component's state while maintaining attribute context. ```python comp = CPEComponent2_3_FS('sp2', CPEComponent2_3.ATT_UPDATE) comp.set_value('?firefox?', CPEComponent2_3.ATT_UPDATE) print(comp.get_value()) # '?firefox?' ``` -------------------------------- ### Create and Manipulate CPE Names Source: https://github.com/nilp0inter/cpe/blob/main/docs/examples.rst Demonstrates how to instantiate CPE objects from various formats (URI, WFN, FS) and retrieve specific components or system types. ```python from cpe import CPE # Create CPEs from different versions and styles c11 = CPE('cpe://redhat:enterprise_linux:3:as/apache:httpd:2.0.52') c22 = CPE('cpe:/o:redhat:enterprise_linux:4:update4', CPE.VERSION_2_2) c23_uri = CPE('cpe:/a:hp:insight_diagnostics:8::~~online~win2003~x64~') c23_wfn = CPE('wfn:[part="a", vendor="hp", product="?insight_diagnostics?", version="8\\.*", target_sw=ANY, target_hw="x32"]') c23_fs = CPE('cpe:2.3:h:cisco:ios:12.3:enterprise:*:*:*:*:*:*') # Retrieve components and check system type product = c11.get_product() update = c22.get_update() is_hw = c23_fs.is_hardware() ``` -------------------------------- ### Instantiating CPE Objects Source: https://github.com/nilp0inter/cpe/blob/main/tests/testfile_cpe.txt Shows various ways to initialize a CPE object from a string, including handling hardware, OS, application parts, and logical operators like OR (!) and NOT (~). ```python from cpe import CPE # Basic instantiation c = CPE('cpe:/dell:inspiron:8500') # Using OR operator c_or = CPE('cpe://microsoft:windows:xp!vista') # Using NOT operator c_not = CPE('cpe://microsoft:windows:~xp') # Complex string with multiple elements c_complex = CPE('cpe://sun:sunos:5.9/bea:weblogic:8.1;mysql:server:5.0') ``` -------------------------------- ### Initialize CPEComponentEmpty Source: https://github.com/nilp0inter/cpe/blob/main/tests/testfile_cpecomp_empty.txt Demonstrates the instantiation of the CPEComponentEmpty class. This component represents an empty CPE component structure. ```python from cpe.comp.cpecomp_empty import CPEComponentEmpty c = CPEComponentEmpty() ``` -------------------------------- ### Test CPEComponent2_3_FS __init__ Method Source: https://github.com/nilp0inter/cpe/blob/main/tests/testfile_cpecomp2_3_fs.txt Tests the initialization of CPEComponent2_3_FS with various values and attributes. It includes tests for simple values, values with escape characters, and invalid values that should raise ValueErrors. ```python from __future__ import print_function from cpe.comp.cpecomp2_3 import CPEComponent2_3 from cpe.comp.cpecomp2_3_fs import CPEComponent2_3_FS # TEST: simple value value = "j" # comp = CPEComponent2_3_FS(value, CPEComponent2_3.ATT_PART) # doctest: +IGNORE_EXCEPTION_DETAIL # TEST: simple value with escape value = r"micro\$oft" comp = CPEComponent2_3_FS(value, CPEComponent2_3.ATT_VENDOR) # TEST: simple value without escape value = "xp!vista" # comp = CPEComponent2_3_FS(value, 'version') # doctest: +IGNORE_EXCEPTION_DETAIL ``` -------------------------------- ### Test CPEComponent __init__ Method Source: https://github.com/nilp0inter/cpe/blob/main/tests/testfile_cpecomp_notapplicable.txt Tests the initialization of the CPEComponentNotApplicable class. This snippet demonstrates the basic instantiation of the object. ```python from __future__ import print_function from cpe.comp.cpecomp_notapplicable import CPEComponentNotApplicable c = CPEComponentNotApplicable() ``` -------------------------------- ### Initialize CPELanguage2_2 with Expression Source: https://github.com/nilp0inter/cpe/blob/main/tests/testfile_cpelang2_2.txt Demonstrates initializing a CPELanguage2_2 object with either a direct XML string or a path to an XML file. This is used to parse CPE platform definitions. ```python from cpe.cpe2_2 import CPE2_2 from cpe.cpeset2_2 import CPESet2_2 from cpe.cpelang2_2 import CPELanguage2_2 document = '''Sun Solaris 5.8 or 5.9 with BEA Weblogic 8.1 installed''' c1 = CPE2_2('cpe:/o:sun:solaris:5.9:::en-us') c2 = CPE2_2('cpe:/a:bea:weblogic:8.1') s = CPESet2_2() s.append(c1) s.append(c2) l = CPELanguage2_2(document) ``` ```python from cpe.cpe2_2 import CPE2_2 from cpe.cpeset2_2 import CPESet2_2 from cpe.cpelang2_2 import CPELanguage2_2 document = "tests/expression2_2.xml" c1 = CPE2_2('cpe:/o:sun:solaris:5.9:::en-us') c2 = CPE2_2('cpe:/a:bea:weblogic:8.1') s = CPESet2_2() s.append(c1) s.append(c2) isFile = True l = CPELanguage2_2(document, isFile) ``` -------------------------------- ### CPE Creation and CPESet Population Source: https://github.com/nilp0inter/cpe/blob/main/docs/examples.rst Demonstrates how to create CPE objects and add them to a CPESet2_2. ```APIDOC ## CPE Creation and CPESet Population ### Description This section shows how to instantiate CPE objects and populate a CPESet2_2 with them. ### Method N/A (Python code examples) ### Endpoint N/A ### Parameters N/A ### Request Example ```python from cpe import CPE from cpe.cpeset2_2 import CPESet2_2 c1 = CPE('cpe:/o:sun:solaris:5.9:::en-us', CPE.VERSION_2_2) c2 = CPE('cpe:/a:bea:weblogic:8.1', CPE.VERSION_2_2) K = CPESet2_2() K.append(c1) K.append(c2) ``` ### Response N/A ``` -------------------------------- ### CPE2.3 WFN Matching with CPESet2_3 Source: https://github.com/nilp0inter/cpe/blob/main/tests/testfile_cpeset2_3.txt This snippet shows how to create CPE2.3 WFN objects from string representations and use a CPESet2_3 to perform name matching against another WFN. It illustrates scenarios with wildcards and the 'ANY' logical value. ```python from cpe import CPE2_3_WFN, CPESet2_3 # Example 1: Basic matching with wildcards and ANY wfn1 = r'wfn:[part="a", vendor="microsoft", product="internet_explorer", version="8.0.*", update="beta", edition=ANY]' wfn2 = r'wfn:[part="o", vendor="microsoft", product="windows", version="6.0*", update="sp2", edition=ANY]' wfn3 = 'wfn:[part="a", vendor="microsoft", product="internet_explorer"]' c1 = CPE2_3_WFN(wfn1) c2 = CPE2_3_WFN(wfn2) m = CPE2_3_WFN(wfn3) s = CPESet2_3() s.append(c1) s.append(c2) print(s.name_match(m)) # Example 2: Matching with version source and target (UNDEFINED) wfn1 = r'wfn:[part="a", vendor="microsoft", product="internet_explorer", version="8.0.*"]' wfn2 = r'wfn:[part="o", vendor="microsoft", product="windows", version="6.0*", update="sp2", edition=ANY]' wfn3 = 'wfn:[part="a", vendor="microsoft", product="internet_explorer"]' c1 = CPE2_3_WFN(wfn1) c2 = CPE2_3_WFN(wfn2) m = CPE2_3_WFN(wfn3) s = CPESet2_3() s.append(c1) s.append(c2) print(s.name_match(m)) # Example 3: Not matching scenario wfn1 = r'wfn:[part="a", vendor="microsoft", product="internet_explorer", version="8.0*", update="beta", edition=ANY]' wfn2 = r'wfn:[part="o", vendor="microsoft", product="windows", version="6.0*", update="sp2", edition=ANY]' wfn3 = 'wfn:[part="h", vendor="hp", product="compact"]' c1 = CPE2_3_WFN(wfn1) c2 = CPE2_3_WFN(wfn2) m = CPE2_3_WFN(wfn3) s = CPESet2_3() s.append(c1) s.append(c2) print(s.name_match(m)) ``` -------------------------------- ### CPE2_3_WFN Initialization and Validation Source: https://github.com/nilp0inter/cpe/blob/main/tests/testfile_cpe2_3_wfn.txt Covers the initialization of CPE2_3_WFN objects with various string inputs, including valid WFN formats, empty CPEs, and malformed inputs that trigger ValueErrors. This highlights the library's validation mechanisms. ```python from __future__ import print_function from cpe.cpe2_3_wfn import CPE2_3_WFN # Valid WFN wfn_valid = r'wfn:[part="o", vendor="acme", product="producto", version="1\.0", update="update2", edition="pro", language="en\-us"]' c_valid = CPE2_3_WFN(wfn_valid) # Empty CPE wfn_empty = 'wfn:[]' c_empty = CPE2_3_WFN(wfn_empty) # CPE with special characters and wildcards wfn_wild = r'wfn:[part="a", vendor="???hp*", product="?insight_diagnostics?", version="8\.*", sw_edition="?", target_sw=ANY, target_hw="x32"]' c_wild = CPE2_3_WFN(wfn_wild) # Invalid WFN prefix try: CPE2_3_WFN('baduri') except ValueError as e: print(e) # Invalid attribute name try: CPE2_3_WFN('wfn:[bad="hw"]') except ValueError as e: print(e) # Invalid value with unquoted question mark try: CPE2_3_WFN(r'wfn:[part="a", vendor="h??p", product="?insight_diagnostics?", version="8\.*", sw_edition="?", target_sw=ANY, target_hw="x32"]' ) except ValueError as e: print(e) ``` -------------------------------- ### CPEComponent2_2 Initialization and Validation Source: https://github.com/nilp0inter/cpe/blob/main/tests/testfile_cpecomp2_2.txt Tests the initialization of CPEComponent2_2 with different values and attributes, including simple values, negated values, and invalid combinations. It also covers validation for specific attributes like 'part', 'edition', and 'language'. ```python from __future__ import print_function from cpe.comp.cpecomp2_2 import CPEComponent2_2 from cpe.comp.cpecomp_simple import CPEComponentSimple from cpe.comp.cpecomp_anyvalue import CPEComponentAnyValue from cpe.comp.cpecomp_undefined import CPEComponentUndefined from cpe.comp.cpecomp_notapplicable import CPEComponentNotApplicable # TEST: simple value value = "j" comp = CPEComponent2_2(value, CPEComponentSimple.ATT_PART) # doctest: +IGNORE_EXCEPTION_DETAIL # Traceback (most recent call last): # ValueError: Invalid value of attribute 'part': 'j' # TEST: simple value value = "micros%ft" comp = CPEComponent2_2(value, CPEComponentSimple.ATT_VENDOR) # TEST: value with negation value = "~microsoft" comp = CPEComponent2_2(value, CPEComponentSimple.ATT_VENDOR) # TEST: NOT and OR operators cannot be together value = "xp!vista" comp = CPEComponent2_2(value, 'version') # doctest: +IGNORE_EXCEPTION_DETAIL # Traceback (most recent call last): # ValueError: Invalid value of attribute 'version': xp!vista >>> att = CPEComponentSimple.ATT_PART # TEST >>> val = 'a' >>> comp1 = CPEComponent2_2(val, att) # TEST >>> val = 'j' >>> comp1 = CPEComponent2_2(val, att) #doctest: +IGNORE_EXCEPTION_DETAIL # Traceback (most recent call last): # ValueError: Invalid value of attribute 'part': 'j' >>> att = CPEComponentSimple.ATT_EDITION # TEST: a simple value >>> val ='microsoft' >>> comp1 = CPEComponent2_2(val, att) # TEST: a simple value >>> val ='micro$oft' >>> comp1 = CPEComponent2_2(val, att) >>> att = CPEComponentSimple.ATT_LANGUAGE # - TEST >>> val = 'es-ES' >>> comp1 = CPEComponent2_2(val, att) # - TEST >>> val = 'es-noesES' >>> comp1 = CPEComponent2_2(val, att) #doctest: +IGNORE_EXCEPTION_DETAIL # Traceback (most recent call last): # ValueError: Invalid value of attribute 'language': es-noesES # - TEST >>> val = 'esES' >>> comp1 = CPEComponent2_2(val, att) #doctest: +IGNORE_EXCEPTION_DETAIL # Traceback (most recent call last): # ValueError: Invalid value of attribute 'language': esES ``` -------------------------------- ### Initialize CPEComponentUndefined Source: https://github.com/nilp0inter/cpe/blob/main/tests/testfile_cpecomp_undefined.txt Demonstrates the instantiation of the CPEComponentUndefined class. This class represents an undefined component in the CPE structure. ```python from cpe.comp.cpecomp_undefined import CPEComponentUndefined c = CPEComponentUndefined() ``` -------------------------------- ### CPEComponent2_2: Convert to Well-Formed Name (as_wfn) Source: https://github.com/nilp0inter/cpe/blob/main/tests/testfile_cpecomp2_2.txt Explains the as_wfn method, which converts CPE component values to the Well-Formed Name (WFN) format. Shows how specific characters, like '%' in version strings, are escaped. ```python val = 'sp2' comp1 = CPEComponent2_2(val, CPEComponentSimple.ATT_UPDATE) comp1.as_wfn() # 'sp2' val = 'linux%7' comp1 = CPEComponent2_2(val, CPEComponentSimple.ATT_VERSION) comp1.as_wfn() # 'linux\%7' ``` -------------------------------- ### Convert CPE2.3 FS to WFN Format Source: https://github.com/nilp0inter/cpe/blob/main/tests/testfile_cpe2_3_fs.txt Demonstrates the conversion of a CPE2.3 Filesystem (FS) string to its Well-Formed Name (WFN) representation using the `as_wfn()` method. This method is useful for standardizing CPE string formats for easier parsing and comparison. It handles various attributes within the CPE string. ```python from cpe import CPE2_3_FS fs3 = 'cpe:2.3:a:hp:insight_diagnostics:7.4.0.1570:*:*:*:online:win2003:x64:*' c3 = CPE2_3_FS(fs3) c3.as_wfn() 'wfn:[part="a", vendor="hp", product="insight_diagnostics", version="7\.4\.0\.1570", update=NA, edition=ANY, language=ANY, sw_edition="online", target_sw="win2003", target_hw="x64", other=ANY]' ``` ```python fs4 = r'cpe:2.3:a:foo\bar:big$money:2010:*:*:*:special:ipod_touch:80gb:*' c4 = CPE2_3_FS(fs4) c4.as_wfn() 'wfn:[part="a", vendor="foo\\bar", product="big\$money", version="2010", update=ANY, edition=ANY, language=ANY, sw_edition="special", target_sw="ipod_touch", target_hw="80gb", other=ANY]' ``` -------------------------------- ### Test CPEComponent1_1 Initialization and Validation Source: https://github.com/nilp0inter/cpe/blob/main/tests/testfile_cpecomp1_1.txt Tests the __init__ method, ensuring valid inputs are accepted and invalid inputs (like unsupported characters or combinations) raise ValueError. ```python try: comp = CPEComponent1_1('~xp!vista', 'version') except ValueError as e: print(e) # Handles invalid operator combinations ``` -------------------------------- ### Convert CPE2.3 WFN to FS Format Source: https://github.com/nilp0inter/cpe/blob/main/tests/testfile_cpe2_3_fs.txt Illustrates the conversion of a CPE2.3 string to its Filesystem (FS) format using the `as_fs()` method. This method is essential for generating CPE strings that are compatible with filesystem paths or specific application requirements. It preserves the original structure and values of the CPE entry. ```python from cpe import CPE2_3_FS fs1 = 'cpe:2.3:a:microsoft:internet_explorer:8.0.6001:beta:*:*:*:*:*:*' c1 = CPE2_3_FS(fs1) c1.as_fs() 'cpe:2.3:a:microsoft:internet_explorer:8.0.6001:beta:*:*:*:*:*:*' ``` -------------------------------- ### Initialize and Validate CPE URIs Source: https://github.com/nilp0inter/cpe/blob/main/tests/testfile_cpe2_3_uri.txt Demonstrates the initialization of CPE2_3_URI objects with various valid and invalid URI strings, including handling of special characters and malformed inputs. ```python try: c1 = CPE2_3_URI('cpe:/a:acme:product:1.0:update2:pro:en-us') c2 = CPE2_3_URI('cpe:/o:microsoft:windows_xp:::pro') c3 = CPE2_3_URI('cpe:/h:nvidia') except ValueError as e: print(f'Validation error: {e}') ``` -------------------------------- ### Convert CPE 2.2 URI to WFN and CPE 2.3 Formatted String Source: https://github.com/nilp0inter/cpe/blob/main/tests/testfile_cpe2_3_uri.txt Demonstrates how to initialize a CPE2_3_URI object from a legacy URI string and convert it into a Well-Formed Name (WFN) representation or a standard CPE 2.3 formatted string. This is useful for normalizing platform identifiers across different security tools. ```python from cpe import CPE2_3_URI # Initialize with legacy URI uri = 'cpe:/a:hp:openview_network_manager:7.51:-:~~~linux~~' c1 = CPE2_3_URI(uri) # Convert to WFN wfn = c1.as_wfn() # Convert to CPE 2.3 formatted string fs = c1.as_fs() print(f"WFN: {wfn}") print(f"FS: {fs}") ``` -------------------------------- ### Test CPEComponent1_1 String Representation Source: https://github.com/nilp0inter/cpe/blob/main/tests/testfile_cpecomp1_1.txt Demonstrates __repr__ and __str__ methods for displaying component values, including negated values. ```python comp = CPEComponent1_1('~microsoft', CPEComponentSimple.ATT_VENDOR) print(str(comp)) # Outputs: ~microsoft print(repr(comp)) # Outputs: NOT CPEComponent1_1(microsoft) ``` -------------------------------- ### CPE 2.3 Set Relations and Comparison (Python) Source: https://context7.com/nilp0inter/cpe/llms.txt Explains how to perform set-theoretic comparisons (SUPERSET, SUBSET, EQUAL, DISJOINT) between WFN CPE Names using the CPESet2_3 class. This enables complex logic for matching CPEs. ```python from cpe import CPE from cpe.cpeset2_3 import CPESet2_3 # Create two WFN CPE Names source = CPE('wfn:[part="a", vendor="microsoft", product="internet_explorer", version="8\.0\.*"]') target = CPE('wfn:[part="a", vendor="microsoft", product="internet_explorer", version="8\.0\.6001", update="beta"]') # Check set relations print(CPESet2_3.cpe_superset(source, target)) # Output: True (source is superset of target) print(CPESet2_3.cpe_subset(source, target)) # Output: False print(CPESet2_3.cpe_equal(source, target)) # Output: False print(CPESet2_3.cpe_disjoint(source, target)) # Output: False # Equal CPE Names cpe1 = CPE('wfn:[part="o", vendor="redhat", product="enterprise_linux", version="7"]') cpe2 = CPE('wfn:[part="o", vendor="redhat", product="enterprise_linux", version="7"]') print(CPESet2_3.cpe_equal(cpe1, cpe2)) # Output: True # Disjoint CPE Names (mutually exclusive) cpe_win = CPE('wfn:[part="o", vendor="microsoft", product="windows"]') cpe_linux = CPE('wfn:[part="o", vendor="redhat", product="linux"]') print(CPESet2_3.cpe_disjoint(cpe_win, cpe_linux)) # Output: True # Create a CPE 2.3 set and perform name matching cpe_set = CPESet2_3() cpe_set.append(CPE('cpe:2.3:a:hp:insight_diagnostics:7.4.0.1570:-:*:*:online:win2003:x64:*')) cpe_set.append(CPE('cpe:2.3:a:hp:openview_network_manager:7.51:-:*:*:*:linux:*:*')) candidate = CPE('wfn:[part="a", vendor="hp", product="insight_diagnostics", version="7\.4\.0\.1570"]') print(cpe_set.name_match(candidate)) # Output: True ``` -------------------------------- ### CPE Parsing and Initialization with CPE2_3_FS Source: https://github.com/nilp0inter/cpe/blob/main/tests/testfile_cpe2_3_fs.txt Demonstrates how to initialize and parse CPE strings using the CPE2_3_FS class. It covers valid and invalid CPE formats, including different types like OS and applications, and handling of special characters. ```python from cpe.cpe2_3_fs import CPE2_3_FS # Valid OS CPE fs = 'cpe:2.3:o:acme:producto:1.0:update2:pro:en-us:*:*:*:*' c = CPE2_3_FS(fs) # Valid Application CPE fs = 'cpe:2.3:a:hp:insight_diagnostics:7.4.0.1570:-:online:-:windows_2003:x64:*:*' c = CPE2_3_FS(fs) # Application CPE with wildcards fs = 'cpe:2.3:a:hp:insight_diagnostics:8.*:es?:*:-:-:x32:*:*' c = CPE2_3_FS(fs) # CPE with special characters fs = 'cpe:2.3:a:hp:insight_diagnostics:8.*:es?:*:-:-:x32~:*:*' c = CPE2_3_FS(fs) # CPE with unquoted question mark at the beginning fs = 'cpe:2.3:a:hp:insight_diagnostics:*8.*:?es?:*:-:-:x32~:*:*' c = CPE2_3_FS(fs) # CPE with contiguous unquoted question marks fs = 'cpe:2.3:a:???hp*:insight_diagnostics:8.*:es:*:-:-:x32~:*:*' c = CPE2_3_FS(fs) # CPE with unquoted question marks and asterisks fs = 'cpe:2.3:a:hp:insight_?diagnostics:8.*:es:*:-:-:x32~:*:*' c = CPE2_3_FS(fs) # Invalid CPE (bad URI) fs = 'baduri' c = CPE2_3_FS(fs) # Raises ValueError # Invalid CPE (malformed vendor value) fs = 'cpe:2.3:a:???hp**:insight_diagnostics:8.*:es:*:-:-:x32~:*:*' c = CPE2_3_FS(fs) # Raises ValueError # Invalid CPE (unquoted question mark in wrong place) uri = 'cpe:2.3:h:?*nvidia:*:*:*:*:*:*:*:*:*' c = CPE2_3_FS(uri) # Raises ValueError ``` -------------------------------- ### Represent CPEComponentEmpty as String Source: https://github.com/nilp0inter/cpe/blob/main/tests/testfile_cpecomp_empty.txt Illustrates how to retrieve the string representation and the printable format of a CPEComponentEmpty instance. ```python from cpe.comp.cpecomp_empty import CPEComponentEmpty c = CPEComponentEmpty() # __repr__ output print(repr(c)) # Output: CPEComponentEmpty() # __str__ output print(str(c)) # Output: ```