Search code examples
grepnmapscanning

grep nmap output , print select lines


i just finshed scan a host on my internal network ... and it's vuln to smb i need to grep the select lines from the scan result

    root@kali:~# nmap -p445 --script smb-vuln-* 192.168.99.50
Starting Nmap 7.80 ( https://nmap.org ) at 2020-01-13 00:21 EET
Nmap scan report for 192.168.99.50
Host is up (0.19s latency).

PORT    STATE SERVICE
445/tcp open  microsoft-ds
MAC Address: 00:50:56:BA:7F:57 (VMware)

Host script results:
| smb-vuln-ms08-067: 
|   VULNERABLE:
|   Microsoft Windows system vulnerable to remote code execution (MS08-067)
|     State: VULNERABLE
|     IDs:  CVE:CVE-2008-4250
|           The Server service in Microsoft Windows 2000 SP4, XP SP2 and SP3, Server 2003 SP1 and SP2,
|           Vista Gold and SP1, Server 2008, and 7 Pre-Beta allows remote attackers to execute arbitrary
|           code via a crafted RPC request that triggers the overflow during path canonicalization.
|           
|     Disclosure date: 2008-10-23
|     References:
|       https://technet.microsoft.com/en-us/library/security/ms08-067.aspx
|_      https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2008-4250
|_smb-vuln-ms10-054: false
|_smb-vuln-ms10-061: false
| smb-vuln-ms17-010: 
|   VULNERABLE:
|   Remote Code Execution vulnerability in Microsoft SMBv1 servers (ms17-010)
|     State: VULNERABLE
|     IDs:  CVE:CVE-2017-0143
|     Risk factor: HIGH
|       A critical remote code execution vulnerability exists in Microsoft SMBv1
|        servers (ms17-010).
|           
|     Disclosure date: 2017-03-14
|     References:
|       https://technet.microsoft.com/en-us/library/security/ms17-010.aspx
|       https://blogs.technet.microsoft.com/msrc/2017/05/12/customer-guidance-for-wannacrypt-attacks/
|_      https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2017-0143

Nmap done: 1 IP address (1 host up) scanned in 7.11 seconds

i just want to grep the name of testes vuln and the status : like this

| smb-vuln-ms08-067: 
|   VULNERABLE:
| smb-vuln-ms10-054: false
|_smb-vuln-ms10-061: false
| smb-vuln-ms17-010: 
|   VULNERABLE:

Solution

  • Filter the output of nmap through next pipeline:

    sed -e '/^..smb-vuln-[^:]*: false/p' -e '/^..smb-vuln-[^:]*: *$/{n;p}' -e d
    

    Explanation:

    1. the first sed command prints the lines beginning with a specific pattern and ending with ": false"
    2. the second command prints the lines beginning with the same pattern but with nothing after the ":" and also the next line; {n;p} means add next line and print
    3. the third command d removes everything else from the output