T3SEC —–第十三讲:python-nmap

{% meting "2111364820" "tencent" "playlist" "theme:#FF4081" "mode:circulation" "mutex:true" "listmaxheight:200px" "preload:auto" "autoplay" %}

Nmap–扫描网上电脑开放的连接端口

可用于评估系统漏洞

案例前提条件:

  • python基础 pip
  • TCP/IP
  • Python 3.7.x
  • python-nmap
    • portscanner() ---扫描
    • portscannerhostdict() --- 返回结果

实现内网端口扫描工具

#准备工作:
#windows: nmap.exe  
#python-nmap    :pip  install python-nmap
#目标: 实现指定ip/ip范围 的端口扫描神器'
import sys
import nmap


# www.baidu.com/127.0.0.1/192.168.0-255.1-255/  192.168.1.1/24
#1-8080/80/1443,8080,443
#-sV 
#scan(self,hosts='',ports=None,arguments='-sV')

scan_row=[]
input_data=input("Please input hosts and ports:")#12.169.1.1 8080,443
scan_row =input_data.split(" ")
#输入是否满足条件
if len(scan_row)!=2:
    print("Input error.Example \"192.168.1.1 80,443\"")
    sys.exit(0)

hosts=scan_row[0]#主机
port=scan_row[1]#端口

#创建一个扫描对象
try:
    nm=nmap.PortScanner()
except Exception as e:
    print("Nmap not found")
    sys.exit(0)


nm.scan(hosts=hosts,arguments='-v -sS -p'+port)

#遍历扫描主机
for host in nm.all_hosts():
    print('-'*40)
    print('Host:{}({})'.format(host,nm[host].hostname()))
    print('State:{}'.format(nm[host].state()))#输出主机状态  up/down

    for proto in nm[host].all_protocols():#遍历扫描所有协议
        print("-"*20)
        print("protocol:{}".format(proto))

        #扫描所有端口所对应的状态
        lport=nm[host][proto].keys()
        for port in lport:
            print("port:{}\tstate:{}".format(port,nm[host][proto][port]['state']))



Q.E.D.