-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.py
445 lines (385 loc) · 17.1 KB
/
Main.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
#! /usr/bin/python3
# coding=utf-8
import re
import csv
import time
import queue
import random
import timeit
import hashlib
import threading
from multiprocessing import cpu_count
from multiprocessing.pool import ThreadPool
from concurrent.futures import ThreadPoolExecutor, as_completed
from cassandra import InvalidRequest
from cassandra.query import tuple_factory
from cassandra.cluster import Cluster, ExecutionProfile, EXEC_PROFILE_DEFAULT
from cassandra.policies import WhiteListRoundRobinPolicy, DowngradingConsistencyRetryPolicy
TABLE = "passkull"
KEYSPACE = "passkullspace"
DELIMITERS = {",": ","}
LINE_FORMAT = "^[a-zA-Z0-9\-\._À-ԯ]+@[A-Za-z0-9\-\._]+\.[A-Za-z0-9\-\._]+{0}[^{0}]+$"
NUM_OF_WORKERS = 2 * cpu_count()
MAX_QUEUE = 100000
PASSKUL_FINISH = "PASSKUL_FINISH"
thread_lock = threading.Lock()
lines_queue = queue.Queue()
queue_lock = threading.Lock()
counter = 1
profile = ExecutionProfile(load_balancing_policy=WhiteListRoundRobinPolicy(['127.0.0.1']),
retry_policy=DowngradingConsistencyRetryPolicy(),
request_timeout=None,
row_factory=tuple_factory)
cluster = Cluster(execution_profiles={EXEC_PROFILE_DEFAULT: profile})
# FILES_PATH = r"/var/test/all_files/"
def run_func_in_threads_pool(func, args_lists):
'''
for each arg_list in args_lists opens a thread for func(*arg_list)
return list of results
'''
pool = ThreadPool()
threads_list = []
for args_tup in args_lists:
args_tup = tuple(args_tup)
threads_list.append(pool.apply_async(func, args_tup))
l = [t.get() for t in threads_list]
return l
def create_values(line, delimiter, keyspace, table):
global counter
global thread_lock
session = create_db_session()
line = line.strip()
line = line.replace(" ", "")
reMails = '[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-_]+\.[a-zA-Z0-9-.]+'
try:
try:
mail_address, password = line.split(sep=delimiter, maxsplit=1)
mail_address = re.findall(reMails, str(mail_address))[0]
username, domain = mail_address.split(sep="@", maxsplit=1)
except:
password, mail_address = line.split(sep=delimiter, maxsplit=1)
mail_address = re.findall(reMails, str(mail_address))[0]
username, domain = mail_address.split(sep="@", maxsplit=1)
keyp = f'{mail_address}:{password}'
key = str(hashlib.md5(keyp.encode()).hexdigest())
exist = search_if_ukey_in_database(value=key, session=session, keyspace=keyspace, table=table)
if not exist:
NTLM = str(hashlib.new('md4', password.encode('utf-16le')).hexdigest())
MD5 = str(hashlib.md5(password.encode()).hexdigest())
SHA1 = str(hashlib.sha1(password.encode()).hexdigest())
with thread_lock:
pre = session.prepare(f'INSERT INTO {keyspace}.{table} (ID, UserName, DomainName, Password, MD5, SHA1, NTLM, ukey)' + """ VALUES (?,?,?,?,?,?,?,?)""")
counter += 1
counter1 = counter
session.execute(pre.bind((counter1, username.lower(), domain.lower(), password, MD5, SHA1, NTLM, key)))
else:
pass
except Exception as e:
print(f'ERROR: {line}. {e}')
session.shutdown()
def search_if_ukey_in_database(value, keyspace, table):
session = create_db_session()
value1 = set()
value1.add(value)
# session.row_factory = tuple_factory
pre = session.prepare(f'SELECT * FROM {keyspace}.{table} ' + """WHERE ukey=? ALLOW FILTERING""")
rows = session.execute(pre.bind(value1))
# rows = session.execute(select_cli)
session.shutdown()
if len(rows.all()) > 0:
return True
else:
return False
def get_regex_csv(key, value, keyspace, table, hashsearch=False):
session = create_db_session()
value = value.lower()
if key == 'mail':
username, domainname = value.split('@')
pre = session.prepare(
f'SELECT username,domainname,password FROM {keyspace}.{table} ' + """WHERE UserName=? and DomainName=? ALLOW FILTERING""")
rows = session.execute(pre.bind((username, domainname))).all()
# select_cli = f"SELECT username,domainname,password,md5,ntlm,sha1,id FROM {keyspace}.{table} where username='{username}' and domainname='{domainname}' ALLOW FILTERING;"
elif key in ("MD5", "NTLM", "SHA1") and hashsearch:
value1 = set()
value1.add('%%{0}%'.format(value))
pre = session.prepare(
f'SELECT password,MD5,NTLM,SHA1 FROM {keyspace}.{table} WHERE {key}' + """ LIKE ? ALLOW FILTERING""")
rows = session.execute(pre.bind(value1))
rows = rows.all()
else:
value1 = set()
value1.add('%%{0}%'.format(value))
pre = session.prepare(
f'SELECT username,domainname,password FROM {keyspace}.{table} WHERE {key}' + """ LIKE ? ALLOW FILTERING""")
rows = session.execute(pre.bind(value1))
rows = rows.all()
session.shutdown()
return tuple(rows)
def initilize_export_file(username, hashsearch=False):
with open(f"/tmp/{username}_export.csv", "w", newline="") as export_file:
writer = csv.writer(export_file)
if hashsearch:
writer.writerow((("Password", "MD5", "NTLM", "SHA1")))
else:
writer.writerow((("User Name", "Domain Name", "Password")))
def create_csv_file_to_export(key, value, keyspace, table, username, hashsearch=False):
# session.row_factory = tuple_factory
session = create_db_session()
if key == 'hash':
keys = ("MD5", "NTLM", "SHA1")
else:
keys = []
keys.append(key)
lists_of_args = [[key, value, session, keyspace, table,hashsearch] for key in keys]
rows_lists = run_func_in_threads_pool(get_regex_csv, lists_of_args)
all_rows = []
for rows_list in rows_lists:
all_rows.extend(rows_list)
all_rows = list(set(all_rows))
# print(all_rows)
with open(f"/tmp/{username}_export.csv", "a", newline="") as export_file:
writer = csv.writer(export_file)
writer.writerows(all_rows)
session.shutdown()
def get_regex(key, value, keyspace, table):
session = create_db_session()
value = value.lower()
if key == 'mail':
try:
username, domainname = value.split('@')
pre = session.prepare(f'SELECT username,domainname,password,md5,ntlm,sha1,ukey FROM {keyspace}.{table} ' + """WHERE UserName=? and DomainName=? ALLOW FILTERING""")
rows = session.execute(pre.bind((username.lower(), domainname.lower()))).all()
# select_cli = f"SELECT username,domainname,password,md5,ntlm,sha1,id FROM {keyspace}.{table} where username='{username}' and domainname='{domainname}' ALLOW FILTERING;"
except:
rows = 'None'
else:
value1 = set()
value1.add('%%{0}%'.format(value))
pre = session.prepare(f'SELECT username,domainname,password,md5,ntlm,sha1,ukey FROM {keyspace}.{table} WHERE {key}' + """ LIKE ? ALLOW FILTERING""")
rows = session.execute(pre.bind(value1))
rows = rows.all()
session.shutdown()
return rows
def search_in_database_regex(key, value, keyspace, table, username, hashsearch=False):
# session.row_factory = tuple_factory
if key == 'hash':
keys = ("MD5", "NTLM", "SHA1")
else:
keys = []
keys.append(key)
lists_of_args = [[key, value, keyspace, table] for key in keys]
rows_lists = run_func_in_threads_pool(get_regex, lists_of_args)
all_rows = []
if isinstance(rows_lists, list):
for rows_list in rows_lists:
all_rows.extend(rows_list)
else:
all_rows.extend(rows_lists)
csv_args = [key, value, keyspace, table, username, hashsearch]
t = threading.Thread(target=create_csv_file_to_export, args=tuple(csv_args))
t.start() # todo: this will run in the background, and will not return a result
return all_rows
def get_count(keyspace, table, domain_name=None):
session = create_db_session()
if not domain_name:
count_cli = f'SELECT COUNT(ID) FROM {keyspace}.{table}'
domain_name = "total_count" # todo: change for the name you want for the total count (without domain_name)
count = session.execute(count_cli)
else:
domain = domain_name.lower()
domainname = set()
domainname.add('%%{0}%'.format(domain))
pre = session.prepare(f'SELECT COUNT(ID) FROM {keyspace}.{table} ' + """where domainname LIKE ? ALLOW FILTERING""")
count = session.execute(pre.bind(domainname))
total_count = str(count.all()[0][0])
session.shutdown()
return (domain_name, total_count)
def count_db_rows(keyspace, table, domain_name_list=('Gmail', 'Yahoo')):
session = create_db_session()
res_dict = {}
try:
lists_of_args = [[keyspace, table, None]] # count_cli
lists_of_args.extend([[keyspace, table, domain_name] for domain_name in domain_name_list])
count_tuples = run_func_in_threads_pool(get_count, lists_of_args)
for domain_name, count in count_tuples:
res_dict[domain_name] = count
except Exception as e:
if f"Keyspace {keyspace} does not exist" in str(e):
session.shutdown()
return {"total_count": "Error: PassDB DB does not exist"}
else:
print(e)
session.shutdown()
return res_dict
def delete_row(keyspace, table, id):
session = create_db_session()
id1 = set()
id1.add(id)
pre = session.prepare(f'DELETE FROM {keyspace}.{table} ' + """WHERE ukey=?""")
session.execute(pre.bind(id1))
session.shutdown()
def random_password(keyspace, table):
session = create_db_session()
username = None
password = None
try:
count_cli = f'SELECT COUNT(ID) FROM {keyspace}.{table}'
count = session.execute(count_cli)
count = int(count.all()[0][0])
password_of_the_day = None
while not password_of_the_day:
try:
rand_id = set()
rand_id.add(random.randint(1, count))
pre = session.prepare(f'SELECT username, domainname, password FROM {keyspace}.{table} ' + """WHERE id=? ALLOW FILTERING""")
password_of_the_day = session.execute(pre.bind(rand_id))
password_of_the_day = list(password_of_the_day.all())[0]
username = f'{password_of_the_day[0]}@{password_of_the_day[1]}'
password = password_of_the_day[2]
except:
username = None
password = None
except Exception as e:
if f"Keyspace {keyspace} does not exist" in str(e):
password = "Error: PassDB DB does not exist" # todo the error will return as password, and not printed to the screen here
session.shutdown()
return username, password
def initilize_cassandra(keyspace, table):
session = create_db_session()
print("creating keyspace...")
session.execute(f"CREATE KEYSPACE IF NOT EXISTS {keyspace} WITH replication = " + "{ 'class': 'SimpleStrategy', 'replication_factor': '1' }")
print("setting keyspace...")
session.set_keyspace(keyspace)
print("creating table...")
create_table_cli = f'CREATE TABLE IF NOT EXISTS {keyspace}.{table} (ID int, UserName text, DomainName text, Password text, MD5 text, SHA1 text, NTLM text, ukey text, PRIMARY KEY (ukey))'
session.execute(create_table_cli)
print("creating indexes...")
indexex_options = "USING 'org.apache.cassandra.index.sasi.SASIIndex' WITH OPTIONS = {'mode': 'CONTAINS', " \
"'analyzer_class': 'org.apache.cassandra.index.sasi.analyzer.StandardAnalyzer', " \
"'case_sensitive': 'false'}"
index_cli = [f'CREATE CUSTOM INDEX UserName_idx ON {keyspace}.{table} (UserName)',
f'CREATE CUSTOM INDEX DomainName_idx ON {keyspace}.{table} (DomainName)',
f'CREATE CUSTOM INDEX MD5_idx ON {keyspace}.{table} (MD5)',
f'CREATE CUSTOM INDEX SHA1_idx ON {keyspace}.{table} (SHA1)',
f'CREATE CUSTOM INDEX NTLM_idx ON {keyspace}.{table} (NTLM)',]
for cli in index_cli:
try:
cli = f'{cli} {indexex_options}'
session.execute(cli)
except InvalidRequest as err:
err = str(err).split('"')[1]
print(f'ERROR: {err}')
pass
session.shutdown()
def create_db_session():
profile = ExecutionProfile(load_balancing_policy=WhiteListRoundRobinPolicy(['127.0.0.1']),
retry_policy=DowngradingConsistencyRetryPolicy(),
request_timeout=None,
row_factory=tuple_factory)
cluster = Cluster(execution_profiles={EXEC_PROFILE_DEFAULT: profile})
session = cluster.connect()
return session
def upload_worker(index, delimiter, keyspace, table):
session = create_db_session()
try:
global lines_queue
global queue_lock
while True:
try:
with queue_lock:
is_empty = lines_queue.empty()
if is_empty:
time.sleep(2)
continue
with queue_lock:
line = lines_queue.get(timeout=0.1)
if line == PASSKUL_FINISH:
session.shutdown()
return
create_values(line, delimiter, session, keyspace, table)
except Exception as e:
print(f"Exception : {e} in thread {index}")
except Exception as e:
print(f"Error in thread {index}. {e}")
finally:
session.shutdown()
def read_file_and_upload(file_name, delimiter, keyspace, table):
initilize_cassandra(keyspace=keyspace, table=table)
global lines_queue
global queue_lock
# update counter to current max id
global counter
global thread_lock
with thread_lock:
try:
counter = int(get_max_id_for_count(keyspace, table)) + 1
except:
counter = 1
print("Start uploading...\n")
start = timeit.default_timer()
# open workers
workers = []
for i in range(NUM_OF_WORKERS):
worker_thread = threading.Thread(target=upload_worker, args=(i, delimiter, keyspace, table,))
workers.append(worker_thread)
worker_thread.start()
with open(file_name, encoding='utf-8', errors='ignore') as dump_file:
counter_file = 0
while True:
with queue_lock:
size = lines_queue.qsize()
if size >= MAX_QUEUE:
time.sleep(1)
continue
if counter_file % MAX_QUEUE == 0:
print("still upload.")
if lines_queue.qsize() < MAX_QUEUE:
line = dump_file.readline()
counter_file += 1
if not line:
# end of file
for i in range(NUM_OF_WORKERS):
with queue_lock:
lines_queue.put(PASSKUL_FINISH)
break
with queue_lock:
lines_queue.put(line)
print('Ulpoad Time: ', timeit.default_timer() - start)
print("Done...")
def get_max_id_for_count(keyspace, table):
session = create_db_session()
counter_cli = f"Select MAX(ID) FROM {keyspace}.{table}"
counter = session.execute(counter_cli)
session.shutdown()
return counter.one().system_max_id
def create_list_from_search_file(users_file_list):
users_list = set()
with open(users_file_list, encoding='utf-8', errors='ignore') as users_file:
for user in users_file.readlines():
try:
user = user.strip()
users_list.add(user)
except:
pass
return list(users_list)
def search_list_in_db(file_list, keyspace, table, username, hashsearch, key):
print('search list')
initilize_export_file(username, hashsearch=hashsearch)
users_list = list(set(create_list_from_search_file(file_list)))
args_list = [(key, user, keyspace, table, username, hashsearch) for user in users_list]
results_set = multi_search_list_in_db(func=search_in_database_regex, args_lists=args_list)
print('finish search list')
return list(results_set)
def multi_search_list_in_db(func, args_lists, num_workers=10):
tasks = []
results = []
with ThreadPoolExecutor(max_workers=num_workers) as threads_executor:
for args_tup in args_lists:
tasks.append(threads_executor.submit(func, *args_tup))
for task in as_completed(tasks):
try:
result = task.result()[0]
results.append(result)
except:
pass
return results