python端口扫描的两个小脚本

为了加深自己对端口扫描的理解,写了两个小的脚本
1、使用python socket 模块多线程进行tcp端口扫描

2、使用python-nmap 模块进行端口探测

综合来看,还是更喜欢masscan,如上,开源地址
https://github.com/dongfangyuxiao/py-study/tree/master/py-study/practice/port-scan

代码如下:

!/usr/bin/env python

-- coding:utf-8 --

Time : 18-6-23 上午8:06

Author : xiaodong

File : port-tcp.py

一个多线程的tcp端口扫描器,,定义了两个函数

import argparse
import socket
from socket import *
from threading import *
screenlock = Semaphore(value=1)

def connScan(tgtHost,tgtPort):# 这个主要是进行端口扫描,进行tcp链接,获取一些信息

try:
    connskt = socket(AF_INET,SOCK_STREAM)# 创建一个socket 对象family的取值通常是AF_INET。type 的取值通常是SOCK_STREAM(用于定向的连接,可靠的TCP连接)或SOCK_DGRAM(用于UDP)
    connskt.connect((tgtHost,tgtPort))
    connskt.send('hello word\r\n')
    result = connskt.recv(100)
    screenlock.acquire()
    print "{} open  and found {}".format(tgtPort,result)
except:
    screenlock.acquire()
    print "sorry, {} closed".format(tgtPort)
finally:
    screenlock.release()
    connskt.close()

def portscan(tgtHost,tgtPorts):# 这个主要是进行主机名的变换,并且多线程调用connScan函数
try:
tgtIp = gethostbyname(tgtHost)#gethostbyname(name) 尝试将给定的主机名解释为一个IP地址。
except:
print “cannot resolve {}”.format(tgtHost)
return
try:
tgtName = gethostbyaddr(tgtIp)#gethostbyaddr() 由IP 地址得到DNS 信息,返回一个类似gethostbyname_ex()的3 元组。
print “scan result for {}”.format(tgtName[0])
except:
print “scan result for {}”.format(tgtIp)
setdefaulttimeout(1)
for tgtPort in tgtPorts:
t = Thread(target=connScan,args=(tgtHost,int(tgtPort)))
t.start()

def main():
parser = argparse.ArgumentParser()
parser.add_argument(‘host’,help=’please input what you want to scan host,eg:www.baidu.com’)
parser.add_argument(‘port’,help=’please input port what you want to scan,eg:21,22’)
args = parser.parse_args()

tgtHost = args.host

tgtPorts = str(args.port).split(',')
if (tgtHost == None) | (tgtPorts ==None):
    print " are you sure you input is right?"
    exit(0)
portscan(tgtHost,tgtPorts)

if name == “main“:
main()

!/usr/bin/env python

-- coding:utf-8 --

Time : 18-6-23 上午9:54

Author : xiaodong

File : port-nmap.py

import nmap

(此处导入的nmap 为python-nmap)

import argparse

https://www.cnblogs.com/aylin/p/5996229.html

AttributeError: ‘function’ object has no attribute ‘PortScanner’ 如果出现这个,说明安装错了,要安装python-nmap

def nmapScan(tgtHost,tgtPort):
nmscan = nmap.PortScanner()
nmscan.scan(tgtHost,tgtPort)
state = nmscan[tgtHost][‘tcp’][int(tgtPort)][‘state’]#获取主机 端口(tcp)的状态 (open|closed|filter)
print “{} found {}”.format(tgtHost,state)

def main():
parser = argparse.ArgumentParser()
parser.add_argument(‘host’,help=’input ip you want to scan’)
parser.add_argument(‘port’,help=’scan port’)
args = parser.parse_args()

tgtHost = args.host
tgtPorts = str(args.port).split(',')
for tgtPort in tgtPorts:
    nmapScan(tgtHost,tgtPort)

if name == “main“:
main()

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章