【问题标题】:Formating nmap results to get http server格式化 nmap 结果以获取 http 服务器
【发布时间】:2023-01-26 09:15:10
【问题描述】:

我正在尝试获取 nmap 扫描结果,确定 http 端口(http、https、http-alt ...)并捕获它们的 ip 和端口,以便自动执行 Web 应用程序扫描。

我有 grepable 格式的 nmap 结果。使用 grep 删除任何不包含字符串“http”的行。但我现在不确定我该如何继续。

Host: 127.0.0.1 (localhost)     Ports: 3390/open/tcp//dsc///, 5901/open/tcp//vnc-1///, 8000/open/tcp//http-alt///       Ignored State: closed (65532)

这是我目前的结果。由此我可以通过使用 cut 命令并获取第二个字段来获取打开了 http 服务器的主机的 IP。这是我解决问题的第一部分。

但是现在我正在寻找一种方法来只获取(从上面的例子中)

8000/open/tcp//http-alt///

(注意:我不希望只针对特定情况使用它,使用 cut -f 3 -d "," 将适用于这种情况,但如果 http 服务器位于第一个字段中,它将不起作用。)

之后我可以使用 cut 命令获取端口,然后将其添加到带有 ip 的文件中,从而得到

127.0.0.1:8000

谁能建议一个好的方法来做到这一点?

我的简单 bash 脚本代码用于对所有端口进行基本扫描,然后根据找到的开放端口进行更高级的扫描。下一步和目标是通过对已识别的 Web 应用程序进行目录扫描和 niktoo 扫描来自动扫描 Web 应用程序

#!/bin/bash

