python-nmap使用方法(python3),python-nmappython3,nmap是一个知名的


nmap是一个知名的端口扫描工具,超级好用,可调的参数也多(但需懂得网络相关知识,否则就别费精神研究参数了)

一般在linux上使用,当然,它也有windows的版本,但不在这里展开。


关于nmap的用法,可以参考在线手册 https://nmap.org/book/man-briefoptions.html

python-nmap 实际是在python里面调用底层的nmap,所以第一步是先安装系统的nmap,再装python-nmap


以下是安装步骤

本文使用的系统是centos 6,python的版本是3.5.2


1)安装系统的nmap

# yum install nmap -y

......

Package 2:nmap-5.51-6.el6.x86_64 already installed and latest version

Nothing to do

由于我已经装过了,所以提示已安装

验证一下

# nmap -v

Starting Nmap 5.51...


2)安装python-nmap

[[email protected] ~]# pip3 install python-nmap

Requirement already satisfied: python-nmap in ....

同样已经装过


以下是python3中使用(https://xael.org/pages/python-nmap-en.html)

最基本的用法,也是串行的方式,请自行去上面的网站上查询

这里说的是异步方式,要使用python来进行扫描,我相信大多是批量扫描,否则没必要用python,直接在命令行下执行nmap

python-nmap有两种异步的使用方式,根据源码来看,实际上就是多进程。


第一种:

# 先定义一个回调方法,参数必须是两个,名字随便取,这里用的是host和scan_result

importnmapdefcallback_result(host,scan_result):print('------------------')print(host,scan_result)#异步Scannernm=nmap.PortScannerAsync()#扫描参数,第一个是扫描对象,可以是单个IP、网段、IP-IP诸多写法,详细自己查手册或者百度#第二个是ports参数,同样写法多样#第三个arguments参数,这个就有讲究了,假如不写这个参数,默认会带一个-sV,然后你扫描一个ip都能等到天荒地老,关于-sV的含义在文后给出作为参考。在这里,我们给一个-sS,或者可以给个空白字符串也是可以的#第四个是指定回调函数nm.scan('192.168.1.0/24',ports='22,80,8888',arguments='-sS',callback=callback_result)#以下是必须写的,否则你会看到一运行就退出,没有任何的结果whilenm.still_scanning():print("sleep")nm.wait(2)



第二种:

importnmapnm=nmap.PortScannerYield()forresultinnm.scan('192.168.1.0/24',ports='22,80,8888,8080,443',arguments="-sS"):print(result)

这种调用方式简单很多,也是推荐的写法。得到的结果

('192.168.1.1',{'scan':{'192.168.1.1':{'tcp':{80:{'extrainfo':'','state':'filtered','name':'http','product':'','reason':'no-response','conf':'3','cpe':'','version':''},8080:{'extrainfo':'','state':'filtered','name':'http-proxy','product':'','reason':'no-response','conf':'3','cpe':'','version':''},443:{'extrainfo':'','state':'closed','name':'https','product':'','reason':'reset','conf':'3','cpe':'','version':''},22:{'extrainfo':'','state':'closed','name':'ssh','product':'','reason':'reset','conf':'3','cpe':'','version':''},8888:{'extrainfo':'','state':'open','name':'sun-answerbook','product':'','reason':'syn-ack','conf':'3','cpe':'','version':''}},'vendor':{},'status':{'state':'up','reason':'timestamp-reply'},'addresses':{'ipv4':'192.168.1.1'},'hostnames':[{'type':'','name':''}]}},'nmap':{'scanstats':{'uphosts':'1','downhosts':'0','elapsed':'1.29','totalhosts':'1','timestr':'WedJun1317:25:282018'},'command_line':'nmap-oX--p22,80,8888,8080,443-sS192.168.1.1','scaninfo':{'tcp':{'services':'22,80,443,8080,8888','method':'syn'}}}})


如何分析使用result,各位自己发挥吧, 它其实就是个元组,内嵌了字典


SERVICE/VERSION DETECTION:

-sV: Probe open ports to determine service/version info# 探测端口的服务、版本信息


python-nmap使用方法(python3)

评论关闭