Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Regexp is wrong for port extract #10

Open
JonasBVS opened this issue May 23, 2017 · 2 comments
Open

Regexp is wrong for port extract #10

JonasBVS opened this issue May 23, 2017 · 2 comments

Comments

@JonasBVS
Copy link

JonasBVS commented May 23, 2017

openvas_to_report/libs/data/parsed_data.py

this pattern should extraxt correct information (without description)
i'am not sure if this is the correct line that it searches, but using the patten below you can extract the port and protocol information:
<port>22/tcp<host>xxx.xxx.xxx.xxx</host><severity>5.3</severity><threat>Medium</threat></port>

change number of groups to 3(line 100), and remove return of description(line 107)

pattern: ([\d]+)(/)([\w]+)

It fills out the information in the worksheets :)
image

@lilloxxx
Copy link

Thanks a lot for the tip!

I'm not a python expert so to simplify

image

@TheGroundZero
Copy link

TheGroundZero commented Sep 14, 2018

@lilloxxx Note that port numbers in the report can now also have the form of general/icmp next to tcp/443.

Below is my version of the code.
Just like you, I decided to drop the description and go for the number and protocol only.

I'm still doubting whether I should make the port number 0 or None when there's no explicit port number.

@staticmethod
def string2port(info):
    """
    Extract port number, protocol and description from an string.

    ..note:
        Raises value error if information can't be processed.

    # >>> p=Port.string2port("2000/tcp")
    # >>> print p.number
      2000
    # >>> print p.proto
      "tcp"

    # >>> p=Port.string2port("general/icmp")
    # >>> print p.number
      0
    # >>> print p.proto
      "icmp"

    :param info: raw string with port information
    :type info: basestring

    :return: Port instance
    :rtype: Port

    :raises: ValueError
    """
    if not isinstance(info, str):
        raise TypeError("Expected basestring, got '{}' instead".format(type(info)))

    regex_nr = re.search("([\d]+)(/)([\w]+)", info)
    regex_general = re.search("(general)(/)([\w]+)", info)

    if regex_nr and len(regex_nr.groups()) == 3:
        number = int(regex_nr.group(1))
        protocol = regex_nr.group(3)
    elif regex_general and len(regex_general.groups()) == 3:
        number = 0
        protocol = regex_general.group(3)
    else:
        raise ValueError("Can't parse input string")

    return Port(number, protocol)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants