-
Notifications
You must be signed in to change notification settings - Fork 1
/
ssh-keyfinder.py
executable file
·383 lines (306 loc) · 13.4 KB
/
ssh-keyfinder.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
#!/usr/bin/python3
# A script to automate the extraction of SSH keys from a ssh-agent process memory dump
# Supports: RSA, DSA, ED25519, ECDSA
# Author: @Kracken256
# Version: 2.0
import os
import sys
from pwn import log
from typing import List
import binascii
import subprocess
# Finds the index of the string '/tmp/ssh-' in a file
gdb_key_unshield_cmd = """break main
break sshkey_free
r
set $miak = (struct sshkey *)sshkey_new(0)
set $shielded_private = (unsigned char *)malloc(1392)
set $shield_prekey = (unsigned char *)malloc(16384)
set $fd = fopen("/tmp/key-priv-shield.raw", "r")
call fread($shielded_private, 1, 1392, $fd)
call fclose($fd)
set $fd = fopen("/tmp/key-prekey-shield.raw", "r")
call fread($shield_prekey, 1, 16384, $fd)
call fclose($fd)
set $miak->shielded_private=$shielded_private
set $miak->shield_prekey=$shield_prekey
set $miak->shielded_len=1392
set $miak->shield_prekey_len=16384
call sshkey_unshield_private($miak)
bt
f 1
x *kp
call sshkey_save_private(*kp, "/tmp/key-extracted.pem", "", "comment", 0, \"\\x00\", 0)
k
q
"""
program_version = 'v2.0'
log.info(f"SSH Key Finder {program_version}")
log.info("Author: @Kracken256")
def find_magic_marker(filepath: str) -> int:
with open(filepath, "rb") as file:
chunk_size = 4096
chunk_index = 0
chunk = file.read(chunk_size)
while chunk:
index = chunk.find(b'/tmp/ssh-')
if index != -1:
return index + chunk_index
chunk = file.read(chunk_size)
chunk_index += chunk_size
return None
# Step back until the first pointer is found. Return as int.
def find_idtable(filepath: str, index: int) -> int:
with open(filepath, "rb") as file:
file.seek(index)
current_index = index
flip_flop = False
result_str = ""
while True:
current_index -= 1
file.seek(current_index)
byte = file.read(1)
if byte == b'\x00' and flip_flop:
pointer = int.from_bytes(binascii.unhexlify(
result_str), byteorder='big', signed=False)
return pointer
if byte != b'\x00':
flip_flop = True
result_str += byte.hex()
if index - current_index > 256:
return None
# This is flipped wierdly. It returns the data in the format of a string of hex bytes. But is works.
def pointer_tostring(pointer: int) -> str:
return f"0x{binascii.hexlify(pointer.to_bytes(8, byteorder='big')).decode('utf-8')}"
def gdb_run_command_parse(binary_path: str, coredump: str, pointer: int) -> bytes:
p = subprocess.Popen(['gdb', binary_path, coredump, '-ex', 'echo StartOfExec\n', '-ex', f'x/24gx {pointer}', '-ex', 'quit', '-q'],
stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=False)
out, err = p.communicate()
result = out.decode('utf-8')
result = result[result.find("StartOfExec")+len("StartOfExec"):].strip()
lines = result.split("\n")
new_result = b''
new_lines = []
for line in lines:
tmp = line[line.find(":")+1:].strip().replace(' ',
'').replace('\t', '').replace('0x', '')
new_lines.append(tmp)
new_result = ''.join(new_lines).replace('\n', '')
new_result = binascii.unhexlify(new_result)
return new_result
# Checks if a pointer is valid. Not the best way to do this.
def check_virtual_pointer(pointer: int) -> bool:
if pointer < 0xfffffffff:
return False
return True
# Save our sanity by checking the first pointer
def validate_idtable_ptr(coredump: str, binary_path: str, pointer: int) -> bool:
data = gdb_run_command_parse(binary_path, coredump, pointer)
if len(data) < 32:
return None
if data[7] == 0:
return None
other_pointer_1 = int.from_bytes(
data[8:16], byteorder='big', signed=False)
other_pointer_2 = int.from_bytes(
data[16:24], byteorder='big', signed=False)
if not check_virtual_pointer(other_pointer_1) or not check_virtual_pointer(other_pointer_2):
return None
return data[7]
def find_identity(coredump: str, binary_path, pointer: int, i: int) -> int:
# get the i'th identity pointer from list
data = gdb_run_command_parse(binary_path, coredump, pointer)
pointer = int.from_bytes(
data[16:24], byteorder='big', signed=False)
other_pointer = 0
iteration = 0
while iteration < i:
data = gdb_run_command_parse(binary_path, coredump, pointer)
pointer = int.from_bytes(
data[8:16], byteorder='big', signed=False)
other_pointer = int.from_bytes(
data[16:24], byteorder='big', signed=False)
iteration += 1
if not check_virtual_pointer(other_pointer):
return None
return other_pointer
def shred_file(filepath: str):
subprocess.run(['shred', '-uzf', filepath])
def find_sshkey(coredump: str, binary_path, pointer: int):
data = gdb_run_command_parse(binary_path, coredump, pointer)
if len(data) < 8:
return None
return pointer, data[7]
def dump_ecdsa(coredump: str, binary_path, sshkey_ptr: int) -> str:
raw_sshkey = None
raw_sshkey = gdb_run_command_parse(binary_path, coredump, sshkey_ptr)
shielded_private = int.from_bytes(
raw_sshkey[0x90-8:0x90], byteorder='big', signed=False)
shielded_private_size = int.from_bytes(
raw_sshkey[0x90:0x90 + 8], byteorder='big', signed=False)
shielded_prekey = int.from_bytes(
raw_sshkey[0x90+8:0x90+16], byteorder='big', signed=False)
shielded_prekey_size = int.from_bytes(
raw_sshkey[0x90+16:0x90+24], byteorder='big', signed=False)
# Dump the shielded private key from core
subprocess.run(['gdb', binary_path,
coredump, '-ex', f'dump memory /tmp/key-priv-shield.raw {shielded_private} {shielded_private+shielded_private_size}', '-ex', f'dump memory /tmp/key-prekey-shield.raw {shielded_prekey} {shielded_prekey+shielded_prekey_size}', '-ex', 'quit', '-q'], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
# Unshield the private key
sp = subprocess.Popen(["gdb", "./ssh-keygen", "-q"], stdin=subprocess.PIPE,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
sp.stdin.write(gdb_key_unshield_cmd.encode("utf-8"))
out, err = sp.communicate()
# Secure the private key
shred_file("/tmp/key-prekey-shield.raw")
shred_file("/tmp/key-priv-shield.raw")
with open("/tmp/key-extracted.pem", "r") as file:
data = file.read()
shred_file("/tmp/key-extracted.pem")
return data
def dump_rsa(coredump: str, binary_path, sshkey_ptr: int) -> str:
raw_sshkey = None
raw_sshkey = gdb_run_command_parse(binary_path, coredump, sshkey_ptr)
shielded_private = int.from_bytes(
raw_sshkey[0x90-8:0x90], byteorder='big', signed=False)
shielded_private_size = int.from_bytes(
raw_sshkey[0x90:0x90 + 8], byteorder='big', signed=False)
shielded_prekey = int.from_bytes(
raw_sshkey[0x90+8:0x90+16], byteorder='big', signed=False)
shielded_prekey_size = int.from_bytes(
raw_sshkey[0x90+16:0x90+24], byteorder='big', signed=False)
# Dump the shielded private key from core
# Dump the shielded prekey from core
subprocess.run(['gdb', binary_path,
coredump, '-ex', f'dump memory /tmp/key-priv-shield.raw {shielded_private} {shielded_private+shielded_private_size}', '-ex', f'dump memory /tmp/key-prekey-shield.raw {shielded_prekey} {shielded_prekey+shielded_prekey_size}', '-ex', 'quit', '-q'], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
# Unshield the private key
sp = subprocess.Popen(["gdb", "./ssh-keygen"], stdin=subprocess.PIPE,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
sp.stdin.write(gdb_key_unshield_cmd.encode("utf-8"))
sp.communicate()
shred_file("/tmp/key-prekey-shield.raw")
shred_file("/tmp/key-priv-shield.raw")
with open("/tmp/key-extracted.pem", "r") as file:
data = file.read()
shred_file("/tmp/key-extracted.pem")
return data
def dump_dsa(coredump: str, binary_path, sshkey_ptr: int) -> str:
raw_sshkey = None
raw_sshkey = gdb_run_command_parse(binary_path, coredump, sshkey_ptr)
shielded_private = int.from_bytes(
raw_sshkey[0x90-8:0x90], byteorder='big', signed=False)
shielded_private_size = int.from_bytes(
raw_sshkey[0x90:0x90 + 8], byteorder='big', signed=False)
shielded_prekey = int.from_bytes(
raw_sshkey[0x90+8:0x90+16], byteorder='big', signed=False)
shielded_prekey_size = int.from_bytes(
raw_sshkey[0x90+16:0x90+24], byteorder='big', signed=False)
# Dump the shielded private key from core
# Dump the shielded prekey from core
subprocess.run(['gdb', binary_path,
coredump, '-ex', f'dump memory /tmp/key-priv-shield.raw {shielded_private} {shielded_private+shielded_private_size}', '-ex', f'dump memory /tmp/key-prekey-shield.raw {shielded_prekey} {shielded_prekey+shielded_prekey_size}', '-ex', 'quit', '-q'], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
# Unshield the private key
sp = subprocess.Popen(["gdb", "./ssh-keygen"], stdin=subprocess.PIPE,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
sp.stdin.write(gdb_key_unshield_cmd.encode("utf-8"))
sp.communicate()
shred_file("/tmp/key-prekey-shield.raw")
shred_file("/tmp/key-priv-shield.raw")
with open("/tmp/key-extracted.pem", "r") as file:
data = file.read()
shred_file("/tmp/key-extracted.pem")
return data
def extract_keys(filepath: str, ssh_agent) -> List[str]:
keys: List[str] = []
# Check if filepaths are valid
if not os.path.exists(filepath):
log.error(f"Error: File {filepath} does not exist.")
return None
# Fine the magic marker
magic_marker = find_magic_marker(filepath)
if magic_marker is None:
log.warn("Error: Could not find magic marker.")
return None
log.info(f"Magic marker found at {magic_marker}")
# Find the first pointer
idtable_pointer = find_idtable(filepath, magic_marker)
if idtable_pointer is None:
log.warn("Error: Could not find idtable struct pointer.")
return None
# Validate the first pointer
num_keys = validate_idtable_ptr(filepath, ssh_agent, idtable_pointer)
if not num_keys:
log.warn("Error: First pointer is invalid.")
return None
log.info(
f"Found the idtable struct at {pointer_tostring(idtable_pointer)}")
log.success(f"Found {num_keys} ssh private keys.")
# Get second pointer
for i in range(1, num_keys+1):
key_type = 0
try:
second_pointer = find_identity(
filepath, ssh_agent, idtable_pointer, i)
if second_pointer is None:
log.warn(
"Could not find identity struct pointer (The key could be encrypted.)")
raise Exception("Could not find identity struct pointer.")
log.info(
f"Found identity struct {i} at {pointer_tostring(second_pointer)}")
# Get the key struct pointer
_, key_type = find_sshkey(
filepath, ssh_agent, second_pointer)
if key_type < 0 or key_type > 3:
log.warn("Error: Key type is invalid.")
raise Exception("Key type is invalid.")
# Display the sshkey type
if key_type == 0:
log.info("RSA key found")
# Dump the key data
rsa_ssh = dump_rsa(filepath, ssh_agent, second_pointer)
if not rsa_ssh:
log.warn("Error: Could not dump RSA key.")
raise Exception("Could not dump RSA key.")
log.success("RSA key extracted")
keys.append(rsa_ssh)
elif key_type == 1:
log.info("DSA key found")
# Dump the key data
dsa_ssh = dump_dsa(filepath, ssh_agent, second_pointer)
if not dsa_ssh:
log.warn("Error: Could not dump DSA key.")
raise Exception("Could not dump DSA key.")
log.success("DSA key extracted")
keys.append(dsa_ssh)
elif key_type == 3:
log.info("ECDSA key found")
ecdsa_ssh = dump_ecdsa(filepath, ssh_agent, second_pointer)
if not ecdsa_ssh:
log.warn("Error: Could not dump EXDSA key.")
raise Exception("Could not dump ECDSA key.")
log.success("ECDSA key extracted")
keys.append(ecdsa_ssh)
except:
log.warn(f"Error: Could not extract key of type {key_type}.")
continue
return keys
def main():
# Get filepaths (coredumps) from command line arguments
if (len(sys.argv) < 2):
print(
f"Usage: python3 {sys.argv[0]} <coredump>")
sys.exit(1)
filepaths = sys.argv[1]
ssh_agent = '/usr/bin/ssh-agent' # default ssh-agent path
keys = extract_keys(filepaths, ssh_agent)
if keys is None:
print("Error extracting SSH keys from the memory dumps.")
sys.exit(1)
if len(keys) == 0:
print("No SSH keys found in the memory dumps.")
sys.exit(1)
for key in keys:
print()
sys.stderr.write(key)
sys.exit(0)
if __name__ == "__main__":
main()