python-libnmap运用知识

安装

pip install python-libnmap

使用

libnmap.process

该模块的目的是使lib用户能够启动和控制nmap扫描。该模块将根据构造函数中提供的指定参数触发nmap命令

用法:

from libnmap.process import NmapProcess

nm = NmapProcess("scanme.nmap.org", options="-sV")
rc = nm.run()

if nm.rc == 0:
    print nm.stdout
else:
    print nm.stderr
    
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE nmaprun>
<?xml-stylesheet href="file:///usr/bin/../share/nmap/nmap.xsl" type="text/xsl"?>
<!-- Nmap 7.01 scan initiated Mon Mar  5 10:52:34 2018 as: /usr/bin/nmap -oX - -vvv -&#45;stats-every 1s -sV scanme.nmap.org -->
<nmaprun scanner="nmap" args="/usr/bin/nmap -oX - -vvv -&#45;stats-every 1s -sV scanme.nmap.org" start="1520218354" startstr="Mon Mar  5 10:52:34 2018" version="7.01" xmloutputversion="1.04">
<scaninfo type="connect" protocol="tcp" numservices="1000" services="1,3-4,6-7,9,13,17,19-26,30,32-33,37,42-43,49,53,70,79-85,88-90,99-100,106,109-111,113,119,125,135,139,143-144,146,161,163,179,199,211-212,222,254-256,259,264,280,301,306,311,340,366,389,406-407,416-417,425,427,443-445,458,464-465,481,497,500,512-515,524,541,543-545,548,554-555,563,587,593,616-617,625,631,636,646,648,666-668,683,687,691,700,705,711,714,720,722,726,749,765,777,783,787,800-801,808,843,873,880,888,898,900-903,911-912,981,987,990,992-993,995,999-1002,1007,1009-1011,5952,5959-5963,5987-5989,5998-6007,6009,6025,6059,6100-6101,6106,6112,,9220,9290,"/>
<verbose level="3"/>
<debugging level="0"/>
<taskbegin task="Ping Scan" time="1520218355"/>
<taskprogress task="Ping Scan" time="1520218355" percent="100.00" remaining="0" etc="1520218355"/>
<taskend task="Ping Scan" time="1520218355" extrainfo="1 total hosts"/>
<taskbegin task="Parallel DNS resolution of 1 host." time="1520218355"/>
<taskend task="Parallel DNS resolution of 1 host." time="1520218356"/>
<taskbegin task="Connect Scan" time="1520218356"/>
<taskprogress task="Connect Scan" time="1520218356" percent="2.05" remaining="0" etc="1520218356"/>
<taskprogress task="Connect Scan" time="1520218357" percent="6.10" remaining="16" etc="1520218372"/>
<taskprogress task="Connect Scan" time="1520218358" percent="12.63" remaining="14" etc="1520218372"/>
<taskprogress task="Connect Scan" time="1520218359" percent="18.32" remaining="14" etc="1520218372"/>
<taskprogress task="Connect Scan" time="1520218364" percent="39.72" remaining="13" etc="1520218376"/>
<taskprogress task="Connect Scan" time="1520218365" percent="47.08" remaining="11" etc="1520218375"/>
<taskprogress task="Connect Scan" time="1520218366" percent="52.08" remaining="10" etc="1520218375"/>
<taskprogress task="Connect Scan" time="1520218367" percent="59.00" remaining="8" etc="1520218375"/>
<taskprogress task="Connect Scan" time="1520218368" percent="71.66" remaining="5" etc="1520218373"/>
<taskprogress task="Connect Scan" time="1520218369" percent="82.58" remaining="3" etc="1520218372"/>
<taskprogress task="Connect Scan" time="1520218370" percent="90.92" remaining="2" etc="1520218371"/>
<taskprogress task="Connect Scan" time="1520218371" percent="99.34" remaining="1" etc="1520218371"/>
<taskend task="Connect Scan" time="1520218371" extrainfo="1000 total ports"/>
<taskbegin task="Service scan" time="1520218371"/>
<taskprogress task="Service scan" time="1520218372" percent="75.00" remaining="1" etc="1520218372"/>
<taskprogress task="Service scan" time="1520218378" percent="75.00" remaining="3" etc="1520218380"/>
<taskprogress task="Service scan" time="1520218378" percent="100.00" remaining="0" etc="1520218378"/>
<taskend task="Service scan" time="1520218378" extrainfo="4 services on 1 host"/>
<taskbegin task="NSE" time="1520218378"/>
<taskprogress task="NSE" time="1520218379" percent="98.57" remaining="1" etc="1520218379"/>
<taskend task="NSE" time="1520218379"/>
<host starttime="1520218355" endtime="1520218379"><status state="up" reason="conn-refused" reason_ttl="0"/>
<address addr="45.33.32.156" addrtype="ipv4"/>
<hostnames>
<hostname name="scanme.nmap.org" type="user"/>
<hostname name="scanme.nmap.org" type="PTR"/>
</hostnames>
<ports><extraports state="closed" count="991">
<extrareasons reason="conn-refused" count="991"/>
</extraports>
<port protocol="tcp" portid="22"><state state="open" reason="syn-ack" reason_ttl="0"/><service name="ssh" product="OpenSSH" version="6.6.1p1 Ubuntu 2ubuntu2.10" extrainfo="Ubuntu Linux; protocol 2.0" ostype="Linux" method="probed" conf="10"><cpe>cpe:/a:openbsd:openssh:6.6.1p1</cpe><cpe>cpe:/o:linux:linux_kernel</cpe></service></port>
<port protocol="tcp" portid="80"><state state="open" reason="syn-ack" reason_ttl="0"/><service name="http" product="Apache httpd" version="2.4.7" extrainfo="(Ubuntu)" method="probed" conf="10"><cpe>cpe:/a:apache:http_server:2.4.7</cpe></service></port>
<port protocol="tcp" portid="593"><state state="filtered" reason="no-response" reason_ttl="0"/><service name="http-rpc-epmap" method="table" conf="3"/></port>
<port protocol="tcp" portid="31337"><state state="open" reason="syn-ack" reason_ttl="0"/><service name="tcpwrapped" method="probed" conf="8"/></port>
</ports>
<times srtt="207970" rttvar="1965" to="215830"/>
</host>
<runstats><finished time="1520218379" timestr="Mon Mar  5 10:52:59 2018" elapsed="25.17" summary="Nmap done at Mon Mar  5 10:52:59 2018; 1 IP address (1 host up) scanned in 25.17 seconds" exit="success"/><hosts up="1" down="0" total="1"/>
</runstats>
</nmaprun>

