-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathportscan.py
More file actions
136 lines (117 loc) · 3.51 KB
/
portscan.py
File metadata and controls
136 lines (117 loc) · 3.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import socket,threading,sys,time
#tcp扫描
def scan1(ip,port,openports):
s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(1)
try:
s.connect((ip,port))
outcome=f"{port} open"
#print(f"{port} open")
openports.append(outcome)
except:
pass
finally:
s.close()
#udp扫描
def scan2(ip, port, openports):
print('Server %s, Port: %s is scanning' % (ip, port)) # 打印IP和端口
try:
port = int(port) # 整数型port
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # socket 实例化
s.settimeout(5) # 设置超时时间
s.sendto(b'11', (ip, port)) # 发送数据
try:
r, i = s.recvfrom(1024)
print(r)
if r:
#print(f"{port} open")
outcome=f"主机:{ip}\n{port} open"
openports.append(outcome)
else:
#print('close')
outcome=f"主机:{ip}\n{port} close"
openports.append(outcome)
except socket.timeout:
outcome=f"主机:{ip}\n{port} close"
openports.append(outcome)
except Exception as e:
print(f"Error scanning port {port}: {e}")
finally:
s.close() # 关闭套接字
#线程分配
def thread(ip,ports,flag):
count=0
#模式选择
if flag==1:
mode=scan1
elif flag==2:
mode=scan2
openports=[]
threads = []
for port in ports:
t=threading.Thread(target=mode,args=(ip,int(port),openports))
t.start()
threads.append(t)
for t in threads:
t.join()
return openports
#主函数
def main():
banner()
#ip="bilibili.com"
ports=[]
type=""
type1=""
flag=0
ip=input("请输入ip:")
while type!="1" or type!="2":
type=input("模式1:固定端口/模式2:全端口\n")
if type=="1":
ports=input("请输入端口号,用英文','分割\n").split(",")
break
elif type=="2":
st=int(input("起始端口:"))
ed=int(input("结束端口:"))
for i in range(st,ed+1):
ports.append(i)
break
else:
print("模式错误")
while type1!="1" or type1!="2":
type1=input("模式1:tcp/模式2:udp\n")
if type1=="1":
flag=1
break
elif type1=="2":
flag=2
break
else:
print("模式错误")
print("\n开始扫描\n")
time1=time.time()
openports=thread(ip,ports,flag)
time2=time.time()
time3=time2-time1
print("------------------------------------------------------\n")
print("扫描结果\n")
for port in openports:
print(port)
print(f"\n扫描用时:{round(time3,2)}s\n")
print("------------------------------------------------------\n")
#banner
def banner():
banner_text="""
_____ _________ ____ _ _ _
| ____|__ / ___| / ___| / \ | \ | |
| _| / /\___ \| | / _ \ | \| |
| |___ / /_ ___) | |___ / ___ \| |\ |
|_____/____|____/ \____/_/ \_\_| \_|
author:yiranloveyou
"""
print(banner_text)
if __name__ == "__main__":
while(1):
main()
restart = input("是否需要重新开始?(y/n): ")
if restart.lower() == 'n':
sys.exit()