<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: How to write regex to extract Port numbers and status from nmap output? in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/How-to-write-regex-to-extract-Port-numbers-and-status-from-nmap/m-p/198735#M57511</link>
    <description>&lt;P&gt;Assuming that for each port mentioned in log we have "portid=", "state=" and "service name=" then this might work&lt;/P&gt;

&lt;PRE&gt;
your base search
| rex field=_raw "addr=\"(?&amp;lt; ip&amp;gt;[^\"]+)" max_match=10 
| rex field=_raw "portid=\"(?&amp;lt; port&amp;gt;[^\"]+)" max_match=10
| rex field=_raw "state=\"(?&amp;lt; state&amp;gt;[^\"]+)" max_match=10 
| rex field=_raw "service name=\"(?&amp;lt; service&amp;gt;[^\"]+)" max_match=10
| eval allFields= if(match(state, "open"), mvzip(port, service), "")
| table ip, allFields
&lt;/PRE&gt;

&lt;P&gt;&lt;STRONG&gt;&lt;EM&gt;Note:&lt;/EM&gt;&lt;/STRONG&gt; Due to formatting issue, each field &lt;EM&gt;**&amp;lt; ip&amp;gt;, &amp;lt; port&amp;gt;, &amp;lt; state&amp;gt;, &amp;lt; service_name&amp;gt; *&lt;/EM&gt;* has a space between "&amp;lt;" and first letter of field name. Remove those spaces when runnign the query&lt;BR /&gt;
Keep &lt;STRONG&gt;&lt;EM&gt;max_match&lt;/EM&gt;&lt;/STRONG&gt; according to your need&lt;/P&gt;</description>
    <pubDate>Fri, 07 Oct 2016 07:31:35 GMT</pubDate>
    <dc:creator>gokadroid</dc:creator>
    <dc:date>2016-10-07T07:31:35Z</dc:date>
    <item>
      <title>How to write regex to extract Port numbers and status from nmap output?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-write-regex-to-extract-Port-numbers-and-status-from-nmap/m-p/198733#M57509</link>
      <description>&lt;P&gt;Hi&lt;/P&gt;

&lt;P&gt;this took at least three days of my life but I still couldn't handle it, please, I need help&lt;/P&gt;

&lt;P&gt;I have a data file includes output of nmap traces (couple of traces on different times on each IP) in our subnet like this:&lt;/P&gt;

&lt;P&gt;host starttime="1402600202" endtime="1402601288" status state="up" reason="echo-reply" address addr="128.150.254.114" addrtype="ipv4" hostnames hostname name="sunvirt01.srv.be" type="PTR" hostnames ports extraports state="filtered" count="985" extrareasons reason="no-responses" count="985" extraports: port protocol="tcp" portid="20" state state="closed" reason="reset" reason_ttl="63" service name="ftp-data" method="table" conf="3" port port protocol="tcp" portid="21" state state="open" reason="syn-ack" reason_ttl="63"/ service name="ftp" product="ProFTPD" method="probed" conf="10" cpe cpe:/a:proftpd:proftpd cpe service port  port protocol="tcp" portid="22" port protocol="tcp" portid="222" state state="open" reason="syn-ack" reason_ttl="63" service name="tcpwrapped" method="probed" ... &lt;BR /&gt;
(os)portused state="open" proto="tcp" portid="21"/ portused state="closed" proto="tcp" portid="20"/ osmatch name="Linux 3.0" accuracy="99" line="47080" (...)&lt;/P&gt;

&lt;P&gt;what I need is a table to show the IP address and "open" Ports on that machine beside the service(s) are using that open port, something like this(e.g):&lt;/P&gt;

&lt;P&gt;------ -----IP  -----  ------- Ports -----  ----- Service&lt;/P&gt;

&lt;P&gt;128.150.254.114 -------21------------------ ftp&lt;/P&gt;

&lt;P&gt;(if there are more open ports o one host they have to be listed below "Ports" by their services)&lt;/P&gt;

