Python3 端口扫描器
用python3 实现 connect syn udp扫描
话不多说,直接上代码
1.connect.py
#!/usr/bin/python3
# -*- coding: utf-8 -*-
from socket import *
def portScanner(host,port):
try:
s = socket(AF_INET,SOCK_STREAM)
s.connect((host,port))
print('[+] %d open' % port)
s.close()
except:
print('[-] %d close' % port)
def main_c(host,start,end):
for p in range(start,end):
portScanner(host,p)
if __name__ == '__main__':
ip=input()
start=int(input())
end=int(input())
main(ip,start,end)
2.udp.py
from scapy.all import *
def UdpScanner(ip,start,end):
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('[+] %d open' % port)
else:
print('[-] %d close' % port)
pass
- scanner.py
#!/usr/bin/python3
# -*- coding: utf-8 -*-
from socket import *
from scapy.all import *
from connent import *
from udp import *
def syn_scan(host,port):
a=sr1(IP(dst=host)/TCP(dport=port),timeout=1,verbose=0)
if a==None:
print('[-] %d close' % port)
pass
else:
if(a[TCP].flags)==18:
print('[+] %d open' % port)
if __name__=='__main__':
print('请输入要扫描的ip地址:')
host=input()
print('请输入要扫描的起始端口:')
start=int(input())
print('请输入要扫描的结束端口:')
end=int(input())
while(True):
print('————————————————————————————————————————————————')
print('请选择扫描类型:\n1.connect扫描\n2.syn扫描\n3.udp扫描\n4.按任意键退出')
num=int(input())
if num == 1:
main_c(host, start, end)
elif num == 2:
print("开始扫描主机:%s..."%host)
for p in range(start, end):
syn_scan(host, p)
elif num == 3:
UdpScanner(host,start,end)
else:
break
用法
创建connect.py和udp.py然后把我的代码复制到里面,最后创建scanner.py,把代码复制到其中,直接运行scanner.py即可。
最后
刚刚在创建虚拟机,等的时间有点长,用这个时间写下了这个博客,这个是我的一个网络编程实验,第一次写博客,有点混乱,代码放在这里 还请大家斧正。