python-nmap浣跨敤鍙婃渚?----0012

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python-nmap浣跨敤鍙婃渚?----0012相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/%e9%bb%91%e5%ae%a2%e5%b8%9d%e5%9b%bd' title='榛戝甯濆浗'>榛戝甯濆浗   mat   tin   compile   art   鍑芥暟   man   mail   rom   

 

nmap姒傚康鍙婂姛鑳?/h1>

姒傚康

NMap锛屼篃灏辨槸Network Mapper锛屾渶鏃╂槸Linux涓嬬殑缃戠粶鎵弿鍜?a href="http://baike.baidu.com/view/282229.htm" target="_blank">鍡呮帰宸ュ叿鍖呫€?/p>

nmap鏄竴涓綉缁滆繛鎺ョ鎵弿杞欢锛岀敤鏉ユ壂鎻忕綉涓婄數鑴戝紑鏀剧殑缃戠粶杩炴帴绔€傜‘瀹氬摢浜涙湇鍔¤繍琛屽湪鍝簺杩炴帴绔紝骞朵笖鎺ㄦ柇璁$畻鏈鸿繍琛屽摢涓搷浣滅郴缁燂紙杩欐槸浜︾О fingerprinting锛夈€傚畠鏄綉缁滅鐞嗗憳蹇呯敤鐨勮蒋浠朵箣涓€锛屼互鍙婄敤浠ヨ瘎浼扮綉缁滅郴缁熷畨鍏ㄣ€?/p>

姝e澶у鏁拌鐢ㄤ簬缃戠粶瀹夊叏鐨勫伐鍏凤紝nmap 涔熸槸涓嶅皯榛戝鍙婇獓瀹紙鍙堢О鑴氭湰灏忓瓙锛夌埍鐢ㄧ殑宸ュ叿 銆傜郴缁熺鐞嗗憳鍙互鍒╃敤nmap鏉ユ帰娴嬪伐浣滅幆澧冧腑鏈粡鎵瑰噯浣跨敤鐨勬湇鍔″櫒锛屼絾鏄粦瀹細鍒╃敤nmap鏉ユ悳闆嗙洰鏍囩數鑴戠殑缃戠粶璁惧畾锛屼粠鑰岃鍒掓敾鍑荤殑鏂规硶銆?/p>

Nmap 甯歌璺熻瘎浼扮郴缁熸紡娲炶蒋浠?a href="http://baike.baidu.com/view/1382512.htm" target="_blank">Nessus 娣蜂负涓€璋堛€侼map 浠ラ殣绉樼殑鎵嬫硶锛岄伩寮€闂叆妫€娴嬬郴缁熺殑鐩戣锛屽苟灏藉彲鑳戒笉褰卞搷鐩爣绯荤粺鐨勬棩甯告搷浣溿€?/p>

Nmap 鍦?a href="http://baike.baidu.com/subview/6918/8159699.htm" target="_blank" data-lemmaid="34564">榛戝甯濆浗(The Matrix)涓紝杩炲悓SSH1鐨?2浣嶅厓寰幆鍐椾綑鏍¢獙婕忔礊锛岃宕斿Ξ钂傜敤浠ュ叆渚靛彂鐢电珯鐨勮兘婧愮鐞嗙郴缁熴€?/p>

 

鍔熻兘

