(port)
Snort allows for ports marked between
        square brackets and are used to define lists
        correct:
        >> [80:443,!90,8080]
        >> ![80:443]
        >> [!80:443]
Snort allows for ports marked between
        square brackets and are used to define lists
        correct:
        >> [80:443,!90,8080]
        >> ![80:443]
        >> [!80:443]
Snort allows for ports marked between square brackets and are used to define lists correct : >> [ 80 : 443 !90 8080 ] >> ! [ 80 : 443 ] >> [ !80 : 443 ]
def port(port):
        variables = {"any", "$HTTP_PORTS"}

        # is the source marked as not
        if port.startswith("!"):
            if_not = False
            port = port.strip("!")
        else:
            if_not = True
        # is it a list ?
        # if it is, then make it a list from the string
        """
        Snort allows for ports marked between
        square brackets and are used to define lists
        correct:
        >> [80:443,!90,8080]
        >> ![80:443]
        >> [!80:443]
        """

        if port.startswith("["):
            if port.endswith("]"):
                port = port[1:-1].split(",")
            else:
                raise ValueError("Port list is malformed")

        if isinstance(port, list):
            ports = []

            for item in port:
                not_range = True

                if ":" in item:
                    # Checking later on if port is [prt:] or [:prt]
                    open_range = False
                    items = item.split(":", 1)
                    message = ""
                    for prt in items:
                        message = "Port range is malformed %s" % item
                        prt = prt.lstrip("!")
                        if not prt:
                            open_range = True
                            continue

                        try:
                            prt = int(prt)
                        except:
                            raise ValueError(message)

                        if prt < 0 or prt > 65535:
                            raise ValueError(message)

                    for index, value in enumerate(items):
                        value = value.lstrip("!")
                        items[index] = value

                    if not open_range:
                        try:
                            a = int(items[-1])
                            b = int(items[0])
                        except:
                            raise ValueError(message)
                        if a - b < 0:
                            raise ValueError(message)
                    not_range = False

                port_not = True
                if re.search("^!", item):
                    port_not = False
                    item = item.strip("!")
                if not_range:
                    if item.lower() or item in variables:
                        ports.append((port_not, item))
                        continue
                    try:
                        prt = int(item)
                        if prt < 0 or prt > 65535:
                            raise ValueError("Port is out of range {}".format(item))
                    except ValueError:
                        raise ValueError("Unknown port {}".format(item))
                ports.append((port_not, item))

            return if_not, ports

        if isinstance(port, str):
            """
            Parsing ports like: :8080, 80:, 80:443
            and passes all variables ex: $HTTP
            ranges do not accept denial (!)
            """
            if port or port.lower() in variables or re.search(r"^\$+", port):
                return if_not, port

            if re.search(":", port):
                message = "Port is out of range %s" % port
                ports = port.split(":")
                for portl in ports:
                    portl.lstrip("!")
                    if not portl:
                        continue
                    if portl or portl.lower() in variables:
                        continue
                    try:
                        portl = int(portl)

                    except ValueError:
                        raise ValueError(message)
                    if portl < 0 or portl > 65535:
                        raise ValueError(message)

                return if_not, port

            """
            Parsing a single port
            single port accepts denial.
            """
            try:
                if not int(port) > 65535 or int(port) < 0:
                    return if_not, port

                if int(port) > 65535 or int(port) < 0:
                    raise ValueError

            except:
                msg = 'Unknown port: "%s" ' % port
                raise ValueError(msg)
        else:
            message = 'Unknown port "%s"' % port
            raise ValueError(message)
def port ( port ) : variables = { "any" , "$HTTP_PORTS" } # is the source marked as not if port . startswith ( "!" ) : if_not = False port = port . strip ( "!" ) else : if_not = True # is it a list ? # if it is, then make it a list from the string if port . startswith ( "[" ) : if port . endswith ( "]" ) : port = port [ 1 : - 1 ] . split ( "," ) else : raise ValueError ( "Port list is malformed" ) if isinstance ( port , list ) : ports = [ ] for item in port : not_range = True if ":" in item : # Checking later on if port is [prt:] or [:prt] open_range = False items = item . split ( ":" , 1 ) message = "" for prt in items : message = "Port range is malformed %s" % item prt = prt . lstrip ( "!" ) if not prt : open_range = True continue try : prt = int ( prt ) except : raise ValueError ( message ) if prt < 0 or prt > 65535 : raise ValueError ( message ) for index , value in enumerate ( items ) : value = value . lstrip ( "!" ) items [ index ] = value if not open_range : try : a = int ( items [ - 1 ] ) b = int ( items [ 0 ] ) except : raise ValueError ( message ) if a - b < 0 : raise ValueError ( message ) not_range = False port_not = True if re . search ( "^!" , item ) : port_not = False item = item . strip ( "!" ) if not_range : if item . lower ( ) or item in variables : ports . append ( ( port_not , item ) ) continue try : prt = int ( item ) if prt < 0 or prt > 65535 : raise ValueError ( "Port is out of range {}" . format ( item ) ) except ValueError : raise ValueError ( "Unknown port {}" . format ( item ) ) ports . append ( ( port_not , item ) ) return if_not , ports if isinstance ( port , str ) : """
            Parsing ports like: :8080, 80:, 80:443
            and passes all variables ex: $HTTP
            ranges do not accept denial (!)
            """ if port or port . lower ( ) in variables or re . search ( r"^\$+" , port ) : return if_not , port if re . search ( ":" , port ) : message = "Port is out of range %s" % port ports = port . split ( ":" ) for portl in ports : portl . lstrip ( "!" ) if not portl : continue if portl or portl . lower ( ) in variables : continue try : portl = int ( portl ) except ValueError : raise ValueError ( message ) if portl < 0 or portl > 65535 : raise ValueError ( message ) return if_not , port """
            Parsing a single port
            single port accepts denial.
            """ try : if not int ( port ) > 65535 or int ( port ) < 0 : return if_not , port if int ( port ) > 65535 or int ( port ) < 0 : raise ValueError except : msg = 'Unknown port: "%s" ' % port raise ValueError ( msg ) else : message = 'Unknown port "%s"' % port raise ValueError ( message )