&lt;P&gt;I'm using this regex: (?i) portid="(?P&lt;FIELDNAME&gt;[^"]+) to extract Port numbers but unfortunately in some cases I face with values like 21/ or duplicate values like this:&lt;/FIELDNAME&gt;&lt;/P&gt;

&lt;P&gt;21 --- ftp&lt;/P&gt;

&lt;P&gt;80 --- http&lt;/P&gt;

&lt;P&gt;1718&lt;/P&gt;

&lt;P&gt;21&lt;/P&gt;

&lt;P&gt;or like this one:&lt;/P&gt;

&lt;P&gt;20 --- &lt;/P&gt;

&lt;P&gt;443 --- http&lt;/P&gt;

&lt;P&gt;20/&lt;/P&gt;

&lt;P&gt;21 ---- ftp&lt;/P&gt;

&lt;P&gt;21&lt;/P&gt;

&lt;P&gt;( I mean I need the duplicate values to be removed (or not detected) or values with "/" at the end which I have no idea about them!) can you give me some hint or the right regex I have to used to extract only open ports (once without considering duplicates at the rest)? &lt;/P&gt;

&lt;P&gt;I've even tried this one but still doesn't work properly... :&lt;/P&gt;

&lt;P&gt;rex max_match=10 "(?i) portid=\"(?P&lt;FIELDNAME&gt;[^\"]+)"|rename FIELDNAME as OpenPorts|rex max_match=10 "(?im)^&amp;lt;\w+&amp;gt;&amp;lt;\w+\s+\w+=\"(?P&lt;FIELDNAME&gt;[^\"]+)" |rename FIELDNAME as status&lt;/FIELDNAME&gt;&lt;/FIELDNAME&gt;&lt;/P&gt;

&lt;P&gt;I think this is related to part after (os) or at least tell me what's the meanin of that part that repeats the result? or how can I ignore that part in my search?&lt;/P&gt;

&lt;P&gt;I'm waiting for any kind of help,&lt;/P&gt;

&lt;P&gt;Thanks in advance&lt;/P&gt;</description>
      <pubDate>Mon, 28 Sep 2020 17:26:52 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-write-regex-to-extract-Port-numbers-and-status-from-nmap/m-p/198733#M57509</guid>
      <dc:creator>sina_shafaei</dc:creator>
      <dc:date>2020-09-28T17:26:52Z</dc:date>
    </item>
    <item>
      <title>Re: How to write regex to extract Port numbers and status from nmap output?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-write-regex-to-extract-Port-numbers-and-status-from-nmap/m-p/198734#M57510</link>
      <description>&lt;P&gt;Check if this works (?i)portid=\"(?P&lt;FIELDNAME&gt;.*?\d+)\W&lt;/FIELDNAME&gt;&lt;/P&gt;</description>
      <pubDate>Sat, 30 Aug 2014 12:03:08 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-write-regex-to-extract-Port-numbers-and-status-from-nmap/m-p/198734#M57510</guid>
      <dc:creator>pradeepkumarg</dc:creator>
      <dc:date>2014-08-30T12:03:08Z</dc:date>
    </item>
    <item>
      <title>Re: How to write regex to extract Port numbers and status from nmap output?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-write-regex-to-extract-Port-numbers-and-status-from-nmap/m-p/198735#M57511</link>
      <description>&lt;P&gt;Assuming that for each port mentioned in log we have "portid=", "state=" and "service name=" then this might work&lt;/P&gt;

&lt;PRE&gt;
your base search
| rex field=_raw "addr=\"(?&amp;lt; ip&amp;gt;[^\"]+)" max_match=10 
| rex field=_raw "portid=\"(?&amp;lt; port&amp;gt;[^\"]+)" max_match=10
| rex field=_raw "state=\"(?&amp;lt; state&amp;gt;[^\"]+)" max_match=10 
| rex field=_raw "service name=\"(?&amp;lt; service&amp;gt;[^\"]+)" max_match=10
| eval allFields= if(match(state, "open"), mvzip(port, service), "")
| table ip, allFields
&lt;/PRE&gt;

