扫描整个IP范围/子网以查找开放的端口80?

7
我需要一种快速高效的方式来扫描一个IP范围内是否开放了80端口。
例如,如果我想扫描OVH IP范围"46.105.0.0/16",我需要扫描该范围内的每个IP,并输出每个开放了80端口的IP列表。
46.105.0.51
46.105.0.72
46.105.0.91
46.105.0.7
46.105.0.15

我需要扫描多个子网,并且需要将结果输出到文件中。

编辑:我还在我的专用服务器上运行CentOS,并拥有1Gbit上行带宽。

3个回答

7

nmap to the rescue!:

nmap -Pn -p80 --open 46.105.0.0/16

使用以下命令将为您列出响应 tcp/80 并对应 nmap 输出的主机列表:

  • -Pn:跳过 Ping 测试,只关心开放的端口
  • --open:仅返回打开您所需端口的 IP 地址

通过少量的 awk(和 grep,因为我很懒,不是很擅长 awk - 一个 awk 大师能否替我解决这个问题?),您可以获得 IP 列表:

nmap -Pn -p80 --open 46.105.0.0/16 | grep 46.105 | awk '{print  $5}NF == 6{print $6}'

nmap还有特定的选项,可以将输出结果保存至指定格式的文件中。或者你可以使用>将结果导出至文件:

nmap -Pn -p80 --open 46.105.0.0/16 | grep 46.105 | awk '{print  $5}NF == 6{print $6}' > output.txt

我在使用你的命令时遇到了错误。-P参数存在非法参数,请使用-P0、-PI、-PB、-PE、-PM、-PP、-PA、-PU、-PT或-PT80(或任何您想要的TCP探测目标端口号)。退出!我还需要将其输出到一个.txt文件中。 - user3385815
好的,将“-Pn”替换为“-P0”。将输出到文件中则只需在命令末尾添加“> output.txt”即可。 - admdrew
2
尝试运行以下命令:nmap -P0 -p80 --open 46.105.0.0/16 | grep 46.105 | awk '{print $5}NF == 6{print $6}' > output.txt - admdrew
好的,非常感谢admdrew。到目前为止工作得很好,但它导出的格式是这样的:vpsxxxxx.ovh.net(46.105.x.x)上的有趣端口: PORT STATE SERVICE 80/tcp filtered http我该如何摆脱所有这些无用的垃圾,只保留IP地址?对grep和nmap一窍不通。谢谢。 - user3385815
你使用的nmap版本是什么(nmap --version)? --open标志应该只返回“开放”的主机,而不是“过滤”或“关闭”的。 - admdrew

1

对于任何读到这篇文章但无法访问nmap的人,以下是一种快速而基本的扫描网络上80端口的方法。此脚本所需的唯一工具是ipcalc,该工具很可能已经可用。

#!/bin/bash
# easier to end the script if signal is caught
trap exit 1 2 3 4 5 6 7 8
# define a function that emulate netcat by opening a port to an ip via file descriptor
netcat() {
        exec 20<>/dev/tcp/${1}/${2}
}

# using ipcalc, get the nwtork address and the broadcast address and make both $NETWORK and $BROADCAST available to the script
export $(ipcalc -b -n $1)

# Convert the NETWORK and BROADCAST from dotted notation to hex
printf -v startHexIP "%0.2x%0.2x%0.2x%0.2x" $(tr '\.' ' ' <<< $NETWORK)
printf -v endHexIP "%0.2x%0.2x%0.2x%0.2x" $(tr '\.' ' ' <<< $BROADCAST)

# computations are done in decimal so we need decimal representation of the BROADCAST address to control the list of IP addresses
printf -v endDecIP "%d" 0x${endHexIP}

# legitimate IP addresses start from NETWORK ADDRESS + 1 and end at BROADCAST ADDRESS - 1
for((i=$(( 0x$startHexIP + 1 )); i<$endDecIP; i++)); do
        # $i is in decimal. we need to convert to hex
        printf -v hexI "%8.8x" $i
        # convert hex to dotted notation.
        printf -v ip "%d.%d.%d.%d" 0x${hexI:0:2} 0x${hexI:2:2} 0x${hexI:4:2} 0x${hexI:6:2}
        if (netcat $ip 80 > /dev/null 2>&1); then
                echo $ip
        fi
done

脚本可以只传递一个参数 <network>/<prefix> 来执行。
例如。
./script 192.168.1.5/23

0

这可以用只有4行脚本来完成。

创建名为“scanall”(仅为示例)的脚本文件,并复制下面的代码,它将扫描/24网络(1-254)。

# SCRIPT NAME: scanall
# USAGE      : scanall SUBNET PORT # put up to 3rd octet
#
# EXAMPLE    : scanall 192.168.1 80
#                       subnet  port

END=254
for i in $(seq 1 $END); do
    netcat -vz -w1 $1.$i $2;
done

# script will put 4th octet starting from 1 to 254 each line of netcat test.
# edit END to 128 if you want ip range 1~128 to be checked which is /25

如果我想扫描 192.168.1.0/24 找到开放的 22 端口,只需运行(在 chmod +x 后)

bash:~$ scanall 192.168.1 22

然后结果将会显示

192.168.1.1 [192.168.1.2] 22 (ssh): open
192.168.1.2 [192.168.1.2] 22 (ssh): Connection refused
.
.
192.168.1.183 [192.168.1.183] 22 (ssh): Operation timed out
192.168.1.184 [192.168.1.184] 22 (ssh): Connection refused 
192.168.1.185 [192.168.1.185] 22 (ssh) open                
192.168.1.186 [192.168.1.186] 22 (ssh): Operation timed out
192.168.1.187 [192.168.1.187] 22 (ssh): Operation timed out
192.168.1.188 [192.168.1.188] 22 (ssh): Operation timed out
.
.
192.168.1.254 [192.168.1.254] 22 (ssh): Operation timed out

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接