用于机器和机器人的NMAP匹配线的正则表达式。仅限OS

时间:2013-09-21 00:30:13

标签: regex perl sed awk nmap

使用Linux,我正在寻找过滤掉只有XP匹配的机器的数据并删除连续的" nmap扫描报告"线。

Nmap scan report for 13.93.27.138
445/tcp open  microsoft-ds Microsoft Windows XP microsoft-ds
|   OS: Windows XP (Windows 2000 LAN Manager)
Nmap scan report for 13.93.27.139
Nmap scan report for 13.93.27.140
Nmap scan report for 13.93.27.141
Nmap scan report for 13.93.27.143
445/tcp   open  microsoft-ds Microsoft Windows XP microsoft-ds
Aggressive OS guesses: Microsoft Windows 2003 Small Business Server SP1 (91%), Microsoft Windows Server 2003 SP2 (91%), Microsoft Windows Server 2003 SP1 or SP2 (86%), Microsoft Windows XP Professional SP2 (French) (85%)
|   OS: Windows XP (Windows 2000 LAN Manager)
Nmap scan report for 13.93.27.144
Nmap scan report for 13.93.27.147
445/tcp   open  microsoft-ds Microsoft Windows XP microsoft-ds
Aggressive OS guesses: Microsoft Windows 2003 Small Business Server SP1 (91%), Microsoft Windows Server 2003 SP2 (90%), Microsoft Windows XP Professional SP2 (French) (85%), Microsoft Windows Server 2003 SP1 or SP2 (85%)
|   OS: Windows XP (Windows 2000 LAN Manager)
Nmap scan report for 13.93.27.148
OS: Windows XP (Windows 2000 LAN Manager)
Nmap scan report for 13.93.27.191
445/tcp   open  microsoft-ds Microsoft Windows XP microsoft-ds
Aggressive OS guesses: Microsoft Windows 2003 Small Business Server SP1 (91%), Microsoft Windows Server 2003 SP2 (91%), Microsoft Windows Server 2003 SP1 or SP2 (86%), Microsoft Windows XP Professional SP2 (French) (85%)
|   OS: Windows XP (Windows 2000 LAN Manager)
Nmap scan report for 13.93.27.192
OS details: Microsoft Windows 2000 SP2 - SP4, Windows XP SP2 - SP3, or Windows Server 2003 SP0 - SP2

寻找仅显示的报告:

Nmap scan report for 13.93.27.138
OS: Windows XP (Windows 2000 LAN Manager)
Nmap scan report for 13.93.27.147
OS: Windows XP (Windows 2000 LAN Manager)

我的想法是使用awk,grep,sed或perl:m / ^ Nmap。 \ n。!(^ Nmap)。* / m 寻找以Nmap开头的行,并在换行后复制不是以Nmap开头的下一行,例如" OS:Windows XP"。然后重新开始...

感谢您的帮助: - )

3 个答案:

答案 0 :(得分:3)

使用awk

awk -F "OS: " '/^Nmap/ {a=$0} /OS:/ {print a"\n"FS$2}' file
Nmap scan report for 13.93.27.138
OS: Windows XP (Windows 2000 LAN Manager)
Nmap scan report for 13.93.27.143
OS: Windows XP (Windows 2000 LAN Manager)
Nmap scan report for 13.93.27.147
OS: Windows XP (Windows 2000 LAN Manager)
Nmap scan report for 13.93.27.148
OS: Windows XP (Windows 2000 LAN Manager)
Nmap scan report for 13.93.27.191
OS: Windows XP (Windows 2000 LAN Manager)

答案 1 :(得分:1)

当问题是搜索Nmap输出时,答案是始终,“使用XML output format”。这是因为Nmap的常规输出可以在不同版本之间更改,并且不是为机器输入而构建的。 zyou可以让Nmap使用-oX-oA选项发出XML。

你已经对输出进行了大量过滤,但我可以从“|”中看出来在行的开头,您想要的输出来自NSE script而不是OS检测引擎。具体来说,这是smb-os-discovery脚本的输出。知道了这一点,我们就可以使用XML解析器来查找//script[@id='smb-os-discovery']属性包含字符串“OS:Windows XP”的每个元素output。以下是使用xmlstarlet

的方法
xmlstarlet sel -t -m "//script[@id='smb-os-discovery' and contains(@output, 'OS: Windows XP')" -v "ancestor::host/address[@addrtype='ipv4']/@addr" -n scan-output.xml

您可以使用每种语言中的许多XML解析库执行类似的操作。 Python,Perl和Ruby都有专门为Nmap的XML输出设计的好的解析器。

编辑:由于您只希望smb-os-detection检测到操作系统,因此只需运行此脚本并跳过操作系统指纹识别步骤,即可节省扫描时间。这是一个像这样的快速扫描的例子:

nmap -p 445 --script smb-os-detection -oA smb-scan-%y%m%d 192.0.2.0/24

答案 2 :(得分:0)

这应该可以解决问题:

 perl -ne 'if (/nmap/i) { $nmap = $_ }; if (/(OS:.*XP.*)/) { print $nmap,$1,"\n"; }' report