&lt;P&gt;&lt;STRONG&gt;&lt;EM&gt;Note:&lt;/EM&gt;&lt;/STRONG&gt; Due to formatting issue, each field &lt;EM&gt;**&amp;lt; ip&amp;gt;, &amp;lt; port&amp;gt;, &amp;lt; state&amp;gt;, &amp;lt; service_name&amp;gt; *&lt;/EM&gt;* has a space between "&amp;lt;" and first letter of field name. Remove those spaces when runnign the query&lt;BR /&gt;
Keep &lt;STRONG&gt;&lt;EM&gt;max_match&lt;/EM&gt;&lt;/STRONG&gt; according to your need&lt;/P&gt;</description>
      <pubDate>Fri, 07 Oct 2016 07:31:35 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-write-regex-to-extract-Port-numbers-and-status-from-nmap/m-p/198735#M57511</guid>
      <dc:creator>gokadroid</dc:creator>
      <dc:date>2016-10-07T07:31:35Z</dc:date>
    </item>
    <item>
      <title>Re: How to write regex to extract Port numbers and status from nmap output?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-write-regex-to-extract-Port-numbers-and-status-from-nmap/m-p/198736#M57512</link>
      <description>&lt;P&gt;This is how I parse .gnmap files.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;index="gnmap" 
| eval ports=split(_raw,",")
| mvexpand ports
| Search ports!="*host*"
| rex field=_raw "Host:\s(?&amp;lt;dest_ip&amp;gt;\d+.\d+.\d+.\d+)\s+\((?&amp;lt;fqdn&amp;gt;.*)\)"
| rex field=ports "(?&amp;lt;port&amp;gt;\d+)\/+(?&amp;lt;status&amp;gt;\w+)\/+(?&amp;lt;proto&amp;gt;\w+)\/+(?&amp;lt;desc&amp;gt;\w+|\/)"
| rex field=_raw "OS:\s(?&amp;lt;os&amp;gt;\w+)"
| table dest_ip fqdn port status proto desc os
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 21 Sep 2017 17:27:24 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-write-regex-to-extract-Port-numbers-and-status-from-nmap/m-p/198736#M57512</guid>
      <dc:creator>AnthonyPhipps</dc:creator>
      <dc:date>2017-09-21T17:27:24Z</dc:date>
    </item>
    <item>
      <title>Re: How to write regex to extract Port numbers and status from nmap output?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-write-regex-to-extract-Port-numbers-and-status-from-nmap/m-p/198737#M57513</link>
      <description>&lt;P&gt;what does this line do ?&lt;BR /&gt;
| foreach %* [eval &amp;lt;&amp;gt;=mvindex(&amp;lt;&amp;gt;,0)]&lt;/P&gt;</description>
      <pubDate>Thu, 21 Sep 2017 21:37:07 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-write-regex-to-extract-Port-numbers-and-status-from-nmap/m-p/198737#M57513</guid>
      <dc:creator>amorgado</dc:creator>
      <dc:date>2017-09-21T21:37:07Z</dc:date>
    </item>
    <item>
      <title>Re: How to write regex to extract Port numbers and status from nmap output?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-write-regex-to-extract-Port-numbers-and-status-from-nmap/m-p/198738#M57514</link>
      <description>&lt;P&gt;Extraneous, I will remove it.&lt;/P&gt;</description>
      <pubDate>Fri, 22 Sep 2017 11:53:34 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-write-regex-to-extract-Port-numbers-and-status-from-nmap/m-p/198738#M57514</guid>
      <dc:creator>AnthonyPhipps</dc:creator>
      <dc:date>2017-09-22T11:53:34Z</dc:date>
    </item>
  </channel>
</rss>

