有后台朋友咨询了,有没有 udp 端口扫描教程,特意去学习了一遍端口扫描教程,才有这篇文章的出现
发现到存活的IP以后,那么下一步就是针对特定的主机进行端口扫描了,因为端口对应的是网络服务及其应用段的程序,一旦发现开放的端口,便可以借此进行渗透。
假设ICMP 返回port-unreachable 响应代表端口关闭,但是如果目标系统不响应ICMP port-unreachable时,那么此端口可能处于开放状态
#!/usr/bin/python
import logging
import subprocess
logging.getLogger("scapy.runtime").setLevel(logging.ERROR)
from scapy.all import *
import time
if len(sys.argv) != 4:
print "Usage - ./udp_port.py [Target - TP] [First port] [Last port]"
print "Usage - ./udp-port.py 128.13.34.13 1 100"
sys.exit()
ip = sys.argv[1]
start = int(sys.argv[2])
end = int(sys.argv[3])
for port in range(start,end):
a = sr1(IP(dst=ip)/UDP(dport=port),timeout=5,verbose=0)
time.sleep(1)
if a == None:
print port
else:
pass
对特定ip端口扫描,如果不指定端口的话,namp默认会对1000常用端口进行扫描,即使是nmap如果是基于UDP的扫描话,也只是利用了UDP端口不可达的这一个特征信息,即返回一个ICMP不可达
nmap -sU 192.168.0.101
指定端口扫描(-p53):
nmap -sU -p53 192.168.0.101
指定端口范围进行扫描(-p53-80):
nmap -sU -p53-80 192.168.0.100
需求:扫描目标网站开放哪些端口号,将所有开放的端口号输出。
分析:使用socket连接,如果连接成功,认为端口开放,如果连接失败,认为端口关闭(有可能端口开放但连接失败,这里简单认为端口不开放)
使用到的库:socket, threading
过程:
先定义一个函数,对给定的(ip, port)进行扫描,看其是否能连接成功。
def tcpPortScan(ip, port, openPort):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # 创建套接字
sock.settimeout(0.1) # 设置延时时间
try:
result = sock.connect_ex((ip, port))
if result == 0: # 如果连接成功,返回值为0
openPort.append(port) # 如果端口开放,就把端口port赋给openPort
except:
pass
sock.close() # 关闭套接字
当需要扫描目标地址的多个端口时,循环使用上述函数的话,扫描速度会极其慢,因为考虑使用多线程。
再定义一个函数,实现多线程扫描。
def threadingPortScan(host, portList, openPorts = []):
hostIP = socket.gethostbyname(host) # 获取域名对应的IP地址
nloops = range(len(portList))
threads = []
for i in nloops:
t = threading.Thread(target=tcpPortScan, args=(hostIP, portList[i], openPorts))
threads.append(t)
for i in nloops:
threads[i].start()
for i in nloops:
threads[i].join()
return openPorts # 返回值为该域名下开放的端口列表
完整代码如下:
# -*- coding:utf-8 -*-
'''
使用多线程,检测一个目标地址的端口开放情况,目标地址由用户输入,端口暂时定义为0~1024,
检测TCP连接是否成功,如果连接成功,则端口开放,不成功则端口关闭
'''
import socket
import threading
def main():
host = input('please input domain:')
portList = range(0, 1025)
openPorts = threadingPortScan(host, portList)
print(host,'open ports:', openPorts)
# 对给定的(ip, port)进行TCP连接扫描
def tcpPortScan(ip, port, openPort):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # 创建套接字
sock.settimeout(0.1) # 设置延时时间
try:
result = sock.connect_ex((ip, port))
if result == 0:
openPort.append(port) # 如果端口开放,就把端口port赋给openPort
except:
pass
sock.close() # 关闭套接字
def threadingPortScan(host, portList, openPorts = []):
hostIP = socket.gethostbyname(host) # 获取域名对应的IP地址
nloops = range(len(portList))
threads = []
for i in nloops:
t = threading.Thread(target=tcpPortScan, args=(hostIP, portList[i], openPorts))
threads.append(t)
for i in nloops:
threads[i].start()
for i in nloops:
threads[i].join()
return openPorts # 返回值为该域名下开放的端口列表
if __name__ == '__main__':
main()
使用www.qq.com做一个测试,测试结果如下:
>>>please input domain: www.qq.com
www.qq.com open ports: [80, 843]
import optparse
import socket
from socket import *
from threading import *
screenLock = Semaphore(value=1)
def connScan(tgtHost,tgtPort):
try:
connSkt = socket(AF_INET,SOCK_STREAM)
connSkt.connect((tgtHost,tgtPort))
connSkt.send('ViolentPython\r\n'.encode('utf-8'))
result = connSkt.recv(1024)
screenLock.acquire()
print("[+] %d/tcp open"%tgtPort)
print("[+]"+str(result))
except Exception as e:
screenLock.acquire()
print(e)
finally:
screenLock.release()
connSkt.close()
def portScan(tgtHost,tgtPorts):
try:
tgtIp = gethostbyname(tgtHost)
except:
print("[-] Cannot reslove '%s' : Unknown host" % tgtHost)
return
try:
tgtName = gethostbyaddr(tgtIp)
print("\n [+] Scan Result for: " + tgtName[0])
except:
print("\n [+] Scan Result for: " + tgtIp)
setdefaulttimeout(1)
for tgtPort in tgtPorts:
print("Scanning port "+ tgtPort)
t = Thread(target=connScan,args=(tgtHost,int(tgtPort)))
t.start()
def main():
parser = optparse.OptionParser("usage%prog -H -p ")
parser.add_option('-H',dest='tgtHost',type='string',help='specify target host')
parser.add_option('-p',dest='tgtPort',type='string',help='specify target port[s] separated by comma')
options,args = parser.parse_args()
tgtHost = options.tgtHost
tgtPorts = str(options.tgtPort).split(',')
if(tgtHost == None or tgtPorts[0] == None):
print("[-] You must specify a target host and port[s]")
exit(0)
portScan(tgtHost,tgtPorts)
if __name__ == '__main__':
main()
使用命令
python BannerDemo.py -H 192.168.200.101 -p 22,3306
请继续关注我
记得点赞加关注哦,记得加鸡腿啊