echo "Welcome to the quick lil tool. This runs a basic nmap scan, collects open ports and does a more advanced scan. reducing the time needed"
echo -e "\nUsage: ./getPorts.sh [Hosts]\n"
if [ $# -eq 0 ]
then
        echo "No argument specified. Usage: ./getPorts.sh [Host or host file]"
        exit 1
fi

if [[ "$EUID" -ne 0 ]]; then
        echo "Not running as root"
        exit 1
fi

nmap -iL $1 -p- -oA results

#Replace input file with gnmap scan, It will generate a list of all open ports
cat results.gnmap |awk -F'[/ ]' '{h=$2; for(i=1;i<=NF;i++){if($i=="open"){print h,":",$(i-1)}}}'| awk -F ':' '{print $2}' | sed -z 's/\n/,/g;s/,$/\n/' >> ports.list

#more advanced nmap scan
ports=$(cat ports.list)
echo $ports
nmap -p $ports -sC -sV -iL $1

编辑:找到了一种方法。不知道为什么我如此专注于为此使用 gnmap 格式,如果我使用常规的 .nmap 格式。我可以用 http in 简单地 grep 该行并使用 cut 来获取第一个字段。

(cat results.nmap | grep 'http' | cut -d "/" -f 1)

EDIT2:我意识到我第一次编辑中提到的方法在处理多个结果时并不是最佳的,因为我有一个来自 .nmap 的 IP 列表和一个来自 .gnmap 的端口列表。我使用单个文件找到了解决问题的好方法。见下文:

#!/bin/bash




httpalt=$(cat test.gnmap | awk '/\/http-alt\// {for(i=5;i<=NF;i++)if($i~"/open/.+/http-alt/"){sub("/.*","",$i); print "http://"$2":"$i}}')
    if [ -z "$httpalt" ]
    then
          echo "No http-alt servers found"
    else
          echo "http-alt servers found"
          echo $httpalt
          printf "\n"
    fi
    
http=$(cat test.gnmap | awk '/\/http\// {for(i=5;i<=NF;i++)if($i~"/open/.+/http/"){sub("/.*","",$i);print "http://"$2":"$i}}')
    if [ -z "$http" ]
    then
          echo "No http servers found"
    else
          echo "http servers found"
          
          echo $http
          printf "\n"
          
    fi
    
https=$(cat test.gnmap | awk '/\/https\// {for(i=5;i<=NF;i++)if($i~"/open/.+/https/"){sub("/.*","",$i); print "https://"$2":"$i}}')
    if [ -z "$https" ]
    then
          echo "No http servers found"
    else
          echo "https servers found"
          echo $https
          printf "\n"
    fi


echo ----
printf "All ip:webapps \n"

webserver=$(echo "$httpalt $http $https" | sed -e 's/\s\+/,/g'|sed -z 's/\n/,/g;s/,$/\n/')


if [[ ${webserver::1} == "," ]]
then
  webserver="${webserver#?}"
else
  echo 0; fi

for webservers in $webserver; do
    echo $webservers
done

echo $https

https=$(echo "$https" | sed -e 's/\s\+/,/g'|sed -z 's/\n/,/g;s/,$/\n/')
echo $https

mkdir https
mkdir ./https/nikto/
mkdir ./https/dirb/
for onehttps in ${https//,/ }
do
    echo "Performing Dirb and nikto for https"
    dirb $onehttps > ./https/dirb/https_dirb
    nikto -url $onehttps > ./https/nikto/https_nitko
done

mkdir http
mkdir ./http/nikto
mkdir ./http/dirb/
for onehttp in ${http//,/ }
do
    echo $onehttp
    echo "Performing Dirb for http"
    dirb $onehttp >> ./http/dirb/http_dirb
    nikto -url $onehttp >> ./http/nikto/http_nikto
done

mkdir httpalt
mkdir httpalt/nikto/
mkdir httpalt/dirb/
for onehttpalt in ${httpalt//,/ }
do
    echo "Performing Dirb for http-alt"
    dirb $onehttpalt >> ./httpalt/dirb/httpalt_dirb
    nikto -url $onehttpalt >> ./httpalt/nikto/httpalt_nikto
done

这将检查任何 http、https 和 http-alt 服务器,将它们存储在一个变量中,检查重复项并删除开头的任何尾随逗号,它远非完美,但现在是一个很好的解决方案!

【问题讨论】:

  • 欢迎来到 SO,请将您尝试过的代码添加为您在问题中的努力,这在 SO 上受到高度鼓励,谢谢。
  • @RavinderSingh13 到目前为止添加了我的代码,只是想知道一种方法来获得我不满意的结果,他们将在我的 man bash 脚本中实现它

标签: bash grep cut nmap


【解决方案1】:
printf "Host: 127.0.0.1 (localhost)     Ports: 3390/open/tcp//dsc///, 5901/open/tcp//vnc-1///, 8000/open/tcp//http-alt///       Ignored State: closed (65532)" > file

cat file | tr -s ' ' | tr ',' '
' | sed s'@^ @@g' > f2
string=$(sed -n '3p' f2 | cut -d' ' -f1)

只有水平搜索是困难的;垂直很容易。你可以从任何你喜欢的文本中得到任何字符串,只要你能在它自己的行中得到字符串,然后确定你需要打印哪一行。

如果您完全依赖水平搜索,则只需要复杂的正则表达式。在几乎所有情况下,只要您的子字符串在其自己的行上,cut 就可以带您完成剩下的工作。

【讨论】:

    【解决方案2】:

    只想在 GitHub 上分享一个出色的开源工具,可以用来轻松解析 NMAP XML 文件。

    https://github.com/honze-net/nmap-query-xml

    我使用一些 python 代码从 nmap xml 文件中提取 http/https URL。

    # pip3 install python-libnmap
    from libnmap.parser import NmapParser
    
    def extract_http_urls_from_nmap_xml(file): 
      
         try: 
             report = NmapParser.parse_fromfile(file) 
             urls = [] 
         except IOError: 
             print("Error: Nmap XML file %s not found. Quitting!" % file) 
             sys.exit(1) 
      
         for host in report.hosts: 
             for service in host.services: 
                 filtered_services = "http,http-alt,http-mgmt,http-proxy,http-rpc-epmap,https,https-alt,https-wmap,http-wmap,httpx" 
                 if (service.state == "open") and (service.service in filtered_services.split(",")): 
                     line = "{service}{s}://{hostname}:{port}" 
                     line = line.replace("{xmlfile}", nmap_file) 
                     line = line.replace("{hostname}", host.address if not host.hostnames else host.hostnames[0]) # TODO: Fix naive code. 
                     line = line.replace("{hostnames}", host.address if not host.hostnames else ", ".join(list(set(host.hostnames)))) # TODO: Fix naive code. 
                     line = line.replace("{ip}", host.address) 
                     line = line.replace("{service}", service.service) 
                     line = line.replace("{s}", "s" if service.tunnel == "ssl" else "") 
                     line = line.replace("{protocol}", service.protocol) 
                     line = line.replace("{port}", str(service.port)) 
                     line = line.replace("{state}", str(service.state)) 
                     line = line.replace("-alt", "") 
                     line = line.replace("-mgmt", "") 
                     line = line.replace("-proxy", "") 
                     line = line.replace("-rpc-epmap", "") 
                     line = line.replace("-wmap", "") 
                     line = line.replace("httpx", "http") 
                     urls.append(line) 
      
         return list(dict.fromkeys(urls))
    

    【讨论】:

      猜你喜欢
      • 2014-06-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多