
本文详细介绍了如何使用正则表达式解析nmap扫描报告中包含可选主机名和ip地址的输出格式。通过构建一个健壮的正则表达式,结合go语言的`regexp`包和后处理逻辑,我们能够准确地从两种nmap输出格式中提取出主机名和ip地址,即使主机名缺失也能将ip地址作为主机名处理,避免了传统分组带来的冗余捕获问题。
在网络扫描中,Nmap是一个广泛使用的工具。其输出报告通常包含被扫描主机的各种信息,其中一个常见且具有挑战性的部分是主机名和IP地址的显示。Nmap的报告格式可能因是否能解析到主机名而有所不同:
包含主机名和IP地址的格式:
Nmap scan report for 2u4n32t-n4 (192.168.2.168)
在这种情况下,我们希望捕获 2u4n32t-n4 作为主机名,192.168.2.168 作为IP地址。
仅包含IP地址的格式(无主机名):
Nmap scan report for 192.168.2.1
在这种情况下,我们希望捕获 192.168.2.1 作为IP地址,并根据需求将其也作为主机名。
使用正则表达式解析这类混合格式的字符串时,常见的挑战是如何灵活地处理可选部分,并确保只捕获我们真正需要的信息,避免捕获多余的括号或空字符串。
最初尝试的正则表达式可能类似于: Nmap scan report for\s+([^[:space:]]+)(\s+\(([^[:space:]]+)\))?
这个正则表达式的意图是:
然而,这种方法在Go语言中会产生以下问题:
对于第一种格式 (Nmap scan report for 2u4n32t-n4 (192.168.2.168)): 会得到 [..., 2u4n32t-n4, (192.168.2.168), 192.168.2.168]。 其中,(192.168.2.168) 作为第二个捕获组出现,包含了我们不想要的括号。
对于第二种格式 (Nmap scan report for 192.168.2.1): 会得到 [..., 192.168.2.1, , ]。 其中,表示可选括号部分的捕获组和其内部的IP捕获组都为空,需要额外的判断。
这些冗余或不精确的捕获增加了后续数据处理的复杂性。
为了更精确地解决上述问题,我们可以结合使用命名捕获组、非捕获组和可选组,并辅以后续的编程逻辑来确定最终的主机名和IP地址。
我们采用以下正则表达式:
Nmap scan report fors+(?P<first_part>[^()s]+)(?:s+((?P<ip_in_parens>d{1,3}.d{1,3}.d{1,3}.d{1,3})))?让我们详细解析这个正则表达式的各个部分:
以下Go语言代码演示了如何使用上述正则表达式来解析Nmap输出并提取所需信息:
package main
import (
"fmt"
"regexp"
)
func parseNmapReport(line string) (hostname string, ipAddress string, err error) {
// 定义正则表达式,使用命名捕获组
// first_part: 捕获主机名或IP
// ip_in_parens: 捕获括号中的IP(如果存在)
re := regexp.MustCompile(`Nmap scan report fors+(?P<first_part>[^()s]+)(?:s+((?P<ip_in_parens>d{1,3}.d{1,3}.d{1,3}.d{1,3})))?`)
matches := re.FindStringSubmatch(line)
if matches == nil {
return "", "", fmt.Errorf("no match found for line: %s", line)
}
// 获取命名捕获组的索引
firstPartIndex := re.SubexpIndex("first_part")
ipInParensIndex := re.SubexpIndex("ip_in_parens")
// 提取捕获到的值
potentialHostnameOrIP := matches[firstPartIndex]
actualIPFromParens := matches[ipInParensIndex] // 如果没有匹配,此值为""
// 根据捕获结果进行逻辑判断
if actualIPFromParens != "" {
// 格式1: Hostname (IP)
hostname = potentialHostnameOrIP
ipAddress = actualIPFromParens
} else {
// 格式2: 只有IP
hostname = potentialHostnameOrIP // 此时 potentialHostnameOrIP 就是IP
ipAddress = potentialHostnameOrIP
}
return hostname, ipAddress, nil
}
func main() {
// 示例Nmap输出
line1 := "Nmap scan report for 2u4n32t-n4 (192.168.2.168)"
line2 := "Nmap scan report for 192.168.2.1"
line3 := "Nmap scan report for another-host (10.0.0.5)"
line4 := "Nmap scan report for 172.16.0.100"
line5 := "Nmap scan report for host-without-ip-in-parens" // 这是一个不符合预期的格式,会报错
fmt.Println("--- Parsing Nmap Report Lines ---")
// 测试第一种格式
h1, ip1, err1 := parseNmapReport(line1)
if err1 != nil {
fmt.Printf("Error parsing '%s': %v
", line1, err1)
} else {
fmt.Printf("Line: '%s'
Hostname: %s, IP Address: %s
", line1, h1, ip1)
}
// 测试第二种格式
h2, ip2, err2 := parseNmapReport(line2)
if err2 != nil {
fmt.Printf("Error parsing '%s': %v
", line2, err2)
} else {
fmt.Printf("Line: '%s'
Hostname: %s, IP Address: %s
", line2, h2, ip2)
}
// 更多测试
h3, ip3, err3 := parseNmapReport(line3)
if err3 != nil {
fmt.Printf("Error parsing '%s': %v
", line3, err3)
} else {
fmt.Printf("Line: '%s'
Hostname: %s, IP Address: %s
", line3, h3, ip3)
}
h4, ip4, err4 := parseNmapReport(line4)
if err4 != nil {
fmt.Printf("Error parsing '%s': %v
", line4, err4)
} else {
fmt.Printf("Line: '%s'
Hostname: %s, IP Address: %s
", line4, h4, ip4)
}
// 测试不匹配的行
h5, ip5, err5 := parseNmapReport(line5)
if err5 != nil {
fmt.Printf("Error parsing '%s': %v
", line5, err5)
} else {
fmt.Printf("Line: '%s'
Hostname: %s, IP Address: %s
", line5, h5, ip5)
}
}输出结果:
--- Parsing Nmap Report Lines --- Line: 'Nmap scan report for 2u4n32t-n4 (192.168.2.168)' Hostname: 2u4n32t-n4, IP Address: 192.168.2.168 Line: 'Nmap scan report for 192.168.2.1' Hostname: 192.168.2.1, IP Address: 192.168.2.1 Line: 'Nmap scan report for another-host (10.0.0.5)' Hostname: another-host, IP Address: 10.0.0.5 Line: 'Nmap scan report for 172.16.0.100' Hostname: 172.16.0.100, IP Address: 172.16.0.100 Error parsing 'Nmap scan report for host-without-ip-in-parens': no match found for line: Nmap scan report for host-without-ip-in-parens
这个解决方案利用了正则表达式中的几个高级特性:
以上就是高效解析Nmap扫描报告:处理可选主机名与IP地址的正则表达式教程的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号