Skip to content

Commit cbfeb42

Browse files
WillowSauceRWillowSauceR
authored andcommitted
support scan port range
1 parent 94c255f commit cbfeb42

File tree

2 files changed

+39
-41
lines changed

2 files changed

+39
-41
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
## scan.py
88

9-
### 使用方法: `python3 scan.py [目标地址] [是否启用详细输出] [可选:超时时间,单位为秒,默认无限] [可选:保存结果的文件名]`
9+
### 使用方法: `python3 scan.py [目标地址] [端口范围: 如1145-1919或all] [是否启用详细输出] [可选:超时时间,单位为秒,默认无限] [可选:保存结果的文件名]`
1010

1111
#### 描述: 扫描IP上的所有BE协议服务器,使用前建议安装[Npcap](https://npcap.com/dist/npcap-1.60.exe),然后使用命令 `pip install scapy`来安装依赖,基于scapy,基本不漏服
1212

scan.py

Lines changed: 38 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,21 @@
1414
localHostPort = randint(1024, 65535)
1515

1616
try:
17-
TargetAddr = str(sys.argv[1])
18-
verboseMode = str(sys.argv[2])
17+
TargetAddr = sys.argv[1]
18+
portRange = sys.argv[2]
19+
verboseMode = sys.argv[3]
1920
except:
2021
TargetAddr = input("Target IP: ")
22+
portRange = input("Port range(like 1145-1919 and all): ")
2123
verboseMode = input("Show verbose info(y/n): ")
2224

2325
try:
24-
timeout = int(sys.argv[3])
26+
timeout = int(sys.argv[4])
2527
except:
2628
timeout = None
2729

2830
try:
29-
fileName = str(sys.argv[4])
31+
fileName = str(sys.argv[5])
3032
except:
3133
fileName = ""
3234

@@ -69,33 +71,40 @@ def getIpList(ip: str):
6971
return [ip]
7072

7173

72-
def sendPacket(startNum, count, ip):
73-
port = startNum
74+
def sendPacket(startPort, count, ip):
75+
port = startPort
7476
while True:
7577
if stopThread:
7678
break
7779
Time = time.strftime('%H:%M:%S')
78-
if port % 1000 == 0 and verboseMode == "y":
79-
print(f"[{Time} I] Scaning port: {str(port)} ~ {str(port + 1000)}")
80+
if port % int(count / 5) == 0 and verboseMode == "y":
81+
print(f"[{Time} I] Scaning port: {str(port)} ~ {str(port + int(count / 5))}")
8082
send(IP(src=localHostIP, dst=ip) / UDP(sport=localHostPort, dport=port) /
8183
motdData,
8284
verbose=False)
83-
if port == 65535:
85+
if port == startPort + count - 1:
8486
if verboseMode == "y":
85-
print(f"[{Time} I] Port {startNum} ~ 65535 Done")
86-
while True:
87-
if threading.enumerate().__len__() == 2:
88-
break
89-
time.sleep(1)
90-
elif port == startNum + count - 1:
91-
if verboseMode == "y":
92-
print(f"[{Time} I] Port {startNum} ~ {startNum + count} Done")
87+
print(f"[{Time} I] Port {startPort} ~ {startPort + count} Done")
9388
break
9489
port += 1
9590

9691

9792
def startThreads():
9893
global stopThread
94+
if "-" in portRange:
95+
portRangeStart = int(portRange.split("-")[0])
96+
portRangeEnd = int(portRange.split("-")[1])
97+
else:
98+
portRangeStart = 0
99+
portRangeEnd = 65535
100+
portCount = portRangeEnd - portRangeStart
101+
if portCount < 7:
102+
portCount += 7
103+
singleThreadProcPort = (portCount - (portCount % 7)) / 7
104+
portStartList = []
105+
for i in range(7):
106+
portStartList.append(int(portRangeStart))
107+
portRangeStart += singleThreadProcPort
99108
ipList = getIpList(TargetAddr)
100109
for ip in ipList:
101110
time.sleep(1)
@@ -104,27 +113,14 @@ def startThreads():
104113
print(
105114
f"[{time.strftime('%H:%M:%S')} I] Scaning target: {ip}")
106115
print()
107-
t1 = threading.Thread(target=sendPacket, args=(0, 10000, ip))
108-
t2 = threading.Thread(target=sendPacket, args=(10000, 10000, ip))
109-
t3 = threading.Thread(target=sendPacket, args=(20000, 10000, ip))
110-
t4 = threading.Thread(target=sendPacket, args=(30000, 10000, ip))
111-
t5 = threading.Thread(target=sendPacket, args=(40000, 10000, ip))
112-
t6 = threading.Thread(target=sendPacket, args=(50000, 10000, ip))
113-
t7 = threading.Thread(target=sendPacket, args=(60000, 5535, ip))
114-
t1.setDaemon(True)
115-
t2.setDaemon(True)
116-
t3.setDaemon(True)
117-
t4.setDaemon(True)
118-
t5.setDaemon(True)
119-
t6.setDaemon(True)
120-
t7.setDaemon(True)
121-
t1.start()
122-
t2.start()
123-
t3.start()
124-
t4.start()
125-
t5.start()
126-
t6.start()
127-
t7.start()
116+
for portStart in portStartList:
117+
time.sleep(1)
118+
if portStart == portStartList[-1]:
119+
t1 = threading.Thread(target=sendPacket, args=(portStart, int(singleThreadProcPort + (portCount % 7)), ip))
120+
else:
121+
t1 = threading.Thread(target=sendPacket, args=(portStart, int(singleThreadProcPort), ip))
122+
t1.setDaemon(True)
123+
t1.start()
128124
tmpServerCount = serverCount
129125
if timeout:
130126
time.sleep(timeout)
@@ -220,8 +216,10 @@ def startThreads():
220216
elif re.search(b"eyser", data):
221217
geyserCount += 1
222218
sk_rec.close()
223-
except OSError:
224-
pass
219+
except OSError as info:
220+
if verboseMode == "y":
221+
print(f"[{time.strftime('%H:%M:%S')} R] {info}, skipped.")
222+
error += 1
225223
except Exception as info:
226224
print(f"[{time.strftime('%H:%M:%S')} R] {info}, skipped.")
227225
error += 1

0 commit comments

Comments
 (0)