python3实现端口扫描脚本
啊 好久没写代码了,想着多线程啥的都忘得差不多了
就下面这个啦,
#coding=utf-8
import time,threading
import socket
opennum=0
lock=threading.Lock()
def tcp_connect(ip,port):
global opennum
tcpsock=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
tcpsock.settimeout(0.5)
try:
result=tcpsock.connect_ex((ip,port))
lock.acquire()
if(result==0):
print ("%s:%s 开启" % (ip, port))
opennum+=1
lock.release()
else:
#print ("%s:%s 关闭" % (ip, port))
pass
tcpsock.close()
except socket.error as e:
print ("[!]错误",e)
def url_ip(url):
if('http://' in url or 'https://' in url):
url=url[url.find('://')+3:]
targetip = socket.getaddrinfo(url, 'http')[0][4][0]
print (targetip)
port_scan(targetip)
else:
targetip = socket.getaddrinfo(url, 'http')[0][4][0]
print (targetip)
port_scan(targetip)
def port_scan():
print ("***扫描开始***")
w = raw_input("ip=")
port=raw_input("port=")
start=time.time()
mythreads=[]
for i in range(int(port)):
t = threading.Thread(target=tcp_connect, args=(w,i))
mythreads.append(t)
for i in range(int(port)):
mythreads[i].start()
for i in range(int(port)):
mythreads[i].join()
end=time.time()
print("[*]总耗时%s" % time.strftime("%H:M:S", time.gmtime(end - start)))
print ("开放%s个端口" % opennum)
def main():
print "-------------"
print "1:ip扫描端口"
print "2:url扫描端口"
print "-------------"
q=raw_input("选择功能:")
if(int(q)==1):
port_scan()
else:
e=raw_input("url=")
url_ip(e)
print("扫描结束")
main()
#!/usr/bin/python3 # -*- coding: utf-8 -*- from socket import * import threading lock = threading.Lock() openNum = 0 threads = [] def portScanner(host,port): global openNum try: s = socket(AF_INET,SOCK_STREAM) s.connect((host,port)) lock.acquire() openNum+=1 print('[+] %d open' % port) lock.release() s.close() except: pass def main(): setdefaulttimeout(1) for p in range(1,6558): t = threading.Thread(target=portScanner,args=('14.215.177.38',p)) threads.append(t) t.start() for t in threads: t.join() print('[*] The scan is complete!') print('[*] A total of %d open port ' % (openNum)) if __name__ == '__main__': main()
这是协程
# -*-coding:UTF-8 -*- # @Time: 2018/9/7 11:07 # @Author:gumgui # @File: 端口扫描.py import time import socket from gevent import monkey monkey.patch_all() import gevent import gevent.pool def TCP_connect(ip,port): """模拟TCP连接""" TCP_sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM) TCP_sock.settimeout(0.5) #设置连接超时 try: result = TCP_sock.connect_ex((ip,int(port))) if result == 0: print("[*]%s 端口 开启\t"%port) else: # print("[!]%s端口 关闭"%port) pass TCP_sock.close() except socket.error as e: print("[!]错误:",e) def scan_ip(): """扫描目标IP""" ip = raw_input("[+]输入扫描目标IP:") print("[*]正在扫描") scan_port(ip) def scan_web(): """扫描目标网址""" web = raw_input("[+]输入扫描网址:") if "http://" in web or "https://" in web: web = web[web.find('://')+3:] print(web) print("[*]正在分析网站服务器IP") try: server_ip = socket.gethostbyname(str(web)) print("[*]服务器IP为%s"%server_ip) scan_port(server_ip) except Exception as e: print("[!]服务器IP获取失败") pass def scan_port(ip): """扫描端口""" print("[*]开始扫描目标端口") start = time.time() g = gevent.pool.Pool(50) #设置线程数 run_list = [] for port in range(1,200): run_list.append(g.spawn(TCP_connect,ip,port)) gevent.joinall(run_list) end = time.time() print("[*]总耗时%s"%time.strftime("%H:%M:%S",time.gmtime(end-start))) def main(): print( """ 1.通过IP扫描端口 2.通过网址扫描端口 """ ) uc = int(raw_input("[+]请输入选择:")) if 1 == uc: scan_ip() elif 2 == uc: scan_web() else: print("[!]输入有误") if __name__ == "__main__": main()