bash - 如何按“块”文本拆分解析的文本?
问题描述
我正在编写一个运行 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
对不起,文字墙,我认为信息越多越好!
解决方案
这很容易解析awk
:
BEGIN {os_details="Not found"}
/^Nmap scan report/ {target=$5}
/^MAC Address/ {mac_address=$3}
/^OS details/ {os_details=substr($0, length("OS details: "))}
/^Aggressive OS guesses/ {
os_details=substr($0, length("Aggressive OS guesses: "))
}
# This matches the blank lines between hosts
/^$/ {
printf "%s - %s - %s\n", target, mac_address, os_details
target=""
mac_address=""
os_details="Not found"
}
END {
printf "%s - %s - %s\n", target, mac_address, os_details
}
在您的示例数据上运行它可以让您:
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 (91%)
我必须纠正我认为您的示例数据中的错误...我在MAC Address
此处删除了该行之前的空白行:
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)
推荐阅读
- python - Keras 有没有办法立即停止训练?
- android - 如何获取调用者活动的上下文
- python - 如何根据数据框中其他列的值填充列?
- apache-kafka - 卡夫卡 - 融合分布有什么区别
- python - Python如何通过特定列和额外的行循环序列匹配数据帧
- dialogflow-es - Dialogflow 代理 git 版本控制
- python - 在 python 中的 matplotlib 中绘制特定格式的时间戳
- javascript - 获取值等于某物的对象的键名
- powershell - PowerShell WebClient.DownloadFile() 在下载几秒钟后停止
- react-native - backgroundColor 在 react-native-elements 的 Input 中隐藏 clearButtonMode 的 X