方法:

rc : nmap执行的返回码的访问器
返回:nmap执行的 返回码
print nm.rc
为0,nmap执行成功有结果。

run() : 公共方法通常在NmapProcess的构造函数之后调用。该方法启动nmap可执行文件的子进程。它还会绑定一个Process,它将从子进程stdout和stderr中读取并推送python队列中读取的行以进一步处理。每次将数据从nmap二进制文件推送到stdout读取例程时,都会唤醒此处理。处理可以通过用户提供的回调来执行。整个NmapProcess对象可以异步访问。
返回:从nmap执行返回代码
nm.run()

Libnmap.parser

这些模块使您能够解析nmap扫描的输出。现在,只支持XML解析。NmapParser是一个工厂,它将返回一个NmapReport,NmapHost或NmapService对象。所有这些对象的API都有记录。

根据提供的数据,NmapParse.parse()可以返回以下内容:

  • NmapReport对象:如果推荐完整的nmap xml / dict报告
  • NmapHost对象:如果提供了nmap xml <host>部分
  • NmapService对象:如果提供了nmap xml <port>节
  • Python字典与以下键:端口和extraports; python列表。

所有上述方法:

  • nmap扫描结果并返回NmapReport对象
  • XML(<host> ... </ host>标记)中的扫描主机,并将返回一个NmapHost对象
  • XML(<ports> ... </ ports>标记)中的扫描服务列表,并将返回一个NmapService对象的Python数组
  • 使用XML(<port> ... </ port>标记)扫描的服务,并返回NmapService对象
from libnmap.process import NmapProcess
from libnmap.parser import NmapParser

nm = NmapProcess("127.0.0.1, scanme.nmap.org")
nm.run()

nmap_report = NmapParser.parse(nm.stdout)

for scanned_hosts in nmap_report.hosts:
    print scanned_hosts

NmapHost: [127.0.0.1 (localhost) - up]
NmapHost: [45.33.32.156 (scanme.nmap.org scanme.nmap.org) - up]

方法:

nmapRopert:

get_dict() 返回NmapReport对象的python dict表示。这用于通过NmapDiff diff()NmapReport对象

nmap_report.get_dict()
返回:字典

get_host_byid(host_id ) 通过查找id直接从主机阵列获取NmapHost对象。
参数:要查找的主机的IP地址 nmap_host=nmap_report.get_host_byid('192.168.8.5')

返回:NmapHost对象

hosts_total :访问者返回总共扫描的主机数量。 nmap_report.hosts_total
返回:整数

summary :返回描述和总结扫描的字符串。 返回:字符串

NmapHost:

get_ports() :检索NmapHost的每个服务使用的端口列表

返回: list:元组(port,'proto')即:[(22,'tcp'),(25,'tcp')]

ipv4 :所扫描主机的IPv4地址的访问者
返回: IPv4地址作为字符串

mac :扫描主机MAC地址的访问者
返回: MAC地址作为一个字符串

os_class_probabilities() :返回操作系统指纹识别期间检测到的可能的OS类数组

返回: NmapOSClass对数组

NmapOSFingerprint

get_osmatch(osclass_obj ) :此函数使NmapOSFingerprint可以确定NmapOSClass对象是否可以附加到现有的NmapOSMatch对象,以便尊重nmap xml版本<1.04和> = 1.04的通用接口

此方法将返回与参数中提供的NmapOSClass匹配的NmapOSMatch对象(匹配基于准确性执行)

返回: NmapOSMatch对象

ports_used :使用用于执行操作系统指纹的端口返回一个OSFPPortUsed对象数组。这个字典可能包含另一个包含ports_reason值的字典。

NmapOSMatch

get_cpe() :此方法返回cpe stings列表,而不是CPE对象作为NmapOSClass.cpelist属性。该方法是简化数据管理的帮手。

要进一步处理CPE数据,请使用NmapOSClass.cpelist并使用CPE类中的方法

osclasses :与此OS匹配匹配的所有NmapOSClass对象的访问器

NmapOSClass

cpelist :访问器助手,它从NmapOSClass对象返回有价值的属性的concataned字符串
返回:字符串

osfamily :OS系列信息的访问器(Windows,Linux,...)
返回:字符串

vendor :供应商信息的访问器(Microsoft,Linux,...)

返回:字符串

 

 

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值