【发布时间】:2020-08-13 16:48:17
【问题描述】:
我正在编写一个运行 Nmap 网络扫描的 bash 脚本。在此之后,需要检查扫描并且需要提取相关位。
我需要从完成的扫描中提取 IP、MAC 和操作系统。问题是 Nmap 并不总是从扫描中获取操作系统,因此不会将其放入结果中。我需要在最终结果中关联 IP、MAC 和操作系统。
这是一个测试扫描的例子:
Nmap scan report for 192.168.0.1
Host is up (0.0029s latency).
Not shown: 990 closed ports
PORT STATE SERVICE
PORT# STATE XXXXXXX
MAC Address: MA:CA:DR:ES:S0:03 (Unknown)
Device type: general purpose
Running: Linux 2.6.X|3.X
OS CPE: cpe:/o:linux:linux_kernel:2.6 cpe:/o:linux:linux_kernel:3
OS details: Linux 2.6.32 - 3.13
Network Distance: 1 hop
Nmap scan report for 192.168.0.102
Host is up (0.0044s latency).
Not shown: 999 closed ports
PORT STATE SERVICE
PORT# STATE XXXXXXX
MAC Address: MA:CA:DR:ES:S0:02 (Sony Mobile Communications AB)
Too many fingerprints match this host to give specific OS details
Network Distance: 1 hop
Nmap scan report for 192.168.0.104
Host is up (0.00024s latency).
Not shown: 995 filtered ports
PORT STATE SERVICE
PORT# STATE XXXXXX
MAC Address: MA:CA:DR:ES:S0:01 (Micro-star Intl)
Warning: OSScan results may be unreliable because we could not find at least 1 open and 1 closed port
Device type: general purpose
Running (JUST GUESSING): Microsoft Windows 2008 (91%)
OS CPE: cpe:/o:microsoft:windows_server_2008::sp1 cpe:/o:microsoft:windows_server_2008:r2
Aggressive OS guesses: Microsoft Windows Server 2008 SP1 or Windows Server 2008 R2 (91%)
No exact OS matches for host (test conditions non-ideal).
Network Distance: 1 hop
还要注意上面示例中的最后一个是如何找不到操作系统的,在这种情况下需要攻击性猜测
最终结果需要是一个文本文件,其内容如下:
192.168.0.1 - MA:CA:DR:ES:S0:03 - Linux 2.6.32 - 3.13
192.168.0.102 - MA:CA:DR:ES:S0:02 - Not found
192.168.0.104 - MA:CA:DR:ES:S0:01 - Microsoft Windows Server 2008 SP1 or Windows Server 2008 R2
我做了一些研究,但找不到任何解释我如何将 IP 与 mac 地址和文本块中的操作系统相关联的东西。
我有以下命令可用于简单扫描,其中 IP 和 Mac 地址彼此相邻
while read line; do
Mac="$(grep -oE '[A-Z0-9]{2}:[A-Z0-9]{2}:[A-Z0-9]{2}:[A-Z0-9]{2}:[A-Z0-9]{2}:[A-Z0-9]{2}' <<< "$line")"
ip="$(grep -oE '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' <<< "$line")"
echo -e $ip'\t-\t '$Mac >>/path/to/results.txt
done </path/to/testscan.txt
我对 bash 脚本相当陌生,如果我遗漏了一些明显的东西,我深表歉意。
任何有兴趣的人都可以使用 nmap 命令:
nmap -O --osscan-guess 192.168.0.0/24 -oN /path/to/testscan.txt
对不起,文字墙,我认为信息越多越好!
【问题讨论】:
-
如果您将您的 grep 解决方案与下面的 AWK 解决方案进行比较,您就会明白正则表达式不再适用于多行模式匹配。