-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathscanCore.py
198 lines (156 loc) · 4.82 KB
/
scanCore.py
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import asyncio
import json
import multiprocessing
import multiprocessing.pool
import random
import sys
import threading
import time
import traceback
import masscan as msCan
import pymongo
import utils
useWebHook, pingsPerSec, maxActive = False, 4800, 10
masscan_search_path = (
"masscan",
"/usr/bin/masscan",
"/usr/local/bin/masscan",
"/sw/bin/masscan",
"/opt/local/bin/masscan",
)
DISCORD_WEBHOOK = "discord.api.com/..."
try:
from privVars import *
except ImportError:
MONGO_URL = "mongodb+srv://..."
DSICORD_WEBHOOK = "discord.api.com/..."
if MONGO_URL == "mongodb+srv://...":
print("Please add your mongo url to privVars.py")
input()
sys.exit()
if useWebHook and DISCORD_WEBHOOK == "discord.api.com/...":
print("Please add your discord webhook to privVars.py")
input()
sys.exit()
# Setup
# ---------------------------------------------
DEBUG = True
time_start = time.time()
threads = []
pool = multiprocessing.pool.ThreadPool(maxActive)
client = pymongo.MongoClient(
MONGO_URL, server_api=pymongo.server_api.ServerApi("1")
) # type: ignore
db = client["mc"]
col = db["servers"]
utils = utils.utils(col, debug=DEBUG)
logger = utils.logger
finder = utils.finder
# Funcs
# ---------------------------------------------
def print(*args, **kwargs):
logger.print(*args, **kwargs)
def check(scannedHost):
# example host: "127.0.0.1": [{"status": "open", "port": 25565, "proto": "tcp"}]
try:
if scannedHost.replace(".", "").isdigit():
ip = scannedHost
else:
ip = list(scannedHost.keys())[0]
except Exception:
logger.print("Error parsing host: " + str(scannedHost))
logger.error(traceback.format_exc())
return
portsJson = (
scannedHost[ip]
if not isinstance(scannedHost, str)
else [{"status": "open", "port": 25565, "proto": "tcp"}]
)
for portJson in portsJson:
if portJson["status"] == "open":
if useWebHook:
return finder.check(
host=str(ip) + ":" + str(portJson["port"]),
webhook=DISCORD_WEBHOOK,
full=False,
)
else:
return finder.check(
host=str(ip) + ":" + str(portJson["port"]), full=False
)
else:
return
def scan(ip_list):
try:
scanner = msCan.PortScanner(masscan_search_path=masscan_search_path)
except msCan.PortScannerError:
print("Masscan not found, please install it")
return []
try:
import json
scanner.scan(
ip_list,
ports="25565-25577",
arguments="--max-rate {}".format(pingsPerSec / maxActive),
sudo=True,
)
result = json.loads(scanner.scan_result)
return list(result["scan"])
except OSError:
sys.exit(0)
except Exception:
logger.error(traceback.format_exc())
return []
def disLog(text, end="\r"):
if useWebHook:
try:
import requests
url = DSICORD_WEBHOOK
data = {"content": text + end}
requests.post(url, data=data)
except Exception:
logger.error(text + "\n" + traceback.format_exc())
async def threader(ip_range):
try:
ips = scan(ip_range)
if len(ips) > 0:
pool = multiprocessing.pool.ThreadPool(maxActive // 2)
pool.map(check, ips)
except OSError:
sys.exit(0)
except Exception:
logger.error(traceback.format_exc())
def crank(ip_range):
asyncio.run(threader(ip_range))
# Main
# ---------------------------------------------
# create a list of ipv4 ranges
ip_lists = []
for i in range(255):
for j in range(255):
ip_lists.append(f"{i}.{j}.0.0/16")
random.shuffle(ip_lists)
ip_lists = ip_lists[:1000] # remove for final version
time.sleep(0.5)
tStart = time.time()
normal = threading.active_count()
async def makeThreads():
# Create a thread for each list of IPs
for ip_list in ip_lists:
# check to make sure that more than 2*maxActive threads haven't been created in the last 1 second
if len(threads) >= maxActive * 2 and time.time() - tStart <= 1:
await asyncio.sleep(0.5)
logger.error(
"Too many threads spawned to quickly, exiting\nPlease check the console and logs for more details"
)
sys.exit()
t = threading.Thread(
target=crank, args=(ip_list,), name=f"Scan func thread: {ip_list}"
)
threads.append(t)
# If the number of active threads is greater than the max, sleep for 0.1 seconds
while threading.active_count() - normal >= maxActive:
await asyncio.sleep(0.1)
t.start()
if __name__ == "__main__":
asyncio.run(makeThreads())