鍩烘湰鍔熻兘鏈変笁涓紝涓€鏄帰娴嬩竴缁勪富鏈烘槸鍚﹀湪绾匡紱鍏舵鏄壂鎻?涓绘満绔彛锛?a href="http://baike.baidu.com/view/282229.htm" target="_blank">鍡呮帰鎵€鎻愪緵鐨勭綉缁滄湇鍔★紱杩樺彲浠ユ帹鏂富鏈烘墍鐢ㄧ殑鎿嶄綔绯荤粺 銆侼map鍙敤浜庢壂鎻忎粎鏈変袱涓妭鐐圭殑LAN锛岀洿鑷?00涓妭鐐逛互涓婄殑缃戠粶銆侼map 杩樺厑璁哥敤鎴峰畾鍒舵壂鎻忔妧宸с€傞€氬父锛屼竴涓畝鍗曠殑浣跨敤ICMP鍗忚鐨刾ing鎿嶄綔鍙互婊¤冻涓€鑸渶姹傦紱涔熷彲浠ユ繁鍏ユ帰娴婾DP鎴栬€匱CP绔彛锛岀洿鑷?a href="http://baike.baidu.com/view/23880.htm" target="_blank">涓绘満鎵€ 浣跨敤鐨勬搷浣滅郴缁燂紱杩樺彲浠ュ皢鎵€鏈夋帰娴嬬粨鏋滆褰曞埌鍚勭鏍煎紡鐨勬棩蹇椾腑锛?渚涜繘涓€姝ュ垎鏋愭搷浣溿€?/p>

杩涜ping鎵弿锛屾墦鍗板嚭瀵规壂鎻忓仛鍑哄搷搴旂殑涓绘満,涓嶅仛杩涗竴姝ユ祴璇?濡?a href="http://baike.baidu.com/view/700108.htm" target="_blank">绔彛鎵弿鎴栬€呮搷浣滅郴缁熸帰娴?锛?/p>

nmap -sP 192.168.1.0/24

浠呭垪鍑烘寚瀹氱綉缁滀笂鐨勬瘡鍙颁富鏈猴紝涓嶅彂閫佷换浣?a href="http://baike.baidu.com/view/175122.htm" target="_blank">鎶ユ枃鍒扮洰鏍囦富鏈猴細

nmap -sL 192.168.1.0/24

鎺㈡祴鐩爣涓绘満寮€鏀剧殑绔彛锛屽彲浠ユ寚瀹氫竴涓互閫楀彿鍒嗛殧鐨勭鍙e垪琛?濡?PS22锛?3锛?5锛?0)锛?/p>

nmap -PS 192.168.1.234

浣跨敤UDP ping鎺㈡祴涓绘満锛?/p>

nmap -PU 192.168.1.0/24

浣跨敤棰戠巼鏈€楂樼殑鎵弿閫夐」锛歋YN鎵弿,鍙堢О涓哄崐寮€鏀炬壂鎻忥紝瀹冧笉鎵撳紑涓€涓畬鍏ㄧ殑TCP杩炴帴锛屾墽琛屽緱寰堝揩锛?/p>

nmap -sS 192.168.1.0/24

 

nmap瀹夎

鏈枃浠inux Ubuntu16.04涓轰緥锛屾渶鍚庝富瑕佺敤python鎿嶄綔

1. 鍏堝畨瑁卬map

sudo apt-get install nmap

2.鍐嶅畨瑁卲ython-nmap

sudo pip install python-nmap

瀹夎瀹屼箣鍚巔ython瀵煎叆nmap娴嬭瘯楠岃瘉鏄惁鎴愬姛

root@LiDebin:~# python
Python 2.7.12 (default, Jul  1 2016, 15:12:24) 
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import nmap

 

python鎿嶄綔nmap

1.绠€鍗曠殑灏忔渚?/p>

鍒涘缓PortScanner瀹炰緥锛岀劧鍚庢壂鎻?59.239.210.26杩欎釜IP鐨?0-443绔彛銆?/p>

鎶€鏈浘鐗? src=
import nmap

nm = nmap.PortScanner()
ret = nm.scan(鈥?15.239.210.26鈥?鈥?0鈥?
print ret

杩斿洖鏍煎紡濡備笅:
{鈥榥map鈥? {鈥榮canstats鈥? 
{鈥榰phosts鈥? 鈥?鈥? 鈥榯imestr鈥? 鈥楾ue Oct 25 11:30:47 2016鈥? 鈥榙ownhosts鈥? 鈥?鈥? 鈥榯otalhosts鈥? 鈥?鈥? 鈥榚lapsed鈥? 鈥?.11鈥榼,
 鈥榮caninfo鈥? {鈥榯cp鈥? {鈥榮ervices鈥? 鈥?0鈥? 鈥榤ethod鈥? 鈥榗onnect鈥榼}, 鈥榗ommand_line鈥? 鈥榥map -oX - -p 20 -sV 115.239.210.26鈥榼,
 鈥榮can鈥? {鈥?15.239.210.26鈥? {鈥榮tatus鈥? {鈥榮tate鈥? 鈥榰p鈥? 鈥榬eason鈥? 鈥榮yn-ack鈥榼, 鈥榟ostnames鈥? [{鈥榯ype鈥? 鈥樷€? 鈥榥ame鈥? 鈥樷€榼],
 鈥榲endor鈥? {}, 鈥榓ddresses鈥? {鈥榠pv4鈥? 鈥?15.239.210.26鈥榼,
 鈥榯cp鈥? {20: {鈥榩roduct鈥? 鈥樷€? 鈥榮tate鈥? 鈥榝iltered鈥? 鈥榲ersion鈥? 鈥樷€? 鈥榥ame鈥? 鈥榝tp-data鈥? 鈥榗onf鈥? 鈥?鈥? 鈥榚xtrainfo鈥? 鈥樷€? 
鈥榬eason鈥? 鈥榥o-response鈥? 鈥榗pe鈥? 鈥樷€榼 } } } }
鎶€鏈浘鐗? src=

 

2.鍐呯疆鏂规硶锛?/p>

杩樺彲浠ユ墦鍗板嚭绠€鍗曠殑淇℃伅

鎶€鏈浘鐗? src=
import nmap  
nm = nmap.PortScanner() 
print nm.scaninfo()
# {u鈥榯cp鈥? {鈥榮ervices鈥? u鈥?0-443鈥? 鈥榤ethod鈥? u鈥榮yn鈥榼}
print nm.command_line() 
# u鈥榥map -oX - -p 20-443 -sV 115.239.210.26鈥?
鎶€鏈浘鐗? src=

鏌ョ湅鏈夊灏戜釜host

print nm.all_hosts()
# [u鈥?15.239.210.26鈥榏 

鏌ョ湅璇ost鐨勮缁嗕俊鎭?/p>

nm[鈥?15.239.210.26鈥榏

鏌ョ湅璇ost鍖呭惈鐨勬墍鏈夊崗璁?/p>

nm[鈥?15.239.210.26鈥榏.all_protocols() 

鏌ョ湅璇ost鐨勫摢浜涚鍙f彁渚涗簡tcp鍗忚

nm[鈥?15.239.210.26鈥榏[鈥榯cp鈥榏

nm[鈥?15.239.210.26鈥榏[鈥榯cp鈥榏.keys() 

鏌ョ湅璇ョ鍙f槸鍚︽彁渚涗簡tcp鍗忚

nm[鈥?15.239.210.26鈥榏.has_tcp(21)

杩樺彲浠ュ儚杩欐牱璁剧疆nmap鎵ц鐨勫弬鏁?/p>

nm.scan(hosts=鈥?92.168.1.0/24鈥? arguments=鈥?n -sP -PE -PA21,23,80,3389鈥? 

鏇村鎿嶄綔璇疯繘瀹樼綉http://xael.org/pages/python-nmap-en.html 

 

瀹為獙妗堜緥

妫€娴嬪唴缃戞満鍣ㄧ鍙?/p>

1.瀹氫箟鍑芥暟搴搈ytools.py

鎶€鏈浘鐗? src=
#-*- coding:utf-8 -*- 
import smtplib 
from email.mime.text import MIMEText 
from email.header import Header 
def sendemail(sender,receiver,subject,content,smtpserver,smtpuser,smtppass): 
    msg = MIMEText(content,鈥榟tml鈥?鈥榰tf-8鈥?#涓枃闇€鍙傛暟‘utf-8鈥橈紝鍗曞瓧鑺傚瓧绗︿笉闇€瑕?
    msg[鈥楽ubject鈥榏 = Header(subject, 鈥榰tf-8鈥? 
    msg[鈥楩rom鈥榏 = 鈥?lt;%s>鈥?% sender 
    msg[鈥楾o鈥榏 = ";".join(receiver) 
    try: 
        smtp = smtplib.SMTP() 
        smtp.connect(smtpserver) 
        smtp.login(smtpuser, smtppass) 
        smtp.sendmail(sender, receiver, msg.as_string()) 
        smtp.quit() 
    except Exception,e: 
        print e
鎶€鏈浘鐗? src=

2. 瀹炵幇绔彛鎵弿鐨勭▼搴忥紝鍗曠嚎绋嬬増鏈琻mscan.py

鎶€鏈浘鐗? src=
# !/usr/bin/python 
# -*- coding:utf-8 -*- 

import nmap
import re
import mytools as tool
import sys

reload(sys)
sys.setdefaultencoding(鈥榰tf8鈥?


def nmScan(hostlist, portrange, whitelist):
    p = re.compile("^(d*)-(d*)$")


    if type(hostlist) != list:
        help()
    portmatch = re.match(p, portrange)
    if not portmatch:
        help()
    l = []
    for host in hostlist:
        result = 鈥樷€?    nm = nmap.PortScanner()
    tmp = nm.scan(host, portrange)
    result = result + "<h2>ip鍦板潃:%s 涓绘満鍚?[%s]  ......  %s</h2><hr>" % (
    host, tmp[鈥榮can鈥榏[host][鈥榟ostname鈥榏, tmp[鈥榮can鈥榏[host][鈥榮tatus鈥榏[鈥榮tate鈥榏)
    try:
        ports = tmp[鈥榮can鈥榏[host][鈥榯cp鈥榏.keys()
    except KeyError, e:
        if whitelist:
            whitestr = 鈥?鈥?join(whitelist)
            result = result + "鏈壂鍒板紑鏀剧鍙?璇锋鏌?s绔彛瀵瑰簲鐨勬湇鍔$姸鎬? % whitestr
        else:
            result = result + "鎵弿缁撴灉姝e父锛屾棤鏆存紡绔彛"
    for port in ports:
        info = 鈥樷€?        if port not in whitelist:
            info = 鈥?lt;strong><font color=red>Alert:闈為鏈熺鍙?lt;/font><strong>&nbsp;&nbsp;鈥?        else:
            info = 鈥?lt;strong><font color=green>Info:姝e父寮€鏀剧鍙?lt;/font><strong>&nbsp;&nbsp;鈥?        portinfo = "%s <strong>port</strong> : %s &nbsp;&nbsp;<strong>state</strong> : %s &nbsp;&nbsp;<strong>product<strong/> : %s <br>" % (
        info, port, tmp[鈥榮can鈥榏[host][鈥榯cp鈥榏[port][鈥榮tate鈥榏,
        tmp[鈥榮can鈥榏[host][鈥榯cp鈥榏[port][鈥榩roduct鈥榏)
        result = result + portinfo
    l.append([host, str(result)])
    return l


def help():
    print "Usage: nmScan([鈥?27.0.0.1鈥?],鈥?-65535鈥?"


if __name__ == "__main__":
    hostlist = [鈥?0.10.10.10鈥? 鈥?0.10.10.11鈥榏
    portrange = 鈥?-65535鈥?    whitelist = [80, 443]
    l = nmScan(hostlist, portrange, whitelist)
    sender = 鈥?5501664@qq.com鈥?    receiver = [鈥榸hangyanlin8851@163.com鈥? 鈥?77986976@qq.com鈥榏
    subject = 鈥樻湇鍔″櫒绔彛鎵弿鈥?    smtpserver = 鈥榮mtp.exmail.qq.com鈥?    smtpuser = 鈥榸hangyanlin8851@163.cn鈥?    smtppass = 鈥榣inuxidc163鈥?    mailcontent = 鈥樷€?    for i in range(len(l)):
        mailcontent = mailcontent + l[i][1]
    tool.sendemail(sender, receiver, subject, mailcontent, smtpserver, smtpuser, smtppass)
鎶€鏈浘鐗? src=

3.澶氱嚎绋嬬増鏈?/p>

鎶€鏈浘鐗? src=
# !/usr/bin/python
# -*- coding:utf-8 -*-

import nmap
import re
import mytools as tool
import sys
from multiprocessing import Pool
from functools import partial

reload(sys)
sys.setdefaultencoding(鈥榰tf8鈥?


def nmScan(host, portrange, whitelist):
    p = re.compile("^(d*)-(d*)$")
    # if type(hostlist) != list:
    #    help()
    portmatch = re.match(p, portrange)
    if not portmatch:
        help()

    if host == 鈥?21.42.32.172鈥?
        whitelist = [25, ]
    result = 鈥樷€?    nm = nmap.PortScanner()
    tmp = nm.scan(host, portrange)
    result = result + "<h2>ip鍦板潃:%s 涓绘満鍚?[%s]  ......  %s</h2><hr>" % (
    host, tmp[鈥榮can鈥榏[host][鈥榟ostname鈥榏, tmp[鈥榮can鈥榏[host][鈥榮tatus鈥榏[鈥榮tate鈥榏)
    try:
        ports = tmp[鈥榮can鈥榏[host][鈥榯cp鈥榏.keys()
        for port in ports:
            info = 鈥樷€?            if port not in whitelist:
                info = 鈥?lt;strong><font color=red>Alert:闈為鏈熺鍙?lt;/font><strong>&nbsp;&nbsp;鈥?            else:
                info = 鈥?lt;strong><font color=green>Info:姝e父寮€鏀剧鍙?lt;/font><strong>&nbsp;&nbsp;鈥?            portinfo = "%s <strong>port</strong> : %s &nbsp;&nbsp;<strong>state</strong> : %s &nbsp;&nbsp;<strong>product<strong/> : %s <br>" % (
            info, port, tmp[鈥榮can鈥榏[host][鈥榯cp鈥榏[port][鈥榮tate鈥榏, tmp[鈥榮can鈥榏[host][鈥榯cp鈥榏[port][鈥榩roduct鈥榏)
            result = result + portinfo
    except KeyError, e:
        if whitelist:
            whitestr = 鈥?鈥?join(whitelist)
            result = result + "鏈壂鍒板紑鏀剧鍙?璇锋鏌?s绔彛瀵瑰簲鐨勬湇鍔$姸鎬? % whitestr
        else:
            result = result + "鎵弿缁撴灉姝e父锛屾棤鏆存紡绔彛"
    return result


def help():
    print "Usage: nmScan([鈥?27.0.0.1鈥?],鈥?-65535鈥?"
    return None


if __name__ == "__main__":
    hostlist = [鈥?0.10.10.10鈥? 鈥?0.10.10.11鈥榏
    portrange = 鈥?-65535鈥?    whitelist = [80, 443]
    l = nmScan(hostlist, portrange, whitelist)
    sender = 鈥?5501664@qq.com鈥?    receiver = [鈥榸hangyanlin8851@163.com鈥? 鈥?77986976@qq.com鈥榏
    subject = 鈥樻湇鍔″櫒绔彛鎵弿鈥?    smtpserver = 鈥榮mtp.exmail.qq.com鈥?    smtpuser = 鈥榸hangyanlin8851@163.cn鈥?    smtppass = 鈥榣inuxidc163鈥?    mailcontent = 鈥樷€?    for i in range(len(l)):
        mailcontent = mailcontent + l[i][1]
    tool.sendemail(sender, receiver, subject, mailcontent, smtpserver, smtpuser, smtppass)
鎶€鏈浘鐗? src=

 

銆€

以上是关于python-nmap浣跨敤鍙婃渚?----0012的主要内容,如果未能解决你的问题,请参考以下文章

MySQL涔嬪垵浣跨敤

濡備綍鎻愰珮绋嬪簭鍛樼殑閿洏浣跨敤鐜囷紵

window10Cmd浣跨敤

SpringCloudConfig浣跨敤

vue axios浣跨敤

mac浣跨敤